Skip to content

Latest commit

 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 

Repository files navigation

Trading Signal Visualizations

Chart gallery and technical writeup for two independent quantitative-trading research projects by James Sawyer (jsawyerdev):

  • theil-sen-chop-lab — a robust-regression trend/chop classifier for hourly commodity price windows.
  • sr-breakout-signal — a support/resistance breakout early-warning signal built on change-point segmentation and Theil-Sen line fitting.

Both projects share a lineage: they are standalone, runnable extractions of statistics used inside a larger multi-agent commodities trading system (consensus-trading-stack), built and backtested by James Sawyer against ~3 years of real Dukascopy hourly OHLC data across Gold, Silver, Oil (WTI Crude), Oil (Brent), and Natural Gas.

This repo exists purely to host and explain the generated chart output of both projects — the code, tests, and full statistical methodology live in their own repositories.


Table of contents


theil-sen-chop-lab

A robust trend-and-chop measurement tool, by James Sawyer, that answers — for any window of hourly commodity candles — which way price trended, how choppy it was, and how confidently either statement can be made.

How the classifier works

  1. Fit a robust trend line. A Theil-Sen estimator (scipy.stats.theilslopes, method="joint") fits price = slope * bar_index + intercept through each bar's typical price (high + low) / 2. The slope is the median of all pairwise slopes between points, giving it a ~29% breakdown point — a single flash-crash wick barely moves the line, unlike an ordinary least-squares fit that gets dragged toward outliers.
  2. Count crossings, with a deadband. Each close's residual against the line is tested against a deadband set at 25% of the window's own median absolute residual, so the statistic is scale-free across instruments with wildly different price levels. A bar inside the deadband carries the previous side forward instead of splitting a run.
  3. Count close-to-close reversals. A magnitude-blind swing_density statistic counts sign changes between consecutive closes, catching whipsaw behavior that a magnitude-weighted path-efficiency score would hide.
  4. Label the window. Direction (rising / falling / flat) comes from the Theil-Sen slope against a noise band; chop band (trending / mixed / choppy) comes from the reversal count, calibrated so a 20-bar window's random-walk null (9 reversals) sits inside the mixed band rather than at an arbitrary edge.

On the source system's development replay, the < 6 reversal band scored +0.0522 R/trade, 6–10 scored +0.0013, and > 10 scored −0.0473 — monotonic across all three, which is the only threshold in this tool that is asserted as measured rather than descriptive.

Reading a window figure

Example window: three-panel Theil-Sen chop/trend breakdown

Every per-window figure has three stacked panels:

  • Top — closes (black), the typical price used for the fit (grey dashed), the Theil-Sen line (blue) with its shaded deadband, points colored green above / orange below the line, and dotted purple verticals marking confirmed crossings.
  • Middle — each close's residual against the line, with the deadband shaded — the panel that makes a crossing visually unambiguous.
  • Bottom — close-to-close changes, with an x marking every reversal the flip count registered.

Sample summary distribution

Distribution of chop and trend statistics across a full random sample of windows

Aggregate distributions of both chop statistics (crossing-based and reversal-based) across an entire random sample of 20-bar windows drawn from the Dukascopy hourly archive (BRENT, CRUDE, GOLD, NATGAS, SILVER; 2023-07 to 2026-07).

Individual sampled windows

Eight windows sampled and rendered from the same run, spanning every direction/chop combination the labeling scheme produces:

Figure Direction Chop band Instrument Window end
Falling, trending window on Crude Oil falling trending Oil (WTI Crude) 2026-05-27 08:00
Flat, mixed window on Gold flat mixed Gold 2025-07-07 17:00
Falling, mixed window on Natural Gas falling mixed Natural Gas 2024-11-01 05:00
Falling, mixed window on Brent falling mixed Oil (Brent) 2025-03-04 19:00
Rising, mixed window on Gold rising mixed Gold 2023-12-06 07:00
Flat, choppy window on Natural Gas flat choppy Natural Gas 2026-06-17 07:00
Falling, choppy window on Gold falling choppy Gold 2023-07-17 05:00
Flat, choppy window on Natural Gas flat choppy Natural Gas 2025-11-06 21:00

Each filename encodes <index>_<direction>-<chop band>_<symbol>_<window end timestamp>, generated deterministically from a fixed random seed so every run is reproducible.


sr-breakout-signal

An early-warning support/resistance breakout signal, by James Sawyer, built on change-point segmentation and robust line fitting, backtested with full statistical rigor (significance testing, real cost modeling, regime conditioning, and a 250-cell parameter sweep) rather than reported on raw hit rate alone.

How the signal works

  1. Run PELT change-point detection (ruptures, L2 cost) separately on a window's low prices and high prices.
  2. Take each detected segment's minimum (low series) or maximum (high series) as a candidate support/resistance pivot, keeping only pivots clustered near that side's own price extreme.
  3. Fit a Theil-Sen robust line through each side's surviving pivots.
  4. Compare pivot counts between sides. If price sits within a volatility-scaled tolerance band ("a whisper") of the side with more pivots, fire a directional bias. Two mirror-image theses assign that bias oppositely:
    • Coiled spring (shipping default): the dominant side is failing to break — dominant support → likely_long, dominant resistance → likely_short.
    • Persistent pressure (alternative): repeated tests are the pressure — dominant resistance → likely_long, dominant support → likely_short.
  5. Confidence is the pivot-count asymmetry between the two sides, scaled 0.0–1.0.

