ATR trailing stop modify order error 1

 
Hi! Why am i getting modify order error 1 here?
void TrailStops(){

double ATR = iATR(NULL,0,14,0);
double TrailingStop = ATR * 2 ;
   

for (int i = OrdersTotal() - 1; i >=0; i--) {

OrderSelect(i, SELECT_BY_POS, MODE_TRADES);

   if ( OrderSymbol()==Symbol() )
   {
      if (OrderType() == OP_BUY) {
         if (Bid - OrderOpenPrice() > TrailingStop && OrderStopLoss() < Bid - TrailingStop ) {
                if (!OrderModify(OrderTicket(), OrderOpenPrice(), Bid - TrailingStop, OrderTakeProfit(), 0, Green)) {
                    Print("OrderModify error ",GetLastError());
                }
                return;
            }
      } else if (OrderType() == OP_SELL) {
         if (OrderOpenPrice() - Ask > TrailingStop && OrderStopLoss() > Ask + TrailingStop) {
           if (!OrderModify(OrderTicket(), OrderOpenPrice(), Ask + TrailingStop, OrderTakeProfit(), 0, Green)) {
               Print("OrderModify error ", GetLastError());
           }
        return;
    }
      }
   }
} 
}
 
Josip Marić:
Hi! Why am i getting modify order error 1 here?
Josip Marić:
Hi! Why am i getting modify order error 1 here?
 if (OrderType() == OP_BUY && OrderStopLoss()!=Bid - TrailingStop) 
                
 
Mehmet Bastem:
Unfortunately, error is still here...
 
Josip Marić:
Hi! Why am i getting modify order error 1 here?
void TrailStops(){

double ATR = iATR(NULL,0,14,0);
double TrailingStop = ATR * 2 ;
   

for (int i = OrdersTotal() - 1; i >=0; i--) {

OrderSelect(i, SELECT_BY_POS, MODE_TRADES);

   if ( OrderSymbol()==Symbol() )
   {
      if (OrderType() == OP_BUY && OrderStopLoss()!=Bid - TrailingStop


) {
         if (Bid - OrderOpenPrice() > TrailingStop && OrderStopLoss() < Bid - TrailingStop ) {
                if (!OrderModify(OrderTicket(), OrderOpenPrice(), Bid - TrailingStop, OrderTakeProfit(), 0, Green)) {
                    Print("BUY OrderModify error ",GetLastError()," SL ",OrderStopLoss()," New SL ",Bid - TrailingStop ," Ticket Number:",OrderTicket());
                }
                return;
            }
      } else if (OrderType() == OP_SELL && OrderStopLoss()!=Bid - TrailingStop


) {
         if (OrderOpenPrice() - Ask > TrailingStop && OrderStopLoss() > Ask + TrailingStop) {
           if (!OrderModify(OrderTicket(), OrderOpenPrice(), Ask + TrailingStop, OrderTakeProfit(), 0, Green)) {
              // Print("OrderModify error ", GetLastError());
               Print("SELL OrderModify error ",GetLastError()," SL ",OrderStopLoss()," New SL ",Bid - TrailingStop," Ticket Number:",OrderTicket());
           }
        return;
    }
      }
   }
} 
}

Could you please run this and upload the screenshot?

 
Mehmet Bastem:

Could you please run this and upload the screenshot?

here it is...



 
Josip Marić:

here it is...



ok. Please using Normalizedouble

Order Stop Lost is =1.0465 New Sl=1.04656565654.  When both are the same, you get error 1. For this you need to use the NormalizeDouble command.

The corrected code has been added.

void TrailStops(){

double ATR = iATR(NULL,0,14,0);
double TrailingStop = ATR * 2 ;
   

for (int i = OrdersTotal() - 1; i >=0; i--) {

OrderSelect(i, SELECT_BY_POS, MODE_TRADES);

   if ( OrderSymbol()==Symbol() )
   {
      if (OrderType() == OP_BUY && OrderStopLoss()!=NormalizeDouble((Bid - TrailingStop),Digits)


) {
         if (Bid - OrderOpenPrice() > TrailingStop && OrderStopLoss() < Bid - TrailingStop ) {
                if (!OrderModify(OrderTicket(), OrderOpenPrice(), NormalizeDouble((Bid - TrailingStop),Digits), OrderTakeProfit(), 0, Green)) {
                    Print("BUY OrderModify error ",GetLastError()," SL ",OrderStopLoss()," New SL ",Bid - TrailingStop ," Ticket Number:",OrderTicket());
                }
                return;
            }
      } else if (OrderType() == OP_SELL && OrderStopLoss()!=NormalizeDouble((Bid - TrailingStop),Digits)


) {
         if (OrderOpenPrice() - Ask > TrailingStop && OrderStopLoss() > Ask + TrailingStop) {
           if (!OrderModify(OrderTicket(), OrderOpenPrice(), NormalizeDouble((Ask + TrailingStop),Digits), OrderTakeProfit(), 0, Green)) {
              // Print("OrderModify error ", GetLastError());
               Print("SELL OrderModify error ",GetLastError()," SL ",OrderStopLoss()," New SL ",Bid - TrailingStop," Ticket Number:",OrderTicket());
           }
        return;
    }
      }
   }
} 
}
 
Mehmet Bastem:

ok. Please using Normalizedouble

Order Stop Lost is =1.0465 New Sl=1.04656565654.  When both are the same, you get error 1. For this you need to use the NormalizeDouble command.

The corrected code has been added.

Thank you so much that did the job! I fugured out that i need to normalize it... but i didn't know where to place it...


I have one more thing if you could help me.... This trailing stop when conditions are true, changes on every tick , which i dont like, because it basically sets stop  at price High.

I would like that it sets only on price Close. Is that possible?

 
Josip Marić:

Thank you so much that did the job! I fugured out that i need to normalize it... but i didn't know where to place it...


I have one more thing if you could help me.... This trailing stop when conditions are true, changes on every tick , which i dont like, because it basically sets stop  at price High.

I would like that it sets only on price Close. Is that possible?

I think i have done it... thank you anyhow!