Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

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
    Hello adeelshahzad,

    Thanks for your post.

    It seems the indicator is not plotting on the next session because you do not have any data for that session yet.

    When an indicator is set to run using Calculate.OnPriceChange, it will plot or draw objects up to the current bar on the chart.

    If you are using Time[0] for the endTime argument of Draw objects, this would draw the object up to the time of the current bar.

    Debugging prints should be added to the indicator (outside the conditions) that prints out all the values being used in the conditions to see how they are evaluating. You should also add prints one line above each of your Draw methods that prints out the values you are passing into the startTime, endTime, staryY, and endY parameters of the Draw objects to see how those are evaluating.

    Below is a link to a forum post that demonstrates using prints to understand behavior.



    Brandon H.NinjaTrader Customer Service

    Comment


      #3
      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


        #4
        Hello adeelshahzad,

        Thanks for your notes.

        No, you would not necessarily need to change the Calculate mode in your script. Calculate.OnBarClose means logic would only process at the close of a bar, whereas Calculate.OnPriceChange means OnBarUpdate() logic would process for each change in price.

        Calculate: https://ninjatrader.com/support/help.../calculate.htm

        Please comment out any logic in the script that is not necessary for reproducing the behavior you are report, retest the strategy to ensure the behavior reproduces, then send us an exported copy of the reduced script along with the steps and settings you are using to reproduce the behavior so we may investigate further.

        To export the script, go to Tools > Export > NinjaScript AddOn.

        Exporting: https://ninjatrader.com/support/help...tAsSourceFiles

        We look forward to assisting further.
        Brandon H.NinjaTrader Customer Service

        Comment


          #5
          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


            #6
            Hello adeelshahzad,

            Thanks for your notes.

            It would not be supported to draw into the blank area of a chart using Draw methods.

            Drawing objects have to be drawn on existing bars that are populated on the chart.

            If you wish to draw in the blank area to the right of the last bar, you would need to use SharpDX in OnRender() to custom render objects on the chart window.

            See the help guide documentation below for more information about SharpDX and OnRender().

            Using SharpDX for Custom Chart Rendering: https://ninjatrader.com/support/help..._rendering.htm
            OnRender(): https://ninjatrader.com/support/help...8/onrender.htm
            Brandon H.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by PaulMohn, Yesterday, 01:45 PM
            3 responses
            1 view
            0 likes
            Last Post NinjaTrader_ChelseaB  
            Started by PaulMohn, Yesterday, 02:06 AM
            5 responses
            17 views
            0 likes
            Last Post PaulMohn  
            Started by ChartTourist, 04-27-2024, 08:22 AM
            4 responses
            55 views
            0 likes
            Last Post NinjaTrader_BrandonH  
            Started by r68cervera, Today, 04:45 AM
            1 response
            11 views
            0 likes
            Last Post NinjaTrader_Jesse  
            Started by AveryFlynn, 04-25-2024, 04:57 AM
            2 responses
            17 views
            0 likes
            Last Post RogerHall  
            Working...
            X