Formula to calculate risk lotsize - page 2

 
Fernando Carreiro #:

If you want a stop-loss of the size of one ATR with 1% risk, then...

Hello, i want to let you know that i did find often problem with your lotsize calculation function in the way how you have build it, but the error is not from technical you can say, it is something about zero calcuate error, but the complete EA does stop to work if this happen, i have slove it with one additional if statement, but i have the feeling that to often this statement is true and then no calculations are done, take a look at the following code then you maybe understand better what i mean:


double dblLotsRisk( string symbol, double dbRiskRatio )
{
   double dbStopLoss = iATR(symbol,PERIOD_CURRENT,10,0);
  
   if(dbStopLoss>0 && AccountInfoDouble( ACCOUNT_EQUITY) >0 && AccountInfoDouble( ACCOUNT_MARGIN_FREE)>0 && AccountInfoDouble( ACCOUNT_BALANCE)>0 && SymbolInfoDouble( _Symbol, SYMBOL_VOLUME_STEP)>0 && SymbolInfoDouble( _Symbol, SYMBOL_VOLUME_MAX) >0 && SymbolInfoDouble( _Symbol, SYMBOL_VOLUME_MIN)>0 && SymbolInfoDouble( _Symbol, SYMBOL_TRADE_TICK_VALUE )>0 && SymbolInfoDouble( _Symbol, SYMBOL_TRADE_TICK_SIZE  )>0)
   {
   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 );
   } else return (0);
};

Maybe you have a idea how to optimize it to make some calculations also if some variables have zero value, because currently it looks like to often some of the variables have zero and then no risk lotsize is calculated, i can not really say also which of this variables have often zero value, i find it also little bit complicate how you define your variables writine just one double constructor and then with komma all other values, it would be more usefull to save every value in his own double and then after that you can write a if statement to see which is zero and then create a Alert that is more useful for debugin.

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

This code is incorrect in 2 aspects regardless of the source of origin

First is explained
here =1/1=> https://www.mql5.com/en/forum/229850/page3#comment_52736349
and
here =1/2=> https://www.mql5.com/en/forum/112782#comment_3091472

Second  is explained
here =2/1=> https://www.mql5.com/en/forum/112782#comment_3091475
and
here =2/2=> https://www.mql5.com/en/forum/112782#comment_3091476

Correct code sample

//+----------------------------------------------------------------------------------------+
//|                                                  Formula to calculate risk lotsize.mq4 |
//|                                              Copyright 2024, MetaQuotes Software Corp. |
//|                                                                   https://www.mql5.com |
//+----------------------------------------------------------------------------------------+
#property copyright "Copyright 2024, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
//+----------------------------------------------------------------------------------------+
//| Script program start function                                                          |
//+----------------------------------------------------------------------------------------+
double RiskRate      = 0.05                                                          ; // 01
                                                                                       // 02
void   OnStart ()                                                                      // 03
{                                                                                      // 04
double MaxLots       = MarketInfo  ( Symbol () , MODE_MAXLOT         )               ; // 05
double MinLots       = MarketInfo  ( Symbol () , MODE_MINLOT         )               ; // 06
double LotStep       = MarketInfo  ( Symbol () , MODE_LOTSTEP        )               ; // 07
double NominalMargin = MarketInfo  ( Symbol () , MODE_MARGINREQUIRED )               ; // 08
                                                                                       // 09
double Risk          = RiskRate    * AccountEquity ()                                ; // 10
double SizeLimit     = Risk        / NominalMargin                                   ; // 11
double LotSize                                                                       ; // 12
                                                                                       // 13
if   ( SizeLimit    >= MinLots )                                                       // 14
     { int Steps     = (int) MathFloor ( ( SizeLimit - MinLots ) / LotStep )         ; // 15
       LotSize       =                                 MinLots   + LotStep * Steps ; } // 16
else   LotSize       = 0                                                             ; // 17
                                                                                       // 18
if   ( LotSize      >= MaxLots )                                                       // 19
       LotSize       = MaxLots                                                       ; // 20
                                                                                       // 21
Alert(" "                                                                          ) ; // 22
Alert("LotSize       = " , LotSize          , " " , "lots"                         ) ; // 23
Alert("Output :"                                                                   ) ; // 24
Alert(" "                                                                          ) ; // 25
Alert("SizeLimit     = " , SizeLimit        , " " , "lots"                         ) ; // 26
Alert("LotStep       = " , LotStep          , " " , "lots"                         ) ; // 27
Alert("MinLots       = " , MinLots          , " " , "lots"                         ) ; // 28
Alert("MaxLots       = " , MaxLots          , " " , "lots"                         ) ; // 29
Alert("NominalMargin = " , NominalMargin    , " " , AccountCurrency ()             ) ; // 30
Alert("Processing :"                                                               ) ; // 31
Alert(" "                                                                          ) ; // 32
Alert("Risk          = " , Risk             , " " , AccountCurrency ()             ) ; // 33
Alert("RiskRate      = " , RiskRate * 100   , " " , " %"                           ) ; // 34
Alert("AccountEquity = " , AccountEquity () , " " , AccountCurrency ()             ) ; // 35
Alert("Input :"                                                                    ) ; // 36
}                                                                                      // 37
//+----------------------------------------------------------------------------------------+
OrderSend: Error code 131
OrderSend: Error code 131
  • 2024.03.14
  • Ahmed Ali
  • www.mql5.com
I get this error on validation my product but it is running well on backtests...
 
AIRAT SAFIN #:

This code is incorrect in 2 aspects regardless of the source of origin

First is explained
here =1/1=> https://www.mql5.com/en/forum/229850/page3#comment_52736349
and
here =1/2=> https://www.mql5.com/en/forum/112782#comment_3091472

Second  is explained
here =2/1=> https://www.mql5.com/en/forum/112782#comment_3091475
and
here =2/2=> https://www.mql5.com/en/forum/112782#comment_3091476

Correct code sample

thank you for your code, but the other function which i get from Fernando is working with stoploss your function looks like working with other technic. His function is also calculation correctly, but during trading and using that functions i have see sometimes the error caluate by zero and that have make the EA stop working, it is happening because of the variables they have sometimes no value maybe if the metatrader have not open a chart for that and terminal have no data