Set Take Profit without elimination of some orders

EnrichWave

Master Trader
May 15, 2018
325
82
74
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
18,532
1,355
144
Odesa
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.
 
  • 👍
Reactions: EnrichWave

EnrichWave

Master Trader
May 15, 2018
325
82
74
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());