How to calculate margin for a currency pair

 

Hello, would you please know how I could calculate a currently used margin for all the open positions on a currency pair

 

Thanks a lot! Pavel 

 
solator:

Hello, would you please know how I could calculate a currently used margin for all the open positions on a currency pair? 

Select the Orders one by one,  OrderSelect(),  get their OrderLots() and calculate the margin used for each order,  keep a running total . . .
 
RaptorUK:
Select the Orders one by one,  OrderSelect(),  get their OrderLots() and calculate the margin used for each order,  keep a running total . . .


Great idea, thank you! Does it mean that I would keep the margin required for 1 lot as a constant in my program and calculate the actual margin from that? Or is there a way how to get the unit margin by a function?

Thanks and have a good day! Pavel 

 
solator: Hello, would you please know how I could calculate a currently used margin for all the open positions on a currency pair?

It is not the current margin used that is important. It is the free margin at the most adverse excursion for all orders that is, to avoid being stopped out.

Look at my code, how I compute lot size from allowable risk, initial stop loss and free margin.

 
WHRoeder:

It is not the current margin used that is important. It is the free margin at the most adverse excursion for all orders that is, to avoid being stopped out.

Look at my code, how I compute lot size from allowable risk, initial stop loss and free margin.


Hello, Friend, I understand what you are saying but my question was motivated by having a number of open positions, currently about 13 to 15. Then I would be glad if I could know what is the margin for each of the positions. It would help me calculate my other positions. Maybe it looks unnecessary but I need it and at the same time I would like to learn how to do it in the MQL 4 :-).

Thank you a lot and I will have a look at your code.

Pavel 

 

Hello, Friends,

I found it!! MarketInfo() function can have a parametr MODE_MARGINREQURED and that means: Initial margin required to open 1 lot.. That is exactly what I needed.

Thank you all and we have learned something new... Pavel

void CalculateMarginUsed() {
  
  margin_per_lot = MarketInfo(symbol_to_trade, MODE_MARGINREQUIRED);
  margin_used = position_used *  margin_per_lot;
  
  return;
}
Reason: