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

Strategy triggering before BarsRequiredToTrade

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

    #16
    Right... I think I get where the confusion originates: I thought the 9 bars I set up are the number of bars since I activated the strategy, but from your post it looks like they are the first historical bars from the start of the chart?

    That makes 'Out of range errors' so much stranger, then. Please see my other post below, similar with the errors I am talking about in my first post on this thread. How can I possibly get 'Out of range errors' when I have 5 days of data on my chart?

    Hello I am coding a strategy that uses a crossover of two T3 moving averages and the strategy stops and throws the error message in the output window: "Error: Error on calling 'EventHandlerBarsUpdate' method: Object reference not set to an instance of an object." At the same time, the Log shows: "Indicator

    Comment


      #17
      Hello itrader46,
      I thought the 9 bars I set up are the number of bars since I activated the strategy, but from your post it looks like they are the first historical bars from the start of the chart?
      Correct your script will process historical data first. 9 bars will be starting from bar 0 of historical data, the error you were seeing is happening farther into the processing past that point.

      Regarding the other post and errors, I believe it would be good to clarify some of the information there as the error is the same as this post.

      You have two different errors listed, the first error with EventHandlerBarsUpdate is not something we have a documented solution or case for. This error will require debugging the script further to isolate what code is causing that to happen so we can figure out how to prevent that.

      The other error relating to BarsAgo is a generic error that is produced any time an error occurs based on data usage, this could just be a side effect of the first error. We will need to reduce the test case and find a situation to explore these errors.

      One part I want to clarify in the other post:


      This would mean that you are referencing an object that is null. This is not the same thing as an index out of range error which would be seen if you are referencing some BarsAgo value before the script processes that many bars.

      I suggest debugging the code in your EventHandlerBarsUpdate method by adding prints so you can find out which specific line is creating the "Object reference not set to an instance of an object" error. (The output is hinting that this error is being thrown in your EventHandlerBarsUpdate method.)
      I am not certain this is completely accurate toward your case. If you do not have a "EventHandlerBarsUpdate" method or property specifically coded in your script than this information would not apply. You need to debug your code to isolate the issue however the error is likely being displayed as a result of the situation and syntax used so we would need to know what syntax and situation to further explore this.

      I understand that debugging the script can be tedious however you are currently in the best position to see the problem. It would be best if you can try to further isolate a specific test case where we can explore these errors in more detail. The steps to explore the problem are important here because you noted this is pretty random. If you are able to isolate any steps where we can see this even a couple of times that would be helpful to explore the errors further.



      I look forward to being of further asisstance.
      JesseNinjaTrader Customer Service

      Comment


        #18
        Having said that, I would have expected the code below to print the first bar on chart (Bar 0), but it doesn't, as you can see below the code

        Code:
        protected override void OnBarUpdate()
                {
                    // Make sure this strategy does not execute against historical data
                    if (State == State.Historical)
                        return;
        
                       if (!strategyStart)
                        {
                            Print ("Strategy start bar: " + CurrentBar + " - " + Time[0]);
                            strategyStart = true;
                        }
        Prints

        " Enabling NinjaScript strategy 'ATM2/178978059'
        Strategy start bar: 4480 - 21/10/2019 05:01:00 "

        Whereas this code produces the prints below,

        Code:
        protected override void OnBarUpdate()
                {
                    if (!strategyStart)
                    {
                        Print ("Strategy start bar: " + CurrentBar + " - " + Time[0]);
                        strategyStart = true;
                    }
                    // Make sure this strategy does not execute against historical data
                    if (State == State.Historical)
                        return;
        Prints

        " Strategy start bar: 0 - 15/10/2019 23:01:00
        Enabling NinjaScript strategy 'ATM2/178978060'
        ----------
        Short condition at: 53.74 - Current Bid: 53.74 - R = 1 - Open: 53.76 - Current Bar: 4483 @ 21/10/2019 05:05:00
        Short ATM triggered at: 53.74 Open: 53.76 - Current Bar: 4483 Short order bar = 4483 - 21/10/2019 05:05:00 "

        So basically, it looks like the strategy doesn't execute before the " if (State == State.Historical) return; ", just as I expected.
        Does that mean that, when you say

        Correct your script will process historical data first. 9 bars will be starting from bar 0 of historical data, the error you were seeing is happening farther into the processing past that point.
        that I should never ever get Out of Range exception, unless I set an indicator with a period of over 5000?

        Does the script actually process historical data, coz I thought it wasn't with ATM strategies?

        In that case, the error message is completely ...erroneous to begin with and doesn't offer the slightest bit of hint as to what the issue is.






        Regarding those errors, since I added this code (OnBarUpdate), I've never had them anymore. Do you think that would have anything to do with it, or they are just not coming now and they might return again?

        Code:
                    else if (State == State.DataLoaded)
                    {
                        T31b0           = T3(Closes[0], 5, 3, 0.7);
                        T32b0           = T3(Closes[0], 8, 3, 0.7);
                        T31b1            = T3(Closes[1], 5, 3, 0.7);
                        T32b1            = T3(Closes[1], 8, 3, 0.7);
                        T31b2            = T3(Closes[2], 5, 3, 0.7);
                        T32b2            = T3(Closes[2], 8, 3, 0.7);
                        T31b0.Plots[0].Brush = Brushes.Transparent;
                        T32b0.Plots[0].Brush = Brushes.Transparent;
                        T31b1.Plots[0].Brush = Brushes.Transparent;
                        T32b1.Plots[0].Brush = Brushes.Transparent;
                        T31b2.Plots[0].Brush = Brushes.Transparent;
                        T32b2.Plots[0].Brush = Brushes.Transparent;
                        AddChartIndicator(T31b0);
                        AddChartIndicator(T32b0);
                        AddChartIndicator(T31b1);
                        AddChartIndicator(T32b1);
                        AddChartIndicator(T31b2);
                        AddChartIndicator(T32b2);
        
        protected override void OnBarUpdate()
        {
                    if (CurrentBars[0] < 9 || CurrentBars[1] < 9 || CurrentBars[2] < 9 /*|| CurrentBars[3] < 9*/)
                        return;
        
                    if (T31b0 == null || T32b0 == null || T31b1 == null || T32b1 == null || T31b2 == null || T32b2 == null)
                    {
                        T31b0 = T3(Closes[0], 5, 3, 0.7);
                        T32b0 = T3(Closes[0], 8, 3, 0.7);
                        T31b1 = T3(Closes[1], 5, 3, 0.7);
                        T32b1 = T3(Closes[1], 8, 3, 0.7);
                        T31b2 = T3(Closes[2], 5, 3, 0.7);
                        T32b2 = T3(Closes[2], 8, 3, 0.7);
                    }
        Last edited by itrader46; 11-14-2019, 12:35 PM.

        Comment


          #19
          Hello itrader46,

          Having said that, I would have expected the code below to print the first bar on chart (Bar 0), but it doesn't, as you can see below the code

          Code:
          protected override void OnBarUpdate()
          {
          // Make sure this strategy does not execute against historical data
          if (State == State.Historical)
          return;


          if (!strategyStart)
          {
          Print ("Strategy start bar: " + CurrentBar + " - " + Time[0]);
          strategyStart = true;
          }
          You are using a return statement for historical, this should only print in realtime and would not likely be the first bar unless there was no data present. It could be a high number depending on the amount of historical data you skipped over or a low number if limited data is present.

          Whereas this code produces the prints below,
          Correct because you put the print before the return now. Return will prevent code execution beyond that point or returns out of the method.


          So basically, it looks like the strategy doesn't execute before the " if (State == State.Historical) return; ", just as I expected.
          Does that mean that, when you say

          Correct your script will process historical data first. 9 bars will be starting from bar 0 of historical data, the error you were seeing is happening farther into the processing past that point.

          that I should never ever get Out of Range exception, unless I set an indicator with a period of over 5000?
          In general a script will normally process from bar 0 which is what your script does also. You are additionally returning for historical in your conditions. If realtime happens to be larger than 9 bars the remainder of your conditions checking for data wouldn't apply because they are after the return. If there is limited data and you had less than 9 bars entering realtime for one of the series you may see your later conditions being used checking for data.

          In your specific error it was over bar 5000 for that series so it looks like that was farther into processing however that could be just entering realtime after processing historical. I have no context of the test you did to understand the print in contrast to the logic.This is a situation where debugging and finding the combination of steps used and syntax would be needed to further understand the error better.


          Does the script actually process historical data, coz I thought it wasn't with ATM strategies?
          Yes your script will still have its OnBarUpdate called normally, only managed strategy properties won't apply to ATM's. An ATM works outside your strategy so your strategy can only communicate with it or initiate it, other Managed or Unmanaged strategy properties (position, pnl, performance etc) do not apply to ATMs.


          Regarding those errors, since I added this code (OnBarUpdate), I've never had them anymore. Do you think that would have anything to do with it, or they are just not coming now and they might return again?
          I couldn't say as I don't have the full context of the original problem, if that is what you have identified as the problem and that has solved it we could likely conclude the case. If you wanted to explore that in more detail I would suggest extracting the code required and making it into a specific sample that you can attach so that we could explore that directly.



          Please let me know if I may be of further assistance.

          JesseNinjaTrader Customer Service

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by fitspressoburnfat, Today, 04:25 AM
          0 responses
          2 views
          0 likes
          Last Post fitspressoburnfat  
          Started by Skifree, Today, 03:41 AM
          1 response
          4 views
          0 likes
          Last Post Skifree
          by Skifree
           
          Started by usazencort, Today, 01:16 AM
          0 responses
          1 view
          0 likes
          Last Post usazencort  
          Started by kaywai, 09-01-2023, 08:44 PM
          5 responses
          604 views
          0 likes
          Last Post NinjaTrader_Jason  
          Started by xiinteractive, 04-09-2024, 08:08 AM
          6 responses
          23 views
          0 likes
          Last Post xiinteractive  
          Working...
          X