MQL5: How to calculate the maximum number of lots that can be placed in an order based on balance/equity and leverage?
- Find the maximum number of lots that can be opened
- How to calculate Maximum lot size based on Equity
- How can I calculate lot size risking X% of account balance programmatically
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893
You should not be calculating your order's volume based only on required margin. That is gambling,
You should be calculating it based on the stop-loss risk first and only then adjusting for margin as an extra precaution.
If you calculate for risk (stop-loss), then even if the leverage changes, you will still be within an acceptable volume size.
Please run a search as this has been discussed many times before in the forum.
You should not be calculating your order's volume based only on required margin. That is gambling,
You should be calculating it based on the stop-loss risk first and only then adjusting for margin as an extra precaution.
If you calculate for risk (stop-loss), then even if the leverage changes, you will still be within an acceptable volume size.
Please run a search as this has been discussed many times before in the forum.
Thank you for reply.
As i wrote, i always calculate volume based on stop-loss risk (my orders always have stop-loss price). My account has unlimit leverage. So, usually, all pending order will be filled. But sometimes my broker (exness) reduces the leverage (I asked and they explained it to me like that) --> pending order was canceled due to insufficient margin.
So my idea is that before placing an order, the EA will check the equity, leverage at that time to calculate the maximum possible volume so that the order is not canceled when hit price, compare it with the volume calculated by stop-loss risk and use the smaller volume.
How to calculate volume based on stop loss risk I already know.
But i don't know how to calculate (max) volume based on equity, leverage.
Sorry for creating new topic, I searched but couldn't find the answer (perhaps because my trading knowledge is weak, I'm a newbie).
Thank you for reply.
As i wrote, i always calculate volume based on stop-loss risk (my orders always have stop-loss price). My account has unlimit leverage. So, usually, all pending order will be filled. But sometimes my broker (exness) reduces the leverage (I asked and they explained it to me like that) --> pending order was canceled due to insufficient margin.
So my idea is that before placing an order, the EA will check the equity, leverage at that time to calculate the maximum possible volume so that the order is not canceled when hit price, compare it with the volume calculated by stop-loss risk and use the smaller volume.
How to calculate volume based on stop loss risk I already know.
But i don't know how to calculate (max) volume based on equity, leverage.
Sorry for creating new topic, I searched but couldn't find the answer (perhaps because my trading knowledge is weak, I'm a newbie).
You misunderstand. Having a stop-los does not mean that you have calculated your risk accordingly.
The fact that you are getting a "not enough money" message when the leverage changes to 1:200, shows that you have not calculated the volume correctly for that stop-loss.
Either that or your %risk for the stop-loss is way too high. It should be no more that 5% and ideally it should be 1-2%. And if you have multiple trades at a time then consider using even lower risk.
You misunderstand. Having a stop-los does not mean that you have calculated your risk accordingly.
The fact that you are getting a "not enough money" message when the leverage changes to 1:200, shows that you have not calculated the volume correctly for that stop-loss.
Either that or your %risk for the stop-loss is way too high. It should be no more that 5% and ideally it should be 1-2%. And if you have multiple trades at a time then consider using even lower risk.
Already answered ... "Please run a search as this has been discussed many times before in the forum."
Forum on trading, automated trading systems and testing trading strategies
calculating volume based on risk amount for gold
Fernando Carreiro, 2023.12.10 21:05
In MQL5, you can just use the function OrderCalcProfit instead.Forum on trading, automated trading systems and testing trading strategies
Trying to replicate OrderCalcMargin()
Fernando Carreiro, 2023.10.31 22:40
You will find it here ... https://www.mql5.com/en/docs/constants/environment_state/marketinfoconstants#enum_symbol_calc_mode
Both the OrderCalcMargin and OrderCalcProfit functions will calculate differently depending on the ENUM_SYMBOL_CALC_MODE
Forum on trading, automated trading systems and testing trading strategies
SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_VALUE) sometimes zero
Fernando Carreiro, 2022.08.23 17:41
You can! These are the steps I take. I supply the function with a lot size equal to the “Max Lot Size” allowed for the symbol in question, then calculate the ratio needed to achieve the fractional risk that I wish to apply, to get the correct volume for the order. I then align that with the “Lot Step” and finally check it against both the maximum and minimum allowed lots for the symbol.
The reason I use the “maximum” lots instead of just “1.0” lots as a reference value is because there is no guarantee that the value of 1.0 is within the minimum and maximum values allowed. Given that using 1.0, or the maximum, gives equivalent results anyway (by using the ratio method), I choose to use the “max lots” as the reference point which also offers the most precision for the calculation.
Something like this ...
// This code will not compile. It is only a example reference if( OrderCalcProfit( eOrderType, _Symbol, dbLotsMax, dbPriceOpen, dbPriceStopLoss, dbProfit ) ) { dbOrderLots = fmin( fmax( round( dbRiskMax * dbLotsMax / ( -dbProfit * dbLotsStep ) ) * dbLotsStep, dbLotsMin ), dbLotsMax ); // the rest of the code ... };
Forum on trading, automated trading systems and testing trading strategies
Fernando Carreiro, 2023.11.03 14:25
No, there is no historical record of account leverage, nor can you calculate the leverage from a position's trade details.
You should not be setting a positions size (volume) primarily based on margin/leverage. That is a dangerous way of trading.
A position's size (volume) should be calculated based primarily on stop-loss size and your risk amount, and then only further checked against required margin as a secondary concern.
Calculates the margin required for the specified order type, in the deposit currency
Calculates the profit based on the parameters passed, in the deposit currency
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use