本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
// Session Opening Range [Badr Edge] · v1.0.0
// Marks the opening range of the Asia, London and New York sessions and the first candle close outside it.
// Educational chart-marking tool. It does not predict price and it is not a trade signal.
indicator("Session Opening Range [Badr Edge]", shorttitle = "BE ORB", overlay = true, max_boxes_count = 150, max_lines_count = 450, max_labels_count = 50)
// ───────── Palette (Badr Edge dark theme)
const color GOLD = #D9B46C
const color TEAL = #4FB8A0
const color ROSE = #E07A6C
const color GREY = #8F939B
const color BLUE = #7F9FD6
// ───────── Inputs
const string G_S = "Sessions (each in its own local time, daylight saving handled)"
showAsia = input.bool(true, "Asia (Tokyo time)", inline = "a", group = G_S)
asiaSess = input.session("0900-1500", "", inline = "a", group = G_S)
showLon = input.bool(true, "London (London time)", inline = "l", group = G_S)
lonSess = input.session("0800-1630", "", inline = "l", group = G_S)
showNY = input.bool(true, "New York (New York time)", inline = "n", group = G_S)
nySess = input.session("0930-1600", "", inline = "n", group = G_S)
const string G_R = "Range"
lenMin = input.int(15, "Opening range length (minutes)", options = [5, 10, 15, 30, 45, 60], group = G_R, tooltip = "Minutes from the session open. The chart timeframe must be equal to or smaller than this.")
showMid = input.bool(true, "Midline", group = G_R)
showMarks = input.bool(true, "Breakout markers", group = G_R, tooltip = "A small triangle on the first candle that closes outside the range. A marker shows what happened. It is not a trade signal.")
firstOnly = input.bool(true, "First close per side only", group = G_R)
const string G_Y = "Style"
colAsia = input.color(BLUE, "Asia", inline = "c", group = G_Y)
colLon = input.color(GREY, "London", inline = "c", group = G_Y)
colNY = input.color(GOLD, "New York", inline = "c", group = G_Y)
showWm = input.bool(true, "Watermark", group = G_Y)
const string G_A = "Alerts"
useAlerts = input.bool(true, "Alert on a close outside a range", group = G_A, tooltip = "Create the alert with \"Any alert() function call\". Fires once, after the bar closes.")
lenMs = lenMin * 60000
tfOK = timeframe.isintraday and timeframe.in_seconds() <= lenMin * 60
// ───────── One opening range per session
openingRange(bool show, string sess, string tz, string name, color col) =>
var float hi = na
var float lo = na
var int t0 = na
var bool done = false
var bool upDone = false
var bool dnDone = false
var box bx = na
var line lh = na
var line ll = na
var line lm = na
bool up = false
bool dn = false
bool inS = show and tfOK and not na(time(timeframe.period, sess, tz))
if inS and not inS[1]
hi := high
lo := low
t0 := time
done := false
upDone := false
dnDone := false
bx := box.new(bar_index, hi, bar_index, lo, border_color = color.new(col, 45), border_width = 1, bgcolor = color.new(col, 90), text = name + " OR", text_size = size.tiny, text_color = color.new(col, 25), text_halign = text.align_left, text_valign = text.align_top)
else if inS and not na(t0)
if not done and time < t0 + lenMs
hi := math.max(hi, high)
lo := math.min(lo, low)
bx.set_top(hi)
bx.set_bottom(lo)
bx.set_right(bar_index)
else
if not done
done := true
lh := line.new(bar_index - 1, hi, bar_index, hi, color = color.new(col, 20), width = 1)
ll := line.new(bar_index - 1, lo, bar_index, lo, color = color.new(col, 20), width = 1)
if showMid
lm := line.new(bar_index - 1, (hi + lo) / 2, bar_index, (hi + lo) / 2, color = color.new(col, 55), width = 1, style = line.style_dotted)
lh.set_x2(bar_index)
ll.set_x2(bar_index)
if showMid
lm.set_x2(bar_index)
if barstate.isconfirmed
up := close > hi and (firstOnly ? not upDone : close[1] <= hi)
dn := close < lo and (firstOnly ? not dnDone : close[1] >= lo)
if up
upDone := true
if dn
dnDone := true
[inS ? hi : na, inS ? lo : na, up, dn]
[aH, aL, aUp, aDn] = openingRange(showAsia, asiaSess, "Asia/Tokyo", "ASIA", colAsia)
[lH, lL, lUp, lDn] = openingRange(showLon, lonSess, "Europe/London", "LONDON", colLon)
[nH, nL, nUp, nDn] = openingRange(showNY, nySess, "America/New_York", "NEW YORK", colNY)
anyUp = aUp or lUp or nUp
anyDn = aDn or lDn or nDn
plotshape(showMarks and anyUp, "Close above range", shape.triangleup, location.belowbar, color.new(TEAL, 0), size = size.tiny)
plotshape(showMarks and anyDn, "Close below range", shape.triangledown, location.abovebar, color.new(ROSE, 0), size = size.tiny)
// ───────── Data Window values
plot(aH, "Asia range high", display = display.data_window)
plot(aL, "Asia range low", display = display.data_window)
plot(lH, "London range high", display = display.data_window)
plot(lL, "London range low", display = display.data_window)
plot(nH, "New York range high", display = display.data_window)
plot(nL, "New York range low", display = display.data_window)
// ───────── Alerts (bar close only)
say(string sessName, bool up, float lvl) =>
alert(syminfo.ticker + " closed " + (up ? "above" : "below") + " the " + sessName + " opening range " + (up ? "high " : "low ") + str.tostring(lvl, format.mintick), alert.freq_once_per_bar_close)
if useAlerts
if aUp
say("Asia", true, aH)
if aDn
say("Asia", false, aL)
if lUp
say("London", true, lH)
if lDn
say("London", false, lL)
if nUp
say("New York", true, nH)
if nDn
say("New York", false, nL)
// ───────── Watermark and timeframe note
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, "Use a chart timeframe of " + str.tostring(lenMin) + " minutes or less.", text_color = GOLD, bgcolor = color.new(color.black, 20), text_size = size.small)