Recreating Stochastic

 

Hi,


I am trying to recreate the Stochastic for fun.


This should be the formula: %K = (Current Close - Lowest Low)/(Highest High - Lowest Low) * 100

%D = 3-day SMA of %K


However the results of my code does not match the indicator provided by MQL4. can you help me understand where the error is?

This is the bulk of my indicator.

thanks


int start() {

ArraySetAsSeries(ExtMainBuffer,false);
ArraySetAsSeries(ExtSignalBuffer,false);


    stocaMax = 0; // Refreshes the indicator value every bar
    stocaMin = 0; // Refreshes the indicator value every bar
   
   for (int i = 0; i < Bars-1 ; i++) {
   
   //stocaMax = ((Close[0] -Low[iLowest(NULL,0,MODE_LOW,10,shift+1)])/(High[iHighest(NULL,0,MODE_HIGH,10,shift+1)]-Low[iLowest(NULL,0,MODE_LOW,10,shift+1)]))*100;
   //stocaMin = iMA(NULL,0,3,shift,MODE_SMA,,0);
    stocaMax = High[iHighest(NULL,0,MODE_HIGH,5,i+1)];
     stocaMin = Low[iLowest(NULL,0,MODE_LOW,5,i+1)];
   
   
   
   ExtHighesBuffer[i]= stocaMax;
   
  ExtLowesBuffer[i]=stocaMin;
  
  
  ExtMainBuffer[i] =((Close[i]-ExtLowesBuffer[i])/(stocaMax-stocaMin))*100;
  
  
    
    
}  

   for(int l=3; l<Bars-1 && !IsStopped(); l++)
     {
      double sum=0.0;
         for( int k=0; k<InpDPeriod; k++)
            sum+=ExtMainBuffer[l-k];
         ExtSignalBuffer[l]=sum/InpDPeriod;
     }
  
 
   

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+

return(0);
 }
 

Your buffers are not as-series.

iHighest/iLowest is. High/Low/Close is.
// ArraySetAsSeries(ExtMainBuffer,false);
// ArraySetAsSeries(ExtSignalBuffer,false);
 
whroeder1:

Your buffers are not as-series.

iHighest/iLowest is. High/Low/Close is.

thanks whroeder1.

I think i did set them as series above.

Are you saying that I should not?

 
int start()
{
//---
   int i,k,y,limit,counted_bars=IndicatorCounted();
   double stocaMax,stocaMin,sum;

   if(counted_bars<0)
      return(-1);
   if(counted_bars==0)
      limit=K_Period;
   else
      limit=counted_bars-1;

   ArraySetAsSeries(High,false);
   ArraySetAsSeries(Low,false);
   ArraySetAsSeries(Close,false);

   ArraySetAsSeries(ExtMainBuffer,false);
   ArraySetAsSeries(ExtSignalBuffer,false);

   for(i=limit; i<Bars; i++)
     {
      y=i-K_Period+1;

      stocaMax=High[ArrayMaximum(High,K_Period,y)];
      stocaMin=Low[ArrayMinimum(Low,K_Period,y)];

      ExtMainBuffer[i]=(Close[i]-stocaMin)/(stocaMax-stocaMin)*100;
     }

   for(i=limit; i<Bars; i++)
     {
      sum=0.0;

      for(k=0; k<D_Period; k++)
         sum+=ExtMainBuffer[i-k];

      ExtSignalBuffer[i]=sum/D_Period;
     }
//--- 
   return(0);
}

iHighest and iLowest are not available in this case.

You should use new MQL4 format.

 
Naguisa Unada:

iHighest and iLowest are not available in this case.

You should use new MQL4 format.

Hi @Naguisa Unada,

thanks for looking into this.

have iHighest and iLowest been deprecated?

if so, why does m4t not say so?


thanks

 
billysballo:

Hi @Naguisa Unada,

thanks for looking into this.

have iHighest and iLowest been deprecated?

if so, why does m4t not say so?


thanks

No, they are invalid in this case, i.e., they can not be used when "false" is specified in "ArraySetAsSeries". Usually, "true" is specified in default.

 
Naguisa Unada:

No, they are invalid in this case, i.e., they can not be used when "false" is specified in "ArraySetAsSeries". Usually, "true" is specified in default.

thanks @Naguisa Unada.


I sent you a message :)

 

> y=i-InpKPeriod+1; why are you adding 1 at every loop?

It's the same as "InpKPeriod" for search from y to i. Try to count it.

> High[ArrayMaximum(High,InpKPeriod,y)]; vs High[iHighest(NULL,0,MODE_HIGH,14,i)]; what s the difference?

I know the difference, but it's too difficult to explain in English, study it by yourself.

> sum+=ExtMainBuffer[i-k]; what is this doing as well?

sum = ExtMainBuffer[i-0] + ExtMainBuffer[i-1] + ExtMainBuffer[i-2] + ExtMainBuffer[i-3] ..........


> You also commented:" No, they are invalid in this case, i.e., they can not be used when "false" is specified in "ArraySetAsSeries". Usually, "true" is specified in default. " where can i find this info?

I don't know, but you can understand it by trying to work programs.
 
billysballo:

thanks whroeder1.

I think i did set them as series above.

Are you saying that I should not?

 They are not as serie, and should be as serie

// ArraySetAsSeries(ExtMainBuffer,false);
// ArraySetAsSeries(ExtSignalBuffer,false);

This is not correct, at each tick, the indicator will be redrawed for all Bars

  for(i=limit; i<Bars; i++)

for the rest it should work

 
ffoorr:

This is not correct, at each tick, the indicator will be redrawed for all Bars

for the rest it should work

This is correct, because in my program limit = counted_bars - 1.

 
Naguisa Unada:

> y=i-InpKPeriod+1; why are you adding 1 at every loop?

It's the same as "InpKPeriod" for search from y to i. Try to count it.

> High[ArrayMaximum(High,InpKPeriod,y)]; vs High[iHighest(NULL,0,MODE_HIGH,14,i)]; what s the difference?

I know the difference, but it's too difficult to explain in English, study it by yourself.

> sum+=ExtMainBuffer[i-k]; what is this doing as well?

sum = ExtMainBuffer[i-0] + ExtMainBuffer[i-1] + ExtMainBuffer[i-2] + ExtMainBuffer[i-3] ..........


> You also commented:" No, they are invalid in this case, i.e., they can not be used when "false" is specified in "ArraySetAsSeries". Usually, "true" is specified in default. " where can i find this info?

I don't know, but you can understand it by trying to work programs.

thanks Naguisa :)

Reason: