Historical Volatility Percentile For ThinkOrSwim

samer800

Moderator - Expert
VIP
Lifetime
converted and modified from TV.

kBga2Ea.png


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:

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

#// 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) then 0 else hv) < 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
Hello @samer80 , nice work! What indicator do you have on the upper chart? Mostly interested in the one that is trailing the price, turning from red to green.. can you share the link to that?
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
338 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top