Set Take Profit without elimination of some orders

EnrichWave

Active Trader
May 15, 2018
57
6
34
India
Dear fellow traders.
I use the following Script to set Take Profit at current Bid Price (My broker having Stops level floating point at 0 point) of Buy order.
But If I have 10-15 open trades and I drag the Script. It is missing to set TP for couple of orders. Any one can modify this? (If the price changed we can set SL price at that price so it can be done, But I don't know how to code that)
MQL4:
int start()
{
  int digits   = MarketInfo(Symbol(),MODE_DIGITS);
   for(int i=OrdersTotal()-1;i>=0;i--)
   {
      if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
         continue;
      if(OrderSymbol()!=Symbol())
         continue;
 
      RefreshRates();
 
      if((OrderType()==OP_BUY))
          OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),Bid,OrderExpiration(),CLR_NONE);
 
   }
   return(0);
  }
 
Last edited:

Enivid

Administrator
Staff member
Nov 30, 2008
14,152
633
144
Odessa
www.earnforex.com
What errors do you get in Experts/Journal tab for the failed modifications?
I would also check OrderModify() for return value (true/false) and use GetLastError() to print the error code for failed cases.
Also, moving RefreshRates() inside the condition scope just before calling OrderModify() could help with using the current latest Bid.
 
  • Like
Reactions: EnrichWave

EnrichWave

Active Trader
May 15, 2018
57
6
34
India
What errors do you get in Experts/Journal tab for the failed modifications?
Yeah @Enivid really I forgot to look and post that error messages, I'll update you once the market open.

I would also check OrderModify() for return value (true/false) and use GetLastError() to print the error code for failed cases.
you mean I've to write this code after OrderModify() to print the GetLastError()?
MQL4:
Print("OrderModify Error: " ,GetLastError());