Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Overnight gap Indicator

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Overnight gap Indicator

    Hi There,
    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]);

    }


    #2
    Hello alfonso,

    If you are trying to get the prior day close, I would suggest trying the PriorDayOHLC indicator to gather this value. Close[1] may or may not represent the yesterdays close depending on the series being used and when this is called.



    This would not require adding a secondary series or doing other series related tasks, mainly just calling the indicator to retrieve the value.

    I look forward to being of further assistance.

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by CarlTrading, 03-31-2026, 09:41 PM
    1 response
    81 views
    1 like
    Last Post NinjaTrader_ChelseaB  
    Started by CarlTrading, 04-01-2026, 02:41 AM
    0 responses
    42 views
    0 likes
    Last Post CarlTrading  
    Started by CaptainJack, 03-31-2026, 11:44 PM
    0 responses
    64 views
    2 likes
    Last Post CaptainJack  
    Started by CarlTrading, 03-30-2026, 11:51 AM
    0 responses
    68 views
    0 likes
    Last Post CarlTrading  
    Started by CarlTrading, 03-30-2026, 11:48 AM
    0 responses
    55 views
    0 likes
    Last Post CarlTrading  
    Working...
    X