TRAILING STOP HELP

 

Good morning people,

I would like a help, unfortunately I am still a beginner in programming, I know how to do the basics but I always get a lot.

I am having difficulty changing a TRAILING STOP mode that I currently use, I have tried several times and I have not been able to solve it.

What happens is that this TRAILING module is only capable of managing a single position at a time.

I would like help to change the module so that it can manage the multi-position TRAILING STOP at the same time.

Thanks in advance for your help.

if(UseTrailing){Trailing();}


void Trailing(){
double newsl;
   int _tp=PositionsTotal();
   for(int i=_tp-1; i>=0; i--)
     {
     string _p_symbol=PositionGetSymbol(i);
     double _p_tp = PositionGetDouble(POSITION_TP);
     double _p_sl = PositionGetDouble(POSITION_SL);
     double _p_op = PositionGetDouble(POSITION_PRICE_OPEN);
     long _p_type = PositionGetInteger(POSITION_TYPE);
     
     if(_p_symbol==_Symbol && _p_type==POSITION_TYPE_BUY && ((Ask-_p_op)>TrailStop*Point())){
     newsl=Ask-(TrailStep*Point());
     if(newsl>_p_sl && newsl!=_p_sl){
     trade.PositionModify(_Symbol,newsl,_p_tp);
     }
     }
     
     if(_p_symbol==_Symbol && _p_type==POSITION_TYPE_SELL && (_p_op-Bid>TrailStop*Point())){
     newsl=Bid+(TrailStep*Point());
     if((_p_sl>newsl||_p_sl==0) && newsl!=_p_sl){
     trade.PositionModify(_Symbol,newsl,_p_tp);
     }
     }
   }
}
 
Joao Luiz Sa Marchioro:

Good morning people,

I would like a help, unfortunately I am still a beginner in programming, I know how to do the basics but I always get a lot.

I am having difficulty changing a TRAILING STOP mode that I currently use, I have tried several times and I have not been able to solve it.

What happens is that this TRAILING module is only capable of managing a single position at a time.

I would like help to change the module so that it can manage the multi-position TRAILING STOP at the same time.

Thanks in advance for your help.

If this is mql5 code Ask/Bid are not available as is.

If this is mql4 code PositionXXX() functions are not available.

 

They are MQL5 codes.

This is just the module.

Some parameters I put straight on ontick

   Ask = SymbolInfoDouble(Symbol(),SYMBOL_ASK);
   Bid = SymbolInfoDouble(Symbol(),SYMBOL_BID);
 

From beforehand I would like to thank the help.

Of the hundreds of forums I've read, I always see you helping colleagues.

It would be harder if we did not have help from the more experienced people

thank you.

 

I put the EA to make it clearer

Files:
 
Alain Verleyen:

If this is mql5 code Ask/Bid are not available as is.

If this is mql4 code PositionXXX() functions are not available.


I'm still very inexperienced in programming, most of the time I use ready-made modules and I'm changing as needed.

 
Joao Luiz Sa Marchioro:

I put the EA to make it clearer

Your original posted code is not the same as in your attached file.

   int _tp=PositionsTotal();
   for(int i=_tp-1; i>=0; i--)
     {
      ulong tick=PositionGetTicket(i);

      if(PositionGetString(POSITION_SYMBOL)==_Symbol) // Add here needed filters : symbol ? magic ?
        {
         double _p_tp = PositionGetDouble(POSITION_TP);
         double _p_sl = PositionGetDouble(POSITION_SL);
         double _p_op = PositionGetDouble(POSITION_PRICE_OPEN);
         long _p_type = PositionGetInteger(POSITION_TYPE);

         if(_p_type==POSITION_TYPE_BUY && ((Ask-_p_op)>TrailStop*Point()))
           {
            newsl=Ask-(TrailStep*Point());
            if(newsl>_p_sl && newsl!=_p_sl)
              {
               trade.PositionModify(_Symbol,newsl,_p_tp);
              }
           }

         if(_p_type==POSITION_TYPE_SELL && (_p_op-Bid>TrailStop*Point()))
           {
            newsl=Bid+(TrailStep*Point());
            if((_p_sl>newsl || _p_sl==0) && newsl!=_p_sl)
              {
               trade.PositionModify(_Symbol,newsl,_p_tp);
              }
           }
        }
     }
NOT TESTED
 
Alain Verleyen:

Your original posted code is not the same as in your attached file.

NOT TESTED

I should have sent a version with some minor change that I have made trying to solve the problem, but it continues with the same function, managed only the first open position. I'll send the correct night


Thanks for the help, as soon as I get home I will test the code that you attached.


I'll check which one I sent and send the correct code at night.


Thank you my friend.

 

It did not work, EA can not manage the trailing stop individually from all positions at the same time.


I attached the EA with the change to look at.

Reason: