MQL5: How to calculate the maximum number of lots that can be placed in an order based on balance/equity and leverage?

 
My EA still orders buy/sell stop (alway have a fix stoploss) and matches orders normally. However, at certain times when the market fluctuated strongly, my broker reduced the leverage to only 1:200, causing the stop orders to be canceled when hit price because not enough money.

Can I calculate maximum lots, based on my balance/equity account and leverage? So when the leverage goes down to 1:200, EA can change lot size to smaller to avoid be cancellation. Thank you.
 
Your topic has been moved to the section: Expert Advisors and Automated Trading
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893
 
totebobi: My EA still orders buy/sell stop (alway have a fix stoploss) and matches orders normally. However, at certain times when the market fluctuated strongly, my broker reduced the leverage to only 1:200, causing the stop orders to be canceled when hit price because not enough money. Can I calculate maximum lots, based on my balance/equity account and leverage? So when the leverage goes down to 1:200, EA can change lot size to smaller to avoid be cancellation. Thank you.

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.

 
Fernando Carreiro #:

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).

 
totebobi #:

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.

 
Fernando Carreiro #:

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.

Thank you for the advice. But about how to calculate the largest possible volume, can you guide me?
 
totebobi #: Thank you for the advice. But about how to calculate the largest possible volume, can you guide me?

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

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

Historical Account Leverage

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.

OrderCalcMargin

Calculates the margin required for the specified order type, in the deposit currency

OrderCalcProfit

Calculates the profit based on the parameters passed, in the deposit currency