• 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

Prior Day values using GetBar

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

    Prior Day values using GetBar

    Hi there

    I created a method that finds the High and Low of yesterdays values from a specific point in time using DateTime.

    startDateTime = new DateTime(Time[0].Year, Time[0].Month, Time[0].Day-1, 10, 00, 0);

    int barsAgo = CurrentBar - Bars.GetBar(startDateTime); // Store price data from 10:30 AM

    getHighestHigh = MAX(High, 1)[barsAgo];
    ...

    It works fine except when I get to Monday then it spits out values that are maybe from Sunday since they are futures?

    I prefer only US Equities RTH and assumed it would only use Monday-Friday...

    Do I have to add a line that specifies the days?

    Irvin

    #2
    Ooops - I think I found my reply in SampleGetBar:

    /* If the day is Monday, and we want the value from Friday, we must subtract 3 days from the current date.
    If the day is Tuesday-Friday, just subtract one day. */
    if (Time[0].DayOfWeek == DayOfWeek.Monday)
    timeOfInterest =
    new DateTime(Time[0].Year, Time[0].Month, Time[0].Day - 3, 9, 30, 0);
    else
    timeOfInterest = new DateTime(Time[0].Year, Time[0].Month, Time[0].Day - 1, 9, 30, 0);

    Comment


      #3
      Hello,

      That should work for you. Let me know if you have any questions on these references.
      MatthewNinjaTrader Product Management

      Comment


        #4
        I added the below code and Tuesday through Friday work but now on Monday I get an output msg:


        2780.5 3/28/2012 10:00:00 AM
        2774.5 3/28/2012 10:00:00 AM
        2790.25 3/29/2012 10:00:00 AM
        2779.5 3/29/2012 10:00:00 AM
        2766.5 3/30/2012 10:00:00 AM
        2752 3/30/2012 10:00:00 AM
        Error on calling 'OnBarUpdate' method for indicator 'MyMethodDrawFibs' on bar 52: You are accessing an index with a value that is invalid since its out of range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.

        I don't understand why it doesnt work like the template -3 will get you back to Friday but barsAgo doesn't have enough bars on the chart?

        Comment


          #5
          Hello,

          Have you added a currentbar check to ensure that there are enough bars to calculate?

          Please try adding if(CurrentBar < barsAgo) return; to the beginning of your OnBarUpdate method.
          MatthewNinjaTrader Product Management

          Comment


            #6
            Yes, but that didn't help.

            I broke it down and started literally counting and am starting to think its my example where its Friday at end of month, so the -3 doesn't point to anything.

            That is:

            Friday May 30, 2012 and I'm comparing Monday May 2nd

            I probably need to factor in end of month?

            Comment


              #7
              Update: I added more days to view in my chart and it does work but gives the same message only at the end of the month. So its simply comparing dates at the end of the months...

              Comment


                #8
                Hmm... yeah from this approach you would need to add logic for the end of the month. You are essentially emulating functionally that exists when using a session tempalte.

                Lets take a step back here: you're sure that using the session template of US Equities RTH there are values being printed from outside those hours? Can I see an example of this?
                Last edited by NinjaTrader_Matthew; 04-26-2012, 11:43 AM.
                MatthewNinjaTrader Product Management

                Comment


                  #9
                  Yes, I am using US Equities RTH.

                  Is there a better approach?

                  I can't seem to figure out the logic for comparing the months because we are not able to simply comparing from Time[0] to Time[1].

                  To clarify, the snippet of code works except for the Monday at end of the month. So a couple of times a month its okay, lol.

                  The reason, I believe has nothing to do with US Equities RTH as I mentioned earlier but the fact that we are comparing days and it has switched months.

                  This approach was from the provided template so there much be an issue there as well?

                  Comment


                    #10
                    You're on the right track, however there will need to have custom code to handle this.

                    You would need to use something that is basically saying, if "the .Day value for the given bars ago interest time" is between 1 and 7, then Subtract 1 for the month of interest
                    MatthewNinjaTrader Product Management

                    Comment


                      #11
                      Originally posted by NinjaTrader_Matthew View Post
                      You're on the right track, however there will need to have custom code to handle this.

                      You would need to use something that is basically saying, if "the .Day value for the given bars ago interest time" is between 1 and 7, then Subtract 1 for the month of interest
                      Let me see if I follow you.

                      The time of interest is say - 9:30 AM so we define it with:

                      timeOfInterest = new DateTime(Time[0].Year, Time[0].Month, Time[0].Day, 9, 30, 0);

                      Then

                      int myBarsAgo = GetBar(timeOfInterest);

                      But how will - if myBarsAgo >= 1 && myBarsAgo <= 7
                      Time[0].Month-1 help?

                      Sorry - I guess I'm not following because I went by the provided template and also searched the forum for like examples but each of them has the same error that I don't see corrected...


                      Irvin

                      Comment


                        #12
                        Hello,

                        There reference sample would also exhibit this behavior and was not designed to provide solutions to all items you might run into in this area. These are just used to help point you in the right direction.

                        You will need to add custom logic which is missing from the reference sample to handle the month breaks.

                        You should compare the Time[barsAgo] for that Day

                        Code:
                        if (Time[barsago].Day >= 1 && Time[barsago].day <=7) 
                        //make my month special
                        MatthewNinjaTrader Product Management

                        Comment


                          #13
                          Thanks Matthew for your suggestion.

                          Below is the code I wrote.

                          Hopefully it proves useful to somebody searching on this exact topic and helps avoid wasting time.

                          I went with more of a nested approach because after struggling with this easy concept it was the only way I could process all of the different scenarios in my head.

                          Please note: I commented out the print statements and return but they are there so you can test them if neccessary.

                          After correctly identifying the dates, go ahead and use the barsAgo for whatever you need it to.

                          I ended up using barsAgo with the MAX function to find the maximum values at 10 AM for any time interval on any timeframe.

                          I'm interested in opening range trading as they say ...

                          Cheers.
                          Irvin


                          PHP Code:
                          #region Using declarations
                          using System;
                          using System.ComponentModel;
                          using System.Diagnostics;
                          using System.Drawing;
                          using System.Drawing.Drawing2D;
                          using System.Xml.Serialization;
                          using NinjaTrader.Cbi;
                          using NinjaTrader.Data;
                          using NinjaTrader.Gui.Chart;
                          #endregion
                          // This namespace holds all indicators and is required. Do not change it.
                          namespace NinjaTrader.Indicator
                          {
                          [
                          Description("")]
                          public class 
                          MyMethod Indicator
                          {
                          #region Variables
                          private DateTime startDateTime;
                           
                           
                          #endregion
                           
                          protected override void Initialize()
                          {
                          Overlay true;
                          CalculateOnBarClose true;
                          }
                           
                          protected 
                          override void OnBarUpdate()
                          {
                           
                          if (
                          Bars.FirstBarOfSession && // Display only once a session
                          CurrentBar && // My preference to start after the first bar of the session
                          Time[0].DayOfWeek != DayOfWeek.Saturday && // No weekends
                          Time[0].DayOfWeek != DayOfWeek.Sunday // No weekends
                          )
                          {
                          if (
                          Time[0].DayOfWeek != DayOfWeek.Monday// Don't deal with Monday here
                          {
                          if (
                          Time[0].Day-0// Avoid crashing here because of beginning of month

                          startDateTime = new DateTime(Time[0].YearTime[0].MonthTime[0].Day-110000); // Set time to previous day 10 AM
                          //Print("Day of Week: "+Time[0].DayOfWeek+" "+startDateTime+" | "+Time[0].ToString());
                          }
                           
                          if (
                          Time[0].Day-== 0// 1st day of beginning of month. Since don't deal with Monday here only worried about first of month

                          startDateTime = new DateTime(Time[0].YearTime[0].Month-1Time[0].Day-1+DateTime.DaysInMonth(Time[0].YearTime[0].Month-1), 10000); // Subtract month by 1, days by 1 but add the previous Months days
                          //Print("Days in Month: "+DateTime.DaysInMonth(Time[0].Year, Time[0].Month-1)+" | "+Time[0].ToString());
                          //Print("Day of Week: "+Time[0].DayOfWeek+" and 1st of Month: "+startDateTime+" | "+Time[0].ToString());
                          //return;
                          }
                          }
                           
                          else if(
                          Time[0].DayOfWeek == DayOfWeek.Monday// if its Monday
                          {
                          if(
                          Time[0].Day-0// and its not beginning of month so wont crash

                          startDateTime = new DateTime(Time[0].YearTime[0].MonthTime[0].Day-310000); // Set time to previous day 10 AM
                          //Print("Monday so get Friday: "+startDateTime+" | "+Time[0].ToString());
                          }
                           
                          if (
                          Time[0].Day-== -||
                          Time[0].Day-== -||
                          Time[0].Day-== 0


                          startDateTime = new DateTime(Time[0].YearTime[0].Month-1Time[0].Day-3+DateTime.DaysInMonth(Time[0].YearTime[0].Month-1), 10000); // Subtract month by 1, days by 1 but add the previous Months days
                          //Print("Days in Month: "+DateTime.DaysInMonth(Time[0].Year, Time[0].Month-1)+" | "+Time[0].ToString());
                          //Print("Day of Week: "+Time[0].DayOfWeek+" and 1st of Month: "+startDateTime+" | "+Time[0].ToString());
                          //return;
                          }
                          }
                          }
                          }
                           
                          #region Properties
                           
                          #endregion
                          }

                          Last edited by ij001; 04-30-2012, 11:58 AM.

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by vpzdcv, Today, 02:31 AM
                          0 responses
                          7 views
                          0 likes
                          Last Post vpzdcv
                          by vpzdcv
                           
                          Started by YevhenShynkarenko, Today, 01:22 AM
                          0 responses
                          14 views
                          0 likes
                          Last Post YevhenShynkarenko  
                          Started by ttodua, Today, 12:52 AM
                          0 responses
                          6 views
                          0 likes
                          Last Post ttodua
                          by ttodua
                           
                          Started by ttodua, Today, 12:50 AM
                          0 responses
                          5 views
                          0 likes
                          Last Post ttodua
                          by ttodua
                           
                          Started by mulshah, Yesterday, 11:12 PM
                          0 responses
                          7 views
                          0 likes
                          Last Post mulshah
                          by mulshah
                           
                          Working...
                          X