PositionGetDouble(POSITION_SL) does not work for me???

 

Hi, I am pretty new here and to the platform.

So I wanna deal with trailing stop loss, so I will have to get access to the stop loss of the current postion.


Here is the way I open a position, which has no problem as far as I know.

   if(signal!=WRONG_VALUE)
     {
      if(TerminalInfoInteger(TERMINAL_TRADE_ALLOWED) && Bars(_Symbol,_Period)>100)
         ExtTrade.PositionOpen(_Symbol,signal,0.1,//TradeSizeOptimized(),
                               now_price,now_price - ATRloss*sign_for_stop_loss*STOPLOSS[1],now_price + ATRgain*sign_for_stop_loss*STOPLOSS[1]);
//                               SymbolInfoDouble(_Symbol,signal==ORDER_TYPE_SELL ? SYMBOL_BID:SYMBOL_ASK) + 1.5*STOPLOSS[1]);
                               
                               
                               
   CheckTrailingStop(now_price, ATRtrail*sign_for_stop_loss*STOPLOSS[1]);

However, when it enters the function

void CheckTrailingStop(double now_price, double SLTrigger){
  double SL = NormalizeDouble(now_price - SLTrigger, _Digits);
  for(int i =PositionsTotal()-1;i>=0;i--)
   {
   string symbol=PositionGetSymbol(i);
   if(_Symbol==symbol)
      {
      ulong PositionTicket=PositionGetInteger(POSITION_TICKET);
      double CurrentStopLoss=PositionGetDouble(POSITION_SL);
      if(CurrentStopLoss<SL && now_price > PositionGetDouble(POSITION_PRICE_OPEN) + 0.5*SLTrigger){
         ExtTrade.PositionModify(PositionTicket, now_price,0);
         }
      }
   }
  }


And I use debug mode an watch the value of CurrentStopLoss, it gave me 8.3575835936946108E-312, which makes no sense.

Attached is the time when I watch the variable, as you can see, the position ticket gives me 2, which I guess means that there indeed has a position.


Thanks a lot. Any help will be appreciated.

Files:
Untitled.png  93 kb
 
Well use the debug mode not only to check a certain value but even the reason - single calculation by single calculation - why this value was assigned to the variable.
 
Carl Schreiber:
Well use the debug mode not only to check a certain value but even the reason - single calculation by single calculation - why this value was assigned to the variable.

Hello,

After I close and open and debug again, there are reasonable values now. Don't know what happened, but this bug is not my worries now.

If you got the time, I wish you would answer my new bug. Any help would be appreciated:

https://www.mql5.com/en/forum/346215

Trailing stop loss only work in next bar, not every single tick as intended.
Trailing stop loss only work in next bar, not every single tick as intended.
  • 2020.07.08
  • www.mql5.com
Hello everyone! So I have a stop loss check in the Ontick function like this: Some part of the code is hide away for simplicity...