MQL4 MathMax/MathMin with NormalizeDouble anomaly

137 Views Asked by At

In an MQL4 EA, using a calculated moving average for a stoploss, I used MathMax and Min to make sure I wasn't too close. The results were a bit off, so I put in a Print() to get me details:

stoploss=MathMax(NormalizeDouble(Ask+(minstoplevel*Point),Digits),NormalizeDouble(MA_Line,Digits));
Print("Sell SLCalc = "+NormalizeDouble(Ask+(minstoplevel*Point),Digits)+", MA_Line = "+NormalizeDouble(MA_Line,Digits),", Max is ",MathMax(NormalizeDouble(Ask+(minstoplevel*Point),Digits),NormalizeDouble(MA_Line,Digits)));

The result:

2023.03.24 16:46:36.607 2022.07.18 03:14:29  MA1 GBPUSD,M1: Sell SLCalc = 1.18836, MA_Line = 1.18835, Max is 1.1884

I was expecting the Max to have the same 5-digit precision. Why is the MathMax function rounding the result, when I've specified the point to the function, rather than the pip?

It may seem like a small difference, but that's the world of Forex, and a small rounding error could stop me out or keep me in a long trend. Besides, I just want things to work as advertised, or understand why they don't.

1

There are 1 best solutions below

0
PaulB On

There is nothing inherently wrong with your code, however by printing the values you are casting them to a string. You should use DoubleToString() around your printed functions as follows:

Print("Sell SLCalc = "+DoubleToString(Ask+(minstoplevel*Point),Digits)+", MA_Line = "+DoubleToString(MA_Line,Digits),", Max is ",DoubleToString(MathMax(NormalizeDouble(Ask+(minstoplevel*Point),Digits),NormalizeDouble(MA_Line,Digits)),Digits));