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.Green2), PlotStyle.TriangleUp"High");
                    
    AddPlot(new Stroke(Brushes.Red2), 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.Green2), 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.Red2), PlotStyle.TriangleDown"Low");
    }
    }

            
    #region Properties

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

            [
    Browsable(false)]
            [
    XmlIgnore]
            public 
    Series<doubleLow
            
    {
                
    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<doubleinput)
            {
                if (
    cacheHighLow != null)
                    for (
    int idx 0idx cacheHighLow.Lengthidx++)
                        if (
    cacheHighLow[idx] != null &&  cacheHighLow[idx].EqualsInput(input))
                            return 
    cacheHighLow[idx];
                return 
    CacheIndicator<HighLow>(new HighLow(), inputref cacheHighLow);
            }
        }
    }

    namespace 
    NinjaTrader.NinjaScript.MarketAnalyzerColumns
    {
        public 
    partial class MarketAnalyzerColumn MarketAnalyzerColumnBase
        
    {
            public 
    Indicators.HighLow HighLow()
            {
                return 
    indicator.HighLow(Input);
            }

            public 
    Indicators.HighLow HighLow(ISeries<doubleinput )
            {
                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<doubleinput )
            {
                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.
    Paul H.NinjaTrader Customer Service

    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.Green2), PlotStyle.TriangleUp"Highs");
                      
      AddPlot(new Stroke(Brushes.Red2), 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] + TickSize// Place triangle 2 ticks above the high
      {
      AddPlot(new Stroke(Brushes.Green2), PlotStyle.TriangleUp"Highs");
      }

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

              
      #region Properties

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

              [
      Browsable(false)]
              [
      XmlIgnore]
              public 
      Series<doubleLows
              
      {
                  
      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.Green2), PlotStyle.TriangleUp"Highs");
                        
        AddPlot(new Stroke(Brushes.Red2), 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] + TickSize// Place triangle 2 ticks above the high
        {
        AddPlot(new Stroke(Brushes.Green2), PlotStyle.TriangleUp"Highs");
        }

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

                
        #region Properties

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

                [
        Browsable(false)]
                [
        XmlIgnore]
                public 
        Series<doubleLows
                
        {
                    
        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.
            Paul H.NinjaTrader Customer Service

            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.Green2), PlotStyle.TriangleUp"High1");
                              
              AddPlot(new Stroke(Brushes.Red2), 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] + TickSize// Place triangle 2 ticks above the high

              // Condition set 2
              Low1[0] = Low[0] - TickSize// place triangle 2 ticks below the low
              }

                      
              #region Properties

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

                      [
              Browsable(false)]
                      [
              XmlIgnore]
                      public 
              Series<doubleLow1
                      
              {
                          
              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.
                Paul H.NinjaTrader Customer Service

                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
                      Paul H.NinjaTrader Customer Service

                      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] + 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] + 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<doubleLow1
                                
                        {
                                    
                        get { return Values[0]; }
                                }

                                [
                        Browsable(false)]
                                [
                        XmlIgnore]
                                public 
                        Series<doubleHigh1
                                
                        {
                                    
                        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 Haiasi, Today, 06:53 PM
                          1 response
                          3 views
                          0 likes
                          Last Post NinjaTrader_Manfred  
                          Started by ScottWalsh, Today, 06:52 PM
                          1 response
                          6 views
                          0 likes
                          Last Post NinjaTrader_Manfred  
                          Started by ScottW, Today, 06:09 PM
                          1 response
                          4 views
                          0 likes
                          Last Post NinjaTrader_Manfred  
                          Started by ftsc2022, 10-25-2022, 12:03 PM
                          5 responses
                          256 views
                          0 likes
                          Last Post KeyonMatthews  
                          Started by Board game geek, 10-29-2023, 12:00 PM
                          14 responses
                          244 views
                          0 likes
                          Last Post DJ888
                          by DJ888
                           
                          Working...
                          X