Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Previous swing

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

    Previous swing

    Hello!

    I want to have the number of the bars from the current bar back to the previous swing.

    I have tested the following:

    int NumberBars = Swing(5).SwingLowBar(0, 1, 10);
    In this case I have more than one number in my output, maybe all the numbers for the entire chart/period selected?

    How to have only the number of the bars regarding the previous swing?

    Best ragards

    #2
    Hello Stanfillirenfro,

    Thanks for your post.

    "In this case I have more than one number in my output, maybe all the numbers for the entire chart/period selected?"

    When you apply a script to a chart, the script will process all the historical bars in the data series from the beginning of the series to the right edge of the chart executing your code as programmed. So it would, on each OnBarUpdate() perform the check for the swing low bar and return those results.

    "How to have only the number of the bars regarding the previous swing?' The code of .SwingLowBar(0, 1, 10); will return the most recent swing low bars ago within the last 10 bars if one exists otherwise it will return a -1 value. If you are looking for the previous to the current swing then you would need to use something like .SwingLowBar(0, 2, 10); to get the previous swing low bars ago, however you may need to lengthen the number of bars to search.

    Comment


      #3
      Many thanks Paul!
      Valuable information.
      I am wondering!
      I have tried to count the number of the bars from each swing to the opposite one, but the numbers I have obtained do not match with those printed on the output.
      I am missing something?

      Best ragards

      Comment


        #4
        Hello Stanfillirenfro,

        Thanks for your post.

        The swing indicator draws at the swing points with dots, however, it does not determine the swing point at the swing point but requires X number of bars after the swing point to confirm. X is the setting used as Strength. The default strength is 5 bars, so a swing point will not be known(confirmed) until 5 bars after the swing point (or however many bars you have set the strength to).
        If you adjust for the strength setting, does that make things line up?

        Comment


          #5
          Hallo Paul!

          MAny thanks for your reply.
          The explanation is very clear, but I am not moving forward. I am building a strategy and I MUST have the swings. It is not the problem if the swings are confirmed after 5 or 7 bars. It is very importat fo me to locate these swing and it is at this level that I am blocked.

          I have also tested the follwing but without success:

          int NumberBars = (CurrentBar - (Swing(5).SwingLowBar(0, 1, 10)));

          Any help?

          Regards,

          Comment


            #6
            Hello Stanfillirenfro,

            Thanks for your reply.

            I'm not sure why you would subtract the value of CurrentBar from the swinglow bar as the method returns the bars ago value.

            Here is an example you can test with that may be helpful in understanding.

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

            if (mySwing.SwingHigh[0] > 0) // check for valid swing high
            {
            for (int i = 1; i <4; i++) // look for up to 4 instances in the window
            {
            if (mySwing.SwingHighBar(0, i, 30) > -1 ) // check for valid swing high bar
            {
            myswingHighBarsAgo = mySwing.SwingHighBar(0, i, 30); // get the bars ago value for the instance i
            Print (Time[0]+" Instance # "+i+" bars ago: "+myswingHighBarsAgo+ " High was: "+High[myswingHighBarsAgo]);
            }
            }
            }
            }


            Here is an example of that applied to a chart and also checking the output window:

            Click image for larger version

Name:	swingExample2.PNG
Views:	837
Size:	117.1 KB
ID:	1142314

            The Blue rectangle shows the 30 bar look back that the indicator is using to look for swing highs. In the lookback period, it found 2 instances. #1, 6 bars ago and #2, 33 bars ago, so this shows that it will find the swing high points and provide the correct bars ago directly.

            Comment


              #7
              Hello Paul!

              Many thanks!
              It is improving. but while compiling, I am geting errors:
              Swing requires an "int or double" right? Also with the compiling error I have added: "double Swing" and the error is still does not wanish.
              What is wrong?

              Regards?

              Comment


                #8
                Hello Stanfillirenfro,

                Thanks for your reply.

                My example was incomplete, sorry about that.

                I've attached the working example I referenced. Please download, install and test f you wish.

                SwingExample.zip

                To Import:
                1. Download the NinjaScript to your desktop, keep them in the compressed .zip file.
                2. From the Control Center window select the menu Tools > Import > NinjaScript Add-on...
                3. Select the downloaded .zip file
                4. NinjaTrader will then confirm if the import has been successful.
                Critical - Specifically for some NinjaScripts, it will prompt that you are running newer versions of @SMA, @EMA, etc. and ask if you want to replace, press 'No'

                Comment


                  #9
                  Hello Paul and many thanks again for your reply.

                  The strategy on the picture you sent me today at 08:58 AM ist excatly what I want, but the code in the zip. file (11:07 AM) considers/plots on each single bar, what is not I want. I just want the swings.

                  What I am missing?

                  Best regards

                  Comment


                    #10
                    Hello Stanfillirenfro,

                    Thanks for your reply.

                    The code in the zip file as i advised is an example of looking for upto 4 swing highs in a 30 bar lookbavck period and then print the information that it finds, It was not intended as a solution for you.

                    Can you elaborate on what your overall objective is as I don't have the context of the strategy goal to work from?

                    If it helps, please feel free to post a chart image marked up that would help to clarify.

                    Comment


                      #11
                      I just want to cash pullbacks. This is possible for me if I identify a swing and from there I can measure a leg by counting the number of the bars formed after the swing since I am scalper. So it is very important for me to have the previous swing. I also need the accurate position/bar of the previous swing from which I can add one or two tick to be saved if I take a position regarding a stop loss. I just want to identify the previous swing, not the swing #2.
                      I do not have any picture for the moment since I am still struggling to implement the strategy.

                      Any help would be welcome.

                      Best regards

                      Comment


                        #12
                        Hello Stanfillirenfro,

                        Thanks for your reply.

                        Just to be clear by the previous swing you mean the current swing, swing #1 in my picture.

                        The example shows that it is pulling the number of bars from the swing point so I am not sure what else you need then.

                        If you would like a strategy created for you, we can provide a link to 3rd party providers of that kind of service.

                        Comment


                          #13
                          Hello Paul!
                          Thanks again for your reply.
                          You are right whit the explanation of the picture. Paul, please how to get the number of the bars after the swing? It is excactly here that I am blocked. From the swing #1 on your picture, which function allows me to get the number of the bars till current bar (bar 0)? So I have to locate the current swing and than start counting the bars until current bar.

                          For th link,I want to try first and I would revert to you in case I have no other way. For the moment it is better for me to implement so by this way I am learning also. Many thanks in advance.

                          Best regards

                          Comment


                            #14
                            Hello Stanfillirenfro,

                            Thanks for your reply.

                            If you have not already, please download and install the example strategy I posted. That is the complete code and will produce the same results for you.

                            When you look at the code, this is the line: myswingHighBarsAgo = mySwing.SwingHighBar(0, i, 30); // get the bars ago value for the instance i

                            Comment


                              #15
                              Hallo Paul!

                              I have downloaded already and installed You know, this code is writeen as strategy, I first want it as indicator. I have realized that the following triggers errors in "indicator"

                              if (State == State.DataLoaded)
                              {
                              mySwing = Swing(5);
                              AddChartIndicator(mySwing);
                              }

                              Please how can I implement this as indicator and not as strategy? I think this is the key.

                              Best regards

                              Comment

                              Latest Posts

                              Collapse

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