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

Calculating RTH average range from daily bar

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

    Calculating RTH average range from daily bar

    Hi,

    Im trying to calculate 10 day average daily range of regular trading hour from daily bar but not sure how to do it.

    protected override void Initialize()
    {
    Add(PeriodType.Day, 1);
    }
    protected override void OnBarUpdate()
    {
    if (BarsInProgress == 1)
    {
    averageRange = SMA(Range(),10)[0];
    Print(Time[0].ToString());
    Print("averageRange of 10 days is "+averageRange.ToString());
    }
    }

    As you can see below output, all the calculations are starting from default session time...
    How would I need to do so Range calculation is done base only on RTH 6:30-1:15 PST.

    Is this possible? any suggestion?

    Any help would be appreciated.

    Output:
    9/24/2007 12:00:00 AM
    averageRange of 10 days is 156.5
    9/25/2007 12:00:00 AM
    averageRange of 10 days is 150.285714285714
    9/26/2007 12:00:00 AM
    averageRange of 10 days is 145.75
    9/27/2007 12:00:00 AM
    averageRange of 10 days is 138.888888888889

    #2
    Something like you have would probably work, but you'd probably have to change the chart properties session begin & end times. I wrote some code a while back, which I've modified somewhat to match your situation. I offer the following untested code as a possible starting point:

    Code:
    private DataSeries Day_Range;      // Range for specified hours.
    private DataSeries Average_Range;  // Average of day range
    private double low_value = Double.MaxValue;  // Start high, and reduce if smaller value found.
    private double high_value = Double.MinValue; // Start low, and increase if larger value found.
    
    protected override void Initialize()
    {
      Day_Range = new DataSeries(this);    // Initialize the data series.
      Average_Range = new DataSeries(this);
    }
    
    if ((ToTime(Time[0]) >= 063000) && (ToTime(Time[0]) <= 133000))
    {                              // During Pacific Standard Time trading hours...
      if (High[0] > high_value)    // Higher value for day?
         high_value = High[0];     // Yes.  Save it.
      if (Low[0] > low_value)      // Lower value for day?
         low_value = Low[0];       // Yes.  Save it.
    }
    else if (low_value != Double.MaxValue)
    {                              // At end of day...
      Day_Range.Set( high_value - low_value );   // Save range for the day.
      Average_Range.Set( SMA(Day_Range,10)[0] ); // Save the 10-day average.
      low_value = Double.MaxValue; // Start over for tomorrow.
      high_value = Double.MinValue;// Start over for tomorrow.
    }

    Comment


      #3
      Since you are running your calculations off of daily bars you can't really choose the trading hours you want included. Your calculation will take whatever trading hours the exchange and your data provider decides to aggregate into a daily bar as.

      Also when you are running historically all of the daily bars will have the 12:00 timestamp. There is no 6:30AM or 7AM on a daily bar.
      Josh P.NinjaTrader Customer Service

      Comment


        #4
        Josh,
        Thanks for clearing that up.

        KBJ,
        Thanks for the offering codes. I really appreciated.

        I clearly see the logic and understand how it should work but its not working. Looks like the Day_Range (range for the day) is not getting save into data series past current day.

        Is there something thats missing?

        protected override void OnBarUpdate()
        {
        if ((ToTime(Time[0]) >= 063000) && (ToTime(Time[0]) <= 131500))
        { // During Pacific Standard Time trading hours...
        if (High[0] > high_value) // Higher value for day?
        high_value = High[0]; // Yes. Save it.
        if (Low[0] < low_value) // Lower value for day? ************Typo correction here
        low_value = Low[0]; // Yes. Save it.
        }
        else if (low_value != Double.MaxValue)
        { // At end of day...
        Day_Range.Set( high_value - low_value ); // Save range for the day. ************Not saving!
        Average_Range.Set( SMA(Day_Range,10)[0] ); // Save the 10-day average.
        Print(Time[0].ToString());
        Print("Today high is " + high_value);
        Print("Today and Yday's Range is "+Day_Range[0]+" "+Day_Range[1]);
        Print("averageRange of 10 days is "+Average_Range[0]);
        Print(" ");
        low_value = Double.MaxValue; // Start over for tomorrow.
        high_value = Double.MinValue;// Start over for tomorrow.

        }
        }


        Output:

        11/2/2007 1:20:00 PM
        Today high is 13663
        Today and Yday's Range is 196 0 <---previous day range always zero..
        averageRange of 10 days is 19.6

        11/5/2007 1:20:00 PM
        Today high is 13640
        Today and Yday's Range is 173 0
        averageRange of 10 days is 17.3

        11/6/2007 1:20:00 PM
        Today high is 13690
        Today and Yday's Range is 158 0
        averageRange of 10 days is 15.8

        11/7/2007 1:20:00 PM
        Today high is 13594
        Today and Yday's Range is 282 0
        averageRange of 10 days is 28.2

        Comment


          #5
          How have you setup your Day_Range DataSeries object? Did you follow the code KBJ posted exactly? Namely with this line in the Initialize() method?
          Code:
          Day_Range = new DataSeries(this);
          I suggest you use some print commands to track the value changes of your high_value and low_value variables.
          Josh P.NinjaTrader Customer Service

          Comment


            #6
            Yes I initialized as KBJ stated. It compiles fine and the v high_value and low_value calculated fined. I confirmed this on Output. Here is the complete code.

            #region Variables
            // Wizard generated variables
            // User defined variables (add any user defined variables below)
            private DataSeries Day_Range; // Range for specified hours.
            private DataSeries Average_Range; // Average of day range
            private double low_value = Double.MaxValue; // Start high, and reduce if smaller value found.
            private double high_value = Double.MinValue; // Start low, and increase if larger value found.

            #endregion

            /// <summary>
            /// This method is used to configure the strategy and is called once before any strategy method is called.
            /// </summary>
            protected override void Initialize()
            {
            CalculateOnBarClose = true;
            Day_Range = new DataSeries(this); // Initialize the data series.
            Average_Range = new DataSeries(this);
            }

            /// <summary>
            /// Called on each bar update event (incoming tick)
            /// </summary>
            protected override void OnBarUpdate()
            {
            if ((ToTime(Time[0]) >= 063000) && (ToTime(Time[0]) <= 131500))
            { // During Pacific Standard Time trading hours...
            if (High[0] > high_value) // Higher value for day?
            high_value = High[0]; // Yes. Save it.
            if (Low[0] < low_value) // Lower value for day?
            low_value = Low[0]; // Yes. Save it.
            // Print(Time[0].ToString());
            // Print("is this working");
            // Print("High so far "+high_value);
            // Print("Low so far "+ low_value);
            // Print(" ");
            }
            else if (low_value != Double.MaxValue)
            { // At end of day...
            Day_Range.Set( high_value - low_value ); // Save range for the day.
            Average_Range.Set( SMA(Day_Range,10)[0] ); // Save the 10-day average.
            Print(Time[0].ToString());
            Print("Today high is " + high_value);
            Print("Today and Yday's Range is "+Day_Range[0]+" "+Day_Range[1]);
            Print("averageRange of 10 days is "+Average_Range[0]);
            Print(" ");
            low_value = Double.MaxValue; // Start over for tomorrow.
            high_value = Double.MinValue;// Start over for tomorrow.

            }

            Comment


              #7
              From the looks of your code you are no longer running on a daily chart. This is why your Day_Range[1] is 0. Your code is only setting the DataSeries to its range value once a day. This is only 1 bar out of all of your bar objects. The previous bar in the object is NOT the bar of the previous day; it is just the bar of the previous 1min (if you are on a 1min chart).

              What you want to do is create a multi-time framed strategy and sync your DataSeries to the secondary daily bar object. Please see this reference sample of how to do so: http://www.ninjatrader-support.com/v...ead.php?t=3572
              This will allow you to reference Day_Range[1] and have it actually be 1 day ago.
              Josh P.NinjaTrader Customer Service

              Comment


                #8
                Originally posted by Josh View Post
                From the looks of your code you are no longer running on a daily chart.

                why would I run this daily bar? all the historical datas are processed on close of the bar? if daily then "((ToTime(Time[0]) >= 063000) && (ToTime(Time[0]) <= 131500))" will never get processed...

                This is why your Day_Range[1] is 0. Your code is only setting the DataSeries to its range value once a day. This is only 1 bar out of all of your bar objects. The previous bar in the object is NOT the bar of the previous day; it is just the bar of the previous 1min (if you are on a 1min chart).

                Not true.
                This else statement is to find end of the day and set Day_Range once a day.
                so Day_Range[1] is not just previous bar.

                else if (low_value != Double.MaxValue)
                { // At end of day...
                Day_Range.Set( high_value - low_value ); // Save range for the day.
                Average_Range.Set( SMA(Day_Range,10)[0] ); // Save the 10-day average.

                What you want to do is create a multi-time framed strategy and sync your DataSeries to the secondary daily bar object. Please see this reference sample of how to do so: http://www.ninjatrader-support.com/v...ead.php?t=3572
                This will allow you to reference Day_Range[1] and have it actually be 1 day ago.

                Whole point of this manually tracking the intra highs/lows to find day's range is so that we dont have to use multi-time frame strategy?
                Josh,

                It KBJ's code's intention is the work around so I do not use multi-time frame strategy since one can not specified session time on daily bar from small time frame (5min in my case)

                If you look at the code closely, it is supposed to save one data(Day_Range) at the end of the session and calculate 10day average of range using this saved data for the day.

                Now, looking at your suggestion of syncing multi-frame, I would have to daily bar which means im back to the original problem where I would be referencing data that starts 12:00am...

                Comment


                  #9
                  No, it does not bring you back to the original problem. You do all your calculations on the 1min series, but you save the relevant data onto a data series in sync with the daily bars.

                  If you added this line of code to the end of your current code in OnBarUpdate() you will see your current problem.
                  Code:
                  Print("Day_Range[0]: "+Day_Range[0]+" Day_Range[1]: "+Day_Range[1]);
                  As I mentioned earlier, on a 1min chart there are too many bars that you are not setting any value for. Referencing Day_Range[1] is NOT the same as referencing 1 day ago. That is simply referencing 1 bar ago. Now, if you created a DataSeries in sync with the daily bars that will allow you to do Day_Range[1] and have it actually reference 1 day ago.
                  Josh P.NinjaTrader Customer Service

                  Comment


                    #10
                    Thanks Josh,

                    I got it to work storing the data in daily time frame.


                    11/19/2007 6:35:00 AM
                    163 210 176 263 193
                    average range for last 5 days is 201


                    11/20/2007 6:35:00 AM
                    182 163 210 176 263
                    average range for last 5 days is 198.8

                    Comment


                      #11
                      Glad to hear it is working.
                      Josh P.NinjaTrader Customer Service

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by pkefal, 04-11-2024, 07:39 AM
                      11 responses
                      36 views
                      0 likes
                      Last Post jeronymite  
                      Started by bill2023, Yesterday, 08:51 AM
                      8 responses
                      43 views
                      0 likes
                      Last Post bill2023  
                      Started by yertle, Today, 08:38 AM
                      6 responses
                      25 views
                      0 likes
                      Last Post ryjoga
                      by ryjoga
                       
                      Started by algospoke, Yesterday, 06:40 PM
                      2 responses
                      24 views
                      0 likes
                      Last Post algospoke  
                      Started by ghoul, Today, 06:02 PM
                      3 responses
                      16 views
                      0 likes
                      Last Post NinjaTrader_Manfred  
                      Working...
                      X