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

Questions about MultiCharts .NET and user contributed studies.
Sylpha
Posts: 29
Joined: 14 Apr 2015
Has thanked: 1 time

How to access the previous value of an indicator?

Postby Sylpha » 21 Apr 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);
}
}
}
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.
Last edited by Sylpha on 21 Apr 2015, edited 1 time in total.

Sylpha
Posts: 29
Joined: 14 Apr 2015
Has thanked: 1 time

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

Postby Sylpha » 21 Apr 2015

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?

User avatar
Henry MultiСharts
Posts: 9165
Joined: 25 Aug 2011
Has thanked: 1264 times
Been thanked: 2957 times

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

Postby Henry MultiСharts » 22 Apr 2015

Hello Sylpha,

You can use VariableSeries to achieve your goal.


Return to “MultiCharts .NET”