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

Bar Corresponding to Given DateTime

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

    Bar Corresponding to Given DateTime

    Hi,

    Suppose I pick an arbitrary DateTime, call it myTime, that comes after the time of the first bar painted on the chart, i.e. myTime > ChartControl.FirstTimePainted.

    Is there a way to find the bar index and bar time of the first bar on the chart that comes at, or after myTime? Will the xCoordinate approach work on this?

    Any suggestions would be greatly appreciated. Thanks.

    Currently I am using:

    Code:
                        int startBarIndex = ChartBars.GetBarIdxByTime(ChartControl, startDateTime);
                        startDateTime = ChartBars.GetTimeByBarIdx(ChartControl, startBarIndex);
    Last edited by Zeos6; 08-18-2017, 05:11 PM.

    #2
    Hello Zeos6,

    You are looking for the bar index of the first bar painted?

    <ChartBars>.FromIndex <ChartBars>.ToIndex



    Or you are looking for the very first historical bar?
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Hi ChelseaB,

      I am looking for the DateTime of the first bar on the chart that comes AFTER a given DateTime. I specify a DateTime and want to know the first bar that comes after that DateTime. Of course if the given DateTime is before the first bar painted on the chart, the first bar should be returned.

      Comment


        #4
        Hello,

        Thank you for the reply.

        In this situation, you can use the methods you had originally posted to locate the target DateTime. In post 2, the FromIndex and ToIndex are shown, you can utilize these indexes to know what range on the chart is currently being viewed. Using this information, you could supply the index from the visible range to check if the Time of that bar is the target time.

        Here is a simple loop that would go over the visible bars:

        Code:
        for (int idx = ChartBars.FromIndex; idx <= ChartBars.ToIndex; idx++)
        {
        
        }
        You could supply idx to ChartBars.GetTimeByBarIdx to get the time of that bars index and compare that time to your target time.

        Once located, you could create a variable to store the values of the next bar, the next bars index would be the idx incremented by 1. Using the idx + 1 would allow you to gather the values from the next bar but you may need to add error handling in case the idx +1 ends up being more than the bars of the chart I.E. the target date is the current bar and no other bars are after this bar.




        I look forward to being of further assistance.
        JesseNinjaTrader Customer Service

        Comment


          #5
          Hi Jesse,

          Thank you for your reply. The information is useful but does not apply in my case as I don't really care whether the bar, or the desired DateTime, is visible or not. The method I outlined earlier actually works quite well. It is a two step approach and I just thought there might be a simpler method.

          Comment


            #6
            Is this what you're looking for?

            Code:
            DateTime startDateTime = your arbitrary DateTime;
            
            //  allow for any bar that comes after the first painted bar, even the ones off screen
            int      startBarIndex = (startDateTime >= ChartControl.FirstTimePainted && startDateTime <= ChartBars.GetTimeByBarIdx(ChartControl, CurrentBar)) ? ChartBars.GetBarIdxByTime(ChartControl, startDateTime) : null;
            
            //  limit the index to only the bars on screen
            int      startBarIndex = (startDateTime >= ChartControl.FirstTimePainted && startDateTime <= ChartControl.LastTimePainted) ? ChartBars.GetBarIdxByTime(ChartControl, startDateTime) : null;
            
            if(startBarIndex != null)
            {
                //  do something
            }

            Comment


              #7
              Hi gubbar924,

              Thanks for your reply. This is essentially what I had posted. I simply omitted the checks when I posted but my code does contain the appropriate checks, as you have shown.

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by merzo, 06-25-2023, 02:19 AM
              10 responses
              823 views
              1 like
              Last Post NinjaTrader_ChristopherJ  
              Started by frankthearm, Today, 09:08 AM
              5 responses
              15 views
              0 likes
              Last Post NinjaTrader_Clayton  
              Started by jeronymite, 04-12-2024, 04:26 PM
              3 responses
              43 views
              0 likes
              Last Post jeronymite  
              Started by yertle, Today, 08:38 AM
              5 responses
              16 views
              0 likes
              Last Post NinjaTrader_BrandonH  
              Started by adeelshahzad, Today, 03:54 AM
              3 responses
              19 views
              0 likes
              Last Post NinjaTrader_BrandonH  
              Working...
              X