Library

ChatgptLibraryLibrary "ChatgptLibrary"
TODO: add library description here
effective_period(high_series, low_series, volume_series, period_length, lookback_length, max_search)
Calculates adaptive effective period.
Parameters:
high_series (float) : High price series.
low_series (float) : Low price series.
volume_series (float) : Volume series.
period_length (simple int) : Base period.
lookback_length (simple int) : EMA lookback multiplier.
max_search (int) : Maximum search distance.
Returns: Adaptive effective period.
adaptive_ema(source, high_series, low_series, volume_series, period_length, lookback_length, max_search)
Adaptive EMA using effective period.
Parameters:
source (float) : Source series.
high_series (float) : High price series.
low_series (float) : Low price series.
volume_series (float) : Volume series.
period_length (simple int) : Base period.
lookback_length (simple int) : EMA lookback multiplier.
max_search (int) : Maximum search distance.
Returns: Adaptive EMA, alpha and effective period.
adaptive_channel(high_series, low_series, volume_series, period_length, lookback_length, smooth_length, max_search)
Adaptive price channel.
Parameters:
high_series (float) : High price series.
low_series (float) : Low price series.
volume_series (float) : Volume series.
period_length (simple int) : Base period.
lookback_length (simple int) : EMA lookback multiplier.
smooth_length (simple int) : EMA smoothing.
max_search (int) : Maximum search distance.
Returns: Effective period, upper, lower, middle and width.
adaptive_rsi(source, high_series, low_series, volume_series, period_length, lookback_length, max_search)
Adaptive RSI.
Parameters:
source (float) : Source series.
high_series (float) : High price series.
low_series (float) : Low price series.
volume_series (float) : Volume series.
period_length (simple int) : Base period.
lookback_length (simple int) : EMA lookback multiplier.
max_search (int) : Maximum search distance.
Returns: Adaptive RSI and effective period.
adaptive_atr(high_series, low_series, close_series, volume_series, period_length, lookback_length, max_search)
Adaptive ATR.
Parameters:
high_series (float) : High price series.
low_series (float) : Low price series.
close_series (float) : Close price series.
volume_series (float) : Volume series.
period_length (simple int) : Base period.
lookback_length (simple int) : EMA lookback multiplier.
max_search (int) : Maximum search distance.
Returns: Adaptive ATR and effective period.
adaptive_macd(source, high_series, low_series, volume_series, fast_period, slow_period, signal_period, lookback_length, max_search)
Adaptive MACD.
Parameters:
source (float) : Source series.
high_series (float) : High price series.
low_series (float) : Low price series.
volume_series (float) : Volume series.
fast_period (simple int) : Fast adaptive period.
slow_period (simple int) : Slow adaptive period.
signal_period (int) : Signal EMA period.
lookback_length (simple int) : EMA lookback multiplier.
max_search (int) : Maximum search distance.
Returns: MACD, Signal, Histogram.
adaptive_bollinger(source, high_series, low_series, volume_series, period_length, deviation, lookback_length, max_search)
Adaptive Bollinger Bands.
Parameters:
source (float) : Source series.
high_series (float) : High price series.
low_series (float) : Low price series.
volume_series (float) : Volume series.
period_length (simple int) : Base period.
deviation (float) : Standard deviation multiplier.
lookback_length (simple int) : EMA lookback multiplier.
max_search (int) : Maximum search distance.
Returns: Upper band, Middle band, Lower band, Band width and Effective period.
adaptive_supertrend(high_series, low_series, close_series, volume_series, period_length, multiplier, lookback_length, max_search)
Adaptive SuperTrend.
Parameters:
high_series (float) : High price series.
low_series (float) : Low price series.
close_series (float) : Close price series.
volume_series (float) : Volume series.
period_length (simple int) : Base period.
multiplier (float) : ATR multiplier.
lookback_length (simple int) : EMA lookback multiplier.
max_search (int) : Maximum search distance.
Returns: SuperTrend, Trend Direction and Effective Period.
adaptive_donchian(high_series, low_series, volume_series, period_length, lookback_length, max_search)
Adaptive Donchian Channel.
Parameters:
high_series (float) : High price series.
low_series (float) : Low price series.
volume_series (float) : Volume series.
period_length (simple int) : Base period.
lookback_length (simple int) : EMA lookback multiplier.
max_search (int) : Maximum search distance.
Returns: Upper band, Lower band, Middle line, Width and Effective period.
adaptive_keltner(source, high_series, low_series, close_series, volume_series, period_length, multiplier, lookback_length, max_search)
Adaptive Keltner Channel.
Parameters:
source (float) : Source series.
high_series (float) : High price series.
low_series (float) : Low price series.
close_series (float) : Close price series.
volume_series (float) : Volume series.
period_length (simple int) : Base period.
multiplier (float) : ATR multiplier.
lookback_length (simple int) : EMA lookback multiplier.
max_search (int) : Maximum search distance.
Returns: Upper band, Middle band, Lower band, Width and Effective period.
adaptive_adx(high_series, low_series, close_series, volume_series, period_length, lookback_length, max_search)
Adaptive ADX.
Parameters:
high_series (float) : High price series.
low_series (float) : Low price series.
close_series (float) : Close price series.
volume_series (float) : Volume series.
period_length (simple int) : Base period.
lookback_length (simple int) : EMA lookback multiplier.
max_search (int) : Maximum search distance.
Returns: ADX, +DI, -DI and Effective Period.
adaptive_stochastic(close_series, high_series, low_series, volume_series, period_length, smooth_k, smooth_d, lookback_length, max_search)
Adaptive Stochastic.
Parameters:
close_series (float) : Close price series.
high_series (float) : High price series.
low_series (float) : Low price series.
volume_series (float) : Volume series.
period_length (simple int) : Base period.
smooth_k (int) : K smoothing.
smooth_d (int) : D smoothing.
lookback_length (simple int) : EMA lookback multiplier.
max_search (int) : Maximum search distance.
Returns: K, D and Effective Period.
adaptive_cci(high_series, low_series, close_series, volume_series, period_length, lookback_length, max_search)
Adaptive Commodity Channel Index.
Parameters:
high_series (float) : High price series.
low_series (float) : Low price series.
close_series (float) : Close price series.
volume_series (float) : Volume series.
period_length (simple int) : Base period.
lookback_length (simple int) : EMA lookback multiplier.
max_search (int) : Maximum search distance.
Returns: CCI and Effective Period.
adaptive_williams_r(high_series, low_series, close_series, volume_series, period_length, lookback_length, max_search)
Adaptive Williams %R.
Parameters:
high_series (float) : High price series.
low_series (float) : Low price series.
close_series (float) : Close price series.
volume_series (float) : Volume series.
period_length (simple int) : Base period.
lookback_length (simple int) : EMA lookback multiplier.
max_search (int) : Maximum search distance.
Returns: Williams %R and Effective Period.
adaptive_roc(source, high_series, low_series, volume_series, period_length, lookback_length, max_search)
Adaptive Rate of Change.
Parameters:
source (float) : Source series.
high_series (float) : High price series.
low_series (float) : Low price series.
volume_series (float) : Volume series.
period_length (simple int) : Base period.
lookback_length (simple int) : EMA lookback multiplier.
max_search (int) : Maximum search distance.
Returns: ROC and Effective Period.
adaptive_pivot(source, left_bars, right_bars)
Adaptive Pivot Detector.
Parameters:
source (float) : Source series.
left_bars (int) : Left pivot bars.
right_bars (int) : Right pivot bars.
Returns: Pivot High, Pivot Low, Pivot High Price, Pivot Low Price.
adaptive_divergence(price_source, indicator_source, pivot_length)
Adaptive Divergence Detector.
Parameters:
price_source (float) : Price series.
indicator_source (float) : Indicator series.
pivot_length (int) : Pivot length.
Returns: Bullish divergence, Bearish divergence and Divergence strength.
adaptive_pivot_divergence(price_source, signal_source, pivot_length)
Adaptive Pivot Divergence Detector.
Parameters:
price_source (float) : Price series.
signal_source (float) : Indicator series.
pivot_length (int) : Pivot length.
Returns: Bullish divergence, Bearish divergence and Divergence strength.
adaptive_flat_channel(upper_channel, lower_channel, flat_length, tolerance)
Adaptive Flat Channel Detector.
Parameters:
upper_channel (float) : Upper channel.
lower_channel (float) : Lower channel.
flat_length (int) : Number of bars to evaluate.
tolerance (float) : Maximum allowed movement.
Returns: Flat upper, Flat lower and Flat channel.
adaptive_breakout_strength(close_series, upper_channel, lower_channel, channel_width, volume_series, volume_length)
Adaptive Breakout Strength.
Parameters:
close_series (float) : Close price.
upper_channel (float) : Upper channel.
lower_channel (float) : Lower channel.
channel_width (float) : Channel width.
volume_series (float) : Volume.
volume_length (simple int) : Volume EMA length.
Returns: Breakout direction and Breakout strength.
adaptive_channel_rejection(open_series, high_series, low_series, close_series, upper_channel, lower_channel)
Adaptive Channel Rejection.
Parameters:
open_series (float) : Open price.
high_series (float) : High price.
low_series (float) : Low price.
close_series (float) : Close price.
upper_channel (float) : Upper channel.
lower_channel (float) : Lower channel.
Returns: Rejection direction and Rejection strength.
adaptive_channel_compression(channel_width, compression_length)
Adaptive Channel Compression.
Parameters:
channel_width (float) : Width of the channel.
compression_length (simple int) : Number of bars.
Returns: Compression ratio, Is compressing, Is expanding.
adaptive_market_energy(channel_width, volume_series, volume_length)
Adaptive Market Energy.
Parameters:
channel_width (float) : Width of channel.
volume_series (float) : Volume series.
volume_length (simple int) : Volume EMA length.
Returns: Energy score.
adaptive_market_phase(adx, rsi, compression_ratio, breakout_strength)
Adaptive Market Phase.
Parameters:
adx (float) : Adaptive ADX.
rsi (float) : Adaptive RSI.
compression_ratio (float) : Channel compression ratio.
breakout_strength (float) : Breakout strength.
Returns: Market phase.
adaptive_rsi_zigzag(rsi_series, center_level, lookback_length)
Adaptive RSI Zigzag Detector.
Parameters:
rsi_series (float) : RSI series.
center_level (float) : Center level.
lookback_length (int) : Number of bars.
Returns: Zigzag count and Zigzag detected.
adaptive_flat_level(level_series, flat_length, tolerance)
Adaptive Flat Level Detector.
Parameters:
level_series (float) : Channel upper or lower series.
flat_length (int) : Number of bars.
tolerance (float) : Maximum allowed movement.
Returns: Flat state and Flat strength.
adaptive_level_strength(level_series, high_series, low_series, tolerance, lookback_length)
Adaptive Level Strength.
Parameters:
level_series (float) : Support or resistance level.
high_series (float) : High price series.
low_series (float) : Low price series.
tolerance (float) : Touch tolerance.
lookback_length (int) : Number of bars.
Returns: Touch count and Level strength.
adaptive_breakout_probability(breakout_strength, level_strength, compression_ratio, volume_ratio)
Adaptive Breakout Probability.
Parameters:
breakout_strength (float) : Breakout strength.
level_strength (float) : Level strength.
compression_ratio (float) : Channel compression ratio.
volume_ratio (float) : Volume ratio.
Returns: Breakout probability.
adaptive_reversal_probability(rsi, divergence_strength, rejection_strength, flat_strength, channel_width_percent)
Adaptive Reversal Probability.
Parameters:
rsi (float) : Relative Strength Index.
divergence_strength (float) : Divergence strength.
rejection_strength (float) : Rejection strength.
flat_strength (float) : Flat level strength.
channel_width_percent (float) : Channel width percentage.
Returns: Reversal probability.
adaptive_trend_exhaustion(rsi, adx, momentum, roc)
Adaptive Trend Exhaustion.
Parameters:
rsi (float) : Relative Strength Index.
adx (float) : Average Directional Index.
momentum (float) : Momentum.
roc (float) : Rate of Change.
Returns: Trend exhaustion score.
adaptive_channel_memory(upper_channel, lower_channel, tolerance, lookback_length)
Adaptive Channel Memory.
Parameters:
upper_channel (float) : Upper channel.
lower_channel (float) : Lower channel.
tolerance (float) : Maximum channel difference.
lookback_length (int) : Number of bars.
Returns: Memory score.
adaptive_false_breakout(breakout_strength, rejection_strength, volume_ratio)
Adaptive False Breakout Detector.
Parameters:
breakout_strength (float) : Breakout strength.
rejection_strength (float) : Rejection strength.
volume_ratio (float) : Current volume divided by average volume.
Returns: False breakout probability.
adaptive_trap_detector(breakout_direction, breakout_strength, rejection_strength, rsi)
Adaptive Trap Detector.
Parameters:
breakout_direction (int) : Breakout direction.
breakout_strength (float) : Breakout strength.
rejection_strength (float) : Rejection strength.
rsi (float) : Relative Strength Index.
Returns: Trap direction and Trap probability.
adaptive_rsi_behavior(rsi, zigzag_count, divergence_strength, rejection_strength)
Adaptive RSI Behavior.
Parameters:
rsi (float) : Relative Strength Index.
zigzag_count (int) : RSI zigzag count.
divergence_strength (float) : Divergence strength.
rejection_strength (float) : Rejection strength.
Returns: RSI behavior score.
adaptive_market_behavior(trend_strength, reversal_probability, breakout_probability, exhaustion, energy, rsi_behavior)
Adaptive Market Behavior.
Parameters:
trend_strength (float) : Trend strength.
reversal_probability (float) : Reversal probability.
breakout_probability (float) : Breakout probability.
exhaustion (float) : Trend exhaustion.
energy (float) : Market energy.
rsi_behavior (float) : RSI behavior.
Returns: Market behavior score. Library

