illegal switch expression type - error - page 2

 
double step = MarketInfo(Symbol(), MODE_LOTSTEP);
   Lotdecimal_0_1_2=-log10(step);
Code breaks if lot step is not a power of 10.

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

 
William Roeder:
Code breaks if lot step is not a power of 10.

When I suggested the -log10() function I was giving a solution referring to his very limited "switch" list, that also only allowed for the values 0.01, 0.1 and 1. For these cases the function works. If you include other values, e.g.

-log10(0.02)=1.698

-log10(0.3)=0.523

-log10(0.4)=0.398

rounding UP (not MathRound) is the correct way to obtain the number of digits, as can be seen with the example -log10(0.4), because the result is below 0.5, so MathRound should return 0, whereas the number of digits after the decimal point for 0.4 is 1

so I guess

ceil(-log10(step)) should be correct, not round(-log10(step));

or am I getting this wrong?

 
Chris70: or am I getting this wrong?

What is wrong is doing it at all. "If that is not a power of 1/10," then Lotdecimal_0_1_2 is meaningless.

 
Hi, I am getting this same error 0.1 constant expression is not integral ,can someone please help me?
 

Forget about switch. Use if. 

No need to make it complicated. 

      if (step == 0.01) Lotdecimal = 2;
      if (step == 0.1)  Lotdecimal = 1;
      if (step == 1)    Lotdecimal = 0; 
 
amrali:

Forget about switch. Use if. 

No need to make it complicated. 

Yes, my opinion too: keep it simple.

But suppose you have other steps (like 0.25 or 0.5) then I have an unconventional (but working) solution, which I use for long time: the function GetNoDigits.

Please see -


#property strict
int GetNoDigits (double step) {
   int d = 0;
   double f = 1.0;
   // Emergency stop at 10
   while (d <= 10) {
      double m = f*step;
      if (MathFloor(m) == m) {
         return d;
      }
      f = f * 10.0;
      d++;
   }
   return 0;
}
void OnStart() {
   PrintFormat("START\n");
   // For test of GetNoDigits
   double steps[] = {1.0, 0.1, 0.25, 0.5, 0.01, 0.025, 0.05, 0.001, 0.0001};
   for (int i = 0; i < ArraySize(steps); i++) {
      PrintFormat ("step %6.4f  ===> ndigits %4d", steps[i], GetNoDigits(steps[i]));
   }
}


Output

2021.06.09 14:04:51.782 NoDigits (DE30,D1)      START
2021.06.09 14:04:51.782 NoDigits (DE30,D1)      
2021.06.09 14:04:51.782 NoDigits (DE30,D1)      step 1.0000  ===> ndigits    0
2021.06.09 14:04:51.782 NoDigits (DE30,D1)      step 0.1000  ===> ndigits    1
2021.06.09 14:04:51.782 NoDigits (DE30,D1)      step 0.2500  ===> ndigits    2
2021.06.09 14:04:51.782 NoDigits (DE30,D1)      step 0.5000  ===> ndigits    1
2021.06.09 14:04:51.782 NoDigits (DE30,D1)      step 0.0100  ===> ndigits    2
2021.06.09 14:04:51.782 NoDigits (DE30,D1)      step 0.0250  ===> ndigits    3
2021.06.09 14:04:51.782 NoDigits (DE30,D1)      step 0.0500  ===> ndigits    2
2021.06.09 14:04:51.782 NoDigits (DE30,D1)      step 0.0010  ===> ndigits    3
2021.06.09 14:04:51.782 NoDigits (DE30,D1)      step 0.0001  ===> ndigits    4


Enjoy your life

Matthias

------------------------------------------------------

"Is it more important to feel strong than to be strong."

 
Dr Matthias Hammelsbeck:

Yes, my opinion too: keep it simple.

But suppose you have other steps (like 0.25 or 0.5) then I have an unconventional (but working) solution, which I use for long time: the function GetNoDigits.

Please see -



Output


Enjoy your life

Matthias

------------------------------------------------------

"Is it more important to feel strong than to be strong."

Now, you have the number of digits,

great!

so what then?

It seems to be a correct answer, but for a different question.

 
Dr Matthias Hammelsbeck: But suppose you have other steps (like 0.25 or 0.5) then I have an unconventional (but working) solution, which I use for long time: the function GetNoDigits.

Stop using NormalizeDouble, It's use is usually wrong, as it is in your case.

  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 2013.06.07

  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) — 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

  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 - MQL5 programming forum
              How to Normalize - Expert Advisors and Automated Trading - MQL5 programming forum

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

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

 
William Roeder:
  1. Prices you get from the terminal are already normalized.

William, unfortunately this is a wrong statement. I don't agree with that. 
Please take a look in math_utils library for the proof. https://www.mql5.com/en/code/20822

Some prices from the terminal are off by 1 Epsilon (ulp) from the rounded value. It may be due to some post processing inside the terminal software. 
 
amrali: Some prices from the terminal are off by 1 Epsilon (ulp) from the rounded value. It may be due to some post processing inside the terminal software. 

Of course, they are. Reread my post, especially № 1 and № 2.

Reason: