PINE LIBRARY
Zaktualizowano

ml_session

213
ml_session is a dependency-free library for Pine v6 that fixes a blind spot every intraday study shares: a flat rolling average has no idea what time it is. Intraday volume and range have a strong U-shape — heavy at the open and the close, thin around lunch — so a normal open prints as a "volume spike" against a flat baseline, and a genuinely quiet mid-session bar looks average. This library judges the current bar against the same time-of-day slot on prior sessions, and lets you rank which slots of the day your signals actually pay in.

How it works

Each bar maps to a wall-clock slot (say every 5 minutes). The library keeps one rolling mean / stdev per slot, updated only when that slot occurs, so "how unusual is now" is always measured against this time of day's own history. The estimators are exponential — an N-session memory — so there are no large buffers and nothing repaints.

Slot mapping
slotOf(slotMinutes) — the time-of-day slot index for the current bar, from the symbol's exchange clock ((hour·60 + minute) ÷ slotMinutes). For NSE that's IST, so it lines up with NIFTY's 09:15–15:30 session.
slotCount(slotMinutes) — how many slots cover a 24h day at that granularity. Pass it as nSlots to size the per-slot state.
sessionAlpha(sessions) — the EMA weight for an N-session memory (≈ 2 / (N+1)). Feed it to the estimators below.
slotLabel(slot, slotMinutes) — an "HH:MM" label for a slot, for dashboards.
Per-slot baselines
slotMean(src, slot, nSlots, alpha) — the rolling mean of src for this time-of-day slot: the baseline itself.
slotStdev(src, slot, nSlots, alpha) — the rolling dispersion for this slot.
slotZ(src, slot, nSlots, alpha) — the time-of-day z-score, (src − slot mean) ÷ slot stdev, in one call. "How unusual is this bar for this time of day." The core self-calibrating read — pass volume, range, or any intraday series.
slotRatio(src, slot, nSlots, alpha) — src ÷ slot mean (1.0 = a normal reading for this time of day, 2.0 = twice the usual). Ideal for volume — "heavy for the open", not "heavy vs a flat average".
Time-of-day edge ranking
slotHitRate(add, win, slot, nSlots) — per-slot forward-test bookkeeping. When a signal outcome resolves, call with add = true and win = true/false, passing the signal bar's slot (e.g. slot[horizon]); it returns that slot's running hit rate (%). Use it to see which parts of the session your signal works in — and which to sit out.
slotCountN(add, slot, nSlots) — the sample count accrued for a slot, so you can weight its hit rate by confidence.
How to use

Make "high volume" mean high for this time of day, and pair it with a time-of-day edge read:

//version=6
indicator("Example — time-of-day baselines", overlay = false)
import Market_Logic_India/ml_session/1 as sess

slotMin = input.int(5, "Slot minutes")
memory = input.int(20, "Session memory")

n = sess.slotCount(slotMin)
a = sess.sessionAlpha(memory)
sl = sess.slotOf(slotMin)

volRatio = sess.slotRatio(volume, sl, n, a) // volume vs its time-of-day norm
volZ = sess.slotZ(volume, sl, n, a) // standardized for this slot
plot(volRatio, "Vol vs ToD", color = volRatio > 1.5 ? color.orange : color.gray)

// time-of-day edge (host resolves `win` at its horizon):
// hit = sess.slotHitRate(resolvedNow, win, sl[horizon], n)

Pairs naturally with a VSA / effort-vs-result read: a true "climactic" bar is one whose volume is extreme for its slot, not merely above a flat mean.

Notes
Non-repainting: every read is a pure function of the values you pass and per-slot state that only moves forward. Feed confirmed-bar values (gate on barstate.isconfirmed) and the baselines never look ahead. No ta.* inside, so nothing can short-circuit.
Warm-up: each slot needs a few sessions before its baseline is meaningful; early bars return the seed value or na.
Types: pass series for the source and slot, simple int for nSlots, and simple float for alpha.
The clock is the symbol's exchange timezone, so it's correct for NSE without configuration; on a 24h symbol every slot simply fills.
Concept credits

Intraday seasonality — the U-shaped time-of-day profile of volume and volatility — is long established in market-microstructure research. This library is an original, dependency-free Pine v6 packaging of that idea; it is not affiliated with, nor endorsed by, any originator.

License

Mozilla Public License 2.0 — as required for TradingView libraries (open source). Free to import and build on.

Informacje o Wersji
Adds day-auction anchors: isNewDay, inOpeningWindow (opening-lock / IB window), initialBalance (opening-range high/low), ibBreak, and slotSignificant (one-call time-of-day significance gate over slotZ). All v1 functions unchanged; import bumps to /2.


v2

Added:
isNewDay()

inOpeningWindow(startHour, startMin, windowMins)
  Parameters:
    startHour (simple int)
    startMin (simple int)
    windowMins (simple int)

initialBalance(isNewSession, inIB, h, l)
  Parameters:
    isNewSession (bool)
    inIB (bool)
    h (float)
    l (float)

ibBreak(c, ibHigh, ibLow)
  Parameters:
    c (float)
    ibHigh (float)
    ibLow (float)

slotSignificant(src, slot, nSlots, alpha, zThr)
  Parameters:
    src (float)
    slot (int)
    nSlots (simple int)
    alpha (simple float)
    zThr (simple float)

Wyłączenie odpowiedzialności

Informacje i publikacje nie stanowią i nie powinny być traktowane jako porady finansowe, inwestycyjne, tradingowe ani jakiekolwiek inne rekomendacje dostarczane lub zatwierdzone przez TradingView. Więcej informacji znajduje się w Warunkach użytkowania.