156周均线倍数 原始码

//@version=6
// BTC 156-week SMA multiple: bottom line (1x), relative top line (80th percentile of the past 4 years).
// Candles turn green in the bottom zone, red in the top zone, gray in between.
// Use BITSTAMP:BTCUSD (history from 2011) so the 4-year ranking has enough data.
indicator("156周均线倍数", shorttitle="156W倍数", overlay=true, max_bars_back=2000, max_labels_count=500)

lenDays = input.int(1092, "均线天数(156周 = 1092天)", minval=50)
rankDays = input.int(1460, "相对排名回看天数(4年 = 1460天)", minval=200)
topPct = input.float(80, "顶部区:倍数排进过去4年的前百分之几(80 = 前20%)", minval=50, maxval=99)
showTags = input.bool(true, "标出进入底部区 / 顶部区的位置")

// Work on the chart's own bars (no request.security): TradingView only hands a script a few years of
// daily bars on some plans, which is less than the 7 years (3-year line + 4-year ranking) the top line needs.
// Day counts are converted to bars, so a weekly chart uses 156 weeks / 209 weeks.
daysPerBar = timeframe.in_seconds() / 86400.0
lenBars = math.max(2, math.round(lenDays / daysPerBar))
rankBars = math.max(10, math.round(rankDays / daysPerBar))

sma156 = ta.sma(close, lenBars)
mult = close / sma156
var win = array.new_float()
if not na(mult)
array.push(win, mult)
if array.size(win) > rankBars
array.shift(win)
topMult = array.size(win) >= math.round(rankBars * 0.8) ? array.percentile_linear_interpolation(win, topPct) : na

topLine = sma156 * topMult
inBottom = mult <= 1 inTop = not na(topMult) and mult >= topMult

GREEN = color.rgb(0, 200, 83)
RED = color.rgb(255, 23, 68)

// candles: green = cheap, red = expensive, gray = middle
barcolor(inBottom ? GREEN : inTop ? RED : color.new(color.gray, 50))

pPrice = plot(close, "价格", display=display.none)
pBot = plot(sma156, "底部线:156周均线", color=GREEN, linewidth=3)
pTop = plot(topLine, "顶部线", color=RED, linewidth=3)
fill(pPrice, pBot, color=inBottom ? color.new(GREEN, 55) : na, title="底部区填色")
fill(pPrice, pTop, color=inTop ? color.new(RED, 55) : na, title="顶部区填色")
bgcolor(inBottom ? color.new(GREEN, 85) : inTop ? color.new(RED, 85) : na, title="区域底色")

if showTags and inBottom and not inBottom[1]
label.new(bar_index, low, "进入底部区\n便宜", yloc=yloc.belowbar, style=label.style_label_up, color=GREEN, textcolor=color.white, size=size.normal)
if showTags and inTop and not inTop[1]
label.new(bar_index, high, "进入顶部区\n贵", yloc=yloc.abovebar, style=label.style_label_down, color=RED, textcolor=color.white, size=size.normal)

// name tags at the right end of both lines
var label tagBot = na
var label tagTop = na
if barstate.islast
label.delete(tagBot)
label.delete(tagTop)
tagBot := label.new(bar_index + 3, sma156, "底部线(156周均线)" + str.tostring(sma156, "#,###"), style=label.style_label_left, color=GREEN, textcolor=color.white, size=size.large)
if not na(topLine)
tagTop := label.new(bar_index + 3, topLine, "顶部线 " + str.tostring(topLine, "#,###") + "(" + str.tostring(topMult, "0.00") + " 倍)", style=label.style_label_left, color=RED, textcolor=color.white, size=size.large)

var table tb = table.new(position.top_right, 1, 2, border_width=1)
if barstate.islast
zone = inBottom ? "底部区:便宜" : inTop ? "顶部区:贵" : "中间区"
zc = inBottom ? GREEN : inTop ? RED : color.gray
table.cell(tb, 0, 0, "现在是 156 周均线的 " + str.tostring(mult, "0.00") + " 倍", text_color=color.white, bgcolor=zc, text_size=size.huge)
table.cell(tb, 0, 1, zone, text_color=color.white, bgcolor=zc, text_size=size.large)

图表:

点赞

发表回复

电子邮件地址不会被公开。必填项已用 * 标注