Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Need Help with custom NT8 Indicator script

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

    Need Help with custom NT8 Indicator script

    Hello, i am not a scripter by any means i originally made this as an NT7 strategy and converted to indicator. I see in NT8 i never had to do that, but its not actually plotting... there are no errors on the scripter... please help, the indicator is supposed to plot at triangle just above a high that is higher than two bars to the left or right, and just below a low that is lower than two bars to the right or left. thanks ahead of time!


    PHP 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.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 HighLow : Indicator
        {
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    Description                            = @"Plots Highs and Lows";
                    Name                                = "HighLow";
                    Calculate                            = Calculate.OnBarClose;
                    IsOverlay                            = true;
                    DisplayInDataBox                    = false;
                    DrawOnPricePanel                    = false;
                    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;
                    AddPlot(new Stroke(Brushes.Green, 2), PlotStyle.TriangleUp, "High");
                    AddPlot(new Stroke(Brushes.Red, 2), PlotStyle.TriangleDown, "Low");
                }
                else if (State == State.Configure)
                {
                }
            }
    
            protected override void OnBarUpdate()
            {
    if (CurrentBar<1)
    return;
    
    // Condition set 1
    if (High[0] > High[1]
    && High[0] > High[-1]
    && High[0] > High[2]
    && High[0] > High[-2])
    {
    AddPlot(new Stroke(Brushes.Green, 2), PlotStyle.TriangleUp, "High");
    }
    
    // Condition set 2
    if (Low[0] < Low[1]
    && Low[0] < Low[-1]
    && Low[0] < Low[2]
    && Low[0] < Low[-2])
    {
    AddPlot(new Stroke(Brushes.Red, 2), PlotStyle.TriangleDown, "Low");
    }
    }
    
            #region Properties
    
            [Browsable(false)]
            [XmlIgnore]
            public Series<double> High
            {
                get { return Values[0]; }
            }
    
            [Browsable(false)]
            [XmlIgnore]
            public Series<double> Low
            {
                get { return Values[1]; }
            }
            #endregion
    
        }
    }
    
    #region NinjaScript generated code. Neither change nor remove.
    
    namespace NinjaTrader.NinjaScript.Indicators
    {
        public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase
        {
            private HighLow[] cacheHighLow;
            public HighLow HighLow()
            {
                return HighLow(Input);
            }
    
            public HighLow HighLow(ISeries<double> input)
            {
                if (cacheHighLow != null)
                    for (int idx = 0; idx < cacheHighLow.Length; idx++)
                        if (cacheHighLow[idx] != null &&  cacheHighLow[idx].EqualsInput(input))
                            return cacheHighLow[idx];
                return CacheIndicator<HighLow>(new HighLow(), input, ref cacheHighLow);
            }
        }
    }
    
    namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns
    {
        public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase
        {
            public Indicators.HighLow HighLow()
            {
                return indicator.HighLow(Input);
            }
    
            public Indicators.HighLow HighLow(ISeries<double> input )
            {
                return indicator.HighLow(input);
            }
        }
    }
    
    namespace NinjaTrader.NinjaScript.Strategies
    {
        public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase
        {
            public Indicators.HighLow HighLow()
            {
                return indicator.HighLow(Input);
            }
    
            public Indicators.HighLow HighLow(ISeries<double> input )
            {
                return indicator.HighLow(input);
            }
        }
    }
    
    #endregion 
    

    #2
    Hello Columbcille,

    Thanks for your post.

    In Ninjatrader8 to output to the plot you must use the plot name followed by the bar index value (typically [0]), however the complication is that you have named your plots High and Low which will conflict with the normal High and Low bar references. So step 1 is to rename your plots to something like High1 and Low1 just to make them different. Make sure to change them not only in the state.Defaults but also in the region properties, for example:

    IsSuspendedWhileInactive = true;
    AddPlot(new Stroke(Brushes.Green, 2), PlotStyle.TriangleUp, "High1");
    AddPlot(new Stroke(Brushes.Red, 2), PlotStyle.TriangleDown, "Low1");
    ... and

    [Browsable(false)]
    [XmlIgnore]
    public Series<double> High1
    {
    get { return Values[0]; }
    }

    [Browsable(false)]
    [XmlIgnore]
    public Series<double> Low1
    {
    get { return Values[1]; }
    }


    Next, to output the plots you would assign a value to them when the condition you have are true. For example:

    High1[0] = High[0] + 2 * TickSize; // Place triangle 2 ticks above the high

    Low1[0] = Low[0] - 2 * TickSize; // place triangle 2 ticks below the low


    Finally, the conditions, they are not correct as you cannot use a negative barsago number which would be a bar in the future that does not exist. You will generate a bars ago reference error.


    Once you have your logic in place you will also need to add a line like:

    if (CurrentBar < 2) return; // do not process the first 3 bars

    If you are looking at a bar index of [2] then you need to make sure 3 bars are loaded first and the above statement will have you covered.

    Comment


      #3
      so i THINK ive understood you, renamed the high and low to Highs and Lows (wasnt sure if 1 was actually necessary, and changed the parameter script... now it just crashes NT LOL

      PHP Code:
      //This namespace holds Indicators in this folder and is required. Do not change it. 
      namespace NinjaTrader.NinjaScript.Indicators
      {
          public class HighLow : Indicator
          {
              protected override void OnStateChange()
              {
                  if (State == State.SetDefaults)
                  {
                      Description                            = @"Plots Highs and Lows";
                      Name                                = "HighLow";
                      Calculate                            = Calculate.OnBarClose;
                      IsOverlay                            = true;
                      DisplayInDataBox                    = false;
                      DrawOnPricePanel                    = false;
                      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;
                      AddPlot(new Stroke(Brushes.Green, 2), PlotStyle.TriangleUp, "Highs");
                      AddPlot(new Stroke(Brushes.Red, 2), PlotStyle.TriangleDown, "Lows");
                  }
                  else if (State == State.Configure)
                  {
                  }
              }
      
              protected override void OnBarUpdate()
              {
      if (CurrentBar<2)
      return; // do not process the first 3 bars
      
      // Condition set 1
      Highs[0] = High[0] + 2 * TickSize; // Place triangle 2 ticks above the high
      {
      AddPlot(new Stroke(Brushes.Green, 2), PlotStyle.TriangleUp, "Highs");
      }
      
      // Condition set 2
      Lows[0] = Low[0] - 2 * TickSize; // place triangle 2 ticks below the low
      {
      AddPlot(new Stroke(Brushes.Red, 2), PlotStyle.TriangleDown, "Lows");
      }
      }
      
              #region Properties
      
              [Browsable(false)]
              [XmlIgnore]
              public Series<double> Highs
              {
                  get { return Values[0]; }
              }
      
              [Browsable(false)]
              [XmlIgnore]
              public Series<double> Lows
              {
                  get { return Values[1]; }
              }
              #endregion
      
          }
      } 
      

      Comment


        #4
        Originally posted by Columbcille View Post
        so i THINK ive understood you, renamed the high and low to Highs and Lows (wasnt sure if 1 was actually necessary, and changed the parameter script... now it just crashes NT LOL

        PHP Code:
        //This namespace holds Indicators in this folder and is required. Do not change it. 
        namespace NinjaTrader.NinjaScript.Indicators
        {
            public class HighLow : Indicator
            {
                protected override void OnStateChange()
                {
                    if (State == State.SetDefaults)
                    {
                        Description                            = @"Plots Highs and Lows";
                        Name                                = "HighLow";
                        Calculate                            = Calculate.OnBarClose;
                        IsOverlay                            = true;
                        DisplayInDataBox                    = false;
                        DrawOnPricePanel                    = false;
                        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;
                        AddPlot(new Stroke(Brushes.Green, 2), PlotStyle.TriangleUp, "Highs");
                        AddPlot(new Stroke(Brushes.Red, 2), PlotStyle.TriangleDown, "Lows");
                    }
                    else if (State == State.Configure)
                    {
                    }
                }
        
                protected override void OnBarUpdate()
                {
        if (CurrentBar<2)
        return; // do not process the first 3 bars
        
        // Condition set 1
        Highs[0] = High[0] + 2 * TickSize; // Place triangle 2 ticks above the high
        {
        AddPlot(new Stroke(Brushes.Green, 2), PlotStyle.TriangleUp, "Highs");
        }
        
        // Condition set 2
        Lows[0] = Low[0] - 2 * TickSize; // place triangle 2 ticks below the low
        {
        AddPlot(new Stroke(Brushes.Red, 2), PlotStyle.TriangleDown, "Lows");
        }
        }
        
                #region Properties
        
                [Browsable(false)]
                [XmlIgnore]
                public Series<double> Highs
                {
                    get { return Values[0]; }
                }
        
                [Browsable(false)]
                [XmlIgnore]
                public Series<double> Lows
                {
                    get { return Values[1]; }
                }
                #endregion
        
            }
        } 
        
        Incorrect use of the reserved word Highs, Lows, which have a very specific meaning in NT: a specific meaning which would likely cause NT to crash if used in the manner that you have.

        Use the given suggestion, High1 etc.,

        Comment


          #5
          Originally posted by koganam View Post
          Incorrect use of the reserved word Highs, Lows, which have a very specific meaning in NT: a specific meaning which would likely cause NT to crash if used in the manner that you have.

          Use the given suggestion, High1 etc.,
          ok i just changed them all to Hish1 and Low1 and i still crash

          Comment


            #6
            Hello Columbcille,

            Thanks for your replies.

            In your code:
            Highs[0] = High[0] + 2 * TickSize; // Place triangle 2 ticks above the high
            {
            AddPlot(new Stroke(Brushes.Green, 2), PlotStyle.TriangleUp, "Highs");
            }

            // Condition set 2
            Lows[0] = Low[0] - 2 * TickSize; // place triangle 2 ticks below the low
            {
            AddPlot(new Stroke(Brushes.Red, 2), PlotStyle.TriangleDown, "Lows");
            }

            You would need to delete both of the AddPlot(...) lines.

            Comment


              #7
              Originally posted by NinjaTrader_Paul View Post
              Hello Columbcille,

              Thanks for your replies.

              In your code:
              Highs[0] = High[0] + 2 * TickSize; // Place triangle 2 ticks above the high
              {
              AddPlot(new Stroke(Brushes.Green, 2), PlotStyle.TriangleUp, "Highs");
              }

              // Condition set 2
              Lows[0] = Low[0] - 2 * TickSize; // place triangle 2 ticks below the low
              {
              AddPlot(new Stroke(Brushes.Red, 2), PlotStyle.TriangleDown, "Lows");
              }

              You would need to delete both of the AddPlot(...) lines.
              ok so i did that, before trying and crashing again, this is the correct code then?

              PHP 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.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 HighLow : Indicator
                  {
                      protected override void OnStateChange()
                      {
                          if (State == State.SetDefaults)
                          {
                              Description                            = @"Plots Highs and Lows";
                              Name                                = "HighLow";
                              Calculate                            = Calculate.OnBarClose;
                              IsOverlay                            = true;
                              DisplayInDataBox                    = false;
                              DrawOnPricePanel                    = false;
                              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;
                              AddPlot(new Stroke(Brushes.Green, 2), PlotStyle.TriangleUp, "High1");
                              AddPlot(new Stroke(Brushes.Red, 2), PlotStyle.TriangleDown, "Low1");
                          }
                          else if (State == State.Configure)
                          {
                          }
                      }
              
                      protected override void OnBarUpdate()
                      {
              if (CurrentBar<2)
              return; // do not process the first 3 bars
              
              // Condition set 1
              High1[0] = High[0] + 2 * TickSize; // Place triangle 2 ticks above the high
              
              // Condition set 2
              Low1[0] = Low[0] - 2 * TickSize; // place triangle 2 ticks below the low
              }
              
                      #region Properties
              
                      [Browsable(false)]
                      [XmlIgnore]
                      public Series<double> High1
                      {
                          get { return Values[0]; }
                      }
              
                      [Browsable(false)]
                      [XmlIgnore]
                      public Series<double> Low1
                      {
                          get { return Values[1]; }
                      }
                      #endregion
              
                  }
              } 
              

              Comment


                #8
                Hello Columbcille,

                Thanks for your reply.

                Yes, the code shown should compile and run. The output will be a red and green triangle on every bar.

                Comment


                  #9
                  Originally posted by NinjaTrader_Paul View Post
                  Hello Columbcille,

                  Thanks for your reply.

                  Yes, the code shown should compile and run. The output will be a red and green triangle on every bar.
                  ok but thats not what i was trying to get it to do, in NT7 code i has it only place a triangle on the bars whose highs were higher than the previous and following two bars

                  Comment


                    #10
                    I added back in the NT7 script without the "-1" &"-2" and have partial success...

                    How do i get it to correct with the future bars though? here is a chart i have arrows where correct and circles where im trying to remove the triangle
                    Attached Files

                    Comment


                      #11
                      Hello Columbcille,

                      Thanks for your reply.

                      That is the issue you will need to resolve. You cannot know what future bars will do so you would need to test for the high being higher than the next two bars which means you would not be able to identify the high until two more bars have passed.

                      In that regard then you could only test when the current bar is the 2nd bar past the high.

                      Here is an example:

                      if (High[0] < High[2] && High[1] < High[2] && High[3] < High[2] && High[4] < High[2])
                      {
                      High1[2] = High[2] + 2 * TickSize; //plot triangle 2 bars ago
                      }

                      I've attached a picture that will illustrate the bar references related above.
                      Attached Files

                      Comment


                        #12
                        ok i get what youre saying, the script you posted looked like what i had in NT& so i copied and pasted, but now it crashes again...

                        This works
                        PHP Code:
                        if (CurrentBar < 2) return; // do not process the first 3 bars
                        
                        // Condition set 1 
                        if (High[0] > High[1] 
                        && High[0] > High[2])
                        High1[0] = High[0] + 5 * TickSize; //plot triangle 
                        
                        This doesnt
                        PHP Code:
                        if (CurrentBar < 2) return; // do not process the first 3 bars
                        
                        // Condition set 1 
                        if (High[2] > High[0] 
                        && High[2] > High[1] 
                        && High[2] > High[3] 
                        && High[2] > High[4])
                        High1[2] = High[2] + 5 * TickSize; //plot triangle 
                        
                        the extra { } around the plot height didnt correct anything either. Im strarting to understand trhe logic in the code, and it seems like it should work especially with the CurrentBar.

                        Do i need to change something withing the properties field values?
                        PHP Code:
                        #region Properties
                        
                                [Browsable(false)]
                                [XmlIgnore]
                                public Series<double> Low1
                                {
                                    get { return Values[0]; }
                                }
                        
                                [Browsable(false)]
                                [XmlIgnore]
                                public Series<double> High1
                                {
                                    get { return Values[1]; }
                                }
                                #endregion 
                        

                        Comment


                          #13
                          AH nvm i changed the CurrentBar to 4 and it worked!!! thanks a ton!

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                          0 responses
                          670 views
                          0 likes
                          Last Post Geovanny Suaza  
                          Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                          0 responses
                          379 views
                          1 like
                          Last Post Geovanny Suaza  
                          Started by Mindset, 02-09-2026, 11:44 AM
                          0 responses
                          111 views
                          0 likes
                          Last Post Mindset
                          by Mindset
                           
                          Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                          0 responses
                          575 views
                          1 like
                          Last Post Geovanny Suaza  
                          Started by RFrosty, 01-28-2026, 06:49 PM
                          0 responses
                          582 views
                          1 like
                          Last Post RFrosty
                          by RFrosty
                           
                          Working...
                          X