Page 1 of 1

How to access the previous value of an indicator?

Posted: Apr 21 2015
by Sylpha

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);
}
}
}
Say i am on the 2nd bar of the daily series, I want to access the variable of trueRange of first bar so that I can calculate the average of two days. How can I do this? Thanks.

Re: How to access the previous value of an indicator?

Posted: Apr 21 2015
by Sylpha
One more question, how can I store the indicator value of current bar into a array so that I have a series of values that I can read anytime?

Re: How to access the previous value of an indicator?  [SOLVED]

Posted: Apr 22 2015
by Henry MultiŠ”harts
Hello Sylpha,

You can use VariableSeries to achieve your goal.