TRAILING STOP HELP

To add comments, please log in or register
Joao Luiz Sa Marchioro
3956
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(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);
     }
     }
   }
}
Alain Verleyen
36041
Alain Verleyen  
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.

Joao Luiz Sa Marchioro
3956
Joao Luiz Sa Marchioro  

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);
Joao Luiz Sa Marchioro
3956
Joao Luiz Sa Marchioro  

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.

Joao Luiz Sa Marchioro
3956
Joao Luiz Sa Marchioro  

I put the EA to make it clearer

Files:
Joao Luiz Sa Marchioro
3956
Joao Luiz Sa Marchioro  
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.

Alain Verleyen
36041
Alain Verleyen  
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
Joao Luiz Sa Marchioro
3956
Joao Luiz Sa Marchioro  
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.

Joao Luiz Sa Marchioro
3956
Joao Luiz Sa Marchioro  

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.

To add comments, please log in or register