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

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

    Multi Time Frame

    I currently have a strategy that works on one data series. See below code:
    HTML 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 InsideBar : Strategy
        {
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    Description                                    = @"Enter the description for your new custom Strategy here.";
                    Name                                        = "InsideBar";
                    Calculate                                    = Calculate.OnBarClose;
                    EntriesPerDirection                            = 1;
                    EntryHandling                                = EntryHandling.AllEntries;
                    IsExitOnSessionCloseStrategy                = true;
                    ExitOnSessionCloseSeconds                    = 30;
                    IsFillLimitOnTouch                            = false;
                    MaximumBarsLookBack                            = MaximumBarsLookBack.TwoHundredFiftySix;
                    OrderFillResolution                            = OrderFillResolution.Standard;
                    Slippage                                    = 0;
                    StartBehavior                                = StartBehavior.WaitUntilFlat;
                    TimeInForce                                    = TimeInForce.Gtc;
                    TraceOrders                                    = false;
                    RealtimeErrorHandling                        = RealtimeErrorHandling.StopCancelClose;
                    StopTargetHandling                            = StopTargetHandling.PerEntryExecution;
                    BarsRequiredToTrade                            = 20;
                    // Disable this property for performance gains in Strategy Analyzer optimizations
                    // See the Help Guide for additional information
                    IsInstantiatedOnEachOptimizationIteration    = true;
                    Barssinceentry = 2;
                }
                else if (State == State.Configure)
                {
                }
            }
    
            protected override void OnBarUpdate()
            {
                if (CurrentBar < BarsRequiredToTrade)
                    return;
                //Long Entry
                if(High[2]>High[1] && Low[2]<Low[1] && Close[0]>High[1] && Low[0]>Low[1])
                    EnterLong();
                if(BarsSinceEntryExecution()>Barssinceentry)
                    ExitLong();
            }
            
            #region Properties
            [Range(1, int.MaxValue), NinjaScriptProperty]
            [Display(ResourceType = typeof(Custom.Resource), Name = "Bars Since Entry", GroupName = "NinjaScriptStrategyParameters", Order = 0)]
            public int Barssinceentry
            { get; set; }
            #endregion
        }
    }
    
    ​
    I now what to only execute this trade when the current weekly bar is green but can't seem to get it to work. What am I doing wrong? See below updated code:
    HTML 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 InsideBar : Strategy
        {
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    Description                                    = @"Enter the description for your new custom Strategy here.";
                    Name                                        = "InsideBar";
                    Calculate                                    = Calculate.OnBarClose;
                    EntriesPerDirection                            = 1;
                    EntryHandling                                = EntryHandling.AllEntries;
                    IsExitOnSessionCloseStrategy                = true;
                    ExitOnSessionCloseSeconds                    = 30;
                    IsFillLimitOnTouch                            = false;
                    MaximumBarsLookBack                            = MaximumBarsLookBack.TwoHundredFiftySix;
                    OrderFillResolution                            = OrderFillResolution.Standard;
                    Slippage                                    = 0;
                    StartBehavior                                = StartBehavior.WaitUntilFlat;
                    TimeInForce                                    = TimeInForce.Gtc;
                    TraceOrders                                    = false;
                    RealtimeErrorHandling                        = RealtimeErrorHandling.StopCancelClose;
                    StopTargetHandling                            = StopTargetHandling.PerEntryExecution;
                    BarsRequiredToTrade                            = 20;
                    // Disable this property for performance gains in Strategy Analyzer optimizations
                    // See the Help Guide for additional information
                    IsInstantiatedOnEachOptimizationIteration    = true;
                    Barssinceentry = 2;
                }
                else if (State == State.Configure)
                {
                    AddDataSeries(BarsPeriodType.Week,1);
                }
            }
    
            protected override void OnBarUpdate()
            {
                if (CurrentBar < BarsRequiredToTrade)
                    return;
                if(Close[1][0]>Open[1][0])
                {
                    //Long Entry
                    if(High[0][2]>High[0][1] && Low[0][2]<Low[0][1] && Close[0][0]>High[0][1] && Low[0][0]>Low[0][1])
                        EnterLong();
                    if(BarsSinceEntryExecution()>Barssinceentry)
                        ExitLong();
                }
            }
            
            #region Properties
            [Range(1, int.MaxValue), NinjaScriptProperty]
            [Display(ResourceType = typeof(Custom.Resource), Name = "Bars Since Entry", GroupName = "NinjaScriptStrategyParameters", Order = 0)]
            public int Barssinceentry
            { get; set; }
            #endregion
        }
    }
    
    ​

    #2
    Hello algospoke,

    Thank you for your post.

    You should use Closes, Highs, and Lows to check these values for a specific bars object. You will get a compile error about applying indexing to a double using Close[1][0] or High[0][1].





    Make sure to also add a CurrentBars check for all added data series.




    In order to better understand how the code is working, it will be necessary to use Print to see how the conditions are evaluating and enable TraceOrders to see if orders are being submitted, ignored, rejected, or cancelled.

    Below is a link to a forum post that demonstrates using prints to understand behavior and includes a link to a video recorded using the Strategy Builder to add prints.


    Enable TraceOrders, print the time of the bar and all values used in the conditions that submit entry orders. Include labels for all values and comparison operators.

    Let me know if you need any assistance creating a print or enabling TraceOrders.

    Save the output from the output window to a text file and provide this with your reply.

    I'll be happy to assist with analyzing the output.​
    Gaby V.NinjaTrader Customer Service

    Comment


      #3
      Gaby,

      Thanks for your help. I was able to fix the code and get it to compile correctly however now I am getting an error on my Control Center saying: "Startegy 'InsideBar' on calling 'OnBarUpdate' method on bar 20: You are accessing an index with a value that is invalid since it is out-of-range.IE accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart".

      I am using a 5 min ES chart display 1 years worth of data. I believe this error is related to not having enough data on the chart to execute the strategy but I think i do have enough. I have more than 20 5min bars and and I have more than 20 1wk bars (secondary data). I am only looking to execute this long trade if weekly candle is wrong. Not sure why I would be getting this error. Below is my code:
      HTML 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 InsideBar : Strategy
          {
              protected override void OnStateChange()
              {
                  if (State == State.SetDefaults)
                  {
                      Description                                    = @"Enter the description for your new custom Strategy here.";
                      Name                                        = "InsideBar";
                      Calculate                                    = Calculate.OnBarClose;
                      EntriesPerDirection                            = 1;
                      EntryHandling                                = EntryHandling.AllEntries;
                      IsExitOnSessionCloseStrategy                = true;
                      ExitOnSessionCloseSeconds                    = 30;
                      IsFillLimitOnTouch                            = false;
                      MaximumBarsLookBack                            = MaximumBarsLookBack.TwoHundredFiftySix;
                      OrderFillResolution                            = OrderFillResolution.Standard;
                      Slippage                                    = 0;
                      StartBehavior                                = StartBehavior.WaitUntilFlat;
                      TimeInForce                                    = TimeInForce.Gtc;
                      TraceOrders                                    = false;
                      RealtimeErrorHandling                        = RealtimeErrorHandling.StopCancelClose;
                      StopTargetHandling                            = StopTargetHandling.PerEntryExecution;
                      BarsRequiredToTrade                            = 20;
                      // Disable this property for performance gains in Strategy Analyzer optimizations
                      // See the Help Guide for additional information
                      IsInstantiatedOnEachOptimizationIteration    = true;
                      Barssinceentry = 2;
                  }
                  else if (State == State.Configure)
                  {
                      AddDataSeries(BarsPeriodType.Week,1);
                  }
              }
      
              protected override void OnBarUpdate()
              {
                  if (CurrentBar < BarsRequiredToTrade)
                      return;
                  if(Closes[1][0]>Opens[1][0])
                  {
                      //Long Entry
                  if(Highs[0][2]>Highs[0][1] && Lows[0][2]<Lows[0][1] && Closes[0][0]>Highs[0][1] && Lows[0][0]>Lows[0][1])
                      EnterLong();
                  if(BarsSinceEntryExecution()>Barssinceentry)
                      ExitLong();
                  }
                  
              }
              
              #region Properties
              [Range(1, int.MaxValue), NinjaScriptProperty]
              [Display(ResourceType = typeof(Custom.Resource), Name = "Bars Since Entry", GroupName = "NinjaScriptStrategyParameters", Order = 0)]
              public int Barssinceentry
              { get; set; }
              #endregion
          }
      }
      
      ​

      Comment


        #4
        Hello algospoke,

        Make sure to also add a CurrentBars check for all added data series. You are currently only checking CurrentBar for the primary data series but are accessing values on the secondary series as well.

        Gaby V.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by smartromain, Today, 02:52 AM
        0 responses
        8 views
        0 likes
        Last Post smartromain  
        Started by Marklhc1988, 04-19-2023, 11:11 AM
        12 responses
        571 views
        1 like
        Last Post victor68133  
        Started by nicthe, Yesterday, 02:58 PM
        1 response
        9 views
        0 likes
        Last Post nicthe
        by nicthe
         
        Started by percy3687, 05-17-2024, 12:28 AM
        3 responses
        30 views
        0 likes
        Last Post percy3687  
        Started by SilverSurfer1, Yesterday, 01:33 PM
        0 responses
        13 views
        0 likes
        Last Post SilverSurfer1  
        Working...
        X