本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
// Session Initial Balance [Badr Edge] · v1.0.0
// Marks the first hour (Initial Balance) of the Asia, London and New York sessions, compares its width with recent sessions,
// and marks the first candle close outside it. Educational chart-marking tool. Not a trade signal.
indicator("Session Initial Balance [Badr Edge]", shorttitle = "BE IB", 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 = "Initial Balance"
lenMin = input.int(60, "Initial Balance length (minutes)", options = [30, 60, 90, 120], 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)
showWidth = input.bool(true, "Width note", group = G_R, tooltip = "Prints the Initial Balance width and how it compares with the average of the same session's recent Initial Balances.")
avgN = input.int(10, "Sessions in the width average", minval = 3, maxval = 50, group = G_R)
showMarks = input.bool(true, "Breakout markers", group = G_R, tooltip = "A small triangle on the first candle that closes outside the Initial Balance. 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 an Initial Balance", 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 Initial Balance per session
initialBalance(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
var array<float> widths = array.new<float>()
float ratio = 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, 91), text = name + " IB", 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
float w = hi - lo
if widths.size() >= 3
ratio := w / widths.avg()
if showWidth
bx.set_text(name + " IB " + str.tostring(w, format.mintick) + " · " + str.tostring(ratio, "0.0") + "x avg")
widths.push(w)
if widths.size() > avgN
widths.shift()
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, ratio]
[aH, aL, aUp, aDn, aR] = initialBalance(showAsia, asiaSess, "Asia/Tokyo", "ASIA", colAsia)
[lH, lL, lUp, lDn, lR] = initialBalance(showLon, lonSess, "Europe/London", "LONDON", colLon)
[nH, nL, nUp, nDn, nR] = initialBalance(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 Initial Balance", shape.triangleup, location.belowbar, color.new(TEAL, 0), size = size.tiny)
plotshape(showMarks and anyDn, "Close below Initial Balance", shape.triangledown, location.abovebar, color.new(ROSE, 0), size = size.tiny)
// ───────── Data Window values
plot(aH, "Asia IB high", display = display.data_window)
plot(aL, "Asia IB low", display = display.data_window)
plot(lH, "London IB high", display = display.data_window)
plot(lL, "London IB low", display = display.data_window)
plot(nH, "New York IB high", display = display.data_window)
plot(nL, "New York IB low", display = display.data_window)
plot(nz(aR, nz(lR, nR)), "IB width vs average (x)", display = display.data_window)
// ───────── Alerts (bar close only)
say(string sessName, bool up, float lvl) =>
alert(syminfo.ticker + " closed " + (up ? "above" : "below") + " the " + sessName + " Initial Balance " + (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)