Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Basic Multi Time Frame

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

    Basic Multi Time Frame

    Hi,

    New to Ninja Trader, trying to do something I thought would be pretty simple but it's not going quite as smoothly as I was hoping.
    Any help would be appreciated :-)

    I'm trying to create a pretty simple multi time frame strategy.
    As you can see in the code below I'm adding 3 Data Series: 1 Second, 30 Minutes and 1 Day
    When I run the strategy it seems to ignore the 1 Second (1 Second isn't written to output), I've also tried 1 Tick (which is actually what I want) but that doesn't work either.
    When I change that 1 second to something like 1 Minute it works as expected.
    What am I missing here?

    Another oddity (or just lack of understanding on my part), the code below only works when I set the primary Data Series (via the UI) to 1 Minute or above, if I set it to something like 30 seconds nothing is printed to the output window at all and I don't quite understand why.

    Also a couple questions about the SMA in the code.
    1. It looks like the Time Frame I choose in the UI must be 200 Days or above, even though I don't really need that level of detail for the primary Data Series (I actually ignore it), I just need it to preload for the 1 Day Data Series so I can calculate the SMA.
    Am I approaching this correctly?

    2. I noticed the SMA is only updated on bar close even though in the State.SetDefaults I've set Calculate to OnEachTick. (I've also enabled "Show Tick Replay" in options)
    Again, what am I not understanding here?

    Code:
    public class MyMultiTimeFrame : Strategy
    {
    private int _dailyBarsRequired = 201;
    private SMA _daily200SMA;
    public bool DebugMode { get; set; }
    
    protected override void OnStateChange()
    {
    
    if (State == State.SetDefaults)
    {
    Description = @"Enter the description for your new custom Strategy here.";
    Name = "MyMultiTimeFrame";
    Calculate = Calculate.OnEachTick;
    ExitOnSessionCloseSeconds = 30;
    IsFillLimitOnTouch = false;
    MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
    OrderFillResolution = OrderFillResolution.Standard;
    TimeInForce = TimeInForce.Gtc;
    //TraceOrders = true;
    //RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
    }
    
    else if (State == State.Configure)
    {
    AddDataSeries(Data.BarsPeriodType.Second, 1);
    AddDataSeries(Data.BarsPeriodType.Minute, 30);
    AddDataSeries(Data.BarsPeriodType.Day, 1);
    }
    else if (State == State.DataLoaded)
    {
    _daily200SMA = SMA(Closes[2], 200);
    }
    }
    
    
    protected override void OnBarUpdate()
    {
    // Return if not enough daily bars or current bar is the primary series
    if (CurrentBars[3] < _dailyBarsRequired || BarsInProgress == 0)
    return;
    
    var sb = new StringBuilder();
    sb.AppendLine(string.Format("BarsInProgeess: {0}", BarsInProgress));
    
    // Enter on 1 Second Bar
    if (BarsInProgress == 1)
    {
    sb.AppendLine(string.Format(" 1 Second Bar"));
    sb.AppendLine(string.Format(" Time: {0}", Time[0]));
    sb.AppendLine(string.Format(" Close: {0}", Close[0]));
    sb.AppendLine(string.Format(" Day 200 SMA: {0}", _daily200SMA[0]));
    }
    
    // Enter on 60 Minute Bar
    if(BarsInProgress == 2)
    {
    sb.AppendLine(string.Format(" 30 Minute Bar"));
    sb.AppendLine(string.Format(" Time: {0}", Time[0]));
    sb.AppendLine(string.Format(" Close: {0}", Close[0]));
    sb.AppendLine(string.Format(" Day 200 SMA: {0}", _daily200SMA[0]));
    }
    
    // Enter on Daily Bar
    if (BarsInProgress == 3)
    {
    sb.AppendLine(string.Format(" Daily Bar"));
    sb.AppendLine(string.Format(" Time: {0}", Time[0]));
    sb.AppendLine(string.Format(" Close: {0}", Close[0]));
    sb.AppendLine(string.Format(" Day 200 SMA: {0}", _daily200SMA[0]));
    
    }
    
    DebugOutput(sb.ToString());
    }
    
    private void DebugOutput(object obj)
    {
    if (!DebugMode)
    return;
    
    Print(obj);
    Print(string.Empty);
    }
    }
    Thanks again for any help!

    #2
    Hello BarakSel,

    Add a simple print above (outside) of all conditions in OnBarUpdate().

    Print(string.Format("{0} | BarsInProgress: {1}", Time[0], BarsInProgress));
    https://ninjatrader.com/support/foru...121#post791121

    Save the output to a text file and include this with your reply.

    I am happy to assist with analyzing the output.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Hi Chelsea,

      I've removed my prints and left only the one you've indicated, as you can see in the output below DataSeries[1] (this is the 1 second series) is apparently never called.
      This happens with Ticks and Seconds, Minutes and above seem to work as expected.

      Code:
      1/13/2021 5:00:00 PM | BarsInProgress: 2
      1/13/2021 5:30:00 PM | BarsInProgress: 2
      1/13/2021 6:00:00 PM | BarsInProgress: 2
      1/13/2021 6:30:00 PM | BarsInProgress: 2
      1/13/2021 7:00:00 PM | BarsInProgress: 2
      1/13/2021 7:30:00 PM | BarsInProgress: 2
      1/13/2021 8:00:00 PM | BarsInProgress: 2
      1/13/2021 8:30:00 PM | BarsInProgress: 2
      1/13/2021 9:00:00 PM | BarsInProgress: 2
      1/13/2021 9:30:00 PM | BarsInProgress: 2
      1/13/2021 10:00:00 PM | BarsInProgress: 2
      1/13/2021 10:30:00 PM | BarsInProgress: 2
      1/13/2021 11:00:00 PM | BarsInProgress: 0
      1/13/2021 11:00:00 PM | BarsInProgress: 2
      1/13/2021 11:00:00 PM | BarsInProgress: 3
      1/14/2021 5:00:00 PM | BarsInProgress: 2
      1/14/2021 5:30:00 PM | BarsInProgress: 2
      1/14/2021 6:00:00 PM | BarsInProgress: 2
      1/14/2021 6:30:00 PM | BarsInProgress: 2
      1/14/2021 7:00:00 PM | BarsInProgress: 2
      1/14/2021 7:30:00 PM | BarsInProgress: 2
      1/14/2021 8:00:00 PM | BarsInProgress: 2
      1/14/2021 8:30:00 PM | BarsInProgress: 2
      1/14/2021 9:00:00 PM | BarsInProgress: 2
      1/14/2021 9:30:00 PM | BarsInProgress: 2
      1/14/2021 10:00:00 PM | BarsInProgress: 2
      1/14/2021 10:30:00 PM | BarsInProgress: 2
      1/14/2021 11:00:00 PM | BarsInProgress: 0
      1/14/2021 11:00:00 PM | BarsInProgress: 2
      1/14/2021 11:00:00 PM | BarsInProgress: 3
      1/15/2021 5:00:00 PM | BarsInProgress: 2
      1/15/2021 5:30:00 PM | BarsInProgress: 2
      1/15/2021 6:00:00 PM | BarsInProgress: 2
      1/15/2021 6:30:00 PM | BarsInProgress: 2
      1/15/2021 7:00:00 PM | BarsInProgress: 2
      1/15/2021 7:30:00 PM | BarsInProgress: 2
      1/15/2021 8:00:00 PM | BarsInProgress: 2
      1/15/2021 8:30:00 PM | BarsInProgress: 2
      1/15/2021 9:00:00 PM | BarsInProgress: 2
      1/15/2021 9:30:00 PM | BarsInProgress: 2
      1/15/2021 10:00:00 PM | BarsInProgress: 2
      1/15/2021 10:30:00 PM | BarsInProgress: 2
      1/15/2021 11:00:00 PM | BarsInProgress: 0
      1/15/2021 11:00:00 PM | BarsInProgress: 2
      1/15/2021 11:00:00 PM | BarsInProgress: 3
      1/19/2021 5:00:00 PM | BarsInProgress: 2
      1/19/2021 5:30:00 PM | BarsInProgress: 2
      1/19/2021 6:00:00 PM | BarsInProgress: 2
      1/19/2021 6:30:00 PM | BarsInProgress: 2
      1/19/2021 7:00:00 PM | BarsInProgress: 2
      1/19/2021 7:30:00 PM | BarsInProgress: 2
      1/19/2021 8:00:00 PM | BarsInProgress: 2
      1/19/2021 8:30:00 PM | BarsInProgress: 2
      1/19/2021 9:00:00 PM | BarsInProgress: 2
      1/19/2021 9:30:00 PM | BarsInProgress: 2
      1/19/2021 10:00:00 PM | BarsInProgress: 2
      1/19/2021 10:30:00 PM | BarsInProgress: 2
      1/19/2021 11:00:00 PM | BarsInProgress: 0
      1/19/2021 11:00:00 PM | BarsInProgress: 2
      1/19/2021 11:00:00 PM | BarsInProgress: 3
      1/20/2021 5:00:00 PM | BarsInProgress: 2
      1/20/2021 5:30:00 PM | BarsInProgress: 2
      1/20/2021 6:00:00 PM | BarsInProgress: 2
      1/20/2021 6:30:00 PM | BarsInProgress: 2
      1/20/2021 7:00:00 PM | BarsInProgress: 2
      1/20/2021 7:30:00 PM | BarsInProgress: 2
      1/20/2021 8:00:00 PM | BarsInProgress: 2
      1/20/2021 8:30:00 PM | BarsInProgress: 2
      1/20/2021 9:00:00 PM | BarsInProgress: 2
      1/20/2021 9:30:00 PM | BarsInProgress: 2
      1/20/2021 10:00:00 PM | BarsInProgress: 2
      1/20/2021 10:30:00 PM | BarsInProgress: 2
      1/20/2021 11:00:00 PM | BarsInProgress: 0
      1/20/2021 11:00:00 PM | BarsInProgress: 2
      1/20/2021 11:00:00 PM | BarsInProgress: 3
      1/21/2021 5:00:00 PM | BarsInProgress: 2
      1/21/2021 5:30:00 PM | BarsInProgress: 2
      1/21/2021 6:00:00 PM | BarsInProgress: 2
      1/21/2021 6:30:00 PM | BarsInProgress: 2
      1/21/2021 7:00:00 PM | BarsInProgress: 2
      1/21/2021 7:30:00 PM | BarsInProgress: 2
      1/21/2021 8:00:00 PM | BarsInProgress: 2
      1/21/2021 8:30:00 PM | BarsInProgress: 2
      1/21/2021 9:00:00 PM | BarsInProgress: 2
      1/21/2021 9:30:00 PM | BarsInProgress: 2
      1/21/2021 10:00:00 PM | BarsInProgress: 2
      1/21/2021 10:30:00 PM | BarsInProgress: 2
      1/21/2021 11:00:00 PM | BarsInProgress: 0
      1/21/2021 11:00:00 PM | BarsInProgress: 2
      1/21/2021 11:00:00 PM | BarsInProgress: 3
      1/22/2021 5:00:00 PM | BarsInProgress: 2
      1/22/2021 5:30:00 PM | BarsInProgress: 2
      1/22/2021 6:00:00 PM | BarsInProgress: 2
      1/22/2021 6:30:00 PM | BarsInProgress: 2
      1/22/2021 7:00:00 PM | BarsInProgress: 2
      1/22/2021 7:30:00 PM | BarsInProgress: 2
      1/22/2021 8:00:00 PM | BarsInProgress: 2
      1/22/2021 8:30:00 PM | BarsInProgress: 2
      1/22/2021 9:00:00 PM | BarsInProgress: 2
      1/22/2021 9:30:00 PM | BarsInProgress: 2
      1/22/2021 10:00:00 PM | BarsInProgress: 2
      1/22/2021 10:30:00 PM | BarsInProgress: 2
      1/22/2021 11:00:00 PM | BarsInProgress: 0
      1/22/2021 11:00:00 PM | BarsInProgress: 2
      1/22/2021 11:00:00 PM | BarsInProgress: 3
      Thanks for helping :-)

      Comment


        #4
        Hello BarakSel,

        Does your connection support historical Tick data?
        https://ninjatrader.com/support/help...y_provider.htm

        Can you open a new chart to 1 second?
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Hi Chelsea,

          I'm using Interative Brokers as my provider which explains the problem.
          However, IB actually does support Historical Tick Data as you can see here:
          https://interactivebrokers.github.io...and_sales.html

          Also, I can open a 5 second chart in TWS while I can't do it in Ninja Trader.
          Since IB does support historical tick data does that mean it isn't supported in your integration yet and there are plans to add support? :-)

          Also, does Real-Time Data column in the link you sent me to means I will have tick data when trading live (paper included?) I just don't have that data when back-testing?

          Can you suggest a workaround / alternative?

          Comment


            #6
            Hello BarakSel,

            May I confirm you have reviewed the data by provider page in the help guide I have linked you?
            This will tell you that NinjaTrader is not able to load historical tick data from Interactive Brokers.
            This means tick bars, second bars, renko bars, range bars, or any other bar type that is built from tick data will not load.

            If real-time data is received, this can be used for a script or a chart.

            A workaround is to subscribe to a data feed that provides historical tick data such as Kinetic. Please see the data by provider page in the help guide.
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Hi Chelsea,

              I've reviewed the page and understand I can use a service like Kinetic as my data provider while using IB as my broker simultaneously.

              I am curious why IB historical tick data is not supported though, since IB does support it via TWS or their API (as I've linked before), and also curious if there are any plans to add that support to IB.

              Thanks,
              Barak.

              Comment


                #8
                Hello Barak,

                This would be a question for Interactive Brokers when they made the legacy API. This was a decision IB made, to not supply historical tick data to the API connected applications.

                I cannot discuss future plans for the NinjaTrader Platform.
                Chelsea B.NinjaTrader Customer Service

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by JakeOfSpades, Yesterday, 08:36 AM
                5 responses
                47 views
                0 likes
                Last Post hedgeplay  
                Started by MarkF60, Yesterday, 05:25 AM
                1 response
                14 views
                0 likes
                Last Post NinjaTrader_ChelseaB  
                Started by soon123, 03-05-2021, 10:15 PM
                2 responses
                12 views
                0 likes
                Last Post soon123
                by soon123
                 
                Started by soon123, 03-05-2021, 09:48 PM
                3 responses
                9 views
                0 likes
                Last Post soon123
                by soon123
                 
                Started by Amaria77, 03-05-2021, 08:21 PM
                2 responses
                15 views
                0 likes
                Last Post NinjaTrader_ChelseaB  
                Working...
                X