Error with EA > Update Stoploss Fail Error 0/4108

 

Attached is my code of EA.



The first trade works fine and I left the EA on and when I returned, I got the following alerts/errors.

"Order 3764742 failed to update stop. Error 0/4108" The alert box was filled with this exact message (some error 0 and some error 4108, with the same order number)

I believe the error comes from trying to delete an already deleted order. I don't know why the Ea keeps trying to update stoploss.

I am confused as to how to fix this. Someone please give me a hand? Thanks.

 
skyhr:

Attached is my code of EA.



The first trade works fine and I left the EA on and when I returned, I got the following alerts/errors.

"Order 3764742 failed to update stop. Error 0/4108" The alert box was filled with this exact message (some error 0 and some error 4108, with the same order number)

I believe the error comes from trying to delete an already deleted order. I don't know why the Ea keeps trying to update stoploss.

I am confused as to how to fix this. Someone please give me a hand? Thanks.



Ok, I figured out error 0 - it is an error with the updatestoploss function. But the main problem of the EA not recognizing that the trade hit a stoploss remains. The method of recognizing whether a trade exists or not is the main problem.
 
  1. datetime diff = TimeLocal() - last_time;
       if (TimeMinute(diff) >= 15)
       {
          last_time = iTime(NULL,PERIOD_M15,0);
          first_tick = true; //this is the first tick of the new fifteen minute bar
          MessageBox("First tick of 15 min","Tick info");
    
    This isn't calculating the first tick of a new bar, it's getting 15 minutes past the last time and that will drift away from the start of a bar. If you want the new bar use Time[0]. Also you never reset last_time.
    if (Time[0] > time0) { time0=Time[0]; ...

  2. bool trade_closed = false;
       if (order_Ticket >= 0)
    
    Since tickets are never zero, do you mean >0 here.
  3. int slippage = 5;            //PIP slippage
    OrderSend(Symbol(),OP_BUY,lots,Ask,slippage, 0, 0, 
    For five digit brokers TP, SL, and slippage must be adjusted from pips to points.
    //++++ These are adjusted for 5 digit brokers.
    double  pips2points,    // slippage  3 pips    3=points    30=points
            pips2dbl;       // Stoploss 15 pips    0.0015      0.00150
    int     Digits.pips;    // DoubleToStr(dbl/pips2dbl, Digits.pips)
    int init(){
        if (Digits == 5 || Digits == 3){    // Adjust for five (5) digit brokers.
                    pips2dbl    = Point*10; pips2points = 10;   Digits.pips = 1;
        } else {    pips2dbl    = Point;    pips2points =  1;   Digits.pips = 0; }
        // OrderSend(... Slippage.Pips * pips2points, Bid - StopLossPips * pips2dbl
    

  4. order_Ticket = OrderSend(Symbol(),OP_BUY,lots,Ask,slippage, 0, 0..
    if (order_Ticket < 0)..
    else{
        OrderSelect(order_Ticket, SELECT_BY_TICKET);
        if (!OrderModify(order_Ticket, OrderOpenPrice(), Ask-stop_loss, OrderTakeProfit(), 0, Blue))
                  
    You must RefreshRates() between server calls or Ask/Bid will not be correct. You open a buy at the Ask, but stops are at the Bid. You never check that stop_loss is at least MarketInfo(Symbol(), MODE_STOPLEVEL)*Point.
  5. void FlattenAccount(){ ...
          case OP_BUY       : result = OrderClose( OrderTicket(), OrderLots(), Bid, slippage, Red ); 
    Instead of case/Bid/Ask you can just use OrderClosePrice()
  6.    bool trade_closed = false;
       if (order_Ticket >= 0){
          if (OrderSelect(order_Ticket,SELECT_BY_TICKET,MODE_TRADES)){
             trade_exists = true;      }
          else if (OrderSelect(order_Ticket,SELECT_BY_TICKET,MODE_HISTORY)) {
             trade_exists = false;
             trade_closed = true;      }
          else      {
             order_Ticket = -1;
             return (-1);      }
       }
       else
          trade_exists = false;
    
    instead try
       bool trade_closed = false;
            trade_exists = false;
       if (order_Ticket > 0){
          if (OrderSelect(order_Ticket,SELECT_BY_TICKET,MODE_TRADES)){
             trade_exists = true;      }
          else if (OrderSelect(order_Ticket,SELECT_BY_TICKET,MODE_HISTORY)) {
             trade_exists = false;
             trade_closed = true;      }
          else      {
             order_Ticket = -1;
             return (-1);      }
       }
    

Reason: