• 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

ATR Indicator Method

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

    ATR Indicator Method

    Hello NT Forum,

    I would like to use an ATR indicator method as part of a strategy on a 5 minute chart. However, I need the ATR to return the average true range of a certain number of daily bars (and not 5 min bars). Looking at the following example copied from the manual, it appears this will only return information from 5 minute bars:

    // Prints the current value of a 20 period ATR using
    default price type
    double
    value = ATR(20)[0];
    Print("The current ATR value is " + value.ToString

    ());

    Is there a way to do this without having to calculate how many 5 minute bars there are in a day per security?

    Thanks,
    Gennaker

    #2
    Hello Gennaker,
    You can create an multi-timeframe strategy to calculate the values of the ATR based on the daily bars.

    For more details please refer to here http://www.ninjatrader.com/support/h...nstruments.htm.

    A sample code will look like:
    in Initialize section of the code
    Code:
    Add(PeriodType.Day, 1);
    http://www.ninjatrader.com/support/h....html?add3.htm

    Calculate the Atr based on the daily series as
    Code:
    double atrDailyValue = ATR(Closes[1], 14)[0];
    http://www.ninjatrader.com/support/h...tml?closes.htm
    http://www.ninjatrader.com/support/h...inprogress.htm

    Please let me know if I can assist you any further.
    Joydeep M.NinjaTrader Customer Service

    Comment


      #3
      Thanks Joydeep,

      I got the following errors when I added both the sample codes. I placed both code snippets under the Initialize() section. Not sure what happened:

      CS0120 An object reference is required for the non-static field, method or property for 'Ninjatrader.Strategy.StrategyBase.Closes.get'

      CS1520 Method must have return type

      CS1001 Identifier expected

      CS1031 Type expected

      Class member declaration expected.

      Would appreciate your advice.

      Regards,
      Gennaker

      Comment


        #4
        Hello Gennaker,
        You need to place only the Add code in the Initialize portion of the code.
        Code:
        Add(PeriodType.Day, 1);
        The code will add a daily bar series
        http://www.ninjatrader.com/support/h....html?add3.htm

        Once you have the daily bar series added, you can call it from the OnBarUdpate method via the below code to calculate the ATR from the daily bar series.
        Code:
        double atrDailyValue = ATR(Closes[1], 14)[0];
        http://www.ninjatrader.com/support/h...tml?closes.htm

        If you can upload a sample strategy depicting the issue or email it to support[AT]ninjatrader[DOT]com I can look into it. If you mail it please add Attn:Joydeep in the subject line of the email and give reference of this thread in the body of the email.

        Also please refer to the SampleMultiTimeFrame strategy that comes with NinjaTrader to get further reference on how to calculate an indicator value based on a different bar series.
        In Control Center menu bar goto Tools>Edit NinjaScript>Strategies...
        In the Strategies dialog select SampleMultiTimeframe and click the Ok button



        I would also suggest you first read how a multi-series indicator works before code it. Please refer here and understand how multi-series indicator works
        http://www.ninjatrader.com/support/h...nstruments.htm

        Please let me know if I can assist you any further.
        Joydeep M.NinjaTrader Customer Service

        Comment


          #5
          Joydeep, I sent you an email with my sample script.

          Regards, Gennaker

          Comment


            #6
            Adam / Joydeep,

            Were you able to see what was generating the errors from lines 59 and 113 or do I need to find another way to send the script?

            Regards,
            Gennaker

            Comment


              #7
              Hello Gennaker,
              I have received your mail and have replied to it.

              Please let me know if I can assist you any further.
              Joydeep M.NinjaTrader Customer Service

              Comment


                #8
                It works. Thanks Joydeep.

                Comment


                  #9
                  Hi Joydeep, I have a question as well related to ATR. I'm using an a full day chart but wanted to calculate the ATR/Daily Range from say 9.30 am (the opening of the retail session). For example I can do something like this to calculate bars since the beginning for the session. Because I'm using an extended hours chart it won't calculate from the RTH. Any ideas how to modify this to calculate from a certain time would be appreciated:
                  PHP Code:
                    protected override void OnBarUpdate()
                          {
                              
                  double ATRdiff ATR (14)[0];
                              
                  double HOD MAX(HighBars.BarsSinceSession)[0];
                              
                  double LOD MIN(LowBars.BarsSinceSession)[0];
                              
                              
                              if ((
                  HOD LOD)/ATRdiff 0.8)
                              
                              {    
                                  
                  DrawTextFixed("shortpivot1","ATR %:  "+(((HOD LOD)*100)/ATRdiff).ToString("N0"), tPosition,Color.White,textFontMedColor.BlackColor.Red10);
                              }
                              
                              else if 
                                  
                              ((
                  HOD LOD)/ATRdiff 0.8)
                                  {    
                                  
                  DrawTextFixed("shortpivot1","ATR %:  "+(((HOD LOD)*100)/ATRdiff).ToString("N0"), tPosition,Color.White,textFontMedColor.BlackColor.Green10);
                              }
                              
                              
                  DrawTextFixed("shortpivot3","ATR:  "+ATRdiff.ToString("N1"), blPosition,Color.White,textFontMedColor.BlackColor.Green10);    
                              
                                  
                          } 



                  Thanks in advance
                  DJ

                  Comment


                    #10
                    Hello djkiwi,
                    You can timefilter your code. Please refer to this sample code which demonstrates how to filter your code based on a certain time
                    http://ninjatrader.com/support/forum...ead.php?t=3226

                    Also, you cannot use the Bars.BarsSinceSession method. Instead you can use the Bars.GetBar() method to calculate the HOD and LOD.
                    http://www.ninjatrader.com/support/h...tml?getbar.htm

                    Please refer to this sample code which demonstrates it
                    http://ninjatrader.com/support/forum...ad.php?t=19176
                    Joydeep M.NinjaTrader Customer Service

                    Comment


                      #11
                      Thanks Joydeep, nearly there I think. What I'm trying to do now is try and define the start hour and minute in the Variables field but get an error. For example this works ok...

                      PHP Code:
                      int daybarsAgo CurrentBar-Bars.GetBar(new DateTime(Time[0].YearTime[0].MonthTime[0].Day6300)); 
                      but this doesn't when I define it in the Variables section:

                      PHP Code:

                      Variables

                      private int startHour 6// Default setting for StartHour
                      private int startMinute 30// Default setting for StartMinute


                       
                      int daybarsAgo CurrentBar-Bars.GetBar(new DateTime(Time[0].YearTime[0].MonthTime[0].DayStartHourStartMinute0)); 
                      The error I get is The StartHour and Start Minute does not exist in the current context....

                      Any ideas on this one?
                      Thanks
                      DJ

                      Comment


                        #12
                        Hello djkiwi,
                        You have used StratHour and StartMinute instead of stratHour and strartMinute. C# is case sensitive.

                        Please try the below code
                        Code:
                        int daybarsAgo = CurrentBar-Bars.GetBar(new DateTime(Time[0].Year, Time[0].Month, Time[0].Day, startHour, startMinute, 0));

                        Please let me know if I can assist you any further.
                        Joydeep M.NinjaTrader Customer Service

                        Comment


                          #13
                          Thanks Joydeep. What I'm trying to do now is obtain the High and Low between the start of yesterday's retail session and the current time today. I am doing this to capture the time frame.

                          PHP Code:
                          int daybarsAgo CurrentBar-Bars.GetBar (new DateTime(Time[0].YearTime[0].MonthTime[0].Day-16300)); 
                          And doing this to capture the High and low of the day:

                          PHP Code:
                          double HOD MAX(HighdaybarsAgo)[0];
                                      
                          double LOD MIN(LowdaybarsAgo)[0]; 

                          The HOD is giving me yesterdays HOD and the LOD is giving me what looks like 3 days ago the low of the day. Any ideas on this one.

                          Cheers
                          DJ

                          Comment


                            #14
                            Hello DJ,
                            You should subtract a date like


                            Code:
                            int daybarsAgo = CurrentBar-Bars.GetBar (new DateTime(Time[0].Year, Time[0].Month, Time[0].Date.AddDays(-1).Day, 6, 30, 0));

                            Please let me know if I can assist you any further.
                            Joydeep M.NinjaTrader Customer Service

                            Comment


                              #15
                              Thanks Joydeep, still having lots of problem with this, not sure why wrong values are returned. I'm trying to go back to basics. How for example would I find out the highest high and the lowest low on the entire chart?

                              So instead of this:

                              //double HOD = MAX(High, Bars.BarsSinceSession)[0];
                              //double LOD = MIN(Low, Bars.BarsSinceSession)[0];

                              It would be something different. I tried this:

                              double HOD = MAX(High, CurrentBar)[0];
                              double LOD = MIN(Low, CurrentBar)[0];

                              But that just gave the first bar of the chart. I thought currentBar would be the Close[0] bar?


                              Thanks
                              DJ

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by TazoTodua, Today, 12:21 AM
                              0 responses
                              7 views
                              0 likes
                              Last Post TazoTodua  
                              Started by U0000999, Today, 12:20 AM
                              0 responses
                              7 views
                              0 likes
                              Last Post U0000999  
                              Started by sidviciousX, Yesterday, 06:47 AM
                              1 response
                              21 views
                              0 likes
                              Last Post bltdavid  
                              Started by Mykro, Yesterday, 10:22 PM
                              0 responses
                              10 views
                              0 likes
                              Last Post Mykro
                              by Mykro
                               
                              Started by rudccv, Yesterday, 08:42 PM
                              0 responses
                              6 views
                              0 likes
                              Last Post rudccv
                              by rudccv
                               
                              Working...
                              X