Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Historic Volatility

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

    Historic Volatility

    My second indicator. Calculates historic volatility with 1 input, the period (in trading days). Gives you an annualized number that is a percentage. A result of 8 is the same as 8% historical volatility.

    Shows historic volatility as you pass the period to it.

    NOTE: This is only usable while in a minute or smaller timeframe. It collects the number based on a cumulation of intersession closes. So the period you pass to it is the number of sessions. For a more flexible volatility tool look to my HistVolAny function posted around the same time as this. Note however that this is the correct tool if you wish to refer only to the same scale of volatility as something like VIX measures (this result correlates to VIX). Useful for an intraday system that you want to control based on broad interday volatility trends.

    So HistVol(30)[0] calculates today's historic volatility based on the last 30 days of data. Historic volatility doesn't change during the day in this indicator. It is calculated once at open (since it is based off of previous market closes only). Note you need to open plenty of backfill into your charts to use this.

    I programmed this on NT 6.5, so no guarantees for 6.0. Also, any performance issues you have should probably be posted here. I'm new to writing these and have a lot to learn, so maybe progress can be made in discussion.

    Finally though, if you have a system that you want to dynamically adjust stops and profits, or just stay out, based on volatility, you now have something to work with.

    Also note that I've based this indicator off of subsequent closes, not high versus low of consecutive days. This is all customizable. I matched these results up to CBOT data for some contracts I trade, and it looks good.

    This uses natural log and standard deviation to derive historic volatility for a set of data (however long the period). The longer period should perform slower.
    Attached Files
    Last edited by scriabinop23; 02-02-2008, 11:09 AM.

    #2
    Thanks for the contribution.
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by Josh View Post
      Thanks for the contribution.
      If you don't mind, please take a look at it. Lemme know your thoughts on performance and possible ways to clean this relatively small snippet of code up.

      Comment


        #4
        I only briefly looked at your code. I didn't work through your code completely, but the thing that comes to mind is the use of Math.Sqrt(252) * 100. You could increase performance by removing the math.sqrt function and doing the multiplication out beforehand. Because, in the end, it would seem like your index doesn't really care about all the decimal places involved with a double (unless I am wrong), 2 decimal places should be plenty to determine the general volatility of the day.
        Josh P.NinjaTrader Customer Service

        Comment


          #5
          good idea. all done; original reuploaded.

          Comment


            #6
            HistVol - 1587.45

            Hello:

            Can you tell me what this number is for in the histvol code:

            histvolatile = Math.Sqrt(Math.Abs(sumsqdev/ period)) *
            1587.45;

            Whatever it is for, shouldn't it change no matter what Interval Setting (i.e., 1 min, 120 min, etc)?

            Thanks.

            Tony

            Comment


              #7
              Hi Tony,

              This code doesn't access the bar interval. This is done with Bars.Period.Id and Bars.Period.Value




              The author defined period in their original post:
              the period (in trading days).So the period you pass to it is the number of sessions.
              Ryan M.NinjaTrader Customer Service

              Comment


                #8
                It is the sqrt of 252 (number of trading days in a year) * 100. ie the output of .10 (representing 10%) is converted to 10 to denote 10%.

                This is to represent annualized volatility as the output, which is typically what is referred to in finance / options pricing.

                Comment


                  #9
                  HistVol - 1587.45

                  Hello:

                  I understand why this is done on an annualized basis for a daily chart. However, this program is designed for volatility less than a year back. So how can this number be right if I am only interested in 3 days (sessions) of historical volatility (period =3) on a 1 minute chart? If there are 420 minutes in a day, then I would want to look at 1260 minutes total.

                  Thanks.

                  Tony

                  Comment


                    #10
                    Originally posted by tonyh View Post
                    Hello:

                    I understand why this is done on an annualized basis for a daily chart. However, this program is designed for volatility less than a year back. So how can this number be right if I am only interested in 3 days (sessions) of historical volatility (period =3) on a 1 minute chart? If there are 420 minutes in a day, then I would want to look at 1260 minutes total.

                    Thanks.

                    Tony
                    (I haven't touched this in about 2 yrs, so let me work out your example and get back to you.)

                    Comment


                      #11
                      Thanks for the contribution scriabinop23. I just found this thread as I'm trying to build a HV indicator.

                      I got a bit lost in the loops in your code. Is there a simpler way to do this if it is to be used on daily bars only (say for the traditional 10-, 20-, 30- day HV)?

                      You could force the BarsPeriod to 1 day values in Initialize() and perhaps add something like this in OnStartUp():

                      PHP Code:
                      if (BarsPeriod.Value != || BarsPeriod.Id != PeriodType.Day)
                      {
                      Print(
                      DateTime.Now " ERROR! Indicator requires 1 Day bars.");
                      return; 

                      I'm in NT7, and I can't get code you posted to plot anything other than 0 when I look at, say, SPY daily bars. Thanks for your time.

                      Comment


                        #12
                        I found the HistVolAll indicator from you in another thread and that is working for me. Thanks!

                        Comment


                          #13
                          Perhaps this is more efficient?

                          PHP Code:
                          protected override void OnBarUpdate()//////////////////////////////////////////////////////////////////////
                          {
                           
                          if (
                          CurrentBar period+|| BarsPeriod.Value != || BarsPeriod.Id != PeriodType.Day)
                          {
                          Value.Set(0);
                          return;
                          }
                           
                          clLog[0] = Math.Log(Close[0]/Close[1]);
                          histvol = (StdDev(clLogperiod)[0])*(1612.45); //16.1245 is sqrt of 260, 15.8745 of 252. 19.1050 of 365. *100.
                           
                          Value.Set(histvol);
                           

                          HV and IV seem to be in the eyes of the user. Thoughts?

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by DJ888, 04-16-2024, 06:09 PM
                          6 responses
                          18 views
                          0 likes
                          Last Post DJ888
                          by DJ888
                           
                          Started by Jon17, Today, 04:33 PM
                          0 responses
                          1 view
                          0 likes
                          Last Post Jon17
                          by Jon17
                           
                          Started by Javierw.ok, Today, 04:12 PM
                          0 responses
                          6 views
                          0 likes
                          Last Post Javierw.ok  
                          Started by timmbbo, Today, 08:59 AM
                          2 responses
                          10 views
                          0 likes
                          Last Post bltdavid  
                          Started by alifarahani, Today, 09:40 AM
                          6 responses
                          41 views
                          0 likes
                          Last Post alifarahani  
                          Working...
                          X