MathRound Problem

 

Hey everyone, I wanted to create a function for risk management as seen in the code below. 

double OptimalLotSize(double maxRiskPercentage, double ATR_Lot_Multiplier)
{  
   double accEquity = AccountEquity();
   double lotSize = MarketInfo(_Symbol,MODE_LOTSIZE);
   double tickValue = MarketInfo(_Symbol,MODE_TICKVALUE);
   double ATR = iATR(_Symbol,_Period,14,1); 
   double maxLoss = ATR*ATR_Lot_Multiplier;
   if(_Digits <= 3)
   {
      tickValue = tickValue/100;
      maxLoss = ATR*ATR_Lot_Multiplier/100;
   }
   double maxLossCurrency = accEquity * maxRiskPercentage;
   double maxLossinQuoteCurr = maxLossCurrency / tickValue;
   double optimalLotSize = maxLossinQuoteCurr/ maxLoss/lotSize;
   double optimalLotSizeRounded = MathRound(optimalLotSize/MarketInfo(_Symbol,MODE_LOTSTEP)) * MarketInfo(_Symbol,MODE_LOTSTEP);
   return optimalLotSizeRounded;
}

After sending 0.01 as the risk percentage and 1.5 for the SL Multiplier, the code seems to work fine. However, when observing the optimalLotSizeRounded variable with Comment() during visual EA backtesting, the determined lot size sometimes does not get set to a round number, i.e. 0.4200000000001. This happens rarely though and has not been an issue for sending orders so far, but after selecting trades with "OrderLots()==" for partial closes later on, the EA cant seem to access these non round numbers. I saw a few posts on this board advising to stay away from "NormalizeDouble()", so I tried to use "MathRound()" by employing "MarketInfo" to convert the lots into an integer to be able to round this number and then convert it back into a double.

If anyone knows why the "MathRound()" sometimes (rarely) returns a double with so many decimal places I would greatly appreciate your help! Also I apologize if the code is bad, I'm new to programming and this snippet is a combination of two tutorials and some own attempts at coding.

Documentation on MQL5: Math Functions / MathRound
Documentation on MQL5: Math Functions / MathRound
  • www.mql5.com
MathRound - Math Functions - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 

Floating-point has an infinite number of decimals, it's you, not understanding floating-point and that some numbers can't be represented exactly. (like 1/10.)
          Double-precision floating-point format - Wikipedia

See also The == operand. - MQL4 programming forum 2013.06.07

If you want to see the correct number of digits, convert it to a string with the correct/wanted accuracy.
          question about decima of marketinfo() - MQL4 programming forum 2016.05.18

 
William Roeder:

If you want to see the correct number of digits, convert it to a string with the correct/wanted accuracy.
          question about decima of marketinfo() - MQL4 programming forum 2016.05.18

Thanks for your response, so Id have to convert the lots into a string with 2 decimal places and then back into a double so MQL4 can continue to use the number for further calculations? Wouldnt I encounter the floating-point problem again?
 
cne53622d so Id have to convert the lots into a string with 2 decimal places and then back into a double so MQL4 can continue to use the number for further calculations? Wouldnt I encounter the floating-point problem again?

Yes you would. You already rounded your lot size to lot step — you are done. (№ 3.) Your problem is the result of you printing your doubles.

  1. SL/TP (stops) need to be normalized to tick size (not Point) — code fails on metals. (On 5Digit Broker Stops are only allowed to be placed on full pip values. How to find out in mql? - MQL4 programming forum) and abide by the limits Requirements and Limitations in Making Trades - Appendixes - MQL4 Tutorial and that requires understanding floating point equality Can price != price ? - MQL4 programming forum

  2. Open price for pending orders need to be adjusted. On Currencies, Point == TickSize, so you will get the same answer, but it won't work on Metals. So do it right: Trailing Bar Entry EA - MQL4 programming forum or Bid/Ask: (No Need) to use NormalizeDouble in OrderSend - MQL4 programming forum

  3. Lot size must also be adjusted to a multiple of LotStep and check against min and max. If that is not a power of 1/10 then NormalizeDouble is wrong. Do it right.

  4. MathRound() and NormalizeDouble() are rounding in a different way. Make it explicit.
              MT4:NormalizeDouble - MQL5 programming forum
              How to Normalize - Expert Advisors and Automated Trading - MQL5 programming forum

  5. Prices you get from the terminal are already normalized.

  6. PIP, Point, or Tick are all different in general.
              What is a TICK? - MQL4 programming forum 2014.08.03

 

So after using the OptimalLotSize() as seen in my first post, I save the value in a global double "Lots" and use it to open a trade with the OrderSend() function. Using "OrderLots()==Lots" after selecting the trade (during backtesting), the EA was able to close various position over 200,300 times. However, very rarely "OrderLots()==Lots" delivers a "false", even though the order was just opened with "Lots" moments before, leading to the position not being closed. Does anyone know why this works most of the time and fails sometimes?

Reason: