True Strength Index with Levels

Paramkalyan · study · 76 行 · 点赞 21 · TradingView 原页

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

Pine Script

//@version=6
indicator("True strength index", shorttitle="TSI", format=format.price, precision=2)
 
// Inputs
long = input.int(title="Long Length", defval=25)
short = input.int(title="Short Length", defval=13)
signal = input.int(title="Signal Length", defval=13)
price = close
 
// Overstretched Inputs
grp_levels = "Overstretched Levels"
upperLevel = input.float(35.0, title="Upper OS Level", group=grp_levels)
lowerLevel = input.float(-35.0, title="Lower OS Level", group=grp_levels)
 
// Readout Box Inputs
grp_box = "Top Center Readout Box"
showBox = input.bool(true, title="Show Readout Box", group=grp_box)
boxSizeIn = input.string("Small", title="Box Text Size", options=["Tiny", "Small", "Normal", "Large"], group=grp_box)
 
toTableSize(sizeStr) =>
    switch sizeStr
        "Tiny"   => size.tiny
        "Small"  => size.small
        "Normal" => size.normal
        "Large"  => size.large
        => size.small
 
boxTextSize = toTableSize(boxSizeIn)
 
// Double Smoothing Function
double_smooth(src, long_len, short_len) =>
    first_smooth = ta.ema(src, long_len)
    ta.ema(first_smooth, short_len)
 
// TSI Calculations
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)
tsi_signal = ta.ema(tsi_value, signal)
tsi_gap = tsi_value - tsi_signal
 
// Core Plots (Style tab editable)
p_tsi = plot(tsi_value, title="True Strength Index", color=#2962FF, linewidth=2)
p_sig = plot(tsi_signal, title="Signal", color=#E91E63, linewidth=2)
hline(0, title="Zero", color=#787B86, linestyle=hline.style_dashed)
 
// OS Level Plots & Boundary Limit Fills
p_upper = plot(upperLevel, title="Upper OS Level", color=#FF5252, linewidth=1, style=plot.style_line)
p_lower = plot(lowerLevel, title="Lower OS Level", color=#00E676, linewidth=1, style=plot.style_line)
p_top_limit = plot(100.0, title="Top Limit (+100)", color=color.new(color.white, 100), display=display.none)
p_bot_limit = plot(-100.0, title="Bottom Limit (-100)", color=color.new(color.white, 100), display=display.none)
 
// Beyond OS Level Color Fills (Customizable in Style Tab)
fill(p_upper, p_top_limit, color=color.new(#FF5252, 85), title="OS Bullish Fill (> Upper OS)")
fill(p_lower, p_bot_limit, color=color.new(#00E676, 85), title="OS Bearish Fill (< Lower OS)")
 
// Top Center Single-Row Readout Box
var table tsiBox = table.new(position.top_center, 6, 1, bgcolor=color.new(#131722, 10), border_color=#363c4e, border_width=1)
 
if barstate.islast and showBox
    color tsiValColor = tsi_value >= 0.0 ? #00E676 : #FF5252
    color sigValColor = tsi_signal >= 0.0 ? #00E676 : #FF5252
    color gapValColor = tsi_gap >= 0.0 ? #00E676 : #FF5252
    
    // Column 0 & 1: TSI
    table.cell(tsiBox, 0, 0, "TSI:", bgcolor=#1e222d, text_color=color.gray, text_size=boxTextSize)
    table.cell(tsiBox, 1, 0, str.tostring(tsi_value, "#.##"), bgcolor=#1e222d, text_color=tsiValColor, text_size=boxTextSize)
    
    // Column 2 & 3: Signal
    table.cell(tsiBox, 2, 0, "Signal:", bgcolor=#1e222d, text_color=color.gray, text_size=boxTextSize)
    table.cell(tsiBox, 3, 0, str.tostring(tsi_signal, "#.##"), bgcolor=#1e222d, text_color=sigValColor, text_size=boxTextSize)
    
    // Column 4 & 5: Gap
    table.cell(tsiBox, 4, 0, "Gap:", bgcolor=#1e222d, text_color=color.gray, text_size=boxTextSize)
    table.cell(tsiBox, 5, 0, (tsi_gap > 0 ? "+" : "") + str.tostring(tsi_gap, "#.##"), bgcolor=#1e222d, text_color=gapValColor, text_size=boxTextSize)

← 返回列表