TradingView Backtesting

Mutual Information for Trading Features: Binning and Leakage

Mutual information measures dependence between a feature and a target, including nonlinear relationships that Pearson correlation can miss. For trading research, pair a feature available at the decision time with a later outcome, estimate the score under a stated method, and compare it with a valid null baseline. A positive sample score alone does not establish predictive value, causation or a profitable trading rule.

See the decision guide

Calculate mutual information from a contingency table

Suppose a feature is classified as Low or High at a decision timestamp and the subsequent return is classified as Up or Not up. These 20 hypothetical observations illustrate arithmetic, not measured market performance. Each row and each column totals 10. The joint probabilities are the cell counts divided by 20; both marginal distributions are 0.5 and 0.5.

  • Base-2 logarithms give bits; natural logarithms give nats. The scikit-learn mutual_info_score function uses natural logarithms. Divide nats by ln(2) to compare with this example.
  • Here H(Y) = 1 bit and H(Y|X) = 0.721928 bits, so I(X;Y) = H(Y) - H(Y|X). This is an average reduction in uncertainty, not a 27.8% return or a forecast accuracy.
  • For finite discrete variables, 0 ≤ I(X;Y) ≤ min(H(X), H(Y)). A constant feature gives zero empirical MI; a constant target also gives zero and cannot support a directional classification study.
  • With five observations in every cell, joint probabilities equal the products of the marginals and MI is zero. The population zero-independence result does not make a near-zero finite-sample estimate proof of independence.
Feature at decision timeLater UpLater Not upTotal
Low8210
High2810
Total101020
Empirical discrete mutual information; zero-count cells contribute zerotext
I(X;Y) = sum over occupied cells of p(x,y) * log2(p(x,y) / (p(x)*p(y)))
       = 2 * 0.4 * log2(0.4 / 0.25) + 2 * 0.1 * log2(0.1 / 0.25)
       = 0.2780719051 bits
I_nats = I_bits * ln(2) = 0.1927447570 nats

A nonlinear relationship with zero correlation

Let X take -1, 0 and +1 with equal probability, and let Y = X². Then Y is 1, 0 and 1 respectively. E[X] = 0 and E[XY] = 0, so covariance and Pearson correlation are zero. Yet X determines Y exactly: H(Y|X) = 0 and I(X;Y) = H(Y) = 0.918296 bits, or 0.636514 nats.

  • A trading analogue is a feature whose positive and negative extremes both coincide with large subsequent absolute returns. A signed linear relationship can cancel even when magnitude is informative.
  • This constructed distribution is a demonstration, not evidence that any market feature predicts volatility. Define whether Y is signed return, absolute return or a class before estimating MI.
  • MI has no positive or negative direction and is symmetric in X and Y. Inspect conditional outcome distributions to formulate a rule; the score itself is not an entry or exit instruction.

Fit bins on training data and preserve their meaning

Discretization changes the question from dependence in the original values to dependence between their assigned categories. Equal-width bins preserve numeric distances; quantile bins aim for similar training counts. Neither is a universal default. Extra bins create more cells to estimate, often with very few observations.

  • For example, a training median of 0.8 defines Low as X ≤ 0.8 and High as X > 0.8. A later median of 1.1 does not authorize moving that boundary during the held-out evaluation.
  • Compute a new contingency table from held-out pairs using the frozen categories. Its empirical frequencies describe that later sample; the bin definitions must not be re-estimated from it.
  • A raw plug-in table score can be positive under independence because finite counts fluctuate. More sparse categories can magnify this effect. Report sample size, occupied cells and the null distribution alongside any ranking.
ChoiceTraining ruleLater-period treatment
BoundariesChoose bin count and estimate boundaries using training observations only.Apply the stored boundaries unchanged, including explicit outer bins for new extremes.
TiesSpecify which side owns each edge; merge duplicate quantile boundaries and record the effective bin count.Use the same interval convention; do not split identical values arbitrarily.
Missing valuesDefine valid-row and missing-category policies before examining outcomes.Report excluded counts; an unknown return must not become a zero return.
Feature searchSelect features, horizons and estimator settings inside training and validation periods.Keep the final holdout untouched until the specification is fixed.

Continuous estimation avoids bins but still has bias

Nearest-neighbor estimators such as the Kraskov method estimate MI from local distances rather than a fixed grid. They still have finite-sample error: local density variation and the neighborhood scale affect the estimate. Avoid treating a continuous estimator as an unbiased reference against which every binned result must agree.

  • The scikit-learn mutual_info_regression interface estimates dependence with a continuous target and returns nats. For a categorical target, use an estimator intended for that setting; mark discrete features explicitly.
  • Increasing n_neighbors can reduce variance while introducing bias. Repeated continuous values require care; the implementation adds small random noise, so save the random seed and software version.
  • Scikit-learn clips negative estimates to zero. A returned zero can therefore reflect sampling error and clipping, rather than established independence.
  • Compare preselected estimator settings on training folds and report sensitivity to sample size. Bias can depend on distribution, dimension and settings; there is no fixed correction that makes all continuous MI estimates comparable.
  • Raw MI is not generally bounded by one. A binary target limits discrete MI to at most one bit, but a continuous-continuous problem has no such bound. Do not apply the finite discrete entropy bound to differential entropies.

An exact permutation baseline for the small table

For this arithmetic exercise only, assume the 20 rows are exchangeable under the null of independence. Keep the feature groups fixed and assign ten Up labels among the twenty positions. There are C(20,10) = 184,756 equally likely assignments. If a is the number of Up labels in the Low group, the table is [[a,10-a],[10-a,a]] and its probability is C(10,a)² / C(20,10).

  • The positive mean null score shows why zero is an inadequate finite-sample benchmark. Subtracting that mean is only a descriptive adjustment; it does not create a universally unbiased estimator.
  • The exact tail probability above includes both association directions because MI is unsigned. For B randomly sampled valid permutations instead of exhaustive enumeration, a common Monte Carlo estimate is (1 + count of null scores ≥ observed score) / (B + 1). Save B and the seed.
  • If the reported feature was the winner of a search, a single-feature null understates that selection. Repeat the same search within each valid permutation, or use a prespecified multiple-testing procedure and separate evaluation data.
QuantityExact calculationResult
Observed MIa = 80.278072 bits
Mean null MISum I(a) × C(10,a)² / C(20,10), for a = 0,...,100.039054 bits
MI at least as large as observeda ≤ 2 or a ≥ 8: 2 × (1 + 100 + 2025) / 1847560.023014

Why a market series cannot be shuffled like independent rows

Serially dependent features, clustered volatility and overlapping forward returns violate the simple row-exchangeability assumption. Shuffling individual outcomes destroys that structure and changes the null distribution. The exact p-value above is therefore not a ready-made significance test for twenty consecutive market bars.

  • A block-based null may preserve short-range dependence, but its validity needs assumptions about stationarity and dependence, a justified block length and sensitivity checks. The cited temporal-independence paper establishes results for its own procedure; it does not certify an arbitrary block shuffle of MI.
  • Circular shifts also need a justified stationary null and a treatment of the artificial wrap-around boundary. A large shift alone is not proof of validity.
  • Predefine the resampling design for the sampling calendar, sessions and target horizon. If no defensible null is available, report the score as descriptive and rely on a separately specified chronological evaluation rather than presenting an unsupported p-value.

Align future labels before making chronological splits

Store decision time, feature availability time and target end time as separate columns. For a close-to-close research target, pair Xₜ known after close t with Yₜ = Cₜ₊ₕ / Cₜ - 1. The target belongs to row t but becomes observable only at close t+h. This research return does not imply that an order submitted after close t can fill at that same close.

  • Remove training observations whose outcome windows cross the evaluation boundary. With irregular events, compare actual interval endpoints instead of assuming a fixed row gap is sufficient.
  • TradingView chart-data export can include price and indicator columns and reflects loaded chart data. Check time zone, session, adjustments, duplicate timestamps and missing bars before joining features to future outcomes.
  • Use only indicator values available at the decision time, including confirmed higher-timeframe inputs. Recomputed or future-informed values can produce large MI even when the original decision had no such information.
  • Freeze the feature set and thresholds before testing. If the held-out result prompts another change, that period is now validation data; reserve a later untouched period for the next evaluation.
Example with h = 5 barsRequired treatment
Training ends at bar 100Use training feature rows only through bar 95, so every training label has ended by bar 100.
Test decisions run from bars 101 through 150If target prices stop at 150, score only decisions through bar 145. Later labels are incomplete.
Adjacent five-bar labelsTheir windows share four one-bar returns. A chronological split alone does not make observations independent.
Strategy enters at the next openDefine the target from that executable entry convention and include the intended exit horizon and costs.

Connect the feature study to a reproducible strategy test

A high univariate MI score does not show incremental information beyond other features or economic value after costs. Compare the frozen strategy with and without the candidate feature on the same later dates, execution assumptions and costs. Keep the full feature dataset and its timing audit alongside the strategy trade export.

  • Backtest Deep Report imports TradingView List of Trades CSV data and calculates supported trade results, including net profit and drawdown. Its parsed trade fields do not contain a full bar-level feature matrix or future-label table.
  • Compute MI and its null baseline separately. This workflow does not claim that Backtest Deep Report has an MI estimator or that Pineify provides built-in statistical feature selection.
  • Pine Script AI Coding Agent can help draft a rule from an explicit feature specification. Review feature availability and fill timing, validate the script, and compare its outputs with the independent research data.
Where Pineify fits

Backtest Deep Report

Review supported profit and drawdown results from exported strategy trades after completing the separate feature and label study.

Also useful: Pine Script AI Coding Agent. Draft a rule with explicit feature timing, entry assumptions and exits, then validate the code and its outputs.

Review exported strategy trades

Frequently asked questions

Educational information, not investment advice. Numerical examples are hypothetical, not measured trading results. Statistical dependence and historical backtests do not guarantee future returns.

Sources and verification

Published by Pineify. Pineify is an independent product and is not affiliated with TradingView. Sources support the technical explanations; examples do not establish trading performance.