How to determine the correct lot size for any given symbol

 
Hello coders, I know that there are other threads where there is a discussion about this topic, I found the code of William Roeder (https://www.mql5.com/en/forum/133792/page3) and I tried to implement in my code, but I'm not able to find the unique formula which works for forex (3 or 5 digits), commodities and indices cfd. I built an example with a starting capital of 5000 euros and I want to try to open a position with 30 pips stop loss risking 0.5% of my account. This code works for forex, for commodities and indices it does not. Can someone help me understanding my error? I attach the example EA, Thank you.
Basic Principles - Trading Operations - MetaTrader 5 Help
Basic Principles - Trading Operations - MetaTrader 5 Help
  • www.metatrader5.com
Before you proceed to study the trade functions of the platform, you must have a clear understanding of the basic terms: order, deal and position...
Files:
LotsREDO.mq4  4 kb
 
  1. ElGuapo: Hello coders, I know that there are other threads where there is a discussion about this topic, 

    What topic, you haven't stated one.
         How To Ask Questions The Smart Way. (2004)
              Be precise and informative about your problem

    How To Ask Questions The Smart Way. (2004)
              Use meaningful, specific subject headers.

  2. ElGuapo I found the code of William Roeder and I tried to implement in my code, but I'm not able to find the unique formula which works for forex (3 or 5 digits), commodities and indices cfd

    Unique forumula for what?
         How To Ask Questions The Smart Way. (2004)
              Be precise and informative about your problem

  3. ElGuapo: starting capital of 5000 euros and I want to try to open a position with 30 pips stop loss risking 0.5% of my account.

    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 (on leveraged symbols). 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 Free Margin to avoid stop out

    6. For MT5, see 'Money Fixed Risk' - MQL5 Code Base (2017)

    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.

  4. ElGuapo This code works for forex, for commodities and indices it does not. Can someone help me understanding my error? I attach the example EA,

    “Doesn't work” is meaningless — just like saying the car doesn't work. Doesn't start, won't go in gear, no electrical, missing the key, flat tires — meaningless.
         How To Ask Questions The Smart Way. (2004)
              When asking about code
              Be precise and informative about your problem

    See № 3.3. Did you “verify”?

 
William Roeder #:
  1. What topic, you haven't stated one.
         How To Ask Questions The Smart Way. (2004)
              Be precise and informative about your problem

    How To Ask Questions The Smart Way. (2004)
              Use meaningful, specific subject headers.

  2. Unique forumula for what?
         How To Ask Questions The Smart Way. (2004)
              Be precise and informative about your problem

  3. 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 (on leveraged symbols). 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 Free Margin to avoid stop out

    6. For MT5, see 'Money Fixed Risk' - MQL5 Code Base (2017)

    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.

  4. “Doesn't work” is meaningless — just like saying the car doesn't work. Doesn't start, won't go in gear, no electrical, missing the key, flat tires — meaningless.
         How To Ask Questions The Smart Way. (2004)
              When asking about code
              Be precise and informative about your problem

    See № 3.3. Did you “verify”?

Hello William, sorry for being not clear. I'm developing a way to determine the lot size for any given symbol given (as i wrote), the following conditions: 

"starting capital of 5000 euros and I want to try to open a position with 30 pips stop loss risking 0.5% of my account"

These information are enough to determine the lot size once you know how much a pip is valued in your account currency and for that given symbol.


lotsize = (AccountEquity() * risk) / (stop_loss (in pips) * value_per_pip)


I attached the EA which incapsulates your formulas (like DeltaValuePerLot (https://www.mql5.com/en/forum/133792/page3), and I'm trying to obtain the lot size opening a position. This code works for forex (5 and 3 digits) but not for Commodities and Indices. I think that the problem comes from the correct number of initial digits, or maybe in the pips2dbl number of decimals, but I'm not found the part of code which is wrong to compute the right lots for these symbols (for example XAUUSD, US30).


Regarding MODE_TICKVALUE, this seems to work for forex pairs, so why should not be for the others? I double checked with this website ( <Deleted> )

 
ElGuapo #: Hello William, sorry for being not clear. I'm developing a way to determine the lot size for any given symbol given (as i wrote), the following conditions: "starting capital of 5000 euros and I want to try to open a position with 30 pips stop loss risking 0.5% of my account". These information are enough to determine the lot size once you know how much a pip is valued in your account currency and for that given symbol. lotsize = (AccountEquity() * risk) / (stop_loss (in pips) * value_per_pip) I attached the EA which incapsulates your formulas (like DeltaValuePerLot (https://www.mql5.com/en/forum/133792/page3), and I'm trying to obtain the lot size opening a position. This code works for forex (5 and 3 digits) but not for Commodities and Indices. I think that the problem comes from the correct number of initial digits, or maybe in the pips2dbl number of decimals, but I'm not found the part of code which is wrong to compute the right lots for these symbols (for example XAUUSD, US30). Regarding MODE_TICKVALUE, this seems to work for forex pairs, so why should not be for the others? I double checked with this website ( <Deleted> )

If you want to develop a method that will work on ALL symbol types, then stop thinking in terms of "pips".

Instead work with "ticks" (tick size and tick value), so that you don't have to worry about the number of "digits" or what is a "pip" or a "point".

Forum on trading, automated trading systems and testing trading strategies

Tick size vs Point(), can be a little tricky in Multicurrency EA

Fernando Carreiro, 2022.03.09 12:11

Tick Size and Point Size can be very different especially on stocks and other symbols besides forex.

Always use Tick Size to adjust and align your prices, not the point size. In essence, make sure that your price quotes, are properly aligned to the Tick size (see following examples).

...
double tickSize = SymbolInfoDouble( _Symbol, SYMBOL_TRADE_TICK_SIZE );
...
double normalised_price = round( price / tick_size ) * tick_size;
...
// Or use a function
double Round2Ticksize( double price )
{
   double tick_size = SymbolInfoDouble( _Symbol, SYMBOL_TRADE_TICK_SIZE );
   return( round( price / tick_size ) * tick_size );
};

Forum on trading, automated trading systems and testing trading strategies

Symbol Point Value

Fernando Carreiro, 2022.06.02 01:14

Here are two examples from AMP Global (Europe):

  • Micro E-mini S&P 500 (Futures): point size = 0.01, tick size = 0.25, tick value = $1.25
  • EURO STOXX Banks (Stock Index): point size = 0.01, tick size = 0.05, tick value = €2.50

Forum on trading, automated trading systems and testing trading strategies

Symbol Point Value

Fernando Carreiro, 2022.05.18 21:05

double
   dbTickSize   = SymbolInfoDouble( _symbol, SYMBOL_TRADE_TICK_SIZE  ), // Tick size
   dbTickValue  = SymbolInfoDouble( _symbol, SYMBOL_TRADE_TICK_VALUE ), // Tick value
   dbPointSize  = SymbolInfoDouble( _symbol, SYMBOL_POINT ),            // Point size
   dbPointValue = dbTickValue * dbPointSize / dbTickSize;               // Point value
Remember, it's best to use tick size and tick value in your calculations, instead of point size and its value.
 
Fernando Carreiro #:

If you want to develop a method that will work on ALL symbol types, then stop thinking in terms of "pips".

Instead work with "ticks" (tick size and tick value), so that you don't have to worry about the number of "digits" or what is a "pip" or a "point".

Fernando thanks for your reply. However if you look at my code, I already tried to adapt it in tick and not pips for some of the computations. In real life, people reason usually with pips, so I write 30 pips, because if for some reason I need to develop an EA, I would like to put an external parameter where user set the number of pips for the symbol of interest, and internally the code transform that 30 pips to tick points or whatever is good for mql4. I tried to implement this kind of logic and I posted my code here as an attempt to do that, and not for asking to the forum to do the entire job for me. From my understanding, I produced my code and I tried to be compliant with these logics, however I have done something wrong apparently and I'm not able to figure it out.


Other logics, like the last that uou provided to me (normalized price) is not present in my code but I'm not able to understand and adjust my code to integrate this logic becuase I don't get the point. 


If my code is not clear in some points, I would be very happy to explain to you or to people willing to help me. 

 
ElGuapo #: Fernando thanks for your reply. However if you look at my code, I already tried to adapt it in tick and not pips for some of the computations. In real life, people reason usually with pips, so I write 30 pips, because if for some reason I need to develop an EA, I would like to put an external parameter where user set the number of pips for the symbol of interest, and internally the code transform that 30 pips to tick points or whatever is good for mql4. I tried to implement this kind of logic and I posted my code here as an attempt to do that, and not for asking to the forum to do the entire job for me. From my understanding, I produced my code and I tried to be compliant with these logics, however I have done something wrong apparently and I'm not able to figure it out. Other logics, like the last that uou provided to me (normalized price) is not present in my code but I'm not able to understand and adjust my code to integrate this logic becuase I don't get the point. If my code is not clear in some points, I would be very happy to explain to you or to people willing to help me. 

"pip" is only used mostly for Forex Currency pairs. Most other symbols don't use "pips".

So, if your aim is to make your EA work for all types of symbols, you CAN'T have a parameter as "pips" because it will then not work on other symbols besides Forex. For non-Forex symbols it is impossible to convert from "pips" because such a thing does not exist for those symbols.

If however, you only work in "ticks", then in the case of Forex, users can convert "pips" to "ticks" very easily just by multiplying by 10.