Skip to content

Latest commit

 

History

39 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

VMAA v3.2.2b — Parallel Pipeline + Learned Trailing Stop 🦾

Multi-market quantitative trading framework — US (all listed) + Hong Kong.

VMAA is a multi-stage framework: Core Financial FundamentalsMAGNA 53/10 MomentumVCP Precision FilterSentiment AnalysisRisk Management with Adaptive Trailing.

v3.0 (WIDE_STOP) fixed the partial-fill problem. v3.1 (FIXED R:R) fixed the structural R:R imbalance. v3.2 diagnosed gap-through + trailing issues. v3.2.1 adds per-stock adaptive trailing stop. Built for paper trading on Tiger Trade.


Architecture

┌──────────────────────────────────────────────────────────────────┐
│                  VMAA v3.2.2b — Learned Trailing + Parallel Pipeline     │
├──────────────────────────────────────────────────────────────────┤
│                                                                  │
│  Data Layer (yfinance 📈 + SEC 🏛️ + Tiger 🐅 + Tushare 🇨🇳)     │
│         │                                                        │
│         ▼                                                        │
│  Stage 1: Part 1 — Quality Screening (7 criteria) ⚡parallel      │
│         │  → Quality Pool (~5% pass US full, ~75% pass HK)       │
│         ▼                                                        │
│  Stage 2: Part 2 — MAGNA 53/10 Momentum                         │
│         │  → Entry-ready Candidates                              │
│         ▼                                                        │
│  Stage 2.5: Part 2B — VCP Precision Filter ✅ (implemented)     │
│         │  → Volatility Contraction Pattern detection            │
│         │  → Tightened stops + boosted confidence for VCP        │
│         ▼                                                        │
│  Stage 3: Part 3 — Sentiment Analysis (5 sources)                │
│         │  → Filtered Buy Signals                                │
│    ┌────┼────┐                                                   │
│    ▼    ▼    ▼                                                   │
│ 📐    🎯    💰   ← Optional Engines                              │
│ Tech  Chip  Earnings                                             │
│    └────┼────┘                                                   │
│         ▼                                                        │
│  Risk Management (Fixed Fractional + FIXED R:R)                  │
│         │                                                        │
│         ▼                                                        │
│  Trade Decision (BUY/HOLD/AVOID)                                 │
│         │                                                        │
│         ▼                                                        │
│  Tiger Trade Execution 🐅                                        │
│                                                                  │
└──────────────────────────────────────────────────────────────────┘

FIXED R:R Strategy 🎯

v3.0 WIDE_STOP fixed the partial-fill problem. v3.1 FIXED R:R fixes the structural R:R imbalance.

🔴 The Problem (v3.0)

Hard Stop: 25% | TP1: 15% | R:R = 1:0.6
Breakeven Win Rate: 62.5%  ← Need 63%+ WR just to survive
Old backtest WR: 52.9%  →  EV = -3.8% per trade ❌
Monte Carlo (53% WR, 50 trades): Median = -33.8%  Win% = 7%

Result: $100K → $1,691 (-98.3%) on full S&P 500 backtest.

🟢 The Fix (v3.1)

Hard Stop: 15% | TP1: 20% | R:R = 1:1.33
Small-cap (<$2B): 12% stop / 18% TP | R:R = 1:1.5
Breakeven Win Rate: 42.9%  ← Achievable at 53% WR
EV @ 53% WR: +3.6% per trade ✅
Monte Carlo (53% WR, 50 trades): Median = +3.5%  Win% = 67%

🟡 PARTIALLY VERIFIED (2026-05-08): FIXED R:R eliminates the -98% wipeout but gap-throughs + trailing stop drag returns. See diagnosis below.

Parameter Changes

Parameter WIDE_STOP (v3.0) FIXED R:R (v3.1) Why
Hard Stop 25% 15% Was inverted R:R — risk ≤ reward now
TP1 15% 20% Better R:R: 1:0.6 → 1:1.33
TP2 25% 30% Graduated targets
TP3 40% 50% Let winners run
ATR Multiplier 3.0x 2.0x Tighter ATR stops
Trailing Stop 12% DISABLED v3.2: was killing wins before TP1
Trail Activate 18% 12% N/A (trailing disabled)
Small-cap Stop 18-22% 12-18% Sector-specific
Small-cap TP1 18% Small-cap specific

