Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Current DayOHL modification

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

    Current DayOHL modification

    I have a chart that includes extended hours (Pre-Market) I want to modify the default NT8 Current DayOHL indicator (see code below) to show the OHL for regular session hours for stocks (9:30 to 4:00) so not to include premarket data when calculating the High and Low of the day. Any suggestions? Thank you.

    Code:
    //
    // Copyright (C) 2023, NinjaTrader LLC <www.ninjatrader.com>.
    // NinjaTrader reserves the right to modify or overwrite this NinjaScript component with each release.
    //
    #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.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
    {
        /// <summary>
        /// Plots the open, high, and low values from the session starting on the current day.
        /// </summary>
        public class CurrentDayOHL : Indicator
        {
            private DateTime            currentDate            =    Core.Globals.MinDate;
            private double                currentOpen            =    double.MinValue;
            private double                currentHigh            =    double.MinValue;
            private double                currentLow            =    double.MaxValue;
            private DateTime            lastDate            =     Core.Globals.MinDate;
            private SessionIterator        sessionIterator;
    
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    Description                    = Custom.Resource.NinjaScriptIndicatorDescriptionCurrentDayOHL;
                    Name                        = Custom.Resource.NinjaScriptIndicatorNameCurrentDayOHL;
                    IsAutoScale                    = false;
                    DrawOnPricePanel            = false;
                    IsOverlay                    = true;
                    IsSuspendedWhileInactive    = true;
                    ShowLow                        = true;
                    ShowHigh                    = true;
                    ShowOpen                    = true;
                    BarsRequiredToPlot            = 0;
    
                    AddPlot(new Stroke(Brushes.Goldenrod,    DashStyleHelper.Dash, 2), PlotStyle.Square, Custom.Resource.CurrentDayOHLOpen);
                    AddPlot(new Stroke(Brushes.SeaGreen,    DashStyleHelper.Dash, 2), PlotStyle.Square, Custom.Resource.CurrentDayOHLHigh);
                    AddPlot(new Stroke(Brushes.Red,            DashStyleHelper.Dash, 2), PlotStyle.Square, Custom.Resource.CurrentDayOHLLow);
                }
                else if (State == State.Configure)
                {
                    currentDate            = Core.Globals.MinDate;
                    currentOpen            = double.MinValue;
                    currentHigh            = double.MinValue;
                    currentLow            = double.MaxValue;
                    lastDate            = Core.Globals.MinDate;
                }
                else if (State == State.DataLoaded)
                {
                    sessionIterator = new SessionIterator(Bars);
                }
                else if (State == State.Historical)
                {
                    if (!Bars.BarsType.IsIntraday)
                    {
                        Draw.TextFixed(this, "NinjaScriptInfo", Custom.Resource.CurrentDayOHLError, TextPosition.BottomRight);
                        Log(Custom.Resource.CurrentDayOHLError, LogLevel.Error);
                    }
                }
            }
    
            protected override void OnBarUpdate()
            {
                if (!Bars.BarsType.IsIntraday) return;
    
                lastDate         = currentDate;
                currentDate     = sessionIterator.GetTradingDay(Time[0]);
    
                if (lastDate != currentDate || currentOpen == double.MinValue)
                {
                    currentOpen        = Open[0];
                    currentHigh        = High[0];
                    currentLow        = Low[0];
                }
    
                currentHigh            = Math.Max(currentHigh, High[0]);
                currentLow            = Math.Min(currentLow, Low[0]);
    
                if (ShowOpen)
                    CurrentOpen[0] = currentOpen;
    
                if (ShowHigh)
                    CurrentHigh[0] = currentHigh;
    
                if (ShowLow)
                    CurrentLow[0] = currentLow;
            }
    
            #region Properties
            [Browsable(false)]    // this line prevents the data series from being displayed in the indicator properties dialog, do not remove
            [XmlIgnore()]        // this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
            public Series<double> CurrentOpen
            {
                get { return Values[0]; }
            }
    
            [Browsable(false)]    // this line prevents the data series from being displayed in the indicator properties dialog, do not remove
            [XmlIgnore()]        // this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
            public Series<double> CurrentHigh
            {
                get { return Values[1]; }
            }
    
            [Browsable(false)]    // this line prevents the data series from being displayed in the indicator properties dialog, do not remove
            [XmlIgnore()]        // this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
            public Series<double> CurrentLow
            {
                get { return Values[2]; }
            }
    
            [Display(ResourceType = typeof(Custom.Resource), Name = "ShowHigh", GroupName = "NinjaScriptParameters", Order = 1)]
            public bool ShowHigh
            { get; set; }
    
            [Display(ResourceType = typeof(Custom.Resource), Name = "ShowLow", GroupName = "NinjaScriptParameters", Order = 2)]
            public bool ShowLow
            { get; set; }
    
            [Display(ResourceType = typeof(Custom.Resource), Name = "ShowOpen", GroupName = "NinjaScriptParameters", Order = 3)]
            public bool ShowOpen
            { get; set; }
            #endregion
    
            public override string FormatPriceMarker(double price)
            {
                return Instrument.MasterInstrument.FormatPrice(price);
            }
        }
    }​
    I've tried this modification but do not get any plots.

    Code:
    // protected override void OnBarUpdate()
    {
    if (!Bars.BarsType.IsIntraday) return;
    
    lastDate = currentDate;
    currentDate = sessionIterator.GetTradingDay(Time[0]);
    
    // Check if the current bar is within regular trading hours (9:30 am to 4:00 pm)
    if (Time[0].TimeOfDay >= new TimeSpan(9, 30, 0) && Time[0].TimeOfDay <= new TimeSpan(16, 0, 0))
    {
    
    if (lastDate != currentDate || currentOpen == double.MinValue)
    {
    currentOpen = Open[0];
    currentHigh = High[0];
    currentLow = Low[0];
    }
    
    currentHigh = Math.Max(currentHigh, High[0]);
    currentLow = Math.Min(currentLow, Low[0]);
    
    if (ShowOpen)
    CurrentOpen[0] = currentOpen;
    
    if (ShowHigh)
    CurrentHigh[0] = currentHigh;
    
    if (ShowLow)
    CurrentLow[0] = currentLow;
    }
    
    else
    {
    // If the current bar is outside regular trading hours, set the values to NaN (Not-a-Number)
    currentOpen = double.NaN;
    currentHigh = double.NaN;
    currentLow = double.NaN;
    }
    }​
    Last edited by designer01; 11-17-2023, 01:47 PM.

    #2
    Hello designer01,

    The easiest way would be to create a custom Trading hours template with your desired hours and use this as the input series for the standard indicator.




    If you are determined to do this in the code, use prints to debug the behavior and understand why no plot values are being set.


    Start by verifying the code within the time condition block is being reached by printing the time of the bar from within that block.
    Then check the last date comparison condition block is being reached.
    Chelsea B.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by Geovanny Suaza, 02-11-2026, 06:32 PM
    0 responses
    558 views
    0 likes
    Last Post Geovanny Suaza  
    Started by Geovanny Suaza, 02-11-2026, 05:51 PM
    0 responses
    324 views
    1 like
    Last Post Geovanny Suaza  
    Started by Mindset, 02-09-2026, 11:44 AM
    0 responses
    101 views
    0 likes
    Last Post Mindset
    by Mindset
     
    Started by Geovanny Suaza, 02-02-2026, 12:30 PM
    0 responses
    545 views
    1 like
    Last Post Geovanny Suaza  
    Started by RFrosty, 01-28-2026, 06:49 PM
    0 responses
    547 views
    1 like
    Last Post RFrosty
    by RFrosty
     
    Working...
    X