项目 / TreborNamor/TradingView-Machine-Learning-GUI
该仓库路径仍为 `TreborNamor/TradingView-Machine-Learning-GUI`,当前 README 将项目称为 HyperView,是一套以终端为主的策略研究工具:从 TradingView 下载历史行情,按接近 Pine 的执行假设回测 Python 策略,并在可复现的流程里做参数优化。统一命令现为 `tradingview-backtest`,仍兼容 `tvbacktest`、`hyperview` 和 `python -m hyperview`。运行需要 Python 3.11 及以上,以及随包安装的 TA-Lib;可用 `uvx`、`pipx` 或 `pip` 安装。FAQ 写明:不必使用 TradingView Pro,也可下载、回测和优化,但未登录会话的历史深度通常更浅;数据按 K 线周期组织,不提供 tick 级行情,也不能直接做 tick 级或高频回放校验;下载不依赖 TradingView API 密钥。许可为 MIT。本页只介绍用途与入口。说明见[仓库 README](https://github.com/TreborNamor/TradingView-Machine-Learning-GUI)。
本站整理,非 GitHub 官方 description。
官方快照(gh_history.db,2026-11-25):总榜 未进 Top50 · 日榜 未进 Top50 · 周榜未满 7 天基线(官方不生成) · 月榜暂无官方口径;下方四卡为站内趋势聚合(周卡按上榜次数,非官方 7 日增量窗)。
以下三段按该项目自己的 GitHub README 章节摘录(可合理截断并标明);README 没有对应章节的留空,不编造。
可先运行 python -m pip install tradingview-backtest,再运行 python -m hyperview --help。
项目支持从 TradingView websocket 下载最多约4万根K线,提供 Python 策略框架与 TA-Lib 指标、组合权益曲线回测、Optuna 止盈止损优化、Pine 两阶段与批量优化及结构化产物目录。
该分叉版本要求 Python 3.11+,依赖 TA-Lib 和 rich(pip 安装时自动装上),可选 Firefox 用于复用已登录的 TradingView 会话以提高历史数据上限。
来自 GitHub 仓库 README / 主页介绍正文,正文较长,已截断:保留前 20000 字符(原始 22,313 字节),完整内容见 GitHub 仓库。
# HyperView
**Turn TradingView ideas into testable, terminal-speed trading systems.**
HyperView is a CLI-first toolkit for downloading TradingView market data, backtesting Python strategies with Pine-like execution behavior, and optimizing parameters in a repeatable workflow.
Vietnamese version: [README.vi.md](README.vi.md)
## Support This Project
If you discover a strong strategy using this repo, please consider giving the project a **star** and **forking** it to support further development.
## Pain Points
- Manual optimization on TradingView charts is slow, hard to reproduce, and difficult to audit.
- Results often diverge between Pine and local scripts because fill assumptions differ.
- Data, presets, and reports are scattered across CSVs, notebooks, and ad-hoc scripts.
- Multi-strategy and multi-pair research takes too long without a standardized pipeline.
## Solution
- A unified CLI workflow: `download -> signal -> backtest -> optimize -> export artifacts`.
- A TradingView-like backtester (next-bar-open entries + intrabar SL/TP path simulation).
- Automatic preset/report/context storage for comparison and reproducible reruns.
- Pine input optimization with best-parameter injection into exported Pine scripts.
## Key Features
- Historical download from TradingView websocket (up to ~40K bars with eligible authenticated session).
- Python strategy framework + TA-Lib (20 wrapped indicators + direct TA-Lib access).
- Backtesting with mathematically correct portfolio aggregation from combined equity curves.
- SL/TP optimization via Optuna (TPE), with top preset persistence.
- Two-stage Pine optimization + batch optimization for multiple strategy files.
- Structured artifacts (`data/`, `results/`, `strategies/raw/`, `strategies/optimized/`).
## FAQ (Pro Account & Tick Data)
### Can I use HyperView without a TradingView Pro account?
Yes. You can still use download, backtest, and optimization features without Pro.
In practice, data depth is typically lower for anonymous/non-authenticated sessions (often around ~5K bars), while authenticated paid sessions can reach higher limits (up to ~40K bars, depending on account/session availability).
### Does HyperView support tick data?
No. HyperView is candle/timeframe based (`1m`, `5m`, `15m`, `1h`, etc.), not tick-by-tick data stream based.
### Is `1m` data equivalent to tick data?
No. `1m` is still aggregated OHLCV bar data, not raw market ticks.
### Can this be used for tick-level/HFT validation?
Not natively in the current architecture. The backtester uses bar-level + intrabar-path assumptions, not full tick replay.
### Do I need any TradingView API key?
No. The downloader uses TradingView websocket/session mechanics and optional local browser session credentials.
## Practical Outcomes
- Faster strategy research loops by replacing chart-click workflows with automated CLI pipelines.
- Easier team collaboration through consistent presets, reports, and saved contexts.
- Lower mismatch risk when moving from Pine ideas to quantitative validation.
- Better scaling for multi-strategy, multi-symbol, and multi-timeframe experiments.
## Improvements Over The Original Fork Source
The fork improvements are not only cosmetic; they are implemented as concrete workflow tasks:
1. **CLI Standardization Task**
- Unified command entry around `tradingview-backtest`.
- Kept backward-compatible aliases: `hyperview`, `python -m hyperview`.
- Goal: reduce team friction across local terminal + AI CLI usage.
2. **Pine Optimization Pipeline Task**
- Added optimized Pine export with best params injected into defaults.
- Added compact filename metrics (`np/dd/pf/tc`) for fast scanning.
- Added per-run metadata headers in exported Pine snapshots.
3. **Batch Orchestration Task**
- Added batch runner to optimize multiple Pine files from `strategies/raw/`.
- Added matrix execution for symbols/timeframes.
- Added leaderboard outputs for top results aggregation.
4. **Artifact Contract Task**
- Standardized directories:
- `strategies/raw/`
- `strategies/optimized/`
- `results/optimizations/<symbol>/<timeframe>/`
- Goal: deterministic outputs for commit/push/audit and easier collaboration.
5. **Automation & Onboarding Task**
- Added cross-platform bootstrap scripts (`.cmd`, `.ps1`, `.sh`).
- Added GitHub release workflow and build smoke checks.
- Added Codex/Claude quick workflow guidance in docs.
## Prerequisites
### Core runtime requirements
- **Python 3.11+**
- **TA-Lib** — installed automatically by `pip install`. Pre-built wheels are available for major OS/Python combinations.
- **rich** — installed automatically for CLI tables/panels/progress UI.
- **Firefox** *(optional)* — for authenticated TradingView session reuse (higher historical bar limits).
### Additional requirements to use fork improvements fully
- **Git + GitHub CLI (`gh`)** — needed for release automation, fork sync, and GitHub-native workflow management.
- **One of `uvx` / `pipx` / `pip`** — needed for portable install/run modes documented in this fork.
- **Writable workspace for artifacts** — required because this fork intentionally tracks richer outputs in:
- `data/`
- `results/`
- `strategies/raw/`
- `strategies/optimized/`
### Verification baseline (recommended before serious runs)
```bash
python -m unittest discover -s tests -v
python -m hyperview --help
python -m hyperview list-strategies
```
## Install & Run Anywhere
Canonical CLI command is now `tradingview-backtest`.
Backwards-compatible aliases still work: `tvbacktest`, `hyperview`, and `python -m hyperview`.
### Option A (Recommended, npx-like): `uvx`
```bash
# Run directly from GitHub (no long-lived install)
uvx --from git+https://github.com/<org>/tradingview-backtest.git tradingview-backtest --help
```
```bash
# After publishing to PyPI
uvx tradingview-backtest --help
```
### Option B (npm -g-like): `pipx`
```bash
pipx install tradingview-backtest
tradingview-backtest --help
```
### Option C (Universal fallback): `pip`
```bash
python -m pip install tradingview-backtest
python -m hyperview --help
```
### Bootstrap Scripts (from this repo)
```bash
# Windows (cmd)
scripts\bootstrap.cmd local
# Windows (PowerShell)
.\scripts\bootstrap.ps1 -Mode local
# Linux/macOS
./scripts/bootstrap.sh local
```
## Quick Start (Project Development)
```bash
# Install in editable mode (creates `tradingview-backtest`, `tvbacktest`, and `hyperview`)
pip install -e .
# Download data for specific pairs
tradingview-backtest download-data --pairs NASDAQ:NFLX NASDAQ:AAPL --timeframe 1h --session extended
# Or define your pairs in config.json and download multiple timeframes at once:
tradingview-backtest download-data --timeframe 1h 15m
# Run a single backtest (uses config pairlist)
tradingview-backtest backtest --sl 3.23 --tp 13.06 --mode long
# Or target a specific symbol using values from a hyperopt preset file
tradingview-backtest backtest --symbol NASDAQ:NFLX --preset-file results/adx_stochastic_presets.json
# Hyper-optimize SL/TP across all pairs in config
tradingview-backtest hyperopt --mode long
# List cached data and registered strategies
tradingview-backtest list-data
tradingview-backtest list-strategies
```
You can still run via `python -m hyperview` for environments that prefer module execution.
Python bytecode is redirected into the project-level `.pycache/` directory, so runtime imports do not create scattered `__pycache__` folders under `hyperview/` or `strategy/`.
## Fast Workflow With Codex CLI / Claude Code
This repo works well with both **Codex CLI** and **Claude Code** for AI-assisted development.
### 1) One-time setup
```bash
# Clone repo
git clone https://github.com/hungpixi/tradingview-backtest.git
cd tradingview-backtest
# Bootstrap local env (cross-platform script)
# Windows:
scripts\bootstrap.cmd local
# Linux/macOS:
./scripts/bootstrap.sh local
```
### 2) Prompt examples for AI CLI
- `"Run pine-batch-optimize for OANDA:XAUUSD on 15m and summarize best result."`
- `"Add a new CLI flag for pine-optimize and include unit tests."`
- `"Refactor hyperview/cli/pine.py but keep command behavior backward-compatible."`
- `"Review this branch for regressions in backtest and pine optimize flow."`
### 3) Quick verification before commit
```bash
python -m unittest discover -s tests -v
python -m hyperview --help
python -m hyperview list-strategies
```
### 4) Tips for better AI output quality
- Be explicit about the goal and desired output (specific files/reports/commands).
- Always require verification commands before an AI says work is done.
- For larger changes, ask for scoped commits (`feat`, `chore`, `docs`).
- In this repo, prioritize real CLI validation (`download-data`, `backtest`, `hyperopt`, `pine-optimize`) rather than code-only edits.
## How It Works
1. **Download** — Connects to TradingView's websocket using your existing Firefox session cookies. Supports up to 40K historical bars on paid plans with automatic backfill.
2. **Signal** — Runs a pluggable strategy (e.g. the included MACD+RSI or ADX+Stochastic) in pure Python with TA-Lib indicator parity.
3. **Backtest** — Simulates trades bar-by-bar using TradingView-parity fill assumptions (next-bar-open entry, intrabar SL/TP exit ordering). Multi-pair runs produce a true **PORTFOLIO** aggregate row with combined equity-curve statistics.
4. **Hyper-Optimize** — Runs Bayesian optimization (Optuna TPE) across SL/TP combinations, then updates a strategy preset file with the best result for each pair/context.
## Terminal Output
Both the backtest and hyperopt commands produce styled terminal output using [rich](https://github.com/Textualize/rich):
- **Backtest summary** — A bordered table with colored directional arrows (▲ green for gains, ▼ red for losses) on Return, Drawdown, Expectancy, and Worst Trade, using readable short labels that fit a normal terminal width. When multiple pairs are run, a **PORTFOLIO** row is appended with mathematically correct aggregate statistics computed from a combined equity curve (not simple averages).
- **Hyperopt results** — A panel header showing strategy/mode/timeframe, bullet-point data and signal summaries per pair, and a top-N results table with cyan-highlighted parameter columns (SL/TP) visually separated from metric columns.
## Repository Layout
```
pyproject.toml Package metadata & CLI entry point
config.json Default configuration (timeframe, pairlist, opt ranges)
config.schema.json JSON Schema for editor validation & autocompletion
data/ Cached candle CSVs (auto-generated)
results/ Optimization presets & reports (auto-generated)
strategies/raw/ Source Pine scripts for optimization input
strategies/optimized/ Best optimized Pine exports (filename includes metrics)
```
### `strategy/` — Pluggable Strategy Framework
```
strategy/
├── __init__.py Plugin registry & auto-discovery
├── base.py BaseStrategy ABC & prepare_candles()
├── indicators.py TA-Lib wrappers, conversion helpers & signal toolkit
├── adx_stochastic.py ADX+Stochastic strategy
└── macd_rsi.py MACD+RSI strategy
```
### `hyperview/` — Core Engine
```
hyperview/
├── __main__.py Module entry point (python -m hyperview)
├── config.py Config loader (JSON + CLI overrides + env vars)
├── models.py Shared dataclasses (CandleRequest, Trade, BacktestMetrics, …)
├── presets.py Preset load/save for optimized SL/TP parameters
├── validators.py Configuration & preset validation rules
├── runtime.py Bytecode cache redirection
│
├── cli/ CLI router & subcommand handlers
│ ├── __init__.py Argument parser & main() dispatcher
│ ├── formatting.py Shared formatting helpers (rich tables, arrow decorators)
│ ├── backtest.py backtest command
│ ├── download.py download-data command
│ ├── hyperopt.py hyperopt command
│ └── list.py list-data & list-strategies commands
│
├── backtest/
│ └── engine.py TradingView-parity OHLC simulator
│
├── downloader/
│ ├── client.py TradingView websocket downloader & cache orchestration
│ ├── cache.py CSV-backed local candle cache
│ ├── credentials.py Firefox credential extraction
│ ├── session.py WebSocket chart session manager
│ └── timeframes.py Timeframe constants & utilities
│
└── hyperopt/
└── optimizer.py Bayesian optimizer (Optuna TPE)
```
## Configuration
HyperView loads defaults from `config.json` at the project root. CLI flags always override config values.
The sample below shows a customized setup; if a key is omitted, HyperView falls back to runtime defaults.
```json
{
"timeframe": "1h",
"session": "regular",
"mode": "long",
"strategy": "adx_stochastic",
"initial_capital": 100000,
"data_dir": "data",
"output_dir": "results",
"pairlist": [
"NASDAQ:NFLX",
"NASDAQ:TSLA",
"COINBASE:BTCUSD",
"COINBASE:ETHUSD"
],
"optimization": {
"n_trials": 200,
"objective": "net_profit_pct",
"top_n": 10,
"sl_range": { "min": 1.0, "max": 15.0 },
"tp_range": { "min": 1.0, "max": 15.0 }
}
}
```
Use `--config /path/to/custom.json` to load a different file.
### Pairlist
The `pairlist` array defines the symbols you want to work with. Every entry must use the `EXCHANGE:SYMBOL` format — this lets you mix pairs from different exchanges in a single config:
```json
"pairlist": [
"NASDAQ:NFLX",
"NASDAQ:TSLA",
"NASDAQ:AAPL",
"COINBASE:BTCUSD"
]
```
When you run a command without `--pairs` or `--symbol`, HyperView automatically uses the config pairlist — downloading, backtesting, or optimizing every pair in sequence. If you pass `--pairs` or `--symbol` on the CLI, the config pairlist is ignored for that run.
You can maintain separate config files for different asset classes:
```bash
hyperview --config stocks.json download-data
hyperview --config crypto.json hyperopt --mode long
```
## CLI Reference
All command examples below can use `tradingview-backtest` (recommended) or legacy alias `hyperview`.
## Migration Notes
- `hyperview` command is still supported for backward compatibility.
- New canonical command for docs/releases is `tradingview-backtest`.
- Short alias `tvbacktest` is also available.
### `download-data` — Fetch Candle Data
```bash
# Download all pairs from config pairlist
hyperview download-data
# Or specify pairs directly, including multiple timeframes
hyperview download-data --pairs NASDAQ:NFLX NASDAQ:AAPL NASDAQ:TSLA --timeframe 1h 15m --start 2023-01-03
```
| Flag | Required | Default | Description |
|------|----------|---------|-------------|
| `--pairs` | No | config pairlist | One or more `EXCHANGE:SYMBOL` pairs (overrides pairlist) |
| `--timeframe` | No | config | One or more bar intervals: `1m` `5m` `15m` `1h` `4h` `1d` etc. |
| `--start` / `--end` | No | — | Date range (ISO format) |
| `--session` | No | config | `regular` or `extended` |
| `--adjustment` | No | `splits` | Price adjustment (`splits`, `dividends`, `none`) |
### `backtest` — Single Strategy Evaluation
```bash
# Backtest all pairs from config pairlist
hyperview backtest --sl 5.0 --tp 5.0 --mode long --start 2023-01-03
# Or target a specific symbol using a preset file created by hyperopt
hyperview backtest --symbol NASDAQ:NFLX --preset-file results/adx_stochastic_presets.json --start 2023-01-03
```
If `--sl` and `--tp` are omitted, HyperView looks for a matching entry in the provided `--preset-file` using `pair + timeframe + session + adjustment + mode`. CLI values still override preset-file values.
| Flag | Required | Default | Description |
|------|----------|---------|-------------|
| `--symbol` | No | config pairlist | `EXCHANGE:SYMBOL` pair (overrides pairlist) |
| `--sl` | No* | — | Stop-loss % (*required unless a matching `--preset-file` entry exists) |
| `--tp` | No* | — | Take-profit % (*required unless a matching `--preset-file` entry exists) |
| `--preset-file` | No | auto-detected | Path to a strategy preset JSON (auto-detects `<strategy>_presets.json` in output dir) |
| `--strategy` | No | config | Strategy name (e.g. `macd_rsi`, `adx_stochastic`) |
| `--mode` | No | `long` | `long`, `short`, or `both` |
| `--timeframe`, `--session`, `--adjustment`, `--start`, `--end` | No | config / defaults | Standard filters |
### `hyperopt` — Hyper-Optimize SL/TP
```bash
# Optimize all pairs from config pairlist (runs one optimization per pair)
hyperview hyperopt --n-trials 300
# Or target a specific symbol
hyperview hyperopt --symbol NASDAQ:NFLX --n-trials 300
```
| Flag | Required | Default | Description |
|------|----------|---------|-------------|
| `--symbol` | No | config pairlist | `EXCHANGE:SYMBOL` pair (overrides pairlist) |
| `--sl-min`, `--sl-max` | No | config | Stop-loss % search range |
| `--tp-min`, `--tp-max` | No | config | Take-profit % search range |
| `--n-trials` | No | config | Number of Bayesian optimization trials (default: 200) |
| `--objective` | No | config | `net_profit_pct` `profit_factor` `win_rate_pct` `max_drawdown_pct` `trade_count` |
| `--top-n` | No | config | Number of top candidates to keep |
| `--strategy`, `--mode`, `--timeframe`, `--adjustment`, etc. | No | config / defaults | Standard filters |
### `list-data` — Show Cached Datasets
```bash
hyperview list-data
```
### `list-strategies` — Show Available Strategies
```bash
hyperview list-strategies
```
### `pine-optimize` — Optimize Pine Inputs + Export Best Pine
```bash
hyperview pine-optimize --pine-file strategies/raw/smc_swing_strategy.pine --symbol OANDA:XAUUSD --timeframe 15m
```
- Exports best Pine by default to `strategies/optimized/`.
- Default optimized filename template:
`{symbol}_{tf}_{strategy}_np{net}_dd{dd}_pf{pf}_tc{trades}.pine`
- Pair/timeframe reports are written under:
`results/optimizations/<symbol>/<timeframe>/`
### `pine-batch-optimize` — Run Matrix Optimization for Many Pine Files
```bash
hyperview pine-batch-optimize --input-dir strategies/raw --symbols OANDA:XAUUSD OANDA:EURUSD --timeframes 15m 1h
```
- Runs `pine-optimize` for each `pine x symbol x timeframe`.
- Writes aggregate leaderboard files:
- `results/optimizations/leaderboard.json`
- `results/optimizations/leaderboard.md`
## Indicators
HyperView ships with **20 wrapped indicators** backed by TA-Lib, plus **4 signal helpers**. You also have direct access to all **150+ TA-Lib functions** via the `to_numpy` / `wrap` conversion helpers.
### Wrapped Indicators
| Category | Functions |
|----------|-----------|
| **Moving Averages** | `ema`, `sma`, `wma` |
| **Momentum** | `rsi`, `macd`, `stochastic`, `stochastic_rsi`, `cci`, `williams_r`, `momentum`, `roc` |
| **Trend** | `adx` (returns ADX, +DI, −DI), `aroon` (returns down, up), `psar` |
| **Volatility** | `atr`, `bollinger_bands` (returns upper, middle, lower) |
| **Volume** | `obv`, `mfi`, `ad`, `vwap` |
### Signal Helpers
| Function | Description |
|----------|-------------|
| `crossed_above(a, b)` | True on bars where `a` crosses above `b` |
| `crossed_below(a, b)` | True on bars where `a` crosses below `b` |
| `barssince(cond)` | Bars since condition was last True |
| `to_unix_timestamp(dt)` | Convert ISO date string to UTC unix timestamp |
### Using TA-Lib Directly
For any of TA-Lib's 150+ functions not wrapped above, call `talib` directly and use the conversion helpers:
```python
import talib
from strategy.indicators import to_numpy, wrap
df["cci"] = wrap(talib.CCI(to_numpy(df["high"]),
to_numpy(df["low"]),
to_numpy(df["close"]), timeperiod=20), df.index)
```
## Adding Custo