Calculate lot size

 

Hello Everyone, 

I'm new here, long time ago I used to be a programmer - changed my mind but I restarted a few weeks ago to build my own trading script. I'm used to stock markets - not with Forex - but I did read a lot about it, and try to understand everything I've read. I've got good notions about risk management, % of trade and balance, etc ... 

So to define my script, what I actually did : 

- identify signals I want to use (RSI, Moving average, etc ...)

- Building my MqlTradeRequest

- Defining my stop loss

- Defining my take profit

What i do miss is to calculate my lot size. I did read a very interesting post here and I did like the solution I saw, but I think I dont fully understand the script that was posted: https://www.mql5.com/en/forum/388742

So I tried to redo the script myself, it is actually not finished, but I already am not sure about results I'm getting, or I just dont understand it ? 

Here the script I'm going for step by step, to understand what I'm doing : 

   double setLotSize( double dbStopLoss, double dbRiskRatio ) {
      double
                        tickRange               = this.request.type == ORDER_TYPE_BUY ? this.symbol_bid - this.request.sl : this.request.sl - this.symbol_ask,
                        lotsMin                 = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MIN), 
                        lotsMax                 = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MAX),
                        lotsStep                = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_STEP),
                        tickSize                = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_SIZE), 
                        tickValue               = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_VALUE),
                        valueRisk               = AccountInfoDouble(ACCOUNT_BALANCE) * dbRiskRatio, 
                        riskPerPip              = valueRisk * tickRange,
                        riskPerTick             = riskPerPip / tickValue, 
                        lotSize                 = riskPerTick / (valueRisk * tickSize);
                        
      
      Print("tickRange : ", tickRange);
      Print("lotsMin : ", lotsMin);
      Print("lotsMax : ", lotsMax);
      Print("lotsStep : ", lotsStep);
      Print("tickSize : ", tickSize);
      Print("tickValue : ", tickValue);
      Print("valueRisk : ", valueRisk);
      Print("riskPerPip : ", riskPerPip);
      Print("riskPerTick : ", riskPerTick);
      Print("lotSize : ", lotSize);
      
      // Calcul de la taille de la position en lots
      //double riskPerPip = valueRisk / tickRange;
      //double riskPerTick = riskPerPip / tickValue;
      //double lotSizeUnrounded = riskPerTick / (100000 * tickSize);
      //lotSize = NormalizeDouble(lotSizeUnrounded, 2); // Arrondir à 2 décimales
      
      Print("lotSize : ", lotSize);
      
      return lotSize;

Here the printed results : 

2023.08.09 14:11:25.585 2023.08.07 00:00:00   tickRange : 0.006610000000000005
2023.08.09 14:11:25.585 2023.08.07 00:00:00   lotsMin : 0.01
2023.08.09 14:11:25.585 2023.08.07 00:00:00   lotsMax : 10000.0
2023.08.09 14:11:25.585 2023.08.07 00:00:00   lotsStep : 0.01
2023.08.09 14:11:25.585 2023.08.07 00:00:00   tickSize : 0.00001
2023.08.09 14:11:25.586 2023.08.07 00:00:00   tickValue : 0.9081909743980966
2023.08.09 14:11:25.586 2023.08.07 00:00:00   valueRisk : 300.0
2023.08.09 14:11:25.586 2023.08.07 00:00:00   riskPerPip : 1.9830000000000014
2023.08.09 14:11:25.586 2023.08.07 00:00:00   riskPerTick : 2.183461470000001
2023.08.09 14:11:25.586 2023.08.07 00:00:00   lotSize : 7.278204900000004e-08
2023.08.09 14:11:25.586 2023.08.07 00:00:00   lotSize : 7.278204900000004e-08


The lotSize seems a little bit too big no :) ? 

Hope someone will be able to help ! Thanks a lot for your answer. 

How to calculate proper lot size ?
How to calculate proper lot size ?
  • 2022.02.11
  • www.mql5.com
Hi, So i'm trying to build a scalping system that watch the market in 5 minutes timeframe and execute trades based on simple algorithm, until here...