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

Swing indicator within strategies

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

    Swing indicator within strategies

    In strategy script, SwingHigh and SwingLow values are incorrectly calculated by Swing indicator if Calulate Mode is "per tick" instead of "on bar close".

    With NT7 the solution was to create a named instance of the swing indicator an asign it the correct propertie:

    this.MySwing = Swing(str);
    this.MySwing.CalculateOnBarClose = true;


    In NT8 the equivalent I suppose is:

    this.MySwing = Swing(str);
    this.MySwing.Calculate= Calculate.OnBarClose;


    But in this case, the calculated values for SwingHigh and SwingLow ara incorrect and this workaround, that is ok for NT7, doesn't work with NT8.

    I would appreciate any sugestion that help me to solve this problem.

    Thanks in advance

    #2
    Hello lillomur,

    Thank you for your post.

    I am not seeing incorrect values in either NinjaTrader 8 or 7 when setting the Calculate for the indicator separate from the strategy. I am using Update() on the swing indicator in OnBarUpdate(), are you doing this as well?

    You can find my two test strategies attached.

    I look forward to your response.
    Attached Files

    Comment


      #3
      Hi PatrickH,

      Iv'e tried with your strategy "TestSwingHosted.cs" and the problem persists.

      As you can see in the image attached, in this example ( Strength = 15) SwingMin is calculated correctly up to 9:22 bar, but after that, de value change from 1.7327 to 1.7273.

      In this case SwingMax remain ok, but other times both values are incorrect.

      For NT7 I found the solution in this thread http://www.ninjatrader.com/support/f...ad.php?t=48386 and I thought would be the same in NT8.


      Thanks a lot for your help.
      Attached Files

      Comment


        #4
        Hello lillomur,

        Thank you for your response.

        The strategies I provided use a 5 strength Swing, can you confirm you changed this in code to 15 and compiled? If you leave the strategies at 5 strength and add a 5 strength Swing to the chart does it then report correctly?

        I look forward to your response.

        Comment


          #5
          Yes I did, I changed and compile the strength value to 15 before the test.

          I've tested right now with strength=5 and again the values are incorrect, as you can see in the attached image.


          My NT8 release is 8.0.10.0 64-bit (Standard)
          Attached Files

          Comment


            #6
            Hello lillomur,

            Thank you for your patience.

            I am unable to reproduce the same behavior on my end.

            Please send me your log and trace files so that I may look into what occurred. You can do this by going to the Control Center-> Help-> Email Support. Ensuring 'Log and Trace Files' is checked will include these files. This is checked by default.

            Please list 'ATTN: Patrick H' in the subject line and reference this thread in the body of the email.

            I look forward to assisting you further.

            Comment


              #7
              Are there any updates on this issue? I am also running into this same issue when I have my strategy set to 'each tick' (which it requires).

              Comment


                #8
                I don't think NinjaTrader_PatrickH is understanding the issue you are reporting. You can't run Swing on bar close within an intrabar strategy, and Swing is not designed for intrabar.

                First of all, you cannot force an indicator to run in Calculate.OnBarClose within a strategy that is Calculate.OnEachTick or Calculate.OnPriceChange. So, you should not even be trying to do that.

                Not every indicator is capable of running in an intrabar environment. @Swing.cs the inbuilt Swing indicator has problems intrabar.

                Here's an illustration of one of the things that's wrong with it. Just add Swing twice to an ES chart with tick replay turned on, and set one instance to Calculate.OnBarClose and one to Calculate.OnPriceChange. I set the OnBarClose one to have bigger, darker dots, with the other one on top so you could see the differences.

                As you can see, Swing does not return the same values in all cases if it is run intrabar. So, unless or until they fix this, you basically shouldn't use it in a strategy like this. What you should do instead is write your own little function to figure out the swing pivots that way you can be sure it is written in a way that will work intrabar.

                Click image for larger version  Name:	image.png Views:	0 Size:	548.0 KB ID:	1252217
                Bruce DeVault
                QuantKey Trading Vendor Services
                NinjaTrader Ecosystem Vendor - QuantKey

                Comment


                  #9
                  Originally posted by QuantKey_Bruce View Post
                  I don't think NinjaTrader_PatrickH is understanding the issue you are reporting. You can't run Swing on bar close within an intrabar strategy, and Swing is not designed for intrabar.

                  First of all, you cannot force an indicator to run in Calculate.OnBarClose within a strategy that is Calculate.OnEachTick or Calculate.OnPriceChange. So, you should not even be trying to do that.

                  Not every indicator is capable of running in an intrabar environment. @Swing.cs the inbuilt Swing indicator has problems intrabar.

                  Here's an illustration of one of the things that's wrong with it. Just add Swing twice to an ES chart with tick replay turned on, and set one instance to Calculate.OnBarClose and one to Calculate.OnPriceChange. I set the OnBarClose one to have bigger, darker dots, with the other one on top so you could see the differences.

                  As you can see, Swing does not return the same values in all cases if it is run intrabar. So, unless or until they fix this, you basically shouldn't use it in a strategy like this. What you should do instead is write your own little function to figure out the swing pivots that way you can be sure it is written in a way that will work intrabar.

                  Click image for larger version Name:	image.png Views:	0 Size:	548.0 KB ID:	1252217
                  Thanks for your detailed response!

                  Have you found a work around or have been using a fix that solves this problem?

                  Comment


                    #10
                    I just don't use the Swing function - it's easier to just code up something to keep track of the siwng pivots yourself.

                    But, this is part of a broader context. If you regularly develop strategies that are intrabar, you necessarily must become attuned to the issue that unless you've looked at the code for the indicator and are sure it is correct for intrabar operation, you have to be very cautious. Usually, I don't worry about using simple things like SMA or EMA or RSI or Stochastics and the like but for something that has an "awareness of state" like a Swing or a ZigZag I've learned to be very cautious, and often I'll just code it in-line rather than calling the function because I don't want to risk losing hours trying to track down someone else's mistake, or worse yet, it looking like it was is my fault the strategy doesn't work when I was just relying on a functionality that I should not have relied on. It's the easiest thing in the world when writing an indicator like Swing to miss something about how to "reset" when it swings one way intrabar then swings back the other way and you have to "undo" the state flip you just did by returning to the state you were in before when the bar started. That sounds simple, but in a complex indicator it's easy to miss something and have a subtle bug.

                    An indicator like Swing absolutely can be fixed, and perhaps NinjaTrader will fix it. But right now, as you can see, we're still at the stage where we're trying to get a common understanding there is a problem, so if you would like to move forward, you should probably just code the swing determination. It's easy to do so. Then, you won't have to worry about whether the inbuilt one does it correctly.
                    Last edited by QuantKey_Bruce; 05-20-2023, 05:11 PM.
                    Bruce DeVault
                    QuantKey Trading Vendor Services
                    NinjaTrader Ecosystem Vendor - QuantKey

                    Comment


                      #11
                      Today, being Saturday, I was trying to recreate this using Playback
                      Connection, but in my testing I've not seen any issues.

                      ​Bruce, did you generate that chart using Playback Connection, or
                      was this a screenshot you created earlier in the week during a
                      live session?

                      I think issues w/Swing+OnEachTick do exist. But I'm not able to
                      reproduce the problem ... or maybe the issue doesn't manifest
                      in Playback?

                      Bruce, could you please upload a chart template for the chart
                      in that screenshot?

                      Also, could you provide the steps needed (Playback vs Live, etc)
                      so that everyone can reproduce the behavior you're seeing?

                      Thanks!

                      Comment


                        #12
                        bltdavid No, I did it just now, and I checked the box for Tick Replay.

                        These are the steps:
                        1. Make a new chart of ES 06-23. I chose the defaults on my system which are 1-minute bars and 5 days. Check the box for tick replay.
                        2. Add the Swing indicator with its default settings to the chart twice.
                        3. Change the second one to OnPriceChange. (The first one will remain its default OnBarClose.)
                        4. Change the first one to have dots size 4 for each plot and change the colors to dark blue and dark red so you can tell them apart from the defaults.
                        5. Click OK.
                        That's all there is to it. Here's a chart template though as well.

                        Let me know please if you are still unable to reproduce this.

                        Also, I doubt if this is the only bug, it is a bug.

                        Click image for larger version  Name:	image.png Views:	0 Size:	535.0 KB ID:	1252226
                        Bruce DeVault
                        QuantKey Trading Vendor Services
                        NinjaTrader Ecosystem Vendor - QuantKey

                        Comment


                          #13
                          Thanks, Bruce. I download that in a moment.

                          I was finally able to see the issue reproduce using Playback
                          Connection, it looks like the same issue in your screenshot.

                          I did not use Tick Replay, so I think that part is immaterial.

                          See below, Click image for larger version  Name:	IMGDT_20230520_171347.png Views:	0 Size:	108.2 KB ID:	1252230
                          Last edited by bltdavid; 05-20-2023, 06:33 PM.

                          Comment


                            #14
                            Excellent. Just now, I followed your steps (thanks for that).

                            Using just Historical minute data (no Playback) ...

                            With Tick Replay enabled, I reproduced your chart.

                            With Tick Replay disabled, the issue did not reproduce.

                            Here is my Tick Replay enabled chart showing the same issue,
                            Click image for larger version