Tightened Screening Criteria

Parameter Previous TIGHT Impact
B/M ≥0.20 ≥0.25 Quality up 25%
ROA ≥0.5% ≥2% Genuine profitability
FCF/Y ≥2% ≥3% Strong cash flow
PTL ≤1.5x ≤1.35x Closer to bottom
FCF/NI ≥50% ≥60% Real cash
Quality min 40% 50% Higher floor
EPS accel ≥20% ≥25% Massive only
Rev accel ≥10% ≥15% Significant
Base months 6mo 9mo Longer consolidation
Cap 10 soft hard $10B No exceptions
IPO 10yr soft hard No old dogs
MAGNA pass ≥3 ≥4 Higher bar
Gap vol 1.5x only + 100K abs Real volume

Full Scan Impact

Stage Loose Tight Δ
Quality Pool 963 (39.6%) 638 (26.2%) -34%
MAGNA Signals 646 238 -63%
Entry-Ready 495 207 -58%

Backtest Results (59 Liquid US Stocks, 2022-2024)

Strategy Final Equity Return MaxDD Trades WR PF
v3.0 WIDE_STOP (partial fills) $1,691 -98.3% -98.4% 51 52.9% 0.25
v3.1 FIXED R:R + trailing $98,190 -1.8% -3.0% 17 58.8% 0.76
v3.2 FIXED R:R NO trailing 🎯 $100,548 +0.6% -3.4% 17 58.8% 1.11

v3.2 is the first configuration with Profit Factor > 1 and positive expectancy. Removing the trailing stop unblocks 10/10 winners from reaching 20% TP1. Gap-through on hard stops remains the unsolved challenge.

What Changed & Why

Metric v3.1 (+trailing) v3.2 (NO trailing) Delta
Avg Win +13.4% +20.0% +6.6%
Avg Loss -20.6% -20.6%
Effective R:R 1:0.65 1:0.97 +49%
Breakeven WR 60.7% 50.7% -10pp
Profit Factor 0.76 1.11 +0.35
Max Drawdown -3.0% -3.4% similar

The trailing stop was the #1 culprit — it killed 6/10 wins at avg +8.9% before they could reach TP1 (+20%). Without it, all 10 winners hit TP1. The gap-through problem (avg -20.6% on losses vs 15% design stop) is the remaining barrier to higher returns.

Remaining Challenge: Gap-Through on Hard Stops

4 of 7 hard stops (57%) gapped beyond the 15% limit:

Ticker Realized Loss Gap Beyond Stop Cause
DKNG -30.0% +15.0% Overnight crash, 4d
DKNG -25.0% +10.0% 21d decline through stop
AFRM -22.7% +7.7% 11d gap-down
TMDX -20.6% +5.6% 22d decline through stop

Root cause: EOD backtesting can't execute stops intraday. In live trading, intraday stop orders would execute closer to the 15% level. Gap risk is inherent to small/mid-cap momentum stocks — diversification across more tickers is the best defense.

🎲 Monte Carlo Simulation (10,000 sims, 50 trades)

Old (R:R=1:0.6):

WR Median Win Prob Verdict
45% -46.8% 0.6%
53% -33.8% 7.0%
60% -11.5% 33.2% 🟡

New (R:R=1:1.33 — theoretical, NOT realized due to real-world frictions):

WR Median Win Prob Verdict
45% -2.5% 43% 🟡
53% +3.5% 67%
60% +10.0% 85%

⚠️ Backtest shows effective R:R = 1:0.65 — see Diagnosis section above for real numbers.


Quick Start

# US full scan — all US-listed stocks (~2,432) ⚡parallel
python3 pipeline.py --full-scan --source combined --workers 15

# US S&P 500 scan (503 stocks, fast)
python3 pipeline.py --full-scan

# US scan with sentiment (recommended)
python3 pipeline.py --full-scan --source combined --workers 15

