本页源码来自 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/
// © dub4art
//@version=6
indicator('Trend or Range Widget', 'TRWT+', overlay = true)
var g_CStats = "Trend or Range Widget"
length = input.int(14, 'Lookback', minval = 1, group = g_CStats, tooltip = "How many candles are used for calculations")
t_val = input.int(50, "Trending Threshold", minval = 1,inline = 'lvl1', group = g_CStats, tooltip = "Any value above signals trending condition")
colt = input.color(color.new(color.green, 40), '', inline = 'lvl1', group = g_CStats)
coln = input.color(color.new(color.gray, 40), 'Neutral', inline = 'lvl2', group = g_CStats)
c_val = input.int(25, "Ranging Threshold", minval = 1,inline = 'lvl3', group = g_CStats)
colc = input.color(color.new(color.red, 40), '', inline = 'lvl3', group = g_CStats, tooltip = "Any value below signals ranging condition")
concise = input.bool(false, 'Show Only the Score', inline = 'lvl3a', group = g_CStats)
concise2 = input.bool(false, 'Minimal', inline = 'lvl3a', group = g_CStats)
dspm = input.string('Horizontal', 'Widget Orientation & Location', options = ['Horizontal', 'Vertical'], inline = 'lvl4', group = g_CStats)
clPS = input.string('Bottom Right', '', options = ['Bottom Center', 'Bottom Left', 'Bottom Right', 'Middle Right', 'Top Center', 'Top Right'], inline = 'lvl4', group = g_CStats)
txt_col = input.color(color.new(color.white,0), "Text colors", inline = 'cols1',group = g_CStats)
txt_col2 = input.color(color.new(color.silver,0), "", inline = 'cols1',group = g_CStats)
txt_size = input.int(13, "Size", 5, inline = "cols1", group = g_CStats, display = display.none)
hdrBg = input.color(color.new(color.gray,80), "Header BG", inline = 'cols1',group = g_CStats)
show_markers = input.bool(false, 'Show Markers', inline = 'cols2', group = g_CStats)
show_trend = input.bool(false, 'Trend Only', inline = 'cols2', group = g_CStats)
tPOS = switch clPS
'Bottom Center' => position.bottom_center
'Bottom Left' => position.bottom_left
'Bottom Right' => position.bottom_right
'Middle Right' => position.middle_right
'Top Center' => position.top_center
'Top Right' => position.top_right
candleSize = high - low
bodySize = math.abs(close - open)
bodyPct = candleSize > 0 ? bodySize / candleSize * 100 : 0.0
buyVol = candleSize > 0 ? volume * (close - low) / candleSize : volume / 2
sellVol = volume - buyVol
rangeTop = ta.highest(high, length)[1]
rangeBottom = ta.lowest(low, length)[1]
atrVal = ta.atr(length)
range_width = atrVal > 0 ? (rangeTop - rangeBottom) / atrVal : 0.0
// Kaufman's Efficiency Ratio: net move / total path travelled (0 = chop, 1 = straight line)
netChange = math.abs(close - close[length])
pathSum = math.sum(math.abs(close - close[1]), length)
er = pathSum > 0 ? netChange / pathSum : 0.0
avgBodyPct = ta.ema(bodyPct, length)
totalVol = math.sum(volume, length)
totalBuy = math.sum(buyVol, length)
totalSell = math.sum(sellVol, length)
volBuy = totalVol != 0 ? totalBuy / totalVol * 100 : na
volSell = volBuy != 0 ? 100 - volBuy : na
volPrevailing = volBuy != 0 ? volBuy > 50 ? 1 : -1 : 0
volDirUp = volBuy > volBuy[1] ? ' ▲' : volBuy < volBuy[1] ? ' ▼' : na
volDirDn = volSell > volSell[1] ? ' ▲' : volSell < volSell[1] ? ' ▼' : na
range_score = avgBodyPct * range_width * er
score_color = range_score < c_val + 1 ? colc : c_val < range_score and range_score < t_val ? coln : range_score > t_val - 1 ? colt : na
string score_txt = range_score < c_val + 1 ? "Ranging" : c_val < range_score and range_score < t_val ? "Neutral" : range_score > t_val - 1 ? "Trending" : na
plotshape(show_markers and not show_trend and range_score > -1 ? 1 : show_trend and range_score > t_val - 1 ? 1 : na, style = shape.circle, location = location.bottom, color = score_color, size = size.tiny, display = display.all - display.status_line, force_overlay = true)
// --- Widget ---
horizontal_dspm = dspm == 'Horizontal'
var table stats = table.new(tPOS, 5, 5, border_width = 1, frame_color = color.gray, frame_width = 0, border_color = color.new(color.gray, 50))
if barstate.islast
if not concise
table.cell(stats, horizontal_dspm ? 0 : 0, horizontal_dspm ? 0 : 0, 'Body/Candle %', text_color = txt_col2, text_size = txt_size, bgcolor = hdrBg, tooltip = "Body / whole Candle average ratio calculated over the lookback period")
table.cell(stats, horizontal_dspm ? 1 : 0, horizontal_dspm ? 0 : 1, 'Range Width', text_color = txt_col2, text_size = txt_size, bgcolor = hdrBg, tooltip = "ATR based size of the current range defined by lookback paramaeter")
table.cell(stats, horizontal_dspm ? 2 : 0, horizontal_dspm ? 0 : 2, 'KER', text_color = txt_col2, text_size = txt_size, bgcolor = hdrBg, tooltip = "Kaufman Efficiency Ratio (KER) - between 0 and 1, higher values represent stronger trend")
table.cell(stats, horizontal_dspm ? 3 : 0, horizontal_dspm ? 0 : 3, volPrevailing == 1 ? 'Buying Vol.' : volPrevailing == 1 ? 'Selling Vol.' : 'No Volume', text_color = txt_col2, text_size = txt_size, bgcolor = hdrBg, tooltip = "Prevailing volume over the lookback period")
table.cell(stats, horizontal_dspm ? 4 : 0, horizontal_dspm ? 0 : 4, score_txt + " (" + str.tostring(length) + ")", text_color = txt_col2, text_size = txt_size, bgcolor = hdrBg, tooltip = "The score based on calculation made over the lookback period")
table.cell(stats, horizontal_dspm ? 0 : 1, horizontal_dspm ? 1 : 0, str.tostring(avgBodyPct, '#.#'), text_color = txt_col, text_size = txt_size)
table.cell(stats, horizontal_dspm ? 1 : 1, horizontal_dspm ? 1 : 1, str.tostring(range_width, '#.#'), text_color = txt_col, text_size = txt_size)
table.cell(stats, horizontal_dspm ? 2 : 1, horizontal_dspm ? 1 : 2, str.tostring(er, '#.##'), text_color = txt_col, text_size = txt_size)
table.cell(stats, horizontal_dspm ? 3 : 1, horizontal_dspm ? 1 : 3, volBuy != 0 ? volBuy > volSell ? str.tostring(volBuy, '#.#') + ' %' + volDirUp : str.tostring(volSell, '#.#') + ' %' + volDirDn : na, text_color = volBuy > volSell ? color.lime : color.red, text_size = txt_size)
table.cell(stats, horizontal_dspm ? 4 : 1, horizontal_dspm ? 1 : 4, str.tostring(range_score, '#'), text_color = txt_col, bgcolor = score_color, text_size = txt_size)
else if not concise2
table.cell(stats, horizontal_dspm ? 0 : 0, horizontal_dspm ? 0 : 0, score_txt + " (" + str.tostring(length) + ")", text_color = txt_col2, text_size = txt_size, bgcolor = hdrBg)
table.cell(stats, horizontal_dspm ? 1 : 0, horizontal_dspm ? 0 : 1, str.tostring(range_score, '#'), text_color = txt_col, text_size = txt_size, bgcolor = score_color)
else
table.cell(stats, horizontal_dspm ? 0 : 0, horizontal_dspm ? 0 : 0, str.tostring(range_score, '#'), text_color = txt_col, text_size = txt_size, bgcolor = score_color)