Library

NeuralMarketsNetworkToolkitLibrary "NeuralMarketsNetworkToolkit"
Open-source network analysis toolkit for Pine Script.
This library provides reusable graph algorithms, matrix utilities and network analytics for building advanced multi-asset indicators. Rather than treating markets as isolated charts, it enables developers to model relationships between assets as weighted networks and extract structural characteristics such as connectivity, centrality, clustering and influence.
Current Modules
• Matrix utilities
• Directed & undirected graphs
• Network analytics
• Node analytics
• Graph algorithms
• Experimental financial network tools
Example Applications
• Correlation networks
• Market leadership analysis
• Sector relationship maps
• Cross-asset dependency analysis
• Financial network research
Design Philosophy
This toolkit provides reusable quantitative building blocks rather than trading signals. Functions are intentionally modular so they can be combined into custom indicators and research projects.
Markets are networks. This toolkit provides the building blocks to analyze them as such.
--------------------------------------------------------------------
matrixIndex(row, col, n)
Converts row/column coordinates into a flat matrix index.
Parameters:
row (int) : Row index.
col (int) : Column index.
n (int) : Matrix dimension.
Returns: Flat-array index.
clamp(x, lo, hi)
Clamp a float.
Parameters:
x (float) : Value.
lo (float) : Minimum.
hi (float) : Maximum.
Returns: Clamped value.
newMatrix(n, initialValue)
Creates an n x n flat matrix initialized to a value.
Parameters:
n (int) : Number of nodes.
initialValue (float) : Initial cell value.
Returns: Flat float array.
setCell(matrix, row, col, n, value)
Sets a matrix cell.
Parameters:
matrix (array) : Flat matrix.
row (int) : Row.
col (int) : Column.
n (int) : Matrix dimension.
value (float) : New value.
getCell(matrix, row, col, n)
Gets a matrix cell.
Parameters:
matrix (array) : Flat matrix.
row (int) : Row.
col (int) : Column.
n (int) : Matrix dimension.
Returns: Cell value.
setUndirectedEdge(matrix, a, b, n, weight)
Sets both directions of an undirected edge.
Parameters:
matrix (array) : Flat matrix.
a (int) : Node A.
b (int) : Node B.
n (int) : Matrix dimension.
weight (float) : Edge weight.
meanAbsoluteConnectivity(matrix, n)
Average absolute pairwise edge weight.
Parameters:
matrix (array) : Symmetric adjacency/weight matrix.
n (int) : Number of nodes.
Returns: Average absolute connectivity from 0 upward.
meanSignedConnectivity(matrix, n)
Average signed pairwise weight.
Parameters:
matrix (array) : Symmetric matrix.
n (int) : Number of nodes.
Returns: Mean signed relationship.
density(matrix, n, threshold)
Proportion of possible edges whose absolute weight exceeds threshold.
Parameters:
matrix (array) : Symmetric weight matrix.
n (int) : Number of nodes.
threshold (float) : Absolute edge threshold.
Returns: Network density from 0 to 1.
fragmentation(matrix, n, threshold)
Network fragmentation as inverse threshold density.
Parameters:
matrix (array) : Symmetric weight matrix.
n (int) : Number of nodes.
threshold (float) : Edge threshold.
Returns: Fragmentation from 0 to 1.
nodeDegree(matrix, n, node, threshold)
Number of strong edges attached to a node.
Parameters:
matrix (array) : Weight matrix.
n (int) : Number of nodes.
node (int) : Node index.
threshold (float) : Absolute edge threshold.
Returns: Degree count.
nodeStrength(matrix, n, node)
Sum of absolute edge weights attached to node.
Parameters:
matrix (array) : Weight matrix.
n (int) : Number of nodes.
node (int) : Node index.
Returns: Node strength.
strongestNode(matrix, n)
Node with greatest absolute network strength.
Parameters:
matrix (array) : Weight matrix.
n (int) : Number of nodes.
Returns: Strongest node index.
averageNodeStrength(matrix, n)
Average node strength.
Parameters:
matrix (array) : Weight matrix.
n (int) : Number of nodes.
Returns: Mean strength.
centralization(matrix, n)
Measures how much one node dominates the network.
Parameters:
matrix (array) : Weight matrix.
n (int) : Number of nodes.
Returns: Strength centralization approximately 0 to 1.
strengthEntropy(matrix, n)
Shannon entropy of node-strength distribution.
Parameters:
matrix (array) : Weight matrix.
n (int) : Number of nodes.
Returns: Normalized entropy from 0 to 1.
mstDistance(matrix, n)
Computes total Prim minimum-spanning-tree distance.
Similarity is converted to distance using 1 - abs(similarity).
Parameters:
matrix (array) : Similarity matrix.
n (int) : Number of nodes.
Returns: Total MST distance.
mstCompactness(matrix, n)
Converts MST distance to compactness.
Parameters:
matrix (array) : Similarity matrix.
n (int) : Number of nodes.
Returns: Network compactness from approximately 0 to 1.
setDirectedEdge(matrix, fromNode, toNode, n, weight)
Sets one directed edge.
Parameters:
matrix (array) : Flat directed adjacency matrix.
fromNode (int) : Source node.
toNode (int) : Destination node.
n (int) : Number of nodes.
weight (float) : Directed edge weight.
outStrength(matrix, n, node)
Sum of outgoing positive influence from a node.
Parameters:
matrix (array) : Directed matrix.
n (int) : Number of nodes.
node (int) : Source node.
Returns: Total outbound influence.
inStrength(matrix, n, node)
Sum of incoming positive influence to a node.
Parameters:
matrix (array) : Directed matrix.
n (int) : Number of nodes.
node (int) : Destination node.
Returns: Total inbound influence.
netInfluence(matrix, n, node)
Net directional leadership.
Positive means the node influences others more than it follows them.
Negative means the node behaves more like a follower.
Parameters:
matrix (array) : Directed matrix.
n (int) : Number of nodes.
node (int) : Node index.
Returns: Outbound minus inbound influence.
normalizedLeadership(matrix, n, node)
Normalized directional leadership score.
Parameters:
matrix (array) : Directed matrix.
n (int) : Number of nodes.
node (int) : Node index.
Returns: Score approximately from -1 to +1.
leadingNode(matrix, n)
Node with the largest net directional influence.
Parameters:
matrix (array) : Directed matrix.
n (int) : Number of nodes.
Returns: Node index.
followingNode(matrix, n)
Node with the greatest incoming influence.
Parameters:
matrix (array) : Directed matrix.
n (int) : Number of nodes.
Returns: Node index.
meanDirectedInfluence(matrix, n)
Average directed influence in the network.
Parameters:
matrix (array) : Directed matrix.
n (int) : Number of nodes.
Returns: Mean positive directed edge weight.
leadershipConcentration(matrix, n)
Concentration of outbound influence.
High values mean leadership is concentrated in fewer nodes.
Parameters:
matrix (array) : Directed matrix.
n (int) : Number of nodes.
Returns: Herfindahl-style concentration from 0 to 1.
connectedComponentsCount(matrix, n, threshold)
Counts connected components in an undirected threshold graph.
Parameters:
matrix (array) : Symmetric adjacency / similarity matrix.
n (int) : Number of nodes.
threshold (float) : Minimum absolute edge weight required to connect nodes.
Returns: Number of connected components.
localClusteringCoefficient(matrix, n, node, threshold)
Computes local clustering coefficient for one node.
Measures how interconnected the node's neighbors are.
Parameters:
matrix (array) : Symmetric similarity matrix.
n (int) : Number of nodes.
node (int) : Node index.
threshold (float) : Minimum absolute edge weight to define a connection.
Returns: Local clustering coefficient from 0 to 1.
averageClusteringCoefficient(matrix, n, threshold)
Computes mean clustering coefficient across all nodes.
Parameters:
matrix (array) : Symmetric similarity matrix.
n (int) : Number of nodes.
threshold (float) : Minimum absolute edge weight.
Returns: Average clustering coefficient from 0 to 1.
similarityDistance(similarity)
Converts similarity to graph distance.
Higher similarity becomes shorter distance.
Parameters:
similarity (float) : Edge similarity, typically from 0 to 1 in magnitude.
Returns: Distance from 0 to 1.
shortestPathDistance(matrix, n, source, target)
Dijkstra shortest-path distance between two nodes.
Uses distance = 1 - abs(similarity).
Parameters:
matrix (array) : Weighted matrix.
n (int) : Number of nodes.
source (int) : Start node.
target (int) : End node.
Returns: Shortest path distance.
averagePathLength(matrix, n)
Average shortest-path distance across all node pairs.
Parameters:
matrix (array) : Weighted matrix.
n (int) : Number of nodes.
Returns: Mean shortest path distance.
eigenvectorCentrality(matrix, n, node, iterations)
Approximate eigenvector centrality for one node using power iteration.
Parameters:
matrix (array) : Weighted matrix.
n (int) : Number of nodes.
node (int) : Node index.
iterations (int) : Number of power iterations.
Returns: Approximate normalized centrality from 0 to 1.
eigenvectorLeader(matrix, n, iterations)
Returns node with highest eigenvector centrality.
Parameters:
matrix (array) : Weighted matrix.
n (int) : Number of nodes.
iterations (int) : Number of power iterations.
Returns: Node index.
nodeStrengthPercentile(matrix, n, node)
Cross-sectional percentile rank for a node's strength.
Parameters:
matrix (array) : Weighted matrix.
n (int) : Number of nodes.
node (int) : Node index.
Returns: Percentile rank from 0 to 100.
nodeStrengthRank(matrix, n, node)
Returns the rank position of a node by strength.
Rank 1 means strongest.
Parameters:
matrix (array) : Weighted matrix.
n (int) : Number of nodes.
node (int) : Node index.
Returns: One-based rank.
networkCohesion(matrix, n, threshold)
Composite network cohesion score.
Combines connectivity, density and clustering coefficient.
Parameters:
matrix (array) : Symmetric similarity matrix.
n (int) : Number of nodes.
threshold (float) : Edge threshold.
Returns: Composite cohesion from 0 to 1. Library

ExpEngineThe 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 , 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 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:
= 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) Library

Library

EdgeStatsLibrary "EdgeStats"
A win rate on its own is not evidence. This library supplies the four things that turn one into a claim you can defend, none of which Pine ships: a base rate to subtract, a sample size corrected for overlapping forward windows, a confidence interval that behaves at small n, and a p-value that knows how many settings you tried before you picked this one.
The argument in three lines, all from the same 60 wins out of 100:
assess(60, 100, 0.5, horizon = 1) p = 0.046 significant
assess(60, 100, 0.5, horizon = 10) p = 0.527 not significant
assess(60, 100, 0.5, horizon = 10, trials = 30) p = 1.000 nothing at all
Nothing changed about the data. What changed is being honest that ten-bar forward returns sampled every bar are not a hundred independent observations, and that the best of thirty settings is not the same evidence as the only setting you tried.
WHAT THE DEMO SHOWS
Added to a chart directly, the library grades an ordinary signal: close above a 50 EMA, judged on whether price is higher ten bars later, over the last 500 bars. On BTCUSD 1h at the time of writing that is a hit rate of 39.9% against a base rate of 50.4%, an edge of -10.5 percentage points, and a two-sided p of 0.297.
Read that carefully, because it is the whole point. The signal looks bad. It is not reliably bad. Twenty-five independent observations cannot separate -10.5 points from noise, and the interval runs from 23.3% to 59.3%. A tool that says "I cannot tell" when it cannot tell is the only kind worth having.
THREE HONEST CAVEATS
n / horizon is a rough correction, not a theorem. It assumes overlap is the dominant source of dependence between observations. Where returns are autocorrelated beyond the window it is still optimistic. Treat it as a floor on your uncertainty rather than a ceiling.
zFor bisects normCdf, which is itself an approximation, so it inherits that error: zFor(0.95) lands about 1.2e-6 below the textbook 1.9599640. Irrelevant in practice, but it is an approximation of an approximation and you should hear that from me rather than discover it.
roll() uses ta.cum internally, so its call site must execute on every bar. Called inside "if barstate.islast" it has one bar of history and returns nonsense, and no max_bars_back setting repairs that. This is a property of Pine functions rather than of this library, and it is worth knowing generally.
VERIFICATION
Every fixed-input value is plotted to the Data Window and two are printed on the chart, so you can check the arithmetic rather than trust it. Against Python statistics.NormalDist:
normCdf(1.96) 0.9750022 true 0.9750021
normCdf(-1.0) 0.1586553
zFor(0.95) 1.9599628 true 1.9599640
zFor(0.99) 2.5758313 true 2.5758293
wilson(60, 100, 1.96)
selectionAdjusted(0.05, 30) 0.7853612
Corrections welcome, particularly to the effective sample size treatment, which is the part I would most like to be wrong about.
REFERENCE
normCdf(x)
Standard normal cumulative distribution. Abramowitz and Stegun 26.2.17, absolute error below 7.5e-8 across the whole real line.
Parameters:
x (float) : Value to evaluate.
Returns: Probability that a standard normal variate is at most x.
zFor(conf)
Two-sided z multiplier for a confidence level. Bisects normCdf, so any level works rather than a lookup of the usual three.
Parameters:
conf (float) : Confidence level in (0, 1). 0.95 returns 1.9599628.
Returns: The z for which the central interval of that width has the given coverage.
@remark Converged to float precision against normCdf, which is itself an approximation, so the result inherits its error: zFor(0.95) lands about 1.2e-6 below the textbook 1.9599640. Irrelevant for anything you would do with it, but it is an approximation of an approximation and worth saying so.
nEff(n, horizon)
Effective independent sample size when observations use overlapping forward windows.
Parameters:
n (float) : Raw observation count.
horizon (int) : Length in bars of the forward window each observation measures.
Returns: n divided by the horizon, with the horizon floored at 1.
wilson(hits, n, z)
Wilson score interval for a proportion. Unlike the normal approximation it stays inside and stays sane when n is small or the rate sits near an edge.
Parameters:
hits (float) : Successful observations.
n (float) : Total observations. Pass an effective count here, not a raw bar count, when the windows overlap.
z (float) : Multiplier from zFor().
Returns: A tuple on the proportion, or when there is no sample.
selectionAdjusted(p, trials)
Sidak correction. If you searched k settings and reported the best one, the p-value you found is not the p-value that best one deserves.
Parameters:
p (float) : Uncorrected two-sided p-value.
trials (int) : Settings, symbols or variants searched before this one was chosen. Pass 1 if you did not search.
Returns: Probability of seeing something at least this good in k independent tries.
roll(src, len)
Rolling window sum valid from the first bar, unlike math.sum which stays na until the window fills. Useful for counting events over a lookback.
Parameters:
src (float) : Series to accumulate.
len (simple int) : Window length in bars.
Returns: Sum of the last len values of src.
@remark Uses ta.cum internally, so the CALL SITE must execute on every bar. Called inside `if barstate.islast` it has one bar of history and returns nonsense. That is a property of Pine functions rather than of this library, and no max_bars_back setting repairs it. len is `simple` so Pine can size the history buffer at compile time.
assess(hits, n, base, horizon, conf, trials)
The whole assessment in one call.
Parameters:
hits (float) : Observations where the signal was right.
n (float) : Total observations.
base (float) : Rate at which the same outcome occurred unconditionally over the same horizon. This is the number that makes an edge an edge.
horizon (int) : Bars in the forward window. Overlapping windows shrink the effective sample.
conf (float) : Confidence level for the interval, default 0.95.
trials (int) : Settings searched before choosing this one, default 1.
Returns: A Verdict.
describe(v)
One line of plain English for a Verdict, sized to drop straight into a table cell.
Parameters:
v (Verdict) : The Verdict to describe.
Returns: A human-readable summary, or "no sample" when there is nothing to say.
Verdict
Everything needed to decide whether a measured hit rate means anything.
Fields:
rate (series float) : Observed hit rate, 0 to 1.
base (series float) : Base rate the signal is measured against, 0 to 1.
edge (series float) : rate minus base, in percentage points.
n (series float) : Raw observation count as supplied.
nEff (series float) : Observation count after the overlapping-window correction.
lo (series float) : Lower confidence bound on rate, computed on nEff.
hi (series float) : Upper confidence bound on rate, computed on nEff.
z (series float) : Test statistic of rate against base.
p (series float) : Two-sided p-value, already Sidak-adjusted for the trials argument.
clears (series bool) : True when the interval on the rate excludes the base rate. Library

