Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Plot + 4 Bars

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

    Plot + 4 Bars

    How to plot with addPlot from the condition bar to plus 4 bars?

    This plots only on the condition bar:

    Code:
    namespace NinjaTrader.NinjaScript.Indicators
    {
        public class _plotlineplus4Bars : Indicator
        {
            int cBar;
            
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    Description                                    = @"Enter the description for your new custom Indicator here.";
                    Name                                        = "_plotlineplus4Bars";
                    Calculate                                    = Calculate.OnEachTick;
                    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 Dash-Line style plot with 5pixel width and 70% opacity
                    AddPlot(new Stroke(Brushes.Cyan, DashStyleHelper.Dash, 5, 70), PlotStyle.Hash, "MyPlot1");
                }
                else if (State == State.Configure)
                {
                }
            }
    
            protected override void OnBarUpdate()
            {        
                if (CurrentBar <21) return;
                
                if (Open[0] < Close[0])
                {
                    cBar = CurrentBar;
                    
                    Print(" ");
                    Print("cBar : " + cBar);
                    Print("cBar+5 : " + (cBar+5) + " " + Time[0]);
                    
                    for (int i = cBar; i < (cBar+5) ; i++)
                    {
                        MyPlot1[0] = High[0];
                    }​
                }
            }
            
            #region Properties
            
                [Browsable(false)]
                [XmlIgnore]
                public Series<double> MyPlot1
                {
                  get { return Values[0]; }
                }
                
            #endregion
        }
    }​
    It prints this:

    ...
    cBar : 57268
    cBar+5 : 57273 10/07/2024 07:44:59

    cBar : 57269
    cBar+5 : 57274 10/07/2024 07:45:00

    cBar : 57270
    cBar+5 : 57275 10/07/2024 07:45:32​



    I need this done with addPlot, not with drawings.

    The plot needs to start plotting from the initial condition bar (Open[0] < Close[0]) forward on each bar for 4 bars,
    not from the 4th bar past the condition bar displays.
    Attached Files
    Last edited by PaulMohn; 07-11-2024, 05:01 AM.

    #2
    Hello PaulMohn,

    You won't be able to plot into the future so you would need to set the plot each time the bar closes if it is within 4 bars of your saved bar. When your condition first becomes true you could plot the value for the current bar and set a variable to the CurrentBar like you are doing. On each following bar if the CurrentBar minus your variable is less than 4 then continue to plot. Once its beyond that point you would have to clear your variable so the condition to plot is no longer true.
    Last edited by NinjaTrader_Jesse; 07-11-2024, 12:36 PM.

    Comment


      #3
      Thanks that helped but still 1 issue.

      1. New intermediary valid condition bars overwrite the previous plots from bars 1 to 3.

      For example, when:

      CurrentBar - (Open[0] < Close[0]) is true — it only plots from CurrentBar to CurrentBar
      CurrentBar + 1 - (Open[0] < Close[0]) is true — it only plots from CurrentBar + 1 to CurrentBar + 1
      CurrentBar + 2 - (Open[0] < Close[0]) is true — it only plots from CurrentBar + 2 to CurrentBar + 2
      CurrentBar + 3 - (Open[0] < Close[0]) is true — it plots from CurrentBar + 3 to CurrentBar + 6

      CurrentBar + 4 - (Open[0] < Close[0]) is false — it does not plot
      CurrentBar + 5 - (Open[0] < Close[0]) is false — it does not plot
      CurrentBar + 6 - (Open[0] < Close[0]) is false — it does not plot
      CurrentBar + 7 - (Open[0] < Close[0]) is false — it does not plot


      How to get it to plot also for CurrentBar, CurrentBar + 1, CurrentBar + 2 as with CurrentBar + 3?

      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 _plotlineplus4Bars : Indicator
          {
              int cBar;
              double cHigh;
              
              protected override void OnStateChange()
              {
                  if (State == State.SetDefaults)
                  {
                      Description                                    = @"Enter the description for your new custom Indicator here.";
                      Name                                        = "_plotlineplus4Bars";
                      Calculate                                    = Calculate.OnEachTick;
                      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 Dash-Line style plot with 5pixel width and 70% opacity
                      AddPlot(new Stroke(Brushes.Cyan, DashStyleHelper.Dash, 5, 70), PlotStyle.Hash, "MyPlot1");
                  }
                  else if (State == State.Configure)
                  {
                  }
              }
      
              protected override void OnBarUpdate()
              {        
                  if (CurrentBar <21) return;
                  
                  if (Open[0] < Close[0])
                  {
                      cBar = CurrentBar;
                      cHigh = High[0];
                      MyPlot1[0] = cHigh;
                  }
                  else
                  {
                      MyPlot1.Reset();
                  }
                      
                  if ((CurrentBar - cBar) < 4)
                  {
                      MyPlot1[0] = cHigh;
                  }
              }
              
              #region Properties
              
                  [Browsable(false)]
                  [XmlIgnore]
                  public Series<double> MyPlot1
                  {
                    get { return Values[0]; }
                  }
                  
              #endregion
          }
      }
      
      #region NinjaScript generated code. Neither change nor remove.
      
      namespace NinjaTrader.NinjaScript.Indicators
      {
          public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase
          {
              private _plotlineplus4Bars[] cache_plotlineplus4Bars;
              public _plotlineplus4Bars _plotlineplus4Bars()
              {
                  return _plotlineplus4Bars(Input);
              }
      
              public _plotlineplus4Bars _plotlineplus4Bars(ISeries<double> input)
              {
                  if (cache_plotlineplus4Bars != null)
                      for (int idx = 0; idx < cache_plotlineplus4Bars.Length; idx++)
                          if (cache_plotlineplus4Bars[idx] != null &&  cache_plotlineplus4Bars[idx].EqualsInput(input))
                              return cache_plotlineplus4Bars[idx];
                  return CacheIndicator<_plotlineplus4Bars>(new _plotlineplus4Bars(), input, ref cache_plotlineplus4Bars);
              }
          }
      }
      
      namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns
      {
          public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase
          {
              public Indicators._plotlineplus4Bars _plotlineplus4Bars()
              {
                  return indicator._plotlineplus4Bars(Input);
              }
      
              public Indicators._plotlineplus4Bars _plotlineplus4Bars(ISeries<double> input )
              {
                  return indicator._plotlineplus4Bars(input);
              }
          }
      }
      
      namespace NinjaTrader.NinjaScript.Strategies
      {
          public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase
          {
              public Indicators._plotlineplus4Bars _plotlineplus4Bars()
              {
                  return indicator._plotlineplus4Bars(Input);
              }
      
              public Indicators._plotlineplus4Bars _plotlineplus4Bars(ISeries<double> input )
              {
                  return indicator._plotlineplus4Bars(input);
              }
          }
      }
      
      #endregion
      Last edited by PaulMohn; 07-11-2024, 12:37 PM.

      Comment


        #4
        Hello PaulMohn,

        The MyPlot1.Reset(); is not necessary, that will remove a plot from that bar. To plot for 4 consecutive bars your condition to plot would need to remain true for 4 bars. Open less than Close is a very generic condition that will become true frequently so you may need to add other conditions to that so it does not become true and reset your cBar variable. To prevent plotting once you are past the 4 bars you need to reset the cBar variable so your condition to plot is no longer true.

        Comment


          #5
          Thanks. I need MyPlot1.Reset(); because I use OnEachTick. Otherwise it plots when (Open[0] >= Close[0]) too.



          I need it with Open < Close, no other conditions. The end is to mimmick plotting/drawing in the future with addPlot.

          This doesn't reach the end:

          protected override void OnBarUpdate()
          {
          if (CurrentBar <21) return;

          if (Open[0] < Close[0])
          {
          cBar = CurrentBar;
          cHigh = High[0];
          MyPlot1[0] = cHigh;
          }
          // else
          // {
          // MyPlot1.Reset();
          // }

          if ((CurrentBar - cBar) < 4)
          {
          MyPlot1[0] = cHigh;
          }
          else
          {
          cBar = 0;
          }
          }​
          Last edited by PaulMohn; 07-11-2024, 01:14 PM.

          Comment


            #6
            Hello PaulMohn,

            If that is not working as you wanted you would need to use prints to see why based on how your conditions evaluate.

            The basic concept of plotting 4 bars in a row would require that your condition to plot remains true for 4 bars. Because Open less than Close is a generic condition you could see that the cBar variable gets reset before 4 bars have been plotted leading to longer periods of plotting.

            Comment


              #7
              I understand why it is not working — because the bar condition is true from CurrentBar + 0 to + 3 in


              I need it with Open < Close, no other conditions. The end is to mimmick plotting/drawing in the future with addPlot.
              How to mimmick plotting/drawing in the future with addPlot, without adding supplementary conditions to Open < Close / preventing the cBar reset?

              What other solution you or another technician can provide?
              Why doesn't addPlot plots into the future versus drawings?

              Comment


                #8
                Hello PaulMohn,

                There is no ability to plot into the future so your condition would need to remain true for 4 bars as they elapse to plot on those 4 bars. I don't have a specific solution for you, you would need to work that out with how your script is designed. The condition to set the starting point has the ability to become true again before 4 bars so that would be one problem that you need to work out, you likely need to add additional conditions to that so it does not become true more than once in a 4 bar period.

                Future plotting is not a concept because that market has not happened yet, you can't predict what will happen in the market so no future bars are generated ahead of time. The CurrentBar or most recent bar is the most recent data and what is known up to that point. Your script always processes moving forward so if you wanted a plot to start at X bar and then extend to Y bar you need your condition to be true for each of the bars between those two points. Because the Y bar is a future bar you have to wait until that bar is generated to plot on it.

                Comment


                  #9
                  Why drawings can plot into the future whereas addPlot cannot?

                  Comment


                    #10
                    Hello PaulMohn,

                    Drawing objects use slot system because they can also be manually drawn. While it is not documented or supported you can technically draw on non existant slots. In NinjaScript that is not the case because you are working with bar data, the plot is locked to the bars on the chart. In general drawing is only supported on 0 bars ago or greater for past bars.

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                    0 responses
                    578 views
                    0 likes
                    Last Post Geovanny Suaza  
                    Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                    0 responses
                    334 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
                    553 views
                    1 like
                    Last Post Geovanny Suaza  
                    Started by RFrosty, 01-28-2026, 06:49 PM
                    0 responses
                    551 views
                    1 like
                    Last Post RFrosty
                    by RFrosty
                     
                    Working...
                    X