PINE LIBRARY
ExpEngine

The engine behind the EXP GRID / EXP OVERLAY reconstruction pair.
An indicator is either overlay or pane, never both. So a study that wants to draw levels on price AND report statistics about those levels has to be two scripts — and two scripts means two copies of the decision logic, and two copies drift. This library exists so that they cannot: the pane cannot measure a different trade than the price chart draws, because there is only one definition of it.
WHAT IS IN HERE
context() — nine signed volume and efficiency features, each clamped to [-1, 1], and the composite they average into.
aligned() / plan() — the arming condition and the trade state machine: arm on alignment, enter on a break of the prior bar in the armed direction, exit on stop, target, or the clock, whichever comes first. Tracks MFE, MAE, realised R and a running win/loss record.
shadow() — a random-entry baseline that runs under the IDENTICAL exit rule. A base rate computed under a different exit rule is not a base rate.
TWO THINGS WORTH KNOWING
When both the stop and the target are touched inside a single bar, the intrabar path is unknowable, so plan() assumes the STOP filled first. Calling that one a win is the most common way a backtest lies to you.
macroBundle() applies [1] to every leg and is meant to be called with lookahead_on. That pairing is the only one of the four offset/lookahead combinations that reads a CLOSED higher-timeframe bar in both history and realtime; change one without the other and the script either leaks the future or disagrees with itself live. A library cannot make the request itself — Pine rejects a request.*() whose expression depends on an exported function's arguments (CE10051) — so the call site stays in your script:
[mEma, mAtr, mClose] = request.security(syminfo.tickerid, tf, es.macroBundle(len, aLen), lookahead = barmerge.lookahead_on)
float macroAtr = es.macroBias(mEma, mAtr, mClose)
Everything here reads confirmed bars only.
Library "ExpEngine"
version()
macroBundle(len, aLen)
Parameters:
len (simple int)
aLen (simple int)
macroBias(mEma, mAtr, mClose)
Parameters:
mEma (float)
mAtr (float)
mClose (float)
context(volLen, erFastLen, erSlowLen, atrLen, macroAtr)
Parameters:
volLen (simple int)
erFastLen (simple int)
erSlowLen (simple int)
atrLen (simple int)
macroAtr (float)
aligned(c, sessOpen, strongBand, armRvol)
Parameters:
c (Ctx)
sessOpen (bool)
strongBand (float)
armRvol (float)
plan(c, ok, stopAtr, rr, timeExit)
Parameters:
c (Ctx)
ok (bool)
stopAtr (float)
rr (float)
timeExit (int)
shadow(c, sessOpen, every, stopAtr, rr, timeExit)
Parameters:
c (Ctx)
sessOpen (bool)
every (simple int)
stopAtr (float)
rr (float)
timeExit (int)
Ctx
Fields:
fRvol (series float)
fPress (series float)
fCvd (series float)
fVwma (series float)
fEff (series float)
fRange (series float)
fClv (series float)
fMacro (series float)
fPersist (series float)
composite (series float)
macroAtr (series float)
rvol (series float)
atr (series float)
erF (series float)
erS (series float)
hasVol (series bool)
Plan
Fields:
state (series int)
dir (series int)
entry (series float)
stop (series float)
target (series float)
risk (series float)
entryBar (series int)
armed (series bool)
entered (series bool)
exited (series bool)
win (series bool)
rMult (series float)
exitPx (series float)
mfe (series float)
mae (series float)
mfeBar (series int)
lastMfe (series float)
lastMfeMin (series float)
wins (series int)
losses (series int)
avgWinBars (series float)
avgLossBars (series float)
avgMaeWin (series float)
sumR (series float)
Tally
Fields:
state (series int)
dir (series int)
entry (series float)
stop (series float)
target (series float)
bar (series int)
wins (series int)
losses (series int)
sumR (series float)
An indicator is either overlay or pane, never both. So a study that wants to draw levels on price AND report statistics about those levels has to be two scripts — and two scripts means two copies of the decision logic, and two copies drift. This library exists so that they cannot: the pane cannot measure a different trade than the price chart draws, because there is only one definition of it.
WHAT IS IN HERE
context() — nine signed volume and efficiency features, each clamped to [-1, 1], and the composite they average into.
aligned() / plan() — the arming condition and the trade state machine: arm on alignment, enter on a break of the prior bar in the armed direction, exit on stop, target, or the clock, whichever comes first. Tracks MFE, MAE, realised R and a running win/loss record.
shadow() — a random-entry baseline that runs under the IDENTICAL exit rule. A base rate computed under a different exit rule is not a base rate.
TWO THINGS WORTH KNOWING
When both the stop and the target are touched inside a single bar, the intrabar path is unknowable, so plan() assumes the STOP filled first. Calling that one a win is the most common way a backtest lies to you.
macroBundle() applies [1] to every leg and is meant to be called with lookahead_on. That pairing is the only one of the four offset/lookahead combinations that reads a CLOSED higher-timeframe bar in both history and realtime; change one without the other and the script either leaks the future or disagrees with itself live. A library cannot make the request itself — Pine rejects a request.*() whose expression depends on an exported function's arguments (CE10051) — so the call site stays in your script:
[mEma, mAtr, mClose] = request.security(syminfo.tickerid, tf, es.macroBundle(len, aLen), lookahead = barmerge.lookahead_on)
float macroAtr = es.macroBias(mEma, mAtr, mClose)
Everything here reads confirmed bars only.
Library "ExpEngine"
version()
macroBundle(len, aLen)
Parameters:
len (simple int)
aLen (simple int)
macroBias(mEma, mAtr, mClose)
Parameters:
mEma (float)
mAtr (float)
mClose (float)
context(volLen, erFastLen, erSlowLen, atrLen, macroAtr)
Parameters:
volLen (simple int)
erFastLen (simple int)
erSlowLen (simple int)
atrLen (simple int)
macroAtr (float)
aligned(c, sessOpen, strongBand, armRvol)
Parameters:
c (Ctx)
sessOpen (bool)
strongBand (float)
armRvol (float)
plan(c, ok, stopAtr, rr, timeExit)
Parameters:
c (Ctx)
ok (bool)
stopAtr (float)
rr (float)
timeExit (int)
shadow(c, sessOpen, every, stopAtr, rr, timeExit)
Parameters:
c (Ctx)
sessOpen (bool)
every (simple int)
stopAtr (float)
rr (float)
timeExit (int)
Ctx
Fields:
fRvol (series float)
fPress (series float)
fCvd (series float)
fVwma (series float)
fEff (series float)
fRange (series float)
fClv (series float)
fMacro (series float)
fPersist (series float)
composite (series float)
macroAtr (series float)
rvol (series float)
atr (series float)
erF (series float)
erS (series float)
hasVol (series bool)
Plan
Fields:
state (series int)
dir (series int)
entry (series float)
stop (series float)
target (series float)
risk (series float)
entryBar (series int)
armed (series bool)
entered (series bool)
exited (series bool)
win (series bool)
rMult (series float)
exitPx (series float)
mfe (series float)
mae (series float)
mfeBar (series int)
lastMfe (series float)
lastMfeMin (series float)
wins (series int)
losses (series int)
avgWinBars (series float)
avgLossBars (series float)
avgMaeWin (series float)
sumR (series float)
Tally
Fields:
state (series int)
dir (series int)
entry (series float)
stop (series float)
target (series float)
bar (series int)
wins (series int)
losses (series int)
sumR (series float)
Pine library
In true PulseWire spirit, the author has published this Pine code as an open-source library so that other Pine programmers from our community can reuse it. Cheers to the author! You may use this library privately or in other open-source publications, but reuse of this code in publications is governed by House Rules.
I build Pine Script tooling. Quanticator turns plain-English descriptions into Pine Script v6 source - quanticator.app
Disclaimer
The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by PulseWire. Read more in the Terms of Use.
Pine library
In true PulseWire spirit, the author has published this Pine code as an open-source library so that other Pine programmers from our community can reuse it. Cheers to the author! You may use this library privately or in other open-source publications, but reuse of this code in publications is governed by House Rules.
I build Pine Script tooling. Quanticator turns plain-English descriptions into Pine Script v6 source - quanticator.app
Disclaimer
The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by PulseWire. Read more in the Terms of Use.
