• 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

Previous Days ATR Value

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

    Previous Days ATR Value

    I am trying to capture the 14 day ATR value as of yesterdays close

    I am running a 5 minute chart

    I tried using barsarry

    Add(PeriodType.Day, 1);
    double AtrValue = (ATR(BarsArray[1], 14)[1]);

    Strategy stops returning any trades when I add this.

    I am guessing it is looking at today and since today is not complete there is no value to return.

    When I change Day to Minute it works fine except that doesn't return the value I need.

    I want to use the value in my calculation for a stop loss.

    Any help with the coding would be much appreciated.

    #2
    delta20, when you add the series into your script, do you filter then for the appropridate BarsInProgress in OnBarUpdate() to execute your trades?

    Would you see the same outcome if you added a 1440 min series instead?
    BertrandNinjaTrader Customer Service

    Comment


      #3
      Originally posted by delta20 View Post
      I am trying to capture the 14 day ATR value as of yesterdays close

      I am running a 5 minute chart

      I tried using barsarry

      Add(PeriodType.Day, 1);
      double AtrValue = (ATR(BarsArray[1], 14)[1]);

      Strategy stops returning any trades when I add this.

      I am guessing it is looking at today and since today is not complete there is no value to return.

      When I change Day to Minute it works fine except that doesn't return the value I need.

      I want to use the value in my calculation for a stop loss.

      Any help with the coding would be much appreciated.
      Have you got a check for the validity of the BarsArray?
      What is the error in your log, if any?

      Comment


        #4
        I do not have a filter to check for a value. I am new to barsarray and I thought I read it had to be in the initialize section. What was happening was I just wasn't returning any results on the strategy. When I moved the ATR variable into on bar update as Bertrand suggested I started getting returns on the strategy. However I am getting the 5 minute value of the ATR not the daily value.
        I believe if I use 1440 that would still give me the average of the minute bar not the daily bars.

        Here is the current code returning 5 minute values which is what I am using in the strategy

        protectedoverridevoid Initialize()
        {

        Add(PeriodType.Minute,
        1);
        Add(PeriodType.Day,
        1);
        ClearOutputWindow();

        CalculateOnBarClose =
        true;
        }
        ///<summary>
        /// Called on each bar update event (incoming tick)
        ///</summary>
        protectedoverridevoid OnBarUpdate()
        {
        double AtrValue = (ATR(BarsArray[2], 14)[1]);
        Print (
        "ATR");
        Print (AtrValue);

        Thanks

        Comment


          #5
          I had to add some CurrentBars check to get your code to work, but after adding this, I get the previous daily ATR as expected:

          Code:
          			protected override void Initialize()
          			{
          
          				Add(PeriodType.Minute, 1);
          				Add(PeriodType.Day, 1);
          				ClearOutputWindow();
          
          				CalculateOnBarClose = true;
          			}
          			///<summary>
          			/// Called on each bar update event (incoming tick)
          			///</summary>
          			protected override void OnBarUpdate()
          			{
          				
          				if(CurrentBars[0] < 1 || CurrentBars[1] < BarsRequired || CurrentBars[2] < BarsRequired) 
          					return;
          				
          				double AtrValue = (ATR(BarsArray[2], 14)[1]);
          				Print ("ATR");
          				Print (AtrValue);
          			}
          This gives me ATR 16.3619275145877 which matches what loads on a chart on the ES 12-12 for Dec 10th.

          Please let me know if you do not get the same.
          MatthewNinjaTrader Product Management

          Comment


            #6
            Thanks I will give it a try

            Couple of questions if you do not mind how do you create this symbol ||

            This logic works like this?

            currentbar(0), (1),(2) refers to the barsarray 0 being the primary time frame on the chart

            Bars required is referenced from the GUI bars required - Does this value pull from the primary time frame (5 minute chart) or the largest time period being daily in this case.

            So if bars required is set to 20 would it be 20 5 minute bars or 20 daily bars

            return; keeps if from moving further down the code in the on bar update until the condition is met

            This information will definitely help take me to the next level much appreciated

            Comment


              #7
              Originally posted by delta20 View Post
              Thanks I will give it a try

              Couple of questions if you do not mind how do you create this symbol ||
              This is the OR operator and it's made by using two "pipe" symbols, which is done by pressing SHIFT + Backslash on your keyboard

              Originally posted by delta20 View Post

              This logic works like this?

              currentbar(0), (1),(2) refers to the barsarray 0 being the primary time frame on the chart
              Correct - then 1 is the first Added series, 2 is the second, etc

              In your case CurrentBars[1] is the 1 minute, CurrentsBars[2] is the Daily. More information on CurrentBars can be found below:

              http://www.ninjatrader.com/support/h...urrentbars.htm



              Originally posted by delta20 View Post

              Bars required is referenced from the GUI bars required - Does this value pull from the primary time frame (5 minute chart) or the largest time period being daily in this case.

              So if bars required is set to 20 would it be 20 5 minute bars or 20 daily bars
              Depends if you're working in an indicator or a strategy. With an indicator, Bars required is the number of bars on a chart required before the indicator plots. We do this in an multi-time frame indicator to ensure each added bars series has enough data to calculate and run the OnBarUpdate method, otherwise and error will be produced.

              http://www.ninjatrader.com/support/h...rsrequired.htm

              But when working with a strategy, it is set through the UI:

              http://www.ninjatrader.com/support/h...srequired2.htm


              Originally posted by delta20 View Post

              return; keeps if from moving further down the code in the on bar update until the condition is met
              Exactly.

              Originally posted by delta20 View Post
              This information will definitely help take me to the next level much appreciated
              I'm happy to Help. I'd also suggest studying the information on the following link:

              http://www.ninjatrader.com/support/h...nstruments.htm
              MatthewNinjaTrader Product Management

              Comment


                #8
                Originally posted by NinjaTrader_Matthew View Post
                I had to add some CurrentBars check to get your code to work, but after adding this, I get the previous daily ATR as expected:

                Code:
                            protected override void Initialize()
                            {
                
                                Add(PeriodType.Minute, 1);
                                Add(PeriodType.Day, 1);
                                ClearOutputWindow();
                
                                CalculateOnBarClose = true;
                            }
                            ///<summary>
                            /// Called on each bar update event (incoming tick)
                            ///</summary>
                            protected override void OnBarUpdate()
                            {
                                
                                if(CurrentBars[0] < 1 || CurrentBars[1] < BarsRequired || CurrentBars[2] < BarsRequired) 
                                    return;
                                
                                double AtrValue = (ATR(BarsArray[2], 14)[1]);
                                Print ("ATR");
                                Print (AtrValue);
                            }
                This gives me ATR 16.3619275145877 which matches what loads on a chart on the ES 12-12 for Dec 10th.

                Please let me know if you do not get the same.
                Hi, after readin this post I also have the same problem. However I do not have three data series.

                I am using a 5min for primary and 1day for BarsArray[1].

                What code would I need to carry out the same result? I need to get the ATR from the daily chart while my primary is 5min chart.

                Thanks

                Comment


                  #9
                  twitch, then you would simply calculate the ATR on the BarsArray[1] which is your daily series. Of course the CurrentBars check would need to be shortened as well to not include the other series.
                  BertrandNinjaTrader Customer Service

                  Comment


                    #10
                    Hi,

                    I use:

                    atrDailyValue = ATR(BarsArray[1], 14)[0];
                    Print(atrDailyValue);

                    to give me the ATR of the daily, however it is not correct. The 6E daily ATR is 0.00679 and the ATR value I get is 0.00289.

                    The current bars code above. The way I am thinking, if you say currentBars(2) < BarsRequired will also go to return.

                    I printed CurrentBars(1); in my code and the value is zero. This makes sense because on the daily chart there has been no bars closed yet.

                    Can you show me the code I would need please.

                    Comment


                      #11
                      twitch, do you want to access the ATR value of the currently developing (so non closed daily candle)? Then you would need to let your script run with CalculateOnBarClose set to false here.

                      http://www.ninjatrader.com/support/h...onbarclose.htm
                      BertrandNinjaTrader Customer Service

                      Comment


                        #12
                        Originally posted by twitch View Post
                        Hi,

                        I use:

                        atrDailyValue = ATR(BarsArray[1], 14)[0];
                        Print(atrDailyValue);

                        to give me the ATR of the daily, however it is not correct. The 6E daily ATR is 0.00679 and the ATR value I get is 0.00289.

                        The current bars code above. The way I am thinking, if you say currentBars(2) < BarsRequired will also go to return.

                        I printed CurrentBars(1); in my code and the value is zero. This makes sense because on the daily chart there has been no bars closed yet.

                        Can you show me the code I would need please.
                        I am using this below:

                        if(CurrentBars[0] < 1 || CurrentBars[1] < BarsRequired) return;

                        atr = ATR(BarsArray[1],14)[0];
                        Print(atr);


                        However the CurrentBars[1] < BarsRequired will always go to return.

                        Since on the daily it gives a vaule as zero.

                        Comment


                          #13
                          Originally posted by NinjaTrader_Bertrand View Post
                          twitch, do you want to access the ATR value of the currently developing (so non closed daily candle)? Then you would need to let your script run with CalculateOnBarClose set to false here.

                          http://www.ninjatrader.com/support/h...onbarclose.htm
                          Hi Bertrand,

                          No I want to access the daily ATR value of yesterday please.

                          Comment


                            #14
                            How many bars / days are you loading then on the chart where you apply the strategy? Enough to satisfy the BarsRequired for all series?
                            BertrandNinjaTrader Customer Service

                            Comment


                              #15
                              Hi,
                              I can't beleive it was that simple! The code was correct, however I only loaded 5 days to check the output window. The BarRequired needs minimum 20 days. Hence the problem.

                              All is working now Bertrand. Thanks for your help!!

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by PG7940, Today, 04:26 AM
                              0 responses
                              2 views
                              0 likes
                              Last Post PG7940
                              by PG7940
                               
                              Started by marcus2300, Today, 02:25 AM
                              0 responses
                              2 views
                              0 likes
                              Last Post marcus2300  
                              Started by drpapp, 01-24-2019, 06:57 AM
                              5 responses
                              69 views
                              0 likes
                              Last Post pjsmith
                              by pjsmith
                               
                              Started by plydcv, Today, 12:40 AM
                              0 responses
                              3 views
                              0 likes
                              Last Post plydcv
                              by plydcv
                               
                              Started by Sh00tHerMcGavin, Yesterday, 07:39 PM
                              0 responses
                              4 views
                              0 likes
                              Last Post Sh00tHerMcGavin  
                              Working...
                              X