Calculate Lot size based on balance

 
Hello everyone, before you tell me, I've already been searching the forum on the subject, but I can't find anything that works for me.

Since I am only going to Having only one trade open at a time, I don't need to factor in margin or anything like that

I need to know how the lot size is calculated taking into account the balance, the risk to Sl, and the distance to the sl 

THX!!
 
andresking95: Hello everyone, before you tell me, I've already been searching the forum on the subject, but I can't find anything that works for me. Since I am only going to Having only one trade open at a time, I don't need to factor in margin or anything like that. I need to know how the lot size is calculated taking into account the balance, the risk to Sl, and the distance to the sl 

This question has been asked and answer many, many times over. There are even articles on it. Please put in the effort and do a proper search.

Here is a starting point, specific for MQL5, but please do more research on the subject.

Forum on trading, automated trading systems and testing trading strategies

SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_VALUE) sometimes zero

Fernando Carreiro, 2022.08.23 16:51

Instead of using the Tick Value directly, consider using the function OrderCalcProfit, because it will apply the correct profit calculation method depending on the CALC_MODE for that symbol.

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

How to calculate lots using multiplier according to number of opened orders?

Fernando Carreiro, 2017.09.01 21:57

Don't use NormalizeDouble(). Here is some guidance (code is untested, just serves as example):

// Variables for Symbol Volume Conditions
double
   dblLotsMinimum = SymbolInfoDouble( _Symbol, SYMBOL_VOLUME_MIN  ),
   dblLotsMaximum = SymbolInfoDouble( _Symbol, SYMBOL_VOLUME_MAX  ),
   dblLotsStep    = SymbolInfoDouble( _Symbol, SYMBOL_VOLUME_STEP );
   
// Variables for Geometric Progression
double
   dblGeoRatio = 2.8,
   dblGeoInit  = dblLotsMinimum;
   
// Calculate Next Geometric Element
double
   dblGeoNext  = dblGeoInit * pow( dblGeoRatio, intOrderCount + 1 );
   
// Adjust Volume for allowable conditions
double
   dblLotsNext = fmin( dblLotsMaximum,                                     // Prevent too greater volume
                   fmax( dblLotsMinimum,                                   // Prevent too smaller volume
                     round( dblGeoNext / dblLotsStep ) * dblLotsStep ) );  // Align to Step value

Forum on trading, automated trading systems and testing trading strategies

Volume Limit Reached - Validation for new Expert Advisor error

Fernando Carreiro, 2022.07.22 18:22

Your EA must be coded to read the broker's contract specifications, such volume limitations, and prevent that from happening.

SYMBOL_VOLUME_MIN

Minimal volume for a deal

double

SYMBOL_VOLUME_MAX

Maximal volume for a deal

double

SYMBOL_VOLUME_STEP

Minimal volume change step for deal execution

double

SYMBOL_VOLUME_LIMIT

Maximum allowed aggregate volume of an open position and pending orders in one direction (buy or sell) for the symbol. For example, with the limitation of 5 lots, you can have an open buy position with the volume of 5 lots and place a pending order Sell Limit with the volume of 5 lots. But in this case you cannot place a Buy Limit pending order (since the total volume in one direction will exceed the limitation) or place Sell Limit with the volume more than 5 lots.

double

Forum on trading, automated trading systems and testing trading strategies

Complete formula for calculating forex pip value for XAUUSD with account funded in euros

Fernando Carreiro, 2022.08.29 15:43

One more thing that should be considered by the OP or those following this thread ...

After you determine your volume (lots) for your risk amount, you should then check that against the free margin, useing the OrderCalcMargin (MQL5) or order_calc_margin (Python), to verify that the amount of margin that will be required is available in your free margin.

In fact, make sure that the required margin plus the risk is not greater than your free margin and that it will not cause a margin call or stop out.

I personally set a margin % limit and reduce the lot size if required. For example, I set a maximum % margin of 5-10% and use the OrderCalcMargin to adjust the volume to reduce the volume should the margin be higher than my limit.

The reason I set it to 5-10% is because I have to account for multiple positions in the market if I am trading on multiple symbols at the same time. If I were to allow a maximum margin on my balance, then I would not have any free margin left to trade on other symbols.


 
Fernando Carreiro #:

