PINE LIBRARY
Updated

MCLib

231
Library "MCLib"

f_eval_validator_path(sim_buffer, run, setup_horizon, setup_entry_price, setup_direction, setup_tp1, setup_sl, fv_base, fv_drift_step, max_hold_bars)
  Parameters:
    sim_buffer (array<float>)
    run (int)
    setup_horizon (int)
    setup_entry_price (float)
    setup_direction (string)
    setup_tp1 (float)
    setup_sl (float)
    fv_base (float)
    fv_drift_step (float)
    max_hold_bars (int)

f_calc_validator_rr(entry_price, eval_direction, mae_price, mfe_price)
  Parameters:
    entry_price (float)
    eval_direction (string)
    mae_price (float)
    mfe_price (float)

f_run_lite_antithetic_mc(sim_buffer_A, sim_buffer_B, lite_runs, setup_horizon, setup_entry_price, mc_pool_idx, mc_master_pool, mc_current_state, mc_pool_size, per_bar_vol, mc_squeeze_intensity, fv_cyclic_kalman, fv_drift, c0, is_stretched, is_coupled, shadow_price)
  Parameters:
    sim_buffer_A (array<float>)
    sim_buffer_B (array<float>)
    lite_runs (int)
    setup_horizon (int)
    setup_entry_price (float)
    mc_pool_idx (array<int>)
    mc_master_pool (array<float>)
    mc_current_state (int)
    mc_pool_size (int)
    per_bar_vol (float)
    mc_squeeze_intensity (float)
    fv_cyclic_kalman (float)
    fv_drift (float)
    c0 (float)
    is_stretched (bool)
    is_coupled (bool)
    shadow_price (float)

f_run_realtime_mc_chunk(mc_sim_buffer_A, mc_sim_buffer_B, mc_runs_done, mc_target_runs, mc_chunk_size, mc_horizon, mc_pool_idx, mc_master_pool, mc_current_state, mc_pool_size, per_bar_vol, mc_vol_scalar_base, mc_squeeze_intensity, mc_breakout_multiplier, fv_cyclic_kalman, fv_drift, c0, is_stretched, is_coupled, shadow_price)
  Parameters:
    mc_sim_buffer_A (array<float>)
    mc_sim_buffer_B (array<float>)
    mc_runs_done (int)
    mc_target_runs (int)
    mc_chunk_size (int)
    mc_horizon (int)
    mc_pool_idx (array<int>)
    mc_master_pool (array<float>)
    mc_current_state (int)
    mc_pool_size (int)
    per_bar_vol (float)
    mc_vol_scalar_base (float)
    mc_squeeze_intensity (float)
    mc_breakout_multiplier (float)
    fv_cyclic_kalman (float)
    fv_drift (float)
    c0 (float)
    is_stretched (bool)
    is_coupled (bool)
    shadow_price (float)

f_update_progressive_percentiles(mc_sim_buffer_A, mc_sim_buffer_B, mc_horizon, mc_runs_done, mc_progressive_p10, mc_progressive_p50, mc_progressive_p90)
  Parameters:
    mc_sim_buffer_A (array<float>)
    mc_sim_buffer_B (array<float>)
    mc_horizon (int)
    mc_runs_done (int)
    mc_progressive_p10 (array<float>)
    mc_progressive_p50 (array<float>)
    mc_progressive_p90 (array<float>)
Release Notes
v2

Added:
MCPhysicsParams
  Monte Carlo physics parameters for generic simulation configuration
  Fields:
    squeeze_intensity (series float): Squeeze intensity multiplier (0.0 = no amplitude prediction, per U10)
    use_mean_reversion (series bool): Enable mean reversion pull toward fair value
    mr_strength (series float): Mean reversion strength coefficient
    use_vector_coupling (series bool): Enable vector coupling to shadow price
    vector_strength (series float): Vector coupling strength coefficient
    shadow_price (series float): Shadow price for vector coupling (if enabled)
Release Notes
v3

Updated:
f_eval_validator_path(sim_buffer, run, setup_horizon, setup_entry_price, setup_direction, setup_sl, max_hold_bars)
  Parameters:
    sim_buffer (array<float>)
    run (int)
    setup_horizon (int)
    setup_entry_price (float)
    setup_direction (string)
    setup_sl (float)
    max_hold_bars (int)

f_run_lite_antithetic_mc(sim_buffer_A, sim_buffer_B, lite_runs, setup_horizon, setup_entry_price, mc_pool_idx, mc_master_pool, mc_current_state, mc_pool_size, per_bar_vol, mc_squeeze_intensity, fv_cyclic_kalman, fv_drift, c0, is_stretched, is_coupled)
  Parameters:
    sim_buffer_A (array<float>)
    sim_buffer_B (array<float>)
    lite_runs (int)
    setup_horizon (int)
    setup_entry_price (float)
    mc_pool_idx (array<int>)
    mc_master_pool (array<float>)
    mc_current_state (int)
    mc_pool_size (int)
    per_bar_vol (float)
    mc_squeeze_intensity (float)
    fv_cyclic_kalman (float)
    fv_drift (float)
    c0 (float)
    is_stretched (bool)
    is_coupled (bool)

f_run_realtime_mc_chunk(mc_sim_buffer_A, mc_sim_buffer_B, mc_runs_done, mc_target_runs, mc_chunk_size, mc_horizon, mc_pool_idx, mc_master_pool, mc_current_state, mc_pool_size, per_bar_vol, mc_vol_scalar_base, mc_squeeze_intensity, mc_breakout_multiplier, fv_cyclic_kalman, fv_drift, c0, is_stretched, is_coupled)
  Parameters:
    mc_sim_buffer_A (array<float>)
    mc_sim_buffer_B (array<float>)
    mc_runs_done (int)
    mc_target_runs (int)
    mc_chunk_size (int)
    mc_horizon (int)
    mc_pool_idx (array<int>)
    mc_master_pool (array<float>)
    mc_current_state (int)
    mc_pool_size (int)
    per_bar_vol (float)
    mc_vol_scalar_base (float)
    mc_squeeze_intensity (float)
    mc_breakout_multiplier (float)
    fv_cyclic_kalman (float)
    fv_drift (float)
    c0 (float)
    is_stretched (bool)
    is_coupled (bool)
Release Notes
v4

Added:
f_draw_mc_cones(cone_gate, p10, p50, p90, horizon, l10_arr, l50_arr, l90_arr, fill_arr, show_med, opacity, current_bar_index, mintick)
  Parameters:
    cone_gate (bool)
    p10 (array<float>)
    p50 (array<float>)
    p90 (array<float>)
    horizon (int)
    l10_arr (array<line>)
    l50_arr (array<line>)
    l90_arr (array<line>)
    fill_arr (array<linefill>)
    show_med (bool)
    opacity (float)
    current_bar_index (int)
    mintick (float)
Release Notes
v5
Release Notes
v6

Added:
f_get_mc_family(f1_vec, f2_corr, f3_innov, f4_te, f5_orth, f6_phi)
  Classify current market state into MC Family for path interpretation
  Parameters:
    f1_vec (float): VectorOsc (z-scored)
    f2_corr (float): BasketCorr (normalized)
    f3_innov (float): InnovZ (z-scored)
    f4_te (float): TE_Osc (normalized)
    f5_orth (float): OrthoZcvb (z-scored)
    f6_phi (float): PhiDiv (normalized)
  Returns: MC Family ID: 0=Continuation, 1=Mean-Revert, 2=Transition, 3=Velocity, 4=Uncertain
Release Notes
v7

Added:
f_welford_knn_bootstrap(final_knn_ids, cumw, arr_close, arr_ATR, mc_horizon, mc_runs, mc_noise_scale, current_close, pathsUpAtH, mc_progressive_p10, mc_progressive_p50, mc_progressive_p90, bar_idx, time_val)
  Run Welford MC bootstrap with kNN-weighted path resampling and antithetic noise
  Parameters:
    final_knn_ids (array<int>): kNN neighbor indices
    cumw (array<float>): Cumulative weights for neighbor selection
    arr_close (array<float>): Close price history
    arr_ATR (array<float>): ATR history
    mc_horizon (int): Simulation horizon
    mc_runs (int): Maximum MC runs (capped at 500)
    mc_noise_scale (float): Noise scaling factor
    current_close (float): Current close price
    pathsUpAtH (array<int>): Output: paths above current price at each horizon step (mutated)
    mc_progressive_p10 (array<float>): Output: 10th percentile path (mutated)
    mc_progressive_p50 (array<float>): Output: 50th percentile path (mutated)
    mc_progressive_p90 (array<float>): Output: 90th percentile path (mutated)
    bar_idx (int): Current bar index
    time_val (int): Current time value
  Returns: [paths_up, total_paths, wMean, wM2, wCount] for further analysis
Release Notes
v8

Added:
f_welford_knn_chunk(knn_ids, cumw, arr_close, arr_ATR, mc_horizon, chunk_runs, mc_noise_scale, current_close, wMean, wM2, wCount, pathsUp, run_offset, bar_idx, time_val)
  Run a CHUNK of MC runs, updating persistent Welford accumulators across ticks
  Parameters:
    knn_ids (array<int>): kNN neighbor indices
    cumw (array<float>): Cumulative weights for neighbor selection
    arr_close (array<float>): Close price history
    arr_ATR (array<float>): ATR history
    mc_horizon (int): Simulation horizon
    chunk_runs (int): Number of runs to execute in this chunk
    mc_noise_scale (float): Noise scaling factor
    current_close (float): Current close price
    wMean (array<float>): Persistent Welford mean accumulator (mutated)
    wM2 (array<float>): Persistent Welford M2 accumulator (mutated)
    wCount (array<int>): Persistent Welford count accumulator (mutated)
    pathsUp (array<int>): Persistent paths-up counter (mutated)
    run_offset (int): Starting run index for unique PRNG seeds
    bar_idx (int): Current bar index
    time_val (int): Current time value
  Returns: int Number of paths executed (chunk_runs × 2 for antithetic)
Release Notes
v9

Added:
f_render_backtest_table(tbl, raw_total, raw_wins, raw_pnl_sum, raw_mae_sum, raw_mfe_sum, raw_mean, raw_m2, filt_total, filt_wins, filt_pnl_sum, filt_mae_sum, filt_mfe_sum, filt_mean, filt_m2, avg_agree)
  Render backtest summary table (Phase 6)
  Parameters:
    tbl (table): Table object to populate
    raw_total (int): Total raw setups
    raw_wins (int): Raw wins count
    raw_pnl_sum (float): Raw PnL sum
    raw_mae_sum (float): Raw MAE sum
    raw_mfe_sum (float): Raw MFE sum
    raw_mean (float): Raw Welford mean
    raw_m2 (float): Raw Welford M2
    filt_total (int): Filtered setups (MC confirmed)
    filt_wins (int): Filtered wins count
    filt_pnl_sum (float): Filtered PnL sum
    filt_mae_sum (float): Filtered MAE sum
    filt_mfe_sum (float): Filtered MFE sum
    filt_mean (float): Filtered Welford mean
    filt_m2 (float): Filtered Welford M2
    avg_agree (float): Average MC agreement %
Release Notes
v10
Release Notes
v11
Release Notes
v12

Added:
f_prng(seed1, seed2)
  Deterministic pseudo-random number generator (0.0 to 1.0)
  Parameters:
    seed1 (int): Primary seed (e.g., bar_index or iteration counter)
    seed2 (int): Secondary seed (e.g., run number or feature index)
  Returns: Uniform random float in [0.0, 1.0)
note Deterministic - same seeds always produce same output (no time dependency)

Updated:
f_welford_knn_bootstrap(final_knn_ids, cumw, arr_close, arr_ATR, mc_horizon, mc_runs, mc_noise_scale, current_close, pathsUpAtH, mc_progressive_p10, mc_progressive_p50, mc_progressive_p90, bar_idx)
  Run Welford MC bootstrap with kNN-weighted path resampling and antithetic noise
  Parameters:
    final_knn_ids (array<int>): kNN neighbor indices
    cumw (array<float>): Cumulative weights for neighbor selection
    arr_close (array<float>): Close price history
    arr_ATR (array<float>): ATR history
    mc_horizon (int): Simulation horizon
    mc_runs (int): Maximum MC runs (capped at 500)
    mc_noise_scale (float): Noise scaling factor
    current_close (float): Current close price
    pathsUpAtH (array<int>): Output: paths above current price at each horizon step (mutated)
    mc_progressive_p10 (array<float>): Output: 10th percentile path (mutated)
    mc_progressive_p50 (array<float>): Output: 50th percentile path (mutated)
    mc_progressive_p90 (array<float>): Output: 90th percentile path (mutated)
    bar_idx (int): Current bar index (used for deterministic PRNG seed)
  Returns: [paths_up, total_paths, wMean, wM2, wCount] for further analysis

f_welford_knn_chunk(knn_ids, cumw, arr_close, arr_ATR, mc_horizon, chunk_runs, mc_noise_scale, current_close, wMean, wM2, wCount, pathsUp, run_offset, bar_idx)
  Run a CHUNK of MC runs, updating persistent Welford accumulators across ticks
  Parameters:
    knn_ids (array<int>): kNN neighbor indices
    cumw (array<float>): Cumulative weights for neighbor selection
    arr_close (array<float>): Close price history
    arr_ATR (array<float>): ATR history
    mc_horizon (int): Simulation horizon
    chunk_runs (int): Number of runs to execute in this chunk
    mc_noise_scale (float): Noise scaling factor
    current_close (float): Current close price
    wMean (array<float>): Persistent Welford mean accumulator (mutated)
    wM2 (array<float>): Persistent Welford M2 accumulator (mutated)
    wCount (array<int>): Persistent Welford count accumulator (mutated)
    pathsUp (array<int>): Persistent paths-up counter (mutated)
    run_offset (int): Starting run index for unique PRNG seeds
    bar_idx (int): Current bar index (used for deterministic PRNG seed)
  Returns: int Number of paths executed (chunk_runs × 2 for antithetic)
Release Notes
v13

Removed:
f_run_lite_antithetic_mc(sim_buffer_A, sim_buffer_B, lite_runs, setup_horizon, setup_entry_price, mc_pool_idx, mc_master_pool, mc_current_state, mc_pool_size, per_bar_vol, mc_squeeze_intensity, fv_cyclic_kalman, fv_drift, c0, is_stretched, is_coupled)

f_run_realtime_mc_chunk(mc_sim_buffer_A, mc_sim_buffer_B, mc_runs_done, mc_target_runs, mc_chunk_size, mc_horizon, mc_pool_idx, mc_master_pool, mc_current_state, mc_pool_size, per_bar_vol, mc_vol_scalar_base, mc_squeeze_intensity, mc_breakout_multiplier, fv_cyclic_kalman, fv_drift, c0, is_stretched, is_coupled)

f_update_progressive_percentiles(mc_sim_buffer_A, mc_sim_buffer_B, mc_horizon, mc_runs_done, mc_progressive_p10, mc_progressive_p50, mc_progressive_p90)

f_get_mc_family(f1_vec, f2_corr, f3_innov, f4_te, f5_orth, f6_phi)
  Classify current market state into MC Family for path interpretation

Disclaimer

The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.