本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
// Pivots and Swing Zones [Badr Edge] · v1.0.0
// Educational chart-marking tool. Markers show what happened on a bar. They are not trade signals.
indicator("Pivots and Swing Zones [Badr Edge]", shorttitle = "BE Pivots", overlay = true, max_boxes_count = 200, max_labels_count = 50)
const color GOLD = #D9B46C
const color TEAL = #4FB8A0
const color ROSE = #E07A6C
const color GREY = #8F939B
const string G_P = "Daily pivots"
pivType = input.string("Floor", "Pivot type", options = ["Floor", "Camarilla"], group = G_P, tooltip = "Both are calculated from the prior day's high, low and close. Floor: P, R1, S1, R2, S2. Camarilla: P, H3, L3, H4, L4.")
showPiv = input.bool(true, "Pivot levels", group = G_P)
showOut = input.bool(true, "Outer levels (R2 / S2 or H4 / L4)", group = G_P)
const string G_S = "Swing zones"
swLen = input.int(10, "Swing length (bars each side)", minval = 3, maxval = 50, group = G_S, tooltip = "A swing is confirmed this many bars after it forms, so a zone appears with that delay and never moves afterwards.")
zoneAtr = input.float(0.15, "Zone height (x ATR 14)", minval = 0.02, step = 0.05, group = G_S)
maxKeep = input.int(4, "Zones kept per side", minval = 1, maxval = 15, group = G_S)
confAtr = input.float(1.0, "Confluence distance (x ATR 14)", minval = 0, step = 0.05, group = G_S, tooltip = "A swing zone this close to a pivot level is drawn in gold as a confluence zone.")
const string G_V = "Visuals"
showZones = input.bool(true, "Swing zones", group = G_V)
showMarks = input.bool(true, "Rejection markers", group = G_V, tooltip = "A small triangle when a candle trades into a zone and closes back outside it. A marker shows what happened. It is not a trade signal.")
confOnly = input.bool(true, "Markers at confluence zones only", group = G_V)
showLabels = input.bool(true, "Pivot labels", group = G_V)
const string G_Y = "Style"
showWm = input.bool(true, "Watermark", group = G_Y)
const string G_A = "Alerts"
useAlerts = input.bool(true, "Alert on a rejection marker", group = G_A, tooltip = "Create the alert with \"Any alert() function call\". Fires once, after the bar closes.")
tfOK = timeframe.isintraday
atr = ta.atr(14)
[dH, dL, dC] = request.security(syminfo.tickerid, "D", [high[1], low[1], close[1]], lookahead = barmerge.lookahead_on)
bool floorP = pivType == "Floor"
float rng = dH - dL
float pp = (dH + dL + dC) / 3
float a1 = floorP ? 2 * pp - dL : dC + rng * 1.1 / 4
float b1 = floorP ? 2 * pp - dH : dC - rng * 1.1 / 4
float a2 = floorP ? pp + rng : dC + rng * 1.1 / 2
float b2 = floorP ? pp - rng : dC - rng * 1.1 / 2
nearPivot(float p) =>
float d = math.min(math.abs(p - pp), math.abs(p - a1), math.abs(p - b1), math.abs(p - a2), math.abs(p - b2))
d <= confAtr * atr
type Zone
box b
float top
float bot
int dir
int lastMark
var array<Zone> res = array.new<Zone>()
var array<Zone> sup = array.new<Zone>()
ph = ta.pivothigh(high, swLen, swLen)
pl = ta.pivotlow(low, swLen, swLen)
float zh = zoneAtr * atr
if tfOK and not na(ph) and not na(zh) and close < ph
res.push(Zone.new(box.new(bar_index - swLen, ph, bar_index, ph - zh, border_color = na, bgcolor = na), ph, ph - zh, -1, 0))
if res.size() > maxKeep
res.shift().b.delete()
if tfOK and not na(pl) and not na(zh) and close > pl
sup.push(Zone.new(box.new(bar_index - swLen, pl + zh, bar_index, pl, border_color = na, bgcolor = na), pl + zh, pl, 1, 0))
if sup.size() > maxKeep
sup.shift().b.delete()
// Extends live zones, retires broken ones and returns true when a candle rejects a zone
scan(array<Zone> zs) =>
bool hit = false
if zs.size() > 0
for i = zs.size() - 1 to 0
Zone z = zs.get(i)
bool conf = nearPivot((z.top + z.bot) / 2)
color c = conf ? GOLD : GREY
z.b.set_right(bar_index)
z.b.set_bgcolor(color.new(c, showZones ? (conf ? 78 : 88) : 100))
z.b.set_border_color(color.new(c, showZones ? (conf ? 30 : 65) : 100))
if barstate.isconfirmed
bool broken = z.dir == -1 ? close > z.top : close < z.bot
if broken
zs.remove(i)
else
bool rej = z.dir == -1 ? high >= z.bot and close < z.bot : low <= z.top and close > z.top
if rej and (conf or not confOnly) and bar_index - z.lastMark > 5
hit := true
z.lastMark := bar_index
hit
rejDn = scan(res)
rejUp = scan(sup)
lv(float v, bool on) => on and tfOK and v == v[1] ? v : na
plot(lv(pp, showPiv), "Pivot", color.new(GOLD, 10), 1, plot.style_linebr)
plot(lv(a1, showPiv), "R1 / H3", color.new(GREY, 25), 1, plot.style_linebr)
plot(lv(b1, showPiv), "S1 / L3", color.new(GREY, 25), 1, plot.style_linebr)
plot(lv(a2, showPiv and showOut), "R2 / H4", color.new(GREY, 55), 1, plot.style_linebr)
plot(lv(b2, showPiv and showOut), "S2 / L4", color.new(GREY, 55), 1, plot.style_linebr)
plotshape(showMarks and rejUp, "Rejection at a support zone", shape.triangleup, location.belowbar, color.new(TEAL, 0), size = size.tiny)
plotshape(showMarks and rejDn, "Rejection at a resistance zone", shape.triangledown, location.abovebar, color.new(ROSE, 0), size = size.tiny)
plot(res.size() > 0 ? res.last().top : na, "Nearest swing high zone", display = display.data_window)
plot(sup.size() > 0 ? sup.last().bot : na, "Nearest swing low zone", display = display.data_window)
var array<label> tags = array.new<label>()
tag(float v, string txt, color c, bool on) =>
if on and not na(v)
tags.push(label.new(bar_index + 3, v, txt, style = label.style_label_left, color = color.new(color.black, 100), textcolor = c, size = size.small))
if barstate.islast and showLabels and tfOK
for t in tags
t.delete()
tags.clear()
tag(pp, "P", GOLD, showPiv)
tag(a1, floorP ? "R1" : "H3", GREY, showPiv)
tag(b1, floorP ? "S1" : "L3", GREY, showPiv)
tag(a2, floorP ? "R2" : "H4", GREY, showPiv and showOut)
tag(b2, floorP ? "S2" : "L4", GREY, showPiv and showOut)
if useAlerts and rejUp
alert(syminfo.ticker + " traded into a swing low zone and closed back above it", alert.freq_once_per_bar_close)
if useAlerts and rejDn
alert(syminfo.ticker + " traded into a swing high zone and closed back below it", alert.freq_once_per_bar_close)
var table wm = table.new(position.bottom_right, 1, 1)
if barstate.islast and showWm
wm.cell(0, 0, "BADR EDGE", text_color = color.new(GREY, 72), text_size = size.small)
var table note = table.new(position.top_right, 1, 1)
if barstate.islast and not tfOK
note.cell(0, 0, "Pivots and Swing Zones works on intraday charts.", text_color = GOLD, bgcolor = color.new(color.black, 20), text_size = size.small)