Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Help coding overnight strat indicator

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

    Help coding overnight strat indicator

    Hello,

    I am trying to code an indicator for strat that plots overnight high and low but when i load the indicator it shows it in a way that doesn't make sense (the high and low don't seem right at all) and also I want it to be drawn on the chart not as another indicator on the bottom, can you please help?

    Code:
    #region Using declarations
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Xml.Serialization;
    using NinjaTrader.Cbi;
    using NinjaTrader.Gui;
    using NinjaTrader.Gui.Chart;
    using NinjaTrader.Gui.SuperDom;
    using NinjaTrader.Gui.Tools;
    using NinjaTrader.Data;
    using NinjaTrader.NinjaScript;
    using NinjaTrader.Core.FloatingPoint;
    using NinjaTrader.NinjaScript.DrawingTools;
    #endregion
    
    //This namespace holds Indicators in this folder and is required. Do not change it.
    namespace NinjaTrader.NinjaScript.Indicators
    {
        public class OvernightHighLow : Indicator
        {
            private Series<double> overnightHigh;
            private Series<double> overnightLow;
            private bool sessionStarted;
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    Description                                    = @"Indicator to calculate overnight high and low.";
                    Name                                        = "OvernightHighLow";
                    Calculate                                    = Calculate.OnBarClose;
                    IsOverlay                                    = false;
                    DisplayInDataBox                            = true;
                    DrawOnPricePanel                            = true;
                    DrawHorizontalGridLines                        = true;
                    DrawVerticalGridLines                        = true;
                    PaintPriceMarkers                            = true;
                    ScaleJustification                            = NinjaTrader.Gui.Chart.ScaleJustification.Right;
                    //Disable this property if your indicator requires custom values that cumulate with each new market data event.
                    //See Help Guide for additional information.
                    IsSuspendedWhileInactive                    = true;
                     // Adds a blue line style plot
    
                }
                else if (State == State.Configure)
                {
                                    // Initialize the Series<double> objects and add plots
                    overnightHigh = new Series<double>(this);
                    overnightLow = new Series<double>(this);
    
                    AddPlot(Brushes.Red, "OvernightHigh");
                    AddPlot(Brushes.Green, "OvernightLow");
                }
            }
    
            protected override void OnBarUpdate()
            {
          if (Bars == null || CurrentBar == 0)
                    return;
    
                // Define your custom overnight session times here
                TimeSpan overnightStart = new TimeSpan(16, 30, 0); // 4:30 PM
                TimeSpan overnightEnd = new TimeSpan(9, 30, 0);   // 9:30 AM
    
                DateTime currentTime = Time[0];
                DateTime previousTime = Time[1];
    
                bool isNewSessionBar = currentTime.Date != previousTime.Date;
    
                // Check if the overnight session has started
                if (!sessionStarted && currentTime.TimeOfDay >= overnightStart)
                {
                    sessionStarted = true;
                    overnightHigh[0] = High[0];
                    overnightLow[0] = Low[0];
                }
    
                // Check if it's a new session
                if (isNewSessionBar)
                {
                    sessionStarted = false;
                }
    
                // Update the overnight high and low
                if (sessionStarted)
                {
                    if (High[0] > overnightHigh[0])
                        overnightHigh[0] = High[0];
    
                    if (Low[0] < overnightLow[0])
                        overnightLow[0] = Low[0];
                }
    
                // Plot the overnight high and low
                if (sessionStarted)
                {
                    Values[0][0] = overnightHigh[0]; // Plot the overnight high
    
    
                    Values[1][0] = overnightLow[0]; // Plot the overnight low
    
                }
    
            }
        }
    
    }​​
    Click image for larger version  Name:	image.png Views:	0 Size:	91.4 KB ID:	1263225
    Last edited by gummybar; 08-05-2023, 10:46 AM.

    #2
    Hello gummybar,

    The reason he plot looks like you have shown is that it sometimes draws 0 values which would create spikes like that. If you wanted to avoid that you would have to make sure a price is always being plotted even if it was the same price from the previous bar, that would create a consistent plot without 0 segments.

    To have the indiator plot in the chart you would set IsOverlay = true; and then remove and re apply the indicator to the chart.

    Comment


      #3
      Originally posted by NinjaTrader_Jesse View Post
      Hello gummybar,

      The reason he plot looks like you have shown is that it sometimes draws 0 values which would create spikes like that. If you wanted to avoid that you would have to make sure a price is always being plotted even if it was the same price from the previous bar, that would create a consistent plot without 0 segments.

      To have the indiator plot in the chart you would set IsOverlay = true; and then remove and re apply the indicator to the chart.
      Thanks for the reply, i've updated my code, it now shows on the chat, but it still seems to plot 0 values, any ideas based on my code why it doesn't plot the previous value and draws 0 sometime?
      Thanks

      Code:
      #region Using declarations
      using System;
      using System.Collections.Generic;
      using System.ComponentModel;
      using System.ComponentModel.DataAnnotations;
      using System.Linq;
      using System.Text;
      using System.Threading.Tasks;
      using System.Windows;
      using System.Windows.Input;
      using System.Windows.Media;
      using System.Xml.Serialization;
      using NinjaTrader.Cbi;
      using NinjaTrader.Gui;
      using NinjaTrader.Gui.Chart;
      using NinjaTrader.Gui.SuperDom;
      using NinjaTrader.Gui.Tools;
      using NinjaTrader.Data;
      using NinjaTrader.NinjaScript;
      using NinjaTrader.Core.FloatingPoint;
      using NinjaTrader.NinjaScript.DrawingTools;
      #endregion
      
      //This namespace holds Indicators in this folder and is required. Do not change it.
      namespace NinjaTrader.NinjaScript.Indicators
      {
          public class OvernightHighLow : Indicator
          {
              private Series<double> overnightHigh;
              private Series<double> overnightLow;
              private bool sessionStarted;
              protected override void OnStateChange()
      
              {
                  if (State == State.SetDefaults)
                  {
                      Description                                    = @"Indicator to calculate overnight high and low.";
                      Name                                        = "OvernightHighLow";
                      Calculate                                    = Calculate.OnBarClose;
                      IsOverlay                                    = true;
                      DisplayInDataBox                            = true;
                      DrawOnPricePanel                            = true;
                      DrawHorizontalGridLines                        = true;
                      DrawVerticalGridLines                        = true;
                      PaintPriceMarkers                            = true;
                      ScaleJustification                            = NinjaTrader.Gui.Chart.ScaleJustification.Right;
                      //Disable this property if your indicator requires custom values that cumulate with each new market data event.
                      //See Help Guide for additional information.
                      IsSuspendedWhileInactive                    = true;
                       // Adds a blue line style plot
      
                  }
                  else if (State == State.Configure)
                  {
                                      // Initialize the Series<double> objects and add plots
                      overnightHigh = new Series<double>(this);
                      overnightLow = new Series<double>(this);
      
                      AddPlot(Brushes.Red, "OvernightHigh");
                      AddPlot(Brushes.Green, "OvernightLow");
                  }
              }
      
              protected override void OnBarUpdate()
              {
          if (Bars == null || CurrentBar == 0)
              return;
      
          // Define your custom overnight session times here
          TimeSpan overnightStart = new TimeSpan(16, 30, 0); // 4:30 PM
          TimeSpan overnightEnd = new TimeSpan(9, 30, 0);   // 9:30 AM
      
          DateTime currentTime = Time[0];
          DateTime previousTime = Time[1];
      
          bool isNewSessionBar = currentTime.Date != previousTime.Date;
      
          // Check if the overnight session has started
          if (!sessionStarted && currentTime.TimeOfDay >= overnightStart)
          {
              sessionStarted = true;
              overnightHigh[0] = High[0];
              overnightLow[0] = Low[0];
          }
      
          // Check if it's a new session
          if (isNewSessionBar)
          {
              sessionStarted = false;
          }
      
          // Update the overnight high and low
          if (sessionStarted)
          {
              if (High[0] > overnightHigh[0])
                  overnightHigh[0] = High[0];
      
              if (Low[0] < overnightLow[0])
                  overnightLow[0] = Low[0];
          }
      
          // Plot the overnight high and low
          if (sessionStarted)
          {
              if (High[0] != 0) // Check if the current bar's high is not 0
                  Values[0][0] = overnightHigh[0]; // Plot the overnight high
              else
                  Values[0][0] = Values[0][1]; // Plot the previous value
      
              if (Low[0] != 0) // Check if the current bar's low is not 0
                  Values[1][0] = overnightLow[0]; // Plot the overnight low
              else
                  Values[1][0] = Values[1][1]; // Plot the previous value
          }
      }
                 // Expose public properties for the overnight high and low values
              [Browsable(false)]
              [XmlIgnore]
              public double OvernightHigh
              {
                  get { return overnightHigh[0]; }
              }
      
              [Browsable(false)]
              [XmlIgnore]
              public double OvernightLow
              {
                  get { return overnightLow[0]; }
              }
          }
      
      }​

      Comment


        #4
        I also tried to change this but nothing is being drawn...

        Code:
        #region Using declarations
        using System;
        using System.Collections.Generic;
        using System.ComponentModel;
        using System.ComponentModel.DataAnnotations;
        using System.Linq;
        using System.Text;
        using System.Threading.Tasks;
        using System.Windows;
        using System.Windows.Input;
        using System.Windows.Media;
        using System.Xml.Serialization;
        using NinjaTrader.Cbi;
        using NinjaTrader.Gui;
        using NinjaTrader.Gui.Chart;
        using NinjaTrader.Gui.SuperDom;
        using NinjaTrader.Gui.Tools;
        using NinjaTrader.Data;
        using NinjaTrader.NinjaScript;
        using NinjaTrader.Core.FloatingPoint;
        using NinjaTrader.NinjaScript.DrawingTools;
        #endregion
        
        //This namespace holds Indicators in this folder and is required. Do not change it.
        namespace NinjaTrader.NinjaScript.Indicators
        {
            public class OvernightHighLow : Indicator
            {
                private Series<double> overnightHigh;
                private Series<double> overnightLow;
                private bool sessionStarted;
                protected override void OnStateChange()
        
                {
                    if (State == State.SetDefaults)
                    {
                        Description                                    = @"Indicator to calculate overnight high and low.";
                        Name                                        = "OvernightHighLow";
                        Calculate                                    = Calculate.OnBarClose;
                        IsOverlay                                    = false;
                        DisplayInDataBox                            = true;
                        DrawOnPricePanel                            = true;
                        DrawHorizontalGridLines                        = true;
                        DrawVerticalGridLines                        = true;
                        PaintPriceMarkers                            = true;
                        ScaleJustification                            = NinjaTrader.Gui.Chart.ScaleJustification.Right;
                        //Disable this property if your indicator requires custom values that cumulate with each new market data event.
                        //See Help Guide for additional information.
                        IsSuspendedWhileInactive                    = true;
                         // Adds a blue line style plot
        
                    }
                    else if (State == State.Configure)
                    {
                                        // Initialize the Series<double> objects and add plots
                        overnightHigh = new Series<double>(this);
                        overnightLow = new Series<double>(this);
        
                        AddPlot(Brushes.Red, "OvernightHigh");
                        AddPlot(Brushes.Green, "OvernightLow");
                    }
                }
        
                protected override void OnBarUpdate()
                {
            if (Bars == null || CurrentBar == 0)
                return;
        
            // Define your custom overnight session times here
            TimeSpan overnightStart = new TimeSpan(16, 30, 0); // 4:30 PM
            TimeSpan overnightEnd = new TimeSpan(9, 30, 0);   // 9:30 AM
        
            DateTime currentTime = Time[0];
            DateTime previousTime = Time[1];
        
            bool isNewSessionBar = currentTime.Date != previousTime.Date;
        
            // Check if the overnight session has started
            if (!sessionStarted && currentTime.TimeOfDay >= overnightStart)
            {
                sessionStarted = true;
                overnightHigh[0] = High[0];
                overnightLow[0] = Low[0];
            }
        
            // Check if it's a new session
            if (isNewSessionBar)
            {
                sessionStarted = false;
            }
        
            // Update the overnight high and low
            if (sessionStarted)
            {
                if (High[0] > overnightHigh[0])
                    overnightHigh[0] = High[0];
        
                if (Low[0] < overnightLow[0])
                    overnightLow[0] = Low[0];
            }
        
            // Plot the overnight high and low
            if (sessionStarted)
            {
                // Plot the overnight high and low only on the first bar of the session
                if (isNewSessionBar)
                {
                    Draw.Line(this, "OvernightHigh", true, 0, overnightHigh[0], -1, overnightHigh[0] + 5, "Red");
                    Draw.Line(this, "OvernightLow", true, 0, overnightLow[0], -1, overnightLow[0] - 5, "Green");
                }
            }
        }
                   // Expose public properties for the overnight high and low values
                [Browsable(false)]
                [XmlIgnore]
                public double OvernightHigh
                {
                    get { return overnightHigh[0]; }
                }
        
                [Browsable(false)]
                [XmlIgnore]
                public double OvernightLow
                {
                    get { return overnightLow[0]; }
                }
            }
        
        }​

        Comment


          #5
          Hello gummybar,

          I would suggest using Prints to output what your logic is doing so you can have a better idea of why a 0 value is being plotted. I wouldn't be able to tell just by looking at the code as to why that's happening.


          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by NullPointStrategies, Today, 05:17 AM
          0 responses
          41 views
          0 likes
          Last Post NullPointStrategies  
          Started by argusthome, 03-08-2026, 10:06 AM
          0 responses
          124 views
          0 likes
          Last Post argusthome  
          Started by NabilKhattabi, 03-06-2026, 11:18 AM
          0 responses
          64 views
          0 likes
          Last Post NabilKhattabi  
          Started by Deep42, 03-06-2026, 12:28 AM
          0 responses
          41 views
          0 likes
          Last Post Deep42
          by Deep42
           
          Started by TheRealMorford, 03-05-2026, 06:15 PM
          0 responses
          46 views
          0 likes
          Last Post TheRealMorford  
          Working...
          X