Wrong computation

To add comments, please log in or register
Batuhan Ortay
189
Batuhan Ortay  
int maximumLossPerTrade;
int accountBalanceinEuro;
double maximumLossPercentage;

maximumLossPerTrade = (int)(accountBalanceinEuro / 100 * NormalizeDouble(maximumLossPercentage, 1));

When;

accountBalanceinEuro = 14232 
maximumLossPercentage = 1.8

I get 255 for maximumLossPerTrade.


But, in excel, it is 256 which is the correct value.


What is wrong in the above MQL 4 computation ?

paul selvan
627
paul selvan  

 typecasting error

maximumLossPerTrade = accountBalanceinEuro / 100 * NormalizeDouble(maximumLossPercentage, 1);
Batuhan Ortay
189
Batuhan Ortay  
paul selvan:

 typecasting error


Without typecasting, it again gives 255.

William Roeder
18928
William Roeder  
int maximumLossPerTrade;
int accountBalanceinEuro;
 accountBalanceinEuro / 100
14232/100=142, 142*1.8=255
          On MT4 v434, division quotient don't give floating point values(Bug??) - MQL4 and MetaTrader 4 - MQL4 programming forum 2012.09.18
Batuhan Ortay
189
Batuhan Ortay  


Solved it in this way:

maximumLossPerTrade = (int)((accountBalanceinEuro * NormalizeDouble(maximumLossPercentage, 1)) / 100);

Thanks William.

Batuhan Ortay
189
Batuhan Ortay  

... or like so of course :)

maximumLossPerTrade = (int)(accountBalanceinEuro / 100.0 * NormalizeDouble(maximumLossPercentage, 1));
paul selvan
627
paul selvan  

setting all variables as double gives good precision

double accountBalanceinEuro = 14232;
double maximumLossPercentage = 1.8;
double maximumLossPerTrade = accountBalanceinEuro * maximumLossPercentage/ 100 ;
Batuhan Ortay
189
Batuhan Ortay  
paul selvan:

setting all variables as double gives good precision


Of course, thanks.

To add comments, please log in or register