Closing all open trades automatically results in losing price value even when the difference is positive profits - page 5

 
tompips2018:
...

Sooner or later you will have problems with this code, as noted clearly by Keith.

In contrary to Keith I really hope you will learn the hard way and lose money with it.

You should be ashamed by your attitude on this topic. You can count on me to remind it to anyone who will try to help you in the future.

 
tompips2018:

What's wrong with "I had one of my programmers check the 'prized' OrderClosePrice() the mod dude has been peddling and guess what - it doesn't need that stdlib at all upon testing :-)."

You do realise that you are the one ranting and wailing do you?

I believe that I have remained calm and collected throughout this thread.

I asked you

Why on Earth would you think that OrderClosePrice() requires that you use stdlib? You don't have to use it, you can just print the error code.

A simple question but you totally ignore it so that you can continue your ranting and wailing.

Nothing in my posts suggest that you have to use stdlib.

Answer the question

Why on Earth would you think that OrderClosePrice() requires that you use stdlib? You don't have to use it, you can just print the error code.

and without any more of your ranting and wailing please.
 
Alain Verleyen:

Sooner or later you will have problems with this code, as noted clearly by Keith.

In contrary to Keith I really hope you will learn the hard way and lose money with it.

You should be ashamed by your attitude on this topic. You can count on me to remind it to anyone who will try to help you in the future.

Alain, I did learn that a long time ago. All traders do. If you can see the whole code(of course i didn't post it all here :-D), you'll see the error checking in places to protect equity and all those stuffs, because I saw that thing happened already. If you backread, you'll know who started the nice conversation here ;-). The guy's just too absorbed to get a topper ;-) :)


My clients got manual control which I set in place. That's what they want. They have their team monitor their trades 24/5. They are no ordinary traders. They're already seasoned FX pros. They know their stuff. Whatever they do there, I'm out of it. I'm just a hired gun to work on the code. They know the ramifications, I told them that. It's their money, not mine, and all *disclaimers* of my work are in place. Just like all programmers here. I got paid, they already have the source code if they need to tweak it. The works. The ball's on their turf now.


In any case I said, this thread is done, I got the help I want. Thread closed. TYA.


How simple can that be? 


This will be my final post. We are all getting in circles now. I'm out of the carousel.

 
Keith Watford:

You do realise that you are the one ranting and wailing do you?

I believe that I have remained calm and collected throughout this thread.

I asked you

A simple question but you totally ignore it so that you can continue your ranting and wailing.

Nothing in my posts suggest that you have to use stdlib.

Answer the question

and without any more of your ranting and wailing please.

Ahhh, you're doing the "Broken Record" Syndrome again I see.


Here I got a code fix for that, lemme run that for you:


do while(BrokerRecordDetected == true)
{

        //Check if needle keeps on skipping
        if(NeedleJitters == true) //It's skipping repeatedly
        {
                RaiseNeedleOffRecord();
                RelocateNeedleToAnotherTrack();         

                if(RecordPlaysNormallyAgain == true)
                {
                        LeaveItRunningByItself();
                        ThatsHisMeTime();
                        
                }
        }

        if (!RecordRewindsToPlay && NobodysListening)
        {
                if(ItWontTurnOff == true)
                {
                        LowerVolumeToZero();
                        JustLeaveItAlone();
                        continue; //Just let it loop itself like an idiot
                }
        }
        else
        {
                Print("At last, it shuts up!");
                break;
        }
} // End Do While Loop



There. Fixed it. You can play yourself all you want unattended, we'll pretend  you're not ranting.


We'll just crank up the volume again if we need to listen to your wise counsel.


Done with this thread, you're on your own now.


CHEERS!


 

Still have angst? Go to the complaints dept. They can help you there.

Here, fill up this form below. You'll need it when you get there.

Vavush! Gotta have some fun tonight. Til next thread. See you in the next realm. ;-) 
Files:
 

Didn't see that last code coming, hahaHaha!


Got to copy that form.

 

Maybe somebody can help me out and tell me what I have done to deserve this torrent of childish abuse and sarcasm?

I have modified the code that I posted earlier using his new updated CloseAllTrades() function and ran it through the tester with the spread set at 20 points.

#property version   "1.00"
#property strict
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   for(int x=0;x<3;x++)
   OrderSend(_Symbol,OP_BUY,0.02,Ask,50,0,0,NULL,123,0,clrNONE);
   for(int x=0;x<3;x++)
   OrderSend(_Symbol,OP_SELL,0.01,Bid,50,0,0,NULL,123,0,clrNONE);
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   double profit=0;
   for(int x=OrdersTotal()-1;x>=0; x--)
    {
     if(OrderSelect(x,SELECT_BY_POS))
       profit+=OrderProfit();
    }
    Comment("Profit= "+DoubleToStr(profit,2));
   if(profit>=10)
     CloseAllTrades();
  }
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
#include <stdlib.mqh>
//+------------------------------------------------------------------+
// ======================================================================================= 
double CloseAllTrades()
{
   //Go through all open orders
   Print(">>>>>>>>>>>>>>>>>>>>>>>>Inside CloseAllTrades function<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
   int TotalRunningTrades = OrdersTotal();
   bool r = false;
   
   for (int i=TotalRunningTrades-1;  i >= 0; i--)
   {
      //Select a trade
      if (OrderSelect(i,SELECT_BY_POS) == true)
      {
         int BuyOrSell = OrderType();
         //check if trade belongs to current chart
         if (OrderSymbol() == Symbol())
         {
            if(BuyOrSell == 0) //It's a BUY
            {  //Close the trade orders
               //               ticket        lot volume          close price     slippage   arrow color
               r = OrderClose(OrderTicket(), OrderLots(),            Ask,           15,     clrAliceBlue);
            }
            if(BuyOrSell == 1) //It's a SELL
            {  //Close the trade orders
               //               ticket        lot volume          close price                 slippage   arrow color
               r = OrderClose(OrderTicket(), OrderLots(),            Bid,           15,     clrAliceBlue);
            }
         }
      }  
   } // End of for (int i=TotalRunningTrades-1; Loop
   return(r);
}   

Result

2 09:35:19.299 2020.03.03 00:00:00  ##TEST 1 test started

2 09:35:19.395 2020.03.03 00:05:00  ##TEST 1 EURUSD,H1: open #1 buy 0.02 EURUSD at 1.11294 ok

2 09:35:19.395 2020.03.03 00:05:00  ##TEST 1 EURUSD,H1: open #2 buy 0.02 EURUSD at 1.11294 ok

2 09:35:19.395 2020.03.03 00:05:00  ##TEST 1 EURUSD,H1: open #3 buy 0.02 EURUSD at 1.11294 ok

2 09:35:19.395 2020.03.03 00:05:00  ##TEST 1 EURUSD,H1: open #4 sell 0.01 EURUSD at 1.11274 ok

2 09:35:19.395 2020.03.03 00:05:00  ##TEST 1 EURUSD,H1: open #5 sell 0.01 EURUSD at 1.11274 ok

2 09:35:19.395 2020.03.03 00:05:00  ##TEST 1 EURUSD,H1: open #6 sell 0.01 EURUSD at 1.11274 ok

0 09:35:27.408 2020.03.03 17:00:17  ##TEST 1 EURUSD,H1: >>>>>>>>>>>>>>>>>>>>>>>>Inside CloseAllTrades function<<<<<<<<<<<<<<<<<<<<<<<<<<<<

3 09:35:27.408 2020.03.03 17:00:17  ##TEST 1 EURUSD,H1: OrderClose error 138

3 09:35:27.409 2020.03.03 17:00:17  ##TEST 1 EURUSD,H1: OrderClose error 138

3 09:35:27.409 2020.03.03 17:00:17  ##TEST 1 EURUSD,H1: OrderClose error 138

3 09:35:27.409 2020.03.03 17:00:17  ##TEST 1 EURUSD,H1: OrderClose error 138

3 09:35:27.409 2020.03.03 17:00:17  ##TEST 1 EURUSD,H1: OrderClose error 138

3 09:35:27.409 2020.03.03 17:00:17  ##TEST 1 EURUSD,H1: OrderClose error 138

0 09:35:27.409 2020.03.03 17:00:17  ##TEST 1 EURUSD,H1: >>>>>>>>>>>>>>>>>>>>>>>>Inside CloseAllTrades function<<<<<<<<<<<<<<<<<<<<<<<<<<<<

3 09:35:27.409 2020.03.03 17:00:17  ##TEST 1 EURUSD,H1: OrderClose error 138

3 09:35:27.409 2020.03.03 17:00:17  ##TEST 1 EURUSD,H1: OrderClose error 138

3 09:35:27.409 2020.03.03 17:00:17  ##TEST 1 EURUSD,H1: OrderClose error 138

3 09:35:27.409 2020.03.03 17:00:17  ##TEST 1 EURUSD,H1: OrderClose error 138

3 09:35:27.409 2020.03.03 17:00:17  ##TEST 1 EURUSD,H1: OrderClose error 138

3 09:35:27.409 2020.03.03 17:00:17  ##TEST 1 EURUSD,H1: OrderClose error 138

0 09:35:27.409 2020.03.03 17:00:18  ##TEST 1 EURUSD,H1: >>>>>>>>>>>>>>>>>>>>>>>>Inside CloseAllTrades function<<<<<<<<<<<<<<<<<<<<<<<<<<<<

3 09:35:27.409 2020.03.03 17:00:18  ##TEST 1 EURUSD,H1: OrderClose error 138

3 09:35:27.409 2020.03.03 17:00:18  ##TEST 1 EURUSD,H1: OrderClose error 138

3 09:35:27.409 2020.03.03 17:00:18  ##TEST 1 EURUSD,H1: OrderClose error 138

3 09:35:27.409 2020.03.03 17:00:18  ##TEST 1 EURUSD,H1: OrderClose error 138

3 09:35:27.409 2020.03.03 17:00:18  ##TEST 1 EURUSD,H1: OrderClose error 138

3 09:35:27.409 2020.03.03 17:00:18  ##TEST 1 EURUSD,H1: OrderClose error 138

0 09:35:27.409 2020.03.03 17:00:18  ##TEST 1 EURUSD,H1: >>>>>>>>>>>>>>>>>>>>>>>>Inside CloseAllTrades function<<<<<<<<<<<<<<<<<<<<<<<<<<<<

3 09:35:27.409 2020.03.03 17:00:18  ##TEST 1 EURUSD,H1: OrderClose error 138

3 09:35:27.409 2020.03.03 17:00:18  ##TEST 1 EURUSD,H1: OrderClose error 138

3 09:35:27.409 2020.03.03 17:00:18  ##TEST 1 EURUSD,H1: OrderClose error 138

3 09:35:27.409 2020.03.03 17:00:18  ##TEST 1 EURUSD,H1: OrderClose error 138

3 09:35:27.409 2020.03.03 17:00:18  ##TEST 1 EURUSD,H1: OrderClose error 138

3 09:35:27.409 2020.03.03 17:00:18  ##TEST 1 EURUSD,H1: OrderClose error 138

0 09:35:27.409 2020.03.03 17:00:18  ##TEST 1 EURUSD,H1: >>>>>>>>>>>>>>>>>>>>>>>>Inside CloseAllTrades function<<<<<<<<<<<<<<<<<<<<<<<<<<<<

3 09:35:27.409 2020.03.03 17:00:18  ##TEST 1 EURUSD,H1: OrderClose error 138

3 09:35:27.409 2020.03.03 17:00:18  ##TEST 1 EURUSD,H1: OrderClose error 138

3 09:35:27.409 2020.03.03 17:00:18  ##TEST 1 EURUSD,H1: OrderClose error 138

3 09:35:27.409 2020.03.03 17:00:18  ##TEST 1 EURUSD,H1: OrderClose error 138

3 09:35:27.409 2020.03.03 17:00:18  ##TEST 1 EURUSD,H1: OrderClose error 138

3 09:35:27.409 2020.03.03 17:00:18  ##TEST 1 EURUSD,H1: OrderClose error 138


Oh dear!  Not 1 trade closed.

At least with the previous version some of the trades closed.

Now substitute the CloseAllTrades() function

void CloseAllTrades()
{
   for(int x=OrdersTotal()-1; x>=0; x--)
     {
      if(OrderSelect(x,SELECT_BY_POS) && OrderSymbol()==_Symbol)
        {
         if(!OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),15,clrNONE))
            Print(TimeCurrent()," Error with Closing Order #"+(string)OrderTicket()+(string)(GetLastError()));
        }
     }