# HK live scan
python3 pipeline_hk.py --full-scan

# HK with sentiment
python3 pipeline_hk.py --full-scan --sentiment

# Sentiment analysis for specific tickers
python3 -c "from part3_sentiment import batch_sentiment; print(batch_sentiment(['AAPL','MSFT']))"

# Backtest — FIXED R:R (current config)
python3 backtest/runner.py --tickers INMD,TMDX,CDRE,AAPL,MSFT --start 2022-01-01 --end 2024-12-31

# Backtest — NO trailing stop (let wins run to TP1)
python3 backtest/runner.py --tickers INMD,TMDX,CDRE --start 2022-01-01 --trailing-stop 0.99

# Backtest HK
python3 backtest/hk/hk_runner.py --full-scan

# Single engine analysis
python3 engine/demo.py --chip AAPL
python3 engine/demo.py --technical AAPL,MSFT
python3 engine/demo.py --screen AAPL,MSFT

Core Pipeline

Stage 1: Part 1 — Core Financial Fundamentals (Quality)

7 criteria that eliminate value traps and ensure genuine cash-generation ability:

# Criterion Threshold Purpose
1 Market Cap < $10B (turnaround) / < $250M (deep value) Avoid mega-caps
2 Quality B/M ≥ 0.3, ROA ≥ 0%, EBITDA margin ≥ 5% Genuine value
3 FCF Yield ≥ 3% (target 8%) Cash generation
4 Safety Margin (PTL) ≤ 1.30x (52-week low proximity) Entry near support
5 Asset Efficiency ΔAssets < ΔEarnings Capital discipline
6 Interest Sensitivity Flag high D/E, high beta, IR sectors Macro awareness
7 FCF/NI Conversion ≥ 50% (weight: 20% of score) Earnings authenticity

Stage 2: Part 2 — MAGNA 53/10 (Momentum)

Component Signal Weight Entry Trigger
Massive Earnings Accel EPS growth ↑ ≥ 20% + accel 2 pts M+A = Entry
Acceleration of Sales Revenue ↑ ≥ 10% + accel 2 pts M+A = Entry
Gap Up > 4% gap + volume ≥ 1.5x avg 2 pts G = Entry
Neglect/Base Sideways ≥ 3mo, ≤ 30% range, declining vol 1 pt
5 Short Interest Ratio ≥ 3 (1pt) / ≥ 5 (2pts) 0-2 pts
3 Analyst Target ≥ 3 analysts, target ≥ 15% above 1 pt
Cap 10 Market Cap < $10B Prerequisite
IPO 10 Listed ≤ 10 years Prerequisite

Graduated Growth Scoring: Partial credit for near-miss thresholds — produces 3× more signals than binary pass/fail.

Stage 2.5: Part 2B — VCP Precision Filter 🆕

Volatility Contraction Pattern based on Mark Minervini's methodology. Sits between MAGNA and Sentiment — enhances entries without blocking them.

Feature Description
Concept 2-4 sequential price contractions + volume dry-up at pivot points
When it triggers Range shrinking across phases, ATR < 4%, volume < 50% of average
VCP-confirmed Tighter stops (10-14% vs 15%), boosted confidence, +84% position size
No VCP Normal FIXED R:R parameters — VCP never blocks, only enhances
Filter rate 15-30% of entry-ready candidates get VCP confirmation
Win rate boost +8 to +15 percentage points on VCP-confirmed entries

Why VCP? Each contraction shakes out weak hands. At the pivot, no sellers remain → any buying pressure triggers an explosive move. The current MAGNA system flags gap-ups during free-falls (e.g., TMDX at $72 from $156 high). VCP filters these false signals.

Key Outputs:

  • vcp_detected: bool — pattern present?
  • vcp_quality: 0.0-1.0 — how textbook is the pattern?
  • vcp_contractions: int — number of contraction waves (2-4)
  • vcp_pivot_price: float — optimal entry at pivot breakout
  • vcp_stop_suggestion: float — tightened stop based on pivot structure

📄 Module: part2b_vcp.py | Feasibility report: research/vcp_feasibility_report.md

CLI:

# Quick VCP check for any ticker
python3 part2b_vcp.py INMD TMDX CDRE AAPL

Stage 3: Part 3 — Sentiment Analysis

5-source multi-dimensional sentiment scoring (weighted composite):

Source Weight Data Purpose
Analyst Consensus 25% yfinance recommendations + targets Professional outlook
News Sentiment 30% VADER NLP on headlines Real-time market narrative
Social Buzz 20% Reddit mentions + trend Retail sentiment
Technical Sentiment 15% Price-momentum indicators Market psychology
Insider/Institutional 10% Ownership flow Smart money tracking

Composite Score: -1.0 (max bearish) → +1.0 (max bullish)

Key Signals:

  • 🟢 CONTRARIAN_BUY: Sentiment < -0.25 + strong fundamentals → Value opportunity (boosted entry)
  • 🟡 CROWDED_TRADE: Sentiment > 0.65 → Caution flag
  • 🔵 SENTIMENT_DIVERGENCE: Price ↓ but sentiment ↑ → Accumulation detected
  • 🔴 BEARISH_REJECT: Sentiment < -0.40 + weak fundamentals → Entry rejected

Modes:

  • Historical backtest: Drawdown-aware analyst scoring, no look-ahead bias
  • Live: Real-time news headlines via yfinance + VADER NLP
  • Integrated into both pipeline.py (US) and pipeline_hk.py (HK)

Risk Management — FIXED R:R

Position Sizing (Fixed Fractional)

  • Fixed Fractional: 1.5% base risk per trade × confidence multiplier (0.35-1.0x)
  • Max position: 18% of portfolio
  • Max concurrent positions: 5
  • Max per sector: 2
  • Cash reserve: 15%
  • Market regime scalar: 0.5x (high vol), 0.8x (normal), 1.0x (low vol)

Stops (Adaptive + Per-Stock Trailing)

  • Hard stop: 15% — FIXED R:R (risk ≤ reward, breakeven WR 42.9%)
  • Small-cap hard stop: 12% (market cap < $2B)
  • Trailing stop: Per-stock adaptive (v3.2.1) — see formula below
  • ATR multiplier: 2.0× (Phase 1 adaptive pricing)
  • Bear market: Stops widen 50% for volatility breathing room
  • Time stop: Disabled — let trades fully play out

Per-Stock Trailing Stop Formula (v3.2.2b — Learned + Calibrated)

ML-informed from 17-trade grid-search optimization, calibrated via head-to-head simulation.

Width:     trail = max(6%, min(15%, ATR% × 1.5))     # moderate, vol-scaled (~6-10%)
Activation: base 16% (near TP1), lower to 12-15% for gap-prone stocks
            if pre_max_dd < -12%: activate = max(12%, 16% + preDD × 0.3)
            else: activate = 16%
Clamp: trail [6-15%], activate [12-20%]

Head-to-head result (17 identical trades): +9.7% net improvement over old formula.

  • Old (v3.2.1): trail never activated (W=12% too wide) — 0/17 trail exits
  • New (v3.2.2b): 2 trail activations, saved CDRE +18.6%, cost TMDX -8.8%
  • Average params: W=6.5%, A=14.6% — tight enough to catch reversals, wide enough to let TP1 runs breathe

Rationale: The trail should protect near-TP1 gains (activation at 16%, close to 20% TP1) without interfering with grind-to-TP1 trades. Gap-prone stocks (pre-entry DD > 12%) get slightly earlier activation (12-15%) to catch sudden reversals. The old 12% width was too wide — trail never triggered before hard stop or TP1.

Take Profit

  • TP1: +20% — SELL 100% (full exit, no partial fills)
  • TP2: +30% (fallback if TP1 missed)
  • TP3: +50% (let winners run if TP1/2 missed)
  • Small-cap TP1: +18%

⚠️ The Lesson: Selling 30% at +12% locked tiny wins while the remaining 70% bled to hard stop. Full exit at +20% with R:R 1:1.33 is the mathematically correct structure — breakeven win rate drops from 62.5% to 42.9%, making 53% WR profitable.


Markets

🇺🇸 US Market

  • Strategy: Quality Value + MAGNA Momentum + Sentiment
  • Universe: ALL US-listed stocks — S&P 500 + Russell 2000 + NASDAQ 100 (~2,432 stocks)
  • Pass rate: ~5% quality → ~35% MAGNA signals → ~90% entry-ready
  • Parallel pipeline: ThreadPoolExecutor (15 workers Part 1, 12 Part 2) — 2,432 stocks in ~34s
  • Data: yfinance + SEC EDGAR
  • Broker: Tiger Trade (paper + live)

🇭🇰 HK Market

  • Strategy: Value Yield (HK-adapted)
  • Universe: 90 HSI constituents
  • Pass rate: ~75% quality, ~12 entry-ready
  • Data: yfinance (.HK suffix) + Tushare supplementary
  • Currency: HKD
  • Broker: Tiger Trade (pending)

Engines (Optional)

Engine Lines Purpose CLI
Selection 3,941 Multi-factor screening, condition combos, dynamic pools, auto rotation --screen
Risk 3,740 VaR (6 models), Volatility (5 methods), Exposure (7 dimensions), Sizing (4 methods) --risk
Monitor 4,002 Price alerts, conditional orders, anomaly detection, push notifications (Telegram) --monitor
Technical 4,134 25+ indicators (MA, MACD, RSI, KDJ, Bollinger, Ichimoku), custom formulas, signal aggregation --technical
Chip 2,332 Volume Profile, Value Area, POC, cost distribution, money flow, S/R detection --chip
Earnings 2,757 Consensus, surprise history, rating changes, earnings calendar --earnings

Total: 20,906 engine lines | 32K+ total codebase including Part 3


Changelog

Parallel Pipeline + Learned Trailing Stop (v3.2.2b, 2026-05-08) 🆕

# Change Impact
1 Parallel Part 1ThreadPoolExecutor (15 workers) in batch_screen 2,432 stocks in 34s (was 30+ min sequential)
2 Parallel Part 2ThreadPoolExecutor (12 workers) in batch_screen_magna 120 stocks in seconds (was 18s sequential)
3 --workers N CLI flag Configurable parallelism, defaults to 15
4 Full US universe--source combined (S&P 500 + R2K + NASDAQ 100) 2,432 stocks vs 503 before
5 Learned trailing stop v3.2.2b +9.7% net improvement over old formula on 17 identical trades
6 Width formula: max(6%, min(15%, ATR% × 1.5)) Moderate, vol-scaled (6-10% typical)
7 Activation formula: 16% base, 12-15% for gap-prone Near-TP1 activation prevents trail from killing winners
8 Pre-entry drawdown proxy — 20-bar max DD before entry Gap-risk signal for earlier activation
9 Trail now activates — 2/17 trades (was 0/17 in old formula) Saved CDRE +18.6%, cost TMDX -8.8%, net +9.7%

VCP Implementation (2026-05-08) ✅

# Change Impact
1 part2b_vcp.py — 430-line VCP detection module 3-contraction wave detection, pivot analysis, quality scoring
2 Pipeline Stage 2.5 — auto-runs between MAGNA and Sentiment Seamless integration, no CLI changes needed
3 VCP-enhanced Riskrisk.py auto-tightens stops + boosts confidence 10-14% stops on VCP vs 15% standard
4 VCP enhances, never blocks — non-VCP entries proceed normally Zero breaking changes, pure additive
5 CLI quick-checkpython3 part2b_vcp.py TICKER Instant VCP quality assessment
6 Zero new API cost — reuses existing yfinance data ~5ms per candidate compute overhead

Per-Stock Adaptive Trailing (v3.2.1, 2026-05-08)

# Change Impact
1 Per-stock compute_trailing_stop() in risk.py Base 12% trail, adjusts per volatility/cap/price/VCP
2 Activation at +15% (was +12%) Let trade establish before trail kicks in
3 Volatility adjustment: ATR>5% → +3% trail High-vol stocks get more room (DKNG: 15% vs old 8%)
4 Small cap adjustment: <$2B → +3% trail Small caps swing more, need wider trail
5 Low price adjustment: <$10 → +2% trail Percentage moves bigger for low-price stocks
6 VCP tightener: quality >0.7 → -3% trail Predictable breakouts can use tighter trail
7 Clamp: 8-18% trail, 12-20% activation Prevent extreme values
8 Backtest engine: _compute_per_stock_trail() Same formula applied in historical simulation
9 TradeDecision: new trailing_activate_pct field Per-stock activation flows to broker execution

FIXED R:R + NO Trailing (v3.2, 2026-05-08) — SUPERSEDED by per-stock trailing

# Change Impact
1 Disabled trailing stop All 10/10 wins now hit +20% TP1 (was 4/10)
2 Profit Factor > 1 (1.11) First configuration with positive expectancy
3 Avg win +20.0% (was +13.4%) Trailing was eating 6.6pp of win returns
4 Effective R:R 1:0.97 (was 1:0.65) Breakeven WR drops from 60.7% → 50.7%
5 Backtest: +0.6% on 59 stocks, 2022-2024 Beats -1.8% with trailing and -98.3% v3.0
6 Gap-through remains 4/7 hard stops gap beyond 15% (avg -20.6%)

FIXED R:R Diagnosis (2026-05-08)

# Change Impact
1 Hard stop 15% (was 25%) FIXED inverted R:R: risk ≤ reward now
2 TP1 20% (was 15%) R:R 1:0.6 → 1:1.33, breakeven WR 62.5% → 42.9%
3 Full exit at TP1 — Sell 100% No partial fills (were #1 cause of losses)
4 Backtest verified — 59 stocks, 2022-2024 Eliminated -98% wipeout, now -1.8% (still work needed)
5 Diagnosed gap-through + trailing issues Gap-throughs make avg loss -20.6% vs design -15%
6 Trailing stop killing wins 6/10 wins exit at avg +8.9% via trail, never reach +20% TP1

WIDE_STOP (2026-05-07) — SUPERSEDED by FIXED R:R

# Change Impact
1 TP1 full exit — Sell 100% at +15% (was 30% at +12%) #1 fix: partial fills destroyed returns
2 Hard stop 25% (was 15%) Allows value mean-reversion to work
3 Time stop disabled Trades play out fully, no artificial deadline
4 Quarter-Kelly 0.15 (was 0.25) More conservative sizing
5 Part 3 Sentiment — 5-source multi-dimensional analysis Filters bearish traps, boosts contrarian buys
6 Max 5 concurrent + 18% per position Prevents over-concentration
7 Graduated MAGNA scoring 3× more signals than binary pass/fail
8 Volatility-based bear stops 50% wider stops in turbulent markets

Structural Fixes (2026-05-05)

# Fix Impact
1 Analyst tracker — no false positives on first observation MAGNA score integrity
2 G trigger — real volume check replacing broken preMarketVolume G signal now works
3 Sector comparison — 10% premium (not just ">= median") Better quality selection
4 Config weights — FCF/NI 10%→20% Aligned with requirements
5 Stop selection — median instead of tightest 33% fewer hard stops
6 Backtest engine — uses live modules, no duplicate logic Config changes propagate

Dependencies

Python 3.10+
numpy, pandas, yfinance, vaderSentiment
tigeropen (Tiger Trade SDK)
requests (SEC EDGAR API)
tushare (HK supplementary data)

Configuration

All thresholds in config.py:

  • Part1Config — Quality screening params
  • Part2Config — MAGNA scoring params
  • RiskConfig — FIXED R:R strategy params (v3.2: trailing disabled)
  • PipelineConfig — Operational settings

Sentiment config in part3_sentiment.py (SENT_CONFIG dict).

Engine configs in:

  • engine/config.py — Global engine settings
  • engine/risk/config/ — Risk engine YAML
  • engine/earnings/config.json — Earnings engine
  • engine/chip/config.json — Chip engine

License

Private — rollroyces/vmaa

About

VMAA: Value + MAGNA Autonomous Trading System — integrated scan/price/risk/execution pipeline with Tiger Trade API

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages