Wavetrend in Dynamic Zones with Kumo Implied Volatility

AdonisWerther · study · 337 行 · 点赞 1,414 · TradingView 原页

本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。

Pine Script

//@version=5
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// The GNU General Public License can be found here
// <http://www.gnu.org/licenses/>.
//
// -----------------------------------------------------------------------------
 
 
indicator(title='Wavetrend in Dynamic Zones with Kumo Implied Volatility', shorttitle='Wavetrend', precision=0)
 
n1 = input(9, 'Wavetrend Channel Length')
n2 = input(12, 'Wavetrend Average Length')
 
ap = hlc3
esa = ta.ema(ap, n1)
d = ta.ema(math.abs(ap - esa), n1)
ci = (ap - esa) / (0.015 * d)
tci = ta.ema(ci, n2)
 
tsi1 = tci
tsi2 = ta.sma(tsi1, 4)
 
long = n1
short = n2
signal = n2
 
price = hlc3
src2 = hlc3
src = hlc3
input_src = tsi1
 
//input_src = input(close, title='Price Source')
input_lookbackbars = input.int(70, title='Lookback Bars', minval=1)
input_startbuyprobability = input(0.12, title='Start Buy Probability')
input_startsellprobability = input(0.12, title='Start Sell Probability')
 
 
dzsell(src, initvalue, lookbackbars) =>
    left = -10000.0
    right = 10000.0
    eps = 0.001
    yval = (left + right) / 2.0
    delta = yval - left
    maxsteps = 0
    for i = 0 to 99999 by 1
        if not(delta > 0.005 and maxsteps < 50)
            break
        maxsteps += 1
        count = 0
        for k = 0 to lookbackbars - 1 by 1
            if src[k] > yval
                count += 1
                count
        prob = count / lookbackbars
        if prob > initvalue + eps
            left := yval
            yval := (yval + right) / 2.0
            yval
        if prob < initvalue - eps
            right := yval
            yval := (yval + left) / 2.0
            yval
        if prob < initvalue + eps and prob > initvalue - eps
            left := yval
            yval := (yval + right) / 2.0
            yval
        delta := yval - left
        delta
    [yval, delta, maxsteps]
 
dzbuy(src, initvalue, lookbackbars) =>
    left = -10000.0
    right = 10000.0
    eps = 0.001
    yval = (left + right) / 2.0
    delta = yval - left
    maxsteps = 0
    for i = 0 to 99999 by 1
        if not(delta > 0.005 and maxsteps < 50)
            break
        maxsteps += 1
        count = 0
        for k = 0 to lookbackbars - 1 by 1
            if src[k] < yval
                count += 1
                count
        prob = count / lookbackbars
        if prob > initvalue + eps
            right := yval
            yval := (yval + left) / 2.0
            yval
        if prob < initvalue - eps
            left := yval
            yval := (yval + right) / 2.0
            yval
        if prob < initvalue + eps and prob > initvalue - eps
            right := yval
            yval := (yval + left) / 2.0
            yval
        delta := yval - left
        delta
    [yval, delta, maxsteps]
 
[sell_zone, sell_delta, sell_maxsteps] = dzsell(input_src, input_startsellprobability, input_lookbackbars)
[buy_zone, buy_delta, buy_maxsteps] = dzbuy(input_src, input_startbuyprobability, input_lookbackbars)
 
pivR = input(title='WT Pivot Lookback Right', defval=1)
pivL = input(title='WT Pivot Lookback Left', defval=20)
 
 
// Compute VPT ocillator
obvOsc = tsi1
 
obvColor = obvOsc > 0 ? color.new(color.aqua, 60) : color.new(color.orange, 60)
 
// Divergence
 
up = ta.rma(math.max(ta.change(src), 0), short)
down = ta.rma(-math.min(ta.change(src), 0), short)
lbR = input(title='Divergence Pivot Lookback Right', defval=1)
lbL = input(title='Divergence Pivot Lookback Left', defval=5)
rangeUpper = input(title='Max of Lookback Range', defval=100)
rangeLower = input(title='Min of Lookback Range', defval=2)
plotBull = input(title='Plot Bullish', defval=true)
plotHiddenBull = input(title='Plot Hidden Bullish', defval=false)
plotBear = input(title='Plot Bearish', defval=true)
plotHiddenBear = input(title='Plot Hidden Bearish', defval=false)
bearColor = color.new(color.red, 0)
bullColor = color.new(color.green, 0)
hiddenBullColor = color.new(color.aqua, 0)
hiddenBearColor = color.new(color.orange, 0)
textColor = color.new(color.white, 0)
noneColor = color.new(color.white, 100)
osc = tsi1
plFound = na(ta.pivotlow(osc, lbL, lbR)) ? false : true
phFound = na(ta.pivothigh(osc, lbL, lbR)) ? false : true
_inRange(cond) =>
    bars = ta.barssince(cond == true)
    rangeLower <= bars and bars <= rangeUpper
 
//------------------------------------------------------------------------------
// Regular Bullish
// Osc: Higher Low
 
oscHL = osc[lbR] > ta.valuewhen(plFound, osc[lbR], 1) and _inRange(plFound[1])
 
// Price: Lower Low
 
priceLL = low[lbR] < ta.valuewhen(plFound, low[lbR], 1)
bullCond = plotBull and priceLL and oscHL and plFound
 
plot(plFound ? osc[lbR] : na, offset=-lbR, title='Regular Bullish', linewidth=1, color=bullCond ? bullColor : noneColor, transp=0)
 
plotshape(bullCond ? osc[lbR] : na, offset=-lbR, title='Regular Bullish Label', style=shape.labelup, location=location.absolute, size=size.tiny, color=bullColor, textcolor=textColor, transp=0)
 
//------------------------------------------------------------------------------
// Hidden Bullish
// Osc: Lower Low
 
oscLL = osc[lbR] < ta.valuewhen(plFound, osc[lbR], 1) and _inRange(plFound[1])
 
// Price: Higher Low
 
priceHL = low[lbR] > ta.valuewhen(plFound, low[lbR], 1)
hiddenBullCond = plotHiddenBull and priceHL and oscLL and plFound
 
plot(plFound ? osc[lbR] : na, offset=-lbR, title='Hidden Bullish', linewidth=1, color=hiddenBullCond ? hiddenBullColor : noneColor, transp=0)
 
plotshape(hiddenBullCond ? osc[lbR] : na, offset=-lbR, title='Hidden Bullish Label', style=shape.labelup, location=location.absolute, size=size.tiny, color=hiddenBullColor, textcolor=textColor, transp=0)
 
//------------------------------------------------------------------------------
// Regular Bearish
// Osc: Lower High
 
oscLH = osc[lbR] < ta.valuewhen(phFound, osc[lbR], 1) and _inRange(phFound[1])
 
// Price: Higher High
 
priceHH = high[lbR] > ta.valuewhen(phFound, high[lbR], 1)
 
bearCond = plotBear and priceHH and oscLH and phFound
 
plot(phFound ? osc[lbR] : na, offset=-lbR, title='Regular Bearish', linewidth=1, color=bearCond ? bearColor : noneColor, transp=0)
 
plotshape(bearCond ? osc[lbR] : na, offset=-lbR, title='Regular Bearish Label', style=shape.labeldown, location=location.absolute, size=size.tiny, color=bearColor, textcolor=textColor, transp=0)
 
//------------------------------------------------------------------------------
// Hidden Bearish
// Osc: Higher High
 
oscHH = osc[lbR] > ta.valuewhen(phFound, osc[lbR], 1) and _inRange(phFound[1])
 
// Price: Lower High
 
priceLH = high[lbR] < ta.valuewhen(phFound, high[lbR], 1)
 
hiddenBearCond = plotHiddenBear and priceLH and oscHH and phFound
 
plot(phFound ? osc[lbR] : na, offset=-lbR, title='Hidden Bearish', linewidth=1, color=hiddenBearCond ? hiddenBearColor : noneColor, transp=0)
 
plotshape(hiddenBearCond ? osc[lbR] : na, offset=-lbR, title='Hidden Bearish Label', style=shape.labeldown, location=location.absolute, color=hiddenBearColor, size=size.tiny, textcolor=textColor, transp=0)
 
