How to open a opposite order in MQI5 hedging account

I’m using MT5 hedging account and I’m trying to open an opposite market order (hedging order). I’m using MT5 hedging platform. The problem is when I open the hedging order the existing order gets closed, why is that? Here is my order opening code.

// Open buy market order
   if(close[0] > ma[0] && glBuyPlaced == false)
   {
        // Open buy order
        request.action = TRADE_ACTION_DEAL;
                request.type = ORDER_TYPE_BUY;
                request.symbol = _Symbol;
                request.position = 0;
                request.volume = TradeVolume;
                request.price = SymbolInfoDouble(_Symbol,SYMBOL_ASK);
                request.sl = 0;
                request.tp = 0;
                request.deviation = 50;
                
                bool sent = OrderSend(request,result);
        


        // Open opposite  market order
   else if(close[0] < ma[0] && glBuyPlaced == true)
   {
   
   
        // Open sell order
        request.action = TRADE_ACTION_DEAL;
                request.type = ORDER_TYPE_SELL;
                request.symbol = _Symbol;
                request.position = 0;
                request.volume = TradeVolume;
                request.price = SymbolInfoDouble(_Symbol,SYMBOL_BID);
                request.sl = 0;
                request.tp = 0;
                request.deviation = 50;
                
                bool sent = OrderSend(request,result);
request.action=TRADE_ACTION_CLOSE_BY;                         // type of trade operation
request.position=position_ticket;                             // ticket of the position
request.position_by=PositionGetInteger(POSITION_TICKET);      // ticket of the opposite position

Please fill completely MqlTradeRequest before sending it. Double check your inputs. For instance:

request.price = SymbolInfoDouble(request.symbol,SYMBOL_BID);

is safer. Also, use the following BEFORE filling a trade request:

ZeroMemory(request);

And zeroing MqlTradeResult is also a nice idea just before OrderSend():

ZeroMemory(result);

Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Trade Operation Types

You think you’re on a hedging account, but you’re actually on a netted one. Open a new account that is a hedging account. If you’re still in double then run this script…

//+------------------------------------------------------------------+
//|                                    what_kind_of_account_am_i.mq5 |
//|                                                      nicholishen |
//|                                             https://www.webs.com |
//+------------------------------------------------------------------+
#property copyright "nicholishen"
#property link      "https://www.webs.com"
#property version   "1.00"
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
#include <trade/accountinfo.mqh>
void OnStart() 
{
   CAccountInfo info;
   Print(info.MarginModeDescription());
}

it says that on the platform itself see snapshot below.

28

Then something somewhere is messed up in your code, which is why you should be working with the stdlib classes instead of trying to roll your own procedures using the low-level API. Run this script and step through it in the debugger. Compare it to your code and then see where yours went wrong.

#include <trade/trade.mqh>
#include <trade/accountinfo.mqh>

void OnStart() {
   CAccountInfo account;
   if (account.TradeMode() != ACCOUNT_TRADE_MODE_DEMO)
      return;
   Alert(account.MarginModeDescription());
   CTrade trade;
   trade.Sell(0.1);
   trade.Buy(0.1);  
}