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

Adding a second data series one bar ahead of time

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

    Adding a second data series one bar ahead of time

    Dear Forum,


    I am backtesting a strategy running on a historical daily chart.
    In OnBarUpdate(), I want to perform calculations using the open of the next day.

    Is this possible and what would be a good way to achieve this?
    Do I need to prepare a new Series<double> in State.DataLoaded that copies the current data series, but shifts the index by one?
    It would be great if someone could give me a hint.


    Thanks a lot!

    #2
    Hello portislander,

    Thanks for your post.

    If you would like to perform calculations on the opening of the next day, you could do so by adding a daily data series and using Calculate.OnEachTick or Calculate.OnPriceChange. The opening tick can be found on the BarsInProgress for the daily data series with IsFirstTickOfBar.

    Here's an example:

    Code:
    protected override void OnStateChange()
    {
        if (State == State.SetDefaults)
        {
            Name                                        = "OnNextDayExample";
            Calculate                                    = Calculate.OnPriceChange;
        }
        else if (State == State.Configure)
        {
            AddDataSeries(BarsPeriodType.Day, 1);
        }
    }
    
    protected override void OnBarUpdate()
    {
        if (BarsInProgress == 1)
        {
            if(IsFirstTickOfBar)
            {
                double openingTickOfNewDay = Open[0];
                Print(String.Format("{0} {1}", Time[0], openingTickOfNewDay));
            }
        }
    }
    I've included documentation links for the thread's reference.

    IsFirstTickOfBar - https://ninjatrader.com/support/help...ttickofbar.htm

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

    Please let us know if you have any additional questions.
    JimNinjaTrader Customer Service

    Comment


      #3
      Thank you very much for your detailed response.
      And sorry, I should have been clearer and explained this better.

      What I need to do is this:
      The calculation needs to take place on day X. But that calculation needs data from both day X and day X+1.
      I realize that in real life you would need a time machine for that since day X+1 has not happened yet.
      I am trying to circumvent certain limitations for backtesting with historical EOD data.

      Let's say on day X my strategy suggest a buy - but only if we gap up the next morning of day X+1.
      If I do "EnterLong" on day X, the backtest will enter long on day X+1 independent of a gap up or gap down since it's end of day data.
      If I wait and check the next daily bar of X+1 for a gap up and then "enter long", the backtest will enter one day too late (X+2) (again, since it's end of day data).

      These were my results so far. Or am I making a more general mistake?
      My hope was that since we in October 2018 already know all mornings of Q1-Q3 2018, I could store them in a variable and use them in a calculation that would not be possible in a live situation.

      Comment


        #4
        Hello portislander,

        We don't have any supported way to reference data in the future. If you wanted to accomplish this sort of task, you would have to parse the data files that are stored in the NinjaTrader 8/db/ folder. You could learn more about data file formats in the link below, however this sort of approach is not something that we would support.

        Data formats - https://ninjatrader.com/support/help...AndDataFormats

        I'll leave this thread open for any community members to share their experiences if they have tried this.

        If there is something else that we can assist with, please don't hesitate to open a new thread.
        JimNinjaTrader Customer Service

        Comment


          #5
          Thank you very much for the hint, Jim. Creating and parsing text data is an approach I will look into as well.
          Let me ask one more question.

          The way I intended to solve my problem originally looked - in my mind - something like this:

          1) Iterate once over all historical bars before OnBarUpdate(), after State.DataLoaded
          2) In this loop, save the date and the open price to a variable (e.g. a hashmap/dictionary)
          3) In OnBarUpdate(), use that variable for a calculation on the "future"

          2) and 3) I can probably do, but 1) gives me trouble.
          Is it possible to iterate over all bars before OnBarUpdate() and extract out date and price data?

          Comment


            #6
            Hello portislander,

            It would be possible to loop through the historical bars and get that information after State.DataLoaded has been reached.

            Code:
            for (int i = 0; i < Bars.Count; i++)
                Print(Bars.GetClose(i));
            Other methods like GetTime() can be referenced with the Bars documentation.

            Bars - https://ninjatrader.com/support/help...en-us/bars.htm
            JimNinjaTrader Customer Service

            Comment


              #7
              Thank you, Jim, that's very helpful. I think I can solve my problem with this method.

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by mmckinnm, Today, 01:34 PM
              3 responses
              5 views
              0 likes
              Last Post NinjaTrader_ChelseaB  
              Started by f.saeidi, Today, 01:32 PM
              2 responses
              4 views
              0 likes
              Last Post f.saeidi  
              Started by alifarahani, 04-19-2024, 09:40 AM
              9 responses
              55 views
              0 likes
              Last Post NinjaTrader_Jesse  
              Started by Conceptzx, 10-11-2022, 06:38 AM
              3 responses
              60 views
              0 likes
              Last Post NinjaTrader_SeanH  
              Started by traderqz, Today, 12:06 AM
              9 responses
              16 views
              0 likes
              Last Post NinjaTrader_Gaby  
              Working...
              X