From 0d20d7239ed9f50702e491e27ce947f687b67d4f Mon Sep 17 00:00:00 2001 From: yerden Date: Tue, 10 Apr 2018 05:54:54 +0600 Subject: [PATCH] utils: fix rounding functions (#38) Standard MathMod() function may return not-so-correct results so it cannot be relied upon when rounding. For example, MathMod(0.6,0.1) yields 0.099999.. which may be deceiving. This caveat makes rounding completely incorrect with current logic. Use MathRound() instead. Changes: * modify roundUpToMultiple/roundDownToMultiple to use MathRound(); * add roundUpToStep/roundDownToStep functions calculating value with regard to a minimum value and a step. New functions are useful to calculate correct lots with SYMBOL_VOLUME_MIN/SYMBOL_VOLUME_STEP. Signed-off-by: Yerden Zhumabekov --- Utils/Math.mqh | 159 +++++++++++++++++++++++++++---------------------- 1 file changed, 89 insertions(+), 70 deletions(-) diff --git a/Utils/Math.mqh b/Utils/Math.mqh index ee3c37a..24030aa 100644 --- a/Utils/Math.mqh +++ b/Utils/Math.mqh @@ -1,70 +1,89 @@ -//+------------------------------------------------------------------+ -//| Module: Utils/Math.mqh | -//| This file is part of the mql4-lib project: | -//| https://github.com/dingmaotu/mql4-lib | -//| | -//| Copyright 2016 Li Ding | -//| | -//| Licensed under the Apache License, Version 2.0 (the "License"); | -//| you may not use this file except in compliance with the License. | -//| You may obtain a copy of the License at | -//| | -//| http://www.apache.org/licenses/LICENSE-2.0 | -//| | -//| Unless required by applicable law or agreed to in writing, | -//| software distributed under the License is distributed on an | -//| "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, | -//| either express or implied. | -//| See the License for the specific language governing permissions | -//| and limitations under the License. | -//+------------------------------------------------------------------+ -#property strict -#include "../Lang/Mql.mqh" -#include "../Lang/Number.mqh" -//+------------------------------------------------------------------+ -//| | -//+------------------------------------------------------------------+ -class Math - { -public: - template - static T max(T value1,T value2) - { - return value1 > value2 ? value1 : value2; - } - template - static T min(T value1,T value2) - { - return value1 < value2 ? value1 : value2; - } - template - static T abs(T value) - { - return value < 0 ? -value : value; - } - template - static int sign(T value) - { - return value < 0 ? -1 : 1; - } - - //--- round value up to a multiple of min - static double roundUpToMultiple(double value,double min) - { - double r=MathMod(value,min); - return Mql::isEqual(r,0.0) ? value : (value-r+min); - } - //--- round value down to a multiple of min - static double roundDownToMultiple(double value,double min) - { - double r=MathMod(value,min); - return Mql::isEqual(r,0.0) ? value : (value-r); - } - - static double linearInterpolate(double x1,double x2,double y1,double y2,double x) - { - if(Mql::isEqual(x1,x2)) return Double::NaN; - return y1 + (y1-y2)*(x-x1)/(x1-x2); - } - }; -//+------------------------------------------------------------------+ +//+------------------------------------------------------------------+ +//| Module: Utils/Math.mqh | +//| This file is part of the mql4-lib project: | +//| https://github.com/dingmaotu/mql4-lib | +//| | +//| Copyright 2016 Li Ding | +//| | +//| Licensed under the Apache License, Version 2.0 (the "License"); | +//| you may not use this file except in compliance with the License. | +//| You may obtain a copy of the License at | +//| | +//| http://www.apache.org/licenses/LICENSE-2.0 | +//| | +//| Unless required by applicable law or agreed to in writing, | +//| software distributed under the License is distributed on an | +//| "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, | +//| either express or implied. | +//| See the License for the specific language governing permissions | +//| and limitations under the License. | +//+------------------------------------------------------------------+ +#property strict +#include "../Lang/Mql.mqh" +#include "../Lang/Number.mqh" +//+------------------------------------------------------------------+ +//| | +//+------------------------------------------------------------------+ +class Math + { +public: + template + static T max(T value1,T value2) + { + return value1 > value2 ? value1 : value2; + } + template + static T min(T value1,T value2) + { + return value1 < value2 ? value1 : value2; + } + template + static T abs(T value) + { + return value < 0 ? -value : value; + } + template + static int sign(T value) + { + return value < 0 ? -1 : 1; + } + + //--- round value up to a multiple of min + static double roundUpToMultiple(double value,double min) + { + int n=(int)MathRound(value/min); + return Mql::isEqual(n*min,value) ? n*min : ((int)(value/min)+1)*min; + } + //--- round value down to a multiple of min + static double roundDownToMultiple(double value,double min) + { + int n=(int)MathRound(value/min); + return Mql::isEqual(n*min,value) ? n*min : (int)(value/min)*min; + } + + //--- round value up to a min+multiple*step + static double roundUpToStep(double value,double min,double step) + { + value-=min; + int n=(int)MathRound(value/step); + if(!Mql::isEqual(n*step,value)) n=(int)(value/step)+1; + return n*step+min; + } + + //--- round value down to a min+multiple*step + static double roundDownToStep(double value,double min,double step) + { + value-=min; + int n=(int)MathRound(value/step); + if(!Mql::isEqual(n*step,value)) n=(int)(value/step); + return n*step+min; + } + + static double linearInterpolate(double x1,double x2,double y1,double y2,double x) + { + if(Mql::isEqual(x1,x2)) return Double::NaN; + return y1 + (y1-y2)*(x-x1)/(x1-x2); + } + }; +//+------------------------------------------------------------------+ +