Author states: Historical Volatility Percentile tells you the percentage of the days from the past year (252 trading days) that have lower volatility than the current volatility.
original Tradingview code:
https://www.tradingview.com/script/WQEK8atJ-Historical-Volatility-Percentile-SMA/

original Tradingview code:
https://www.tradingview.com/script/WQEK8atJ-Historical-Volatility-Percentile-SMA/
CSS:
#// Copyright (c) 2019-present, Franklin Moormann (cheatcountry)
#// Historical Volatility Percentile + SMA [CC] script may be freely distributed under the MIT license.
#https://www.tradingview.com/script/QM23PXyq-BA-Historical-Volatility-Percentile-SMA/
#study(title="Historical Volatility Percentile + SMA ", overlay=false)
# Converted & Mod by Sam4Cok @ 07/2022
# minor update to fix option to select the plot type (Bar/Line)
declare lower;
input src = close; # "Source"
input bar = yes; # "Allow Bar Color Change?"
input length = 13; # "Length"
input annualLength = 252; # "Annual Length"
input MA_Length = 9; # "EMA Length
def na = Double.NaN;
script nz {
input data = 0;
input replacement = 0;
def ret_val = if IsNaN(data) then replacement else data;
plot return = ret_val;
}
#// the reason why I don't use the pinescript stddev function below is because we are doing sample std deviation not population
def r = Log(src / nz(src[1], src));
def rAvg = SimpleMovingAvg(r, length);
def hv = Sqrt(Sum(Power(r - rAvg, 2), length) / (length - 1)) * Sqrt(annualLength);
def count = fold i = 0 to annualLength - 1
with p
do p + if (if IsNaN(hv[i]) then 0 else hv[i]) < hv then 1 else 0;
def hvp = count / annualLength * 100;
def hvpSma1 = SimpleMovingAvg(hvp, length);
def hvpSma = SimpleMovingAvg(hvpSma1, length);
def srcEma = ExpAverage(src, MA_Length);
def sigUP = if hvp >= hvpSma and src >= srcEma and src >= src[1] then 1 else
if hvp >= hvpSma and src >= srcEma and src < src[1] then -1 else 0;
def sigDN = if hvp >= hvpSma and src < srcEma and src <= src[1] then 1 else
if hvp >= hvpSma and src < srcEma and src > src[1] then -1 else 0;
def ExUp = sigUP > 0 ;
def Up = sigUP < 0 ;
def ExDn = sigDN > 0 ;
def Dn = sigDN < 0 ;
def Range = src >= srcEma and !ExUp and !Up and !ExDn and !Dn ;
plot hvpma = hvpSma;
hvpma.SetDefaultColor(Color.WHITE);
hvpma.SetLineWeight(2);
plot hvpLine = hvp;
hvpLine.SetPaintingStrategy(if bar then PaintingStrategy.HISTOGRAM else PaintingStrategy.Line);
hvpLine.AssignValueColor(if ExUp then Color.GREEN else
if Up then Color.DARK_GREEN else
if ExDn then Color.RED else
if DN then Color.DARK_RED else
if Range then Color.YELLOW else Color.GRAY);
### END Code
Last edited by a moderator: