Workflow
- Requirements parsing: parse user intent, extract instrument codes, time range, and strategy logic, then write
config.json
- Strategy design: think through the 5 questions of data / signal / position sizing / backtest / validation
- Strategy coding: write
code/signal_engine.py (following the SignalEngine contract)
- Syntax check:
bash("python -c \"import ast; ast.parse(open('code/signal_engine.py').read()); print('OK')\"")
- Run backtest: call the
backtest tool (built into the engine; no need to write run_backtest.py)
- Evaluate results: read
artifacts/metrics.csv and judge by the review criteria
- Iterative fixing: if results are poor, modify with
edit_file → run backtest → re-evaluate
You only need to write signal_engine.py and config.json. The backtest tool automatically handles data loading and backtest execution.
Requirements Parsing
Extract the following from the user's description:
- Instrument codes: process them according to the normalization rules below
- Time range: if the user does not specify dates, default to 10 years back from today (for example, if today is
2026-03-18, then start_date=2016-03-18, end_date=2026-03-18)
- Strategy logic: entry / exit conditions and indicator parameters
If critical information is missing, you must ask the user instead of guessing:
- Instrument not specified → ask which instrument they want to backtest (offer several popular suggestions)
- Strategy description is vague (for example, "help me build a strategy") → provide 2-3 strategy directions for the user to choose from
- Mixed markets but not clearly specified → confirm the data source
Write config.json first, then write code. config.json must be placed in the root of run_dir.
Strategy Design
Before writing code, think through these 5 questions:
- Data requirements: what fields are needed (basic OHLCV only? or fundamentals such as
pe/pb/roe as well?), data frequency (daily), and market (which determines the data source)
- Signal logic: what are the entry conditions? What are the exit conditions? Direction (long / short / long-short)? Are there filters (volume, trend confirmation, and so on)?
- Position management: equal-weight allocation or scaling in/out? Risk control (stop-loss, maximum position)? In portfolio strategies, once top N names are selected, each weight = 1/N
- Backtest parameters: time range, initial capital (default 1,000,000), commission (default 0.1%)
- Validation checklist: signal consistency (no NaN signals), position check (normalized to prevent leverage), and completeness of generated artifacts
There is no need to output a JSON design document. Express these design decisions directly in code.
SignalEngine Contract
class SignalEngine:
def generate(self, data_map: Dict[str, pd.DataFrame]) -> Dict[str, pd.Series]:
"""
Args:
data_map: code -> DataFrame (columns: open, high, low, close, volume, DatetimeIndex)
If config.extra_fields is specified, pe, pb, roe, and similar columns will also be present.
Returns:
code -> signal Series, value range [-1.0, 1.0]
1.0 = fully long, 0.5 = half position, 0.0 = flat, -1.0 = fully short
Portfolio strategy: selected stocks split weights equally (for example top 10 -> each 0.1)
Legacy integer signals {-1, 0, 1} remain compatible (treated as -100% / 0% / 100%)
"""
Hard constraints:
- The signal
Series index must align exactly with the input DataFrame index
- Include all required imports (
numpy, pandas, and so on)
- Do not hardcode dates or stock codes (read them from
config.json)
- Do not include an
if __name__ == "__main__" block
- Pure pandas / numpy implementation, with no external signal libraries
- Output plain Python code, not Markdown fences
Quality Checklist
Self-check after writing signal_engine.py:
Instrument Code Normalization
- 6-digit China A-share codes → automatically append suffix: codes starting with
600/601/603 → .SH, all others → .SZ
- US stocks: uppercase letters +
.US, such as AAPL.US (yfinance converts automatically)
- Hong Kong stocks: digits +
.HK, such as 700.HK (yfinance converts automatically)
- Cryptocurrencies:
BTC-USDT format (OKX spot pairs, must use the hyphen -, not slash /)
- The user may write
BTC/USDT, but config.json must use "BTC-USDT"
Cryptocurrency Notes
- Code format: must be
XXX-USDT (uppercase + hyphen), such as BTC-USDT and ETH-USDT
- source: must be set to
"okx"
- extra_fields: must be
null (OKX does not support fundamentals)
- Data format:
DataLoader has already normalized the output to match China A-shares exactly: open, high, low, close, volume + DatetimeIndex
- No special handling needed in strategy code:
signal_engine.py should be written the same way as for China A-shares; do not add extra data conversion for OKX
Market Detection and Data Sources
| Pattern |
Market |
source |
Extra Fields |
^\d{6}\.(SZ|SH|BJ)$ |
China A-shares |
tushare |
pe, pb, pe_ttm, ps_ttm, dv_ttm, total_mv, circ_mv, roe |
^[A-Z]+\.US$ |
US stocks |
yfinance |
- |
^\d{3,5}\.HK$ |
Hong Kong stocks |
yfinance |
- |
^[A-Z]+-USDT$ |
Cryptocurrency |
okx |
- |
extra_fields selection logic: only China A-shares (tushare) support fundamentals. If the strategy needs PE/PB/ROE and similar fields, specify them in config.json.extra_fields and DataLoader will retrieve them automatically. Hong Kong stocks, US stocks, and crypto do not support extra_fields.
config.json Format
{
"source": "auto",
"codes": ["000001.SZ"],
"start_date": "2016-03-18",
"end_date": "2026-03-18",
"interval": "1D",
"initial_cash": 1000000,
"commission": 0.001,
"extra_fields": null,
"optimizer": null,
"optimizer_params": {},
"engine": "daily",
"validation": null
}
source: "auto" (recommended, auto-select by code format) / "tushare" / "yfinance" / "okx" / "akshare" / "ccxt"
"auto" supports mixed instruments. For example, ["000001.SZ", "BTC-USDT"] will be automatically routed to tushare and okx
- Futures codes (e.g.
"IF2406.CFFEX", "ESZ4") and forex pairs (e.g. "EUR/USD") are also auto-routed
interval: candlestick interval, default "1D". Supported values: "1m" / "5m" / "15m" / "30m" / "1H" / "4H" / "1D"
- The annualization factor for minute backtests is inferred automatically from
source (252 trading days for China A-shares, 365 calendar days for crypto)
- Minute backtests can be very data-heavy. Recommended limits are no more than 30 days for
1m, or 1 year for 1H
extra_fields: China A-shares can use values such as ["pe", "pb", "roe"]; other markets should use null
optimizer: optional, one of "equal_volatility" / "risk_parity" / "mean_variance" / "max_diversification" / null (equal-weight by default)
optimizer_params: optimizer parameters, such as {"lookback": 60}. mean_variance additionally supports {"risk_free": 0.0}
engine: backtest engine, default "daily". For options strategies, set "options" (requires OptionsSignalEngine)
initial_cash: default 1,000,000
commission: default 0.1%
validation: optional statistical validation after backtest completes. Omit to skip. Example:"validation": {
"monte_carlo": {"n_simulations": 1000},
"bootstrap": {"n_bootstrap": 1000, "confidence": 0.95},
"walk_forward": {"n_windows": 5}
}
monte_carlo: permutation test — shuffles trade order to compute p-value (is Sharpe significantly better than random?)
bootstrap: resamples daily returns to compute Sharpe 95% confidence interval
walk_forward: splits equity curve into N windows, checks performance consistency
- Each key is optional — include only the validations you want
- Can also run standalone on past results:
python -m backtest.validation <run_dir>
Review Criteria
Hard Gates (any failure → passed=false)
artifacts/metrics.csv exists and is non-empty
artifacts/equity.csv exists and is non-empty
exit_code == 0 (backtest exits normally)
- The
equity column in equity.csv contains no NaN values
trade_count > 0 (zero trades = signal bug)
Scoring Rules
- Successful backtest + complete artifacts + at least 1 trade →
score ≥ 60 → passed
- Poor return / low Sharpe alone should not push the score below 60; they are optimization suggestions only
score ≥ 60 = passed=true
Bug Categories (reduce the score)
- Zero trades (
trade_count=0): signal-logic bug, conditions may be too strict
- Late first trade (first trade > 2 years after backtest start): data-filtering bug or overly long lookback window
- Capital utilization < 50%: position-management bug, portfolio is flat most of the time
- Open position at the end (positions still open when backtest ends): exit-signal timing bug
action_items Format
If improvements are needed after evaluation, write action_items:
- Format:
"Change X from A to B" or "Add X logic in signal_engine.py"
- Must be specific down to parameter values, file names, and function names
- At least 2 items
- Examples:
"Change short MA from 5 to 10 days to reduce whipsaw signals"
"Add stop-loss: force close when loss exceeds 5%"
"Add volume filter in signal_engine.py: only trigger buy on high volume"
Cross-Market Strategies
When the user requests a backtest with codes from different markets (e.g. ["000001.SZ", "BTC-USDT"]):
- Set
source: "auto" in config.json
- The
CompositeEngine handles calendar alignment, shared capital, and per-market rules automatically
- Use volatility-adjusted weights so high-vol assets (crypto) don't dominate the risk budget
- See the cross-market-strategy skill for per-market parameters, vol-adjustment, and example code
Supporting Files
- examples.md — example call sequence
1---2name: strategy-generate3description: Create, modify, and optimize quantitative trading strategies, then backtest and evaluate them.4---56## Workflow781. **Requirements parsing**: parse user intent, extract instrument codes, time range, and strategy logic, then write `config.json`92. **Strategy design**: think through the 5 questions of data / signal / position sizing / backtest / validation103. **Strategy coding**: write `code/signal_engine.py` (following the `SignalEngine` contract)114. **Syntax check**: `bash("python -c \"import ast; ast.parse(open('code/signal_engine.py').read()); print('OK')\"")`125. **Run backtest**: call the `backtest` tool (built into the engine; no need to write `run_backtest.py`)136. **Evaluate results**: read `artifacts/metrics.csv` and judge by the review criteria147. **Iterative fixing**: if results are poor, modify with `edit_file` → run `backtest` → re-evaluate1516**You only need to write `signal_engine.py` and `config.json`. The `backtest` tool automatically handles data loading and backtest execution.**1718## Requirements Parsing1920Extract the following from the user's description:21- **Instrument codes**: process them according to the normalization rules below22- **Time range**: if the user does not specify dates, default to **10 years back from today** (for example, if today is `2026-03-18`, then `start_date=2016-03-18`, `end_date=2026-03-18`)23- **Strategy logic**: entry / exit conditions and indicator parameters2425**If critical information is missing, you must ask the user instead of guessing:**26- Instrument not specified → ask which instrument they want to backtest (offer several popular suggestions)27- Strategy description is vague (for example, "help me build a strategy") → provide 2-3 strategy directions for the user to choose from28- Mixed markets but not clearly specified → confirm the data source2930**Write `config.json` first, then write code.** `config.json` must be placed in the root of `run_dir`.3132## Strategy Design3334Before writing code, think through these 5 questions:35361. **Data requirements**: what fields are needed (basic OHLCV only? or fundamentals such as `pe/pb/roe` as well?), data frequency (daily), and market (which determines the data source)372. **Signal logic**: what are the entry conditions? What are the exit conditions? Direction (long / short / long-short)? Are there filters (volume, trend confirmation, and so on)?383. **Position management**: equal-weight allocation or scaling in/out? Risk control (stop-loss, maximum position)? In portfolio strategies, once top N names are selected, each weight = 1/N394. **Backtest parameters**: time range, initial capital (default 1,000,000), commission (default 0.1%)405. **Validation checklist**: signal consistency (no NaN signals), position check (normalized to prevent leverage), and completeness of generated artifacts4142There is no need to output a JSON design document. Express these design decisions directly in code.4344## `SignalEngine` Contract4546```python47class SignalEngine:48 def generate(self, data_map: Dict[str, pd.DataFrame]) -> Dict[str, pd.Series]:49 """50 Args:51 data_map: code -> DataFrame (columns: open, high, low, close, volume, DatetimeIndex)52 If config.extra_fields is specified, pe, pb, roe, and similar columns will also be present.53 Returns:54 code -> signal Series, value range [-1.0, 1.0]55 1.0 = fully long, 0.5 = half position, 0.0 = flat, -1.0 = fully short56 Portfolio strategy: selected stocks split weights equally (for example top 10 -> each 0.1)57 Legacy integer signals {-1, 0, 1} remain compatible (treated as -100% / 0% / 100%)58 """59```6061**Hard constraints:**62- The signal `Series` index must align exactly with the input `DataFrame` index63- Include all required imports (`numpy`, `pandas`, and so on)64- Do not hardcode dates or stock codes (read them from `config.json`)65- Do not include an `if __name__ == "__main__"` block66- Pure pandas / numpy implementation, with no external signal libraries67- Output plain Python code, not Markdown fences6869## Quality Checklist7071Self-check after writing `signal_engine.py`:72- [ ] All imports are included (`numpy`, `pandas`, `typing`, and so on)73- [ ] No undefined variables74- [ ] Signal logic is consistent with the strategy description75- [ ] Boundary handling: for empty data or insufficient history before the lookback window, use `fillna(0)` or skip76- [ ] Portfolio strategy: once N stocks are selected, each weight = 1/N (for example top 10 → each 0.1), unselected names = 077- [ ] Signal values stay within `[-1.0, 1.0]`7879## Instrument Code Normalization8081- 6-digit China A-share codes → automatically append suffix: codes starting with `600/601/603` → `.SH`, all others → `.SZ`82- US stocks: uppercase letters + `.US`, such as `AAPL.US` (`yfinance` converts automatically)83- Hong Kong stocks: digits + `.HK`, such as `700.HK` (`yfinance` converts automatically)84- Cryptocurrencies: `BTC-USDT` format (OKX spot pairs, **must use the hyphen `-`, not slash `/`**)85 - The user may write `BTC/USDT`, but `config.json` must use `"BTC-USDT"`8687## Cryptocurrency Notes8889- **Code format**: must be `XXX-USDT` (uppercase + hyphen), such as `BTC-USDT` and `ETH-USDT`90- **source**: must be set to `"okx"`91- **extra_fields**: must be `null` (OKX does not support fundamentals)92- **Data format**: `DataLoader` has already normalized the output to match China A-shares exactly: `open, high, low, close, volume` + `DatetimeIndex`93- **No special handling needed in strategy code**: `signal_engine.py` should be written the same way as for China A-shares; do not add extra data conversion for OKX9495## Market Detection and Data Sources9697| Pattern | Market | source | Extra Fields |98|------|------|--------|----------|99| `^\d{6}\.(SZ\|SH\|BJ)$` | China A-shares | tushare | pe, pb, pe_ttm, ps_ttm, dv_ttm, total_mv, circ_mv, roe |100| `^[A-Z]+\.US$` | US stocks | yfinance | - |101| `^\d{3,5}\.HK$` | Hong Kong stocks | yfinance | - |102| `^[A-Z]+-USDT$` | Cryptocurrency | okx | - |103104**`extra_fields` selection logic**: only China A-shares (`tushare`) support fundamentals. If the strategy needs `PE/PB/ROE` and similar fields, specify them in `config.json.extra_fields` and `DataLoader` will retrieve them automatically. Hong Kong stocks, US stocks, and crypto do not support `extra_fields`.105106## `config.json` Format107108```json109{110 "source": "auto",111 "codes": ["000001.SZ"],112 "start_date": "2016-03-18",113 "end_date": "2026-03-18",114 "interval": "1D",115 "initial_cash": 1000000,116 "commission": 0.001,117 "extra_fields": null,118 "optimizer": null,119 "optimizer_params": {},120 "engine": "daily",121 "validation": null122}123```124125- `source`: `"auto"` (recommended, auto-select by code format) / `"tushare"` / `"yfinance"` / `"okx"` / `"akshare"` / `"ccxt"`126 - `"auto"` supports mixed instruments. For example, `["000001.SZ", "BTC-USDT"]` will be automatically routed to `tushare` and `okx`127 - Futures codes (e.g. `"IF2406.CFFEX"`, `"ESZ4"`) and forex pairs (e.g. `"EUR/USD"`) are also auto-routed128- `interval`: candlestick interval, default `"1D"`. Supported values: `"1m"` / `"5m"` / `"15m"` / `"30m"` / `"1H"` / `"4H"` / `"1D"`129 - The annualization factor for minute backtests is inferred automatically from `source` (252 trading days for China A-shares, 365 calendar days for crypto)130 - Minute backtests can be very data-heavy. Recommended limits are no more than 30 days for `1m`, or 1 year for `1H`131- `extra_fields`: China A-shares can use values such as `["pe", "pb", "roe"]`; other markets should use `null`132- `optimizer`: optional, one of `"equal_volatility"` / `"risk_parity"` / `"mean_variance"` / `"max_diversification"` / `null` (equal-weight by default)133- `optimizer_params`: optimizer parameters, such as `{"lookback": 60}`. `mean_variance` additionally supports `{"risk_free": 0.0}`134- `engine`: backtest engine, default `"daily"`. For options strategies, set `"options"` (requires `OptionsSignalEngine`)135- `initial_cash`: default 1,000,000136- `commission`: default 0.1%137- `validation`: optional statistical validation after backtest completes. Omit to skip. Example:138 ```json139 "validation": {140 "monte_carlo": {"n_simulations": 1000},141 "bootstrap": {"n_bootstrap": 1000, "confidence": 0.95},142 "walk_forward": {"n_windows": 5}143 }144 ```145 - `monte_carlo`: permutation test — shuffles trade order to compute p-value (is Sharpe significantly better than random?)146 - `bootstrap`: resamples daily returns to compute Sharpe 95% confidence interval147 - `walk_forward`: splits equity curve into N windows, checks performance consistency148 - Each key is optional — include only the validations you want149 - Can also run standalone on past results: `python -m backtest.validation <run_dir>`150151## Review Criteria152153### Hard Gates (any failure → `passed=false`)1541551. `artifacts/metrics.csv` exists and is non-empty1562. `artifacts/equity.csv` exists and is non-empty1573. `exit_code == 0` (backtest exits normally)1584. The `equity` column in `equity.csv` contains no `NaN` values1595. `trade_count > 0` (zero trades = signal bug)160161### Scoring Rules162163- Successful backtest + complete artifacts + at least 1 trade → `score ≥ 60` → **passed**164- Poor return / low Sharpe alone should not push the score below 60; they are optimization suggestions only165- `score ≥ 60` = `passed=true`166167### Bug Categories (reduce the score)1681691. **Zero trades** (`trade_count=0`): signal-logic bug, conditions may be too strict1702. **Late first trade** (first trade > 2 years after backtest start): data-filtering bug or overly long lookback window1713. **Capital utilization < 50%**: position-management bug, portfolio is flat most of the time1724. **Open position at the end** (positions still open when backtest ends): exit-signal timing bug173174### `action_items` Format175176If improvements are needed after evaluation, write `action_items`:177- Format: `"Change X from A to B"` or `"Add X logic in signal_engine.py"`178- Must be specific down to parameter values, file names, and function names179- At least 2 items180- Examples:181 - `"Change short MA from 5 to 10 days to reduce whipsaw signals"`182 - `"Add stop-loss: force close when loss exceeds 5%"`183 - `"Add volume filter in signal_engine.py: only trigger buy on high volume"`184185## Cross-Market Strategies186187When the user requests a backtest with codes from **different markets** (e.g. `["000001.SZ", "BTC-USDT"]`):188- Set `source: "auto"` in `config.json`189- The `CompositeEngine` handles calendar alignment, shared capital, and per-market rules automatically190- Use volatility-adjusted weights so high-vol assets (crypto) don't dominate the risk budget191- See the [cross-market-strategy](../cross-market-strategy/SKILL.md) skill for per-market parameters, vol-adjustment, and example code192193## Supporting Files194195- [examples.md](examples.md) — example call sequence