Modifying Long position SL

 

My EA needs to modify its position SL but sometimes gets the invalid stops error for long positions. The short positions are always modified without any problem.

Here is my source..

         if(PositionGetDouble(POSITION_PROFIT)>0.0)
         {
            double 
            trailing_points=StopPoints(Ticker,Delta*PositionGetDouble(POSITION_PROFIT),PositionGetDouble(POSITION_VOLUME)),// function for setting trailing distance that works fine
            trailing_stop=0.0,
            spread=SymbolInfoInteger(Ticker,SYMBOL_SPREAD)*SymbolInfoDouble(Ticker,SYMBOL_POINT);
            if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY)
            {
               trailing_stop=PositionGetDouble(POSITION_PRICE_CURRENT)-spread-MathMax(StopFreeze(Ticker),trailing_points);
               if(trailing_stop>PositionGetDouble(POSITION_SL))
               {
                  while(!trade.PositionModify(Ticker,trailing_stop,0.0)&&trailing_stop>PositionGetDouble(POSITION_SL))
                  {
                     trailing_stop-=SymbolInfoDouble(Ticker,SYMBOL_POINT);
                  }
                  if(trailing_stop<=PositionGetDouble(POSITION_SL))
                    {
                        Alert(" failed to modify LONG position. ");
                    }
               }
            }
         }

 Is there a better way of implementing this?

 
This problem is persisting. Any ideas will be appreciated. Thanks
 
I don't see where do you check the new SL against the current market price in your code?
 
enivid:
I don't see where do you check the new SL against the current market price in your code?

It was here...

 

trailing_stop=PositionGetDouble(POSITION_PRICE_CURRENT)-MathMax(StopFreeze(Ticker),trailing_points);

 

the new trailing stop price had to be below the the position's current price. Actually I had even tried the Bid price (in place of position current price), but I was still getting errors.

 

 
What's StopFreeze(Ticker)? You have to compare the new SL to the current price (Bid for LONG) - it shouldn't be closer than SYMBOL_TRADE_STOPS_LEVEL to the current price.
Documentation on MQL5: Standard Constants, Enumerations and Structures / Trade Constants / Order Properties
  • www.mql5.com
Standard Constants, Enumerations and Structures / Trade Constants / Order Properties - Documentation on MQL5
 
enivid:
What's StopFreeze(Ticker)? You have to compare the new SL to the current price (Bid for LONG) - it shouldn't be closer than SYMBOL_TRADE_STOPS_LEVEL to the current price.

Happy new year. 

 

This

 

StopFreeze(Ticker)

 

was the maximum between the stops level which you have mentioned above, and the freeze level. 'Ticker' is just the string for the symbol name. Like I said above I was trying Bid price before and was still getting errors. I suspect its a bug in the CTrade class. I had to rewrite the trade class.