TSI in Dynamic Zones with Divergence

AdonisWerther · study · 248 行 · 点赞 1,321 · TradingView 原页

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

Pine Script

//@version=5
//
// A port of the MT4 indicator of Mladen Rakic found at https://www.mql5.com/en/forum/179876
// which is based on Dynamic Zones that was originally published in Stocks & Commodities 1996 issue.
// 
// Dynamic Zones is meant to be applied to oscillators to get dynamic overbought and oversold levels
// quantified using statistical methods.
//
// -----------------------------------------------------------------------------
// Copyright 2018 sherwind
//
// 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='TSI in Dynamic Zones with Divergence', shorttitle='TSI', precision=0, timeframe='')
 
 
long = input(title='TSI Long Length', defval=5)
short = input(title='TSI Short Length', defval=12)
signal = input(title='TSI Signal Length', defval=10)
treshold = input(title='TSI Crossover treshold', defval=30)
price = ohlc4
src2 = ohlc4
src = ohlc4
double_smooth(src2, long, short) =>
    fist_smooth = ta.ema(src2, long)
    ta.ema(fist_smooth, short)
pc = ta.change(price)
double_smoothed_pc = double_smooth(pc, long, short)
double_smoothed_abs_pc = double_smooth(math.abs(pc), long, short)
tsi_value = 100 * (double_smoothed_pc / double_smoothed_abs_pc)
tsi1 = tsi_value
tsi2 = ta.wma(tsi_value, signal)
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='TSI Pivot Lookback Right', defval=1)
pivL = input(title='TSI 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)
 
 
 
// Plots
 
 
crossoverCond1 = ta.crossunder(obvOsc, 0)
crossoverCond2 = ta.crossover(obvOsc, 0)
 
upper_band = plot(sell_zone, title='Sell Zone', color=color.new(color.white, 70))
lower_band = plot(buy_zone, title='Buy Zone', color=color.new(color.white, 70))
fill(upper_band, lower_band, color=color.new(color.aqua, 90))
plot(input_src, linewidth=2, style=plot.style_line, color=color.new(color.aqua, 0))
plot(tsi2, linewidth=2, style=plot.style_line, color=color.new(color.orange, 0))
//plot(00, title='Zero Line', color=color.new(color.white, 40), style=plot.style_line, linewidth=1)
 
PHCond = obvOsc > 50 ? ta.pivothigh(obvOsc, pivL, pivR) : na
PLCond = obvOsc < 50 ? ta.pivotlow(obvOsc, pivL, pivR) : na
 
plotshape(PHCond, title='TSI Pivot High', location=location.absolute, style=shape.triangledown, size=size.auto, color=color.new(color.white, 00))
plotshape(PLCond, title='TSI Pivot Low', location=location.absolute, style=shape.triangleup, size=size.auto, color=color.new(color.white, 00))
 
alertcondition(PHCond, title='Pivot High', message='Pivot High {{ticker}}')
alertcondition(PLCond, title='Pivot Low', message='Pivot Low {{ticker}}')
alertcondition(crossoverCond1, title='TSI Crossed Up', message='TSI Cross Up {{ticker}}')
alertcondition(crossoverCond2, title='TSI Crossed Down', message='TSI Cross Down {{ticker}}')
alertcondition(bullCond, title='Bull Diversion', message='TSI Regular Bull Div {{ticker}}')
alertcondition(bearCond, title='Bear Diversion', message='TSI Regular Bear Div {{ticker}}')
alertcondition(hiddenBullCond, title='Hidden Bull Diversion', message='TSI Hidden Bull Div {{ticker}}')
alertcondition(hiddenBearCond, title='Hidden Bear Diversion', message='TSI Hidden Bear Div {{ticker}}')

← 返回列表