Machine Learning Statistics: kNN Win Rate vs Random Entries

1 601
Before you trust a "machine learning" indicator - or pay for one - you
can test it. This script measures the method most published ML
indicators are built on against entries taken at random, on your
instrument, your timeframe, your chart.

Why random entries are the comparison that matters. A win rate
on its own tells you very little. Buy anything at any moment, put a
target above and a stop below, and you will win roughly half the time.
That is arithmetic, not skill. So the useful question about a signal is
not "how often does it win" but "how often does it win compared with
entering at a random moment instead". This script answers that
question. For every signal the method produces, it also places matched
random entries - same direction, same target and stop distances,
anchored at bars where no signal happened - then scores both and
reports the difference.

The measurement is tested before it is trusted. The script can
replace price with artificial data that contains one deliberate,
repeating pattern - put there on purpose, so there is definitely
something to find. A plain RSI rule - the relative strength index, a
standard overbought and oversold gauge - reads that pattern directly
and won 75.7% of its trades. Randomly timed trades on the same bars won
50.6%. The pattern was there, and a simple rule found it.

The same test then handed the widely published machine-learning method
the same data, with the pattern sitting in the exact measurements it
looks at. It won 39.9%. Randomly timed trades won 49.3%. It did not
merely fail to find the pattern. It finished 9.4 percentage points
behind random timing - a percentage point being the plain difference
between two percentages, so 51% against 50% is one point - and the gap
grew wider the stronger the pattern was made.

That is the demonstration this script exists to make possible: an
instrument that finds an edge when one is really there, and reports
nothing when there is not. What it reports on live market data is
below.

snapshot
The script on a live chart, with both populations drawn. The green and
red pins are signals from the machine-learning method - green below the
bar for long, red above it for short - created only once the bar has
closed, so nothing is moved or redrawn afterwards. The grey dots are
the control: matched random entries, three for every signal, taken in
the same direction with the same target and stop distances but at bars
where no signal happened. Those are what the signals get measured
against, and the key in the corner names both. The panel reports the
two win rates and the gap between them: 50.1% for the signals against
49.5% for random entries, a difference of 0.6 percentage points across
812 signal trades.


What to Use It For

Checking an ML indicator before you trust it. The method measured
here is the one most published ML indicators are built on: nearest-
neighbor classification over oscillator readings. If you use one, this
tells you what its timing was worth on your instrument, over a window
you choose.

Seeing what a win rate is worth without a control. Every cell in
this study has a raw win rate near 50% and a controlled result near
zero. The raw number and the meaningful number look nothing alike. Run
the script with the control switched off and back on to see it.

Testing the measurement itself. Everything here can be checked.
The synthetic mode lets you plant a pattern and confirm the instrument
finds it, before you believe anything it says about a real market.


The Result in Plain Language

Across three instruments at the default settings, the method's signals
won 48.8% of the time. Matched random entries on the same instruments
won 49.9%. The difference is -1.1 percentage points.

That difference is smaller than what this test could reliably detect.
On this sample the smallest difference the measurement could resolve is
about 2.9 percentage points. So the honest statement is not "the method
is worthless" - it is this:

On this sample, the method's signal timing cannot be told apart from
random timing, and any real advantage larger than about 3 percentage
points would have shown up.


The same answer came back on every configuration tested: different
numbers of neighbors, fewer features, different training lengths,
different targets, both timeframes, and a separate year of data that
shares no bars with the main window. Twenty configurations, and not one
of them produced a result large enough to be distinguished from chance.

The sharpest single test came from switching both of the method's
filters off, which produces far more signals and therefore a finer
measurement. 2,782 signal trades against 8,181 random ones:

Pine Script®



How the Test Works

The signal. The script rebuilds the published method from its
open source: five oscillator readings per bar, a search for past bars
whose readings look similar, and a vote among those neighbors. When
the vote changes sign, that is the signal, and that is the entry.

The trade. Every entry is taken at the close of the bar where
the signal appeared. A target and a stop are placed the same distance
above and below, measured in average true range, which is roughly how
far price travels in one bar. Whichever is reached first decides the
outcome. Trades that reach neither within the time limit are reported
separately and left out of the win rate.

The control. Each signal also creates three random entries, at
fixed distances later on the chart, in the same direction, with the
same target and stop distances. Nothing about them is tied to a signal.
They answer the question "what would any trade of this shape have
returned over this sample".

The comparison. Signals minus random entries, in percentage
points. That is the number this script exists to produce.

snapshot
The same chart and run as above, with the full accounting shown. Every
signal the method produced is followed down the panel into exactly one
outcome, and every random entry alongside it: 862 signals, 821 of them
inside the chosen date window, and 2,430 matched random entries built
from those. Nothing is discarded quietly - each rejected signal is
counted on its own line, the trades still running when the window ended
are counted too, and the error counter near the bottom has to read zero
for the run to be valid.


Validation

Two tests with known answers, both built into the script.

The blank test. Price is replaced with a random walk containing
no pattern at all. Both signals and random entries must then land at
50%. Across sixteen different random walks, signals came in at 49.88%
and random entries at 49.52%, a difference of 0.27 percentage points.
Nothing was found, because there was nothing there.

The planted test. A pattern is added to the artificial data, at
three strengths, sitting in exactly the measurements the method reads.
A simple rule that looks at that pattern directly gains more as the
pattern gets stronger:

Pine Script®


This is what makes the market result believable. The measurement finds
a real edge when one is put there, and reports nothing when there is
nothing to find.

What the method did on the planted data. It went the other way.
The stronger the pattern, the further behind random timing it finished:
-2.48, then -6.75, then -9.40 percentage points. On the same bars, with
the same targets, the simple rule was gaining 25 points.

This is a result on artificial data with one setting per strength, and
it is reported as an observation, not an explanation. There is a
candidate reason in how the published method picks which past bars to
vote - it may keep the least similar ones rather than the most similar
- but that has not been tested and no claim is made here.

snapshot
The test with a known answer. Price has been replaced by artificial
data carrying one deliberate, repeating pattern, so there is certainly
something to find. The lower block of the panel is a plain RSI rule
reading that pattern directly: it wins 75.9% against 50.6% for random
entries, a gap of 25.3 points. The upper block is the machine-learning
method given the same data: 39.7% against 49.3%, a gap of -9.6 points.
A simple rule found the pattern; the method finished behind random
timing. These figures move by a few tenths of a point as a chart loads
more bars, because this test uses all of them. The candles behind the
panel are artificial and show nothing meaningful.


Results

Baseline configuration, 30 minutes, 2025-01-01 to 2026-08-01, the
method's own default settings throughout:

Pine Script®


The three instruments do not disagree by more than ordinary sampling
variation, so the pooled figure stands. The range of plausible values
around the pooled -1.10 runs from -3.6 to +1.5 percentage points, and
includes zero.

Every other configuration tested, all on BTCUSDT at 30 minutes:

Pine Script®


On the hourly chart, across the same three instruments, +0.94 points.
Over calendar 2024, a window sharing no bars with the main one, -1.01
points. Both ranges of uncertainty include zero.

To reproduce these figures: the script's defaults match the
baseline. Switch the date range on, set 2025-01-01 to 2026-08-01, turn
compact panel mode off, and load the full chart history.


Terms Used in the Panel

Signals. Entries taken where the method's vote changed sign.

Random / placebo. Matched entries at fixed offsets later, same
direction and same target distances, anchored where no signal occurred.

Ambiguous outcome. A bar whose range contains both the target
and the stop. Bar data cannot show which came first, so the trade is
counted against the win rate - the cautious choice.

Censored outcome. A trade that reached neither target nor stop
before the time limit. Reported on its own and kept out of the win rate.

95% interval. The range of values the true answer plausibly sits
in. When it includes 50%, the result cannot be told apart from a coin
flip.

Smallest detectable difference. How small a difference this test
could have found on data with no edge. Anything smaller than it is not
evidence.


Settings

Classifier. Number of neighbors, training length, how many
oscillator readings to use, how far ahead the training labels look, and
which of the two labeling conventions to use. The method's own filters
can each be switched off.

Measurement. Target and stop size, the averaging length behind
them, and the time limit.

Control. How many random entries per signal and how far ahead
they are placed.

Sample. The date range uses explicit year, month and day fields
rather than a date picker, so a published sample can be reproduced
exactly.

Validation. Synthetic mode, seed, step size, drift, and the
planted-pattern strength.

Display. Compact panel, signal markers, marker size.


Limitations

The sample is smaller than intended. The target set before any
run was 1,000 completed trades per test. The main test produced 812,
and the platform's history limit made 1,000 unreachable at the
published settings on any instrument, timeframe or date range tried.
Every figure above is reported with the precision it actually achieved:
about 4.6 points for a single instrument and 2.9 points pooled.

The measurement has a small bias of its own. On data with no
edge, random entries come in at 49.62% rather than exactly 50%. Signals
carry the same offset, so it cancels when the two are subtracted, and
the difference on that data sits at +0.27 points. It is disclosed here
because it is real.

One decision was made after seeing data. When the validation
missed one of its own criteria by a small margin, the rule for what to
do next was written before the additional runs but after the result
that prompted them. That is a weaker guarantee than deciding everything
in advance, which is what the rest of this study did.

What was and was not measured. The classifier itself: readings,
similarity, neighbor vote, filters, entry when the vote flips. Not
included are the extra entry conditions and exit rules that individual
published indicators layer on top. Outcomes here are scored by a fixed
target-and-stop race, not by any indicator's own exits.

One family of method. Nearest-neighbor classification is one
class of ML indicator. These numbers apply to the method measured.

Loaded history bounds every test. How many bars your chart loads,
not only the calendar range, limits each run.

Direction splits are descriptive only. In a trending sample the
long and short win rates separate by around 20 points while the overall
comparison holds steady. Read the combined rows.

This is a measurement of the past. It reports what happened
under stated rules on stated samples. It does not predict anything.


Disclaimer

This script and its description are provided for educational and
research purposes only. They do not constitute financial, investment,
trading or other professional advice, and they do not recommend buying,
selling or holding any asset. Historical and simulated results do not
guarantee future performance. Trading involves risk, including the
possible loss of capital. You are solely responsible for your trading
and investment decisions and should conduct your own research and,
where appropriate, consult a qualified financial professional.

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.