Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Trying to use weekly + daily data on an hourly strategy

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

    Trying to use weekly + daily data on an hourly strategy

    Given the below code, i get the printout that there is not enough bars for series 0.

    I am trying to take longs on the 60 when weekly and daily are close > open and shorts when weekly and daily are close < open but can't get the data series to load.

    What am i doing wrong?


    HTML Code:
    namespace NinjaTrader.NinjaScript.Strategies
    {
        public class Test123 : Strategy
        {
            private int numberOfTimeframes = 5;
            /*
            ##################################################################################################################
            State Changes
            ##################################################################################################################
            */
            #region State Changes
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    //Strategy defaults
                    Description = @"Multi-timeframe strategy to check if current price is above or below the open of the current candle on added timeframes.";
                    Name = "Test123";
                    Calculate = Calculate.OnPriceChange;
                    EntryHandling = EntryHandling.AllEntries;
                    IsExitOnSessionCloseStrategy = true;
                    ExitOnSessionCloseSeconds = 30;
                    IsFillLimitOnTouch = false;
                    MaximumBarsLookBack = MaximumBarsLookBack.Infinite;
                    OrderFillResolution = OrderFillResolution.Standard;
                    Slippage = 0;
                    StartBehavior = StartBehavior.WaitUntilFlat;
                    TimeInForce = TimeInForce.Gtc;
                    TraceOrders = true;
                    RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
                    StopTargetHandling = StopTargetHandling.PerEntryExecution;
                    BarsRequiredToTrade = 20;
                    IsInstantiatedOnEachOptimizationIteration = true;
                }
                else if (State == State.Configure)
                {
                    AddDataSeries(BarsPeriodType.Tick, 1);
                    AddDataSeries(BarsPeriodType.Minute, 30);
                    AddDataSeries(BarsPeriodType.Week, 1);
                    AddDataSeries(BarsPeriodType.Day, 1);
                    AddDataSeries(BarsPeriodType.Minute, 240);
                }
            }
            #endregion
            /*
            ##################################################################################################################
            On Bar Update
            ##################################################################################################################
            */
            #region On Bar Update
    
    
            protected override void OnBarUpdate()
            {
                // Ensure enough bars for all data series
                for (int i = 0; i <= numberOfTimeframes; i++)
                {
                    if (CurrentBars[i] < 2)
                        Print($"Not enough bars for series {i}");
                        return;
                }
                if (BarsInProgress == 0)
                {
                }
            }
            #endregion
    
        }
    }
    ​

    #2
    Hello spencerduran

    Thank you for your post.

    It would be simpler to use a hard-coded CurrentBars check.



    Additionally, please note that this script will have 6 total Data Series, not 5. There are 5 added series in addition to the primary data series the strategy is being run on.

    Please let us know if you have any further questions.

    Comment


      #3
      I'm fairly certain that's exactly what i'm doing

      how is this different?

      HTML Code:
              protected override void OnBarUpdate()
              {
                  // Ensure enough bars for all data series
                  for (int i = 0; i <= numberOfTimeframes; i++)
                  {
                      if (CurrentBars[i] < 2)
                          Print($"Not enough bars for series {i}");
                          return;
                  }
                  if (BarsInProgress == 0)
                  {
      
                  }
              }​
      VS

      Click image for larger version

Name:	image.png
Views:	98
Size:	30.3 KB
ID:	1322427

      Comment


        #4
        Either way, the code doesn't work. i still get the message from the print statement Not enough bars

        Comment


          #5
          Explicitly defining the CurrentBar check would make it clear what is going on - you have the incorrect number for numberOfTimeframes. You have this defined as 5 instead of as 6.

          To confirm, you are getting the error message "Make sure you have enough bars in the data series you are accessing"?

          You'll need to debug the script to determine which line of code is throwing the error and why.



          You can use prints, place a print every few lines with a new number: Print("1"); etc. That will help you see where the script gets in execution when you hit the error. Once you know what line throws the error its much easier to debug.
          Last edited by NinjaTrader_Gaby; 10-24-2024, 05:31 AM.

          Comment


            #6
            Well the loop starts at index 0, so 5 is actually the correct number of iterations but i have taken the checks out of the loop for the example purpose:

            HTML Code:
            namespace NinjaTrader.NinjaScript.Strategies
            {
                public class Test123 : Strategy
                {
                    /*
                    ##################################################################################################################
                    State Changes
                    ##################################################################################################################
                    */
                    #region State Changes
                    protected override void OnStateChange()
                    {
                        if (State == State.SetDefaults)
                        {
                            //Strategy defaults
                            Description = @"Multi-timeframe strategy to check if current price is above or below the open of the current candle on added timeframes.";
                            Name = "Test123";
                            Calculate = Calculate.OnPriceChange;
                            EntryHandling = EntryHandling.AllEntries;
                            IsExitOnSessionCloseStrategy = true;
                            ExitOnSessionCloseSeconds = 30;
                            IsFillLimitOnTouch = false;
                            MaximumBarsLookBack = MaximumBarsLookBack.Infinite;
                            OrderFillResolution = OrderFillResolution.Standard;
                            Slippage = 0;
                            StartBehavior = StartBehavior.WaitUntilFlat;
                            TimeInForce = TimeInForce.Gtc;
                            TraceOrders = true;
                            RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
                            StopTargetHandling = StopTargetHandling.PerEntryExecution;
                            BarsRequiredToTrade = 20;
                            IsInstantiatedOnEachOptimizationIteration = true;
                        }
                        else if (State == State.Configure)
                        {
                            AddDataSeries(BarsPeriodType.Tick, 1);
                            AddDataSeries(BarsPeriodType.Minute, 30);
                            AddDataSeries(BarsPeriodType.Week, 1);
                            AddDataSeries(BarsPeriodType.Day, 1);
                            AddDataSeries(BarsPeriodType.Minute, 240);
                        }
                    }
                    #endregion
                    /*
                    ##################################################################################################################
                    On Bar Update
                    ##################################################################################################################
                    */
                    #region On Bar Update
                    protected override void OnBarUpdate()
                    {
                        if (CurrentBars[0] < 2)
                        {
                            Print($"Not enough bars for series 0");
                            return;
                        }
                        else if (CurrentBars[1] < 2)
                        {
                            Print($"Not enough bars for series 1");
                            return;
                        }
                        else if (CurrentBars[2] < 2)
                        {
                            Print($"Not enough bars for series 2");
                            return;
                        }
                        else if (CurrentBars[3] < 2)
                        {
                            Print($"Not enough bars for series 3");
                            return;
                        }
                        else if (CurrentBars[4] < 2)
                        {
                            Print($"Not enough bars for series 4");
                            return;
                        }
                        else if (CurrentBars[5] < 2)
                        {
                            Print($"Not enough bars for series 5");
                            return;
                        }
                    }
                    #endregion
                }​
            Logging looks like this when running strategy analyzer - not enough bars for the primary (60 minute) and series 3 (1 week):
            Click image for larger version

Name:	image.png
Views:	94
Size:	5.8 KB
ID:	1322468

            What would be a reason i'm getting this message? is there a different way i'm supposed to add a weekly timeframe to an intraday timeframe strategy?​
            Attached Files

            Comment


              #7
              Hello Spencer,

              You're correct about the loop - that slipped my mind, my apologies.

              Is this the exact code you tested? If it is, I would expect you to hit all these prints statements at some point because the strategy is going to process CurrentBar 1 and CurrentBar 2 for every single series. Your return statement isn't until after the print statement, so the strategy is going to process CurrentBar 1 and 2 for each series, then print "Not enough bars for series n" then return.

              A better test would be:

              Code:
              protected override void OnBarUpdate()
              {
              if (CurrentBars[0] < 2 || CurrentBars[1] < 2 || CurrentBars[2] < 2 || CurrentBars[3] < 2 || CurrentBars[4] < 2 || CurrentBars[5] < 2)
              return;
              
              Print("CurrentBars[0]: " + CurrentBars[0] + "CurrentBars[1]: " + CurrentBars[1] + "CurrentBars[2]: " + CurrentBars[2] + "CurrentBars[3]: " + CurrentBars[3] + "CurrentBars[4]: " + CurrentBars[4] + "CurrentBars[5]: " + CurrentBars[5]);
              }
              Then we can check if the CurrentBar return statement is working, along with the CurrentBar values of each series.

              Comment


                #8
                I've figured out what's going on

                The strategy analyzer doesn't automatically load the required amount of data, it's required to set the start date to a date that encompasses the minimum number of bars for your largest timeframe.

                It would be a nice feature to add "days to load" to the strategy analyzer, the way that strategies themselves have. The current implementation isn't intuitive in starting a backtest on an undesireable range just to ensure there is the required data.

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                0 responses
                559 views
                0 likes
                Last Post Geovanny Suaza  
                Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                0 responses
                324 views
                1 like
                Last Post Geovanny Suaza  
                Started by Mindset, 02-09-2026, 11:44 AM
                0 responses
                101 views
                0 likes
                Last Post Mindset
                by Mindset
                 
                Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                0 responses
                546 views
                1 like
                Last Post Geovanny Suaza  
                Started by RFrosty, 01-28-2026, 06:49 PM
                0 responses
                547 views
                1 like
                Last Post RFrosty
                by RFrosty
                 
                Working...
                X