Regression_ToolkitThis is toolkit/library bridges advanced regression approaches not natively supported in Pinescript, to Pinescript. Advanced regression frameworks that can be critical to ticker data, such as Ridge, Lasso, ElasticNET, and Logistic (normalized) regression, colinarity measuring and quantile regression. As well as approaches to linear based feature selection and importance assessments.
I hope you find it helpful!
Library "Regression_Toolkit"
multipleRegression(y, x1, x2, length)
Parameters:
y (float)
x1 (float)
x2 (float)
length (simple int)
ridgeRegression(y, x1, x2, x3, x4, nVars, length, lambda)
Parameters:
y (float)
x1 (float)
x2 (float)
x3 (float)
x4 (float)
nVars (simple int)
length (simple int)
lambda (simple float)
lassoRegression(y, x1, x2, x3, x4, nVars, length, lambda, iterations)
Parameters:
y (float)
x1 (float)
x2 (float)
x3 (float)
x4 (float)
nVars (simple int)
length (simple int)
lambda (simple float)
iterations (simple int)
logisticRegression(y, x1, x2, x3, x4, nVars, length, learningRate, iterations)
Parameters:
y (float)
x1 (float)
x2 (float)
x3 (float)
x4 (float)
nVars (simple int)
length (simple int)
learningRate (simple float)
iterations (simple int)
featureSelection(y, x1, x2, x3, x4, nVars, length)
Parameters:
y (float)
x1 (float)
x2 (float)
x3 (float)
x4 (float)
nVars (simple int)
length (simple int)
regressionStats(y, x1, x2, x3, x4, nVars, length, b0, b1, b2, b3, b4)
Parameters:
y (float)
x1 (float)
x2 (float)
x3 (float)
x4 (float)
nVars (simple int)
length (simple int)
b0 (float)
b1 (float)
b2 (float)
b3 (float)
b4 (float)
elasticNetRegression(y, x1, x2, x3, x4, nVars, length, lambda, alpha, iterations)
Parameters:
y (float)
x1 (float)
x2 (float)
x3 (float)
x4 (float)
nVars (simple int)
length (simple int)
lambda (simple float)
alpha (simple float)
iterations (simple int)
huberRegression(y, x1, x2, x3, x4, nVars, length, huberK, iterations)
Parameters:
y (float)
x1 (float)
x2 (float)
x3 (float)
x4 (float)
nVars (simple int)
length (simple int)
huberK (simple float)
iterations (simple int)
quantileRegression(y, x1, x2, x3, x4, nVars, length, tau, learningRate, iterations)
Parameters:
y (float)
x1 (float)
x2 (float)
x3 (float)
x4 (float)
nVars (simple int)
length (simple int)
tau (simple float)
learningRate (simple float)
iterations (simple int) Library

TVA_MathLibraryLibrary "TVA_MathLibrary"
f_htfBundle(tf)
Parameters:
tf (simple string)
f_htfBias(c, e20, e50, e200, r, atrv, adxv)
Parameters:
c (float)
e20 (float)
e50 (float)
e200 (float)
r (float)
atrv (float)
adxv (float)
f_stdTrendScore(c, e20, e50, e200, r, adxv)
Parameters:
c (float)
e20 (float)
e50 (float)
e200 (float)
r (float)
adxv (float)
f_confirmationScore(setupDir, biasW, biasD, bias4H, bias1H)
Parameters:
setupDir (int)
biasW (float)
biasD (float)
bias4H (float)
bias1H (float)
f_confluenceScore(alignmentPct, momentumConverge, adxv, volatilityCtx, volumeConfirm)
Parameters:
alignmentPct (float)
momentumConverge (float)
adxv (float)
volatilityCtx (float)
volumeConfirm (float)
f_buyProbability(baseBullPct, c, e20, e50, e200, r, macdHist, macdHistPrev, vol, volEma20, distToSupportATR, adxv, adxRising)
Parameters:
baseBullPct (float)
c (float)
e20 (float)
e50 (float)
e200 (float)
r (float)
macdHist (float)
macdHistPrev (float)
vol (float)
volEma20 (float)
distToSupportATR (float)
adxv (float)
adxRising (bool)
f_sellProbability(baseBearPct, c, e20, e50, e200, r, macdHist, macdHistPrev, vol, volEma20, distToResistATR, adxv, adxFallingDown)
Parameters:
baseBearPct (float)
c (float)
e20 (float)
e50 (float)
e200 (float)
r (float)
macdHist (float)
macdHistPrev (float)
vol (float)
volEma20 (float)
distToResistATR (float)
adxv (float)
adxFallingDown (bool)
f_marketRegime(adxv, efficiencyRatio, atr20, atr50, bbWidth, bbWidthEma20)
Parameters:
adxv (float)
efficiencyRatio (float)
atr20 (float)
atr50 (float)
bbWidth (float)
bbWidthEma20 (float)
f_chopIndex(adxv, diPlus, diMinus, efficiencyRatio, bbWidth, bbWidthEma20)
Parameters:
adxv (float)
diPlus (float)
diMinus (float)
efficiencyRatio (float)
bbWidth (float)
bbWidthEma20 (float)
f_volatilityClass(atrPercentile, histVolPercentile, trExpansionRatio, bbWidthPercentile)
Parameters:
atrPercentile (float)
histVolPercentile (float)
trExpansionRatio (float)
bbWidthPercentile (float)
f_emaSmooth2(src)
Parameters:
src (float)
f_scoreColor(score, highIsGood)
Parameters:
score (float)
highIsGood (bool)
f_gaugeText(score, label)
Parameters:
score (float)
label (string)
f_alignmentBar(pct)
Parameters:
pct (float) Library

Library

