BasketLib

f_calc_correlation_score(base_return, candidate_return, corr_len, smooth_len, is_self)
Parameters:
base_return (float): Base asset 1-bar return
candidate_return (float): Candidate asset 1-bar return
corr_len (int): Correlation calculation length
smooth_len (simple int): EMA smoothing length
is_self (bool): Whether candidate is the base asset itself
Returns: Correlation score (0.7 * raw + 0.3 * ema), or na if invalid
f_rank_and_select(scores, n)
Parameters:
scores (array<float>): Array of correlation scores
n (int): Number of assets to select (typically 4)
Returns: Array of selected indices [idx1, idx2, idx3, idx4]
f_is_self_reference(candidate_symbol, base_ticker)
Parameters:
candidate_symbol (string): Full symbol string (e.g., "BINANCE:BTCUSDT")
base_ticker (string): Base asset ticker (e.g., "BTC")
Returns: True if candidate is the base asset
f_route_scan_idx(idx, prices)
Parameters:
idx (int): Index (0-9)
prices (array<float>): Array of 10 scan candidate prices
Returns: Price at index, or na if invalid
f_get_preset_basket(preset_name)
Parameters:
preset_name (string): Name of preset ("Basket B (Memes)", etc.)
Returns: [sym1, sym2, sym3, sym4, sym5, use_sym5]
f_get_default_scan_symbols()
f_calc_basket_fit(score1, score2, score3, score4)
Parameters:
score1 (float): Correlation score of asset 1
score2 (float): Correlation score of asset 2
score3 (float): Correlation score of asset 3
score4 (float): Correlation score of asset 4
Returns: Basket fit percentage (0-100)
f_get_fit_label(fit_pct)
Parameters:
fit_pct (float): Basket fit percentage (0-100)
Returns: Quality label ("Excellent", "Good", "Fair", "Poor")
f_get_fit_color(fit_pct)
Parameters:
fit_pct (float): Basket fit percentage (0-100)
Returns: Color (lime, aqua, orange, red)
ScanCandidate
Fields:
symbol (series string)
price (series float)
return_1bar (series float)
correlation_raw (series float)
correlation_ema (series float)
score (series float)
is_self (series bool)
BasketSelection
Fields:
sym1 (series string)
sym2 (series string)
sym3 (series string)
sym4 (series string)
score1 (series float)
score2 (series float)
score3 (series float)
score4 (series float)
idx1 (series int)
idx2 (series int)
idx3 (series int)
idx4 (series int)
Added:
f_process_auto_scan(scan_scores, scan_symbols, basket_scan_ready, basket_latched, basket_rescan_due, fallback_sym1, fallback_sym2, fallback_sym3, fallback_sym4, fallback_sym5, fallback_use_sym5, prev_scan_idx_1, prev_scan_idx_2, prev_scan_idx_3, prev_scan_idx_4)
Parameters:
scan_scores (array<float>): Array of 10 correlation scores
scan_symbols (array<string>): Array of 10 candidate symbols
basket_scan_ready (bool): Whether warmup period is complete
basket_latched (bool): Whether basket has been latched (var state)
basket_rescan_due (bool): Whether rescan is due (based on interval)
fallback_sym1 (string)
fallback_sym2 (string)
fallback_sym3 (string)
fallback_sym4 (string)
fallback_sym5 (string)
fallback_use_sym5 (bool): Whether to use 5th symbol in fallback
prev_scan_idx_1 (int)
prev_scan_idx_2 (int)
prev_scan_idx_3 (int)
prev_scan_idx_4 (int)
Returns: [sym1, sym2, sym3, sym4, sym5, use_sym5, auto_score_1-4, scan_idx_1-4, basket_mode_label, new_basket_latched]
Added:
f_calc_scan_correlation_scores(base_ret, scan_prices, scan_syms, corr_len, smooth_len, base_ticker)
Parameters:
base_ret (float): Base asset 1-bar return
scan_prices (array<float>): Array of scan candidate prices (typically 10)
scan_syms (array<string>): Array of scan candidate symbols (typically 10)
corr_len (int): Correlation calculation length
smooth_len (simple int): EMA smoothing length for correlation
base_ticker (string): Base asset ticker for self-reference check
Returns: Array of correlation scores (na or -1.0 for invalid, 0-1 for valid)
Added:
f_basket_new(name, scheme, op, norm, norm_len)
Build empty basket
Parameters:
name (string)
scheme (series WeightScheme)
op (series AggregateOp)
norm (series NormMode)
norm_len (int)
method add(b, symbol, weight, tier)
Add constituent
Namespace types: BasketSpec
Parameters:
b (BasketSpec)
symbol (string)
weight (float)
tier (series LiquidityTier enum from cybermediaboy/AssetLib/1)
method set_active(b, symbol, active)
Toggle constituent active flag by symbol
Namespace types: BasketSpec
Parameters:
b (BasketSpec)
symbol (string)
active (bool)
method count_active(b)
Count active members
Namespace types: BasketSpec
Parameters:
b (BasketSpec)
method compute_weights(b, caps, vols)
Compute normalized weights given scheme + per-constituent stats.
Returns array<float> aligned with b.members (inactive → 0).
For schemes requiring stats (MARKETCAP, INVERSE_VOL), caller
supplies parallel arrays. Pass na arrays when scheme doesn't need them.
Namespace types: BasketSpec
Parameters:
b (BasketSpec)
caps (array<float>)
vols (array<float>)
f_normalize_value(price, mode, mu, sigma, anchor, lo, hi)
Normalize a single constituent series value.
Caller pre-computes window stats (μ, σ, min, max, anchor) and
passes them. This avoids series-len issues on ta.* simple slots.
For ZSCORE: μ = ta.sma(price, len); σ = ta.stdev(price, len) — both series-int OK.
For PCT_CHANGE: anchor = price[1] (or price[len]).
For REBASE_100: anchor = first_non_na_price.
For MINMAX_01: ta.lowest / ta.highest series-int OK in v6.
Parameters:
price (float)
mode (series NormMode)
mu (float)
sigma (float)
anchor (float)
lo (float)
hi (float)
method aggregate(b, values, weights)
Aggregate normalized constituent values using basket op + weights.
`values` and `weights` parallel to b.members. Inactive → ignored.
Namespace types: BasketSpec
Parameters:
b (BasketSpec)
values (array<float>)
weights (array<float>)
f_pair_value(a, b, kind)
Compute pair value given two prices and PairKind
Parameters:
a (float)
b (float)
kind (series PairKind)
f_pair_zscore(pair_val, mu, sigma)
Pair z-score helper.
Caller pre-computes μ and σ via ta.sma/stdev (series-int OK).
Returns (pair_value - μ) / σ.
Parameters:
pair_val (float)
mu (float)
sigma (float)
f_leadlag_new(max_lag)
Parameters:
max_lag (int)
method update(st, x_buf, y_buf, window)
Update lead-lag state.
Caller maintains var rolling buffers x_buf, y_buf of len ≥ max_lag + window.
Returns mutated state with best_lag / best_corr filled.
Cost: O((2·max_lag + 1) · window) — bounded.
Namespace types: LeadLagState
Parameters:
st (LeadLagState)
x_buf (array<float>)
y_buf (array<float>)
window (int)
f_scheme_str(s)
Parameters:
s (series WeightScheme)
f_op_str(o)
Parameters:
o (series AggregateOp)
f_norm_str(m)
Parameters:
m (series NormMode)
f_pairkind_str(k)
Parameters:
k (series PairKind)
Constituent
Constituent — one basket member
Fields:
symbol (series string): ticker (e.g. "BINANCE:BTCUSDT")
weight (series float): normalized [0,1] weight
tier (series LiquidityTier enum from cybermediaboy/AssetLib/1): liquidity tier (from AssetLib classification)
active (series bool): enabled flag
BasketSpec
BasketSpec — basket configuration
Fields:
name (series string): human label
members (array<Constituent>): list of Constituents
scheme (series WeightScheme): weighting scheme
op (series AggregateOp): aggregation operator
norm (series NormMode): per-constituent normalization
norm_len (series int): window for normalization (caller passes simple int when
PairSpec
PairSpec — two-asset relationship descriptor
Fields:
a_symbol (series string): / b_symbol tickers
b_symbol (series string)
kind (series PairKind): PairKind enum
zscore_len (series int): window for z-score of pair value (caller uses
LeadLagState
LeadLagState — tracker for cross-correlation across lags
Fields:
max_lag (series int): maximum offset to scan
corrs (array<float>): array of corr at each lag in [-max_lag, +max_lag]
best_lag (series int): lag with maximum |corr|
best_corr (series float): signed correlation at best_lag
len (series int): window length used (informational)
Removed:
f_calc_scan_correlation_scores(base_ret, scan_prices, scan_syms, corr_len, smooth_len, base_ticker)
f_calc_correlation_score(base_return, candidate_return, corr_len, smooth_len, is_self)
f_rank_and_select(scores, n)
f_is_self_reference(candidate_symbol, base_ticker)
f_route_scan_idx(idx, prices)
f_get_preset_basket(preset_name)
f_get_default_scan_symbols()
f_calc_basket_fit(score1, score2, score3, score4)
f_get_fit_label(fit_pct)
f_get_fit_color(fit_pct)
f_process_auto_scan(scan_scores, scan_symbols, basket_scan_ready, basket_latched, basket_rescan_due, fallback_sym1, fallback_sym2, fallback_sym3, fallback_sym4, fallback_sym5, fallback_use_sym5, prev_scan_idx_1, prev_scan_idx_2, prev_scan_idx_3, prev_scan_idx_4)
ScanCandidate
BasketSelection
Added:
f_is_active(h, l, c, price_eps)
Price-based activity: H/L range OR close delta > eps.
Parameters:
h (float)
l (float)
c (float)
price_eps (float)
f_is_active_close(c, price_eps)
Close-only activity — for macro tickers where H/L is unavailable.
Parameters:
c (float)
price_eps (float)
f_is_active_full(h, l, c, v, session_active, price_eps)
Full sensor: session-gated H/L + close delta + volume.
Returns false outside session when session_active=false.
Parameters:
h (float)
l (float)
c (float)
v (float)
session_active (bool)
price_eps (float)
f_compute_debug_bits(h, l, c, v, session_active, price_eps)
Bit-encoded debug: +1 H≠L, +2 C≠C[1], +4 vol>0, +8 session open.
Parameters:
h (float)
l (float)
c (float)
v (float)
session_active (bool)
price_eps (float)
f_in_session(session_str, tz)
Session check: true when bar is inside the given session schedule.
session_str: TradingView format e.g. "0930-1600:23456"
tz: timezone e.g. "America/New_York", "UTC"
Parameters:
session_str (string)
tz (string)
method update_full(st, price_active, vol_active, in_session, gate_by_session, sleep_bars, wake_bars, warmup_bars)
Update TickerState (full: session + volume + stall detection).
price_active: H/L or close moved
vol_active: volume > 0 (pass false for volumeless tickers)
in_session: from f_in_session() or external schedule check
gate_by_session: when true, forces asleep outside session
Namespace types: TickerState
Parameters:
st (TickerState)
price_active (bool)
vol_active (bool)
in_session (bool)
gate_by_session (bool)
sleep_bars (int)
wake_bars (int)
warmup_bars (int)
method is_live(st)
True when awake AND warmup damping is effectively zero.
Namespace types: TickerState
Parameters:
st (TickerState)
method live_weight(st)
Trust weight [0,1]: 0 when asleep, cubic ramp 0→1 after wake.
Namespace types: TickerState
Parameters:
st (TickerState)
f_sw_ramp(primary_live)
Soft blend ramp weight — +0.15/bar when live, -0.05/bar when dead.
Each call site maintains independent state (Pine v6 stateful function).
Parameters:
primary_live (bool)
f_sw_blend(primary, proxy, ramp)
Blend primary and proxy using hysteresis-driven soft weight. na-safe.
Parameters:
primary (float)
proxy (float)
ramp (float)
TickerState
TickerState — per-ticker hysteresis sleep/wake tracker
Fields:
silent_bars (series int): consecutive dead bars (sleep counter)
awake_bars (series int): consecutive live bars (wake counter)
asleep (series bool): current sleep state; true = ticker considered dead
warmup (series int): bars remaining in post-wake cooldown
warmup_damp (series float): cubic ramp [0,1]; 1=fully suppressed, 0=fully live
stalled (series bool): in session but not publishing price or volume
debug_bits (series int): sensor bits: +1 H≠L, +2 C≠C[1], +4 vol>0, +8 session
Updated:
method add(b, symbol, weight, tier)
Add constituent
Namespace types: BasketSpec
Parameters:
b (BasketSpec)
symbol (string)
weight (float)
tier (series LiquidityTier enum from cybermediaboy/CyberAssetLib/1)
f_normalize_value(price, mode, mu, sigma, anchor, lo, hi)
Normalize a single constituent series value.
Caller pre-computes window stats (μ, σ, min, max, anchor) and
passes them. This avoids series-len issues on ta.* simple slots.
For ZSCORE: μ = sma(price, window); σ = stdev(price, window) — both series-int OK.
For PCT_CHANGE: anchor = price[1] (or price[len]).
For REBASE_100: anchor = first_non_na_price.
For MINMAX_01: ta.lowest / ta.highest series-int OK in v6.
Parameters:
price (float)
mode (series NormMode)
mu (float)
sigma (float)
anchor (float)
lo (float)
hi (float)
method update(st, active, sleep_bars, wake_bars, warmup_bars)
Update TickerState (simple, price-only). Adds cubic warmup_damp.
Namespace types: TickerState
Parameters:
st (TickerState)
active (bool)
sleep_bars (int)
wake_bars (int)
warmup_bars (int)
Constituent
Constituent — one basket member
Fields:
symbol (series string): ticker (e.g. "BINANCE:BTCUSDT")
weight (series float): normalized [0,1] weight
tier (series LiquidityTier enum from cybermediaboy/CyberAssetLib/1): liquidity tier (from AssetLib classification)
active (series bool): enabled flag
Added:
BlendRamp
Fields:
weight (series float)
Perpustakaan pine
Dengan semangat TradingView yang sesungguhnya, penulis telah menerbitkan kode Pine ini sebagai pustaka sumber terbuka agar programmer Pine lain dari komunitas kami dapat menggunakannya kembali. Salut untuk penulis! Anda dapat menggunakan pustaka ini secara pribadi atau dalam publikasi sumber terbuka lainnya, tetapi penggunaan kembali kode ini dalam publikasi diatur oleh Tata Tertib.
Pernyataan Penyangkalan
Perpustakaan pine
Dengan semangat TradingView yang sesungguhnya, penulis telah menerbitkan kode Pine ini sebagai pustaka sumber terbuka agar programmer Pine lain dari komunitas kami dapat menggunakannya kembali. Salut untuk penulis! Anda dapat menggunakan pustaka ini secara pribadi atau dalam publikasi sumber terbuka lainnya, tetapi penggunaan kembali kode ini dalam publikasi diatur oleh Tata Tertib.