I'm struggling a bit with an indicator I'm trying to build to get the average daily High of the last 5 days, and plot that value on my tick chart from the start of the current trading session to now.
I use a tick chart but load daily data through AddDataSeries to perform the calculation.
Is the below code correct considering I want the average of the last 5 daily Highs (Excluding today)?
When I print the values, it seems that it's older days.
It also seems that my numberOfBars is not working properly as the longer the script runs, the more in the past the lines is drawn (it goes prior to the current session).
else if (State == State.Configure) { AddDataSeries(Data.BarsPeriodType.Day, 1); } protected override void OnBarUpdate() { //Add your custom indicator logic here. if (CurrentBars[1] < 5) return; if (Bars.IsFirstBarOfSession) { // print the number of bars counted from the previous session // Print("Previous session number of bars: " + numberOfBars); // reset the counter as this is a new session numberOfBars = 0; } // increment numberOfBars on every bar close numberOfBars++; double sumHighs= 0; for (int i = 1; i <= 5; i++) { double highs= Bars.GetHigh(CurrentBars[1] - i); sumHighs+= highs; } double averageHighs = sumHighs/ 5; Draw.Line(this, "highs" + CurrentBar, false, numberOfBars, averageHighs, 0, averageHighs, new SolidColorBrush(Color.FromRgb(209, 212, 220)), DashStyleHelper.Dash, 2); }
Many thanks.
Comment