[NEED HELP] 365/544 should not be zero

 

Hi fellow coders.

Please explain this math to me, or where I am wrong.

I know that a division by infinite will return zero. So since I was getting zero on a division, I pointed the denominator.

Returns:


This is in the context of coding a CAGR, which is returning zero for this same reason and should not.

cagr =100*(MathPow((FinalEquity/startingEquity),(365/backtestDurationDays))-1);

 

I am assuming that the variable backtestDurationDays is an integer data-type.

So 365 which is an integer, divided by another integer will produce an integer division of 365/544 and result in zero.

To prevent that use a floating point number:

print( "365/days", 365.0 / backtestDurationDays );

Also, please never post images of code. Use the CODE button (Alt-S) when inserting code

Code button in editor

 

Forum on trading, automated trading systems and testing trading strategies

Rare code issue

Fernando Carreiro, 2021.07.23 22:20

Please understand that dividing by 100 or dividing by 100.0 will give totally different results.

In the former, 100 is considered an Integer literal constant and so it will carry out integer division which will truncate the decimal values.

In the latter, 100.0 will be considered a floating-point literal constant and it will carry out floating-point division which will keep the decimal values.

This is the principal behind the problems you are having. So, to prevent this, either use a floating-point literal constant or typecast the variable into a (double).

Example:

Print( "90   / 100   = ", 90   / 100   );
Print( "90   / 100.0 = ", 90   / 100.0 );
Print( "90.0 / 100   = ", 90.0 / 100   );
Print( "90.0 / 100.0 = ", 90.0 / 100.0 );
2021.07.23 21:26:57.060 TestCopy EURUSD,H1: 90   / 100   = 0
2021.07.23 21:26:57.060 TestCopy EURUSD,H1: 90   / 100.0 = 0.9
2021.07.23 21:26:57.060 TestCopy EURUSD,H1: 90.0 / 100   = 0.9
2021.07.23 21:26:57.060 TestCopy EURUSD,H1: 90.0 / 100.0 = 0.9

Forum on trading, automated trading systems and testing trading strategies

MT4 forcing doubles into int

Fernando Carreiro, 2022.03.23 17:53

Typecast them into floating point — Typecasting - Data Types - Language Basics - MQL4 Reference

double Average = (double) Bars /          Found;
double Average =          Bars / (double) Found;
double Average = (double) Bars / (double) Found;
 
Fernando Carreiro #:

Makes a lot of sense.

Let me correct it and re-run.

EDIT: 365/days: 0.6709558823529411

Thank you for your input, Fernando! Fixed my problem!