How Would one Calculate the Anticipated Required Margin?

 

Morning, Fellow MQL4'ers

How would one kindly calculate the margin required to open a position taking into account the leverage (100:1), lotsize (0.10) and an exchange rate factor? I can't do an OrderSelect() function because there is no order to select. 

I do have a fantastic currency conversion code which just divides 1 / "tickvalue" which is great for calculating the profit on a position, but because margin is calculated based on the Base Currency & not the Counter Currency, it kind of makes it redundant.

I know that the formula is Margin = Lotsize / leverage * rate but putting this into practice with converting the base currency into the account currency is a hell of a feat. 

What has everyone else's solution been to this?

Cheers

 

Risk depends on your initial stop loss, lot size, and the value of the symbol. It does not depend on margin and leverage. No SL means you have infinite risk. Never risk more than a small percentage of your trading funds, certainly less than 2% per trade, 6% total.

  1. You place the stop where it needs to be — where the reason for the trade is no longer valid. E.g. trading a support bounce the stop goes below the support.

  2. AccountBalance * percent/100 = RISK = OrderLots * (|OrderOpenPrice - OrderStopLoss| * DeltaPerLot + CommissionPerLot) (Note OOP-OSL includes the spread, and DeltaPerLot is usually around $10/PIP but it takes account of the exchange rates of the pair vs. your account currency.)

  3. Do NOT use TickValue by itself - DeltaPerLot and verify that MODE_TICKVALUE is returning a value in your deposit currency, as promised by the documentation, or whether it is returning a value in the instrument's base currency.
              MODE_TICKVALUE is not reliable on non-fx instruments with many brokers - MQL4 programming forum (2017)
              Is there an universal solution for Tick value? - Currency Pairs - General - MQL5 programming forum (2018)
              Lot value calculation off by a factor of 100 - MQL5 programming forum (2019)

  4. You must normalize lots properly and check against min and max.

  5. You must also check FreeMargin to avoid stop out

Most pairs are worth about $10 per PIP. A $5 risk with a (very small) 5 PIP SL is $5/$10/5 or 0.1 Lots maximum.

 
William Roeder #:

Risk depends on your initial stop loss, lot size, and the value of the symbol. It does not depend on margin and leverage. No SL means you have infinite risk. Never risk more than a small percentage of your trading funds, certainly less than 2% per trade, 6% total.

  1. You place the stop where it needs to be — where the reason for the trade is no longer valid. E.g. trading a support bounce the stop goes below the support.

  2. AccountBalance * percent/100 = RISK = OrderLots * (|OrderOpenPrice - OrderStopLoss| * DeltaPerLot + CommissionPerLot) (Note OOP-OSL includes the spread, and DeltaPerLot is usually around $10/PIP but it takes account of the exchange rates of the pair vs. your account currency.)

  3. Do NOT use TickValue by itself - DeltaPerLot and verify that MODE_TICKVALUE is returning a value in your deposit currency, as promised by the documentation, or whether it is returning a value in the instrument's base currency.
              MODE_TICKVALUE is not reliable on non-fx instruments with many brokers - MQL4 programming forum (2017)
              Is there an universal solution for Tick value? - Currency Pairs - General - MQL5 programming forum (2018)
              Lot value calculation off by a factor of 100 - MQL5 programming forum (2019)

  4. You must normalize lots properly and check against min and max.

  5. You must also check FreeMargin to avoid stop out

Most pairs are worth about $10 per PIP. A $5 risk with a (very small) 5 PIP SL is $5/$10/5 or 0.1 Lots maximum.

Thank you for your reply,

I realise this, and I'm fully aware that risk measures the S/L and the lot size with a tick value that translates into the account currency, however, I wanted to compare the maximum amount, in monetary terms, I would lose and compare this to the estimated margin requirement to prevent getting stopped out. 

These two figures would come up on a pop-up box so the trader could quickly compare the two figures and make a decision. It's pretty neat actually.

Obviously, if the maximum loss exceeded the margin required, I would need to adjust my risk parameters (reduce leverage or reduce the lot size) or just not enter the trade. 

However, for anyone else who is struggling with this issue, I have since come up with a solution, which is

lot size x MarketInfo(Symbol(),MODE_MARGINREQUIREMENT)

I can confirm this works, having tested this on a demo account across five different currency pairs.

The margin required figure matched the figure at the bottom of the terminal.

You mention tickvalue, food for thought. I will look into this, however, I have only ever experienced this to be out by a few pence and I assumed that due to the passage of time, this would be normal because of price fluctuations, especially in volatile markets. 

I will also investigate your other points, there's always room for improvement. 

Caio :)