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

A way to see average ticks movement an instrument has had during the last n days

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

    A way to see average ticks movement an instrument has had during the last n days

    Hello

    Is there any indicator or way to graphically (or numercally) see the Average of the ATR during a period of time?

    I know the ATR already in itself calculates an average, but what I need is to know the average of the ATR itself during the n selected days.

    For example, an indicator where you select n = 3 and the indicator calculates the average of the ATR subchart for the 3 last Trading days, showing its average maybe in a similar way as in the attached picture here (picture just for illustration to have an idea), or maybe the indicator could be plotted in a more adecuated way like the MA or EMA but instead to be aplied to the instrument chart, to be applied to the ATR indicador itself.

    I tried going to the site https://ninjatraderecosystem.com but I couldn’t find any similar to get this value.

    What I specifically need is to know the average in points/ticks movement an instrument has had during the last n days, and I’ve been thinking the ATR (its average) could show that value I need, but maybe you could show me others ways to be able to see this value I need with others tools.

    Thank you


    Click image for larger version

Name:	A way to calculate and show the ATR average.png
Views:	218
Size:	101.2 KB
ID:	1164359

    #2
    Hi futurenow, thanks for posting.

    The ATR value can be passed into the SMA indicator to calculate a moving average over time e.g.

    Code:
    public class SMAATR : Indicator
    {
    private ATR _ATR;
    private SMA _SMA;
    ...
    OnStateChanged:
    else if (State == State.Configure)
    {
    AddPlot(Brushes.Red, "AvgPlot");
    }
    else if (State == State.DataLoaded)
    {
    _ATR = ATR(20);
    _SMA = SMA(_ATR, 20);
    }
    
    protected override void OnBarUpdate()
    {
    Value[0] = _SMA[0];
    }
    Kind regards,
    -ChrisL
    Chris L.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_ChrisL View Post

      The ATR value can be passed into the SMA indicator to calculate a moving average over time e.g.

      Code:
      public class SMAATR : Indicator
      {
      private ATR _ATR;
      private SMA _SMA;
      ...
      OnStateChanged:
      else if (State == State.Configure)
      {
      AddPlot(Brushes.Red, "AvgPlot");
      }
      else if (State == State.DataLoaded)
      {
      _ATR = ATR(20);
      _SMA = SMA(_ATR, 20);
      }
      
      protected override void OnBarUpdate()
      {
      Value[0] = _SMA[0];
      }
      Thank you for your fast response ChrisL


      I put the code into a new indicator called 'SMAATR' and I think is working, and I would like to clarify if the 2 '20' in the code you posted is about the quantity of previous days the indicator takes to makes the calculations.

      I mean the next code portion:
      Code:
      ...
      else if (State == State.DataLoaded)
      {
      _ATR = ATR(20);
      _SMA = SMA(_ATR, 20);
      }
      ...

      Because if each one of these 20s mean 20 days, then I tried putting the code as follow below, but it simply shows what the standard ATR shows, and the Idea would be to obtain the average of the ATR in 3 previous days (based only in the 3 previous trading days), i.e. something like the result in the picture but only based in the 3 previous days. So if the ATR is an Average, then what I need is the average of the average in the 3 previous days
      Code:
      ...
      else if (State == State.DataLoaded)
      {
      _ATR = ATR(3);
      _SMA = SMA(_ATR, 3);
      }
      
      /*
         With these settings into the custom
         indicator, the custom indicator shows
         exactly what the normal ATR shows
         when the ATR is setted to 'period = 3'
      */
      ...


      As reference for your answer, please note the result I need is to know the average movement (instrument points or instrument ticks) for the selected instrument, BUT IMPORTANT, only based in the 3 previous days of market data (From Monday to Friday, excluding the Sundays).


      Thank you!


      Click image for larger version

Name:	A way to calculate and show the ATR average #2.png
Views:	213
Size:	91.2 KB
ID:	1164384
      Last edited by futurenow; 07-20-2021, 04:04 PM.

      Comment


        #4
        Hi futurenow, thanks for your reply.

        You can add the daily series and only run the logic on that daily series:

        Code:
        namespace NinjaTrader.NinjaScript.Indicators
        {
        public class SMAATR : Indicator
        {
        private ATR _ATR;
        private SMA _SMA;
        protected override void OnStateChange()
        {
        if (State == State.SetDefaults)
        {
        //...
        }
        else if (State == State.Configure)
        {
        AddPlot(Brushes.Red, "AvgPlot");
        AddDataSeries(BarsPeriodType.Day, 1);
        }
        else if (State == State.DataLoaded)
        {
        _ATR = ATR(BarsArray[1], 20); //make sure to load at least a month of data
        _SMA = SMA(_ATR, 3);
        }
        }
        
        double lastValue;
        protected override void OnBarUpdate()
        {
        if(BarsInProgress == 1)
        {
        if(CurrentBars[1] < 20) return;
        
        lastValue = _SMA[0];
        Print(_SMA[0]);
        }
        
        if(BarsInProgress == 0)
        {
        Value[0] = lastValue;
        }
        
        }
        }
        }
        Chris L.NinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_ChrisL View Post

          You can add the daily series and only run the logic on that daily series:

          Code:
          namespace NinjaTrader.NinjaScript.Indicators
          {
          public class SMAATR : Indicator
          {
          private ATR _ATR;
          private SMA _SMA;
          protected override void OnStateChange()
          {
          if (State == State.SetDefaults)
          {
          //...
          }
          else if (State == State.Configure)
          {
          AddPlot(Brushes.Red, "AvgPlot");
          AddDataSeries(BarsPeriodType.Day, 1);
          }
          else if (State == State.DataLoaded)
          {
          _ATR = ATR(BarsArray[1], 20); //make sure to load at least a month of data
          _SMA = SMA(_ATR, 3);
          }
          }
          
          double lastValue;
          protected override void OnBarUpdate()
          {
          if(BarsInProgress == 1)
          {
          if(CurrentBars[1] < 20) return;
          
          lastValue = _SMA[0];
          Print(_SMA[0]);
          }
          
          if(BarsInProgress == 0)
          {
          Value[0] = lastValue;
          }
          
          }
          }
          }

          Good morning ChrisL!

          I want to say I really appreciate you take your time to provide the main code to try to solve the situation I described in this post

          Ok, I just tried with the code you posted today and I don't know if I'm missing something important but as you can see in the capture, the Average of the ATR is showing very high levels (the lower actual result level is 40.14 for the ES) which does not correspond to the range of movement of the ES future just taking a quick visual look for what the standard ATR is showing for the last days/weeks

          I tried making some changes in the numbers into the code trying to adjust the result but what I get is a higher result level as the yellow line shows in the bottom part


          Note #1: I tried loading 30-60-90 days of data for the ES to see if maybe this change anything but the same result

          Note #2: I noticed now the custom indicator shows a straight line (increasing/decreasing) instead of the expected curve Moving Average line as the result of the 1st example you posted yesterday.

          Note 3: I still don't understand well why is the purpose to use the number '20' because the only relevant reange of data time is the last 3 days.


          Thank you very much!


          Click image for larger version

Name:	A way to calculate and show the ATR average #3.png
Views:	215
Size:	77.4 KB
ID:	1164505

          Comment


            #6
            Hi futurenow, thanks for your reply.

            These straight plot lines with sharp up/down edges are the product of calculating the average value of the ATR on daily data plotted on a shorter time frame. There are much less data points in the daily data compared to the 10 minute chart. If you switch to a Daily series this will be more evident. I picked 20 for the ATR period randomly, the period can be whatever you want. I did change the SMA to calculate 3 days hence the 3 Period value in the SMA object.

            Kind regards,
            -ChrisL
            Chris L.NinjaTrader Customer Service

            Comment


              #7
              Originally posted by NinjaTrader_ChrisL View Post
              Hi futurenow, thanks for your reply.

              These straight plot lines with sharp up/down edges are the product of calculating the average value of the ATR on daily data plotted on a shorter time frame. There are much less data points in the daily data compared to the 10 minute chart. If you switch to a Daily series this will be more evident. I picked 20 for the ATR period randomly, the period can be whatever you want. I did change the SMA to calculate 3 days hence the 3 Period value in the SMA object.

              Kind regards,
              -ChrisL

              Hello

              Thank you for the explanation ChrisL and I just would like to clarify something that is, as you can see in the attached picture in my previous reply, you can see the Orange straight line gives as its current result the number 42.72 what does not represent the average of the ATR during any of the previous past period of time, because the higher ATR result peaks are 20 or 32 points as much in the ES, so the average could be maybe around 12-15 points in the ES but not in 42.72 points

              Could we perhaps be missing anything in the calculation into the indicator logic? Because the results the indicator is giving is too high.

              Could you please take a quick look, because I’ve been some test modifying numbers and minors logic changes but it keeps providing high results

              Thank you one more time

              Comment


                #8
                Hi futurenow, thanks for your reply.

                When you apply an SMA with an ATR input series to a daily chart, it's averaging out around 41 up to today, the average value is correct.



                Kind regards,
                -ChrisL
                Chris L.NinjaTrader Customer Service

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by algospoke, Yesterday, 06:40 PM
                2 responses
                19 views
                0 likes
                Last Post algospoke  
                Started by ghoul, Today, 06:02 PM
                3 responses
                14 views
                0 likes
                Last Post NinjaTrader_Manfred  
                Started by jeronymite, 04-12-2024, 04:26 PM
                3 responses
                44 views
                0 likes
                Last Post jeronymite  
                Started by Barry Milan, Yesterday, 10:35 PM
                7 responses
                20 views
                0 likes
                Last Post NinjaTrader_Manfred  
                Started by AttiM, 02-14-2024, 05:20 PM
                10 responses
                180 views
                0 likes
                Last Post jeronymite  
                Working...
                X