Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

How to get previous Swing high/low values?

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

    How to get previous Swing high/low values?

    Hi - I trying to develop a strategy that needs to read the previous 2-3 swing high/low values in Swing indicator. It seems like the indicator itself doesn't memorize previous values. Would you please provide any adivce on how this can be done?

    Thank you!

    #2
    Hello HiddenPhilosopher,

    Thanks for your post.

    Here is a link to the help guide on the swing indicator: https://ninjatrader.com/support/help...nt8/?swing.htm

    The help guide shows that the swing indicator provide the means to access previous swing points. For example from the help guide you see:
    Swing(int strength).SwingHighBar(int barsAgo, int instance, int lookBackPeriod) Where you specify the lookback period (bars) and then the instance to look for where 1 is the latest, 2 if the prior and 3 would be before that. The method then returns the bars ago that the instance occurred and with that you can access the High or Low of those data points.

    If you are working in the Strategy Builder, the swing indicator only provides the Swing High or Swing low, You can detect a change in the swing points by checking the previous bar of the swing high to the current bar of the swing high and when they are Not equal a new swing high has been found. This would provide you with the current swing and the previous swing high values. (same for the swing Low).

    Paul H.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_PaulH View Post
      Hello HiddenPhilosopher,

      Thanks for your post.

      Here is a link to the help guide on the swing indicator: https://ninjatrader.com/support/help...nt8/?swing.htm

      The help guide shows that the swing indicator provide the means to access previous swing points. For example from the help guide you see:
      Swing(int strength).SwingHighBar(int barsAgo, int instance, int lookBackPeriod) Where you specify the lookback period (bars) and then the instance to look for where 1 is the latest, 2 if the prior and 3 would be before that. The method then returns the bars ago that the instance occurred and with that you can access the High or Low of those data points.

      If you are working in the Strategy Builder, the swing indicator only provides the Swing High or Swing low, You can detect a change in the swing points by checking the previous bar of the swing high to the current bar of the swing high and when they are Not equal a new swing high has been found. This would provide you with the current swing and the previous swing high values. (same for the swing Low).

      That's not what I'm looking for. I'm using it in coding my strategy. This sounds like, if I set a lookback period, it re-calculates the high/low points based on the number of bars I set to look back. (e.x. 5 bars lookback and 15 bars lookback can have very different results) What I'm looking for is the previous high/low values recorded on the chart, which are fixed.

      Comment


        #4
        Hello HiddenPhilosopher,

        Thanks for your reply.

        The look back does not recalculate the swing, it will report the bar where the swing point is of the instance you are looking for. Once you have the bars ago where the swing point occurs you can pull the High and Low from the price series using the bars ago reference.

        I've created a short video to demonstrate just using the swing high side: https://paul-ninjatrader.tinytake.co...M18xMzU5MjA1NA


        Paul H.NinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_PaulH View Post
          Hello HiddenPhilosopher,

          Thanks for your reply.

          The look back does not recalculate the swing, it will report the bar where the swing point is of the instance you are looking for. Once you have the bars ago where the swing point occurs you can pull the High and Low from the price series using the bars ago reference.

          I've created a short video to demonstrate just using the swing high side: https://paul-ninjatrader.tinytake.co...M18xMzU5MjA1NA

          Thank you so much for the video! This is very clear!

          Comment


            #6
            Hi Paul,

            My SwingHighs are working. But my SwingLows aren't. Why?

            In the attached screenshot, why does it consider 15 bars ago to be the 3:16 PM Bar most recent Swing Low (there's no Swing Low yellow dots 15 bars ago) — why not the previous Swing Low (with yellow dots at 2:28 PM)?

            How to make it consider the previous (yellow dotted) Swing Low instead?
            Click image for larger version  Name:	why.png Views:	0 Size:	862.1 KB ID:	1159618

            And why does the Print in the 2nd for loop block (SwingLowBar) doesn't appear in the output while the Print in the 1st for loop block (SwingHighBar) does?

            EDIT:
            To better see:


            Thanks a lot for your insights!

            EDIT2:
            I found out how to get the previous SwingLow:
            I needed to increase the Lookback number to 50.
            It was also the solution to the the missing SwingLow Prints.
            A screenshot:
            Last edited by Cormick; 06-11-2021, 05:30 AM. Reason: Solution: Increase the Lookback parameter

            Comment


              #7
              Hello Cormick,

              Thanks for your post.

              Sounds like you resolved your question, well done.

              Paul H.NinjaTrader Customer Service

              Comment


                #8
                Originally posted by NinjaTrader_PaulH View Post
                Hello HiddenPhilosopher,

                Thanks for your post.

                Here is a link to the help guide on the swing indicator: https://ninjatrader.com/support/help...nt8/?swing.htm

                The help guide shows that the swing indicator provide the means to access previous swing points. For example from the help guide you see:
                Swing(int strength).SwingHighBar(int barsAgo, int instance, int lookBackPeriod) Where you specify the lookback period (bars) and then the instance to look for where 1 is the latest, 2 if the prior and 3 would be before that. The method then returns the bars ago that the instance occurred and with that you can access the High or Low of those data points.

                If you are working in the Strategy Builder, the swing indicator only provides the Swing High or Swing low, You can detect a change in the swing points by checking the previous bar of the swing high to the current bar of the swing high and when they are Not equal a new swing high has been found. This would provide you with the current swing and the previous swing high values. (same for the swing Low).
                Thanks NinjaTrader_PaulH for that information. What I tried in the Strategy Builder was to create a Custom Series of Swing Highs. After that tried comparing SwingHigh[0] to SwingHigh[1] and that I guess cannot be accomplished in Strategy Builder? Because it was taking trades even when the condition was not true. It was drawing arrows correctly though on the Swing Highs as defined by a bar with a lower high on either side of it.
                Appreciate all your help.

                Comment


                  #9
                  Hello Trader17,

                  Thanks for your post.

                  If you would like to place an order when an arrow is being drawn, you would need to call your Entry order method in the same Set that you are calling your drawing object method.

                  If the expected trade(s) are appearing, this would indicate that the condition to place the order is evaluating as true in your script.

                  Ultimately, to understand why the script is behaving as it is, such as placing orders or not placing orders or drawing objects when expected, it is necessary to add prints to the script that print the values used for the logic of the script to understand how the script is evaluating.

                  In the strategy add prints (outside of any conditions) that print the values of every variable used in every condition that places an order along with the time of that bar. Prints will appear in the NinjaScript Output window (New > NinjaScript Output window).

                  Below is a link to a forum post that demonstrates how to use prints to understand behavior.
                  https://ninjatrader.com/support/foru...121#post791121

                  Let us know if we may assist further.
                  Brandon H.NinjaTrader Customer Service

                  Comment


                    #10
                    HiddenPhilosopher Active Trader Setups .com has a modified version of the Swing Hi Low indicator that was updated to include Plots to use in a Strategy.

                    They also have a ZigZag that plots the Last 3 Highs and Last 3 Lows of the Zig Zag Indicator, those plots remain available until a new plots replaces it. (the standard Zig Zag only shows the Plotted Value for 1 Bar

                    Comment


                      #11
                      Originally posted by NinjaTrader_PaulH View Post
                      Hello HiddenPhilosopher,

                      Thanks for your reply.

                      The look back does not recalculate the swing, it will report the bar where the swing point is of the instance you are looking for. Once you have the bars ago where the swing point occurs you can pull the High and Low from the price series using the bars ago reference.

                      I've created a short video to demonstrate just using the swing high side: https://paul-ninjatrader.tinytake.co...M18xMzU5MjA1NA

                      Hi Paul,

                      I am looking to use this in my existing strategy which I run currently in a live acct. I added the code to see the values for swing high/swing low and its not showing me the correct values.
                      For example on 8:12 bar (4Min chart) for 1/31/2023 of ES Swing indicator is correctly showing me the last swing high as 4061 (2 bars ago) however when I run the following code I see a different value for swing high that's 20 bars ago

                      Here is what I am using -

                      if (mySwing.SwingHigh[0] > 0)
                      {
                      for (int i = 1; i < 2; i++)
                      {
                      if (mySwing.SwingHighBar(0,i,18) > -1)
                      {
                      mySwingHighBarsAgo = mySwing.SwingHighBar(0, i, 18);
                      Print(Time[0]+" Instance # "+i+" bars ago: "+mySwingHighBarsAgo+ " High was: "+High[mySwingHighBarsAgo]);
                      }
                      }
                      }​

                      On the console output I see the following

                      1/31/2023 8:12:00 AM Instance # 1 bars ago: 20 High was: 4049.5
                      1/31/2023 8:16:00 AM Instance # 1 bars ago: 21 High was: 4049.5
                      1/31/2023 8:20:00 AM Instance # 1 bars ago: 22 High was: 4049.5
                      1/31/2023 8:24:00 AM Instance # 1 bars ago: 5 High was: 4061
                      1/31/2023 8:28:00 AM Instance # 1 bars ago: 6 High was: 4061
                      1/31/2023 8:32:00 AM Instance # 1 bars ago: 7 High was: 4061
                      1/31/2023 8:36:00 AM Instance # 1 bars ago: 8 High was: 4061
                      1/31/2023 8:40:00 AM Instance # 1 bars ago: 9 High was: 4061
                      1/31/2023 8:44:00 AM Instance # 1 bars ago: 10 High was: 4061
                      1/31/2023 8:48:00 AM Instance # 1 bars ago: 11 High was: 4061
                      1/31/2023 8:52:00 AM Instance # 1 bars ago: 12 High was: 4061
                      1/31/2023 8:56:00 AM Instance # 1 bars ago: 13 High was: 4061​

                      Its picking up swing high of 4061 that I need but for 8:24 candle instead of 8:12 candle. On the Swing indicator on the chart it shows correctly

                      What am I missing here?
                      Last edited by vpatanka; 01-31-2023, 10:43 PM.

                      Comment


                        #12
                        Hello vpatanka,

                        I'm am seeing your suggested code is matching the chart exactly.

                        Below is a link to a video testing the suggested code.
                        https://drive.google.com/file/d/1kl1...w?usp=drivesdk

                        Are you certain your indicators are on the same chart and using the same parameters? (Including the Calculate parameter and Strength parameter)

                        What are you comparing with that is indicating the values are not matching?
                        Chelsea B.NinjaTrader Customer Service

                        Comment


                          #13
                          Originally posted by NinjaTrader_ChelseaB View Post
                          Hello vpatanka,

                          I'm am seeing your suggested code is matching the chart exactly.

                          Below is a link to a video testing the suggested code.
                          https://drive.google.com/file/d/1kl1...w?usp=drivesdk

                          Are you certain your indicators are on the same chart and using the same parameters? (Including the Calculate parameter and Strength parameter)

                          What are you comparing with that is indicating the values are not matching?
                          Hi Chelsea,
                          I put that code in a Ninjatrader strategy, compiled it and ran it in "Strategy Analyzer" for 1/31/2022 with a print statement to catch the last swing high from 8:12 bar (which was at 4061 -2 bars ago) but I am seeing different values as mentioned in the previous post. I will look at the video you posted and will take a look at it again

                          Comment


                            #14
                            Originally posted by vpatanka View Post

                            Hi Chelsea,
                            I put that code in a Ninjatrader strategy, compiled it and ran it in "Strategy Analyzer" for 1/31/2022 with a print statement to catch the last swing high from 8:12 bar (which was at 4061 -2 bars ago) but I am seeing different values as mentioned in the previous post. I will look at the video you posted and will take a look at it again
                            Thanks for the video. I didn't realize the for loop wasn't really doing anything as it had only one iteration. I copy pasted the code that Paul had provided earlier on this thread and totally missed that

                            Anyway I updated the code and made it very simple to print last swing high for one single bar i.e. 8:12am (on 4min chart of ES for 1/31/2023)
                            If you run the attached strategy code for ES in Strategy Tester with start time as 1/31/2023 and end time as 1/31/2023 you can see the following output on the console

                            1/31/2023 8:12:00 AM Instance # 20 High was 4049.5
                            1/31/2023 8:12:00 AM Instance # 20 High was 4049.5​

                            Two questions -
                            1) Why is it showing Print() statement two times instead of one as in the strategy code its specific to a single bar so shouldn't there be only one?
                            2) For 8:12am 4min bar on 1/31/2023 if you look at the chart the last swing high was 2 bars ago at 4061 but it's going back and picking up swing high from 20 bars ago . Not sure why. If I add Swing(5) indicator on the 4min chart it shows the last swing high for 8:12am bar correctly at 4061

                            Let me know what I am missing here. Thanks in advance. You guys are awesome !
                            Attached Files
                            Last edited by vpatanka; 02-01-2023, 05:16 PM.

                            Comment


                              #15
                              Hello vpatanka,

                              First, may I confirm you followed the video I provided exactly, with exactly the same code, and the behavior was different?


                              Your script calls AddDataSeries(Data.BarsPeriodType.Minute, 4);

                              OnBarUpdate() will update for every series you have added.

                              You are seeing multiple prints because OnBarUpdate() is being updated for multiple series at that time.

                              Below is a link to the help guide on BarsInProgress.



                              Your script also now splits the setting of mySwingHighBarsAgo and the print, which is not in the code you have suggested.
                              Try the code you suggested in post Infractions and follow along with the video I provided you.

                              If you were actually intending to change when the mySwingHighBarsAgo is assigned a value and not have this in the same condition action block, what are trying to achieve?
                              Chelsea B.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by xiinteractive, 04-09-2024, 08:08 AM
                              3 responses
                              11 views
                              0 likes
                              Last Post NinjaTrader_Erick  
                              Started by Johnny Santiago, 10-11-2019, 09:21 AM
                              95 responses
                              6,193 views
                              0 likes
                              Last Post xiinteractive  
                              Started by Irukandji, Today, 09:34 AM
                              1 response
                              3 views
                              0 likes
                              Last Post NinjaTrader_Clayton  
                              Started by RubenCazorla, Today, 09:07 AM
                              1 response
                              6 views
                              0 likes
                              Last Post RubenCazorla  
                              Started by TraderBCL, Today, 04:38 AM
                              3 responses
                              26 views
                              0 likes
                              Last Post NinjaTrader_Jesse  
                              Working...
                              X