MQL5 script for Hedging to Netting mode conversion

 

Hi, first time asking here, thanks in advance.

As the title implies I am looking for a script/function to account trade deals as "net lots" or "net position" per symbol, while working in a MT5 Hedging account.

One good thing about this requirement is that its a conversion for complex to simple (as hedging mode is generally more complex due to multiple deals).

here is what the script/function will most likely come out to be

void Set_Net_Lots(string symbol, double desired_Lots=0)
  {
   double current_Lots = Find_Net_Lots(symbol)          // +ve lots = net buy and vice versa
   
   LotsToBeAdded = desired_Lots - current_Lots;         // +ve lots = net buy and vice versa
   
   //-----------------------------------------------------------
   //smart algo to set net lots with minimum operations/deals possible 
   //-----------------------------------------------------------
  }


At first, this requirement seems very simple but it really is not, lets look at some examples to see,

Example 1 : For one symbol only -> current lots = -1.00 (-ve is sell) , desired lots = -0.5  (-ve is sell) ,  the some possible solutions are,
--> open +0.5 (+ve is buy) so net lots = -1.00 + 0.5 = -0.5
--> close -0.5 by Trade_action_close_by so net lots = -1.00 - (-0.5) = -0.5

The above two solutions are both fulfilling requirement in one deal transaction however,
->in the first case we are left with additional trade open which will have to be closed at some point and the solution is not complete without addressing that additional position.
->in the second case there is no additional trade but the problem is that this will not work for desired = -1.00 and current -0.5 as there is no function like Trade_Action_Add_By. so that will result in an additional position as well and which will have to be address at some point.

So, the main thing is so design an algo which is most efficient ( minimum deals, minimum simultaneous positions)

I am hoping that this has been done before and that someone can direct me.
I searched it a lot and the only close thing I found was this,
https://www.mql5.com/en/forum/364404

Converting an MQL5 EA designed for Netting to Hedging mode
Converting an MQL5 EA designed for Netting to Hedging mode
  • 2021.03.07
  • www.mql5.com
Hi, I have an EA that I wrote around 5 years ago, that I was using on a real account...