Volatility Cycle Breakout for TOS


New member
  1. Thinkorswim
#PRC_Volatility Cycle Breakout | indicator

#The Volatility Cycle Breakout indicator draws range boxes overlaid on the price chart.
#The upper and lower side of the boxes are made from recent Fractals.
#New boxes will be drawn only if the volatility is weak and
#if the new fractals discovered are far enough from the current upper and lower ones.
#The volatility cycle is extracted from Bollinger Bands (InpBandsPeriod=18 is the bands period).
#Fractals lookback periods can be adapted to catch new fractals far in the history
#or near of the current price (cp=10).
#The change variable is the percent variation needed to change the support and resistance
#levels of the boxes (0.1% seems to work fine for intraday timeframes on forex pairs for instance).
#This setting is important because it will reduce the noises of moving boxes each
#time new Fractals are discovered, specially when price is ranging. It results
#erfect range boxes on price to play mean reversion strategy when price is not moving enough
#to make real breakout.


#2019.11.03 @diazlaz Initial Port

declare upper;

input InpBandsPeriod = 18; #Period
input Smooth = 2; #Smoothness
input cp = 10; #Fractals periods
input change = 0.1; #Percent change to modify the upper/lower channel
input showArrows = no; # price cross channels signals

def StdDev = StDev(close, InpBandsPeriod);

def highindex = Highest(StdDev, InpBandsPeriod)[1];
def lowindex = Lowest(StdDev, InpBandsPeriod)[1];

def VolDer = (StdDev - highindex) / (highindex - lowindex);
def VolSmooth = Average(VolDer, Smooth);

def VolSmooth1 = if VolSmooth > 0 then 0 else
if VolSmooth < -1.0 then -1.0 else VolSmooth;

def LH = if high[cp] >= Highest(high, (2 * cp + 1)) then 1 else 0;
def LL = if low[cp] <= Lowest(low, (2 * cp + 1)) then -1 else 0;

def TOPy = if LH == 1 then high[cp] else TOPy[1];
def BOTy  = if LL == -1 then low[cp] else BOTy[1];

def upperchannel = if VolSmooth1 == -1.0 then
if AbsValue(TOPy - CompoundValue(1, upperchannel[1], 0)) / close > change / 100
then TOPy else upperchannel[1]
else upperchannel[1];

def lowerchannel  = if VolSmooth1 == -1.0 then
if AbsValue(BOTy - CompoundValue(1, lowerchannel[1], 0)) / close > change / 100
then BOTy else lowerchannel[1]
else lowerchannel[1];

plot pUpperChannel = upperchannel;

plot pLowerChannel = lowerchannel;

plot pUP = close crosses above lowerchannel;

plot pDown = close crosses below upperchannel;

AddCloud(pUpperChannel, pLowerChannel,COLOR.GRAY,COLOR.GRAY, yes);

#END OF PRC_Volatility Cycle Breakout | indicator
Last edited by a moderator: