本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © chikaharu
//@version=6
indicator(title = 'Trend Volatility Index (TVI)', overlay = false)
// ====== Source Input ====== //
src = input.source(close, title = 'Base Price')
tickStep = input.float(1.0, title = "Step Factor",tooltip = "Rounds output to nearest price step. Use tick size as a guide.\n出力を価格ステップに丸めます。ティックサイズを目安に。")
mode = input.string("Candle",options = ["Candle","Heikin-Ashi"])
colorMode = input.string("Candle",options = ["Candle","Heikin-Ashi"],tooltip = "Notice:Heikin-Ashi lagged detected color change Reversal Point.\n注意:平均足だとリバーサルポイントの検出がが遅延します")
ATR_len = input.int(14)
Range_len = input.int(14)
// ====== sma Periods for TVI Calculation ====== //
sma1 = ta.sma(src, 10) // Approx. sma 8
sma4 = ta.sma(src, 20) // Approx. sma 16
sma6 = ta.sma(src, 40) // Approx. sma 31
sma9 = ta.sma(src, 70) // sma 65
// ====== Scatter Score Calculation ====== //
n = 1
abs_diff_1_4 = math.abs(math.pow(sma1 - sma4, n))
abs_diff_1_6 = math.abs(math.pow(sma1 - sma6, n))
abs_diff_1_9 = math.abs(math.pow(sma1 - sma9, n))
abs_diff_4_6 = math.abs(math.pow(sma4 - sma6, n))
abs_diff_4_9 = math.abs(math.pow(sma4 - sma9, n))
abs_diff_6_9 = math.abs(math.pow(sma6 - sma9, n))
// === U統計量のgini平均差で散らばり具合をノンパラメトリックに定義 === //
gini_mean_diff = (abs_diff_1_4 + abs_diff_1_6 + abs_diff_1_9 + abs_diff_4_6 + abs_diff_4_9 + abs_diff_6_9) / 6
TVI = gini_mean_diff
lowerTVI= math.floor(TVI/tickStep)*tickStep
upperTVI= math.ceil(TVI/tickStep)*tickStep
// ====== Reference Indicators ====== //
atr = ta.atr(ATR_len)
hl_range = ta.sma(high - low, Range_len)
// ====== Plot Outputs ====== //
// plot(lowerTVI, title = 'TVI', color = color.new(color.purple, 0), linewidth = 1)
// plot(upperTVI, title = 'TVI', color = color.new(color.purple, 0), linewidth = 1)
// plot(TVI,'raw TVI',color.new(color.purple,50),1,plot.style_line)
plot(atr, title = 'ATR (28)', color = color.new(color.red,70), linewidth = 1,style = plot.style_area)
plot(hl_range, title = 'High-Low Range MA (28)', color =color.new(color.black,80), linewidth = 1,style = plot.style_area)
cl = TVI
op = TVI[1]
hi = math.max(op, upperTVI, cl)
lo = math.min(op, lowerTVI, cl)
// plotcandle(op,hi,lo,cl,"TVI candle chart Market Price scale",color = (op>cl?color.red:color.green))
var float ha_op = na
var float ha_cl = na
// 最初のバーだけ初期化
if na(ha_op)
ha_cl := (op + hi + lo + cl) / 4
ha_op := (op + cl) / 2
else
ha_op := (ha_op[1] + ha_cl[1]) / 2
ha_cl := (op + hi + lo + cl) / 4
ha_hi = math.max(hi, ha_op, ha_cl)
ha_lo = math.min(lo, ha_op, ha_cl)
float ca_op = na
float ca_hi = na
float ca_lo = na
float ca_cl = na
if mode == "Candle"
ca_op := op
ca_hi := hi
ca_lo := lo
ca_cl := cl
else
ca_op := ha_op
ca_hi := ha_hi
ca_lo := ha_lo
ca_cl := ha_cl
color candleColor = na
if colorMode == "Candle"
candleColor := cl > op ? #009076 : #F8213D
else
candleColor := ha_cl > ha_op ? #009076 : #F8213D
plotcandle(ca_op,ca_hi,ca_lo,ca_cl,"TVI candle chart Market Price scale",color = candleColor,wickcolor = candleColor,bordercolor = candleColor)