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

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);


    Calculate the Atr based on the daily series as
    Code:
    double atrDailyValue = ATR(Closes[1], 14)[0];



    Please let me know if I can assist you any further.
    JoydeepNinjaTrader 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


        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];


        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


        Please let me know if I can assist you any further.
        JoydeepNinjaTrader 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.
              JoydeepNinjaTrader 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


                    Also, you cannot use the Bars.BarsSinceSession method. Instead you can use the Bars.GetBar() method to calculate the HOD and LOD.


                    Please refer to this sample code which demonstrates it
                    JoydeepNinjaTrader 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.
                        JoydeepNinjaTrader 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, [B]Time[0].Date.AddDays(-1).Day[/B], 6, 30, 0));

                            Please let me know if I can assist you any further.
                            JoydeepNinjaTrader 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 kevinenergy, 02-17-2023, 12:42 PM
                              118 responses
                              2,778 views
                              1 like
                              Last Post kevinenergy  
                              Started by briansaul, Today, 05:31 AM
                              0 responses
                              9 views
                              0 likes
                              Last Post briansaul  
                              Started by traderqz, Yesterday, 12:06 AM
                              11 responses
                              28 views
                              0 likes
                              Last Post NinjaTrader_Gaby  
                              Started by PaulMohn, Today, 03:49 AM
                              0 responses
                              9 views
                              0 likes
                              Last Post PaulMohn  
                              Started by inanazsocial, Today, 01:15 AM
                              1 response
                              12 views
                              0 likes
                              Last Post NinjaTrader_Jason  
                              Working...
                              X