Download MetaTrader 5
To add comments, please log in or register
Do you have Mac OS? Find out how to install MetaTrader on it!
John
79
John 2014.10.22 08:09 

Hi,

I'm creating an EA that uses the:

-Stochastics (Main line);

-SMA21 on Stochastics ("First Indicators Data"),

to generate signals. The problem is that the  "SMA21 on Stochastics" always return 0 , why ?

 

Here's an extract of the code:

int start()

{

   if(!IsBarClosed(0, true)) return(0); 

 

   Main_Line[1] = iStochastic(Symbol(), Period(), 21, 3, 3, MODE_SMA, 0, MODE_MAIN, 1);

   Main_Line[2] = iStochastic(Symbol(), Period(), 21, 3, 3, MODE_SMA, 0, MODE_MAIN, 2);

   Main_Line[3] = iStochastic(Symbol(), Period(), 21, 3, 3, MODE_SMA, 0, MODE_MAIN, 3);

   Main_Line[4] = iStochastic(Symbol(), Period(), 21, 3, 3, MODE_SMA, 0, MODE_MAIN, 4);

   

   for(int i=0;i<5;i++)

   {

      MAofStochBuffer[i] = iMAOnArray(Main_Line, 0, 21, 0, MODE_SMA, i);

      PrintFormat("SMA21 on first indicator: %s", DoubleToStr(MAofStochBuffer[i], 2));

      

   }

 

    return(0);  

}


--Values printed out:

SMA21 on first indicator: 0.00

SMA21 on first indicator: 0.00

SMA21 on first indicator: 0.00   

SMA21 on first indicator: 0.00    


Why ? Thanks in advance.

Jo. 

Ernst Van Der Merwe
4397
Ernst Van Der Merwe 2014.10.22 10:44  
jfortes:

Hi,

I'm creating an EA that uses the:

-Stochastics (Main line);

-SMA21 on Stochastics ("First Indicators Data"),

to generate signals. The problem is that the  "SMA21 on Stochastics" always return 0 , why ?

 

Here's an extract of the code:

int start()

{

   if(!IsBarClosed(0, true)) return(0); 

 

   Main_Line[1] = iStochastic(Symbol(), Period(), 21, 3, 3, MODE_SMA, 0, MODE_MAIN, 1);

   Main_Line[2] = iStochastic(Symbol(), Period(), 21, 3, 3, MODE_SMA, 0, MODE_MAIN, 2);

   Main_Line[3] = iStochastic(Symbol(), Period(), 21, 3, 3, MODE_SMA, 0, MODE_MAIN, 3);

   Main_Line[4] = iStochastic(Symbol(), Period(), 21, 3, 3, MODE_SMA, 0, MODE_MAIN, 4);

   

   for(int i=0;i<5;i++)

   {

      MAofStochBuffer[i] = iMAOnArray(Main_Line, 0, 21, 0, MODE_SMA, i);

      PrintFormat("SMA21 on first indicator: %s", DoubleToStr(MAofStochBuffer[i], 2));

      

   }

 

    return(0);  

}


--Values printed out:

SMA21 on first indicator: 0.00

SMA21 on first indicator: 0.00

SMA21 on first indicator: 0.00   

SMA21 on first indicator: 0.00    


Why ? Thanks in advance.

Jo. 

You should have enough elements in the first array to do calculations on.

double Main_Line[30];
double MAofStochBuffer[4];
//---
 
   for(int i=0;i<30;i++)

   {

   Main_Line[i] = iStochastic(Symbol(), Period(), 21, 3, 3, MODE_SMA, 0, MODE_MAIN, i+1);


   
   }
   
   for(int i=0;i<4;i++)

   {

      MAofStochBuffer[i] = iMAOnArray(Main_Line, 0, 21, 0, MODE_SMA, i);

      PrintFormat("SMA21 on first indicator: %s", DoubleToStr(MAofStochBuffer[i], 2));

      

   }
        


//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+

This should give the same result.

   for(int i=0;i<4;i++)

   {

      MAofStochBuffer[i] = iStochastic(Symbol(), Period(), 21, 21, 3, MODE_SMA, 0, MODE_SIGNAL, i+1);

      PrintFormat("SMA21 on first indicator: %s", DoubleToStr(MAofStochBuffer[i], 2));

      

   }
John
79
John 2014.10.22 13:25  

Thanks. Indeed it was the number of elements in Array Main_Line[] that did not allow the calculation :)

 

Now the only issue left is: I need to compare Main_Line[2] with the value of MAofStochBuffer at shift 2:

if(Main_Line[2] < MAofStochBuffer[2] )  , but for some reason the MAofStochBuffer at shift 2 is different from the value read from the Indicator window.

 

Is it because the iMAOnArray populates the output Array in reverse mode ? (as per documentation).

Thanks again for the help. 

John
79
John 2014.10.23 05:43  

Problem fixed thx.

Created a custom indicator that calculates the Stochastics and iMAOnArray on Stochastics, and read the values from the EA.

Ernst Van Der Merwe
4397
Ernst Van Der Merwe 2014.10.23 06:02  
jfortes:

Thanks. Indeed it was the number of elements in Array Main_Line[] that did not allow the calculation :)

 

Now the only issue left is: I need to compare Main_Line[2] with the value of MAofStochBuffer at shift 2:

if(Main_Line[2] < MAofStochBuffer[2] )  , but for some reason the MAofStochBuffer at shift 2 is different from the value read from the Indicator window.

 

Is it because the iMAOnArray populates the output Array in reverse mode ? (as per documentation).

Thanks again for the help. 

You're right, so it's important to have the exact amount of elements in the first array.

   double Main_Line[21+4];
   double MAofStochBuffer[4];
//---
 
   for(int i=0;i<21+4;i++)

   {

   Main_Line[i] = iStochastic(Symbol(), Period(), 21, 21, 3, MODE_SMA, 0, MODE_MAIN, i);

   
   }
   
   
   for(int i=0;i<4;i++)

   {

      MAofStochBuffer[i] = iMAOnArray(Main_Line, 0, 21, 0, MODE_SMA, i);


   }

   PrintFormat("SMA21 on third index [2] : %s", DoubleToStr(MAofStochBuffer[2], 2));

//--- return value of prev_calculated for next call
   return(rates_total);
  }

.

/
To add comments, please log in or register