The relevant section of code for StdDev() is here:
protected override void OnBarUpdate()
{
if (CurrentBar < 1)
{
Value.Set(0);
sumSeries.Set(Input[0]);
}
else
{
sumSeries.Set(Input[0] + sumSeries[1] - (CurrentBar >= Period ? Input[Period] : 0));
double avg = sumSeries[0] / Math.Min(CurrentBar + 1, Period);
double sum = 0;
for (int barsBack = Math.Min(CurrentBar, Period - 1); barsBack >= 0; barsBack--)
sum += (Input[barsBack] - avg) * (Input[barsBack] - avg);
Value.Set(Math.Sqrt(sum / Math.Min(CurrentBar + 1, Period)));
}
}
The bolded section is the section I want to change. Rather than using the calculated simple average, I want to pass the average from a series (like TEMA) and use that value.
Let's say the second input is called Input2. So the bolded section would now look like this:
sum += (Input[barsBack] - input2[barsBack]) * (Input[barsBack] - input2[barsBack]);
What I can't figure out is to how to have an iDataSeries object passed as input2 as an argument in the function and then use it in this manner.
Here's an example of MLQ4 code that does this and I'm trying to do the 2nd option:
//
//
//
// In the original article the second method is how deviation is
// calculated . It is a questionableble method , but if we apply
// standard calculation the bands tend to be "spiky".That is the
// reason to have the "UseClassicalDeviations" option
//
//
//
double iDeviationPlus(double& array[], double& ma[], int period, int i)
{
double sum = 0;
if (UseClassicalDeviations)
for(int k=0; k<period; k++) sum += (array[i+k]-ma[i]) *(array[i+k]-ma[i]);
else for( k=0; k<period; k++) sum += (array[i+k]-ma[i+k])*(array[i+k]-ma[i+k]);
return(MathSqrt(sum/period));
}
Comment