Need some help as I am a bit puzzled why this is not working. I am trying to create an indicator that tells me how big was the gap (Open Today minus Close yesterday). It seems to let me calculate Open today - close today but not open today vs close yesterday. I tried following the sample script for SampleCustomDataSeries. I am sure its something dumb but have not been able to fix it. My current code is below.
Appreciate your help!
/// <summary>
/// Reference sample demonstrating how to use DataSeries objects to store self calculations.
/// </summary>
[Description("Reference sample demonstrating how to use DataSeries objects to store self calculations.")]
public class TestONRange : Indicator
{
#region Variables
private int period = 5;
private int period2 = 10;
// Defines the DataSeries object
private DataSeries myDataSeries; // Define a DataSeries variable
#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()
{
// Adds a plot to our NinjaScript Indicator
Add(new Plot(Color.Red, PlotStyle.Line, "Average Range"));
// Create a new DataSeries object and assign it to the variable myDataSeries declared in the ‘Variables’ region above
myDataSeries = new DataSeries(this); // "this" refers to the indicator, or strategy
// itself. This syncs the DataSeries object
// to historical data bars
CalculateOnBarClose = true;
Overlay = false;
}
/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
// Calculate the range of the overnight gap and set the value as an average of the last x days (note on the error: if I write the below as myDataSeries.Set(Close[0] - Open[0]); i.e calculate the //intraday range, then the indicator shows up in my charts fine.
//however if I set it to take Close 1 bar ago i.e yesterdays close minus todays open it does not work...…
myDataSeries.Set(Close[1] - Open[0]);
AvgR.Set(SMA(myDataSeries, Period)[0]);
}
Comment