EWCoreLibEWCoreLib — Elliott Wave Pattern Evaluation Library
Overview
EWCoreLib is a Pine Script v6 library of exported functions that validate and score Elliott Wave price patterns. It has no chart output of its own and is not meant to be added to a chart; a calling script supplies the pivot data and decides what to do with the results.
It is one of three scripts published together: the EWCore indicator, this library, and EWCore Docs, an on-chart reference panel for EWCore.
If you are using EWCore, there is nothing to do here. EWCore pulls in this library itself, as part of its own code — you add EWCore to your chart and the library comes with it. This page exists for Pine programmers who want to call these functions from a script of their own, and as the open source behind EWCore's evaluation stage. The dependency runs one way only: EWCore needs this library, the library needs nothing from EWCore, and any script that can produce its own pivot series can use it.
Relation to the earlier publication
An earlier version of this library was published under the name EWCoreEvaluators. This publication continues the same codebase under a new name; it is not a variant or a competing implementation. The name changed because the earlier title stays bound to the earlier publication and cannot be reused. Any script that imports the old path should be pointed at Wick-Sniper/EWCoreLib, which is the one that receives further work.
Concepts
Pivots in, verdicts out. Every evaluator takes a pivot series — prices and bar positions in two parallel arrays — plus a starting index, and asks one question about the window beginning there: does this stretch of price form the pattern I check for? The answer comes back as a filled WaveCount object carrying the wave points, the hard-rule verdict, the component scores and an invalidation level, or as an empty one if the window does not qualify.
Configuration travels as an object, not as a parameter list. An exported function cannot read the global variables of the script importing it, and these evaluators need a great many settings — tolerances, score weights, Fibonacci levels, feature toggles. Passing them individually would mean unwieldy signatures that break on every added option. Instead the calling script builds one EngineConfig object per bar and hands it to whichever functions it calls.
Every exported function is pure. Parameters in, values out, no hidden state. That is what makes it safe to call the same evaluator from several contexts in one bar — a forward search, a historical chain walk, a sub-wave decomposition — without behaviour drifting between them.
Marginal violations are penalised, not rejected. A candidate that misses a rule by a hair is scored down rather than discarded, so a near-miss reading stays visible and comparable instead of vanishing silently.
Exported types
WaveCount — one complete candidate scenario: pattern type, direction, degree label, wave points and their bars, hard-rule and overlap flags, component scores, confidence tier, invalidation level, extension info.
EngineConfig — the settings bundle described above.
Exported functions, by purpose
Pattern evaluation — one per pattern in the canon this project supports: evaluateImpulseWindow, evaluateDiagonalWindow, evaluateZigzagFlatWindow, evaluateTriangleWindow, evaluateComboWXYWindow, evaluateComboWXYXZWindow, evaluateImpulseRecoveredWindow (rebuilds an impulse whose fourth wave the coarse pivot pass swallowed), evaluateLegWhole (judges a whole leg as a single structure rather than a chain of fragments).
Type arbitration — f_discriminateWindow, f_evalWindowTrio: when impulse, diagonal and triangle all pass on the same window, these decide which type the window really is, so the least strict pattern cannot win by default.
Re-checking and scoring — revalidateCandidate, f_comboBonus, f_dpEdgeModifier, f_legDominance, f_legWholeDominance, f_evalCandDegStep, f_evalFloorLeg, f_candDegPasses, f_floorLegPasses.
Chain and gap handling — f_realGapFill (dynamic-programming best-path search for a continuous chain of valid patterns across a pivot series), f_gapNoiseMetrics, f_depth2InnerLegs.
Degree handling — getDegreeLabel, shiftDegreeUp, shiftDegreeDown, f_degreeNameFor, f_notatedLabelTextArray: translate between Elliott degrees and the notation that belongs to each.
Explanatory text — f_patternExplanation, f_extGlyph, f_extTooltip, f_atomicDiagText, f_gapBridgeText, f_rankStage, f_rankWhy, impulseFunnelText, comboFunnelText: turn a verdict into readable reasoning for labels and tooltips.
Drawing helpers — drawImpulseFunnel, drawComboFunnel, f_drawWholeLegFallback, f_drawWholeLegFallbackFromPivots, f_drawWholeLegFallbackFromArrays, f_drawExplanationLabel, f_drawDateMarker, f_drawRoutedLeg.
Utilities — subLowerBound, f_barValToTime (resolves a stored bar value to a timestamp; historical drawings need xloc.bar_time), getLabelSize, f_lineStyle, f_frameCount.
Calling it from your own script
This section is for Pine programmers writing their own script. EWCore users can skip it — EWCore handles the import itself.
import Wick-Sniper/EWCoreLib/1 as ewcore
Type the alias by hand. The "copy to clipboard" button on the library page appends the library name a second time and produces an import line that does not resolve.
From there, build an EngineConfig once per bar, then call the evaluator for the pattern and window you are testing and read the returned WaveCount.
Notes
This library validates structure; it does not detect pivots. The quality of everything it returns depends on the pivot series you feed it — a threshold that admits noise produces confident verdicts about noise. In this project's own testing, wave counting becomes reliable at 15-minute charts and above.
A passing verdict means a reading exists that satisfies the rules checked, not that it is the correct count. Elliott Wave analysis admits more than one valid interpretation of the same chart, and these functions score candidates rather than settle them.
This library is a technical analysis building block. It is not financial advice and generates no trading signals. Library

Public_Library_ChessTalkThis is where the Chess script keeps its trash talk. The library stores every line the computer can say, and decides when it speaks and which line it picks. It imports nothing, knows nothing about chess, and never calls `math.random()`.
There are eight kinds of occasion: the computer won material, it lost material, it promoted, it's still in its opening book, the game just started, it wins, it loses, or the game ends in a draw. Each occasion fires at its own rate, so the computer needles you now and then rather than commenting on every move. Every decision comes from arithmetic on a seed boiled down from the moves played so far, which means the same game always says the same lines, however many times you reload, and a chosen line can't flicker between ticks.
We pass the {piece} token in so that a line can name its victim ("Mmm... free {piece}." becomes "Mmm... free knight."), and per-line filters keep the puns honest - the "good knight" gag only fires when a knight actually dies. A no-repeat rule stops a category saying the same line twice in a row. The banks run to 97 lines of misquoted pop culture.
The `buildTalkState()` function packs the banks, the rates, the no-repeat memory and the wounded detection into one object, so the consumer holds one variable instead of fifteen. The consumer spots the occasion while it replays the game, asks this library for a line, and stores what it gets. The live bar only reads.
See the Chess script for the backchat in action:
Library

