Comparing doubles and normalization

 

would it be ok to compare these double values this way? because I normalized it? I want to prevent calling OrderModify after if the stoploss is the same as the existing OrderStopLoss() so that I don't do unnecessary calls to modify on the server.

    const double normalizedPrice = NormalizeDouble(price, Digits);
    if (stopModify) {
        stoploss = normalizedPrice;
        if (OrderStopLoss() == stoploss) return;
    }
 

No! Don't "normalise" quote prices based on digits or point size. That is incorrect and a mistake newbies always make due to how much misinformation is out there.

Do it correctly with the tick size ...

Forum on trading, automated trading systems and testing trading strategies

Tick size vs Point(), can be a little tricky in Multicurrency EA

Fernando Carreiro, 2022.03.09 12:11

Tick Size and Point Size can be very different especially on stocks and other symbols besides forex.

Always use Tick Size to adjust and align your prices, not the point size. In essence, make sure that your price quotes, are properly aligned to the Tick size (see following examples).

...
double tickSize = SymbolInfoDouble( _Symbol, SYMBOL_TRADE_TICK_SIZE );
...
double normalised_price = round( price / tick_size ) * tick_size;
...
// Or use a function
double Round2Ticksize( double price )
{
   double tick_size = SymbolInfoDouble( _Symbol, SYMBOL_TRADE_TICK_SIZE );
   return( round( price / tick_size ) * tick_size );
};
 

Also don't compare doubles directly, especially not with equality. Read the following too ...

Forum on trading, automated trading systems and testing trading strategies

Heavy "Feature" with doubles produces unpredictable errors in EAs

William Roeder, 2019.07.16 14:13

NormalizeDouble, It's use is usually wrong.
  1. Floating point has infinite number of decimals, it's your not understanding floating point and that some numbers can't be represented exactly. (like 1/10.)
              Double-precision floating-point format - Wikipedia, the free encyclopedia

    See also The == operand. - MQL4 programming forum

  2. Print out your values to the precision you want with DoubleToString - Conversion Functions - MQL4 Reference.

  3. SL/TP (stops) need to be normalized to tick size (not Point.) (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
  4. 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
  5. 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.
  6. MathRound() and NormalizeDouble() are rounding in a different way. Make it explicit.
              MT4:NormalizeDouble - General - MQL5 programming forum
              How to Normalize - Expert Advisors and Automated Trading - MQL5 programming forum

 
thanks
 
ycomp #: thanks
You are welcome!
 

would you know if this is necessary? in my case.. where I am checking to see if a price I have calculated for a stoploss or takeprofit is the same as the existing order stoploss/takeprofit.. and if so then I don't call OrderModify

Is it ok to just call OrderModify? does MT4 have some code where it doesn't talk to the server if the stoploss and takeprofit values specified are the same?

I'm really trying to minimizing the # of modifications done at the server because some brokers limit the # of such operations that can be done per day

 
ycomp #: Is it ok to just call OrderModify? does MT4 have some code where it doesn't talk to the server if the stoploss and takeprofit values specified are the same?

No, you will get repeated error ERR_NO_RESULT

You Server
Change the SL to X It is at X!
Change the SL to X It is at X!
Change the SL to X You are insane

Insanity: doing the same thing over and over again and expecting different results.
          Unknown

Compute the new value, then check that you are moving the existing value at least a tick.
          What is a TICK? - MQL4 programming forum (2014)