Help with moving stop to breakeven.

 
void UpdateStopLoss() {
   
   if(!InpBEtrade) {return;}
   
   double bid = SymbolInfoDouble(_Symbol,SYMBOL_BID);
   bid = NormalizeDouble(bid,_Digits);
   double ask = SymbolInfoDouble(_Symbol,SYMBOL_ASK);
   ask = NormalizeDouble(ask,_Digits);
   
   int total = PositionsTotal();
   for(int i=total-1; i>=0; i--){
      ulong ticket = PositionGetTicket(i);
      if(ticket<=0) {Print("Failed to get position ticket"); return;}
      if(!PositionSelectByTicket(ticket)){Print("Failed to get position by ticket"); return;}
      ulong magicnumber;
      if(!PositionGetInteger(POSITION_MAGIC,magicnumber)) {Print("Failed to get position magic number"); return;}
      if(InpMagicNumber==magicnumber){
         
         //get type
         long type;
         if(!PositionGetInteger(POSITION_TYPE,type)) {Print("Failed to get position type"); return;}
         
         //get current sl and tp
         double currSL, currTP, entry;
         if(!PositionGetDouble(POSITION_SL,currSL)) {Print("Failed to get position stop loss"); return;}
         if(!PositionGetDouble(POSITION_TP,currTP)) {Print("Failed to get position take profit"); return;}
         if(!PositionGetDouble(POSITION_PRICE_OPEN,entry)) {Print("Failed to get position entry"); return;}
         
         //calculate stop loss
         if(type == POSITION_TYPE_BUY){
            if(bid > entry + (InpBE*(entry - currSL))){
               double newSL = entry + InpBuffer*_Point;
               newSL = NormalizeDouble(newSL, _Digits);
               
               if(newSL > currSL){
                  if(trade.PositionModify(ticket,newSL,currTP)){
                     Print("Stop loss moved to break-even after ", InpBE," RR reached.");
                     return;
                  }
               }
            }
         }
         else if(type == POSITION_TYPE_SELL){
            if(ask < entry - (InpBE*(currSL - entry))){
               double newSL = entry - InpBuffer*_Point;
               newSL = NormalizeDouble(newSL, _Digits);
               
               if(newSL < currSL){
                  if(trade.PositionModify(ticket,newSL,currTP)){
                     Print("Stop loss moved to break-even after ", InpBE," RR reached.");
                     return;
                  }
               }
            }
         }
      }
   }
}


 The attached code tries to move stoploss to breakeven when current price is "InpBE" risk multiple away from entry, so if user input "1.0" as InpBE, sl will be moved to entry + Inpbuffer points when price is 1 RR multiple from entry. The function i wrote here does not move my stop, Can anyone help me identify why? Im new to mql5 in eneral. Any help will be appreciated :)

...

Improperly formatted code removed by moderator. Please EDIT your post and use the CODE button (Alt-S) when inserting code.

Code button in editor

Hover your mouse over your post and select "edit" ... 

...

 

Your topic has been moved to the section: Expert Advisors and Automated Trading
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893

Please EDIT your post and use the CODE button (Alt-S) when inserting code.

Code button in editor

 
Wei Lun Lim:


 The attached code tries to move stoploss to breakeven when current price is "InpBE" risk multiple away from entry, so if user input "1.0" as InpBE, sl will be moved to entry + Inpbuffer points when price is 1 RR multiple from entry. The function i wrote here does not move my stop, Can anyone help me identify why? Im new to mql5 in eneral. Any help will be appreciated :)

General debugging approach:  put Print statements at key points when values change and output the key values.

Right now, we don't know where it's failing. Is it the newSL/currSL comparison? What are the values of newSL and currSL at that point?

Or is it failing on the PositionModify? In which case you need the return code, which you can get with trade.ResultRetcode().

You might also need to check if it's a valid stop loss value. Depending on the specifications of the symbol, is it possible that it's trying to set the stop loss too close to the current price?

Reason: