Indicator value not being translated correctly

 

Hi Gurus,

appreciate any assistance!

  if(ATRCurrent <= 0.115 && ATRCurrent >= 0.093)             pATRStrength =  "VIX REVERSAL";   
     else if(ATRCurrent <= 0.092 && ATRCurrent >= 0.070 )    pATRStrength =  "WEAKENING VIX-TREND";      
     else if (ATRCurrent<= 0.069 && ATRCurrent >= 0.047 )    pATRStrength =  "VIX-TRENDING";            
     else if(ATRCurrent <= 0.046 && ATRCurrent >= 0.024 )    pATRStrength =  "START VIX-TREND";     
     else if(ATRCurrent <= 0.023 &&  ATRCurrent >= 0.010  )  pATRStrength =  "VIX REVERSAL";

As you can see from screenshots below, the ATRCurrent value is at 0.034, which should translate to pATRStrength becoming  "START VIX-TREND" instead of "VIX-TRENDING"

what is wrong with my code or...does [if] vs. [else if] make any difference of how it evaluates?

(btw, the statement above is part of an #include function file which i call from the Main EA to reflect a string comment based on value of the ATRCurrent double. )

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

From the code you posted, it should give you the result you're expecting.

The only thing I can think of is that you're looking at a different ATR value from your chart compared to what your code is reading. Run it through the debugger to make sure you're actually getting 0.034.

BTW, you don't need to check if it's <= in your else if statements. The check for <= was already performed in the previous if/else if statements when it failed the condition for >=

Before:

  if(ATRCurrent <= 0.115 && ATRCurrent >= 0.093)             pATRStrength =  "VIX REVERSAL";   
     else if(ATRCurrent <= 0.092 && ATRCurrent >= 0.070 )    pATRStrength =  "WEAKENING VIX-TREND";      
     else if(ATRCurrent<= 0.069 && ATRCurrent >= 0.047 )     pATRStrength =  "VIX-TRENDING";            
     else if(ATRCurrent <= 0.046 && ATRCurrent >= 0.024 )    pATRStrength =  "START VIX-TREND";     
     else if(ATRCurrent <= 0.023 && ATRCurrent >= 0.010  )  pATRStrength =  "VIX REVERSAL";

After:

  if(ATRCurrent <= 0.115 && ATRCurrent >= 0.093)  pATRStrength =  "VIX REVERSAL";   
     else if(ATRCurrent >= 0.070)                 pATRStrength =  "WEAKENING VIX-TREND";      
     else if(ATRCurrent >= 0.047)                 pATRStrength =  "VIX-TRENDING";            
     else if(ATRCurrent >= 0.024)                 pATRStrength =  "START VIX-TREND";     
     else if(ATRCurrent >= 0.010)                 pATRStrength =  "VIX REVERSAL";
 
  1. Alexander showed how to simplify.

  2. Always post all relevant code (using Code button) or attach the file. Your images show your ATR of bar zero. We have no idea what ATRCurrent is.

  3. How do you handle pATRStrength when none of those levels exist?
 
Alexander Martinez #:

From the code you posted, it should give you the result you're expecting.

The only thing I can think of is that you're looking at a different ATR value from your chart compared to what your code is reading. Run it through the debugger to make sure you're actually getting 0.034.

BTW, you don't need to check if it's <= in your else if statements. The check for <= was already performed in the previous if/else if statements when it failed the condition for >=

Before:

After:

thanks Alex for the clear example of how to simplify. that's a cool way to do and i will never have thought about that in a million years.

earlier i tried simplifying it by writing it like so below but i'm still fuzzy why it doenst seem to work. 

0.092 >= ATRCurrent >= 0.070
btw, i'll try to study and learn how to use debugger. still a newbie here!
 
JimSingadventure #: earlier i tried simplifying it by writing it like so below
0.092 >= ATRCurrent >= 0.070

True = non-zero and false = zero so you get:

if( 3 < 2 < 1 )
if( false < 1 )
if(     0 < 1 )
if(     true  )
if( 3 > 2 > 1 )
iftrue > 1 )
if(     1 > 1 )
if(     false )

 
William Roeder #:

True = non-zero and false = zero so you get:

wow, thanks wiliam, i never thought that's how to read it! 

that's insightful for a newbie like me. appreciate. But sometimes your comments are too high "up there" that i have a hard time understanding it at my level.

which means the correct way to write it must be : 
if( 3 > (2 > 1 ))
if( 3> true)
if( 3 > 1)
if(true)

right?

 
  1. I won't generally give it; just point you in the correct direction so you will learn.
  2. Correct, but 3 > (5 > 4) is also true. Don't compare numbers to booleans.
Reason: