Problem in closing a trade in MetaTrader5.

 

Hi,

I am using metatrader 5 and am currently developing an EA for the platform. I hope you guys can help me out with a small problem.

I am using the below code to check if there is a trade placed in my expert advisor and if there is what kind of trade is it (BUY/SELL).

-------------------------------------------------------

FIRST BLOCK

bool Buy_Opened=false;

bool Sell_Opened=false;

if(PositionSelect(_Symbol)==true)
     {
      if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY)
        {
         Buy_Opened=true;
        }
      else if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL)
        {
         Sell_Opened=true;
        }
     }
---------------------------------------------------------

Further, I use the value of Buy_opened/Sell_Opened to send an order in the respective opposite direction (i.e. to close an open trade).

I use the following code to close an open position :

-------------------------------------------------------

SECOND BLOCK

//Buy Order Exit
   if(Buy_Opened)
     {
         mRequest.action=TRADE_ACTION_DEAL;
         mRequest.price=NormalizeDouble(latestPrice.bid,_Digits);
         mRequest.symbol=_Symbol;
         mRequest.volume=Lot;
         mRequest.magic=EAMagic;
         mRequest.type=ORDER_TYPE_SELL;
         mRequest.type_filling=ORDER_FILLING_FOK;
         mRequest.deviation=100;
         OrderSend(mRequest,mResult);
         Buy_Opened=false;
     }

//Sell Order Exit
   if(Sell_Opened)
     {
         mRequest.action=TRADE_ACTION_DEAL;
         mRequest.price=NormalizeDouble(latestPrice.ask,_Digits);
         mRequest.symbol=_Symbol;
         mRequest.volume=Lot;
         mRequest.magic=EAMagic;
         mRequest.type=ORDER_TYPE_BUY;
         mRequest.type_filling=ORDER_FILLING_FOK;
         mRequest.deviation=100;
         OrderSend(mRequest,mResult);
         Sell_Opened=false;
     }
-------------------------------------------------------------

The problem is that when first block is executed Buy_Opened and Sell_Opened become false, after that suppose a sell order is placed and hence sell_opened becomes true now when it enters if(Sell_Opened) in the second block, it executes a buy order in opposite direction,therefore, closing the previous opened trade...Now no trades are opened...Now when First Block is executed again it does not set the values of BUY_OPENED and SELL_OPENED to false instead it sets the value of Buy_Opened(the last order that was used to close the previous order) as true. Can anyone help me with amateur problem of mine? Thank you in advance.

 

Forum on trading, automated trading systems and testing trading strategies


Hello,

Please use the SRC button when you post code. Thank you.


This time, I edited it for you.


 
Alain Verleyen:

Okay.Noted.

 
Sudhanshu Gupta:

Hi,

...

It's impossible to help you without a complete view of your problem. Can you post the full code related to order sending ?

Can you post the logs ?

Reason: