Questions from Beginners MQL4 MT4 MetaTrader 4 - page 16

 

OK.

The most important thing for me to clarify for myself now is probably whether I am correct in assuming that to calculate the margin on one particular trade, a fixed value of the base currency againstthe deposit currency, taken at the time of opening the trade, should be used. Is this correct? And the margin on this trade remains constant until the order is closed? Is this correct?

 
Babu Bonappan:

.....

The most important thing for me to clarify is probably whether or not I am correct in assuming that a fixed value of the base currency against the deposit currency, taken at the time of opening the trade, should be used to calculate the margin for one particular trade. Is this correct? And the margin on this trade remains constant until the order is closed? Is this correct?

Yes, you are correct.

Therefore, the price to calculate the margin is important at the time of opening the order

 
Renat, thank you so much for your help!
 
Babu Bonappan:
Renat, thank you so much for your help!
You're welcome
 
Renat Akhtyamov:

The question is about the code, so you can't do it without the code.

Are you asking about modifiers or order processing?

I do not know what you need. But the error in the code is 100%.

You don't know anything, but you are confidently making conclusions.

I am asking about OrderSelect

 
Babu Bonappan:

Please advise how to use MQL4 to get a margin value for each open position in the terminal?

I used to do it this way:

margin = MarketInfo(Symbol(),MODE_LOTSIZE) * OrderOpenPrice() / AccountLeverage() * OrderLots();

When trading EUR/USD this construction worked fine and I was sure that its logic was correct.

But now I want to get the same result for EUR/JPY (or EUR/CHF). Obviously, instead ofOrderOpenPrice() I need to multiply the value of a standard lot by the rate of the base currency to the deposit currency (in my case, by EUR/USD). But what is this rate? The exchange rate that was at the time of position opening or the one that we have now (at the time when we want to know the deposit amount for this position)?

The OrderOpenPrice - is the opening price of the selected order - it doesn't make any difference which currency it is displaying. We should probably check Point and Digits. Or you should check the rounding. Somewhere there is a rounding.

NormalizeDouble to 5 decimal places, and we need 3 decimal places for JPY. And the bind itself should work in the same way for all currencies.

 
Babu Bonappan:
What if at the moment of order opening, the EUR/USD exchange rate is written into the comment field and then read from there?
It is easier to write "USDJPY" instead of OrderSymbol()
 
A1exPit:
It's easier to write "USDJPY" instead of OrderSymbol()

Why USD/JPY? After all, we are trading EUR/JPY, so 1 lot is 100000 EUR, while the deposit currency is USD. In my opinion, in this case we need the EUR/USD exchange rate at the time of opening a deal.

Besides, if you set a forced symbol in the code, the code will not be suitable for trading on another currency pair, for example NZD/CAD. But I would like to get a universal variant. So far, I can only think of one way to store the deposit amount for each order calculated at the moment of its opening. Maybe we could create an array for this purpose where the order ticket and its deposit would be stored. But perhaps there is a more trivial solution.

 
A1exPit:

OrderOpenPrice - Opening price of the selected order - it doesn't care what currency it is using, it gives the price. Most probably Point and Digits should be checked. Or you should check the Rounding. We should check rounding.

NormalizeDouble is 5 decimal places, while JPY needs 3 decimal places. And the binder itself is supposed to work the same for all currencies.

And why do you lie about the number of digits? Do not mislead people.

 
A1exPit:

OrderOpenPrice - the opening price of the selected order - it doesn't care what currency it is using, it gives out the price.

The OrderOpenPrice, as I understand it, gives out exactly what I need. But only if the deposit currency is the dollar and the traded pair is EUR/USD. In this case, it is as if the OrderOpenPrice stores the exchange rate of the base currency to the currency of deposit at the moment of opening the order knowing which you can easily calculate the deposit.

But if at least one of these conditions is not met, how can we obtain the value of the deposit for an individual order? Where can we find the rate of the base currency of a quote relative to the currency of the deposit at the moment of its opening?

Yes, we have the time of order opening to the nearest second. But what can we get? At most - the parameters of the minute candle of the required symbol. But never the exact value of the rate used for calculating the deposit. But the AccountMargin function gets it somehow! It would be very interesting to understand how exactly.

Reason: