AFL CODE
SetBarsRequired(-2, -2);
MaPeriod = Param("Period", 10, 5, 50, 5);
// calling the plug-in method
MyMa = LoopSampleCS(MaPeriod); // see BasicSampleCS5() method in "Basic Samples.cs" for source
// plot the calculated average
Plot(MyMa, "MyMa", colorBlack, styleLine);
Title = _SECTION_NAME() +", Number of bars:" + NumToStr(BarCount, 1.0);
.NET CODE
[ABMethod(Name = "LoopSampleCS")]
public ATArray BasicSampleCS5(float period)
{
int maPeriod = (int)period;
// allocate memory for result array
ATArray myMa = new ATArray();
// set null values at the beginning of the array
for (int i = 0; i < maPeriod - 1 && i < myMa.Length; i++)
myMa[i] = ATFloat.Null;
// calculate the average for each bar
for (int i = maPeriod - 1; i < myMa.Length; i++)
{
// clear temporary result
float tempSum = 0.0f;
// inner loop to sum bar data
for (int j = 0; j < maPeriod; j++)
tempSum = tempSum + Close[i - j];
// save calculated value to result array element
myMa[i] = tempSum / maPeriod;
}
// returning result to AFL script
return myMa;
}