how to calculate the maximum volume based on your balance

 
I searched a lot on the forum but I did not find anything that answered the question, if there was, I apologize.

it is the first time that I have encountered this problem that I am not understanding how to solve, the last EA I finished gave good results by testing it at a fixed lot, now I would like to implement the Autolot, and the problem is that being an Ea that yes based exclusively on entry and exit signals, therefore without a stoploss, I have no idea how to calculate it; I intended to calculate the maximum possible lot starting from a balance of 200, reduce it manually and gradually increase it as the balance increases, so that it could be used on many different stocks. if someone could give me some further advice, or the calculation code would be very helpful.
explanatory example:
Balance = 200; what is the maximum lot I can open on Apple?
in the same way
Balance = 200; what is the maximum lot I can open on Amazon?

I hope the translation is clear.
Thanks a lot to everyone.
Alexander.
 
Alessandro Borsari: I searched a lot on the forum but I did not find anything that answered the question, if there was, I apologize. it is the first time that I have encountered this problem that I am not understanding how to solve, the last EA I finished gave good results by testing it at a fixed lot, now I would like to implement the Autolot, and the problem is that being an Ea that yes based exclusively on entry and exit signals, therefore without a stoploss, I have no idea how to calculate it; I intended to calculate the maximum possible lot starting from a balance of 200, reduce it manually and gradually increase it as the balance increases, so that it could be used on many different stocks. if someone could give me some further advice, or the calculation code would be very helpful.

Risk can only be truely evaluated when you use some kind of stop-loss, be it real or virtual or just for risk evaluations. If you do not have any concept of a stop-loss in your strategy, even if not in some vague terms, then you can not truely calculate the maximum risk.

There are some traders that use a percentage of margin to calculate the volume, but that does not truly define a maximum risk, because you can still suffer great losses if your price change is too great (as there is no stop-loss).

So, get into the habit of calculating for some form of stop-loss or risk stop and combine it with margin calculations as well for extra protection.

Also read the following references for more information:

Forum on trading, automated trading systems and testing trading strategies

Code for Risk Management

William Roeder, 2021.02.20 17:58

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.10.10
              Is there an universal solution for Tick value? - Currency Pairs - General - MQL5 programming forum 2018.02.11
              Lot value calculation off by a factor of 100 - MQL5 programming forum 2019.07.19

  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.

Forum on trading, automated trading systems and testing trading strategies

How to calculate proper lot size ?

Fernando Carreiro, 2022.02.11 19:28

This is untested, uncompiled code, only to serve as a guide for you to further study and implement depending on your own requirements ...

// Calculate Max Lot Size based on Maximum Risk
double dblLotsRisk( double dbStopLoss, double dbRiskRatio )
{
   double
      dbLotsMinimum  = SymbolInfoDouble( _Symbol, SYMBOL_VOLUME_MIN       ),
      dbLotsMaximum  = SymbolInfoDouble( _Symbol, SYMBOL_VOLUME_MAX       ),
      dbLotsStep     = SymbolInfoDouble( _Symbol, SYMBOL_VOLUME_STEP      ),
      dbTickSize     = SymbolInfoDouble( _Symbol, SYMBOL_TRADE_TICK_SIZE  ),
      dbTickValue    = SymbolInfoDouble( _Symbol, SYMBOL_TRADE_TICK_VALUE ),
      dbValueAccount = fmin( fmin( 
                       AccountInfoDouble( ACCOUNT_EQUITY      )  , 
                       AccountInfoDouble( ACCOUNT_BALANCE     ) ),
                       AccountInfoDouble( ACCOUNT_MARGIN_FREE ) ),
      dbValueRisk    = dbValueAccount * dbRiskRatio,
      dbLossOrder    = dbStopLoss * dbTickValue / dbTickSize,
      dbCalcLot      = fmin(  dbLotsMaximum,                  // Prevent too greater volume
                       fmax(  dbLotsMinimum,                  // Prevent too smaller volume
                       round( dbValueRisk / dbLossOrder       // Calculate stop risk
                       / dbLotsStep ) * dbLotsStep ) );       // Align to step value

   return ( dbCalcLot );
};
 

 have uploaded to the codebase an expert to do lot size calculations for you

