How to access the previous value of an indicator?
Posted: Apr 21 2015
Code: Select all
public class New_ATR : IndicatorObject {
[Input]
public int period { get; set; }
private double trueRange;
private double averageTrueRange;
private IPlotObject plot1;
public New_ATR(object _ctx):
base(_ctx){
// default 14 days
period = 14;
}
protected override void Create() {
// create variable objects, function objects, plot objects etc.
plot1 = AddPlot(new PlotAttributes("ATR", EPlotShapes.Line, Color.Cyan, Color.Empty, 0, 0, true));
}
protected override void StartCalc() {
// assign inputs
}
protected override void CalcBar(){
// calculate the value of first bar
if (Bars.CurrentBar == 0) {
trueRange = Bars.High[0] - Bars.Low[0];
plot1.Set(0, trueRange);
}
else {
// logic from NT7
//double trueRange = High[0] - Low[0];
//trueRange = Math.Max(Math.Abs(Low[0] - Close[1]), Math.Max(trueRange, Math.Abs(High[0] - Close[1])));
//Value.Set(((Math.Min(CurrentBar + 1, Period) - 1 ) * Value[1] + trueRange) / Math.Min(CurrentBar + 1, Period));
trueRange = Bars.High[0] - Bars.Low[0];
trueRange = Math.Max(Bars.Low[0] - Bars.Close[1], Math.Max(trueRange, Math.Abs(Bars.High[0] - Bars.Close[1])));
averageTrueRange = ((Math.Min(Bars.CurrentBar + 1, period) - 1 ) * ?????? + trueRange) / Math.Min(Bars.CurrentBar + 1, period);
plot1.Set(0, averageTrueRange);
}
}
}