Download MetaTrader 5
To add comments, please log in or register
The Market has more than 400 indicators. Try any of them!
Dan
49
Dan 2014.07.10 23:46 

Hi forum,

 

Hoping someone will be kind enough to help me work out why this variable won't hold it's value.  I've tried as a normal boolean value, and as static (as read from another thread on here.) 

 

Also read about latching the value.  This might be what I'm after, but I need some help.

 

If you couldn't tell, I'm pretty new to mql4, and I'm trying to write an EA using comparisons on a Stochastic indicator.

 

Please see the code below:

 


static bool  Stochastic_Open_Long;


      if ( ( iStochastic ( Symbol(), 0, 8, 3, 3, MODE_SMA, 0, MODE_MAIN, 0 ) >= iStochastic ( Symbol(), 0, 8, 3, 3, MODE_SMA, 0, MODE_SIGNAL, 0 ) )
      && ( ( iStochastic ( Symbol(), 0, 8, 3, 3, MODE_SMA, 0, MODE_MAIN, 0 ) <= 30 ) || (iStochastic ( Symbol(), 0, 8, 3, 3, MODE_SMA, 0, MODE_SIGNAL, 0 ) <= 30 )
      ||   ( iStochastic ( Symbol(), 0, 8, 3, 3, MODE_SMA, 0, MODE_MAIN, 1 ) <= 30 ) || (iStochastic ( Symbol(), 0, 8, 3, 3, MODE_SMA, 0, MODE_SIGNAL, 1 ) <= 30 )
      ||   ( iStochastic ( Symbol(), 0, 8, 3, 3, MODE_SMA, 0, MODE_MAIN, 2 ) <= 30 ) || (iStochastic ( Symbol(), 0, 8, 3, 3, MODE_SMA, 0, MODE_SIGNAL, 2 ) <= 30 )
      ||   ( iStochastic ( Symbol(), 0, 8, 3, 3, MODE_SMA, 0, MODE_MAIN, 3 ) <= 30 ) || (iStochastic ( Symbol(), 0, 8, 3, 3, MODE_SMA, 0, MODE_SIGNAL, 3 ) <= 30 )
      ||   ( iStochastic ( Symbol(), 0, 8, 3, 3, MODE_SMA, 0, MODE_MAIN, 4 ) <= 30 ) || (iStochastic ( Symbol(), 0, 8, 3, 3, MODE_SMA, 0, MODE_SIGNAL, 4 ) <= 30 )
      ||   ( iStochastic ( Symbol(), 0, 8, 3, 3, MODE_SMA, 0, MODE_MAIN, 5 ) <= 30 ) || (iStochastic ( Symbol(), 0, 8, 3, 3, MODE_SMA, 0, MODE_SIGNAL, 5 ) <= 30 ) ) )

            {
            Stochastic_Open_Long = true;
            Print( "Stochastic_Open_Long = True");
            }
      else
            {
            Stochastic_Open_Long = false;
            Print( "Stochastic_Open_Long = False");
            }
                   
      if (Stochastic_Open_Long = false)
         {
         Print( "Stochastic now False");
         }
      
      if (Stochastic_Open_Long = true)
         {
         Print( "Stochastic now True");
         }

 

I am seeing the boolean value reset for some reason.

 

In the terminal I see  "Stochastic_Open_Long = False" immediately followed by "Stochastic now True".

 

I  expect to see "Stochastic_Open_Long = False" immediately followed by "Stochastic now False".

 

I have confirmed with other print functions that the multiple &&'s and ||'s are doing their intended job, and the "if" condition is being met as expected.  Have confirmed using data for both true and false outcomes.

 

I want to reset the value each tick, but believe this will happen by default if I declare the variable inside the start () function code?  (Even if I declare it as static?)

 

In any case, I still don't understand why the value changes when execution leaves the conditional section of code, can anyone help shed some light...? 

 

Thanks in advance.

qjol
3243
qjol 2014.07.10 23:54  
      if (Stochastic_Open_Long == false)
         {
         Print( "Stochastic now False");
         }
      
      if (Stochastic_Open_Long == true)
         {
         Print( "Stochastic now True");
         }

but you can do like this:

      if (!Stochastic_Open_Long)
         {
         Print( "Stochastic now False");
         }
      
      if (Stochastic_Open_Long)
         {
         Print( "Stochastic now True");
         }


it's the same (even better, no mistakes like you did)

Dan
49
Dan 2014.07.11 00:01  

qjol, you save me once again...!

 

The old "double equal sign" has caught me out previously...  Damn you double equal sign...!!!

 

Thanks very much for the help.

 

:-) 

Hongliang Liu
1196
Hongliang Liu 2014.07.11 01:42  

the following code style could avoid this kind of mistake:

if (false == Stochastic_Open_Long)
or
if (!Stochastic_Open_Long)
whroeder1
13649
whroeder1 2014.07.11 12:58  
  1. if (Stochastic_Open_Long)
             {
             Print( "Stochastic now True");
             }
    How can stochastic be true. Stochastic is a VALUE, not a logical condition.
  2. This is why I say: You would never write if( (2+2 == 4) == true) would you? if(2+2 == 4) is sufficient. So Don't write if(bool == true), just use if(bool) or if(!bool). Code becomes self documenting when you use meaningful variable names, like bool isDirLong. Stochastic_Open_Long sounds like the stochastic opened an order long or not. if(isDirLong) sounds like testing which direction you want to open (based on some criteria.) If you can't read your code out loud in English and have it make sense, change it until it does.
  3. Are your books one column but two feet wide? No because that is unreadable. They are 6 inches, sometimes two columns, so you can read it easily. So should be your code. You initial if should be made readable, and simplified
    double stoMain[6], stoSig[6];
    for(int i=0; i < 6; i++){
       stoMain[i] = iStochastic(Symbol(), 0, 8, 3, 3, MODE_SMA, 0, MODE_MAIN, i);
       stoSig[i]  = iStochastic(Symbol(), 0, 8, 3, 3, MODE_SMA, 0, MODE_SIGNAL, i);
    }
          if ( stoMain[0] >= stoSig[0]
          && ( stoMain[0] <= 30 || stoSig[0] <= 30
          ||   stoMain[1] <= 30 || stoSig[1] <= 30
          ||   stoMain[2] <= 30 || stoSig[2] <= 30
          ||   stoMain[3] <= 30 || stoSig[3] <= 30
          ||   stoMain[4] <= 30 || stoSig[4] <= 30
          ||   stoMain[5] <= 30 || stoSig[5] <= 30) )
  4. static bool  Stochastic_Open_Long;            
    :
    if(condition)
                {
                Stochastic_Open_Long = true;
                Print( "Stochastic_Open_Long = True");
                }
          else
                {
                Stochastic_Open_Long = false;
    
    Doesn't need to be static since you are setting it every tick. If it's supposed to "hold it's value" don't change it without a signal.
    if(      isLongSignal) Stochastic_Open_Long = true;
    else if(isShortSignal) Stochastic_Open_Long = false;
    // else keep it the same.

/
To add comments, please log in or register