Result.

2 09:44:21.312 2020.03.03 00:00:00  ##TEST 2 test started

2 09:44:21.333 2020.03.03 00:05:00  ##TEST 2 EURUSD,H1: open #1 buy 0.02 EURUSD at 1.11294 ok

2 09:44:21.333 2020.03.03 00:05:00  ##TEST 2 EURUSD,H1: open #2 buy 0.02 EURUSD at 1.11294 ok

2 09:44:21.333 2020.03.03 00:05:00  ##TEST 2 EURUSD,H1: open #3 buy 0.02 EURUSD at 1.11294 ok

2 09:44:21.333 2020.03.03 00:05:00  ##TEST 2 EURUSD,H1: open #4 sell 0.01 EURUSD at 1.11274 ok

2 09:44:21.333 2020.03.03 00:05:00  ##TEST 2 EURUSD,H1: open #5 sell 0.01 EURUSD at 1.11274 ok

2 09:44:21.333 2020.03.03 00:05:00  ##TEST 2 EURUSD,H1: open #6 sell 0.01 EURUSD at 1.11274 ok

2 09:44:29.286 2020.03.03 17:00:17  ##TEST 2 EURUSD,H1: close #6 sell 0.01 EURUSD at 1.11274 at price 1.11696

2 09:44:29.286 2020.03.03 17:00:17  ##TEST 2 EURUSD,H1: close #5 sell 0.01 EURUSD at 1.11274 at price 1.11696

2 09:44:29.286 2020.03.03 17:00:17  ##TEST 2 EURUSD,H1: close #4 sell 0.01 EURUSD at 1.11274 at price 1.11696

2 09:44:29.287 2020.03.03 17:00:17  ##TEST 2 EURUSD,H1: close #3 buy 0.02 EURUSD at 1.11294 at price 1.11676

2 09:44:29.287 2020.03.03 17:00:17  ##TEST 2 EURUSD,H1: close #2 buy 0.02 EURUSD at 1.11294 at price 1.11676

2 09:44:29.287 2020.03.03 17:00:17  ##TEST 2 EURUSD,H1: close #1 buy 0.02 EURUSD at 1.11294 at price 1.11676


All closed with no errors.

 

I've edited his code  and replaced the if statements with "OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),15,clrNONE);"

double CloseAllTrades()
{
   //Go through all open orders
   Print(">>>>>>>>>>>>>>>>>>>>>>>>Inside CloseAllTrades function<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
   int TotalRunningTrades = OrdersTotal();
   bool r = false;
   
   for (int i=TotalRunningTrades-1;  i >= 0; i--)
   {
      //Select a trade
      if (OrderSelect(i,SELECT_BY_POS) == true)
      {
         int BuyOrSell = OrderType();
         //check if trade belongs to current chart
         if (OrderSymbol() == Symbol())
         {
            r = OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),15,clrNONE);
         }
         if((r == false) && (Grand_Total >= PriceReferenceToCloseAllTrades))
         {
            Alert("PROFIT POINT REACHED! MANUAL INTERVENTION MAY BE REQUIRED.");
         }
         else
         {   
            Print("Check the Charts. Error flag occurred.");
         }   
      }  
   } // End of for (int i=TotalRunningTrades-1; Loop
   return(r);
}

It's still getting the 138 error (requote?) nonetheless; So it's not there on that part. Possibly somewhere else in his code that is causing it.


But regardless I think it's clear he said his clients got manual control when errors arises and they are alerted to override it and they're happy with it. Well those are his clients and his code and he got paid for it, I guess let's leave him at that.

 
Jack Olin:

I've edited his code  and replaced the if statements with "OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),15,clrNONE);"

It's still getting the 138 error (requote?) nonetheless; So it's not there on that part. Possibly somewhere else in his code that is causing it.


But regardless I think it's clear he said his clients got manual control when errors arises and they are alerted to override it and they're happy with it. Well those are his clients and his code and he got paid for it, I guess let's leave him at that.

Both approaches are correct, the OP's work though is the long cut of the short cut, but it works - what these people failed to consider is the hardware capabilities they are running that program into. Memory shoots to a 100% when backtested. I ran both approaches on a small 2GB scaled down netbook with compact slipstreamed Vista OS and the 138 and some other esoteric errors start popping everywhere. Transferring both on a Windows 7 with 8GB memory, errors are nowhere to be seen and Memory process stayed well within acceptable levels. Only when I tweak my machine to force memory and CPU to go a 100% full throttle did the 138 error starts to appear albeit sporadically. Maybe the guy's client got real neat hardware running in their office to accommodate his program's memory intensive process, - well, it works for him and/or his client, that's what matters.
Reason: