How to add SMA on Stoch

 

Hi guys,

I've got two functions defined, one to calculate a "manual" stochastic and another to calculate a Simple Moving Average:

double CreateStoch(int Length, int Shift) {

      int HighestBar = iHighest(_Symbol,_Period,MODE_HIGH,Length,Shift);
      int LowestBar = iLowest(_Symbol,_Period,MODE_LOW,Length,Shift);
      
      double HighestValue = iHigh(_Symbol,_Period,HighestBar);
      double LowestValue = iLow(_Symbol,_Period,LowestBar);
      double ClosePrice = iClose(_Symbol,_Period,1);
         
      double Stoch = 100 * ((ClosePrice - LowestValue) / (HighestValue - LowestValue));
      
      return Stoch;           
   }
 double CalculateSMA(double Stoch, int Shift, int Length) {
            
      double movingAverageDefinition = iMA(_Symbol,_Period,Length,Shift,MODE_SMA,Stoch); // MODE_SMMA is smoothed averaging, MODE_SMA stands for Simple Averaging
      double myMovingAverageArray[];
     
      CopyBuffer(movingAverageDefinition,0,0,1,myMovingAverageArray);
      ArraySetAsSeries(myMovingAverageArray, true);
                   
      double myMovingAverageValue = myMovingAverageArray[0];
      
      return myMovingAverageValue;
   }

Whenever I combine the two, for instance:  double SMA5 = CalculateSMA(Stoch5,0,5) the result I get is more like a SMA than a Stochastic. 
I get results like 1,1904377 something you would expect to see from an SMA rather than a Stochastic with an SMA. 

What do I do wrong here? 

 
Are you asking about the old terminal or MQL5?
 

Hi Vladimir,

I don't know what you mean by old terminal, MQL4? I'm using MQL5.

 
sjoerdm :

*** I'm using MQL5.

In this case, I see a gross error: you create an indicator handle at each tick. Remember: in MQL5, an indicator handle is created ONCE and it is done in OnInit () !!!

 
Forgive me for asking such a question, but how would I pass parameters to the iMA if the handle is declared on the OnInit? I'm setting up different SMA's with different lengths, and all dependent on different stochastics.
 
sjoerdm :
Forgive me for asking such a question, but how would I pass parameters to the iMA if the handle is declared on the OnInit? I'm setting up different SMA's with different lengths, and all dependent on different stochastics .

Create an array of handles.