Name:	IMGDT_20230520_175148.png
Views:	402
Size:	87.9 KB
ID:	1252232

                            Comment


                              #15
                              That's good. It is expected that it would not reproduce the error with tick replay disabled - there's no intrabar evlauation then. The point of the bug demonstration is that Swing does not handle intrabar updates correctly. With tick replay disabled, on historical bars, it would be the same with Calculate.OnPriceChange or Calculate.OnBarClose - one evaluation per bar.

                              Market replay would be the same as tick replay for this purpose - it's just another way to get the intrabar evaluations so the error can be demonstrated.

                              Thanks for taking the time to confirm the issue.
                              Bruce DeVault
                              QuantKey Trading Vendor Services
                              NinjaTrader Ecosystem Vendor - QuantKey

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Mestor, 03-10-2023, 01:50 AM
                              16 responses
                              388 views
                              0 likes
                              Last Post z.franck  
                              Started by rtwave, 04-12-2024, 09:30 AM
                              4 responses
                              31 views
                              0 likes
                              Last Post rtwave
                              by rtwave
                               
                              Started by yertle, Yesterday, 08:38 AM
                              7 responses
                              29 views
                              0 likes
                              Last Post yertle
                              by yertle
                               
                              Started by bmartz, 03-12-2024, 06:12 AM
                              2 responses
                              22 views
                              0 likes
                              Last Post bmartz
                              by bmartz
                               
                              Started by funk10101, Today, 12:02 AM
                              0 responses
                              7 views
                              0 likes
                              Last Post funk10101  
                              Working...
                              X