Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Indicator not plotting after session break

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

    Indicator not plotting after session break

    ​Hi,

    I have plotted lines based on the previous day prices series. Which is doing fine on historical data.

    The issue I am facing is that it is not plotting lines for the next trading session i.e. 1800 hrs April 17th onwards when it is running in real-time. This is shown in the attached screenshot​

    More detail,
    1. When I load the indicator it works perfectly fine for all previous days i.e. plotting in a day session based on previous day session
    2. But the issue is it is not plotting in the next trading session after the session break i.e. at 1800 hrs when indicator is already running
    Code,
    HTML Code:
    public class FloorTest : Indicator
    {
        private double var_0;
        private double var_1;
        private double var_2;
        private double var_3;
        private double var_4;
        private double var_5;
        private double var_6;
        private double var_7;
        private double var_8;
        private double var_9;
        private double var_10;
        private double var_12;
        private double var_13;
        private PloyType _pivotPlotType;
        NinjaTrader.Gui.Tools.SimpleFont _font;
    
        private SessionIterator sessionIterator;
        private readonly List<int> newSessionBarIdxArr = new List<int>();
        private DateTime currentDate = Globals.MinDate;
        private DateTime cacheSessionDate = Globals.MinDate;
        private DateTime cacheSessionEnd = Globals.MinDate;
        private DateTime cacheTime;
        private DateTime sessionDateTmp = Globals.MinDate;
    
        private void SetDefaults()
        {
            Name = "Floor Test";
            Description = "";
    
            Calculate = Calculate.OnPriceChange;
            IsAutoScale = false;
            IsOverlay = true;
            IsSuspendedWhileInactive = false;
            DisplayInDataBox = true;
            DrawOnPricePanel = true;
            PaintPriceMarkers = true;
            ScaleJustification = Gui.Chart.ScaleJustification.Right;
            _font = new NinjaTrader.Gui.Tools.SimpleFont("Courier New", 12) { Size = 12, Bold = true };
            var_13 = 1;
    
        }
    
        private void SetParameters()
        {
            _pivotPlotType = PloyType.Five;
        }
    
        protected override void OnStateChange()
        {
            if (State == State.SetDefaults)
            {
                SetDefaults();
                SetParameters();
            }
            else if (State == State.Configure)
            {
                ClearOutputWindow();
                _pivotPlotType = PivotPlotType;
    
                AddDataSeries(Instrument.FullName, new BarsPeriod { BarsPeriodType = BarsPeriodType.Day, Value = 1 },
                    "CME US Index Futures RTH", true);
            }
            else if (State == State.DataLoaded)
            {
                sessionIterator = new SessionIterator(BarsArray[1]);
            }
        }
    
        protected override void OnBarUpdate()
        {
            if (CurrentBar < 1 && BarsInProgress != 0) return;
    
            DateTime lastBarTimeStamp = GetLastBarSessionDate(Times[0][0]);
            if (currentDate != Globals.MinDate && lastBarTimeStamp != currentDate)
            {
                bool plotCondition;
                DateTime today = DateTime.Now;
                if (Time[0].DayOfYear < today.DayOfYear)
                {
                    var_13 += 1;
                    var_12 = PriorDayOHLC().PriorClose[0];
                    var_7 = PriorDayOHLC().PriorHigh[0];
                    var_8 = var_7;
                    var_9 = PriorDayOHLC().PriorLow[0];
                    var_10 = var_9;
                    var_6 = (var_8 + var_10 + var_12) / 3;
                    var_3 = var_6 * 2 - var_10;
                    var_4 = var_6 + var_8 - var_10;
                    var_5 = var_4 + var_8 - var_10;
                    var_0 = var_6 * 2 - var_8;
                    var_1 = var_6 - var_8 + var_10;
                    var_2 = var_1 - var_8 + var_10;
                }
                else
                {
                    if (High[0] > var_7)
                        var_7 = High[0];
                    if (Low[0] < var_9)
                        var_9 = Low[0];
                }
    
                plotCondition = var_13 >= 2 && Bars.BarsType.IsIntraday;
                if (plotCondition)
                {
                    DateTime firstBarTimestamp = lastBarTimeStamp.AddHours(-6);
                    DateTime endBar = firstBarTimestamp.AddHours(24);
                    if (_pivotPlotType == PloyType.Seven)
                    {
                        Draw.Line(this, "line_R3_" + Time[0], false, firstBarTimestamp, var_5, endBar, var_5,
                            Brushes.Green, DashStyleHelper.Solid, 2);
                        Draw.Text(this, "text_R3_" + Time[0], false, "R3", endBar, var_5, 0,
                            Brushes.Green, _font, System.Windows.TextAlignment.Center,
                            Brushes.Green, Brushes.Green, 5);
                    }
                    Draw.Line(this, "line_R2_" + Time[0], true, firstBarTimestamp, var_4, endBar, var_4,
                        Brushes.LimeGreen, DashStyleHelper.Solid, 2);
                    Draw.Text(this, "text_R2_" + Time[0], true, "R2", endBar, var_4, 0,
                            Brushes.LimeGreen, _font, System.Windows.TextAlignment.Center,
                            Brushes.LimeGreen, Brushes.LimeGreen, 5);
    
                    Draw.Line(this, "line_R1_" + Time[0], false, firstBarTimestamp, var_3, endBar, var_3,
                        Brushes.DarkOliveGreen, DashStyleHelper.Solid, 2);
                    Draw.Text(this, "text_R1_" + Time[0], false, "R1", endBar, var_3, 0,
                            Brushes.DarkOliveGreen, _font, System.Windows.TextAlignment.Center,
                            Brushes.DarkOliveGreen, Brushes.DarkOliveGreen, 5);
    
                    Draw.Line(this, "line_PP_" + Time[0], false, firstBarTimestamp, var_6, endBar, var_6,
                        Brushes.LightBlue, DashStyleHelper.Solid, 5);
                    Draw.Text(this, "text_PP_" + Time[0], false, "PP", endBar, var_6, 0,
                            Brushes.LightBlue, _font, System.Windows.TextAlignment.Center,
                            Brushes.LightBlue, Brushes.LightBlue, 5);
    
                    Draw.Line(this, "line_S1_" + Time[0], false, firstBarTimestamp, var_0, endBar, var_0,
                        Brushes.Red, DashStyleHelper.Solid, 2);
                    Draw.Text(this, "text_S1_" + Time[0], false, "S1", endBar, var_0, 0,
                            Brushes.Red, _font, System.Windows.TextAlignment.Center,
                            Brushes.Red, Brushes.Red, 5);
    
                    Draw.Line(this, "line_S2_" + Time[0], false, firstBarTimestamp, var_1, endBar, var_1,
                        Brushes.IndianRed, DashStyleHelper.Solid, 2);
                    Draw.Text(this, "text_S2_" + Time[0], false, "S2", endBar, var_1, 0,
                            Brushes.IndianRed, _font, System.Windows.TextAlignment.Center,
                            Brushes.IndianRed, Brushes.IndianRed, 5);
    
                    if (_pivotPlotType == PloyType.Seven)
                    {
                        Draw.Line(this, "line_S3_" + Time[0], false, firstBarTimestamp, var_2, endBar, var_2,
                            Brushes.MediumVioletRed, DashStyleHelper.Solid, 2);
                        Draw.Text(this, "text_S3_" + Time[0], false, "S3", endBar, var_2, 0,
                            Brushes.MediumVioletRed, _font, System.Windows.TextAlignment.Center,
                            Brushes.MediumVioletRed, Brushes.MediumVioletRed, 5);
                    }
                }
            }
            currentDate = lastBarTimeStamp;
        }
    
        #region Private Methods
        private DateTime GetLastBarSessionDate(DateTime time)
        {
            // Check the time[0] against the previous session end
            if (time > cacheSessionEnd)
            {
                if (Bars.BarsType.IsIntraday)
                {
                    // Make use of the stored session iterator to find the next session...
                    sessionIterator.GetNextSession(time, true);
                    // Store the actual session's end datetime as the session
                    cacheSessionEnd = sessionIterator.ActualSessionEnd;
                    // We need to convert that time from the session to the users time zone settings
                    sessionDateTmp = TimeZoneInfo.ConvertTime(cacheSessionEnd.AddSeconds(-1), Globals.GeneralOptions.TimeZoneInfo, Bars.TradingHours.TimeZoneInfo).Date;
                }
                else
                {
                    sessionDateTmp = time.Date;
                }
            }
    
            if (sessionDateTmp != cacheSessionDate)
            {
                if (newSessionBarIdxArr.Count == 0 || newSessionBarIdxArr.Count > 0 && CurrentBar > newSessionBarIdxArr[newSessionBarIdxArr.Count - 1])
                {
                    newSessionBarIdxArr.Add(CurrentBar);
                }
                cacheSessionDate = sessionDateTmp;
            }
            return sessionDateTmp;
        }
        #endregion
    
        public enum PloyType
        {
            Five,
            Seven
        }
    
        #region Parameters
        [NinjaScriptProperty]
        [Display(ResourceType = typeof(Custom.Resource), Name = "Plot Type", GroupName = "Plot Parameters", Order = 0)]
        public PloyType PivotPlotType
        {
            get; set;
        }
        #endregion
    }​
    Attached Files

    #2
    Thanks Brandon for the reply.
    1. Do I need to change property
      HTML Code:
      Calculate
      to
      HTML Code:
      Calculate.OnBarClose
      ?
    2. As you asked, endTime is set by calculating startTime and then adding 24 hours to it
    3. I have also now added the attachment with debug outputs the way you suggested, below
    Attached Files

    Comment


      #3
      Thanks Brandon for your suggestions.

      Moreover, I would like to bring into your notice that issue is not reproducible in Playback mode. In Playback mode, the plots are created automatically as expected i.e. when a trading session ends and next starts the plots are drawn.

      Please see attachment.
      Attached Files
      Last edited by adeelshahzad; 04-18-2024, 10:58 AM.

      Comment

      Latest Posts

      Collapse

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