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

How to access historical prices?

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

    How to access historical prices?

    Hello,

    I am developing a strategy with Minute Bars and Day Bars, CalculateOnBarClose = false. The primary bars are minute bars.

    I want to use historical Day Close prices (with supplementary Day Bars). I load 2 or 3 days on the chart, but I have access to the current day bar only.

    How can I access historical day prices?

    Thanks,

    Valentin

    #2
    you can add another timeframe in the initialize section by using

    Code:
    protected override void Initialize() 
    {
         Add(PeriodType.Day, 1);
    in your onbarupdate section you can check for conditions on the main timeframe now by using "if (BarsInProgress == 0)"

    // Checks if OnBarUpdate() is called from an update on the primary Bars
    if (BarsInProgress == 0)
    {
    if (Close[0] > Open[0])
    // Do something
    }
    and further you can now check for conditions on your daily timeframe using "if (BarsInProgress == 1)"

    // Checks if OnBarUpdate() is called from an update on daily Bars
    if (BarsInProgress == 1)
    {
    if (Close[0] > Open[0])
    // Do something
    }
    u can also refer to the daily timeframe as a dataseries by using "BarsArray[1]" whenever u can call a dataseries. so for example if ur reading out a condition of an indicator

    if (BarsInProgress == 0)
    {
    if (CCI(20)[0] > 200 && CCI(BarsArray[1], 20)[0] > 200)
    {
    // Do something
    }
    this would check if on ur main timeframe as well as ur daily timeframe, the CCI would be > 200 whenever there is a new candle on the maintimeframe.

    also make sure that u have enough bars to work with to prevent errors by using
    if (CurrentBars[0] <= BarsRequired || CurrentBars[1] <= BarsRequired)
    return;
    finally you can also access the pricedata directly by using a 1 or a 0 before the timestamp. 1 being ur sec timeframe(daily) and 0 ur maintimeframe. tho the 0 is not really nessesary


    if (BarsInProgress == 0)
    {
    double maintimeframe = Close[0];
    double dailytimefram = Closes[1][0];}

    or

    if (BarsInProgress == 0)
    {
    double maintimeframe = Close[0][0];
    double dailytimefram = Closes[1][0];}
    Last edited by BigRo; 11-10-2015, 07:43 AM.

    Comment


      #3
      Thanks, BigRo, but I need historical day price.
      When CalculateOnBarClose = false, Closes[1][0] refers to today's close.
      I need yesterday's close that is in historical data. If I just use Closes[1][1], I get error message:

      "Error on calling 'OnBarUpdate' method for strategy '...': You are accessing an index with a value that is invalid since it's out of range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart."

      I suppose that there should be 3 day bars when I load 2 days of historical data (the third day bar is today's bar). But NinjaTrader recognizes only today's bar.

      Comment


        #4
        do you have?
        if (CurrentBars[0] <= BarsRequired || CurrentBars[1] <= BarsRequired)
        return;

        added to the code?

        the default setting for this is (i believe) look 20 bars back. but ofc on a minute timeframe(which i understand is your main one) 20 bars back would only be 20 min so not even close to 1 day.

        so you might have to play around with Bars required abit.
        try

        if (CurrentBars[0] <= BarsRequired && CurrentBars[1] <= BarsRequired)
        return;


        instead
        or you could go for.

        protected override void Initialize()
        {
        BarsRequired = 1500;
        }

        and get rid of the code above (that from the onbarupdate section)

        Comment


          #5
          Hello,
          Please add in the following at the beginning of your code under the OnBarUpdate Section.
          Code:
          {
              if (CurrentBars[0] < 1 || CurrentBars[1] <1)
                  return;
          }
          Please see the following post on this message. http://ninjatrader.com/support/forum...ead.php?t=3170
          Cody B.NinjaTrader Customer Service

          Comment


            #6
            Thank you, Cody.

            I understand how to check for enough bars in the data series.

            Now I just run some tests to make sure that I have correct references to bars. My primary bars are Minute bars and my supplementary bars are Day bars with the same instrument.

            I tested $AUDUSD with 7 days loaded. I printed Times[0][0].Date and Times[1][0].Date.

            Times[0][0].Date (primary Minute bars) went from 03.11.2015 to 11.11.2015. But Times[1][0].Date (supplementary Day bars with the same instrument) was always 04.11.2015.

            It seems that supplementary bars are never updated. Maybe it is because they are with the same instrument as primary bars?

            If this is how NinjaTrader works, will I be able to access yesterday's Close price with Closes[1][1]?

            Is it a better approach to use only Minute bars and to get yesterday's Close with Close[1] on the first bar of session?

            Comment


              #7
              I did a similar test with MarketReplay - 1 Minute primary bars and 1 Day supplementary bars with the same instrument. I printed CurrentBars[...] and CurrentBars[1] doesn't update:

              22.07.2015 23:58 ч.
              CurrentBars[0] = 3206
              CurrentBars[1] = 1

              22.07.2015 23:59 ч.
              CurrentBars[0] = 3207
              CurrentBars[1] = 1

              22.07.2015 23:59 ч.
              CurrentBars[0] = 3207
              CurrentBars[1] = 1

              23.07.2015 00:00 ч.
              CurrentBars[0] = 3208
              CurrentBars[1] = 1


              23.07.2015 03:01 ч.
              CurrentBars[0] = 3209
              CurrentBars[1] = 1


              23.07.2015 03:01 ч.
              CurrentBars[0] = 3209
              CurrentBars[1] = 1
              Last edited by Valyo; 11-11-2015, 06:53 PM.

              Comment


                #8
                Hello,
                The secondary data series is updated. There does need to be a new daily bar though for it to appear so. You can see this by playingf through market replay for several days.

                You would be able to get the previous days Close with Closes[1][1].
                If we can be of any other assistance please let us know.
                Cody B.NinjaTrader Customer Service

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by jaybedreamin, Today, 05:56 PM
                0 responses
                3 views
                0 likes
                Last Post jaybedreamin  
                Started by DJ888, 04-16-2024, 06:09 PM
                6 responses
                18 views
                0 likes
                Last Post DJ888
                by DJ888
                 
                Started by Jon17, Today, 04:33 PM
                0 responses
                1 view
                0 likes
                Last Post Jon17
                by Jon17
                 
                Started by Javierw.ok, Today, 04:12 PM
                0 responses
                9 views
                0 likes
                Last Post Javierw.ok  
                Started by timmbbo, Today, 08:59 AM
                2 responses
                10 views
                0 likes
                Last Post bltdavid  
                Working...
                X