• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

GetBar versus Bars.GetBar

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

    GetBar versus Bars.GetBar

    Hello,

    I would like to get the High between two DateTimes from a tick chart. For example,

    Code:
    DateTime dt1 = Time[0].AddMinutes(-5);
    DateTime dt2 = Time[0].AddMinutes(-20);
    So I am trying to get the High for the period from 20 minutes ago to 5 minutes ago.

    I am not certain if it is correct to use Bars.GetBar or GetBar.

    1. Could you please explain the difference, if any, between Bars.GetBar and GetBar?

    2. Please provide a sample of how to obtain the High value between two times?

    Thank you.

    #2
    Hello,

    The major difference between Bars.GetBar and GetBar is that Bars.GetBar returns the actual bar number where just GetBar returns a BarsAgo.

    Here are examples of each:

    Bars.GetBar
    Code:
    int bar = Bars.GetBar(Time[0].AddMinutes(-20));
    DrawDot("MyDot", true, CurrentBar - bar, High[0] + 2 * TickSize, Color.Blue);
    GetBar
    Code:
    int bar = GetBar(Time[0].AddMinutes(-20));
    DrawDot("MyDot", true, bar, High[0] + 2 * TickSize, Color.Blue);
    These would be the same statements, the Bars.GetBar requires that you subtract the Bar from the CurrentBar to get a BarsAgo.

    For the HighValue, you could take the amount of BarsAgo you get back from GetBar and use the HighestBar or MAX to get the Highest value in that period.


    Code:
    int highBar = HighestBar(High, bar);
    double high = High[highBar];

    Please let me know if I may be of additional assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Your explanation of the difference between Bars.GetBar and GetBar was very clear. Thank you.

      Regarding how to get the High between two times, wouldn't your sample code just give me the High over the last N bars where N equals the value of GetBar? It would seem to assume you are starting from now and looking back over N bars.

      What I want to do is start 5 minutes ago and look back over the 15 minutes prior to that? As an example, it is now 9:30 am. I want to get the High between 9:10 and 9:25.

      Comment


        #4
        Hello,

        Yes the sample is not entirely everything you would need to just copy and paste into your script. You would need to use logic and create your own code that will fit into your script how you need.

        You can use a for loop and duplicate the initial GetBar line to get the highest value in the range.

        Code:
        int startBar = GetBar(Time[0].AddMinutes(-20));
        int endBar = GetBar(Time[0].AddMinutes(-5));
        After getting the Start and End bars, you could then run a loop between the BarsAgo to find the highest value in the set.

        Code:
        double high = 0;
        			
        for(int i = startBar; i >= endBar; i--)
        {
        	if(high< High[i]) high = High[i];
        }
        Please let me know if I may be of additional assistance.
        JesseNinjaTrader Customer Service

        Comment


          #5
          Extending your example, could I use:

          int startBar = GetBar(Time[0].AddMinutes(-20));
          int endBar = GetBar(Time[0].AddMinutes(-5));
          int barDiff = endBar- startBar;

          double myHigh = this.MAX(High, barDiff)[endbar];

          The variable barDiff tells me over how many bars to seek the High and endBar tells me the number of bars back from the current observation to commence.

          Can you please confirm?

          Thank you.

          Comment


            #6
            Hello,

            I tried the sample you had posted but I do not see the same result, I simply put both in 1 indicator to see the difference in results.

            Code:
            int startBar = GetBar(Time[0].AddMinutes(-20));
            int endBar = GetBar(Time[0].AddMinutes(-5));
            			
            double high = 0;
            			
            for(int i = startBar; i >= endBar; i--)
            {
            	if(high< High[i]) high = High[i];
            }
            	
            DrawLine("High", startBar, high, endBar, high, Color.Red);
            		
            int startBar1 = GetBar(Time[0].AddMinutes(-20));
            int endBar1 = GetBar(Time[0].AddMinutes(-5));
            int barDiff	= endBar1- startBar1;
            
            double myHigh = this.MAX(High, barDiff)[endBar1];
            			
            DrawLine("High1", startBar1, myHigh, endBar1, myHigh, Color.Blue);
            The Start and End are the same but the end result High seems to be different. In the sample you had posted the value or [endBar1] is being used which may or may not be the amount of bars ago that was the HighestBar.

            Using startBar instead of endBar in the BarsAgo gives a more reasonable result but I do see it fluctuating up and down where the forloop is staying at the high.

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

            Comment


              #7
              I have used the following code in numerous strategies that use Bar charts (i.e. 1m, 5m, etc.) to obtain the High between 2 times.

              int barsAgo = CurrentBars[BarsInProgress] - Bars.GetBar(endTime);
              int minutesInPeriod = ((int)endTime.Subtract(startTime).TotalMinutes);
              int barsInPeriod = minutesInPeriod/BarsArray[BarsInProgress].Period.Value;

              double myHigh = this.MAX(High, barsInPeriod)[barsAgo];

              This has worked perfectly countless times. However, in the above case, I can use the High of a particular set of bars. In the current case, I am using tick bars. Could that impact the results I am seeing?

              Comment


                #8
                Hello,

                Being that you are using a Time rather than a fixed number of bars as the period, this should not be the case just being the tick series.

                It looks like you have the difference backwards.


                You are getting a negative number subtracting the End from the Start bar, instead do the StartBar minus the EndBar.

                int barDiff = startBar1 - endBar1;

                and this landed on the same value as my example.

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

                Comment


                  #9
                  Jesse,

                  Calling the code we discussed on tick charts is causing problems. For some reason, the values of start1 and end1 return the same value despite having different DateTime object values passed. Could it be because no tick occurred at the specific value of one of the DateTimes?

                  This is never an issue with minute bars.

                  Thank you.

                  Comment


                    #10
                    Hello,

                    Do you have data loaded for the time you are requesting?

                    This would not return what you are expecting if the data is not available. I had tried this on various Tick charts and it was working so long as I had the amount of data I needed.

                    Can you tell me what instrument and timeframe you are using? is the code you are using identical to what is posted? if not can you post the revised version of what you are using and instrument for me to try?

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

                    Comment


                      #11
                      Yes. I an using the CL futures contract expiring in August (CLQ5). I have loaded 6 days of data and am simply trying to pull yesterday's H and L between 9 am and 2:30 pm Eastern.

                      So:
                      int start1 = GetBar(new DateTime(2015, 7, 6, 9, 0,0));
                      int end1 = GetBar(new DateTime(2015, 7, 6, 14, 30,0));

                      I get a return value for start1, but end1 returns the exact same value. There is definitely enough data loaded.

                      Comment


                        #12
                        Hello,

                        I am not seeing this on my end, this may lie in the code you have not posted.

                        This is the test I completed that worked on the CL.

                        Code:
                        int startBar1 =  GetBar(new DateTime(2015, 7, 6, 9, 0,0)); 
                        int endBar1 = GetBar(new DateTime(2015, 7, 6, 14, 30,0)); 
                        int barDiff	= startBar1- endBar1;
                        
                        double myHigh = this.MAX(High, barDiff)[endBar1];
                        						
                        DrawLine("High1", startBar1, myHigh, endBar1, myHigh, Color.Blue);
                        DrawDot("MyDot", true, startBar1, myHigh, Color.Blue);
                        DrawDot("MyDot2", true, endBar1, myHigh, Color.Red);
                        I look forward to being of further assistance.
                        JesseNinjaTrader Customer Service

                        Comment


                          #13
                          Thank you.

                          Can you please advise what value you obtained for the variable myHigh?

                          I added the code you run above to a new, blank strategy and got a value for myHigh or 58.17. This is very far away from the true High that occurred between 9am and 2:30 pm Eastern. The high value should be 54.73. I am off by more than $3.

                          Any ideas why my value is so far off?

                          Comment


                            #14
                            Hello,

                            I have attached the test being used and an image of the result.

                            The value I am getting on the Cl 09-15 150 tick chart is 54.31.

                            Additionally I have included the for loop version in the script for comparison. Testing between the two the for loop seems to be considerably faster than using Max as there is less involved in that logic than there is with the Max indicator.



                            Please let me know if I may be of further assistance.
                            Attached Files
                            JesseNinjaTrader Customer Service

                            Comment


                              #15
                              I have implemented your loop within my blank strategy. I am still getting values that are very far off.

                              Could there be some issue with the data caches? I have been working on this simple issue for hours and cannot resolve it.

                              Thank you.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by spikeruk, Today, 01:49 PM
                              1 response
                              8 views
                              0 likes
                              Last Post NinjaTrader_PaulH  
                              Started by Yandychang, Today, 02:16 PM
                              4 responses
                              12 views
                              0 likes
                              Last Post Yandychang  
                              Started by priceisking, Today, 01:16 PM
                              1 response
                              6 views
                              0 likes
                              Last Post NinjaTrader_PaulH  
                              Started by kashyapjdave, 03-23-2017, 12:12 AM
                              11 responses
                              1,956 views
                              0 likes
                              Last Post forextrader2003  
                              Started by Gav_G, 07-26-2018, 04:29 PM
                              5 responses
                              422 views
                              0 likes
                              Last Post NinjaTrader_PaulH  
                              Working...
                              X