As you can see from the debug output, the most recent 'period' data points are correct (comparing the 4 period example and the 8 period example) but
the computed SMA is clearly wrong in the 4 period with interval 1 example
Please provide a hint how a sampling of the input price series can be implemented in a way that can be used as an input for different indicators.
Thx in advance,
cbaer
Debug output: OK
--------------- SMA Period 8 , Sample intervall between data points 0 ------
ES:Minute:60:MASample: Date:5/6, Time:06.05.2009 14:00:00
ES:MASample: CurrentBar=1547, numberOfBarsNecessary=7
ES:Minute:60:MASample: Date:5/6, Time:06.05.2009 14:00:00
ES:MASample: CurrentBar=1547, Values: 912,75; 907,25; 908; 906,25; 910,5; 909,75; 897,75; 898,5;
ES:Minute:60:MASample: Date:5/6, Time:06.05.2009 14:00:00
ES:MASample: CurrentBar=1547, mAValue: 906,34375
ERROR:
--------------- SMA Period 4 , Sample intervall between data points 1 ------
ES:Minute:60:MASample: Date:5/6, Time:06.05.2009 14:00:00
ES:MASample: CurrentBar=1547, numberOfBarsNecessary=6
ES:Minute:60:MASample: Date:5/6, Time:06.05.2009 14:00:00
ES:MASample: CurrentBar=1547, Values: 912,75; 908; 910,5; 897,75;
ES:Minute:60:MASample: Date:5/6, Time:06.05.2009 14:00:00
ES:MASample: CurrentBar=1547, mAValue: 982
// This namespace holds all indicators and is required. Do not change it.
namespace NinjaTrader.Indicator
{
/// <summary>
/// General Moving Average Indicator: By selecting a integer type one can switch easily between different MA computation algorithms: 0=TEMA 1=HMA 2=ITMA 3=WMA 4=EMA 5=VWMA 6=SMA 7=TMA 8=I_MIDAS(intraday)
/// </summary>
[Description("General Moving Average Indicator: By selecting a integer type one can switch easily between different MA computation algorithms: 0=TEMA 1=HMA 2=ITMA 3=WMA 4=EMA 5=VWMA 6=SMA 7=TMA 8=I_MIDAS(intraday)")]
public class MASampledExp : Indicator
{
#region Variables
// Wizard generated variables
private int period = 4; // Default setting for Period
private int interval = 1;
private double mAValue = 0;
private DataSeries SampledPricePoints;
private int debug = 3;
// User defined variables (add any user defined variables below)
#endregion
/// <summary>
/// This method is used to configure the indicator and is called once before any bar data is loaded.
/// </summary>
protected override void Initialize()
{
Add(new Plot(Color.FromKnownColor(KnownColor.Brown), PlotStyle.Line, "MAPlot"));
Overlay = true;
PriceTypeSupported = true;
SampledPricePoints = new DataSeries(this);
}
/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
mAValue = 0;
double volPriceSum = 0;
double volSum = 0;
int count = 0;
int index = 0;
int numberOfBarsNecessary = ((period-1) * (interval+1));
if (debug > 0)
{
Print(Instrument.MasterInstrument.Name+":"+Bars.Period.Id.ToString() +":"+
Bars.Period.Value+":"+
"MASample: Date:"+Time[0].Month + "/"+
Time[0].Day+", Time:" + Time[0]);
Print(Instrument.MasterInstrument.Name+ ":"+
"MASample: CurrentBar=" + CurrentBar +
", numberOfBarsNecessary=" + numberOfBarsNecessary);
}
if (CurrentBar < Math.Max(numberOfBarsNecessary*2,(period*2*(interval+1))+1) )
{
SampledPricePoints.Set(Input[0]);
return;
}
else
{
// Period times two to make sure enough values are supplied
// to build the indicator
for (count = 0; count < period*2 ; count++ )
{
// According to docu this sets the value 'count' bars ago
SampledPricePoints.Set(count,Input[index]);
index += (interval+1);
}
}
if (debug > 1)
{
Print(Instrument.MasterInstrument.Name+":"+Bars.Period.Id.ToString() +":"+
Bars.Period.Value+":"+
"MASample: Date:"+Time[0].Month + "/"+
Time[0].Day+", Time:" + Time[0]);
string values = "";
for (count = 0; count < period; count += 1)
{
values += SampledPricePoints[count].ToString() + "; ";
}
Print(Instrument.MasterInstrument.Name+ ":"+
"MASample: CurrentBar="+ CurrentBar +
", Values: " + values );
}
// This should compute the SMA of the last 'period' values of
// the SampledPricePoints dataseries but it does not.
mAValue = SMA(SampledPricePoints,period)[0];
MAPlot.Set(mAValue);
if (debug > 0)
{
Print(Instrument.MasterInstrument.Name+":"+
Bars.Period.Id.ToString() +":"+ Bars.Period.Value+":"+
"MASample: Date:"+Time[0].Month + "/"+
Time[0].Day+", Time:" + Time[0]);
Print(Instrument.MasterInstrument.Name+ ":"+
"MASample: CurrentBar="+ CurrentBar +
", mAValue: " + mAValue );
Print("");
}
}
#region Properties
[Browsable(false)] // this line prevents the data series from being displayed in the indicator properties dialog, do not remove
[XmlIgnore()] // this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
public DataSeries MAPlot
{
get { return Values[0]; }
}
[Description("Period for MA computation")]
[Category("Parameters")]
public int Period
{
get { return period; }
set { period = Math.Max(1, value); }
}
[Description("Sample Interval between the Data Points for the MA computation")]
[Category("Parameters")]
public int Interval
{
get { return interval; }
set { interval = Math.Max(0, value); }
}
#endregion
}
}

Comment