Close All Orders Based On Dollar Profit

 

Can't seem to figure out why this isn't working, any help would be appreciated. I want to close all orders under that specific symbol with MagicNumber_3 if their total floating profit is greater than DollarTakeProfit_s3.

double CurrentPairProfit_s3 = CalculateProfit_s3();

      if (UseDollarTakeProfit_s3) {
      if (CurrentPairProfit_s3 > DollarTakeProfit_s3) {
         CloseThisSymbolAll_s3();
         Print("Closed all Strategy 3 trades under this pair because DollarTakeProfit was met!");
         NewOrdersPlaced_s3 = FALSE;
      }
   }
double CalculateProfit_s3() {
double Profit_s3 = 0;
for (cnt_s3 = OrdersTotal() - 1; cnt_s3 >= 0; cnt_s3--) {
OrderSelect(cnt_s3, SELECT_BY_POS, MODE_TRADES);
if (OrderSymbol() != Symbol() || OrderMagicNumber() != MagicNumber_3) continue;
if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber_3)
if (OrderType() == OP_BUY || OrderType() == OP_SELL) Profit_s3 += OrderProfit();
}
return (Profit_s3);
}
 
kinitex:

Can't seem to figure out why this isn't working, any help would be appreciated. I want to close all orders under that specific symbol with MagicNumber_3 if their total floating profit is greater than DollarTakeProfit_s3.

Perhaps the problem lies in function  CloseThisSymbolAll_s3()  ?  can you show the code for that ?
 
RaptorUK:
Perhaps the problem lies in function  CloseThisSymbolAll_s3()  ?  can you show the code for that ?


void CloseThisSymbolAll_s3() {
for (int trade_s3 = OrdersTotal() - 1; trade_s3 >= 0; trade_s3--) {
OrderSelect(trade_s3, SELECT_BY_POS, MODE_TRADES);
if (OrderSymbol() == Symbol()) {
if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber_3) {
if (OrderType() == OP_BUY) OrderClose(OrderTicket(), OrderLots(), Bid, (Slippage_s3*PipValue)*Point, DarkGray);
if (OrderType() == OP_SELL) OrderClose(OrderTicket(), OrderLots(), Ask, (Slippage_s3*PipValue)*Point, YellowGreen);
}
Sleep(1000);
}
}
}
 

kinitex:

CloseThisSymbolAll_s3() 


You haven't exactly said what isn't working,  is it sometimes working or not working at all ?


Some general comments:

  • you should check that the OrderSelect() has worked otherwise everything that follows it is not valid,  for example Orderticket(), OrderLots(), etc
  • you should check the return value from the OrderClose() and report the error to the log if it fails, more info here:  What are Function return values ? How do I use them ?
  • your EA sleeps for a second before trying to close the next order,  the Bid and Ask may be out of date by that time,  either do a RefreshRates() or use OrderClosePrice() in place of Bid and Ask 
  • there is no need to check OrderSymbol()  twice,  once will suffice  

 
   if (UseStoplossPct_s3) {
      if (CurrentPairProfit_s3 < 0.0 && MathAbs(CurrentPairProfit_s3) > StoplossPct_s3 / 100.0 * AccountEquityHigh_s3()) {
         CloseThisSymbolAll_s3();
         Print("Closed all trades under this pair because StoplossPCT was met");
         NewOrdersPlaced_s3 = FALSE;
      }
   }

This one works fine for me, so I dont see why the DollarTakeProfit wont.

By doesn't work I mean I see no sign of it working at all, even if I set the amount to close at $1.00 

 
kinitex:

This one works fine for me, so I dont see why the DollarTakeProfit wont.

By doesn't work I mean I see no sign of it working at all, even if I set the amount to close at $1.00 

Add some debugging Print statements so you can see what is going on . . .  I can't sere anything obvious but I don't know what type all the variables are or what they are set to, etc.  so I only have part of the picture to look at.
 

Thanks for the help Raptor, I figured it out. It was kind of working, but I added a line of code that screwed up some nested if statements which is what was causing not only this to function wrong but the way my entire EA trades. Thanks for the extra suggestions as well, really helped me clean up a lot of my code :)

 
kinitex:

Thanks for the help Raptor, I figured it out. It was kind of working, but I added a line of code that screwed up some nested if statements which is what was causing not only this to function wrong but the way my entire EA trades. Thanks for the extra suggestions as well, really helped me clean up a lot of my code :)

Glad to hear you are making progress :-)
 

One problem I noticed though is that it can take awhile for it to notice that CurrentPairProfit_s3 is greater than DollarTakeProfit_s3 and it sometimes won't close the trades until minutes later.

 
kinitex:

One problem I noticed though is that it can take awhile for it to notice that CurrentPairProfit_s3 is greater than DollarTakeProfit_s3 and it sometimes won't close the trades until minutes later.

Are you testing this in the Strategy Tester ?
 

No on a demo account as we speak, I have the profit being displayed on chart and it updates every tick.

Reason: