is there any function to access trailing stop instead of "PositionModify();" ?.

 

When we manually set trailing stop, all we do is set it using the Terminal >> Trailing stops. Once we set, it does its job its own. I'm asking for function like that kind. As I know, The EA should check the conditions and move the SL based on every tick if I use PositionModify(); for trailing stop.

 
Hapu Arachchilage Tharindu Lakmal:
or trailing stop.

A simple search in CodeBase and you find a lot of examples.

 
There is a PSAR trailing stop object in the API. 
But AFAIK, it is only for use in the EA wizard scripts.




 

Hi

You a need to write simple trailing stop function – there ‘s no one easy line to do this as it’s done in terminal.

But there are plenty of examples of such functions  for example:

CPositionInfo position;
Int trailingstart = 100;
Int trailingstop = 50;
void TrailingStop() 
{ 
   double bid                    = NormalizeDouble(SymbolInfoDouble(Symbol(), SYMBOL_BID), _Digits); 
   double ask                    = NormalizeDouble(SymbolInfoDouble(Symbol(), SYMBOL_ASK), _Digits); int freeze_level              = (int)SymbolInfoInteger(_Symbol, SYMBOL_TRADE_FREEZE_LEVEL); 
   int stop_level                = (int)SymbolInfoInteger(Symbol(), SYMBOL_TRADE_STOPS_LEVEL); 
   for(int i = PositionsTotal(); i >= 0; i --) { 
      if(position.SelectByIndex(i)) { 
         if(position.Symbol() == _Symbol) { 
            double Priceopen              = NormalizeDouble(position.PriceOpen(), _Digits); 
            double CurrentSL              = NormalizeDouble(position.StopLoss(), _Digits); 
            if(position.PositionType() == POSITION_TYPE_BUY) { 
               double NewSL  = NormalizeDouble(bid-trailingstop*Point(), _Digits);
               bool checkfreeze  = ((bid - NewSL) > freeze_level * _Point &&  (bid-NewSL)>stop_level*_Point); 
               if(CurrentSL < NewSL || CurrentSL == 0) { 
                  if(bid >= (Priceopen + trailingstart*_Point) && checkfreeze) { 
                     bool check = eatrade.PositionModify(position.Ticket(), NewSL, position.TakeProfit()); 
                  } 
               } 
            }
            else if(eaposition.PositionType() == POSITION_TYPE_SELL)  {
               double NewSL  = NormalizeDouble(ask+trailingstop*Point(), _Digits);
               bool checkfreeze  = (( NewSL-ask) > freeze_level * _Point &&  (NewSL-ask)>stop_level*_Point);
                  if(CurrentSL > NewSL || CurrentSL==0){ 
                     if(ask<=(Priceopen-trailingstart*_Point) && checkfreeze)  { 
                        check =  eatrade.PositionModify(position.Ticket(), NewSL, positon.TakeProfit()); 
                     } 
                 } 
            } 
         } 
      } 
   } 
}

Best Regards