Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Swing vs Low[Math.Min] not as expected

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

    Swing vs Low[Math.Min] not as expected

    Hi, am trying to get the values for the last 3 x swing highs and swing lows using the swing indicator, and the output for the swing low give the same value for 1,2 and 3 ? is the code below valid ? many thanks for looking

    Code:
                    Print("0 1 The high of the swing bar "+Swing(5).SwingHighBar(0, 1, 128)+" is " +High[Math.Max(0, Swing(5).SwingHighBar(0, 1, 128))] );
                    Print("0 2 The high of the swing bar "+Swing(5).SwingHighBar(0, 2, 128)+" is " +High[Math.Max(0, Swing(5).SwingHighBar(0, 2, 128))] );
                    Print("0 3 The high of the swing bar "+Swing(5).SwingHighBar(0, 3, 128)+" is " +High[Math.Max(0, Swing(5).SwingHighBar(0, 3, 128))] );
    
                    Print("0 1 The low of the swing bar "+Swing(5).SwingLowBar(0, 1, 128)+" is " +Low[Math.Min(0, Swing(5).SwingLowBar(0, 1, 128))] );
                    Print("0 2 The low of the swing bar "+Swing(5).SwingLowBar(0, 2, 128)+" is " +Low[Math.Min(0, Swing(5).SwingLowBar(0, 2, 128))] );
                    Print("0 3 The low of the swing bar "+Swing(5).SwingLowBar(0, 3, 128)+" is " +Low[Math.Min(0, Swing(5).SwingLowBar(0, 3, 128))] );

    #2
    Ah, should be Math.Max for a Low also .. ok, my bad .. ?!!!!

    Comment


      #3
      Hello 12tkram, thanks for your post.

      The code you posted works fine for me. I made a test that draws a dot 3 swing highs. Please see attached. To add it to your system, place the file within Documents\NinjaTrader 8\bin\Custom\Indicators.

      Let me know if it is not working after reviewing this, kind regards.
      Attached Files

      Comment


        #4
        that code works ? for me all the values for the swing lows were exactly the same, i switched it over to Math.Max and it seemed to work ok .. I will check out your code and mine is still a bit weird to be honest .. thanks for your feedback ..

        Comment


          #5
          oh, ok, yeh, yours is for the swing highs, mine works fine for the swing highs, it was the swing lows that were the issue.

          Comment


            #6
            Hi,

            I revised my OnBarUpdate method and this is working for me:

            protected override void OnBarUpdate()
            {
            //Swing(int strength).SwingHighBar(int barsAgo, int instance, int lookBackPeriod)
            if(CurrentBar < 128)
            return;

            Draw.Dot(this, "swing1", false, Swing(5).SwingHighBar(0, 1, 128), High[Swing(5).SwingHighBar(0, 1, 128)], Brushes.Red);
            Draw.Dot(this, "swing2", false, Swing(5).SwingHighBar(0, 2, 128), High[Swing(5).SwingHighBar(0, 2, 128)], Brushes.Red);
            Draw.Dot(this, "swing3", false, Swing(5).SwingHighBar(0, 3, 128), High[Swing(5).SwingHighBar(0, 3, 128)], Brushes.Red);

            Draw.Dot(this, "swingLow1", false, Swing(5).SwingLowBar(0, 1, 128), Low[Swing(5).SwingLowBar(0, 1, 128)], Brushes.Green);
            Draw.Dot(this, "swingLow2", false, Swing(5).SwingLowBar(0, 2, 128), Low[Swing(5).SwingLowBar(0, 2, 128)], Brushes.Green);
            Draw.Dot(this, "swingLow3", false, Swing(5).SwingLowBar(0, 3, 128), Low[Swing(5).SwingLowBar(0, 3, 128)], Brushes.Green);
            }

            If you don't get the same thing on your side please let me know.

            Comment


              #7
              12tkram,

              I wanted to add that your swing indicator should be set up at the class level to save resources.

              e.g.

              Code:
              public class TestSwing : Indicator
                  {
              
                      Swing MySwing;
              
                      protected override void OnStateChange()
                      {
                          if (State == State.SetDefaults)
                          {
                              Description                                    = @"Enter the description for your new custom Indicator here.";
                              Name                                        = "TestSwing";
                              Calculate                                    = Calculate.OnBarClose;
                              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;
                          }
                          else if (State == State.Configure)
                          {
                          }
                          else if (State == State.DataLoaded)
                          {
                              MySwing = Swing(5);
                          }
                      }
              
                      protected override void OnBarUpdate()
                      {
                          //Swing(int strength).SwingHighBar(int barsAgo, int instance, int lookBackPeriod)
                          if(CurrentBar < 128)
                              return;
              
                          Draw.Dot(this, "swing1", false, MySwing.SwingHighBar(0, 1, 128), High[MySwing.SwingHighBar(0, 1, 128)], Brushes.Red);
                          Draw.Dot(this, "swing2", false, MySwing.SwingHighBar(0, 2, 128), High[MySwing.SwingHighBar(0, 2, 128)], Brushes.Red);
                          Draw.Dot(this, "swing3", false, MySwing.SwingHighBar(0, 3, 128), High[MySwing.SwingHighBar(0, 3, 128)], Brushes.Red);
              
                          Draw.Dot(this, "swingLow1", false, MySwing.SwingLowBar(0, 1, 128), Low[MySwing.SwingLowBar(0, 1, 128)], Brushes.Green);
                          Draw.Dot(this, "swingLow2", false, MySwing.SwingLowBar(0, 2, 128), Low[MySwing.SwingLowBar(0, 2, 128)], Brushes.Green);
                          Draw.Dot(this, "swingLow3", false, MySwing.SwingLowBar(0, 3, 128), Low[MySwing.SwingLowBar(0, 3, 128)], Brushes.Green);
                      }

              Comment


                #8
                thanks, that looks a lot neater than mine .. I will update my code .. many thanks again for the hints/tips

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                0 responses
                637 views
                0 likes
                Last Post Geovanny Suaza  
                Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                0 responses
                366 views
                1 like
                Last Post Geovanny Suaza  
                Started by Mindset, 02-09-2026, 11:44 AM
                0 responses
                107 views
                0 likes
                Last Post Mindset
                by Mindset
                 
                Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                0 responses
                569 views
                1 like
                Last Post Geovanny Suaza  
                Started by RFrosty, 01-28-2026, 06:49 PM
                0 responses
                571 views
                1 like
                Last Post RFrosty
                by RFrosty
                 
                Working...
                X