Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Global method : GetBarsAgo()

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

    Global method : GetBarsAgo()

    NT Team,

    I use the below (and attached) method extensively in Indicators with great success (e.g. High(GetBarsAgo(barNumber))). Indicators calculate and plot as expected.
    Code:
    namespace NinjaTrader.NinjaScript.Indicators
    {
      public partial class Indicator
      {
        public int GetBarsAgo(int barNumber) 
        {
          return CurrentBar - barNumber;
        }
      }
    }
    However, when a Strategy is added to the chart, this method appears to result in exception of type 'System.ArgumentOutOfRangeException' in mscorlib.dll but was not handled in user code. It would appear, when a Strategy is added CurrentBar returns the BarNumber of the last BarNumber for every bar processed (e.g. if chart "Bars to load" = 1850 then CurrentBar = 1849). This logically returns the OutOfRange exception for any bar that is not the last bar.

    This method worked as expected in NT7 with Indicators and Strategies.
    Please advise how this method must change for NT8.

    Regards
    Shannon
    Attached Files

    #2
    Hmm, I do not get any errors when I run that from a strategy or indicator. Can you provide the strategy that results in the exception?

    As a side note, are you aware of the Bars.GetHigh(idx) method? There is a method for each bars data point which gets you that value of the absolute index. I.e., If you pass in Bars.GetHigh(0); it will return the high of the very first bar on the chart.
    MatthewNinjaTrader Product Management

    Comment


      #3
      Also, I suppose you could definitely hit an exception if you tried calculating that OnBarUpdate() when the Currentbar is less than the value you passed in, but that would be the same between an indicator or strategy so I would need clarification on why you get that in one class but not the other. Nor has the behavior changed since NinjaTrader 7. Please let me know exactly what you're passing in to your helper method and I'll be happy to review.
      MatthewNinjaTrader Product Management

      Comment


        #4
        Mathew,

        Thank you for your response, and thank you for the "Bars.GetHigh(idx)" method. I will look to refactor code to include it.

        On attempting to further debug this issue, I have hit another snag. To better understand when the issue presents, Print commands have been added to MyStrategy and MyIndicator. Print commands are placed at the end of State.Configure and at the start of OnBarUpdate as per the below code:
        Code:
        public class MyStrategy: Strategy
        {
          private MyIndicator _myIndicator;
        
          protected override void OnStateChange()
          {
            if (State == State.Configure)
            {
              _myIndicator = MyIndicator();
              Print("MyStrategy State.Configure complete");
            }
          }
        
          protected override void OnBarUpdate()
          {
            Print("MyStrategy OnBarUpdate begun");
            //etc
          }
        }
        
        public class MyIndicator: Indicator
        {
          
          protected override void OnStateChange()
          {
            if (State == State.Configure)
            {
              Print("MyIndicator State.Configure complete");
            }
          }
        
          protected override void OnBarUpdate()
          {
            Print("MyIndicator OnBarUpdate begun");
            //etc
          }
        }
        Which results in the output:
        MyIndicator State.Configure complete
        MyStrategy State.Configure complete
        The output window appears to show that while State.Configure is completed, OnBarUpdate is not begun before the 'System.ArgumentOutOfRangeException' is encountered.

        Debugging in Visual Studio still shows the 'System.ArgumentOutOfRangeException' as a result of referencing the method GetBarsAgo. However, I do not understand how this code has been hit without passing through the Print commands "MyStrategy OnBarUpdate begun" or "MyIndicator OnBarUpdate begun".

        Any ideas are welcome
        Regards
        Shannon

        Comment


          #5
          Hello,

          One thing to note in your code is that you are not necessarily printing those statements when the State completes, but rather just printing them within the state. You may find more insight by printing "State Active" in several States, including State.Historical. This way, you can know for certain whether State.Historical is entered before or after the error.
          Dave I.NinjaTrader Product Management

          Comment


            #6
            Dave,

            On your advice, the code has been changed to the below for MyStrategy (and similar for MyIndicator):
            Code:
            public class MyStrategy: Strategy
            {
            
              protected override void OnStateChange()
              {
                Print("Strategy - " + State + " begun");
                //etc
              }
            
              protected override void OnBarUpdate()
              {
                Print("Strategy - OnBarUpdate begun");
                //etc
              }
            }
            This results in:
            Open Strategy window:
            Strategy - SetDefaults begun
            Strategy - Terminated begun
            Strategy - SetDefaults begun
            Add MyStrategy to Configured Strategy frame
            Strategy - SetDefaults begun
            Order Confirmation pops up
            Strategy - SetDefaults begun
            Strategy - Terminated begun
            Strategy - Terminated begun
            Strategy - Terminated begun
            OK Order Confirmation
            Strategy - Configure begun
            Strategy - Configure begun
            Indicator - SetDefaults begun
            Indicator - Configure begun
            Strategy - Terminated begun
            Indicator - Terminated begun
            If I understand this correctly, OnBarUpdate is never executed.
            The error occurs during Strategy State.Configure.

            Regards
            Shannon

            Comment


              #7
              Thank you -- that does seem to confirm that the error is being triggered before OnBarUpdate() executes. I'm going to dig into this one a bit, and I will post back here with any new information.
              Dave I.NinjaTrader Product Management

              Comment


                #8
                It seems clear that something is going wrong in the Configure State of the hosted indicator, but we are unable to reproduce the type of results you are seeing. Can you share your script(s) with us, so that we can try to replicate the issue and work with a reproducible scenario?
                Dave I.NinjaTrader Product Management

                Comment


                  #9
                  Dave,

                  As requested, the scripts with full explanation on how to replicate the issue was sent via email to PlatformSupport on 2 August with the subject line "ATTN: NinjaTrader_Dave RE: NT Forum thread - Global method : GetBarsAgo())".

                  Please advise if the communication was received.

                  Thanks
                  Shannon

                  Comment


                    #10
                    Sorry for the delay on this one -- I'll post back here with more info soon.
                    Dave I.NinjaTrader Product Management

                    Comment


                      #11
                      Hello,

                      Unfortunately, I'm not able to reproduce this behavior on my end. I've tried both the Beta 3 build and the most recent build on my side, and each time I reload NinjaScript, the indicator plots the same as before the reload, and the strategy remains enabled.

                      Can you think of anything else going on in your full code that may be causing the issue?
                      Dave I.NinjaTrader Product Management

                      Comment


                        #12
                        Dave,

                        Thank you for the reply.

                        Given your testing did not show any issue and your testing started with a blank slate, I rebuilt the indicator from a blank slate. While the incidence of the issue is greatly reduced, one error in approximately 50-100 refreshes, the issue still presents.

                        The issue presents in the exact code as attached to the 2 August 2015 email (I am not testing the full code at this time).

                        The only event which may account for the reduced frequency of error is an update of Microsoft Visual Studio Professional 2013 to Version 12.0.40629.00 Update 5.

                        Any ideas for further investigation would be greatly appreciated.

                        Thanks again
                        Shannon

                        Comment


                          #13
                          Dave,

                          In the VS debug output window, the below output is produced.
                          The thread 0xdf4 has exited with code 259 (0x103).
                          The thread 0xdc0 has exited with code 259 (0x103).
                          The thread 0xbf8 has exited with code 259 (0x103).
                          The thread 0xfac has exited with code 259 (0x103).
                          To date, the output does not appear to have a detrimental impact.

                          To ensure it not causing any harm, I'd like to address and remove the cause.
                          Any ideas on how to investigate what is causing this output?

                          Regards
                          Shannon

                          Comment


                            #14
                            Dave,

                            I have converted the NT8 indicator and strategy (as attached to the 2 August 2015 email) back to NT7 with minor reversions made for code breaking changes.

                            The indicator and strategy are stable in NT7. While this is a crude test, I can only deduce the issue is with the implementation of the indicator and strategy in NT8.

                            Any ideas are welcome.
                            Regards
                            Shannon

                            Comment


                              #15
                              Dave,

                              To assist in recreating the error I am experiencing, please amend the Global Methods.cs file to include the below bold code.

                              Code:
                              namespace NinjaTrader.NinjaScript.Indicators
                              {
                                public partial class Indicator
                                {
                                  public int GetBarsAgo(int barNumber) 
                                  {
                                    [B]Debug.Print(Time[0].ToString("g") + " - CurrentBar = \t" + CurrentBar);[/B]
                                    return CurrentBar - barNumber;
                                  }
                                }
                              }
                              The addition of above code results in the below excerpt from the VS Output window when a strategy uses an indicator using the GetBarsAgo() method. Please note the strategy was applied to a chart where "Bars to load" = 1850. Please note "..." represents output lines that have been removed for conciseness):
                              2015-08-18 06:32:58:328 Indicator - SetDefaults begun
                              2015-08-18 06:32:58:335 Indicator - Terminated begun
                              2015-08-18 06:32:58:342 Indicator - Configure begun
                              2015-08-18 06:32:58:349 Indicator - DataLoaded begun
                              2015-08-18 06:32:58:371 Indicator - Historical begun
                              2015-08-18 06:32:58:374 10/07/2014 1:30 AM - CurrentBar = 35
                              2015-08-18 06:32:58:375 10/07/2014 5:30 AM - CurrentBar = 39
                              2015-08-18 06:32:58:376 10/07/2014 5:30 AM - CurrentBar = 39
                              ...
                              2015-08-18 06:32:58:520 6/09/2014 1:30 AM - CurrentBar = 232
                              2015-08-18 06:32:58:520 6/09/2014 1:30 AM - CurrentBar = 232
                              2015-08-18 06:32:58:520 Strategy - SetDefaults begun
                              2015-08-18 06:32:58:520 6/09/2014 1:30 AM - CurrentBar = 232
                              2015-08-18 06:32:58:520 6/09/2014 1:30 AM - CurrentBar = 232
                              ...
                              2015-08-18 06:32:58:560 13/09/2014 4:30 AM - CurrentBar = 243
                              2015-08-18 06:32:58:560 13/09/2014 4:30 AM - CurrentBar = 243
                              2015-08-18 06:32:58:560 Strategy - Terminated begun
                              2015-08-18 06:32:58:562 16/09/2014 5:30 AM - CurrentBar = 251
                              2015-08-18 06:32:58:562 17/09/2014 12:30 AM - CurrentBar = 253
                              ...
                              2015-08-18 06:32:58:621 26/09/2014 4:30 AM - CurrentBar = 306
                              2015-08-18 06:32:58:621 26/09/2014 4:30 AM - CurrentBar = 306
                              2015-08-18 06:32:58:623 26/09/2014 4:30 AM - CurrentBar = 1849
                              2015-08-18 06:32:58:623 26/09/2014 4:30 AM - CurrentBar = 1849
                              ...
                              2015-08-18 06:32:58:639 Indicator - Transition begun
                              2015-08-18 06:32:58:640 Indicator - Realtime begun
                              I believe the output highlights two issues
                              1. When a Strategy is added using indicators which use the GetBarsAgo() method, midway through calculation CurrentBar has returned the BarNumber of the last BarNumber of the chart (i.e. the chart "Bars to load" = 1850 thus CurrentBar = 1849).
                              2. Strategy OnStateChanges appear to be occurring midway through calculation.


                              Please assist
                              Regards
                              Shannon

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by RookieTrader, Today, 09:37 AM
                              1 response
                              7 views
                              0 likes
                              Last Post NinjaTrader_ChelseaB  
                              Started by alifarahani, Today, 09:40 AM
                              0 responses
                              4 views
                              0 likes
                              Last Post alifarahani  
                              Started by Gerik, Today, 09:40 AM
                              0 responses
                              4 views
                              0 likes
                              Last Post Gerik
                              by Gerik
                               
                              Started by KennyK, 05-29-2017, 02:02 AM
                              3 responses
                              1,283 views
                              0 likes
                              Last Post NinjaTrader_Clayton  
                              Started by AttiM, 02-14-2024, 05:20 PM
                              11 responses
                              185 views
                              0 likes
                              Last Post NinjaTrader_ChelseaB  
                              Working...
                              X