Trailing stop does not work in a fast markt- I need Help

 

I have noticed if the price moves up/down fast trailing stop will not work once the price reaches a broker's stop level. For example, Stop level =20, Opening price=1.30000, Trade Type=buy. As per trailing rule if the BID price reaches 1.30020, trailing stop must trigger and 1.30000 be placed as your stop loss. Then for each increase of BID PRICE the same will be added to stop loss. In fact, if bid price is less than( opening price + stop level )your trailing will not work.

Unfortunately, when the price moves up fast the rule up DOES NOT WORK. Does anyone have any solution for this problem? Please see my code below and SLP refers to broker's stoplevel:

double slbuy=bid-POINT*TrailingStop;

double slsell=ask+POINT*TrailingStop;

if(TrailingStop>0)

{

for(int i=0;i<OrdersTotal();i++)

OrderSelect(i,SELECT_BY_POS,MODE_TRADES);

double value=OrderTakeProfit();

if(OrderSymbol()==Symbol())

{

if(OrderType()==OP_BUY && bid-OrderOpenPrice()>POINT*TrailingStop)

{

if(OrderStopLoss()==0 || OrderStopLoss()<slbuy)

{

if(OrderProfit()<=SLP) slbuy=OrderOpenPrice();

OrderModify(OrderTicket(),OrderOpenPrice(),slbuy,value,0,Red);

Alert("Stoploss change is accepted by the Broker=",DoubleToStr(slbuy,Digits));

}

}

else if(OrderType()==OP_SELL && OrderOpenPrice()-ask>POINT*TrailingStop)

{

if(OrderStopLoss()==0 || OrderStopLoss()>slsell )

{

if(OrderProfit()<=SLP)slsell=OrderOpenPrice();

OrderModify(OrderTicket(),OrderOpenPrice(),slsell,value,0,Red);

}

 

I'm don't quite understand trailing_stops myself :), However, For FAST moving prices, I'll assume the reason why your trailing stop does not work sometimes is because your EA is still processing when price blows through your stop_level. It should however set the trail when the price settles.

I'm going to throw my hat in the ring as well on this question. Can anyone who really know how trailing stops work please answer this concern. The code below is what I use for trailing_stops which works great in demo testing but sometimes generates errors in back_testing. The stop_level for my broker is 10 Pips. I'm able to submit initial stop_loss along with my order. I really want to keep it so that the trailing stop gets placed upon the first pip in profit.

//#####################################======############################################
//#####################################======############################################
//|- Order_TrailStop.mqh
//|Ubzen
//|
//#####################################======############################################
//#####################################======############################################
void Order_TrailStop(int Trail_Stop,int Magic_Number,double Price_Buy,
double Price_Sell){
//#####################################======############################################
//#####################################======############################################
for(int Loop=0; Loop<OrdersTotal(); Loop++){
   if(OrderSelect(Loop,SELECT_BY_POS,MODE_TRADES)==true)
      if(OrderMagicNumber()==Magic_Number){
         //Trailing Stop for Buy
         if(OrderType()==OP_BUY && Price_Sell-OrderOpenPrice()>=Point){
            if(OrderStopLoss()<Price_Sell-Trail_Stop*Point){
               OrderModify(OrderTicket(),OrderOpenPrice(),
               NormalizeDouble(Price_Sell,Digits)-Trail_Stop*Point,
               OrderTakeProfit(),0,White);}}
         //Trailing Stop for Sell
         if(OrderType()==OP_SELL && Price_Buy-OrderOpenPrice()<=Point){
            if(OrderStopLoss()>Price_Buy+Trail_Stop*Point){
               OrderModify(OrderTicket(),OrderOpenPrice(),
               NormalizeDouble(Price_Buy,Digits)+Trail_Stop*Point,
               OrderTakeProfit(),0,White);}}}}
//#####################################======############################################
//#####################################======############################################
return(0);}
//#####################################======############################################
//#####################################======############################################
 

Hi:

Your trailing part is almost the same as mine. I have tried to find out what errors causing the trailing stop failis and normally error 130 and 136 occurs, but, it does not make any sense. I have heard there is a stop loss called forced stop loss. That is, you program your trailing stop in a way that your broker must honer your new stop loss. The problem we are facing at the moment is that when our EA makes a modifying stop loss request the broker does not honer it

.

 

I see, I'm still working in demo mode therefore, I don't have experience with Broker rejects. However, error 130 means: 

"Stops are too close, or prices are ill-calculated or unnormalized (or in the open price of a pending order). The attempt can be repeated only if the error occurred due to the price obsolescense. After 5-second (or more) delay, it is necessary to refresh data using the RefreshRates function and make a retry. If the error does not disappear, all attempts to trade must be stopped, the program logic must be changed."

 

Starting with the easy fix first "ill-calculated", I'll recommend you give the trailing stop some room, try double checking the calculations and make sure it's not within about 12 pips of current market action.

Second  "unnormalized" this is my best guess. Within all my codes, I make sure to Normalize the Ask/Bid. example 

//+----------Price_Buy Function
double Price_Buy(double Price_Buy){
Price_Buy=NormalizeDouble(Ask,Digits);
return(Price_Buy);}
//+----------Price_Sell Function
double Price_Sell(double Price_Sell){
Price_Sell=NormalizeDouble(Bid,Digits);
return(Price_Sell);}

Third "pending order"  make sure it not trying to modify Pending Orders as this cannot be done.

And Lastly, If you're in a live environment then I'll recommend adding some Error Handling to your codes. For example something like if(broker is busy) sleep and retry in 5 seconds. You can find such codes by doing a search. Good Luck.

 

Ps.

136    No quotes. The broker has not supplied with prices or refused, for any reason (for example, no prices at the session start, unconfirmed prices, fast market). After 5-second (or more) delay, it is necessary to refresh data using the RefreshRates function and make a retry. 

Link for error codes here: https://docs.mql4.com/trading/errors

 

Hi:

Thank you very much for your in depth help. I will try your advice and see what will happen. however, I have to wait for the market to open.

 

  1. Your order select loop only selects the last order, not the EA's, and the code fails when there is no order.
    for(int pos = OrdersTotal() - 1; pos >= 0; pos--) if (
        OrderSelect(pos, SELECT_BY_POS)            // Only my orders w/
    &&  OrderMagicNumber()  >= Magic.Number        // my magic number
    &&  OrderSymbol()       == Symbol() ){         // and period and symbol
       if(OrderType()==OP_BUY && bid-OrderOpenPrice()>POINT*TrailingStop) ...
    }

  2. if(OrderType()==OP_BUY && bid-OrderOpenPrice()>POINT*TrailingStop)
    Trailing stops move up only. you need to compare to the current stop so that it trails.
    if(OrderType()==OP_BUY && bid-OrderStopLoss() > POINT*TrailingStop)
    

Reason: