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

get time of x bars ago

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

    get time of x bars ago

    Hi,
    I have a set of integer indexes that I would like to use to get the time of the corresponding bars.
    How do I get the time associated with 3 bars ago, 4 bars ago, etc. ?

    Thank you in advance

    #2
    Hello biegea,

    Thanks for your post.

    You can check the timestamp of the last three bars by specifying the barsAgo index into the Time Series. For example, Time[0], Time[1], Time[2], Time[3], Time[4].

    Below is documentation on Time Series and working with Series objects.

    Time Series - https://ninjatrader.com/support/helpGuides/nt7/time.htm

    Working with Price Data - https://ninjatrader.com/support/help...rice_data2.htm

    I also recommend using the Strategy Wizard to generate code if you are having difficulty writing the syntax.

    Please let us know if we can be of further assistance.
    Attached Files
    JimNinjaTrader Customer Service

    Comment


      #3
      Originally posted by biegea View Post
      Hi,
      I have a set of integer indexes that I would like to use to get the time of the corresponding bars.
      How do I get the time associated with 3 bars ago, 4 bars ago, etc. ?
      The kind of integer index you're referring to is actually called the "BarsAgo" index.

      It's super simple to use.
      The values of any DataSeries object can be accessed backwards in time via its BarsAgo index.

      Close[0] is always the close price of the most recently closed bar, because the BarsAgo index is 0.
      Close[1] is always the close price of the closed bar 1 bar ago.
      Close[2] is always the close price of the closed bar 2 bars ago.
      Close[n] is always the close price of the closed bar n bars ago.

      You access the nth value by enclosing the index in square brackets -- it looks like you're
      accessing an array, but it's different (it's actually a feature of C# known as indexers, which allows
      you to access elements of the class using array-like notation.)

      So, the answer to your question requires the Time DataSeries, so you would specify,
      Time[0] is always the closing time of the most recently closed bar,.
      Time[1] is always the closing time of the closed bar 1 bar ago.
      Time[2] is always the closing time of the closed bar 2 bars ago.
      Time[n] is always the closing time of the closed bar n bars ago.

      A DataSeries is a special kind of data structure, similar to but not quite the same as an array.
      A DataSeries starts references from the last element, and the youngest element is at index 0.
      An array starts references from the beginning element, and the oldest element is at index 0.

      This means the actual physical candlestick that Time[n] is referencing is updated or shifted each
      time a new bar is completed -- this is by design. The nth bar ago is always in relationship to the
      current bar being developed -- so as these new developing bars are completed (and an even newer
      current bar automatically starts developing) the completed bars become known as historical
      bars.

      Thus, technically, the BarsAgo index is actually being used to access values of historical bars.

      Did you see where I said "any DataSeries object"?

      Well, that includes indicators, too.
      Indicators maintain a recording of their historical values, bar by bar, because they, too, are a DataSeries.

      So, whereas High[5] is the high price of the bar at 5 bars ago, we can also "index" the
      indicator itself, like this, EMA(14)[5], which is the value of EMA(14) from 5 bars ago.

      Make sense?

      [EDIT: Btw, in NT7 the term was "DataSeries" which contained double values only. Other
      classes, such as IntSeries and BoolSeries were used for other types. In NT8, they changed to a
      generic class called "Series" which accepts any type via the type placeholder T, such as Series<T>,
      as in Series<double>, Series<int>, Series<bool>, etc ...]
      Last edited by bltdavid; 09-10-2019, 05:01 PM.

      Comment


        #4
        Thank you for the assistance above. It helped!

        I have a different flavor of Time question now.

        I have code that I need to execute on the first tick of a new bar, in order that a desired drawing will paint on the just-closed bar. This is working fine as long as the market remains open. But if it's the last bar of the session, I want the indicator to paint against that closed bar, as though there is a next bar opening, even though the next bar won't actually open until the next trading day. I want to be able to see the drawing since the last bar is complete, even though there won't be a new bar until the next day.

        Can anyone suggest how I might do this? thank you!

        Comment


          #5
          Hello bigea,

          Thanks for your post.

          Since OnBarUpdate is event based and updates with incoming ticks, using a timer and checking for a certain time with the timer would be more reasonable here.

          A reference sample which shows how a timer can be implemented is linked below. Bars.Session.GetNextBeginEnd can be used to obtain the session close as would be set in the Trading Hours template.

          SampleCustomEvents (Using a timer) - https://ninjatrader.com/support/helpGuides/nt7/using_custom_events_to_output_.htm

          Bars.Session.GetNextBeginEnd - https://ninjatrader.com/support/helpGuides/nt7/getnextbeginend.htm

          We look forward to being of further assistance.
          Last edited by NinjaTrader_Jim; 09-19-2019, 07:53 AM.
          JimNinjaTrader Customer Service

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by inanazsocial, Today, 01:15 AM
          0 responses
          2 views
          0 likes
          Last Post inanazsocial  
          Started by trilliantrader, 04-18-2024, 08:16 AM
          5 responses
          22 views
          0 likes
          Last Post trilliantrader  
          Started by Davidtowleii, Today, 12:15 AM
          0 responses
          3 views
          0 likes
          Last Post Davidtowleii  
          Started by guillembm, Yesterday, 11:25 AM
          2 responses
          9 views
          0 likes
          Last Post guillembm  
          Started by junkone, 04-21-2024, 07:17 AM
          9 responses
          70 views
          0 likes
          Last Post jeronymite  
          Working...
          X