This question has been asked and answer many, many times over. There are even articles on it. Please put in the effort and do a proper search.

Here is a starting point, specific for MQL5, but please do more research on the subject.


Okay, look. YES I have searched all over the forum and nothing answers my question, why do they take into account equity, open trades, etc. I simply want the equivalent of a lot size calculator that can be found online (like myfxbook), simply that.


And to show you that I have been investing, take a look at this for example:

// 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 );
};

You can understand the code, but what is the variable dbStopLoss? is it the price? is it the number of pips? This is why I made this post.

 
andresking95 #: Okay, look. YES I have searched all over the forum and nothing answers my question, why do they take into account equity, open trades, etc. I simply want the equivalent of a lot size calculator that can be found online (like myfxbook), simply that. And to show you that I have been investing, take a look at this for example:

You can understand the code, but what is the variable dbStopLoss? is it the price? is it the number of pips? This is why I made this post.

The code you have quoted is my own code from one of my posts a long time ago, regarding mostly MQL4. However, for MQL5 you should consider my previous post instead.

You can use only the Balance if you wish. In my original post, I explained why I was using the minimum of the three for a more conservative approach.

The variable "dbStopLoss" is the stop-loss size in quote price difference (the absolute value of the difference between the opening price and the stop-loss price).

The online calculators are very basic, but real trading is more complex. There are more variables to consider as well as the contract specification limits set by the broker.

 
Fernando Carreiro #:

The code you have quoted is my own code from one of my posts a long time ago, regarding mostly MQL4. However, for MQL5 you should consider my previous post instead.

You can use only the Balance if you wish. In my original post, I explained why I was using the minimum of the three for a more conservative approach.

The variable "dbStopLoss" is the stop-loss size in quote price difference (the absolute value of the difference between the opening price and the stop-loss price).

The online calculators are very basic, but real trading is more complex. There are more variables to consider as well as the contract specification limits set by the broker.

yes, I know it was your code that's why I put it. Ok, thanks, I'll try if it works in my case 
 

I once wrote it here

https://www.mql5.com/en/forum/355652/page2#comment_50590821

How to calculate Maximum lot size based on Equity
How to calculate Maximum lot size based on Equity
  • 2023.11.17
  • www.mql5.com
I have been trying to write a function that calculates the maximum lot size allowed on my account based on my account balance...
 
Maybe this is not the right place for my question, the admin can move it to another category.

I would like to know the formula, how to calculate the lot size that I need to achieve in order to reach a certain profit in currency for a certain number of ticks.

The formula should be used for all symbols, forex, non-forex, indices...

For example: I want to make a profit of $100 for 150 points, what lot size do I need to make it?


If anyone can help me with the formula, how do I do it? Here's how I tried to do it:

point_value = SymbolInfoDouble(_Symbol, MODE_POINT) * SymbolInfoDouble(_Symbol, MODE_TICKVALUE) / SymbolInfoDouble(_Symbol, MODE_TICKSIZE);
TakeProfit = 100; // in $
TargetSize = 150; // in point
LotSize = (TakeProfit / (TargetSize * point_value));
 
Milan Zivanovic #:
Maybe this is not the right place for my question, the admin can move it to another category.

I would like to know the formula, how to calculate the lot size that I need to achieve in order to reach a certain profit in currency for a certain number of ticks.

The formula should be used for all symbols, forex, non-forex, indices...

For example: I want to make a profit of $100 for 150 points, what lot size do I need to make it?


If anyone can help me with the formula, how do I do it? Here's how I tried to do it:


It's really not that easy because of the fact that some brokers don't give data for important variables in the calculation. Some brokers do and some don't. You need to use SYMBOL_MARGIN_INITIAL in the calculation, it's commonly the same value for forex currency pairs, but it is totally different for other types of assets and can be different between brokers
 
Conor Mcnamara #:

It's really not that easy because of the fact that some brokers don't give data for important variables in the calculation. Some brokers do and some don't. You need to use SYMBOL_MARGIN_INITIAL in the calculation, it's commonly the same value for forex currency pairs, but it is totally different for other types of assets and can be different between brokers

What do you think the formula should look like?

Reason: