Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Adding stock Swing indicator to my custom indicator does not work.

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

    Adding stock Swing indicator to my custom indicator does not work.

    I am attempting to add the Swing indicator that comes with NT8 to my custom indicator. The first chart below is the default Swing indicator with a strength of 5. The second chart is my custom indicator. The swing lines do draw on the second chart but seem displaced by 5 or so bars to the right.

    Click image for larger version

Name:	Swing Indicator Problem.png
Views:	374
Size:	41.2 KB
ID:	1096217

    Here is the code for my custom indicator which produced the drawing on the second chart:

    Code:
    namespace NinjaTrader.NinjaScript.Indicators
    {
        public class MySwing : Indicator
        {
            private Swing swing = null;
    
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    Description                                    = @"Enter the description for your new custom Indicator here.";
                    Name                                        = "MySwing";
                    Calculate                                    = Calculate.OnBarClose;
                    IsOverlay                                    = false;
                    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;
    
                    AddPlot(new Stroke(Brushes.DimGray, 1), PlotStyle.Hash, "SwingIndicatorHigh");
                    AddPlot(new Stroke(Brushes.DimGray, 1), PlotStyle.Hash, "SwingIndicatorLow");
                }
                else if (State == State.Configure)
                {
                }
                else if (State == State.DataLoaded) {
                    swing = Swing(5);
                }
            }
    
            protected override void OnBarUpdate()
            {
                SwingIndicatorHigh[0] = swing.SwingHigh[0];
                SwingIndicatorLow[0] = swing.SwingLow[0];
    
            }
    
            #region Properties
    
            [Browsable(false), XmlIgnore]
            public Series<double> SwingIndicatorHigh { get { return Values[0]; } }
    
            [Browsable(false), XmlIgnore]
            public Series<double> SwingIndicatorLow { get { return Values[1]; } }
    
            #endregion
        }
    }
    Any suggestion why the two charts are not identical?







    #2
    Hi robertlaub, thanks for your question.

    The Swing high and low series are not in sync with the primary data series. Try using the SwingHighBar() or SwingLowBar() methods to get the bar of the last swing instead:

    Swing(ISeries<double> input, int strength).SwingHighBar(int barsAgo, int instance, int lookBackPeriod)
    Swing(ISeries<double> input, int strength).SwingLowBar(int barsAgo, int instance, int lookBackPeriod)

    With that bar info, you can plot the same thing the Swing indicator is plotting by targeting specific bars in the past.

    Please let me know if I can assist any further.

    Comment


      #3
      Thank you for the quick response, but I am still having trouble.
      Following the documentation,

      HTML Code:
      // Prints the high price of the most recent swing high
      Print("The high of the swing bar is " + High[Math.Max(0, Swing(5).SwingHighBar(0, 1, 10))]);
      I updated my indicator above to:

      Code:
      protected override void OnBarUpdate()
              {
                  int highBar = swing.SwingHighBar(0, 1, 10);
                  int lowBar = swing.SwingLowBar(0, 1, 10);
                  if (highBar != -1) {
                      SwingIndicatorHigh[0] = High[highBar];
                  }
                  if (lowBar != -1) {
                      SwingIndicatorLow[0] = Low[lowBar];
                  }
              }
      And there is no change in the drawing of my indicator.

      What am I doing wrong?

      Comment


        #4
        Hi robertlaub, thanks for your reply.

        There is still an incorrect offset because your plotting to the current bar (assuming SwingIndicatorHigh is your plot). To replicate the Swing indicator you would need to plot at the lowBar/highBar value e.g.
        SwingIndicatorHigh[highBar] = High[highBar]; Please let me know if this does not resolve your inquiry.

        Comment


          #5
          Yes, as you can tell from the code in my first post, SwingIndicatorHigh is the swing highs plot as SwingIndicatorLow is the swing lows plot.

          I tried your change earlier out of desperation and it did not correct the problem but made it worse. If I make the following changes:

          Code:
                  protected override void OnBarUpdate()
                  {
                      int highBar = swing.SwingHighBar(0, 1, 10);
                      int lowBar = swing.SwingLowBar(0, 1, 10);
                      if (highBar != -1) {
                          SwingIndicatorHigh[highBar] = High[highBar];
                      }
                      if (lowBar != -1) {
                          SwingIndicatorLow[lowBar] = Low[lowBar];
                      }
                  }
          the lines on my plot (both highs and lows) disappear completely so I do believe that your suggestion is not the solution.

          Comment


            #6
            Hi robertlaub, thanks for your reply.

            I will test a few items with the Swing indicator and reply here with my findings.

            Thanks in advance for your patience.

            Comment


              #7
              Hi robertlaub,

              The way the Swing indicator plots is very unique.

              We have a similar discussion on the Swing indicator here and I posted an example of a single dot swing that shows where a swing level is initialized instead of focusing on drawing a series of highs/lows.



              To replicate the swing exactly, you would have to mimic the same way the swing indicator is plotting.

              Please let me know if I can assist any further.

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
              0 responses
              579 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
              554 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