Trailing Stop in one direction

 

Hello, I am new to programming and after multiple searches I cannot resolve this trailing stop problem. I managed to create a trailing stop but it goes up and down compared to my open price. I would like it to follow the price in one direction. For example, for a buy order, I would like the trailing stop to go up only when the price goes up and if the price goes down, the trailing stop doesn't go down, it stays at the same price. Thank you, here my code :

              double OpenBuy = NormalizeDouble(PositionGetDouble(POSITION_PRICE_OPEN),_Digits);
              

              if (Bid = 10+OpenBuy)
                     for(int i=PositionsTotal()-1; i>=0; i--) 
                    {
       
                       int ticket =PositionGetTicket(i);
                       
                       int PositionDirectionBuy=PositionGetInteger(POSITION_TYPE); 
                     
                       if (PositionDirectionBuy==POSITION_TYPE_BUY) 
                     
                       trade.PositionModify((ticket),Bid-(MathMax(MathAbs(MA30OpenValue-MA30CloseValue),(LastHighest-Bid)*0.5)), Bid+ MathMax(MathAbs(MA30OpenValue-MA30CloseValue),(LastHighest-Bid))); 

                     }
 
Please edit your post and use the code button (Alt+S) when pasting code.

EDIT your original post, please do not just post the code correctly in a new post.

You have posted in the MQL4 section, is your code MQL4?

 
Keith Watford:
Please edit your post and use the code button (Alt+S) when pasting code.

EDIT your original post, please do not just post the code correctly in a new post.

You have posted in the MQL4 section, is your code MQL4?


I'm on the Mql5 site though. Ok, I edit.

 
I will move your topic.
 
Mail Cash: I'm on the Mql5 site though. Ok, I edit.

Yes, it is the MQL5 site, but the forum is divided into several sections. Did you not notice? One of them, which is located at the very end, is dedicated to MetaTrader 4 and MQL4.

 
if (Bid = 10+OpenBuy)
  1. That is an assignment, not a comparison.

  2. Doubles are rarely equal. Understand the links in:
              The == operand. - MQL4 programming forum #2 2013.06.07

 
William Roeder:
  1. That is an assignment, not a comparison.

  2. Doubles are rarely equal. Understand the links in:
              The == operand. - MQL4 programming forum #2 2013.06.07


Thanks, I made a mistake in my first post. It is not if (Offer == 10 + OpenBuy) but (Offer > 10 + OpenBuy). With the symbol > the SL goes up and down with each movement price, which is not the case with the == symbol or the SL only moves when the price reaches the condition. How do you make it go up only using the > symbol?

Reason: