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?
#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 } } } }
Comment