Public_Library_ChessBookThis library holds the opening book for the Chess script. It lives apart from the brain (ChessAI) so the openings can grow without republishing the logic. It imports nothing and contains no logic of its own beyond building the map.
Each entry pairs the moves played so far (both sides' moves, lowercase, one space between) with our reply, written the same way - so the key "e2e4 e7e5" answers with "g1f3". Some entries offer several replies separated by "|", and the consumer picks between them with its game seed, so back-to-back games can open differently. The empty-string key holds White's very first move.
We use a map because the book question is exactly a lookup: given the moves played so far, what do we reply? Simply query the canonical move string as the map key and if we get a reply that's our move in response.
Pine maps can't hold arrays as values, which is why several replies pack into one "|"-separated string. Of course we could define a UDT that contains an array, but it's overhead. The trade-off of keying by move sequence rather than by position is that an unusual move order into a known position misses the book - the cost is an early exit to the ladder, never a wrong move, and in exchange every entry in the source reads as a real game you can play through.
The book holds enough entries for every common defence and sideline on both sides, deeper main lines, and wider choices in some of them.
This split of data from calculation is worth using for non-chess scripts: when a big lookup table and the logic that reads it live in separate libraries, the table can grow on its own release schedule.
See the Chess script to play the book in a real game: Library

Public_Library_ChessAIThis library is the brain of the Chess script: a small chess opponent built on the ChessCore rules engine.
It is not a search engine. It is a ladder of ten rules that looks at one position, tries the rules in order, and plays the first that fires:
1. Mate in one
2. Opening book
3. Avoid checkmate
4. Defence
5. Win material
6. Safe check
7. Exchange when ahead
8. Endgame
9. Develop
10. Any other safe move
🟩 THE FORESEE
Before the ladder runs, a foresee stage takes every legal move and adds to its object its most likely material outcome. This score comes from the opponent's best replies to our move, and a few captures after that. We look at the opponent's best captures, his checks, his most menacing quiet threats, and what happens if we don't take. We also look briefly whether two checks in a row force mate. Deep thinking follows more replies and reads six half-moves.
The score works mostly as a veto. From the list of possible moves for a ladder stage, we reject the ones that end in us being checkmated. When every legal move walks into one, we toss a coin to decide between playing the least-bad move and resigning.
🟩 KEEPING A WON GAME WON
If we are winning we want to win, not draw. While ahead on material, the quiet rules refuse any move that recreates a position the game has already seen, so it can't shuffle a rook between two good squares forever. And the foresee prices a stalemate at minus the lead it would throw away, so the winning side sidesteps the trap while the losing side, correctly, steers toward it. The endgame rule gives the ladder actual technique (push passers, rook to the seventh, king up to escort) to try to win.
🟩 DETERMINISM
No `math.random()` call decides anything on its own. The first five rules always give the same answer for a position. The last five pick from their pools with a seed the consumer supplies, derived from the game record. Variety between games comes from the consumer script mixing a clock reading into the seed, not from the library.
🟩 PROCESS FLOW
The exported functions are called in a certian order for each position:
ChessCore generates the legal moves.
`annotateMoves()` counts the attackers and defenders on every move's landing square, and notes the cheapest attacker. Every safety test the rules make reads these three numbers.
`filterPromotions()` and `classifyMoves()` trim pointless pawn promotions and flag moves that make the king's cover worse or that just undo the previous move.
The foresee adds a score to each candidate move: `foreseePrepare()` starts the story, `foreseeFinishCaptures()` follows the capture lines, and `foreseeFinishRest()` follows the checks and quiet threats. The consuming script spreads these calls across chart bars so no single bar works too hard.
The ladder rules run in order, `ruleMateInOne()` down to `ruleFallback()`, and the first one that returns a move wins.
See the Chess script to play against this AI: Library

Public_Library_ChessCoreThe rules of chess as a reusable Pine engine. It doesn't display anything or think of any moves. It just defines what is legal. This is the foundation library of the Chess script, and it's built so that any Pine project needing real chess - a different engine, a puzzle board, a game replayer - can build on it without rewriting the rules.
🟩 WHAT IT DOES
Keeps the whole position in one object: the board, whose turn it is, castling rights, the en-passant target, the move clocks, and a cached king square for each side.
Generates every fully legal move for the side to move, including castling, en passant, and promotions. It checks first based on how the pieces can move, and then creates a copy board to test which moves keeps the king out of check - so pins, discovered checks and the en-passant edge cases all just work.
Applies a move to a position and does all the admin.
Detects checkmate, stalemate, and the automatic draws. For threefold repetition it provides position keys and a counting helper. The consumer keeps the key history, because that's the one draw that needs to remember earlier positions, and these functions deliberately hold no history of their own.
Parses a typed move record like "e2e4 e7e5" - junk-tolerant and case-insensitive, so "e2-e4, E7e5" parses the same - and rebuilds it as one tidy canonical string. The Chess consumer uses that canonical record as its opening-book key and its random seed.
🟩 DESIGN NOTES
The big thing here is the scan that answers "who attacks this square?", with variants that count the attackers and price the cheapest one.
Another important part is the test of whether a move is legal on a COPY board. One definition of "attacked" is shared by the move generator, the game-status detection and castling's transit-square tests, so they can never disagree about what check means.
Each generated move is an object whose scoring fields are declared here but filled in from outside - `foreseeScore` for an AI's look-ahead, plus two endgame scores. ChessCore itself never touches them. This declare-then-fill pattern is how a foundational library can carry data that a higher library computes, without circular imports and without parallel arrays.
Internally everything thinks in (row, column), where row 0 is rank 8 (Black's back rank) and column 0 is file "a". Square names like "e4" appear only at the edges.
🟩 WHAT TRUSTS WHAT
The exports look independent, but they lean on each other in ways worth knowing before you build on them:
`applyMove()` trusts its move and changes the position in place. It doesn't re-check legality, so feed it moves from `generateLegalMoves()` - or from `matchLegalMove()`, which picks the move matching typed coordinates out of that list and brings the filled-in castling and en-passant details with it. A move object you build by hand would miss those.
To ask "what if?" without committing, `copyPosition()` first and apply the move to the copy. The generator's own self-check filter runs on such scratch boards, and so does the whole ChessAI look-ahead.
`gameStatusOf()` spots mate, stalemate and the automatic draws, but not threefold repetition, which needs history this library deliberately doesn't keep. Push each new `positionKey()` onto your own array, then ask `isThreefoldRepetition()`. Push first, then ask.
The position caches each king's square so check tests don't scan the board, and `applyMove()` maintains that cache. If you build a custom position by writing to the board matrix yourself, set the king fields to match, or every check test will look at the wrong square.
See the Chess script for the whole thing playing human vs computer: Library

PessimisticSimLibrary "PessimisticSim"
Broker-agnostic pessimistic fill simulator. Runs a shadow account
alongside any indicator or strategy, filling every signal at the
worst plausible price (half-spread + slippage + a fraction of the
adverse bar excursion) so you can see whether an edge survives
real-world friction.
Instrument-agnostic: set pointValue for futures, pick a commission
model, pick a sizing model, pick a fill model. Defaults reproduce
spot crypto / stock behaviour (multiplier 1, percent commission,
risk-based sizing, next-bar-open fills).
newState(cfg)
Creates a fresh shadow account seeded from `cfg`.
Parameters:
cfg (SimConfig) : Configuration object.
Returns: A SimState ready to pass to step().
commissionFor(cfg, price, qty)
Commission for one fill under the configured model.
Parameters:
cfg (SimConfig) : Configuration object.
price (float) : Fill price.
qty (float) : Units filled.
Returns: Commission in account currency. Zero when qty <= 0.
qtyFor(cfg, equity, stopDist, price, openRisk)
Position size under the configured sizing model, with an optional
leverage cap and quantity-step rounding. Host scripts should call
this for their live orders too, so both engines size identically.
Parameters:
cfg (SimConfig) : Configuration object.
equity (float) : Account equity to size against.
stopDist (float) : Distance from entry to stop, in PRICE units. Only used by
SizeMode.riskStop; pass 0 in the other modes.
price (float) : Fill price, used by the leverage cap and equityPct sizing.
openRisk (float) : Risk already committed by open positions, in CURRENCY
(i.e. qty * stopDist * pointValue). riskStop only.
Returns: Units to trade, rounded down to qtyStep. Zero when unsizable.
buyFillPrice(cfg, refPrice, advHigh)
Worst-case buy fill: reference price + half-spread + slippage +
a slice of the adverse upward excursion.
Parameters:
cfg (SimConfig) : Configuration object.
refPrice (float) : Reference price (bar open, or close under signalClose).
advHigh (float) : Adverse extreme to price against. Pass the bar high under
nextOpen; pass refPrice under signalClose to disable it.
Returns: The pessimistic buy price.
sellFillPrice(cfg, refPrice, advLow)
Worst-case sell fill: reference price - half-spread - slippage -
a slice of the adverse downward excursion.
Parameters:
cfg (SimConfig) : Configuration object.
refPrice (float) : Reference price (bar open, or close under signalClose).
advLow (float) : Adverse extreme to price against. Pass the bar low under
nextOpen; pass refPrice under signalClose to disable it.
Returns: The pessimistic sell price.
method closeAt(s, cfg, exitPrice)
Flattens the position at `exitPrice`, books PnL and updates stats.
No-op when flat.
Namespace types: SimState
Parameters:
s (SimState) : Shadow account state.
cfg (SimConfig) : Configuration object.
exitPrice (float) : Fill price for the exit.
Returns: Void.
method openAt(s, cfg, dir, fillPrice, stopDist)
Opens a position, or adds a unit while `units < cfg.maxUnits`.
Add-ons blend into a volume-weighted average entry and are sized
against the risk already committed.
Namespace types: SimState
Parameters:
s (SimState) : Shadow account state.
cfg (SimConfig) : Configuration object.
dir (int) : 1 to go long, -1 to go short.
fillPrice (float) : Pessimistic fill price.
stopDist (float) : Distance from entry to stop, in price units. Pass 0 under
fixedUnits / equityPct sizing.
Returns: Void.
method mark(s, cfg, price)
Marks the account to market and updates equity peak and drawdown.
Namespace types: SimState
Parameters:
s (SimState) : Shadow account state.
cfg (SimConfig) : Configuration object.
price (float) : Current mark price, normally close.
Returns: Void.
method stepAt(s, cfg, longIn, longOut, shortIn, shortOut, buyPrice, sellPrice, markPrice, stopDistLong, stopDistShort)
General escape hatch: processes one bar against explicit fill prices.
Use when your execution model is neither FillMode case — limit fills,
stop fills, VWAP, session opens, anything.
Order is exits, then reversals, then entries, then mark-to-market.
Namespace types: SimState
Parameters:
s (SimState) : Shadow account state.
cfg (SimConfig) : Configuration object.
longIn (bool) : Long entry signal.
longOut (bool) : Long exit signal.
shortIn (bool) : Short entry signal.
shortOut (bool) : Short exit signal.
buyPrice (float) : Price paid when buying.
sellPrice (float) : Price received when selling.
markPrice (float) : Price for the mark-to-market update.
stopDistLong (float) : Stop distance for longs, price units.
stopDistShort (float) : Stop distance for shorts, price units.
Returns: Void.
method step(s, cfg, longIn, longOut, shortIn, shortOut, o, h, l, c, stopDistLong, stopDistShort)
Processes one bar using the configured FillMode. Call once per
confirmed bar.
FillMode.nextOpen — pass PRIOR-bar signals (sig ); this bar's
open is the fill, its high/low the excursion.
FillMode.signalClose — pass CURRENT-bar signals; the close is the
fill and advFrac is inert.
Getting this pairing wrong produces plausible-but-wrong results, so
check it first when a comparison looks strange.
Namespace types: SimState
Parameters:
s (SimState) : Shadow account state.
cfg (SimConfig) : Configuration object.
longIn (bool) : Long entry signal, shifted per FillMode.
longOut (bool) : Long exit signal, shifted per FillMode.
shortIn (bool) : Short entry signal, shifted per FillMode.
shortOut (bool) : Short exit signal, shifted per FillMode.
o (float) : Bar open.
h (float) : Bar high.
l (float) : Bar low.
c (float) : Bar close.
stopDistLong (float) : Stop distance for longs, price units.
stopDistShort (float) : Stop distance for shorts, price units.
Returns: Void.
pf(gp, gl)
Profit factor with safe handling of an empty loss column.
Parameters:
gp (float) : Gross profit.
gl (float) : Gross loss, as a positive number.
Returns: gp/gl, 999 when there are no losses, 0 when there is nothing.
method netProfit(s, cfg)
Net profit of the shadow account, in currency.
Namespace types: SimState
Parameters:
s (SimState) : Shadow account state.
cfg (SimConfig) : Configuration object.
Returns: equity - initCap.
method profitFactor(s)
Profit factor of the shadow account.
Namespace types: SimState
Parameters:
s (SimState) : Shadow account state.
Returns: Profit factor.
method winRatePct(s)
Win rate of the shadow account, percent.
Namespace types: SimState
Parameters:
s (SimState) : Shadow account state.
Returns: Percentage of closed trades that were profitable.
method avgTradePct(s)
Average per-trade return, percent of equity-before-trade.
Namespace types: SimState
Parameters:
s (SimState) : Shadow account state.
Returns: Mean trade return, percent.
method rtCostPct(cfg)
Round-trip friction as a percent of notional. Only meaningful under
CommMode.pct — flat commissions do not scale with notional, so this
returns na under the other models. Compare against avgTradePct():
if the average trade does not clear this, the edge is smaller than
the cost of trading it.
Namespace types: SimConfig
Parameters:
cfg (SimConfig) : Configuration object.
Returns: Round-trip cost, percent, or na under flat commission models.
rowLabels()
Row labels matching the order of rowValues(). Lay out an audit table
in the host script from these, so strategy.* calls stay in the host.
Returns: Array of ten label strings.
method rowValues(s, cfg)
Preformatted metric strings for the shadow account, aligned to rowLabels().
Namespace types: SimState
Parameters:
s (SimState) : Shadow account state.
cfg (SimConfig) : Configuration object.
Returns: Array of ten value strings.
method verdict(s, cfg, strategyPF, strategyNet)
Compares a host strategy's headline numbers against the shadow
account and returns a verdict string.
Namespace types: SimState
Parameters:
s (SimState) : Shadow account state.
cfg (SimConfig) : Configuration object.
strategyPF (float) : Host strategy profit factor.
strategyNet (float) : Host strategy net profit.
Returns: "DIVERGED — investigate", "SURVIVES", or "NO EDGE".
SimConfig
Instrument, cost, sizing and execution assumptions for the shadow account.
Fields:
spreadPct (series float) : Assumed FULL spread, percent. Half is charged per side.
slipPct (series float) : Extra slippage percent per side.
advFrac (series float) : Fraction of the fill bar's adverse excursion added to the fill.
commMode (series CommMode) : Commission model.
commPct (series float) : Commission percent of notional, per side.
commPerUnit (series float) : Flat commission per contract or share, per side.
commMin (series float) : Minimum commission per fill. Applied only when qty > 0.
pointValue (series float) : Currency value of one full point of price movement, per unit.
qtyStep (series float) : Rounds size DOWN to this increment. 0 = no rounding.
initCap (series float) : Starting equity of the shadow account.
sizeMode (series SizeMode) : Position sizing model.
riskPct (series float) : Risk per trade, percent of equity. SizeMode.riskStop only.
maxTotalPct (series float) : Ceiling on total open risk, percent of equity. riskStop only.
fixedUnits (series float) : Units per entry. SizeMode.fixedUnits only.
equityPct (series float) : Notional as percent of equity. SizeMode.equityPct only.
useLevCap (series bool) : Apply the leverage cap on top of the chosen sizing model.
maxLeverage (series float) : Max notional / equity.
maxUnits (series int) : Max entries per position. MUST equal the host strategy's
fillMode (series FillMode) : Execution assumption. Determines which signals step() wants.
SimState
Mutable state of the shadow account. Create with newState().
Fields:
equity (series float) : Realised equity, commissions already deducted.
dir (series int) : 1 long, -1 short, 0 flat.
entry (series float) : Volume-weighted average entry price.
qty (series float) : Total units held.
units (series int) : Number of fills making up the current position.
trades (series int) : Closed trades.
wins (series int) : Closed trades with net > 0.
grossP (series float) : Sum of winning net PnL.
grossL (series float) : Sum of absolute losing net PnL.
commPaid (series float) : Total commission paid, both sides.
peak (series float) : Mark-to-market equity high water mark.
maxDD (series float) : Worst mark-to-market drawdown, as a negative fraction.
consecL (series int) : Current consecutive-loss run.
maxConsL (series int) : Longest consecutive-loss run.
sumTrPct (series float) : Sum of per-trade returns, percent of equity-before-trade.
skipped (series int) : Entry signals dropped because sizing returned zero units. Library

KC Institutional Core LibraryKC Institutional Core Library v1.0
KCInstitutionalCore is a reusable Pine Script v6 utility library created to support structured technical-analysis workflows without duplicating common helper logic across multiple indicators and strategies.
The library provides transparent and independently reusable functions for:
Score normalization and trade-quality grading
Premium, Discount and Equilibrium classification
Risk-to-reward calculation
Risk-based position-size estimation
Timeframe-aware trading-style classification
Adaptive higher-timeframe selection
Directional alignment analysis
Execution-blocker identification
The exported functions are deterministic utilities. They do not generate guaranteed trading signals, predict future price movement or execute trades.
Basic import example
import Kelly_Carter12/KCInstitutionalCore/1 as kc
string grade = kc.scoreToGrade(78)
string style = kc.tradeStyle(timeframe.in_seconds())
= kc.rangeLocation(close, ta.highest(high, 50), ta.lowest(low, 50))
The detailed function documentation below explains every exported function, parameter and return value.
Library "KCInstitutionalCore"
Reusable Pine Script v6 utilities for timeframe context, score grading, premium/discount classification, alignment, risk-to-reward and position-size calculations. Designed as a transparent helper library for indicators and strategies.
clamp(value, minimum, maximum)
Restricts a numeric value to the supplied minimum and maximum boundaries.
Parameters:
value (float) : Value to restrict.
minimum (float) : Lower boundary.
maximum (float) : Upper boundary.
Returns: The restricted value.
scoreToGrade(score)
Converts a numeric score into a concise quality grade.
Parameters:
score (float) : Score expressed on a 0–100 scale.
Returns: A grade string from AA to D.
normalizeScore(rawScore, maximumScore)
Normalizes a raw score to a 0–100 scale.
Parameters:
rawScore (float) : Current raw score.
maximumScore (float) : Maximum possible raw score.
Returns: Normalized score from 0 to 100, or na when maximumScore is not positive.
rangeLocation(price, rangeHigh, rangeLow)
Classifies the current price inside a supplied dealing range.
Parameters:
price (float) : Current or evaluated price.
rangeHigh (float) : Upper boundary of the range.
rangeLow (float) : Lower boundary of the range.
Returns: A tuple containing PREMIUM, DISCOUNT, or EQUILIBRIUM and the 0–100 range percentage.
riskReward(entry, stop, target)
Calculates reward-to-risk from entry, stop and target prices.
Parameters:
entry (float) : Entry price.
stop (float) : Stop-loss price.
target (float) : Target price.
Returns: Absolute reward-to-risk ratio, or na when the stop distance is zero.
positionSize(accountSize, riskPercent, entry, stop, pointValue)
Estimates position size from account risk and stop distance.
Parameters:
accountSize (float) : Account balance or planning capital.
riskPercent (float) : Percentage of account risked.
entry (float) : Entry price.
stop (float) : Stop-loss price.
pointValue (float) : Monetary value per price point for one unit.
Returns: Estimated units or lots according to the supplied pointValue, or na for invalid inputs.
tradeStyle(chartSeconds)
Maps chart duration in seconds to a general planning style.
Parameters:
chartSeconds (float) : Chart timeframe duration in seconds, normally supplied with timeframe.in_seconds().
Returns: SCALP, INTRADAY, SWING, or POSITION.
adaptiveTimeframes(chartSeconds)
Suggests two broader context timeframes from the chart duration.
Parameters:
chartSeconds (float) : Chart timeframe duration in seconds, normally supplied with timeframe.in_seconds().
Returns: A tuple containing primary and secondary context timeframe strings.
alignmentState(localBias, htfBias, mtfBias)
Summarizes local, higher-timeframe and multi-timeframe directional agreement.
Parameters:
localBias (int) : Local direction: 1 bullish, -1 bearish, 0 neutral.
htfBias (int) : Higher-timeframe direction: 1 bullish, -1 bearish, 0 neutral.
mtfBias (int) : Broader alignment direction: 1 bullish, -1 bearish, 0 neutral.
Returns: BULL ALIGNED, BEAR ALIGNED, PARTIAL, CONFLICT, or NEUTRAL.
executionBlocker(direction, htfBias, mtfBias, location, structureConfirmed, liquidityConfirmed, newsBlocked)
Returns the first material execution blocker in a transparent priority order.
Parameters:
direction (int) : Intended direction: 1 long, -1 short, 0 neutral.
htfBias (int) : Higher-timeframe direction: 1 bullish, -1 bearish, 0 neutral.
mtfBias (int) : Multi-timeframe direction: 1 bullish, -1 bearish, 0 neutral.
location (string) : PREMIUM, DISCOUNT, or EQUILIBRIUM.
structureConfirmed (bool) : True when the required structure event is confirmed.
liquidityConfirmed (bool) : True when the required liquidity event is confirmed.
newsBlocked (bool) : True when a manual news blackout is active.
Returns: A concise blocker description, or CLEAR when no listed blocker is active. Library

Trade Wzrd - Library Alert String UtilsTrade Wzrd - Library Alert String Utils
WHAT IT IS
Open-source Pine library that builds comma-separated webhook alert strings for automated order commands. Import name: TradeWzrdAlerts.
This is an educational protocol helper for strategy and indicator authors. It is not a signal service and does not place broker orders by itself.
WHY IT EXISTS (ORIGINALITY)
Most automation scripts hand-concatenate alert text. That causes dialect drift, missing parameters, and broken multi-command messages. This library is a single export surface for the full command set used with webhook-style automation:
Market: BUY, SELL
Pending: BUYLIMIT, SELLLIMIT, BUYSTOP, SELLSTOP
Futures-style: BRACKET, REMOVE_SL, REMOVE_TP
Manage: MODIFY, BREAKEVEN
Close: CLOSE, CLOSEALL, LAYER_CLOSE
Cancel: CANCEL
It also provides zero-config PRICE helpers so you can pass exact strategy stop and take-profit prices (no manual pip math), plus a multi() joiner for semicolon-separated command chains.
HOW IT WORKS
1) Each command function returns one string: COMMAND,SYMBOL
2) Optional parameters are omitted when unset (na or empty string). Legitimate zero values such as OFFSET=0 are still emitted when you pass them.
3) COMMENT text is sanitized so commas and semicolons cannot break multi-command grammar.
4) Invalid required fields (empty symbol, pending without PRICE, MODIFY with neither SL nor TP) return an empty string. Callers should not fire alerts on empty strings.
5) Zero-config helpers (buyPrice, sellPrice, bracketPrice, modifySlPrice, breakEvenPrice, closePercent) force TPSLTYPE=PRICE and format prices with mintick precision.
HOW TO USE
1) Publish or open this library, then import it in your script (replace username and version as shown on the library page):
import USERNAME/TradeWzrdAlerts/1 as TW
2) Build a message, for example:
msg = TW.buy("EURUSD", vol=0.01, sl=100, tp=200, tpslType="PIPS")
3) Pass msg into strategy.entry / strategy.exit alert_message, or call alert(msg) when length(msg) > 0
4) Create a TradingView alert with message:
{{strategy.order.alert_message}}
or use Any alert() function call when using alert()
5) Point the alert webhook field at whatever endpoint you already use
DEFAULTS AND RULES
- Symbol is pass-through (not force-uppercased)
- Ticket is an optional string parameter
- TPSLTYPE is only written when you provide it (except zero-config PRICE helpers)
- multi(a,b,...) joins non-empty segments with semicolons
LIMITATIONS
- Library only builds text. Execution quality depends on your webhook receiver and broker
- Pending-order automation support depends on your backend and platform
- Past results and example strings do not predict live performance
- Not intended as financial advice
No external links are required to understand or use this library.
Library

TargetExcursionLibLibrary "TargetExcursionLib"
Parent supplies origin price/scale, direction, and path high/low/close series.
Library derives no hidden source data.
Returns bands.ready, bands.status, bands.effectiveSupport, and bands.resolvedCount.
Before minimum support: status is exactly "band stats not ready yet" and all band levels are na.
f_input(direction, originPrice, originScale, predictionValid, directionProbability, externalReliability)
Construct a generic target input from series values.
Parameters:
direction (int)
originPrice (float)
originScale (float)
predictionValid (bool)
directionProbability (float)
externalReliability (float)
f_model_new(gridSize, outcomeCap, smoothing, halfLife, family, shrinkageAlpha, minSupport, supportScale, minTransparency, maxTransparency, transparencyGamma)
Construct an independent stateful model instance.
Parameters:
gridSize (int)
outcomeCap (float)
smoothing (float)
halfLife (float)
family (series DensityFamily)
shrinkageAlpha (float)
minSupport (float)
supportScale (float)
minTransparency (int)
maxTransparency (int)
transparencyGamma (float)
f_update(model, signal, pathHigh, pathLow, pathClose, horizon, currentBar, confirmed)
Parameters:
model (TargetModel)
signal (TargetInput)
pathHigh (float)
pathLow (float)
pathClose (float)
horizon (int)
currentBar (int)
confirmed (bool)
TargetInput
Fields:
direction (series int)
originPrice (series float)
originScale (series float)
predictionValid (series bool)
directionProbability (series float)
externalReliability (series float)
TargetBands
Fields:
ready (series bool)
status (series string)
direction (series int)
originPrice (series float)
originScale (series float)
mfeQ10 (series float)
mfeQ50 (series float)
mfeQ80 (series float)
mfeQ90 (series float)
mfeMode (series float)
maeQ10 (series float)
maeQ50 (series float)
maeQ80 (series float)
maeQ90 (series float)
maeMode (series float)
mfePriceQ10 (series float)
mfePriceQ50 (series float)
mfePriceQ80 (series float)
mfePriceQ90 (series float)
mfePriceMode (series float)
maePriceQ10 (series float)
maePriceQ50 (series float)
maePriceQ80 (series float)
maePriceQ90 (series float)
maePriceMode (series float)
effectiveSupport (series float)
intervalCoverageEstimate (series float)
reliability (series float)
transparency (series int)
pendingCount (series int)
resolvedCount (series int)
lastResolvedBar (series int)
PendingTarget
Fields:
originBar (series int)
resolutionBar (series int)
direction (series int)
originPrice (series float)
originScale (series float)
maxHigh (series float)
minLow (series float)
TargetModel
Fields:
pending (array)
pooledMae (array)
pooledMfe (array)
longMae (array)
longMfe (array)
shortMae (array)
shortMfe (array)
pooledMaeWeight (series float)
pooledMaeWeightSq (series float)
pooledMfeWeight (series float)
pooledMfeWeightSq (series float)
longMaeWeight (series float)
longMaeWeightSq (series float)
longMfeWeight (series float)
longMfeWeightSq (series float)
shortMaeWeight (series float)
shortMaeWeightSq (series float)
shortMfeWeight (series float)
shortMfeWeightSq (series float)
gridSize (series int)
outcomeCap (series float)
smoothing (series float)
halfLife (series float)
family (series DensityFamily)
shrinkageAlpha (series float)
minSupport (series float)
supportScale (series float)
minTransparency (series int)
maxTransparency (series int)
transparencyGamma (series float)
lastDecayBar (series int)
lastResolvedBar (series int)
resolvedCount (series int) Library

Library

Library

FractalMemoryLib [Jayadev Rana]FractalMemoryLib packages the pattern-memory engine used by the Fractal Memory Projection indicator and the Fractal Memory Strategy so any script can import it.
WHAT IT DOES
The library finds the historical window whose movement shape most resembles the most recent bars (mean squared distance between stdev-normalized log returns), replays what followed that window as a projected close path, and sizes stops and targets adaptively by volatility regime.
EXPORTED FUNCTIONS
logRet(src) - one-bar log return of a series.
bestMatch(src, winLen, scanDepth, gapAhead) - scans up to scanDepth bars back and returns the offset of the most similar window plus a 0-100 similarity score. gapAhead reserves bars after the match for a projection.
analogPath(src, offset, fcLen, scaleF) - array of fcLen projected closes built by replaying the returns that followed the match, rescaled by scaleF (for example current ATR over ATR at the match).
adaptiveR(atrLen, rankLen, base) - volatility-adaptive unit risk: ATR times (base plus its 0-1 percentile rank), plus the rank itself. Call on every bar.
volRegime(volRank) - "Low", "Normal" or "High" label from the rank.
targets(entry, dirSign, unitR, slMult) - stop loss and TP1/TP2/TP3 at 1R, 2R and 3R.
USAGE NOTES
Call adaptiveR on every bar for ta consistency. bestMatch and analogPath are loop-heavy; for display purposes call them on the last bar only, and make sure the chart has at least scanDepth plus gapAhead bars of history. When the library itself is added to a chart it draws a small demo projection line from the best analog.
The analog projection is a statistical reference to a similar past episode, not a prediction, and not financial advice. Library

Library
