Skip to content

Repository files navigation

VisionQuant

Multi-Strategy AI Trading Intelligence Engine — a research prototype that treats chart images as first-class input, reconstructs price structure via computer vision, and fuses multiple orthogonal trading strategies (rule-based + ML) into a single calibrated signal.

⚠️ Research/educational prototype. Not investment advice. Alpha decays; no system here is claimed to be profitable in live markets.


Why image-first?

Most quant pipelines start with clean OHLCV feeds from an exchange. This project deliberately inverts that: the input is the same picture a human trader sees. That forces the system to (a) extract structure from raw pixels and (b) think about uncertainty — both in the extraction and in the prediction. The fusion engine then behaves much like a multi-analyst desk combining independent opinions.


Architecture

 ┌──────────────┐   ┌──────────────────┐   ┌──────────────────┐
 │  Data Layer  │→ │   Vision Layer   │→ │ Feature Reconstr.│
 │ (img/screen) │   │  OpenCV + Hough  │   │  pixels → OHLC   │
 └──────────────┘   └──────────────────┘   └──────────────────┘
                                                  │
                                                  ▼
                        ┌──────────────────────────────────────┐
                        │       Strategy Engine (N modules)    │
                        │  patterns • structure • S/R • break- │
                        │  out • candles • indicators • vol •  │
                        │  ML-CNN                              │
                        └──────────────────────────────────────┘
                                                  │
                                                  ▼
                                ┌──────────────────────────┐
                                │   Signal Fusion Engine   │
                                │  weighted · conflict ·   │
                                │  agreement · vol-adjust  │
                                └──────────────────────────┘
                                                  │
                                                  ▼
                        ┌───────────────┐  ┌───────────────┐
                        │  Streamlit UI │  │ Backtest / RT │
                        └───────────────┘  └───────────────┘

Directory layout

VisionQuant/
├── main.py                  # CLI (analyse / gen-dataset / train-cnn)
├── app.py                   # launcher for Streamlit
├── config.yaml              # all tunables
├── requirements.txt
├── src/
│   ├── data/                # loaders, synthetic generator, screen capture
│   ├── vision/              # preprocessing, candle & trendline detection
│   ├── features/            # OHLC reconstruction + technical indicators
│   ├── strategies/          # rule-based modules
│   ├── ml/                  # CNN + meta ensemble
│   ├── fusion/              # orchestrator + fusion + confidence
│   ├── backtest/            # engine + walk-forward
│   ├── realtime/            # async live engine
│   ├── risk/                # risk manager + circuit breakers
│   ├── analysis/            # Monte Carlo + multi-timeframe + NL explainer
│   ├── viz/                 # overlay + plotly helpers
│   └── utils/               # logging, types, config, cache
├── streamlit_app/
│   ├── main.py              # home page
│   └── pages/
│       ├── 1_Chart_Analyzer.py
│       ├── 2_Live_Trading.py
│       ├── 3_Strategy_Dashboard.py
│       ├── 4_Backtesting.py
│       └── 5_Risk_and_Robustness.py
├── notebooks/               # Jupyter research notebooks
├── scripts/                 # demo, profiler, meta-training
└── tests/                   # 32 unit + smoke tests

Strategies

Module Signals from Weight
patterns H&S, Double/Triple Top/Bottom, Triangles, Wedges, Flags 1.0
market_structure HH/HL/LH/LL, BOS, CHOCH 1.4
support_resistance Clustered swing zones + trendlines 1.2
breakout Range, volatility compression, retest, fake-break 1.3
candlestick Engulfing, doji, pin bars, stars 0.8
indicators EMA cross, RSI, MACD hist, Bollinger 1.0
volatility ATR regime (compression / expansion) 0.6
ml_cnn Small CNN on the raw chart image 1.5

Each strategy returns {signal, confidence, metadata, overlays}. The Fusion Engine weights them, then applies:

  • Agreement boost when the majority align,
  • Conflict penalty when bulls and bears tie,
  • Volatility dampener to shrink confidence in chaotic regimes,
  • A final threshold → BUY / SELL / HOLD.

Deploying on Streamlit Cloud

VisionQuant is a fully Streamlit-native app — python app.py or pointing Streamlit Cloud at streamlit_app/main.py is all you need. See DEPLOY_STREAMLIT.md for a step-by-step guide. Features that require platform-specific libraries (torch, mss) degrade gracefully: the UI hides them and the rest of the pipeline keeps working.

The 9 Streamlit pages

  1. Chart Analyzer — upload an image or generate a synthetic chart; full overlays, OHLC plot, strategy table, NL explanation.
  2. Data Import — load CSV / TSV / Parquet OHLC; run the pipeline on numerical data directly.
  3. Strategy Dashboard — per-strategy cards, signed contribution bar, metadata tabs.
  4. Backtesting — classic or risk-aware backtest on synthetic / imported OHLC.
  5. Walk-Forward — expanding-window walk-forward with in-sample fusion tuning per fold.
  6. Risk & Robustness — multi-timeframe, Monte Carlo and NL explanation.
  7. Regime Monitor — trend/range/volatile classification with regime-shaded price chart.
  8. Live Trading — screen capture (desktop) or auto-refreshing image upload (cloud-safe).
  9. Training Lab — generate synthetic datasets, train CNN, train meta-ensemble.

Quickstart

# install
pip install -r requirements.txt

# one-shot demo (no dataset needed)
python scripts/demo.py --save-dir out

# synthetic dataset + CNN training (optional, CPU-friendly)
python main.py gen-dataset --n 600 --out data/synthetic
python main.py train-cnn --data data/synthetic --epochs 8

# meta-ensemble training
python scripts/train_meta.py --n 200 --out models/meta.pkl

# profiling & benchmarking
python scripts/profile_pipeline.py --n 30
python scripts/evaluate.py --n 100

# inference — image, CSV, multi-timeframe, regime, explanation
python main.py analyse chart.png --save-overlay out.png
python main.py analyse-csv prices.csv
python main.py mtf --csv prices.csv --factors 1 3 6
python main.py regime --csv prices.csv
python main.py explain --csv prices.csv

# backtesting
python main.py backtest --csv prices.csv --risk-aware
python main.py mc --mean 5 --sigma 40 --n 100 --runs 2000

# UI + tests + notebook
python app.py            # 5-page Streamlit dashboard
pytest -q tests/         # 42 tests
jupyter lab notebooks/01_pipeline_walkthrough.ipynb

Signal fusion — maths

For i ∈ strategies with weight wᵢ, confidence cᵢ ∈ [0, 1] and directional sign sᵢ ∈ {-1, 0, +1}:

raw     = Σ wᵢ · cᵢ · sᵢ  /  Σ wᵢ
boost   = raw + sign(raw) · A · agreement          # if agreement > 0.5
penalty = boost · (1 - P · conflict)               # if conflict   > 0.4
final   = penalty · max(0.2, 1 - V · volZ)         # volatility dampener

signal  = BUY  if final ≥ τ⁺
        | SELL if final ≤ τ⁻
        | HOLD otherwise

Defaults (config.yaml): A=0.2, P=0.3, V=0.5, τ=±0.25.


Limitations (be honest)

  • Vision brittleness. Extraction from arbitrary chart screenshots is imperfect — unusual colour schemes, overlays (drawings, volume histograms, indicator panes) can confuse the detector. The ROI detector is a heuristic, not a semantic parser.
  • Price scale is unknown. Reconstructed OHLC is normalised to [0, 1]. Absolute targets/stops need the real price scale, which is why the backtester in-app uses synthetic numerical OHLC directly.
  • Alpha decay. Any rule we write has been written before. Results out of sample will be much worse than in-sample.
  • Overfitting risk in the CNN. With only synthetic data the CNN learns the renderer, not the market. Treat its votes with care.
  • No order routing. This is a research platform, not a broker client.
  • Confidence ≠ probability. Raw fusion confidence is not a calibrated probability. The ConfidenceCalibrator can isotonically re-map it, but you need labelled outcomes to fit it.

Research extensions already included

  • Walk-forward backtester (src/backtest/walk_forward.py) — tunes fusion hyperparameters in-sample per fold, evaluates purely out-of-sample, stitches OOS equity curves.
  • Monte Carlo robustness (src/analysis/monte_carlo.py) — bootstrap trade re-ordering and return perturbation to produce 5 / 50 / 95 percentile equity bands and P(profit).
  • Multi-timeframe (src/analysis/multi_timeframe.py) — resamples the reconstructed OHLC to multiple scales, runs the strategies on each, and applies a higher-timeframe bias.
  • Risk manager (src/risk/risk_manager.py) — ATR-sized positions with confidence-scaled / Kelly-capped sizing, plus circuit breakers (max drawdown, consecutive losses, volatility cap, min confidence).
  • Natural-language explainer (src/analysis/explainer.py) — deterministic templated paragraph that summarises the verdict.
  • Profiler (scripts/profile_pipeline.py) — per-stage timing so you can see exactly where budget goes.

Future work

  • Replace the heuristic ROI detector with a U-Net trained to segment chart panels / axes / legends.
  • Volume histogram parsing from below-chart bars.
  • Online CNN training on real charts (platform-agnostic normaliser).
  • Learned meta-ensemble with per-market regime features.
  • Order book / time-series features fused with vision features.
  • Purged K-fold CV with embargo for the meta model.
  • GPU batching of strategy evaluation for backtests.

Testing

pytest -q tests/

The smoke tests verify the full pipeline runs on synthetic data and produces valid signals. They are intentionally loose (no assertions on specific directions) because the engine is probabilistic.

About

Multi-Strategy AI Trading Intelligence Engine — image/CSV chart analysis with fused BUY/SELL/HOLD verdicts. Streamlit dashboard + CLI.

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages