Netting Account: Question about Position Operations

 

Hi,

I'm working on a netting account.

As far as I understand: by reversing an existing position the position will be closed and a new one will be opened with a new id and the contrarian type (buy --> sell or sell --> buy).

Am I correct here?

But I have difficulties to access to the newly opened position. How can I get the position id?

To demonstrate my problem I have written a small EA. Running this EA in the strategy tester allows me to open a new position, to increase it or to reverse it.

Additionally the EA print the correct position id. At least I hope. But it does not :-(

Please see the following EA

#include <Trade/Trade.mqh>
#include <Controls/Button.mqh>
#define BTN_BUY_NAME        "Btn Buy"
#define BTN_SELL_NAME       "Btn Sell"
CButton btnBuy;
CButton btnSell;
CTrade trade;

int OnInit() {
  btnBuy.Create (0, BTN_BUY_NAME, 0, 20,40,120,60);
  btnBuy.Text ("Buy 2");
  btnBuy.Color (clrWhite);
  btnBuy.ColorBackground (clrBlue);

  btnSell.Create (0, BTN_SELL_NAME, 0, 20,61,120,81);
  btnSell.Text ("Sell 3");
  btnSell.Color (clrWhite);
  btnSell.ColorBackground (clrRed);
  return INIT_SUCCEEDED;
}

void OnDeinit(const int reason) {
  btnBuy.Destroy();
  btnSell.Destroy();
}

void OnTick () {
  if (btnBuy.Pressed()) {
    trade.Buy (2.0);
    btnBuy.Pressed(false);
  }
  if (btnSell.Pressed()) {
    trade.Sell (3.0);
    btnSell.Pressed(false);
  }
}

void OnTradeTransaction (const MqlTradeTransaction&    trans, 
                         const MqlTradeRequest&        request,
                         const MqlTradeResult&         result) {
  if (trans.type == TRADE_TRANSACTION_DEAL_ADD) {
    PrintFormat ("trans position   %2d", trans.position);
    if (HistoryDealSelect (trans.deal)) {
      PrintFormat ("deal Position_Id %2d", HistoryDealGetInteger (trans.deal, DEAL_POSITION_ID));
    } else {
      PrintFormat ("ERROR HistoryDealSelect %d", trans.deal);
    }
  }
}

By pressing the Buy or the Sell button you can open and modify the (single) position:


If you press "Buy 2" you buy 2 lots and get the following output:

2025.03.21 21:03:43.644 2025.03.12 00:21:55   instant buy 2 USDJPY at 147.845 (147.841 / 147.845)
2025.03.21 21:03:43.644 2025.03.12 00:21:55   deal #2 buy 2 USDJPY at 147.845 done (based on order #2)
2025.03.21 21:03:43.644 2025.03.12 00:21:55   deal performed [#2 buy 2 USDJPY at 147.845]
2025.03.21 21:03:43.644 2025.03.12 00:21:55   order performed buy 2 at 147.845 [#2 buy 2 USDJPY at 147.845]
2025.03.21 21:03:43.648 2025.03.12 00:21:55   CTrade::OrderSend: instant buy 2.00 USDJPY at 147.845 [done at 147.845]
2025.03.21 21:03:43.649 2025.03.12 00:21:55   trans position    2
2025.03.21 21:03:43.649 2025.03.12 00:21:55   deal Position_Id  2

That is completely understandable for me: The position variable of the MqlTradeTransaction has the value 2 and the Position_Id of the deal as well.

But if I do a reverse operation now by pressing the button "Sell 3"  in order to sell 3 lots I get the following output:

2025.03.21 21:08:02.109 2025.03.12 01:20:33   instant sell 3 USDJPY at 147.861 (147.861 / 147.866)
2025.03.21 21:08:02.109 2025.03.12 01:20:33   deal #3 sell 3 USDJPY at 147.861 done (based on order #3)
2025.03.21 21:08:02.109 2025.03.12 01:20:33   deal performed [#3 sell 3 USDJPY at 147.861]
2025.03.21 21:08:02.109 2025.03.12 01:20:33   order performed sell 3 at 147.861 [#3 sell 3 USDJPY at 147.861]
2025.03.21 21:08:02.111 2025.03.12 01:20:33   CTrade::OrderSend: instant sell 3.00 USDJPY at 147.861 [done at 147.861]
2025.03.21 21:08:02.111 2025.03.12 01:20:33   trans position    2
2025.03.21 21:08:02.111 2025.03.12 01:20:33   deal Position_Id  2

You see:  the trans.position variable and the deal.position_id have the id of the old position which has been closed. These entries remain for all further operations.

Question: how can I access the newly opened position 3? Here you see position 3, indeed open:



Can you help me please?

Matthias