https://www.mql5.com/en/code/28029

Noete that the maximum lotsize is limited by your risk, free margin and the leverage on your account. The larger the risk or free margin or leverage, the larger lot size you can afford. 

Forex Calculators
Forex Calculators
  • www.mql5.com
Margin Calculator, Point Value Calculator, Position Size Calculator, Profit Calculator and Swap Calculator.
 
Fernando Carreiro #:

‎Il rischio può essere veramente valutato solo quando si utilizza una sorta di stop-loss, sia esso reale o virtuale o solo per valutazioni del rischio. Se non hai alcun concetto di stop-loss nella tua strategia, anche se non in termini vaghi, allora non puoi davvero calcolare il rischio massimo.‎

‎Ci sono alcuni trader che utilizzano una percentuale di margine per calcolare il volume, ma questo non definisce veramente un rischio massimo, perché puoi comunque subire grandi perdite se la tua variazione di prezzo è troppo grande (in quanto non c'è stop-loss).‎

‎Quindi, prendi l'abitudine di calcolare una qualche forma di stop-loss o stop al rischio e combinalo con i calcoli del margine per una protezione extra.‎

‎Leggi anche i seguenti riferimenti per ulteriori informazioni:‎

Thanks for the reply.

risk management is fully automated within the Ea based on several factors. therefore a large loss caused by large movements is impossible to happen. The fact remains that I have no stoploss level. which is why I would like to know how to calculate the maximum lot per balance, as explained above, and reduce it manually.

 
amrali #:

I have uploaded to the codebase an expert to do lot size calculations for you

https://www.mql5.com/en/code/28029

Note that the maximum lotsize is limited by your risk, free margin and the leverage on your account. The larger the risk or free margin or leverage, the larger lot size you can afford. 

Thanks for the reply.

not what I'm looking for, as you may have read. automatic assignment based on stoploss is not a problem.

the point is that since everything is automated: risk management, closing signal ect ect. I just need to Automate assignment. to do this, I start by calculating the only thing I can calculate from the factors I have, which is the balance. once I have found the maximum lot for that balance on that specific exchange, I will manually reduce it.

but I need that string of code because I'm not jumping on how to calculate it.

 
Alessandro Borsari #: risk management is fully automated within the Ea based on several factors. therefore a large loss caused by large movements is impossible to happen. The fact remains that I have no stoploss level. which is why I would like to know how to calculate the maximum lot per balance, as explained above, and reduce it manually.

If you have risk management to prevent large movements, then you have effectively a risk-stop a stop-loss, even if it is one for a basket of orders. Use that to calculate your volume.

As I've already answered, without some sense of something resembling a stop-loss or risk stop-out for the calculation, you cannot properly ascertain your ideal order volume.

You can rely on calculating the volume based on margin however, but that is not going to work out very well. Read the various links provided in more detail and you will understand more about it.

 
Another way to have a risk-size for calculations, is to have the EA measure the Maximum Adverse Excursion of your positions and then use the average or the maximum over a certain period, as the stop-size in the calculations of the code provided.
 
Fernando Carreiro #:
Another way to have a risk-size for calculations, is to have the EA measure the Maximum Adverse Excursion of your positions and then use the average or the maximum over a certain period, as the stop-size in the calculations of the code provided.

very interesting method that I will definitely test.

Eventually, for the time being, I folded for a virtual stoploss. I have always used in my Ea the execution of the lots in percentage on Sl and it was the first time that I ran into such a problem. I understand that I have to deepen the topic, I thank you very much for the help, in case you have other useful links, I gladly accept them