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

Multi Time Frame Help

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

    Multi Time Frame Help

    I have read and read and am not sure where the heck i'm going wrong here. I'm trying to run a minute chart that also looks at the same instrument as the chart but is instead in day bars. I've probably gotten something right out of all the combinations I've tried yet if it was it then appears that maybe no day data got loaded. Just not sure. Any advice would be much appreciated.

    Also a few questions related.
    If the primary chart is minute bars and part of the code looks at Day data say SMA(30 days bars) will it poll for 30 days worth of day bars from Kinetick or is it expecting that 30 days of minute data be preloaded on strategy run?

    Should I expect any differences in say SMA(3 day bars) versus SMA(1170 minute bars)? "390 minutes a day x 3 days = 1170"

    Code:
    #region Using declarations
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Xml.Serialization;
    using NinjaTrader.Cbi;
    using NinjaTrader.Gui;
    using NinjaTrader.Gui.Chart;
    using NinjaTrader.Gui.SuperDom;
    using NinjaTrader.Gui.Tools;
    using NinjaTrader.Data;
    using NinjaTrader.NinjaScript;
    using NinjaTrader.Core.FloatingPoint;
    using NinjaTrader.NinjaScript.Indicators;
    using NinjaTrader.NinjaScript.DrawingTools;
    #endregion
    
    //This namespace holds Strategies in this folder and is required. Do not change it. 
    namespace NinjaTrader.NinjaScript.Strategies
    {
        public class TestBed : Strategy
        {
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    Description                                    = @"Enter the description for your new custom Strategy here.";
                    Name                                        = "TestBed";
                    Calculate                                    = Calculate.OnBarClose;
                    EntriesPerDirection                            = 1;
                    EntryHandling                                = EntryHandling.AllEntries;
                    IsExitOnSessionCloseStrategy                = false;
                    ExitOnSessionCloseSeconds                    = 30;
                    IsFillLimitOnTouch                            = false;
                    MaximumBarsLookBack                            = MaximumBarsLookBack.Infinite;
                    OrderFillResolution                            = OrderFillResolution.Standard;
                    Slippage                                    = 0;
                    TimeInForce                                    = TimeInForce.Gtc;
                    TraceOrders                                    = false;
                    RealtimeErrorHandling                        = RealtimeErrorHandling.IgnoreAllErrors;
                    StopTargetHandling                            = StopTargetHandling.PerEntryExecution;
                    BarsRequiredToTrade                            = 0;
                    DaysToLoad                                    = 10;
                    // Disable this property for performance gains in Strategy Analyzer optimizations
                    // See the Help Guide for additional information
                    IsInstantiatedOnEachOptimizationIteration    = true;
                }
                else if (State == State.Configure)
                {
                    AddDataSeries(BarsPeriodType.Day, 1);
                }
                else if (State == State.DataLoaded)
                {
    //                AddDataSeries(BarsPeriodType.Day, 1);
                }
            }
    
            protected override void OnBarUpdate()
            {
    //            if (CurrentBars[0] <= BarsRequiredToTrade )
    //            Print("Not Enough Bars");
    //            return; 
    
                //Print(SMA(Close, 1170)[0]);
                //Print(SMA(Close, 3)[0]);
    
    //            Print(Closes[0][0]);
    //            Print(" : ");
    //            Print(Closes[1][0]);
    
                Print(CurrentBars[0] + " : " + Closes[0][0]);
                Print("---");
                Print(CurrentBars[1] + " : " + Closes[1][0]);
                Print("###########");
    
    
    
    
    
    
    
    
            }
        }
    }

    Thanks in advance,

    #2
    Hello antrux,

    Thanks for your post.

    Data will be loaded going back as far as described in the Data Series window when configured on a chart or as specified when configuring the strategy in the Strategies tab of the Control Center.

    If you have a 30 period SMA in your script that uses daily data and you apply your NinjaScript to a 1 minute chart, the chart will need to load 30 days of data or process 30 days of data before the SMA can calculate. This will have to include days filtered by a Trading Hours template.

    In your script, although commented, I see that you have AddDataSeries in State.DataLoaded. When we should only call AddDataSeries in State.Configure.

    For the thread's reference, I've included a link to our Multi Time Frame and Instruments documentation that can be used as a complete reference for creating multi series NinjaScripts. Help guide information is publicly available.

    Multi Time Frame and Instruments - https://ninjatrader.com/support/help...nstruments.htm

    Please let us know if you have any additional questions.
    Last edited by NinjaTrader_Jim; 12-12-2018, 12:11 PM.
    JimNinjaTrader Customer Service

    Comment


      #3
      AddDataSeries is in the state.configure section in the code above.

      I removed the check for CurrentBars1 just to see anything work. I can tell the chart to load 10 days of minute data before starting so it should have 10 days of minute bars and 10 day bars. I'm using Print to just see the current minute bar as well as the current Day bar values but can never see a value for the day bar.

      This is why I've posted I can't get any day data to be produced.

      Comment


        #4
        Hello antrux,

        I saw CurrentBars[1] and somehow read CurrentBars[1][1] this morning. This would be the case for Closes[1][0] for that daily bar. Sorry for any confusion, I grabbed a cup of coffee and updated my post for clarity. I also added a note regarding Trading Hours templates.

        Here is a demo and test code showing 30 days of daily data printing after reloading the NinjaScript on the chart. As we can see, there are 20 bars of Daily data since the Trading Hours template limits these days.

        Demo - https://drive.google.com/file/d/1Zmn...w?usp=drivesdk

        Code:
        protected override void OnStateChange()
        {
            if (State == State.SetDefaults)
            {
                Description                                    = @"Enter the description for your new custom Indicator here.";
                Name                                        = "TestDailyBars";
                Calculate                                    = Calculate.OnBarClose;
            }
            else if (State == State.Configure)
            {
                AddDataSeries(Data.BarsPeriodType.Day, 1);
            }
        }
        
        protected override void OnBarUpdate()
        {
            if(BarsInProgress == 1)
                Print(CurrentBars[1] + " " + Times[1][0]);
        }
        Please let me know if I can be of further assistance.
        Last edited by NinjaTrader_Jim; 12-13-2018, 09:03 AM.
        JimNinjaTrader Customer Service

        Comment


          #5
          Jim,
          From the original code posted if you compile and run it does it output properly?
          See attached screenshot.

          Comment


            #6
            Hello antrux,

            Your script is referencing the daily bar (Closes[1][0]) when the minute bar is processing, before the daily bar has been processed and would be available for reference.

            You will need to add a CurrentBar check for the daily bar if (CurrentBars[1] < 1) return; so you are not referencing the daily bar on the minute bar, when the daily bar is not yet processed.

            Let me know if you have any additional questions.
            JimNinjaTrader Customer Service

            Comment


              #7
              Since a strategy can't request Day bars from the data provider when on a minute chart, for some reason, that does make sense now. If the Day bar is created based on only the prior Minute bar's data then using US Equities RTH after say 390 minute bars one new Day bar should be available yes?

              In testing this it doesn't seem to pan out though. See screenshot below.


              Code:
              //This namespace holds Strategies in this folder and is required. Do not change it. 
              namespace NinjaTrader.NinjaScript.Strategies
              {
                  public class TestBed : Strategy
                  {
                      protected override void OnStateChange()
                      {
                          if (State == State.SetDefaults)
                          {
                              Description                                    = @"Enter the description for your new custom Strategy here.";
                              Name                                        = "TestBed";
                              Calculate                                    = Calculate.OnBarClose;
                              EntriesPerDirection                            = 1;
                              EntryHandling                                = EntryHandling.AllEntries;
                              IsExitOnSessionCloseStrategy                = false;
                              ExitOnSessionCloseSeconds                    = 30;
                              IsFillLimitOnTouch                            = false;
                              MaximumBarsLookBack                            = MaximumBarsLookBack.Infinite;
                              OrderFillResolution                            = OrderFillResolution.Standard;
                              Slippage                                    = 0;
                              TimeInForce                                    = TimeInForce.Gtc;
                              TraceOrders                                    = false;
                              RealtimeErrorHandling                        = RealtimeErrorHandling.IgnoreAllErrors;
                              StopTargetHandling                            = StopTargetHandling.PerEntryExecution;
                              BarsRequiredToTrade                            = 0;
                              DaysToLoad                                    = 10;
                              // Disable this property for performance gains in Strategy Analyzer optimizations
                              // See the Help Guide for additional information
                              IsInstantiatedOnEachOptimizationIteration    = true;
                          }
                          else if (State == State.Configure)
                          {
                              AddDataSeries(BarsPeriodType.Day, 1);
                          }
                          else if (State == State.DataLoaded)
                          {
              //                AddDataSeries(BarsPeriodType.Day, 1);
                          }
                      }
              
                      protected override void OnBarUpdate()
                      {
                          // Should process 10 days of minute bars ~3,900
                          Print("Minute " + CurrentBars[0] + " : " + Closes[0][0]);
                          Print("---");
                          // Should return for the first 1 day of minute bars ~390
                          if (CurrentBars[1] < 1 )
                          Print("Days - Not Enough Bars");
                          return; 
                          // Should print day bar data after ~390 minute bars
                          Print("Day " + " : " + Closes[1][0]);
                          Print("###########");
              
                      }
                  }
              }

              Comment


                #8
                Hello antrux,

                I've created a demonstration reviewing your code and how we can print out each iterating bar and see how OnBarUpdate unrolls that data.

                Demo - https://drive.google.com/file/d/14Rt...w?usp=drivesdk

                Let me know if there is anything else I can do to help.
                Attached Files
                JimNinjaTrader Customer Service

                Comment


                  #9
                  Ok!
                  Damn it Jim I'm not a great coder and forgot those brackets. I blame it on autocorrect. <grin>
                  Thank you.
                  This makes perfect sense now.

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by RookieTrader, Today, 09:37 AM
                  3 responses
                  15 views
                  0 likes
                  Last Post NinjaTrader_ChelseaB  
                  Started by kulwinder73, Today, 10:31 AM
                  0 responses
                  5 views
                  0 likes
                  Last Post kulwinder73  
                  Started by terofs, Yesterday, 04:18 PM
                  1 response
                  24 views
                  0 likes
                  Last Post terofs
                  by terofs
                   
                  Started by CommonWhale, Today, 09:55 AM
                  1 response
                  4 views
                  0 likes
                  Last Post NinjaTrader_Erick  
                  Started by Gerik, Today, 09:40 AM
                  2 responses
                  7 views
                  0 likes
                  Last Post Gerik
                  by Gerik
                   
                  Working...
                  X