Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //@version=3
- study(title="Daily Historical Volatility StdDev Levels", shorttitle="HVStdDev", overlay = true)
- //
- // Author : JustUncleL
- //
- // Description:
- // Plots Daily Standard deviation levels on price chart based on Historical Volatility.
- // The following uses the most common approach – calculating historical volatility as
- // standard deviation of logarithmic returns, based on daily closing/settlement prices.
- // Assets: Any Currency, Commodities also on stocks, some indices.
- // Time Frames: 5min to 60min.
- // This will also work on Daily Chart, by setting "DaystoExpire" to 21
- //
- // References:
- // - How To Use Standard Deviation In Your Trading Day
- // https://www.youtube.com/watch?v=i28w9HBmJdQ&feature=youtu.be
- // - Deviation Levels Indicator
- // https://www.youtube.com/watch?v=EjHDNIKQH5k&feature=youtu.be
- // - http://www.macroption.com/historical-volatility-calculation/
- // - Historical Volatility based Standard Deviation_V2 by ucsgears
- // - Historical Volatility Strategy by Hpotter
- //
- // Reviosions:
- //
- // 30-July-2017 : Original
- //
- //
- // === INPUTS
- useDaily = input(true,title="Use Daily Data to Calculate HV, otherwise chart TF")
- //startDay = input("1700-1705",title="Time for Start of New Days Session")
- LookBack = input(21, minval=1)
- annual = input(252,minval=1)
- DaystoExpire_ = input (defval=0, minval=0, title="Calender Days to Expiry (0=Auto, default)")
- src = input(close,title="Settlement Source (close=default)")
- sLength_ = input(1,minval=1,title="Settlement Volume Weighted Average Length (1=Use end of day)")
- //
- stddev1 =input(true,title="Standard Deviation 1")
- stddev2 =input(true,title="Standard Deviation 2")
- stddev3 =input(false,title="Standard Deviation 3")
- //
- // === /INPUTs
- //
- // Function to test Bar time in Range
- timeinrange(res, sess) => not na(time(res, sess))
- // Test for new Daily Session or start of new month for Daily.
- start = security(tickerid,"D", time)
- newDay = isintraday ? hour(time)==hour(start) and minute(time)==minute(start) : dayofmonth(time)<dayofmonth(time[1])
- //newDay = isintraday ? timeinrange("1",startDay) : dayofmonth(time)<dayofmonth(time[1])
- // Calculate Annualised Volatility
- sLength = isintraday ? sLength_ : 1
- hv = 0.0
- hv_ = useDaily? security(tickerid,"D", stdev(log(src / src[1]), LookBack) * sqrt(annual)) : stdev(log(src / src[1]), LookBack) * sqrt(annual)
- hv := newDay ? hv_ : nz(hv[1],hv_)
- // get the Daily Settlement
- settlement = sLength==1? src[1] : vwma(src[1],sLength)
- settlement := newDay ? settlement : nz(settlement[1])
- //--- debug
- //plotshape(hv_,location=location.bottom)
- //plotshape(hv,location=location.bottom)
- //plotshape(newDay,location=location.bottom)
- //--- /debug
- //
- // Calculate STDdev over life of the option (generally this is one day for 5min to 60min TFs,
- // and 21 days for Daily TF)
- stdhv = 0.0
- DaystoExpire = DaystoExpire_==0? isintraday? 1 : 21 : DaystoExpire_
- stdhv := newDay? settlement*hv*sqrt(DaystoExpire/annual) : nz(stdhv[1])
- // calculate StdDev lines ratios.
- stdhv05 = stdhv * 0.5
- stdhv07 = stdhv * 0.75
- stdhv1 = stdhv
- stdhv15= stdhv*1.5
- stdhv2 = stdhv*2.0
- stdhv3 = stdhv*3.0
- // Plot the StdDev Levels.
- SettleM = plot(stddev1 ? settlement:na, color = newDay?na:purple, title = "Settlement",linewidth=4, transp=40)
- Stdhv05u = plot(stddev1 ? (settlement+stdhv05):na, color = newDay?na:orange, title = "+0.5 Stdev",linewidth=1, transp=20)
- Stdhv05d = plot(stddev1 ? (settlement-stdhv05):na, color = newDay?na:orange, title = "-0.5 Stdev",linewidth=1, transp=20)
- Stdhv07u = plot(stddev1 ? (settlement+stdhv07):na, color = newDay?na:red, title = "+0.75 Stdev",linewidth=1, transp=20)
- Stdhv07d = plot(stddev1 ? (settlement-stdhv07):na, color = newDay?na:red, title = "+0.75 Stdev",linewidth=1, transp=20)
- Stdhv1u = plot(stddev1 ? (settlement+stdhv1):na, color = newDay?na:lime, title = "+1 Stdev",linewidth=2, transp=20)
- Stdhv1d = plot(stddev1 ? (settlement-stdhv1):na, color = newDay?na:lime, title = "-1 Stdev",linewidth=2, transp=20)
- Stdhv15u = plot(stddev1 ? (settlement+stdhv15):na, color = newDay?na:green, title = "+1.5 Stdev",linewidth=1, transp=20)
- Stdhv15d = plot(stddev1 ? (settlement-stdhv15):na, color = newDay?na:green, title = "-1.5 Stdev",linewidth=1, transp=20)
- Stdhv2u = plot(stddev2 ? (settlement+stdhv2):na, color = newDay?na:blue, title = "+2 Stdev",linewidth=2, transp=20)
- Stdhv2d = plot(stddev2 ? (settlement-stdhv2):na, color = newDay?na:blue, title = "-2 Stdev",linewidth=2, transp=20)
- Stdhv3u = plot(stddev3 ? (settlement+stdhv3):na, color = newDay?na:gray, title = "+3 Stdev",linewidth=2, transp=20)
- Stdhv3d = plot(stddev3 ? (settlement-stdhv3):na, color = newDay?na:gray, title = "-3 Stdev",linewidth=2, transp=20)
- // Colour in between levels.
- fill(Stdhv3d,Stdhv2d,color=gray,transp=95,title="-3 Stdev Fill")
- fill(Stdhv3u,Stdhv2u,color=gray,transp=95,title="+3 Stdev Fill")
- fill(Stdhv2d,Stdhv15d,color=blue,transp=95,title="-2 Stdev Fill")
- fill(Stdhv2u,Stdhv15u,color=blue,transp=95,title="+2 Stdev Fill")
- fill(Stdhv15d,Stdhv1d,color=green,transp=95,title="-1.5 Stdev Fill")
- fill(Stdhv15u,Stdhv1u,color=green,transp=95,title="+1.5 Stdev Fill")
- fill(Stdhv1d,Stdhv07d,color=lime,transp=95,title="-1 Stdev Fill")
- fill(Stdhv1u,Stdhv07u,color=lime,transp=95,title="+1 Stdev Fill")
- fill(Stdhv07d,Stdhv05d,color=red,transp=95,title="-0.75 Stdev Fill")
- fill(Stdhv07u,Stdhv05u,color=red,transp=95,title="+0.75 Stdev Fill")
- fill(Stdhv05d,SettleM,color=purple,transp=92,title="-0.5 Stdev Fill")
- fill(Stdhv05u,SettleM,color=purple,transp=92,title="+0.5 Stdev Fill")
- //
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement