Problem with EA setting BE randomly

 

Hi all, 

As soon as my EA open a trade, the stoploss get directly set to BE and the trade get closed bits by bits within seconds. Even when I try to open the trade manually it does this. However it does not do that all the time and it depends on the instrument. I do not have a single line on my code which modify the position to BE so I really don't know where that come from. Any one ever experienced this? I have been struggling with that for a bit and can't find a solution. 

Thanks!

Files:
BEbug.jpg  478 kb
 

Obviously it is a bug in your code or there is some other EA running that is affecting the trades.

So, if you need help with your code, you are going to need to show your code — ALL relevant code.

Use the CODE button (Alt-S) when inserting code.

Code button in editor

If you don't want to show your code publicly on the forum, then consider hiring someone in the Freelance section to look at it in private.

Trading applications for MetaTrader 5 to order
Trading applications for MetaTrader 5 to order
  • 2023.09.18
  • www.mql5.com
The largest freelance service with MQL5 application developers
 
Fernando Carreiro #:

Obviously it is a bug in your code or there is some other EA running that is affecting the trades.

So, if you need help with your code, you are going to need to show your code — ALL relevant code.

Use the CODE button (Alt-S) when inserting code.

If you don't want to show your code publicly on the forum, then consider hiring someone in the Freelance section to look at it in private.

Thanks you for your reply. So I tested different part of my code and the problem seems to come from this following part: 

   if (Partial){
      for(int i=PositionsTotal()-1; i >= 0; i--){
      
         ulong posticket = PositionGetTicket(i);
         if(PositionSelectByTicket(posticket)){
            double openPrice = PositionGetDouble(POSITION_PRICE_OPEN); 
            string instrument = PositionGetString(POSITION_SYMBOL); 
            double currentTP = PositionGetDouble(POSITION_TP); 
            double slPosition = PositionGetDouble(POSITION_SL);
            double volume = PositionGetDouble(POSITION_VOLUME);
            double LotToClose = volume * PartialCloture;
            ENUM_POSITION_TYPE posType = (ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE);
         
            LotToClose = NormalizeDouble(LotToClose, 2);   
            if (posType == POSITION_TYPE_BUY && bid > openPrice && instrument == _Symbol                   
            && ((bid - openPrice) >= (openPrice - slPosition) * PartialRR) && (slPosition != openPrice) ){       
               trade.PositionClosePartial(posticket, LotToClose);
               trade.PositionModify(posticket,openPrice,currentTP); }
               
            if (posType == POSITION_TYPE_SELL && ask < openPrice && instrument == _Symbol 
            && ((openPrice - ask) >= (slPosition - openPrice) * PartialRR) && (slPosition != openPrice)){
               trade.PositionClosePartial(posticket, LotToClose);
               trade.PositionModify(posticket,openPrice,currentTP);}  
               }  
  }}

Partial(bool), PartialRR(double) and PartialCloture(double) are inputs. The trade got closed where the EA had the input false for Partial but it was actually set to true on another pair. So I added the instrument == _Symbol to the conditions to different instruments but now the problem is still there. 

The logic of the partial is to take a partial once a certain risk-reward is reached and set to BE. There seems to have a problem in the conditions but I went through it multiple times and I don't see where the problem come from. 

Any help would be much appreciated, thanks 

Reason: