QuestionHelp with trailing stop

1
I have an EA I am working on but I can't get the trailing stop to work. The original stop loss is at the low/high of the candle we will call it Xrange and as price moves away from open I want to trail it by values of X
the rules I am looking to implement are:
when price moves away from the open price 50% of the distance of the opening price and the SL the SL moves to BE
when price moves away from opening price moves 2 Xranges the original SL distance it moves in profit to 1 Xrange
the stop loss continues to follow price up

the code is MQL5

Code: Select all

void OnTick(){

   int bars = iBars(_Symbol,PERIOD_CURRENT);
   if(bars > totalBars){
   totalBars = bars;
   double lots;
   int engulfingSignal = getEngulfingSignal();
   if(engulfingSignal < 0){
   Print(__FUNCTION__,"Bearish Buy Signal");
   slDistance = iOpen(_Symbol,PERIOD_CURRENT,1) - iClose(_Symbol,PERIOD_CURRENT,1);
   calcLots();
   lots = calcLots();
   trade.BuyStop(lots,iOpen(_Symbol,PERIOD_CURRENT,1),_Symbol,iClose(_Symbol,PERIOD_CURRENT,1),0,ORDER_TIME_GTC,0,"Engulfing");
   }else if(engulfingSignal > 0){
   Print(__FUNCTION__,"Bullish Sell Signal");
   slDistance = iClose(_Symbol,PERIOD_CURRENT,1) - iOpen(_Symbol,PERIOD_CURRENT,1);
   calcLots();
   lots = calcLots();
   trade.SellStop(lots,iOpen(_Symbol,PERIOD_CURRENT,1),_Symbol,iClose(_Symbol,PERIOD_CURRENT,1),0,ORDER_TIME_GTC,0,"Engulfing");
   }
   }
   
  for(int i = PositionsTotal()-1; i >= 0; i--){
  ulong posTicket = PositionGetTicket(i);
  
   if(PositionSelectByTicket(posTicket)){
      if(PositionGetString(POSITION_SYMBOL)==_Symbol){
      double beSL;
      int n;
      double newSL; 
      double bid = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);
      if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY){
            n =1;
            beSL = PositionGetDouble(POSITION_PRICE_OPEN);
            newSL = (((PositionGetDouble(POSITION_PRICE_OPEN) - PositionGetDouble(POSITION_SL)) * n));
            if(bid > (beSL + newSL)){
         
               if(trade.PositionModify(posTicket,beSL,0)){
                     Print(__FUNCTION__, "Position was modified");
      }
      }
      
      }
   }
  
  }
}
}
Here is a picture of what I am looking to achieve.
Image


Who is online

Users browsing this forum: ChatGPT [Bot] and 0 guests