Skip to content

feat: per-ticker fear-and-greed reading on replay rows - #112

Merged
yanivil merged 2 commits into
mainfrom
feat/backtest-fear-greed
Sep 8, 2026
Merged

yanivil merged 2 commits into
mainfrom
feat/backtest-fear-greed

Conversation

@yanivil

@yanivil yanivil commented Sep 8, 2026 •

Copy link
Copy Markdown
Owner

Description

A per-ticker fear-and-greed reading, recorded as a replay feature under the protocol of docs/wiki/03 and tested over the eleven yearly point-in-time runs from this branch. No rule changes.

  • fear_greed: the equal-weight 0-100 composite of the three price-based oscillators the TradingView community indicators of that name share: RSI 14 (Wilder's smoothing, _rsi), the MACD (12, 26, 9) histogram as its mid-rank percentile within the trailing 250 bars, and Bollinger %B over 20 bars and 2 standard deviations, clipped to 0-100 in the average.
  • Two readings per row: the composite and its components at the scan day (fg_score, fg_rsi, fg_macd_pct, fg_bb_pctb) and the composite at the pattern's last low (fg_base).
  • Buckets in the outcome-by-feature table; wiki 03 "Per-ticker fear and greed, tested"; wiki 04 feature list; changelog.

Results (runs 34267945406 to 34268343900, tuned, 2016 to 2026):

Reading Zone N Hit Mean R Positive years
scan day fear 73 23 % +0.61 6 of 11
scan day neutral 174 40 % +0.45 10 of 11
scan day greed 957 33 % +0.20 10 of 11
scan day extreme greed 427 42 % +0.12 8 of 11
pattern's last low extreme fear 112 35 % +0.30 7 of 11
pattern's last low fear 352 34 % +0.28 8 of 11
pattern's last low neutral 616 39 % +0.24 10 of 11
pattern's last low greed 534 33 % +0.15 8 of 11

The greedier the stock at its breakout, the lower the mean R; bases formed in fear paid about twice what bases formed in greed did. The stretch components carry it: RSI 30-50 at the breakout +0.55 R against +0.12 above 70; Bollinger %B lower half +0.53, upper half +0.26, a close above the upper band +0.04 R on 453 signals, the upper half ahead in 10 of 11 years. Momentum (MACD percentile) does not: +0.18 to +0.26 across buckets. Leaving out above-band breakouts would keep 1179 signals at +0.30 R against 1632 at +0.22, at a cost of 16 R of the ten-year 367. Still a hypothesis under the protocol: the gate has to be replayed as a rule on its own for its drawdown and per-pattern effect.

Verified: ruff clean, 137 tests pass (the composite on accelerating, collapsing, flat and too-short series, Wilder's RSI by hand, %B outside the bands, the cup fixture's readings recomputed independently with the base more fearful than the breakout, rows and buckets rendered); a 30-ticker real-data run renders the buckets.

Related Issues

Related: #99 and #101 (stock-level and market-level context), #108 (drawdowns), #96 (protocol)

Checklist

  • Code follows existing project style and type annotations
  • Threshold changes include a one-line "why" comment in scan.py (no thresholds changed)
  • Offline unit tests pass (python -m pytest -q)
  • Random-walk false-positive rate stays under 5% (detection logic untouched)
  • Documentation updated in docs/wiki/
  • CHANGELOG.md updated

🤖 Generated with Claude Code

yanivil and others added 2 commits September 8, 2026 22:15
fear_greed(): the equal-weight 0-100 composite of RSI 14, the MACD (12, 26, 9)
histogram's mid-rank percentile within the trailing 250 bars and Bollinger %B
(20, 2) that the TradingView community indicators of that name share, with
Wilder's RSI implemented in _rsi. Every replay row carries the composite and
its components at the scan day and the composite at the pattern's last low;
the outcome-by-feature table buckets them (extreme fear to extreme greed, RSI,
%B against the bands, MACD percentile). Recorded to be tested; no rule uses it.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Wiki 03 gains "Per-ticker fear and greed, tested" (runs 34267945406 to
34268343900): the greedier the stock at its breakout the lower the mean R,
bases formed in fear paid about twice bases formed in greed, the stretch
components (RSI, Bollinger %B) carry the effect and momentum does not, and a
breakout closing above its upper Bollinger band ran +0.04 R on 453 signals
with the upper-half bucket ahead in 10 of 11 years. The gate stays a
hypothesis under the protocol. The changelog entry states the outcome.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@yanivil
yanivil merged commit 271d450 into main Sep 8, 2026
4 of 5 checks passed
@yanivil
yanivil deleted the feat/backtest-fear-greed branch September 8, 2026 19:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant