Perfected FlowScript
Initial Balance
Initial Balance — Perfected Edition is indexed as 244_initial_balance.flowscript. The exact FlowScript is viewable here; chart observe and trading automation use the current mmflow chart/recipe signal proxy until FlowScript v2 transpilation is available.
This exact FlowScript uses the v2 source dialect. It is viewable and connected to the chart/automation proxy, but direct editor execution needs the FlowScript v2 transpiler.
Dialect
flowscript-v2
Compatibility
flowscript-v2-needs-transpile
Position
onchart
Axis
false
Size
15.5 KB
Lines
511
Native Signal Candidates
4 source-level chart marker triggers extracted from guarded FlowScript v2 blocks.
IB High Break
line 438:9
Trigger
(showShapes && ibSet) && (ibHighBreak)
Value
ibHigh
Deps
ibHighBreak:var, ibSet:static, showShapes:input
IB Low Break
line 448:9
Trigger
(showShapes && ibSet) && (ibLowBreak)
Value
ibLow
Deps
ibLowBreak:var, ibSet:static, showShapes:input
Initiative Buy
line 458:9
Trigger
(showShapes && ibSet) && (px > ibHigh && px[1] <= ibHigh[1])
Value
px
Deps
ibHigh:var, ibSet:static, px:timeseries, showShapes:input
Initiative Sell
line 468:9
Trigger
(showShapes && ibSet) && (px < ibLow && px[1] >= ibLow[1])
Value
px
Deps
ibLow:var, ibSet:static, px:timeseries, showShapes:input
244_initial_balance.flowscript
source path: kscripts/perfected/244_initial_balance.flowscript · sha256:8d612e2217c07566480d38425d7045c313444d0245fdb1952538b55ac8bddd62
//@version=2
// ============================================================================
// Initial Balance — Perfected Edition
// Author: T (@turkishcryptox) | Enhanced by kScript Perfection Engineer
// Tags: CORE, #initial_balance, #market_profile, #session, #opening_range, #pivot
//
// Production-quality Initial Balance (IB) indicator with proper session
// anchoring, IB extensions, volume-confirmed break detection, and color-coded
// regime classification per Steidlmayer's Market Profile methodology.
//
// Research papers underpinning this design:
// 1. Steidlmayer & Hawkins (2003), "Steidlmayer on Markets: Trading with
// Market Profile" (2nd ed.): IB is the price range in the first hour of
// the session, used as a reference point for the rest of the day. Value
// Area (~70% of volume), POC, and IB extensions are core concepts.
// 2. Murphy (1999), "Technical Analysis of the Financial Markets": the
// opening range (first few minutes or hour) becomes support/resistance
// for the rest of the session; widely applicable to futures and equities.
// 3. Harris (1986), JFE 16(1):99-117 — "A Transaction Data Study of Weekly
// and Intradaily Patterns": significant intraday seasonality in the first
// 45 minutes after market open; volatility is highest in the opening hour.
// 4. Crabel (1990), "Playing the Opening Range Breakout": once price moves
// away from the open after a narrow range day, it normally does not return;
// ORB effectiveness is rooted in institutional order-flow clustering at open.
// 5. Heston, Korajczyk & Sadka (2010), J. Finance 65(4):1369-1407: return
// effects are pronounced in the first and last half-hours of trading,
// with the opening half-hour earning over 11 basis points in daily decile
// spread — validating the IB as a high-information window.
// ============================================================================
define(
title="Initial Balance — Perfected Edition",
position="onchart",
axis=false
)
// -----------------------------------------------------------------------------
// 1. USER INPUTS — grouped with constraints
// -----------------------------------------------------------------------------
// --- Session Configuration ---
var sessionBars = input(
name="sessionBars",
type="number",
defaultValue=12,
label="IB Window (bars)",
constraints={min: 1, max: 100, step: 1},
group="Session"
)
var useTimeSession = input(
name="useTimeSession",
type="boolean",
defaultValue=false,
label="Use Time-Based Session (vs Bar-Based)",
group="Session"
)
var sessionStartHour = input(
name="sessionStartHour",
type="number",
defaultValue=9,
label="Session Start Hour (UTC)",
constraints={min: 0, max: 23, step: 1},
group="Session"
)
var sessionStartMin = input(
name="sessionStartMin",
type="number",
defaultValue=30,
label="Session Start Minute",
constraints={min: 0, max: 59, step: 1},
group="Session"
)
// --- Extensions ---
var showExtensions = input(
name="showExtensions",
type="boolean",
defaultValue=true,
label="Show IB Extensions",
group="Extensions"
)
var ext1Mult = input(
name="ext1Mult",
type="number",
defaultValue=1.5,
label="Extension 1 Multiplier",
constraints={min: 0.5, max: 5.0, step: 0.1},
group="Extensions"
)
var ext2Mult = input(
name="ext2Mult",
type="number",
defaultValue=2.0,
label="Extension 2 Multiplier",
constraints={min: 0.5, max: 5.0, step: 0.1},
group="Extensions"
)
var ext3Mult = input(
name="ext3Mult",
type="number",
defaultValue=3.0,
label="Extension 3 Multiplier",
constraints={min: 0.5, max: 5.0, step: 0.1},
group="Extensions"
)
// --- Volume Confirmation ---
var useVolConfirm = input(
name="useVolConfirm",
type="boolean",
defaultValue=true,
label="Use Volume Confirmation for Breaks",
group="Volume"
)
var volLookback = input(
name="volLookback",
type="number",
defaultValue=20,
label="Volume Lookback",
constraints={min: 5, max: 100, step: 1},
group="Volume"
)
var volMultiplier = input(
name="volMultiplier",
type="number",
defaultValue=1.2,
label="Volume Threshold Multiplier",
constraints={min: 0.5, max: 5.0, step: 0.1},
group="Volume"
)
// --- Display ---
var showMid = input(
name="showMid",
type="boolean",
defaultValue=true,
label="Show IB Midpoint",
group="Display"
)
var showShapes = input(
name="showShapes",
type="boolean",
defaultValue=true,
label="Show Break Shapes",
group="Display"
)
var showFill = input(
name="showFill",
type="boolean",
defaultValue=true,
label="Show IB Zone Fill",
group="Display"
)
var showTable = input(
name="showTable",
type="boolean",
defaultValue=true,
label="Show Status Table",
group="Display"
)
// --- Colors ---
var highColor = input(
name="highColor",
type="color",
defaultValue="#26a69a",
label="IB High Color",
group="Colors"
)
var lowColor = input(
name="lowColor",
type="color",
defaultValue="#ef5350",
label="IB Low Color",
group="Colors"
)
var midColor = input(
name="midColor",
type="color",
defaultValue="#ffeb3b",
label="IB Midpoint Color",
group="Colors"
)
var extColor = input(
name="extColor",
type="color",
defaultValue="#9e9e9e",
label="Extension Color",
group="Colors"
)
var breakColor = input(
name="breakColor",
type="color",
defaultValue="#00e676",
label="Breakout Color",
group="Colors"
)
var breakDownColor = input(
name="breakDownColor",
type="color",
defaultValue="#ff5252",
label="Breakdown Color",
group="Colors"
)
// -----------------------------------------------------------------------------
// 2. DATA SOURCES & NaN GUARD
// -----------------------------------------------------------------------------
timeseries bars = ohlcv(symbol=currentSymbol, exchange=currentExchange)
timeseries px = bars.close
timeseries high = bars.high
timeseries low = bars.low
timeseries vol = bars.volume
timeseries t = bars.time
if (isnan(px[0])) {
return
}
// -----------------------------------------------------------------------------
// 3. SESSION DETECTION — bar-based or time-based
// -----------------------------------------------------------------------------
// Per Harris (1986) and Steidlmayer (2003), the IB is established in the
// first hour of the trading session. We detect session boundaries to reset
// the IB calculation.
var barInSession = 0
var newSession = false
if (useTimeSession) {
// Time-based session: detect hour/minute boundary
var currentHour = hour(t)
var currentMin = minute(t)
newSession = (currentHour == sessionStartHour && currentMin >= sessionStartMin) || (currentHour > sessionStartHour && (currentHour[1] < sessionStartHour || currentHour[1] == sessionStartHour && minute(t[1]) < sessionStartMin))
barInSession = newSession ? 0 : barInSession + 1
} else {
// Bar-based session: reset every N bars
barInSession = bar_index % sessionBars
newSession = barInSession == 0
}
// -----------------------------------------------------------------------------
// 4. IB COMPUTATION — rolling high/low within session window
// -----------------------------------------------------------------------------
// IB High = highest high in first N bars of session
// IB Low = lowest low in first N bars of session
// IB Mid = (High + Low) / 2
// Reset tracking on new session
static sessionIbHigh = 0.0
static sessionIbLow = 0.0
static ibSet = false
static ibSetBar = 0
if (newSession) {
sessionIbHigh = high[0]
sessionIbLow = low[0]
ibSet = false
ibSetBar = 0
}
// Track IB during the first N bars
var inIbWindow = barInSession < sessionBars
if (inIbWindow) {
if (high[0] > sessionIbHigh) {
sessionIbHigh = high[0]
}
if (low[0] < sessionIbLow) {
sessionIbLow = low[0]
}
ibSetBar = ibSetBar + 1
if (ibSetBar >= sessionBars) {
ibSet = true
}
}
// IB values once set, persist for rest of session
var ibHigh = sessionIbHigh
var ibLow = sessionIbLow
var ibMid = (ibHigh + ibLow) / 2.0
var ibRange = ibHigh - ibLow
// -----------------------------------------------------------------------------
// 5. IB EXTENSIONS — Steidlmayer-style measured moves
// -----------------------------------------------------------------------------
// Extensions project the IB range above/below the IB to identify potential
// targets if price breaks out of the initial balance.
var ext1Up = ibHigh + ibRange * (ext1Mult - 1.0)
var ext1Dn = ibLow - ibRange * (ext1Mult - 1.0)
var ext2Up = ibHigh + ibRange * (ext2Mult - 1.0)
var ext2Dn = ibLow - ibRange * (ext2Mult - 1.0)
var ext3Up = ibHigh + ibRange * (ext3Mult - 1.0)
var ext3Dn = ibLow - ibRange * (ext3Mult - 1.0)
// -----------------------------------------------------------------------------
// 6. REGIME DETECTION — price position relative to IB
// -----------------------------------------------------------------------------
// Above IB = initiative buying (bullish)
// Below IB = initiative selling (bearish)
// Inside IB = responsive activity (neutral / two-sided)
var regimeBull = px > ibHigh
var regimeBear = px < ibLow
var regimeNeutral = !regimeBull && !regimeBear
var regimeColor = regimeBull ? breakColor : (regimeBear ? breakDownColor : "#9e9e9e")
// -----------------------------------------------------------------------------
// 7. VOLUME CONFIRMATION — for break validity
// -----------------------------------------------------------------------------
// Per Crabel (1990) and BuildAlpha (2026), volume-confirmed breaks are more
// reliable than breaks on low volume. We require volume above rolling average.
timeseries avgVol = sma(source=vol, period=volLookback)
var volConfirm = vol > avgVol * volMultiplier
// -----------------------------------------------------------------------------
// 8. BREAK DETECTION — with static tracking
// -----------------------------------------------------------------------------
// IB High Break: price crosses above IB high (initiative buying)
// IB Low Break: price crosses below IB low (initiative selling)
var ibHighBreak = px > ibHigh && px[1] <= ibHigh[1]
var ibLowBreak = px < ibLow && px[1] >= ibLow[1]
var ibHighBreakConfirmed = ibHighBreak && volConfirm
var ibLowBreakConfirmed = ibLowBreak && volConfirm
// Static tracking for break counts and session stats
static ibHighBreakCount = 0
static ibLowBreakCount = 0
static ibHighBreakConfirmCount = 0
static ibLowBreakConfirmCount = 0
static lastBarIndex = 0
var currentBarIndex = bar_index[0]
if (!isnan(currentBarIndex) && currentBarIndex != lastBarIndex) {
lastBarIndex = currentBarIndex
if (ibHighBreak) {
ibHighBreakCount = ibHighBreakCount + 1
if (volConfirm) {
ibHighBreakConfirmCount = ibHighBreakConfirmCount + 1
}
}
if (ibLowBreak) {
ibLowBreakCount = ibLowBreakCount + 1
if (volConfirm) {
ibLowBreakConfirmCount = ibLowBreakConfirmCount + 1
}
}
}
// -----------------------------------------------------------------------------
// 9. PLOTTING
// -----------------------------------------------------------------------------
// --- IB Zone Fill ---
if (showFill && ibSet) {
var fillBull = regimeBull
plotLine(
value=[ibHigh, ibLow],
width=1,
colors=[transparency(breakColor, 20), transparency(breakDownColor, 20)],
colorIndex=fillBull ? 0 : 1,
fill=true,
label=["IB High", "IB Low"],
desc=["Initial Balance high zone", "Initial Balance low zone"]
)
}
// --- IB High / Low Lines ---
if (ibSet) {
plotLine(
value=ibHigh,
colors=[highColor],
width=2,
label=["IB High"],
desc=["Initial Balance high (session reference, first " + sessionBars + " bars)"]
)
plotLine(
value=ibLow,
colors=[lowColor],
width=2,
label=["IB Low"],
desc=["Initial Balance low (session reference, first " + sessionBars + " bars)"]
)
}
// --- IB Midpoint ---
if (showMid && ibSet) {
plotLine(
value=ibMid,
colors=[midColor],
width=1,
label=["IB Mid"],
desc=["Initial Balance midpoint (pivot level)"]
)
}
// --- Extensions ---
if (showExtensions && ibSet) {
plotLine(
value=ext1Up,
colors=[transparency(extColor, 50)],
width=1,
label=["Ext 1.5x"],
desc=["IB Extension 1.5x above high"]
)
plotLine(
value=ext1Dn,
colors=[transparency(extColor, 50)],
width=1,
label=["Ext 1.5x"],
desc=["IB Extension 1.5x below low"]
)
plotLine(
value=ext2Up,
colors=[transparency(extColor, 40)],
width=1,
label=["Ext 2x"],
desc=["IB Extension 2x above high"]
)
plotLine(
value=ext2Dn,
colors=[transparency(extColor, 40)],
width=1,
label=["Ext 2x"],
desc=["IB Extension 2x below low"]
)
}
// --- Break Shapes ---
if (showShapes && ibSet) {
if (ibHighBreak) {
plotShape(
value=ibHigh,
shape="triangleup",
colorIndex=volConfirm ? breakColor : midColor,
width=3,
label=["IB High Break"],
desc=["Price broke above IB High" + (volConfirm ? " (volume confirmed)" : " (low volume)")]
)
}
if (ibLowBreak) {
plotShape(
value=ibLow,
shape="triangledown",
colorIndex=volConfirm ? breakDownColor : midColor,
width=3,
label=["IB Low Break"],
desc=["Price broke below IB Low" + (volConfirm ? " (volume confirmed)" : " (low volume)")]
)
}
if (px > ibHigh && px[1] <= ibHigh[1]) {
plotShape(
value=px,
shape="diamond",
colorIndex=breakColor,
width=2,
label=["Initiative Buy"],
desc=["Initiative buying above IB high (Steidlmayer)"]
)
}
if (px < ibLow && px[1] >= ibLow[1]) {
plotShape(
value=px,
shape="diamond",
colorIndex=breakDownColor,
width=2,
label=["Initiative Sell"],
desc=["Initiative selling below IB low (Steidlmayer)"]
)
}
}
// -----------------------------------------------------------------------------
// 10. REGIME TABLE (last bar only)
// -----------------------------------------------------------------------------
if (showTable && isLastBar && ibSet) {
var regimeText = regimeBull ? "ABOVE IB (INITIATIVE BUY)" : (regimeBear ? "BELOW IB (INITIATIVE SELL)" : "INSIDE IB (RESPONSIVE)")
var regimeTextColor = regimeBull ? breakColor : (regimeBear ? breakDownColor : "#9e9e9e")
plotTable(
data=[
["Initial Balance Pro", regimeText],
["IB High", ibHigh[0]],
["IB Low", ibLow[0]],
["IB Mid", ibMid[0]],
["IB Range", ibRange[0]],
["High Breaks", ibHighBreakCount],
["Low Breaks", ibLowBreakCount],
["Confirmed High", ibHighBreakConfirmCount],
["Confirmed Low", ibLowBreakConfirmCount]
],
position="top_right",
headerRow=true,
headerColumn=true,
textColor="#e2e8f0",
headerTextColor="#ffffff",
backgroundColor=transparency("#0f172a", 40),
headerBackgroundColor=transparency("#1e293b", 40),
borderColor=transparency("#334155", 40),
borderWidth=1,
fontSize=10,
cellPadding=4
)
}