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

Previous Swing Highs

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

    Previous Swing Highs

    Hi,

    In reviewing a previous post, I found a code to determine previous swing highs.


    int BarsAgo = Swing(5).SwingHighBar(0, 1, Bars.BarsSinceNewTradingDay);
    if(BarsAgo < 0 || CurrentBar <= BarsAgo) return;
    double swingHigh = Swing(5).SwingHigh[BarsAgo];
    Print(swingHigh);

    int BarsAgo2 = Swing(5).SwingHighBar(0, 2, Bars.BarsSinceNewTradingDay);
    if(BarsAgo2 < 0 || CurrentBar <= BarsAgo2) return;
    double swingHigh2 = Swing(5).SwingHigh[BarsAgo2];
    Print(swingHigh2);

    This is exactly what I was looking for, but when I add this to my strategy, it compiles but does not run in the chart. It produces a bars issue (i have 20 years of data loaded) I believe due to the if(BarsAgo < 0 || CurrentBar <= BarsAgo) return;.

    I'm not sure how to fix this, so I was wondering if anyone has any suggestions on what I can try?

    Thanks,
    Lee
    Last edited by lee612801; 02-17-2021, 07:54 AM.

    #2
    Hello lee612801,

    Thank you for your post.

    Do you see any errors occur in the Log tab of the Control Center? If so, what is the error reporting?

    Does your script contain any secondary data series added with AddDataSeries()?

    Please send us a screenshot of the bars issue you are encountering so we may investigate this matter further.

    I look forward to your reply.
    Brandon H.NinjaTrader Customer Service

    Comment


      #3
      Hi Brandon,

      The script does not contain any AddDataSeries. Here is the only message in the Output.

      1/3/2000 4:00:00 PM CancelAllOrders: BarsInProgress=0.

      So when I check the BarsAgo with just
      int BarsAgo = Swing(5).SwingHighBar(0, 1, Bars.BarsSinceNewTradingDay);, I get primarily -1.

      Once I include the,
      if(BarsAgo < 0 || CurrentBar <= BarsAgo) return;, It no longer makes any trades and just states
      1/3/2000 4:00:00 PM CancelAllOrders: The BarsAgo is now 5 and the BarsInProgress is 0.

      All I really want is to compare the first previous swing high value with the second previous swing high value. The script I posted above should do that If I could get it to work. If you have any other suggestions on how to do this, that's fine too.

      Lee
      Last edited by lee612801; 02-17-2021, 07:54 AM.

      Comment


        #4
        Hello lee612801,

        Thank you for that information.

        If the expected trade(s) are not appearing, this would indicate that the condition to place the order is not evaluating as true or the order is being ignored for other reasons.

        To understand why the script is behaving as it is, such as placing orders or not placing orders 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 using prints to understand behavior and including a link to a video recorded using the Strategy Builder.
        https://ninjatrader.com/support/foru...121#post791121

        Please let me know if I may further assist
        Brandon H.NinjaTrader Customer Service

        Comment


          #5
          Brandon,

          Are you able to use the Strategy builder to find the two previous Swing Highs using the Swing indicator? If not, how would you suggest going about finding the last two Swing Highs ?

          Thanks,

          Lee

          Comment


            #6
            Hello lee612801,

            Thank you for your note.

            This could be accomplished using the Strategy Builder. You could create a condition in the Conditions and Actions section that compares the Swing indicator Swing High plot 1 bar ago with the Swing indicator Swing High plot 2 bars ago.

            Please see the attached example script that demonstrates comparing the previous Swing High plot to the Swing High plot 2 bars ago and calls EnterLong.

            Also, see the help guide documentation below for more information.


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

            Comment


              #7
              Brandon,

              Thanks for the info. After reviewing the help files on the Swing indicator and following your suggestions with the strategy builder I got the original code I sent to work. My issue was with the Swing indicator look back was not set high enough. So I just changed the Bars.BarsSinceNewTradingDay to 200 and now it works as intended.

              Thanks for all the help.

              Regards,
              Lee

              Comment


                #8
                Brandon:

                Thanks for posting the example. I played the with the strategy you posted to learn/modfiy it. My coding goal is to efficiently get historical values for swinghigh/low & compare developing price to past swing levels for trading decisions. The example code prints error after processing several bars on the OnBarUpdate method when trying to "access an index with a value that is invalid. Would you help me figure out what's causing error? I made a print line mod to debug. My executable and log have been attached for convenience. Look forward to your reply. Thanks!
                Attached Files
                Last edited by Kicks.Spin; 03-17-2021, 11:12 AM.

                Comment


                  #9
                  Hello Kicks.Spin,

                  Thank you for your post.

                  I am unable to view the code of your script because it was exported as a compiled assembly. You would need to export your script as source files in order for the code to be viewable.

                  Exporting - https://ninjatrader.com/support/helpGuides/nt8/index.html?export.htm#ExportingninjascriptAsSource Files

                  That being said, I see in your log file that you are getting the following error.

                  Error on calling 'OnBarUpdate' method on bar 225: You are accessing an index with a value that is invalid since it is out-of-range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.

                  This error will be displayed if you try to use a BarsAgo when it is not valid. A more simple example using one series would be on bar 5 you check for 6 BarsAgo. There are not yet 6 bars so the CurrentBar minus 6 would be a negative number or a non-existent bar.

                  A CurrentBar (single data series) or CurrentBars (multiple data series) check would need to be added to your script to ensure that there are enough bars loaded in the data series you are accessing.

                  Please see the help guide documentation below regarding making sure you have enough bars loaded.
                  Make sure you have enough bars - https://ninjatrader.com/support/helpGuides/nt8/
                  CurrentBar - https://ninjatrader.com/support/help...currentbar.htm
                  CurrentBars - https://ninjatrader.com/support/help...urrentbars.htm

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

                  Comment


                    #10
                    Brandon:

                    Believe you can see the attached indicator exported as a script source. I am aware of the links you provided. Please run the code. it will fail on bar x. change up the chart from 500 to 1000 ticks or something. it will fail on another bar. what can be out of range. thanks for pointing me in right direction
                    Attached Files

                    Comment


                      #11
                      Hello Kicks.Spin,

                      Thank you for your note.

                      I am able to view your code in your recently attached indicator script. I see in your script that you are referencing BarsAgo values. Since you are referencing BarsAgo values in your script, you would need to add a CurrentBar check to ensure that there are enough bars loaded for the data series that you are accessing. The code for a CurrentBar check would look something like this.

                      if (CurrentBar < 10)
                      return;

                      Please see my previous post regarding information about adding a CurrentBar check to your script.

                      Additionally, I see that you are passing in 'mySwingHighBarsAgo' for the barsAgo value in mySwingHighValue = High[mySwingHighBarsAgo];

                      In the output window I see that the print for 'mySwingHighBarsAgo' sometimes returns a value of -1. The value -1 cannot be used as a barsAgo value. You would need to pass a barsAgo value of 0 or greater into High[barsAgo].

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

                      Comment


                        #12
                        Thanks. This line worked good to make sure there are a couple of swing levels to process. The code just progresses on bar x when ever condition met
                        if (Swing1.SwingHighBar(0, 1, 50) < 1 || Swing1.SwingHighBar(0, 2, 50) <1) return; // exit if the last 2 swing high bar numbers can not be identified (-1 is returned for error)

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by DJ888, 04-16-2024, 06:09 PM
                        6 responses
                        18 views
                        0 likes
                        Last Post DJ888
                        by DJ888
                         
                        Started by Jon17, Today, 04:33 PM
                        0 responses
                        1 view
                        0 likes
                        Last Post Jon17
                        by Jon17
                         
                        Started by Javierw.ok, Today, 04:12 PM
                        0 responses
                        6 views
                        0 likes
                        Last Post Javierw.ok  
                        Started by timmbbo, Today, 08:59 AM
                        2 responses
                        10 views
                        0 likes
                        Last Post bltdavid  
                        Started by alifarahani, Today, 09:40 AM
                        6 responses
                        41 views
                        0 likes
                        Last Post alifarahani  
                        Working...
                        X