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

Create a Moving Average for Daily Range

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

    Create a Moving Average for Daily Range

    I am trying to write an indicator that will automatically calculate the current days range and create a 7 day moving average.

    I have found CurrentDayOHL and with this can easily calculate the range for the current day as of the last bar, however, I am having trouble creating the 7-day moving average.

    Thanks for helping the novice.

    #2
    netjms,

    To get prior day's information you will need to use PriorDayOHLC.
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      Ok. Reading about the PriorDayOHLC will get me the prior day's information, but how would I go beyond that to create a 7-day moving average?

      Thanks

      Comment


        #4
        netjms,

        You have to do the math in your code. Get the values you want for the 7 days and then run an average calculation. Then you have the information you want.
        Josh P.NinjaTrader Customer Service

        Comment


          #5
          I am ok with doing the math in code to calculate the 7-day average, what I don't understand is how retrieve the data past the prior day; consider the following...

          I use CurrentDayOHL to get current day information, then use PrioDayOHLC to get yesterday's information. I now have two of the 7 years values in which to caculate a moving average.

          How do I go beyond yesterday's values? Do I need to manipulate "barsago" or is there additional informaiton I am not getting?

          Thanks

          Comment


            #6
            sorry I meant '...7 days" not 7 years.

            Comment


              #7
              That is right. You need to manipulate the bars ago value for PriorDayOHLC().

              For instance, let us just say that 10 bars = 1 day. On the most recent bar accessing PriorDayOHLC() with index anywhere between 0 and 10 will give you yesterday's values. When you access index between 11 and 20 you get the day before that. 21 and 30 gets you yet another day earlier. etc. etc.

              You can make it easier on yourself by using GetBar() to give you which [] index to use to get a certain date.
              Josh P.NinjaTrader Customer Service

              Comment


                #8
                Ok. Back to this indicator. I have coded the following. I put the initial code in the initialize section because it really only needs to run 1 time when the indicator starts (is this the correct location?). However, I am having a problem with (what I think) GetBar. The code is returning 0 however, I have validated the barDate variable via print statements. I am entering a time of 16:00 hours because I want to get the daily range at the end each day in the loop. See the contents of the output window below too. In addition to returning zero, it appears the code stops (?) because I get no more output.

                Any input? Thanks!

                protectedoverridevoid Initialize()
                {
                CalculateOnBarClose =
                true;
                Overlay =
                false;
                PriceTypeSupported =
                false;
                //get start date to loop through
                DateTime startdate = DateTime.Now.Subtract(TimeSpan.FromDays(6));
                Print (
                "Start Date " + startdate);
                //get the prior 6 days highs & lows
                for (double i = 0; i < 6; i++)
                {
                DateTime barDate = startdate.AddDays(i);
                Print (
                "Bar Date " + barDate + " " + i);
                Print (barDate.Year +
                " " + barDate.Month + " " + barDate.Day);
                int barsAgo = GetBar(new DateTime(barDate.Year, barDate.Month, barDate.Day, 16, 0, 0));
                Print (
                "barsago is " + barsAgo);
                double dayrange = PriorDayOHLC().PriorHigh[barsAgo] - PriorDayOHLC().PriorLow[barsAgo];
                Print (
                "Day ranage is " + dayrange);
                RangeTotal = ++dayrange;
                }
                }
                protectedoverridevoid OnBarUpdate()
                {
                double dayrange = CurrentDayOHL().CurrentHigh[0] - CurrentDayOHL().CurrentLow[0];
                double avgrange = (RangeTotal + dayrange)/7;
                Print(
                "7-day Average Daily Range is " + avgrange);
                double stoploss = avgrange * 1.0;
                double target = avgrange * 1.5;
                DrawTextFixed(
                "avgdayrange","Average Daily Range = " + avgrange + "\r\n" +
                "Stop Loss = " + stoploss + "Profit Target = " + target,TextPosition.TopLeft);

                }

                Comment


                  #9
                  Forget the ouput window contents...
                  Start Date 4/18/2009 3:03:36 PM
                  Bar Date 4/18/2009 3:03:36 PM 0
                  2009 4 18
                  Start Date 4/18/2009 3:03:36 PM
                  Bar Date 4/18/2009 3:03:36 PM 0
                  2009 4 18
                  barsago is 0

                  Comment


                    #10
                    netjms,

                    You should not place any logic in Initialize(). Please move it to OnBarUpdate(). If you only want it to execute once put in within the context of if (CurrentBar == 0).
                    Josh P.NinjaTrader Customer Service

                    Comment


                      #11
                      Originally posted by netjms View Post
                      ...I am having a problem with (what I think) GetBar. ...
                      You are correct, netjms. GetBar() is a runtime-method and therfore should be executed in OnBarUpdate().

                      Regards
                      Ralph

                      Comment


                        #12
                        Thanks guys for your help. Moving the code and making come changes worked. However, I receive a 0 value for barAgo. I have reviewed the reference material online and cannot understand why. I have verified the barDate values are correct (I even hardcoded to a specific date and still got zero). My chart data has more then two weeks of data. Any ideas?

                        protectedoverridevoid OnBarUpdate()
                        {
                        if (CurrentBar == 0)
                        {
                        //get start date to loop through
                        DateTime startdate = DateTime.Now.Subtract(TimeSpan.FromDays(6));
                        //get the prior 6 days highs & lows
                        for (double i = 0; i < 6; i++)
                        {
                        DateTime barDate = startdate.AddDays(i);
                        int barsAgo = GetBar(new DateTime(barDate.Year, barDate.Month, barDate.Day, 16, 0, 0)); double dayrange = PriorDayOHLC().PriorHigh[barsAgo] - PriorDayOHLC().PriorLow[barsAgo];

                        RangeTotal = ++dayrange;
                        }
                        }

                        Comment


                          #13
                          Although CurrentBar == 0 is the correct place to initiate runtime dependent things, you can't access historical data (barDate) here. At this point in time there simply is no history available yet. You need to pass your 6 days, before you can access the history of 6 days ago.

                          Regards
                          Ralph

                          Comment


                            #14
                            I think I understand however, I have more then 6 days of history on display in the chart. I was able to print data(to the output window) from OnBarUpdate for all the days of data in the chart (roughly 2+ weeks).

                            Are you saying I cannot access history in the OnBarUpdate area?

                            Thanks

                            Comment


                              #15
                              Never mind my last post. I re-read Ralph's post and I understand. Thanks

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by rjbtrade1, 11-30-2023, 04:38 PM
                              2 responses
                              77 views
                              0 likes
                              Last Post DavidHP
                              by DavidHP
                               
                              Started by Stanfillirenfro, Today, 07:23 AM
                              3 responses
                              13 views
                              0 likes
                              Last Post NinjaTrader_ChelseaB  
                              Started by FitSpressoHonest, Today, 09:14 AM
                              0 responses
                              4 views
                              0 likes
                              Last Post FitSpressoHonest  
                              Started by Davide999, 05-18-2023, 03:55 AM
                              4 responses
                              557 views
                              1 like
                              Last Post kcwasher  
                              Started by rexsole, Today, 08:39 AM
                              2 responses
                              9 views
                              0 likes
                              Last Post NinjaTrader_Erick  
                              Working...
                              X