Calculate Stochastic Formula on a array (non indicator buffer array) - is iStochasticOnArray available somewhere?

 

Hi,

I need to calculate the stochastic formula on a array (looking for fast %k only), this array is not a indicator buffered array. This is my code:

 double 
      SmallestBuffer[],
      LargestBuffer[],
      COTIndex[];
      
 int 
      COTIndexPeriod=13, //similar to Stochastic %K period
      position=0;
   
   ArrayResize(SmallestBuffer, ArraySize(Commercial));
   ArrayResize(LargestBuffer, ArraySize(Commercial));  
   ArrayResize(COTIndex, ArraySize(Commercial));
   
   for (i= ArraySize(Commercial)-COTIndexPeriod; i >= 0; i--)
   {   
      position = ArrayMinimum(Commercial, COTIndexPeriod, i+COTIndexPeriod-1);
      SmallestBuffer[i] = Commercial[position];
      //Print("Smallest: ", SmallestBuffer[i], " Index: ", i);
   }
   
   for (i= ArraySize(Commercial)-COTIndexPeriod; i >= 0; i--)
   {   
      position = ArrayMaximum(Commercial, COTIndexPeriod, i+COTIndexPeriod-1);
      LargestBuffer[i] = Commercial[position];
      //Print("Largest: ", LargestBuffer[i], " Index: ", i);
   }
   
   for (i= ArraySize(Commercial)-COTIndexPeriod; i >= 0; i--)
   //for (i= ArraySize(Commercial)-1; i > 0; i--)
   {   
      //position = ArrayMaximum(Commercial, COTIndexPeriod, i+COTIndexPeriod-1);
      double top = Commercial[i]-SmallestBuffer[i];
      double bottom = LargestBuffer[i]-SmallestBuffer[i];
      
      if(bottom == 0.0)    //prevent divide by zero errors
      {  bottom=100.0;  }
      
      COTIndex[i] = (top/bottom)*100;    //calculate fast %k only
      Print("COTIndex: ", COTIndex[i], " Index: ", i);
      ExtMapBuffer1[i] = COTIndex[i]; //graph it
   }

So the goal is to have the Stochastic value stored in the COTIndex[i] array. However, the calculated values are in incorrect. Does anyone have a Stochastic-On-Array (iStochOnArray) implementation? or maybe does anyone see where the logical problem is on my code? I'm stuck and would greatly appreciate any help!

Thank you


Mike

 
Not that I know the logic, but I would venture a guess that the third argument to ArrayMinimum, and likewise ArrayMaximum, are wrong.

For example, in the first iteration, the value of "i" is "ArraySize(Commercial)-COTIndexPeriod", making the third argument equal to "ArraySize(Commercial)-1", and in that case, the logic attempts to look past the top of the array.?

Maybe the "+COTIndexPeriod" terms should be removed?

I would further guess that the "-1" terms should be removed as well, and the loops should start at ".... - 1".

In other words, I'm guessing that SmallestBuffer[i] is to hold the smallest value of the period in Commercial that starts at index i, and is of COTIndexPeriod length. Similarly for LargestBuffer. Then COTIndex[i] would have a value relating to the period [ i, i+COTIndexPeriod-1 ] in Commercial.
 
richplank:
Not that I know the logic, but I would venture a guess that the third argument to ArrayMinimum, and likewise ArrayMaximum, are wrong.

For example, in the first iteration, the value of "i" is "ArraySize(Commercial)-COTIndexPeriod", making the third argument equal to "ArraySize(Commercial)-1", and in that case, the logic attempts to look past the top of the array.?

Maybe the "+COTIndexPeriod" terms should be removed?

I would further guess that the "-1" terms should be removed as well, and the loops should start at ".... - 1".

In other words, I'm guessing that SmallestBuffer[i] is to hold the smallest value of the period in Commercial that starts at index i, and is of COTIndexPeriod length. Similarly for LargestBuffer. Then COTIndex[i] would have a value relating to the period [ i, i+COTIndexPeriod-1 ] in Commercial.


Hi Rich,

Thanks for your input. I couldnt see the forest for the trees, you are right and I removed everything except for the parameters to ArrayMaximum and ArrayMinimum. Below is my final optimized code to run Stochastics on a array:

int start()
{
//<snip>...begin of start()
 
      //calculate Stochastic Fast %K 
      for (i =ArraySize(MyArray)-StochPeriod-1; i >= 0; i--)
      {
         ExtMapBuffer1[i] = CalculateStochOnArray(MyArray,i);
      }
 
//<snip>...end of start()...
}
 
//calculates Stochastics fast %k. takes the array holding our values and current index we are iterating through
double CalculateStochOnArray(double TheArray[], int i)
{
  double 
      SmallestBuffer[],       //buffer to hold the smallest value
      LargestBuffer[],        //buffer to hold the largest value
      StochFastK,             //Stochastic Fast %K
      top,                    //holds top half of calculation
      bottom;                 //holds bottom half of calculation
      
   int 
      position=0;             //place in SmallestBuffer/Largestbuffer for our largest or smallest value
   
   ArrayResize(SmallestBuffer, ArraySize(TheArray));              //size our array accordingly
   ArrayResize(LargestBuffer, ArraySize(TheArray));  
   
   position = ArrayMinimum(TheArray, StochPeriod, i);          //find the smallest value that starts at index i and is of StochasticPeriod length
   SmallestBuffer[i] = TheArray[position];
 
   position = ArrayMaximum(TheArray, StochPeriod, i);          //find the largest value that starts at index i and is of StochasticPeriod length
   LargestBuffer[i] = TheArray[position];
 
   top = TheArray[i]-SmallestBuffer[i];                            //holds top half of StochFastK calculation
   bottom = MathMax(0.0001, LargestBuffer[i] - SmallestBuffer[i]); //holds bottom half of StochFastK calculation. Use MathMax to prevent divide by zero errors
      
   StochFastK = (top/bottom)*100;
   
   return(StochFastK);
}
Reason: