TradingView Backtesting

Sample Entropy in Price Research: Window, Tolerance and Data Length

Sample entropy (SampEn) estimates how often similar sequences remain similar one observation later. Count matching pairs of length m as B and matching extensions of length m+1 as A, exclude self-matches, and calculate -ln(A/B). A lower value means more matches survive under the selected settings. It does not establish a profitable forecast, and some samples cannot produce a finite estimate.

See the decision guide

Define exactly which pairs can match

For N observations x(1),...,x(N), choose an integer m ≥ 1 and use starts i = 1,...,N-m. At each start, form the length-m vector [x(i),...,x(i+m-1)] and its extension through x(i+m). Compare each unordered pair i < j once. The final possible length-m template, starting at N-m+1, is excluded because it has no extension. This is the common-start convention described by Richman, Lake and Moorman.

  • Use Chebyshev distance: d_m(i,j) = max over k = 0,...,m-1 of |x(i+k)-x(j+k)|. A pair matches when d_m ≤ r, where r is an absolute tolerance in the units of x. Equality counts here; software using a strict inequality can disagree at the boundary.
  • B counts matches at length m. A counts those same pairs that also satisfy |x(i+m)-x(j+m)| ≤ r. There are M = N-m eligible starts and M(M-1)/2 candidate pairs, so 0 ≤ A ≤ B. Ordered-pair counting doubles both counts and leaves A/B unchanged.
  • Exclude i = j at both lengths. Distinct templates may overlap in time in this convention. Excluding all overlapping templates is an additional rule and changes the statistic; it is not the same as removing self-matches.
  • SampEn = -ln(A/B) uses the natural logarithm and reports nats. A base-2 version reports bits and equals SampEn/ln(2). This score is not normalized to a universal range from 0 to 1.

Enumerate an eight-observation example

Use the constructed series x = [0, 0, 1, 0, 0, 1, 0, 1], m = 2 and absolute r = 0.2. The six eligible length-2 templates are [0,0], [0,1], [1,0], [0,0], [0,1], [1,0]. Their next observations are 1, 0, 0, 1, 0, 1. Of the 15 candidate pairs, only the three pairs below match at length 2.

  • B = 3 and A = 2, so SampEn = -ln(2/3) = 0.405465 nats, or 0.584963 bits. The other 12 pairs have a coordinate difference of 1, greater than r.
  • Starting at position 7 gives another [0,1] template, but no third observation. Including it only in B adds matches with starts 2 and 5, falsely changing the common-start calculation to -ln(2/5) = 0.916291.
  • The six self-pairs would all match at both lengths. Including them with these unordered counts would instead give -ln(8/9) = 0.117783, illustrating why the counting rule matters.
Starts (one-based)Matching length-2 vectorsNext valuesExtension matches?
(1, 4)[0,0] and [0,0]1 and 1Yes
(2, 5)[0,1] and [0,1]0 and 0Yes
(3, 6)[1,0] and [1,0]0 and 1No

Separate insufficient data, no matches and infinite estimates

N ≥ m+2 is only the structural minimum for one eligible pair. It does not guarantee matches or a reliable estimate. Keep the status and the A and B counts with every result; replacing every failure with zero would make failed estimation look like complete continuation.

  • An infinite estimate does not prove that the underlying process has infinite entropy. A longer sample or a different prespecified tolerance may produce forward matches. Adding a pseudocount changes the estimator and must be disclosed.
  • A constant series has A = B and SampEn = 0 under an absolute nonnegative tolerance with inclusive matching. If tolerance is defined through standardization by a zero standard deviation, the scale-based workflow is degenerate and should report that status.
  • A and B can share observations across many pairs. Treating B as a count of independent Bernoulli trials understates some sources of uncertainty. Report sensitivity and assess uncertainty with a method that respects the dependence.
ConditionResultInterpretation
N < m+2Insufficient data; no scoreFewer than two extendable templates exist
B = 0Undefined; no scoreNo conditioning matches, so A/B is 0/0
B > 0, A = 0+∞; no finite estimateObserved matches never extend; the estimated continuation probability is zero
0 < A < B-ln(A/B) > 0Some matching templates extend
A = B > 00Every observed matching template extends

Test window length and tolerance together

Perturb the example to y = [0, 0.25, 1, 0, 0.5, 1, 0, 1]. The table uses m = 2, inclusive matching and absolute tolerances. N = 6 means the first six observations; N = 8 uses all eight. These are arithmetic experiments on invented values, not market benchmarks or recommended lookbacks.

  • Widening r cannot reduce either count, but their ratio need not change monotonically. At N = 8, increasing r from 0.25 to 0.5 raises SampEn; increasing it to 1 makes every pair match and reduces SampEn to zero.
  • A longer window creates more candidate pairs and can mix different market conditions. Compare several prespecified lengths and report the counts and unavailable-result rate, rather than selecting the length with the most appealing score.
  • Increasing m uses longer patterns and fewer eligible starts. It can leave too few matches. There is no fixed sample size that guarantees reliable estimation for every series, m and tolerance.
Window NAbsolute rABSampEn (nats)
60.12500Undefined
60.25110.000000
60.5120.693147
61660.000000
80.12501+∞
80.25230.405465
80.5350.510826
8115150.000000

State what a standard-deviation tolerance means

A relative tolerance uses r = c × s. This page defines s = sqrt(Σ(x-mean(x))²/N), the population-form standard deviation of the current window. The unperturbed eight-point example has mean 0.375, s = 0.484123 and, for c = 0.2, r = 0.096825. Its exact-match counts remain A = 2 and B = 3.

  • Using an N-1 variance denominator instead gives s = 0.517549 and r = 0.103510. The counts happen to remain unchanged for this binary example, but observations near the tolerance boundary can change classification. State the denominator instead of relying on a library default.
  • Recomputing s in each trailing window keeps c fixed while changing the absolute distance threshold. Freezing s on a development period keeps the absolute threshold fixed instead. These answer different questions and must not be mixed in one comparison.
  • Multiplying all observations by a nonzero constant preserves relative-tolerance matches in exact arithmetic. This removes amplitude scale from the score; two series with very different volatility can have the same SampEn.
  • An outlier can inflate s and allow many ordinary observations to match. Inspect the observations and the resulting absolute r before interpreting a low score as regular market behavior. Quantization, repeated stale prices and synthetic gap fills can also create matches.

Reproduce the calculation in Python

This standard-library implementation returns the counts, actual tolerance and estimation status. It uses consecutive observations, the common start set and no exclusion of overlapping distinct templates. Its O(N²m) pair scan is intended for transparent verification; long rolling histories need a faster implementation checked against the same convention.

Absolute tolerance is the default. relative=True multiplies tolerance by the current-window population standard deviation.python
from math import isfinite, log
from statistics import pstdev


def sample_entropy(values, m=2, tolerance=0.2, relative=False):
    x = [float(value) for value in values]
    if type(m) is not int or m < 1:
        raise ValueError("m must be a positive integer")
    if not all(isfinite(value) for value in x):
        raise ValueError("Observations must be finite")
    r = float(tolerance)
    if not isfinite(r) or r < 0:
        raise ValueError("Tolerance must be finite and nonnegative")
    result = {"N": len(x), "m": m, "r": None,
              "A": 0, "B": 0, "SampEn": None}
    if len(x) < m + 2:
        return dict(result, status="insufficient_data")
    if relative:
        s = pstdev(x)
        if not isfinite(s) or s == 0:
            return dict(result, status="degenerate_scale")
        r *= s
    if not isfinite(r):
        raise ValueError("Absolute tolerance overflowed")
    result["r"] = r
    starts = len(x) - m
    for i in range(starts):
        for j in range(i + 1, starts):
            if all(abs(x[i+k] - x[j+k]) <= r for k in range(m)):
                result["B"] += 1
                if abs(x[i+m] - x[j+m]) <= r:
                    result["A"] += 1
    a, b = result["A"], result["B"]
    if b == 0:
        return dict(result, status="no_template_matches")
    if a == 0:
        return dict(result, SampEn=float("inf"),
                    status="no_forward_matches")
    score = 0.0 if a == b else -log(a / b)
    return dict(result, SampEn=score, status="finite")


x = [0, 0, 1, 0, 0, 1, 0, 1]
print(sample_entropy(x))
y = [0, 0.25, 1, 0, 0.5, 1, 0, 1]
for n in (6, 8):
    for r in (0.125, 0.25, 0.5, 1):
        print(sample_entropy(y[:n], tolerance=r))

Choose the observation series before testing a rule

Price levels, bar returns and trade outcomes describe different processes. For positive prices, log returns ln(P(t)/P(t-1)) remove the price unit but do not guarantee stationarity. Specify the chart interval, session, adjustment policy and missing-bar handling. Do not delete gaps and then treat the remaining rows as equally spaced bars.

  • TradingView chart-data export provides chart and indicator values from loaded data. Its Strategy Report exports trade information and summary metrics separately. Use chart observations for a bar-based series; a trade list is an event-time sample with unequal elapsed times.
  • For a trading feature, calculate each trailing window using only observations available at that decision time. If a bar-close value enters the feature, the execution assumption must respect when that close becomes known. Fit any preprocessing and choose N, m, c and thresholds on development data.
  • Evaluate the frozen rule on later dates with fees, slippage and realistic fills. Overlapping windows share data, so adjacent SampEn values do not represent independent evidence. Low entropy supplies neither a trade direction nor a profitable entry and exit.
  • Pineify Backtest Deep Report parses trade timestamps, entry and exit prices, quantity and P&L, and exposes supported metrics including net profit and profit factor. Use it to audit the resulting strategy trades. The SampEn calculation here is separate; it is not claimed as a built-in report field.
  • Pine Script AI Coding Agent can help draft custom indicator or strategy code from a specification. Give it the exact count convention and boundary statuses, review its output, and compare exported values with this independent calculation before using a filter.

Keep the different entropy questions separate

Sample entropy concerns the continuation of nearby templates. Other entropy methods construct different distributions, so their numbers and thresholds are not interchangeable. None of these scores alone demonstrates an economic edge.

MethodQuantity summarizedRequired choices
Sample entropyAmplitude-template matches that survive one extensionm, distance, tolerance, eligible pairs and window
Spectral entropyDistribution of power across frequency binsSpectrum estimator, frequency range and normalization
Permutation entropyFrequencies of ordinal patternsPattern length, delay and treatment of ties
Hurst analysisScaling or persistence across horizonsEstimator, scales and assumptions about the process
Where Pineify fits

Backtest Deep Report

Audit exported strategy trades and supported performance metrics after testing a separately calculated SampEn feature.

Also useful: Pine Script AI Coding Agent. Draft custom series logic from an explicit specification, then verify its values and timing independently.

Review Backtest Deep Report

Frequently asked questions

Educational information, not investment advice. Numerical examples use constructed observations, not measured trading performance. Sample entropy 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.