//Ichimoku
Tenkansen = (ta.highest(high, 9) + ta.lowest(low, 9)) / 2
Kijunsen = (ta.highest(high, 26) + ta.lowest(low, 26)) / 2
FutureSpanA = (Tenkansen + Kijunsen) / 2
FutureSpanB = (ta.highest(high, 52) + ta.lowest(low, 52)) / 2
 
//Implied volatility based on Kumo thickness
Kumodepth = math.abs(FutureSpanA - FutureSpanB)
 
//Normalize implied volatility
Indicator = Kumodepth
AverageIndicator = ta.sma(Indicator, 200)
StandardDeviation = ta.stdev(Indicator, 200)
UpperBand = AverageIndicator + 2 * StandardDeviation
LowerBand = AverageIndicator - 2 * StandardDeviation
NormalizedIndicator = (Indicator - LowerBand) / (UpperBand - LowerBand) * 100
 
 
// Bollinger Bands Percentage 
 
lengthBB = input.int(20, minval=1, title="Bollinger Bands Lengh")
srcBB = input(close, title="Bollinger Bands Source")
multBB = input.float(2.0, minval=0.001, maxval=50, title="Bollinger Bands StdDev")
basisBB = ta.sma(srcBB, lengthBB)
devBB = multBB * ta.stdev(srcBB, lengthBB)
upperBB = basisBB + devBB
lowerBB = basisBB - devBB
bbrBB = (srcBB - lowerBB)/(upperBB - lowerBB)
 
Perc1=bbrBB
 
 
//BB Width %
 
i_priceSrc = input.source ( close,   'BB Price Source',                                 inline='1', group='BBWP Properties')
i_bbwpLen  = input.int    ( 13,      'BB Length',       minval=1,                       inline='2', group='BBWP Properties')
i_bbwpLkbk = input.int    ( 252,     '     Lookback',  minval=1,                     inline='2', group='BBWP Properties')
i_ma1On    = input.bool   ( true,    '',                                             inline='3', group='BBWP Properties')
i_ma1Type  = input.string ( 'SMA',   'BBWP MA Type', options=['SMA', 'EMA', 'VWMA'], inline='3', group='BBWP Properties')
i_ma1Len   = input.int    ( 5,       'BB Length',       minval=1,                       inline='3', group='BBWP Properties')
 
bbwp       = ta.percentrank(ta.bbw(i_priceSrc, i_bbwpLen, 1), i_bbwpLkbk) 
c_bbwp     = bbwp >= 50 ? color.from_gradient(bbwp, 50, 100, #0AFF00, #FF2900): color.from_gradient(bbwp, 0, 49, #0000FF, #0AFF00)
bbwpMA1    = i_ma1On ? i_ma1Type == 'VWMA' ? ta.vwma ( bbwp, i_ma1Len ) : i_ma1Type == 'EMA' ? ta.ema ( bbwp, i_ma1Len ) : ta.sma ( bbwp, i_ma1Len ) : na
 
//p_bbwp     = plot ( bbwp,    'BBWP',    c_bbwp,  2, plot.style_line, editable=false )
//p_ma1      = plot ( bbwpMA1, 'BBWP MA', #FFFFFF, 1, plot.style_line, 0 )
 
 
// Plots
 
labelwhere = input.int(20, title="Where to Draw the Label 1")
labelwhere2 = input.int(50, title="Where to Draw the Label 2")
 
perccolor = bbrBB < 0.5 and bbrBB > 0 ? color.new(color.aqua,50) : bbrBB < 0 ? color.new(color.green,50) : bbrBB > 1 ? color.new(color.red,50) : color.new(color.orange,50)
pertext = bbrBB > 1 ? "Bollinger Bands Overbought" : bbrBB < 0 ? "Bollinger Bands Oversold" : "BB"
 
if barstate.islast
    var lbl = label.new(na, na)
 
    label.set_xy(lbl, bar_index + labelwhere, input_src)
    label.set_textalign(lbl, text.align_right)
    label.set_text(lbl, str.tostring(Perc1, format.mintick)+ " " + (pertext))
    label.set_textcolor(lbl, perccolor)
    label.set_style(lbl, label.style_none)
    
 
 
 
 
if barstate.islast
    var lbl2 = label.new(na, na)
 
    label.set_xy(lbl2, bar_index + labelwhere2, 55)
    label.set_textalign(lbl2, text.align_right)
    label.set_text(lbl2, str.tostring(bbwp, format.mintick)+ " " + "%" + " " + "BB Width")
    label.set_textcolor(lbl2, color.new(color.aqua, 50))
    label.set_style(lbl2, label.style_none)
 
 
 
crossoverCond1 = ta.crossunder(tsi1, tsi2)
crossoverCond2 = ta.crossover(tsi1, tsi2)
 
upper_band = plot(sell_zone, title='Sell Zone', color=color.new(color.white, 100))
lower_band = plot(buy_zone, title='Buy Zone', color=color.new(color.white, 100))
zero1 = plot(100, title='Absolute Max', color=color.new(color.white, 100))
zero2 = plot(-100, title='Absolute Min', color=color.new(color.white, 100))
fill(lower_band, zero2, color=color.new(color.aqua, 90))
fill(upper_band, zero1, color=color.new(color.orange, 90))
plot(input_src, linewidth=1, style=plot.style_columns, color=NormalizedIndicator >= 105 ? color.new(#8d0000, 70) : NormalizedIndicator >= 90 ? color.new(color.red,70) : NormalizedIndicator >= 70 ? color.new(color.orange,70) : NormalizedIndicator >= 50 ? color.new(color.yellow,70) : NormalizedIndicator >= 20 ? color.new(color.aqua, 70) : NormalizedIndicator >= 5 ? color.new(color.silver,70) : color.new(color.white,70), title='Kumo Implied Historical Volatility')
filltype = plot(input_src, linewidth=2, title='WT Line', style=plot.style_line, color=color.new(color.aqua, 0))
plot(tsi2, linewidth=2, title='WT Signal Line', style=plot.style_line, color=color.new(color.orange, 0))
zeromarker = plot(0, linewidth=1, title='Zero', style=plot.style_line, color=color.new(color.white, 100))
hline(0, 'Zero', color.new(color.white,80), hline.style_dashed, 1)
hline(80, 'Overbought', color.new(color.orange,80), hline.style_dashed, 1)
hline(-80, 'Oversold', color.new(color.aqua,80), hline.style_dashed, 1)
 
 
PHCond = tsi1 > 10 ? ta.pivothigh(tsi1, pivL, pivR) : na
PLCond = tsi1 < -10 ? ta.pivotlow(tsi1, pivL, pivR) : na
 
vwph = ta.valuewhen(PHCond, PHCond, 0)
vwpl = ta.valuewhen(PLCond, PLCond, 0)
 
counterph = 0
counterph := ta.change(vwph < vwph[1]) ? nz(counterph[1]) + 1 : 0
 
counterpl = 0
counterpl := ta.change(vwpl > vwpl[1]) ? nz(counterpl[1]) + 1 : 0
 
sellmaybe = counterph == 1 ? color.new(color.orange,00) : color.new(color.white,00)
 
buymaybe = counterpl == 1 ? color.new(color.aqua,00) : color.new(color.white,00)
 
plotshape(PHCond, title='WT Pivot High', location=location.absolute, style=shape.labeldown, size=size.auto, color=sellmaybe)
plotshape(PLCond, title='WT Pivot Low', location=location.absolute, style=shape.labelup, size=size.auto, color=buymaybe)
 
alertcondition(PHCond, title='Pivot High', message='Pivot High {{ticker}}')
alertcondition(PLCond, title='Pivot Low', message='Pivot Low {{ticker}}')
alertcondition(crossoverCond1, title='WT Crossed Up', message='WT Cross Up {{ticker}}')
alertcondition(crossoverCond2, title='WT Crossed Down', message='WT Cross Down {{ticker}}')
alertcondition(bullCond, title='Bull Diversion', message='WT Regular Bull Div {{ticker}}')
alertcondition(bearCond, title='Bear Diversion', message='WT Regular Bear Div {{ticker}}')
alertcondition(hiddenBullCond, title='Hidden Bull Diversion', message='WT Hidden Bull Div {{ticker}}')
alertcondition(hiddenBearCond, title='Hidden Bear Diversion', message='WT Hidden Bear Div {{ticker}}')

← 返回列表