本页源码来自 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/
// © lapaznyangel
//@version=6
indicator("Strat Bars + Bias", shorttitle="Strat", overlay=true, max_labels_count=500, max_lines_count=100)
// ---------------------------------------------------------------------------
// Labels every bar the Strat way and shows which way the bar sequence points.
// 1 = inside bar (high <= prior high and low >= prior low)
// 2U = broke prior high only 2D = broke prior low only
// 3 = outside bar (broke both)
// Colors: 1 yellow, 2U green, 2D red, 3 purple.
//
// The table shows the current bar on Monthly / Weekly / Daily / Hourly / chart:
// its type so far, the last completed bar's type, and whether price is above or
// below that bar's open. All four up (or all four down) = full time frame continuity.
//
// The bias is the textbook Strat reading, NOT a tested edge. A first test on SPY
// found no reliable edge from inside bars alone. Use it as a checklist, not a signal.
// Use on a chart of 1 hour or shorter so the chart row sits at or below Hourly.
// ---------------------------------------------------------------------------
showLabels = input.bool(true, "Label bars (1 / 2U / 2D / 3)")
colorBars = input.bool(true, "Color bars")
showPatterns = input.bool(true, "Label patterns (2-1-2, 3-1-2, 2-2, 3-2 ...)")
showTrig = input.bool(true, "Show inside bar trigger lines")
showTbl = input.bool(true, "Show table")
showBias = input.bool(true, "Show bias arrow on last bar")
tblPosIn = input.string("Top right", "Table position", options = ["Top right", "Top left", "Bottom right", "Bottom left"])
colIn = input.color(color.new(#f5c518, 0), "Inside (1)")
colUp = input.color(color.new(#26a65b, 0), "Directional up (2U)")
colDn = input.color(color.new(#e74c3c, 0), "Directional down (2D)")
colOut = input.color(color.new(#8e44ad, 0), "Outside (3)")
// Bar type: 1 inside, 2 = 2U, -2 = 2D, 3 outside, 0 = not enough history.
f_type(float h, float l, float ph, float pl) =>
int r = 0
if not na(ph) and not na(pl)
if h <= ph and l >= pl
r := 1
else if h > ph and l < pl
r := 3
else if h > ph
r := 2
else
r := -2
r
f_txt(int t) =>
t == 1 ? "1" : t == 2 ? "2U" : t == -2 ? "2D" : t == 3 ? "3" : "-"
f_col(int t) =>
t == 1 ? colIn : t == 2 ? colUp : t == -2 ? colDn : t == 3 ? colOut : color.gray
f_bar() =>
[open, high, low, close, high[1], low[1], high[2], low[2]]
// --- chart time frame -------------------------------------------------------
t = f_type(high, low, high[1], low[1])
barcolor(colorBars ? (t == 1 ? colIn : t == 2 ? colUp : t == -2 ? colDn : t == 3 ? colOut : na) : na)
plotshape(showLabels and t == 1, "Inside", style = shape.labeldown, location = location.abovebar, color = colIn, textcolor = color.black, text = "1", size = size.tiny)
plotshape(showLabels and t == 2, "2U", style = shape.labeldown, location = location.abovebar, color = colUp, textcolor = color.white, text = "2U", size = size.tiny)
plotshape(showLabels and t == -2, "2D", style = shape.labelup, location = location.belowbar, color = colDn, textcolor = color.white, text = "2D", size = size.tiny)
plotshape(showLabels and t == 3, "Outside", style = shape.labeldown, location = location.abovebar, color = colOut, textcolor = color.white, text = "3", size = size.tiny)
// --- patterns (a = two bars ago, b = previous bar, c = this bar) -----------
f_pattern(int a, int b, int c) =>
string p = ""
if b == 1
if a == 3 and c == 2
p := "3-1-2U"
else if a == 3 and c == -2
p := "3-1-2D"
else if a == 2 and c == 2
p := "2-1-2U cont"
else if a == -2 and c == -2
p := "2-1-2D cont"
else if a == -2 and c == 2
p := "2D-1-2U rev"
else if a == 2 and c == -2
p := "2U-1-2D rev"
else if a == 1 and b == 2 and c == 2
p := "1-2U-2U"
else if a == 1 and b == -2 and c == -2
p := "1-2D-2D"
else if a == 1 and b == 2 and c == -2
p := "1-2U-2D rev"
else if a == 1 and b == -2 and c == 2
p := "1-2D-2U rev"
else if b == 3 and c == 2
p := "3-2U"
else if b == 3 and c == -2
p := "3-2D"
else if b == 2 and c == -2
p := "2U-2D rev"
else if b == -2 and c == 2
p := "2D-2U rev"
else if b == 2 and c == 2
p := "2U-2U cont"
else if b == -2 and c == -2
p := "2D-2D cont"
p
pat = barstate.isconfirmed ? f_pattern(t[2], t[1], t) : ""
if showPatterns and pat != ""
bull = t == 2
label.new(bar_index, bull ? low : high, pat, style = bull ? label.style_label_up : label.style_label_down, color = color.new(bull ? colUp : colDn, 25), textcolor = color.white, size = size.small)
// --- inside bar trigger lines ----------------------------------------------
// When a bar closes as an inside bar, mark its high (long trigger) and low (short
// trigger). Dotted lines mark the bar before it (the "mother" bar's extremes) as the
// first target. Lines stop when the inside bar breaks.
var bool armed = false
var int armBar = na
var float armH = na
var float armL = na
var line lnH = na
var line lnL = na
var line lnTH = na
var line lnTL = na
if showTrig and barstate.isconfirmed and t == 1
if armed
line.set_extend(lnH, extend.none)
line.set_extend(lnL, extend.none)
line.set_extend(lnTH, extend.none)
line.set_extend(lnTL, extend.none)
armed := true
armBar := bar_index
armH := high
armL := low
lnH := line.new(bar_index, high, bar_index + 1, high, extend = extend.right, color = colUp, width = 2)
lnL := line.new(bar_index, low, bar_index + 1, low, extend = extend.right, color = colDn, width = 2)
lnTH := line.new(bar_index, high[1], bar_index + 1, high[1], extend = extend.right, color = color.new(colUp, 40), style = line.style_dotted)
lnTL := line.new(bar_index, low[1], bar_index + 1, low[1], extend = extend.right, color = color.new(colDn, 40), style = line.style_dotted)
else if armed and bar_index > armBar and (high > armH or low < armL)
armed := false
line.set_extend(lnH, extend.none)
line.set_extend(lnL, extend.none)
line.set_extend(lnTH, extend.none)
line.set_extend(lnTL, extend.none)
line.set_x2(lnH, bar_index)
line.set_x2(lnL, bar_index)
line.set_x2(lnTH, bar_index)
line.set_x2(lnTL, bar_index)
// --- higher time frames -----------------------------------------------------
[mo, mh, ml, mc, mph, mpl, mpph, mppl] = request.security(syminfo.tickerid, "M", f_bar())
[wo, wh, wl, wc, wph, wpl, wpph, wppl] = request.security(syminfo.tickerid, "W", f_bar())
[dO, dh, dl, dc, dph, dpl, dpph, dppl] = request.security(syminfo.tickerid, "D", f_bar())
[ho, hh, hl, hc, hph, hpl, hpph, hppl] = request.security(syminfo.tickerid, "60", f_bar())
mt = f_type(mh, ml, mph, mpl), mp = f_type(mph, mpl, mpph, mppl)
wt = f_type(wh, wl, wph, wpl), wp = f_type(wph, wpl, wpph, wppl)
dt = f_type(dh, dl, dph, dpl), dp = f_type(dph, dpl, dpph, dppl)
ht = f_type(hh, hl, hph, hpl), hp = f_type(hph, hpl, hpph, hppl)
f_dir(float o, float c) => c > o ? 1 : c < o ? -1 : 0
mdir = f_dir(mo, mc)
wdir = f_dir(wo, wc)
ddir = f_dir(dO, dc)
hdir = f_dir(ho, hc)
score = mdir + wdir + ddir + hdir
string biasTxt = "Mixed: stand aside"
color biasCol = color.gray
if score == 4
biasTxt := "FULL CONTINUITY UP: look for longs"
biasCol := colUp
else if score == -4
biasTxt := "FULL CONTINUITY DOWN: look for shorts"
biasCol := colDn
else if score >= 2
biasTxt := "Leaning up (" + str.tostring(score) + " of 4)"
biasCol := color.new(colUp, 30)
else if score <= -2
biasTxt := "Leaning down (" + str.tostring(score) + " of 4)"
biasCol := color.new(colDn, 30)
// What the sequence on the chart says right now.
string readTxt = ""
if armed
readTxt := "Inside bar armed: long above " + str.tostring(armH, format.mintick) + ", short below " + str.tostring(armL, format.mintick)
else if t == 1
readTxt := "Inside bar forming: wait for a break of the prior high or low"
else if t == 2
readTxt := "2U: prior high broken, up bias. Turns to a reversal if the prior low breaks"
else if t == -2
readTxt := "2D: prior low broken, down bias. Turns to a reversal if the prior high breaks"
else if t == 3
readTxt := "3: both sides broken, wait for the next bar to pick a side"
// --- table ------------------------------------------------------------------
tblPos = switch tblPosIn
"Top left" => position.top_left
"Bottom right" => position.bottom_right
"Bottom left" => position.bottom_left
=> position.top_right
var table tb = table.new(tblPos, 4, 10, border_width = 1)
f_arrow(int d) => d > 0 ? "▲" : d < 0 ? "▼" : "•"
f_row(int r, string name, int now, int prev, int d) =>
tb.cell(0, r, name, text_color = color.white, bgcolor = color.new(color.gray, 25), text_size = size.small)
tb.cell(1, r, f_txt(now), text_color = color.white, bgcolor = color.new(f_col(now), 20), text_size = size.small)
tb.cell(2, r, f_txt(prev), text_color = color.white, bgcolor = color.new(f_col(prev), 40), text_size = size.small)
tb.cell(3, r, f_arrow(d), text_color = color.white, bgcolor = d > 0 ? color.new(colUp, 25) : d < 0 ? color.new(colDn, 25) : color.new(color.gray, 25), text_size = size.small)
if showTbl and barstate.islast
tb.cell(0, 0, "Strat", text_color = color.white, bgcolor = color.new(color.gray, 10), text_size = size.small)
tb.cell(1, 0, "Now", text_color = color.white, bgcolor = color.new(color.gray, 10), text_size = size.small)
tb.cell(2, 0, "Last", text_color = color.white, bgcolor = color.new(color.gray, 10), text_size = size.small)
tb.cell(3, 0, "vs open", text_color = color.white, bgcolor = color.new(color.gray, 10), text_size = size.small)
f_row(1, "Monthly", mt, mp, mdir)
f_row(2, "Weekly", wt, wp, wdir)
f_row(3, "Daily", dt, dp, ddir)
f_row(4, "Hourly", ht, hp, hdir)
f_row(5, "Chart " + timeframe.period, t, t[1], f_dir(open, close))
tb.cell(0, 6, "Continuity: " + str.tostring(score) + " of 4 (up positive, down negative)", text_color = color.white, bgcolor = color.new(color.gray, 25), text_size = size.small)
tb.merge_cells(0, 6, 3, 6)
tb.cell(0, 7, biasTxt, text_color = color.white, bgcolor = biasCol, text_size = size.normal)
tb.merge_cells(0, 7, 3, 7)
tb.cell(0, 8, readTxt, text_color = color.white, bgcolor = color.new(color.gray, 25), text_size = size.small)
tb.merge_cells(0, 8, 3, 8)
tb.cell(0, 9, "Textbook Strat read, not a tested edge", text_color = color.new(color.white, 30), bgcolor = color.new(color.gray, 45), text_size = size.tiny)
tb.merge_cells(0, 9, 3, 9)
// --- bias arrow on the last bar --------------------------------------------
var label biasLbl = na
if showBias and barstate.islast
label.delete(biasLbl)
string arrow = score >= 2 ? "▲ LONG BIAS" : score <= -2 ? "▼ SHORT BIAS" : "◆ WAIT"
color ac = score >= 2 ? colUp : score <= -2 ? colDn : color.gray
biasLbl := label.new(bar_index + 2, close, arrow, style = label.style_label_left, color = ac, textcolor = color.white, size = size.large)