Set Take Profit without elimination of some orders

Discussion in 'MetaTrader Scripts' started by EnrichWave, Dec 7, 2018.

  1. EnrichWave

    EnrichWave Active Trader

    38
    2
    34
    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)
    Code (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: Dec 7, 2018
  2. Enivid

    Enivid Administrator Staff Member

    10,724
    400
    114
    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.
     
    EnrichWave likes this.
  3. EnrichWave

    EnrichWave Active Trader

    38
    2
    34
    Yeah @Enivid really I forgot to look and post that error messages, I'll update you once the market open.

    you mean I've to write this code after OrderModify() to print the GetLastError()?
    Code (MQL4):
    Print("OrderModify Error: " ,GetLastError());
     
  4. Enivid

    Enivid Administrator Staff Member

    10,724
    400
    114
    EnrichWave likes this.

Share This Page