Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Newbie Questions about Objects Creation and Bars.GetBar()

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

    Newbie Questions about Objects Creation and Bars.GetBar()

    Hi,

    I have 2 questions:

    1, I would like to create MouseEventHandler(ChartPanel_MouseMove), System.Timers.Timer() and TextBox(),
    not sure if it is better to put in (State==State.DataLoaded) or (State==State.Historical) ?
    It seems either way works, but I would like to know the correct way of doing it.

    2, I understand in NinjaTrader, index counting start from the bar on the far right (index 0), wonder why Bars.GetBar(time)
    does the opposite? I will have to do 'CurrentBar - Bars.GetBar(time)' to get the correct index number.


    Thanks so much!

    #2
    Hello warpinator,

    Thank you for your post.

    State.DataLoaded will be called one time after all data series have been loaded. This is the state where you should use logic that needs to access data-related objects like Bars, Instruments, BarsPeriod, TradingHours orinstantiating indicators. If your object depends on data being available, it would be be best to do so from State.DataLoaded. State.Historical is called once an object is processing historical data. For example, this state is called once when running an object in real-time. This is the state that notifies you that the object is processing historical data.

    I'm not sure what you mean by GetTime does the opposite? GetTime returns the time stamp at the specified current bar index value. It doesn't take a time parameter, it takes an index.

    CurrentBar - Bars.GetTime won't work and will likely won't compile, CurrentBar is an integer and Bars.GetTime returns a DateTime object. You can't subtract time from an integer.



    CurrentBar is the same as bar index.



    Please let us know if you have any further questions. ​

    Comment


      #3
      Hi Gaby,

      Thanks so much for your reply, it makes so much sense now.

      I am sorry, it wasn't GetTime, it was GetBar, giving a DateTime value and returning an index number,
      Bars.GetBar(My_time),

      Lets say I have 100 bars in a chart, then the index number of far right (most recent) should be 0 and
      the index number of far left is 99.

      But, when I set 'My_time' value to the time of the most recent bar, I will get index number 99, instead of 0,
      to get the correct index number, I will have to convert it by doing "CurrentBar - Bars.GetBar(My_time)",
      everything works great for me, I am just curious the reason behind it.

      Thanks so much!
      Last edited by warpinator; 10-28-2024, 10:34 AM.

      Comment


        #4
        My apologies, I misread your post as GetTime!

        GetBar returns the first bar that matches the time stamp of the "time" parameter provided. So it all depends on what time you are passing into the method.

        CurrentBar is already the index of the most recent bar (as long as your script is already in real-time).

        If you pass in Time[0] (the time of the current bar processing, which is the most recent bar in realtime) to GetBar it will be the same value as CurrentBar.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
        0 responses
        576 views
        0 likes
        Last Post Geovanny Suaza  
        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
        0 responses
        334 views
        1 like
        Last Post Geovanny Suaza  
        Started by Mindset, 02-09-2026, 11:44 AM
        0 responses
        101 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
        0 responses
        553 views
        1 like
        Last Post Geovanny Suaza  
        Started by RFrosty, 01-28-2026, 06:49 PM
        0 responses
        551 views
        1 like
        Last Post RFrosty
        by RFrosty
         
        Working...
        X