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

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:





              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.



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




              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:

              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.

                      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 CortexZenUSA, Today, 12:53 AM
                              0 responses
                              1 view
                              0 likes
                              Last Post CortexZenUSA  
                              Started by CortexZenUSA, Today, 12:46 AM
                              0 responses
                              1 view
                              0 likes
                              Last Post CortexZenUSA  
                              Started by usazencortex, Today, 12:43 AM
                              0 responses
                              5 views
                              0 likes
                              Last Post usazencortex  
                              Started by sidlercom80, 10-28-2023, 08:49 AM
                              168 responses
                              2,265 views
                              0 likes
                              Last Post sidlercom80  
                              Started by Barry Milan, Yesterday, 10:35 PM
                              3 responses
                              11 views
                              0 likes
                              Last Post NinjaTrader_Manfred  
                              Working...
                              X