Trailling Stop Failure on Boom and Crash Indices (Invalid stops)

 
#include <Trade\Trade.mqh> //Instatiate Trades Execution Library
CTrade         trade; // Trades Info and Executions library
                                                                  
void OnTick()
  {
    double Ask =NormalizeDouble(SymbolInfoDouble(Symbol(),SYMBOL_ASK),_Digits);
    double Bid =NormalizeDouble(SymbolInfoDouble(Symbol(),SYMBOL_BID),_Digits);
    double sl=NormalizeDouble((Ask-5000*_Point),_Digits);
    double tp=NormalizeDouble((Ask+10000*_Point),_Digits);
    if(PositionsTotal()<2){
    
    trade.Buy(0.2,Symbol(),Ask,sl-5000*_Point,tp+10000*_Point,NULL);
    }
      CheckTrailingStop(Ask);                                                                                                                                                                                                                               

  }
void CheckTrailingStop(double ask){
double SL = NormalizeDouble((ask-4000*_Point),_Digits);

   for(int i=PositionsTotal()-1;i>=0;i--)
     {
      string symbol =PositionGetSymbol(i);
      if(symbol==_Symbol){
     ulong PositionTicket =PositionGetInteger(POSITION_TICKET);
     double CurrStoploss = PositionGetDouble(POSITION_SL);
         if(CurrStoploss<SL){
         trade.PositionModify(PositionTicket,(CurrStoploss+1000*_Point),0);
         } 
      }///end symbol if loop
     
     }//end for loop
}
//end trailstop function
Am getting error invalid stops when i try to trailstoploss 
 
You need to normalize SL and TP prices to tick size.

Normalizing prices as double to digits it's not always effective.
 
Fabio Cavalloni #:
You need to normalize SL and TP prices to tick size.

Normalizing prices as double to digits it's not always effective.

what do you mean normalize them to tick sizes

 
ZhuwaoAbel:
trade.PositionModify(PositionTicket,(CurrStoploss+1000*_Point),0);
How can you be sure the current price is above the new stop level you are applying.
 
ZhuwaoAbel #:

what do you mean normalize them to tick sizes

To do something like this.

double NormalizePrice(double price) {
   double step_price=SymbolInfoDouble(_Symbol,SYMBOL_TRADE_TICK_SIZE);
   if(step_price>0.0) price = step_price*MathRound(price/step_price);
   return(NormalizeDouble(price,_Digits));
}

Of course you also need to check that for a buy trade, TP is above BID price + (min. stop level allowed by your broker) and SL is below BID price - (min. stop level allowed by your broker).

Opposite for a sell trade.

 
Yashar Seyyedin #:
How can you be sure the current price is above the new stop level you are applying.

kkk am never sure of anything because am doing my first project, i just do trial and errors until i get a possible solution

 
Fabio Cavalloni #:

To do something like this.

Of course you also need to check that for a buy trade, TP is above BID price + (min. stop level allowed by your broker) and SL is below BID price - (min. stop level allowed by youok

okay let me check it out , but what surprises me is that it works on currencies

 
ZhuwaoAbel:
         if(CurrStoploss<SL){          trade.PositionModify(PositionTicket,(CurrStoploss+1000*_Point),0);          }
        double new_sl=CurrStoploss+1000*_Point;
        double Bid=SymbolInfoDouble(_Symbol, SYMBOL_BID);
         if(CurrStoploss<SL && new_sl<Bid-SYMBOL_TRADE_STOPS_LEVEL){
         trade.PositionModify(PositionTicket, new_sl,0);
         } 

I did not test or compile this. Just to note that you can only update stop loss to a level that is well below the current bid price(when it is a long position). 

 
Yashar Seyyedin #:
new_sl<Bid-SYMBOL_TRADE_STOPS_LEVEL
new_sl<Bid-SYMBOL_TRADE_STOPS_LEVEL*_Point
 
Yashar Seyyedin #:
new_sl<Bid-SYMBOL_TRADE_STOPS_LEVEL*_Point

Am kinda of lost here i have followed your recommendations and it working only for buys and the sl is incrementing buy every tick

Files:
sltrailer.png  122 kb
 
ZhuwaoAbel #:

Am kinda of lost here i have followed your recommendations and it working only for buys and the sl is incrementing buy every tick

How long have you been trading Deriv's BOOM &CRASH??

(Trading Crash)You can't protect your long positions against a drop

(Trading BOOM) you can't protect your short position against a spike