Questions from Beginners MQL4 MT4 MetaTrader 4 - page 15

 
Renat Akhtyamov:
we'll look at the code.
You mix it up.
 
A1exPit:
You stir it up.
Then good luck.
 
Renat Akhtyamov:
then good luck.
Isn't code important in the question you asked? You could have just written - I don't know, but it works somehow.
 
A1exPit:
Isn't the code important in this question? You could have just written "I don't know", but it seems to work.

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 is in the code - 100%.

 

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 one that was at the time of position opening or the one that is now (at the time when we want to know the amount of deposit for this position)?

 
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 one that existed at the time of position opening, or the one that exists now (at the time when we want to know the amount of deposit for this position)?

margin = MarketInfo(OrderSymbol(),MODE_MARGINREQUIRED) * OrderLots();

The result will not be accurate.

Or, you can find the rate of the needed currency pair on the chart through the time of opening the order and, if necessary, consider the spread (bid/ask) and calculate the more precise value of the margin at the time of opening the order using the following formula

The pitfall may be the calculation of margin at a time when the leverage was different

 
What if the EUR/USD exchange rate is written in the comment field when the order is opened and then read from there?
 
Babu Bonappan:
What if the EUR/USD exchange rate is written in the comment field when the order is opened and then read from there?

Comment for other purposes.

This is how to find out the price:

         string Symbol_X="USDCHF";
        
         int bs=iBarShift(OrderSymbol(),Period(),OrderOpenTime());
         double bid_X=iClose(Symbol_X,Period(),bs);
 
Renat Akhtyamov:

Comment for other purposes.

This is how to find out the price:

         string Symbol_X="USDCHF";
        
         int bs=iBarShift(OrderSymbol(),Period(),OrderOpenTime());
         double price_X=iClose(Symbol_X,Period(),bs);

If we trade on, say, daily charts, we will get the opening price of the day, somewhere in the middle of which the order we are interested in was opened, no? And it can be significantly different from the price that was at the time of opening.

 
Babu Bonappan:

So if we trade on, say, daily charts, we will get the opening price of the day, somewhere in the middle of which the order we are interested in was opened, no? And it may be significantly different from the price at which it was opened.

No one is insisting on using Period()

Expressly specify PERIOD_M15, for example, or another

Reason: