Dev.to WebDev 🛠 Dev 👁 0 📖 4 min read

Van Tharp SQN100 and a Counterfactual -1R Trade Leak Audit in Python (Browser Version, Zero Upload)

All trade data in this post is **Illustrative / Hypothetical. Not investment advice. Here is a trade log that looks fine at first glance: 20 closed trades, 12 winners, a 60% win rate. It lost money anyway. Net result:

All trade data in this post is **Illustrative / Hypothetical. Not investment advice.

Here is a trade log that looks fine at first glance: 20 closed trades, 12 winners, a 60% win rate.

It lost money anyway. Net result: -2.5R.

Nothing about the win rate is wrong. The problem is five trades, and you can find them with about 30 lines of Python.

R-multiples in one paragraph

An R-multiple is a trade's result divided by what you planned to risk on it. If you planned to lose $200 at your stop and made $300, that trade is +1.5R. If you planned to lose $200 and lost $700 because you moved the stop, that trade is -3.5R. Working in R strips out position size and makes trades comparable.

The hypothetical log:

  • 12 winners at +1.5R
  • 3 losers stopped where planned, at -1R
  • 5 losers that blew through the plan, at -3.5R
from statistics import mean, stdev

# Illustrative / Hypothetical: 20 closed trades in R-multiples
trades = [1.5] * 12 + [-1.0] * 3 + [-3.5] * 5

wins = [x for x in trades if x > 0]
print(len(wins) / len(trades))   # 0.6
print(sum(trades))               # -2.5
print(mean(trades))              # -0.125

A 60% win rate with an expectancy of -0.125R per trade. Every trade you take costs you an eighth of your risk unit, on average.

SQN: one number for "is this system any good"

Van Tharp's System Quality Number scores a set of trades by expectancy relative to how noisy the results are:

SQN = sqrt(N) * mean(R) / stdev(R)

Tharp caps N at 100. A common companion is SQN100, which rescales the score to 100 trades so you can compare samples of different sizes. It is just 10 * mean(R) / stdev(R).

def sqn100(r):
    n = min(len(r), 100)
    sqn = (n ** 0.5) * mean(r) / stdev(r)
    return round(sqn, 2), round(10 * mean(r) / stdev(r), 2)

print(sqn100(trades))   # (-0.26, -0.57)

SQN100 of -0.57. On Tharp's rough scale, anything below about 1.6 is a system that is hard to trade, and a negative score means it loses money.

The counterfactual: what if every loss had stopped at -1R?

This is the leak audit. Keep every winner exactly as it was, and replace each loss with the loss you had planned to take:

def cap_losses(r, floor=-1.0):
    return [max(x, floor) for x in r]

capped = cap_losses(trades)
print(sum(capped))       # 10.0
print(mean(capped))      # 0.5
print(sqn100(capped))    # (1.78, 3.98)

Same entries. Same exits on the winners. Same 60% win rate.

-2.5R becomes +10R. Expectancy goes from -0.125R to +0.5R, and SQN100 goes from -0.57 to 3.98.

Where did 12.5R go? Straight into the five trades that went past the stop:

def leaks(r, floor=-1.0):
    tail = [x for x in r if x < floor]
    return len(tail), round(sum(x - floor for x in tail), 2)

print(leaks(trades))     # (5, -12.5)

Five trades, 25% of the sample, gave back 17.5R of the 18R the twelve winners made. That is what a "60% win rate but still losing" account usually looks like up close. The entries are fine. The exits on the losers are not.

What this does not tell you

Before anyone screenshots the +10R:

  • It is hindsight. Real stops slip. A gap through your stop fills where it fills, not at -1R. That is why the browser version below caps losses at -1.1R instead of exactly -1R.
  • 20 trades is a tiny sample. SQN gets unstable below roughly 30 trades. The point here is the mechanism, not the score.
  • Capping losses can change behaviour. Some of those -3.5R trades might have been stopped out and then run in your direction. The counterfactual assumes they would not have been re-entered.

What it does tell you is where to look first. If most of your damage sits in a handful of trades past -1R, your edge problem is an execution problem, and that is a much cheaper problem to fix.

Running it on your own trades without uploading them

Pasting a broker export into a random website is a bad idea, so I built a browser-only version of this check. You paste closed P&L, it estimates your habitual 1R, flags the trades past it, computes SQN100 and shows the capped equity curve next to the raw one. All of the math runs in your browser's JavaScript engine and nothing is sent over the network: Account Leak X-Ray.

Or just use the script below. It is the whole thing.

from statistics import mean, stdev

# Illustrative / Hypothetical: replace with your own closed trades in R
trades = [1.5] * 12 + [-1.0] * 3 + [-3.5] * 5


def summary(r):
    wins = [x for x in r if x > 0]
    return {
        "trades": len(r),
        "win_rate": round(len(wins) / len(r), 2),
        "net_R": round(sum(r), 2),
        "expectancy_R": round(mean(r), 3),
    }


def sqn100(r):
    # Van Tharp SQN caps N at 100; SQN100 rescales to N = 100
    n = min(len(r), 100)
    return round((n ** 0.5) * mean(r) / stdev(r), 2), round(10 * mean(r) / stdev(r), 2)


def cap_losses(r, floor=-1.0):
    # Counterfactual: every loss stopped at the planned -1R
    return [max(x, floor) for x in r]


def leaks(r, floor=-1.0):
    # Trades that went past the planned stop, and what they cost
    tail = [x for x in r if x < floor]
    return len(tail), round(sum(x - floor for x in tail), 2)


print("raw:   ", summary(trades), "SQN, SQN100 =", sqn100(trades))
capped = cap_losses(trades)
print("capped:", summary(capped), "SQN, SQN100 =", sqn100(capped))
print("leak:  ", "%d trades past -1R cost %sR" % leaks(trades))

Output:

raw:    {'trades': 20, 'win_rate': 0.6, 'net_R': -2.5, 'expectancy_R': -0.125} SQN, SQN100 = (-0.26, -0.57)
capped: {'trades': 20, 'win_rate': 0.6, 'net_R': 10.0, 'expectancy_R': 0.5} SQN, SQN100 = (1.78, 3.98)
leak:   5 trades past -1R cost -12.5R

Where do you draw the line for a "tail" loss in your own log: -1.5R, -2R, or right at the planned stop?

📰 Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.