Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

average range for one day (or some days) of the week only.

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

    average range for one day (or some days) of the week only.




    regards to everyone.



    i'm interested in the cl contract and want to determine whether it is traded with any more volatility on the days when there are eia - api petroleum reports.


    so, initially i'm interested in calculating the average for the latest 52 observations of the range of the day ( Value[0] = High[0] - Low[0]; ) but only for the days that were tuesdays.


    i'm also interested in this same calculation for all days that were wednesdays only. and subsequently it would be necessary to compare the average range for tuesdays versus the average range for all days that were not tuesdays (mondays, wednesdays, thursdays, fridays), same for wednesdays versus non wednesdays, and finally tuesdays and wednesdays together versus all other days that were neither tuesdays nor wednesdays.



    i asked for help on stack overflow but didn't get anywhere.

    https://stackoverflow.com/questions/...ay-of-the-week


    in mathematical terms i would use indicator functions (1 or 0) for every single day of the week but when it comes to programming i have no idea if it is possible to use this kind of conditional attributes inside calculations like those for the daily range or a simple average.



    very well, thanks, regards.
    Last edited by rtwave; 01-05-2021, 09:03 AM.

    #2
    So mean only Calculate for Tuesday ?

    Comment


      #3
      Hello rtwave,

      Thanks for your post.

      You can determine the day of the week on a per bar basis by using DayOfweek.

      For example if (Times[0][0].DayOfWeek == DayOfWeek.Wednesday)

      Comment


        #4


        PaulH,


        thanks.


        it would be easy to export a little bit of ohlc daily data to libreoffice calc, calculate the daily range, add a column to extract the day of the week, filter the data by day of the week and then have libreoffice calculate the simple averages (the program automatically adjusts the sum and count for any data one selects and/or filters). this would be practical for just one symbol, one day and a one time calculation, but it would be too much work if i wanted to do these calculations repeatedly for different symbols, different days of the week and different periods of time.


        an indicator that could be used for any symbol and for any period of time would be much better.


        the formula for a a simple average is the sum of all relevant values divided by the count of all relevant values.


        sma = ( A1​+A2​+...+An​​ ) / n


        Code:
        protected override void OnBarUpdate()
        {
        if (BarsArray[0].BarsType.IsRemoveLastBarSupported)
        {
        if (CurrentBar == 0)
        Value[0] = Input[0];
        else
        {
        double last = Value[1] * Math.Min(CurrentBar, Period);
        
        if (CurrentBar >= Period)
        Value[0] = (last + Input[0] - Input[Period]) / Math.Min(CurrentBar, Period);
        else
        Value[0] = ((last + Input[0]) / (Math.Min(CurrentBar, Period) + 1));
        }
        }
        else
        {
        if (IsFirstTickOfBar)
        priorSum = sum;
        
        sum = priorSum + Input[0] - (CurrentBar >= Period ? Input[Period] : 0);
        Value[0] = sum / (CurrentBar < Period ? CurrentBar + 1 : Period);
        }

        this code above is nt's version of the sma. ¿how should i incorporate the (Times[0][0].DayOfWeek == DayOfWeek.Tuesday) conditional so that only data points which satisfy this statement are added to the sum and the count and thus the sma is only calculated for the values for tuesdays?


        very well, regards.
        Last edited by rtwave; 01-10-2021, 03:30 PM.

        Comment


          #5
          Hello rtwave,

          What type of data are you working with? Daily bars? Intraday data?

          Comment


            #6



            i'm only interested in daily data. it is the daily range that i am focusing on. other intervals would not make much sense.



            and i think that the ultimate version for this indicator would receive a list of days as an input where the user could define which days to include in the calculations and which to leave out: {mo, tu, we, th, fr, sa, su}, {tu}, {we}, {tu, we}, and so on.

            Comment


              #7
              Hello rtwave,

              Thanks for your reply.

              Here is an example of an approach you could take:

              if (Time[0].DayOfWeek == DayOfWeek.Wednesday)
              {
              accumulatedRange += (High[0] - Low[0]); // accumulatedRange is declared as a double type
              dayCounter ++; // dayCounter is declared as an Int type
              currentAverage = (accumulatedRange / dayCounter); // currentAverage is delcared as a double type.
              }


              You can create bool inputs for each day of the week so that they can be selected at the time the indicator is applied. In your code you would check to see if the bar time day of week matches what the selected bool, like this:

              if (
              // SundayCheck
              ((Sunday == true)
              && (Times[0][0].DayOfWeek == DayOfWeek.Sunday))
              // MondayCheck
              || ((Monday == true)
              && (Times[0][0].DayOfWeek == DayOfWeek.Monday))
              // TuesdayCheck
              || ((Tuesday == true)
              && (Times[0][0].DayOfWeek == DayOfWeek.Tuesday))
              // WednesdayCheck
              || ((Wednesday == true)
              && (Times[0][0].DayOfWeek == DayOfWeek.Wednesday))
              // ThursdayCheck
              || ((Thursday == true)
              && (Times[0][0].DayOfWeek == DayOfWeek.Thursday))
              // FridayCheck
              || ((Friday == true)
              && (Times[0][0].DayOfWeek == DayOfWeek.Friday)))
              {
              accumulatedRange += (High[0] - Low[0]); // accumulatedRange is declared as a double type
              dayCounter ++; // dayCounter is declared as an Int type
              currentAverage = (accumulatedRange / dayCounter); // currentAverage is delcared as a double type.

              }

              #region Properties
              [NinjaScriptProperty]
              [Display(Name="Sunday", Order=1, GroupName="Parameters")]
              public bool Sunday
              { get; set; }

              etc. etc.


              Comment


                #8



                PaulH, people with nt,




                regards.


                i was only able to get to work on this indicator again a while ago and while i have made some progress there are still some details that i can't figure out by myself.


                right now i' working on the simplest version only for tuesdays, once i get this to work i will try to get the more complicated version with checks for every day of the week to work.


                this is the code i have right now:


                Code:
                protected override void OnBarUpdate()
                {
                
                if (Time[0].DayOfWeek == DayOfWeek.Tuesday)
                
                {
                
                cumulativeRange += (High[0] - Low[0]);
                dayCounter ++;
                currentAverage = (cumulativeRange / dayCounter);
                
                }
                
                Value[0] = currentAverage;
                
                }

                this code does generate a plot, but it is obvious to me that the value for period is nowhere to be found. these calculations could be correct, but i only want the cumulative range and day counter to be summed and increased as many times as the value for period and this is nowhere to be found in the code.


                Click image for larger version

Name:	20210121 average range tuesdays only 001.JPG
Views:	595
Size:	133.6 KB
ID:	1136086


                i am missing that strange code where one can get nt to do some operations as many times as there are available observations if the available observations are less than the period, and as many times as period if period is equal or larger than available observations.


                Comment


                  #9
                  Hello rtwave,

                  Thanks for your reply.

                  "i am missing that strange code where one can get nt to do some operations as many times as there are available observations if the available observations are less than the period, and as many times as period if period is equal or larger than available observations."

                  You do not need any additional code as the average is calculated according to the daycounter which increments each time.



                  Comment


                    #10





                    PaulH, people with nt,



                    there is indeed an issue with the number of periods the platform will calculate this indicator over. in the case of the code and the image that i posted, ¿the sum and count correspond to how many periods? i loaded a little more than a calendar year to the chart, so it seems to me like the indicator just included all observations in the chart.


                    when i mentioned strange code i was referring to fragments like these:


                    fastK[0] = CurrentBar == 0 ? 50 : fastK[1];

                    wsum = priorWsum - (CurrentBar >= Period ? priorSum : 0) + myPeriod * Input[0];
                    sum = priorSum + Input[0] - (CurrentBar >= Period ? Input[Period] : 0);

                    Value[0] = (CurrentBar == 0 ? Input[0] : Input[0] * constant1 + constant2 * Value[1]);


                    there is nothing like these conditions that determine how far back the calculations should go in this indicator in progress.




                    and related to this same subject, ¿could it be possible to have the platform calculate the average range for all the appropriate observations between a start and an end date? this would be helpful so that the average range only for tuesdays would be perfectly comparable to any other average range for other days if they had the same start and end dates.



                    i imagine that the logic would be something like:


                    //for all dates between start date and end date or date of today


                    if (Time[0].DayOfWeek == DayOfWeek.Tuesday)

                    {

                    cumulativeRange += (High[0] - Low[0]);
                    dayCounter ++;
                    currentAverage = (cumulativeRange / dayCounter);

                    }

                    Value[0] = currentAverage; }



                    very well, thanks, regards.

                    Comment


                      #11
                      Hello rtwave,

                      Thanks for your reply.

                      "there is indeed an issue with the number of periods the platform will calculate this indicator over. in the case of the code and the image that i posted, ¿the sum and count correspond to how many periods? i loaded a little more than a calendar year to the chart, so it seems to me like the indicator just included all observations in the chart."

                      No, there is no issue. I recommend that you spend print out the results so that you can review what data is actually being processed and what the results are, step by step.

                      The "strange code" is not needed to determine the average. The "strange" code merely provides an adjustable "count" to divide the accumulated data up to the point where the count is actually equal or greater than the period. The "strange" code is from a fixed period average. So this has nothing to do with the code provided to you because the accumulated value is only divided by the count as the count increases.

                      Yes, you can do that same thing with a specific start and stop date. If you are unfamiliar with working with dates, please see this sample: https://ninjatrader.com/es/support/h...me_objects.htm

                      If you would like something created for you we can provide a reference to 3rd party programmers in the NinjaTrader Ecosystem who can provide what you need.

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                      0 responses
                      599 views
                      0 likes
                      Last Post Geovanny Suaza  
                      Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                      0 responses
                      344 views
                      1 like
                      Last Post Geovanny Suaza  
                      Started by Mindset, 02-09-2026, 11:44 AM
                      0 responses
                      103 views
                      0 likes
                      Last Post Mindset
                      by Mindset
                       
                      Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                      0 responses
                      558 views
                      1 like
                      Last Post Geovanny Suaza  
                      Started by RFrosty, 01-28-2026, 06:49 PM
                      0 responses
                      557 views
                      1 like
                      Last Post RFrosty
                      by RFrosty
                       
                      Working...
                      X