Topic: Smoothed Moving Average
I need to now how to calculate a Smoothed Moving Average for a data tester Im making on a spreadsheet.
How does FSB calculate the Smoothed MA Method
Create and Test Forex Strategies
You are not logged in. Please login or register.
Forex Software → Forex Strategy Builder (legacy) → Smoothed Moving Average
I need to now how to calculate a Smoothed Moving Average for a data tester Im making on a spreadsheet.
How does FSB calculate the Smoothed MA Method
/// <summary>
/// Calculates a Moving Average
/// </summary>
/// <param name="iPeriod">Period</param>
/// <param name="iShift">Shift</param>
/// <param name="maMethod">Method of calculation</param>
/// <param name="afSource">The array of source data</param>
/// <returns>the Moving Average</returns>
protected static double[] MovingAverage(int iPeriod, int iShift, MAMethod maMethod, double[] adSource)
{
int iBar;
double sum;
double[] adTarget = new double[Bars];
if (iPeriod <= 1 && iShift == 0)
{ // There is no smoothing
return adSource;
}
if (iPeriod > Bars || iPeriod + iShift <= 0 || iPeriod + iShift > Bars)
{ // Error in the parameters
return null;
}
for (iBar = 0; iBar < iPeriod + iShift - 1; iBar++)
{
adTarget[iBar] = 0;
}
for (iBar = 0, sum = 0; iBar < iPeriod; iBar++)
{
sum += adSource[iBar];
}
adTarget[iPeriod + iShift - 1] = sum / iPeriod;
// Simple Moving Average
if (maMethod == MAMethod.Simple)
{
for (iBar = iPeriod; iBar < Math.Min(Bars, Bars - iShift); iBar++)
{
adTarget[iBar + iShift] = adTarget[iBar + iShift - 1] + adSource[iBar] / iPeriod - adSource[iBar - iPeriod] / iPeriod;
}
}
// Exponential Moving Average
else if (maMethod == MAMethod.Exponential)
{
double pr = 2d / (iPeriod + 1);
for (iBar = iPeriod; iBar < Math.Min(Bars, Bars - iShift); iBar++)
{
adTarget[iBar + iShift] = adSource[iBar] * pr + adTarget[iBar + iShift - 1] * (1 - pr);
}
}
// Weighted Moving Average
else if (maMethod == MAMethod.Weighted)
{
double dWeight = iPeriod * (iPeriod + 1) / 2d;
for (iBar = iPeriod; iBar < Math.Min(Bars, Bars - iShift); iBar++)
{
sum = 0;
for (int i = 0; i < iPeriod; i++)
{
sum += adSource[iBar - i] * (iPeriod - i);
}
adTarget[iBar + iShift] = sum / dWeight;
}
}
// Smoothed Moving Average
else if (maMethod == MAMethod.Smoothed)
{
for (iBar = iPeriod; iBar < Math.Min(Bars, Bars - iShift); iBar++)
{
adTarget[iBar + iShift] = (adTarget[iBar + iShift - 1] * (iPeriod - 1) + adSource[iBar]) / iPeriod;
}
}
for (iBar = Bars + iShift; iBar < Bars; iBar++)
{
adTarget[iBar] = 0;
}
return adTarget;
}
Forex Software → Forex Strategy Builder (legacy) → Smoothed Moving Average
Powered by PunBB, supported by Informer Technologies, Inc.