Discussion of article "MQL5 Cookbook: Getting properties of an open hedge position"

 

New article MQL5 Cookbook: Getting properties of an open hedge position has been published:

MetaTrader 5 is a multi-asset platform. Moreover, it supports different position management systems. Such opportunities provide significantly expanded options for the implementation and formalization of trading ideas. In this article, we discuss methods of handling and accounting of position properties in the hedging mode. The article features a derived class, as well as examples showing how to get and process the properties of a hedge position.

One of the recently added MetaTrader 5 terminal features is the possibility to open bi-directional orders. This order accounting system is called hedging. Support for this order system enables an easy transfer of trading algorithms from MetaTrader 4 to the fifth platform version, while taking the advantage of the advanced MetaTrader 5 functionality. For more details about the hedging options in MetaTrader 5, please read the article "MetaTrader 5 features hedging position accounting system".

In this article, we will discuss the properties of the aggregate position, which is the object of the hedging system.

Hedge position properties panel

Fig.4 Hedge position properties panel

Author: Denis Kirichenko

 

Theopening price is normalised by Digits for some reason.

The commission is calculated with an error and slowly: it is normalised and doubled at each step of the cycle.

 
fxsaber:

Theopening price is normalised by Digits for some reason.

The commission is calculated with an error and slowly: it is normalised and doubled at each step of the cycle.

Thanks for your opinion.

I checked the code regarding normalisation. There are only two cases in the CHedgePositionInfo class: commission and margin. The prices remain unnormalised. The opening price, as far as I understand, is implicitly normalised for the print in the Test_hedge_properties.mq5 test script.

Then about the commission. Have you compared it with the records on the "History" tab? There, for each trade, the commission value is normalised with an accuracy of 2. And doubling is taking into account the moment of exit(DEAL_ENTRY_OUT). That is, the Panel reflects the commission for the full cycle - opening and closing. There is a mention about it in the article.

 
Denis Kirichenko:

Thank you for your opinion.

Checked the code regarding normalisation. There are only two cases in the CHedgePositionInfo class: commission and margin. The prices remain unnormalised. The opening price, as far as I understand, is implicitly normalised for the print in the Test_hedge_properties.mq5 test script.

Yes, it was about the print.

Then about the commission. Have you compared it with the records on the "History" tab? There, for each trade, the commission value is normalised to the nearest 2. And doubling is taking into account the moment of exit(DEAL_ENTRY_OUT). That is, the Panel reflects the commission for the full cycle - opening and closing. There is a mention of this in the article.

No, I was judging by the code. Open a position and then close a part of it. According to your code, the commission of the current position will not change.

Furthermore, doubling down is a good intention which creates a problem. The point is that you need Balance to be equal to Equity + PositionCommission after closing all positions. But then there should be no doubling, because MT5 has already reflected the opening commission in the balance when opening positions.


And it is very expensive to perform normalisation at each step of the cycle when calculating the commission. It is important when optimising.

 
fxsaber:

...No, judging by the code. Open a position and then close part of it. According to your code the commission of the current position will not change.

Moreover, doubling is a good intention which creates a problem. The point is that you need Balance to be equal to Equity + PositionCommission after closing all positions. But then there should be no doubling, because MT5 has already reflected the opening commission in the balance when opening positions.

Yes, it will not change. The purpose was to show the maximum commission that we pay in total for the current hedge. And, moreover, we have already paid part of it, and will pay the second part later. But there are nuances.

For example, there is such a type of deal as DEAL_ENTRY_OUT_BY. There is no commission at all, at least with my broker. The commission can also change (entry and exit can cost differently) if the account currency does not coincide with the base currency. So my example is far from ideal....

What makes you think I need it? I just showed one of the ways of calculating such overheads as commission.


Well, when calculating commission it is very expensive to normalise at each step of the loop. It's important in Optimisation.

Except for optimisation. Well, nobody prevents you from making your own changes and taking them into account in the code.

 
Denis Kirichenko:

Yes, it won't change. The goal was to show the maximum amount of commission that we pay in total for the current hedge. And, moreover, we have already paid part of it, and will pay the second part later. But there are nuances.

For example, there is such a type of deal as DEAL_ENTRY_OUT_BY. There is no commission at all, at least with my broker.

CloseBy is not subject to commission by definition.

The commission can also change (entry and exit can cost differently) if the account currency is not the same as the base currency. So my example is far from perfect....

What makes you think I need it? I just showed one of the ways of calculating such overheads as commission.

Because in the language of MT4 Equity = Balance + Profit + Swap + Commission. This is the most common logic. But of course you can have your own.

Except for optimisation. Well, nobody prevents you from making your own changes and taking them into account in the code.

The developers are trying to make the Optimiser faster. But slow code is allowed in Articles. It looks strange.

 
fxsaber:

Because in the language of the same MT4 Equity = Balance + Profit + Swap + Commission. This is the most common logic. But of course you can have your own logic.

You won't believe me, I think the same way about equity :-))))

I will make a choice for the commission method, how to count. Make changes to the code...

...The developers talk about making the Optimiser faster. But in Articles they allow slow code. It looks weird.

In diplomatic parlance. Are you still boiling? - I've been testing everything in the cloud for a long time and I don't worry that there are unnecessary normalisations and multi-line macros somewhere...

 
Denis Kirichenko:

I've been testing everything in the cloud for a long time and I don't worry that somewhere there are unnecessary normalisations and no multi-line macros...

It seems to be a long-formed trend to replace even the simplest algorithmic optimisation with the power of hardware. I can't do that.

 
fxsaber:

Even the simplest algorithmic optimisation being replaced by the power of hardware is apparently a long-established trend. I can't do that.

The gigahertz and gigabyte generation.

 

Hi,

When I downloaded the zip file and extracted it and opened them up in the editor, when I then compiled them they came back with heaps of errors.


Is there anything I can use?


Cheers

 

If you follow my way there will be no errors. The way is simple. Open you MetaEditor and:

1) create a subfolder "Hedge" in the folder "Shared Projects";

2) place the source files into the subfolder.


ME Navigator

Then you may compile HedgePropertiesEA.mq5 (an expert advisor) and  Test_hedge_properties.mq5 (a script). Once the compilation successfully finishes you will find the executables in the terminal navigator.


MT5 Navigator