Bottom line from the full investigation: across 60+ symbol/side/thesis/regime/ parameter combinations tested, exactly one result is both statistically significant after Bonferroni correction (p = 0.004) and net-profitable after real recorded spread (+23.4 bps/trade) — Natural Gas, persistent-pressure thesis, long side, window = 20h / horizon = 8h. Everything else is indistinguishable from chance or unprofitable after costs.

Scenario gallery

Six-panel synthetic ground-truth scenario gallery: support/resistance defended consolidations, breakouts, broken levels, and a random-walk null control

Six small-multiple panels built from deterministic synthetic scenarios: support- and resistance-defended consolidations, each paired with a correctly predicted breakout, a broken-level case, and a random-walk null control. All four directional panels fire the correct bias; the null control and broken-level case correctly abstain.

Backtest edge and significance

Per-symbol, per-side hit rate edge over base rate with 95% Wilson confidence intervals and binomial significance

Edge over the market base rate for every symbol/side pair, backtested against ~3 years of real hourly OHLC (window = 20h, horizon = 8h, non-overlapping trades, ≥100 signals per cell), with 95% Wilson confidence intervals. 6 of 10 combinations show a positive raw edge, but only 1 of 10 — Natural Gas short, at p = 0.004 — is statistically distinguishable from chance, and that one significant result is worse than chance.

Backtest equity curves

Cumulative signal PnL over the trade sequence, per symbol

Cumulative signal PnL over the trade sequence for each instrument. Oil trends positive and Gold/Natural Gas trend negative over the full sample, though the per-cell significance test above is the more honest read than the raw curve shape.

Confidence calibration

Hit rate split into three confidence-score groups, testing whether the signal's own confidence score predicts accuracy

Tests whether the signal's own confidence score (pivot-count asymmetry) predicts accuracy. Pooled point-biserial correlation between confidence and hit: r = +0.042 — essentially zero. Hit rates across confidence terciles are statistically indistinguishable (47–52%, heavily overlapping confidence intervals).

V1 vs V3 design comparison

Side-by-side backtest comparison between the original V1 design and the simplified V3 rule

The original, more elaborate design (legacy_v1: bar-by-bar touch/break classification, zero-break eligibility, a separate regime trim) run through the identical backtest harness as the current simplified whisper-proximity rule (V3). V1's looser eligibility fires 5–8x more often (639–1024 signals vs. 101–251 per cell), giving lower-variance, near-zero edge estimates; V3's smaller sample lets both bigger wins and bigger losses through. Neither version is a reliable edge source overall.

Cost analysis

Net PnL per trade after real recorded bid-ask spread, per symbol and side

Applies each instrument's real recorded average spread (not an assumed constant) as a round-trip cost. Only 3 of 10 symbol/side combinations are net-profitable after cost, and none of those three were even nominally significant in the hit-rate test — the one statistically significant cell (Natural Gas short) is decisively unprofitable at −77.2 bps/trade.

Parameter sweep heatmap

250-cell window x horizon parameter sweep heatmap across all symbols and sides

A 5×5 grid of window (10–40h) × horizon (4–24h) swept across all 5 symbols and both sides (250 cells). 11 of 200 sufficiently-sampled cells are nominally significant at p < 0.05 — almost exactly the ~10 expected from testing 200 cells by chance alone — and 0 survive Bonferroni correction. The one standout pattern, Natural Gas short at window = 20h, is consistently negative across horizons 4/8/12/16, which is what motivated testing the inverted (persistent-pressure) thesis below.

Regime analysis

Signal hit rate by Kaufman Efficiency Ratio regime tertile against the regime-conditional base rate

Splits signals by Kaufman's Efficiency Ratio (a standard trend/chop measure) into ranging/mixed/trending terciles, compared against each tertile's own conditional base rate rather than the unconditional market rate. No cell is significant at this sample size, but the direction is consistent with the signal's range-bound premise: hit rate declines from ranging toward trending on both sides.

Thesis comparison

Coiled-spring vs. persistent-pressure thesis compared on identical fired events

The two mirror-image theses run through identical significance and cost analysis on the exact same fired events. This is where the project's one surviving result appears: Natural Gas, persistent-pressure, long — p = 0.004, net +23.4 bps/trade after real spread. Flagged in the source project as a narrow lead worth out-of-sample validation, not a proven edge (n = 101 signals, found after testing ~20 independent buckets plus 200 sweep cells).


Credits

All research, code, statistical methodology, and chart generation for both projects were authored by James Sawyer (github.com/jsawyerdev). This repository packages and documents the image output only; see the source projects for full implementations, tests, and methodology writeups.

License / usage

Images and this writeup are shared for portfolio and educational reference. Nothing in this repository is investment advice — every backtest above is explicit that most tested signal/symbol combinations are statistically indistinguishable from chance or unprofitable after real trading costs.

About

Commodity trading research chart gallery explaining Theil-Sen trend detection, support/resistance breakouts, change-point analysis, and backtest results.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors