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

Retrieving historical daily or hour bars, in minute strategy

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

    Retrieving historical daily or hour bars, in minute strategy

    Hi!!!

    I'm begginer writing strategies.

    How can I retrive, for example 3 daily bars ago, starting the strategy today ( or when I've started the strategy), but the strategy working with minute bars?

    Thanks a lot from Argentina!

    #2
    Hello mbcito,

    Thank you for your post.

    If I am understanding correctly, you want to get a value from a daily bar that was three days ago when running the strategy on minute bars, is that correct?

    You could add an additional daily data series to your script and then access the value you need that way. So, for example, if I wanted to set up a simple strategy, where if the current price is greater than the close price of the daily bar from 3 days ago, we enter short, and if it's less than the close price of the daily bar from three days ago, we enter long:

    Code:
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    Description                                    = @"Enter the description for your new custom Strategy here.";
                    Name                                        = "ExampleSecondaryDailySeries";
                    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;
                }
                else if (State == State.Configure)
                {
                    AddDataSeries(Data.BarsPeriodType.Day, 1); // we add our additional data series here
                }
            }
    
            protected override void OnBarUpdate()
            {
                if (BarsInProgress != 0) 
                    return;  // only enter on our primary time frame, we only need the secondary series for the daily data
    
                if (CurrentBars[0] < 1
                || CurrentBars[1] < 3)
                    return; // don't try to process until we have enough bars in each series for our calculations - this means you need at least 3 days of data loaded on the chart for the strategy to function
    
                 // Set 1
                // if the current price (Close[0] is greater than the close of the daily bar from three days ago (Closes[1][3], where 1 is the index of the secondary series and
                // 3 is the index of the bar we want - three days ago.  If we wanted the close of yesterday's daily bar, we'd use 1 there, 2 days ago would be 2 and so on.), then enter short
                if (Close[0] > Closes[1][3]) 
                {
                    EnterShort(Convert.ToInt32(DefaultQuantity), "");
                }
    
                 // Set 2
                // if the current price (Close[0]) is less than the close of the daily bar from three days ago(Closes[1][3], go long
                if (Close[0] < Closes[1][3]) 
                {
                    EnterLong(Convert.ToInt32(DefaultQuantity), "");
                }
    
            }
        }
    You could certainly set this up in the Strategy Builder. You can add the additional data series on the appropriate screen, then use it in the conditions and actions by adjusting the Bars Ago setting and data series setting for the particular value you need to compare.

    Here's a link to our help guide that goes over accessing values from previous bars using brackets:



    Please let us know if we may be of further assistance to you.
    Kate W.NinjaTrader Customer Service

    Comment


      #3
      Hi,
      I understand this part of code provided above:

      if (CurrentBars[0] < 1 || CurrentBars[1] < 3) return; // don't try to process until we have enough bars in each series for our calculations - this means you need at least 3 days of data loaded on the chart for the strategy to function

      However I'm not sure if I clearly understand the comment behind it. I mean this "you need at least 3 days of data loaded on the chart for the strategy to function".
      He has a 1 min timeframe strategy. Does it mean he has to open chart and load at least 3 days of 1 minute data e.g 24 x 60 x 3 = 4320 bars? If not how does he load 3 days of daily timeframe data if he needs a 1 min chart to start strategy on it?
      And what if his strategy needs 6 months back of daily bars? Should he load 24 x 60 x 30 ~= 262 800 bars?

      Thank you!

      Comment


        #4
        Hello WADex,

        Thank you for your note.

        That's correct, if you are running the strategy on a 1 minute primary series, you would need to load at least 3 days of the 1 minute series so that at least 3 daily bars would also be loaded. If the strategy needed six months of daily bars for its calculations, they would need to load six months of the primary series as well.

        Please let us know if we may be of further assistance to you.
        Kate W.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by aussugardefender, Today, 01:07 AM
        0 responses
        3 views
        0 likes
        Last Post aussugardefender  
        Started by pvincent, 06-23-2022, 12:53 PM
        14 responses
        238 views
        0 likes
        Last Post Nyman
        by Nyman
         
        Started by TraderG23, 12-08-2023, 07:56 AM
        9 responses
        384 views
        1 like
        Last Post Gavini
        by Gavini
         
        Started by oviejo, Today, 12:28 AM
        0 responses
        4 views
        0 likes
        Last Post oviejo
        by oviejo
         
        Started by pechtri, 06-22-2023, 02:31 AM
        10 responses
        125 views
        0 likes
        Last Post Leeroy_Jenkins  
        Working...
        X