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

Strategy To Access Historical Data

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

    Strategy To Access Historical Data

    I have a strategy whose actions depend on the week prior. For the sake of simplicity, the strategy needs last week's high. One way of doing this is to actually let the strategy run and manually storing this value in a variable. This works well, especially when back testing years of data but it isn't ideal because at some point you may lose market connection or have to shutdown NT and reboot the computer or other tasks which will force the strategy to lose what was stored in its memory (ie: last weeks high). So I need a way of either going back to historical data (look at last week tick by tick) and recalculate the data I need OR save these variables in a more permanent way when it's running like an external database. Any ideas would be much appreciated.
    Last edited by bc24fl; 12-03-2020, 02:07 PM.

    #2
    Hello bc24fl, thanks for writing in.

    There are a few ways to get this data from the bar series. If you need to capture a specific tick one week ago you would need to identify the bar during the historical data run when the strategy is processing the exact bar you need the price from. Accessing Time[0] will return the time stamp of the bar the strategy is currently processing, so this technique would involve comparing the Time[0] timestamp (Bar times) to the DateTime.Now timestamp (the current PC time), and determining if the currently processing bar is 1 week away from DateTime.Now.

    Another way would be to perform an operation on the current Time[0] DateTime and use GetSlotIndexByTime() e.g.

    Code:
    protected override void OnBarUpdate()
    {
        if(Bars.IsFirstBarOfSession)
        {
            DateTime x = Time[0].AddDays(-7);
            Print(Close[CurrentBar - (int)Math.Truncate(ChartControl.GetSlotIndexByTime (x))] + " " + Time[CurrentBar - (int)Math.Truncate(ChartControl.GetSlotIndexByTime (x))]);
        }
    }
    Another way to get a literal weekly price would be to add a weekly series using AddDataSeries, then access Closes[1][1] when the secondary series has at least 1 bar processed.

    Using external databases to cache the price for later is not something we could support, but it does not have to be too complicated. All you would really need is a .txt file separated by commas and/or newlines (\n)

    We have an example of reading and writing to a txt file here:
    https://ninjatrader.com/support/help..._propertie.htm

    Please let me know if I can provide any further information.
    Chris L.NinjaTrader Customer Service

    Comment


      #3
      Interesting. Does the bar I need historical data from have to be on the chart? More specifically when I load the strategy on live do i need to make sure i have last week's info loading in the chart? Also will this option work with IB see chart (https://ninjatrader.com/support/help...provider.htm)?
      Last edited by bc24fl; 12-03-2020, 06:51 PM.

      Comment


        #4
        Hello bc24fl, thanks for your reply.

        Yes, the bar will need to exist on the chart for any of these ideas to work. Make sure there is at least 1 weeks worth of data on the chart. It will work with Interactive Brokers as well.

        Please let me know if I can assist any further.
        Chris L.NinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_ChrisL View Post
          Hello bc24fl, thanks for your reply.

          Yes, the bar will need to exist on the chart for any of these ideas to work. Make sure there is at least 1 weeks worth of data on the chart. It will work with Interactive Brokers as well.

          Please let me know if I can assist any further.
          The reason I asked about Interactive Brokers is because in that data page all historical tick data is set to no. What if I need to get tick data for last week? Or does the tick data still exist because last week is physically on the chart and therefore not considered historical?

          Comment


            #6
            Hello bc24fl, thanks for your reply.

            Interactive Brokers does not supply historical tick data. You can record the incoming live data as tick data by going to Tools>Options>Market Data>enable "Record Realtime Data As Historical".

            Also, consider a data-only service like Kinetick or IQFeed that will supply historical tick data.

            If you get historical tick data from a third party in the form of a .txt file, that can also be imported into the platform for testing:



            Please let me know if I can provide any further information.
            Chris L.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by LawrenHom, Today, 10:45 PM
            0 responses
            3 views
            0 likes
            Last Post LawrenHom  
            Started by love2code2trade, Yesterday, 01:45 PM
            4 responses
            28 views
            0 likes
            Last Post love2code2trade  
            Started by funk10101, Today, 09:43 PM
            0 responses
            7 views
            0 likes
            Last Post funk10101  
            Started by pkefal, 04-11-2024, 07:39 AM
            11 responses
            37 views
            0 likes
            Last Post jeronymite  
            Started by bill2023, Yesterday, 08:51 AM
            8 responses
            44 views
            0 likes
            Last Post bill2023  
            Working...
            X