Quant backtest (single-stock live / portfolio precomputed) API
Run a built-in strategy over history and get return / risk metrics (optionally the equity curve). Two modes: mode=single (default) backtests one stock in real time — pass code + strategy; mode=portfolio looks up a precomputed multi-stock result — pass strategy; results are preset per range (four standard ranges from 2019; an unsupported start/end returns the list of available ranges). Portfolio mode also accepts weighting: equal (default) or inv_vol (inverse-volatility, Institutional tier — steadier names get more, wilder names get less). Costs are included: commission (0.03%, min ¥5), stamp duty (0.05%, sell side), transfer fee (Shanghai) and slippage (0.1%). Constraints are included: price limits (no fill when sealed) and suspensions (no fill without a quote). ⚠️ ST status uses the current list as an approximation (no public source for day-by-day history) — ST names are about 3.6% of the market and the effect is limited to price-limit days. ⚠️ NOT included: market impact and capacity. ⚠️ Past performance does not indicate future results; research use only, not investment advice. Paid tier — Standard (¥29.9) and above, 30 calls/day (Pro ¥99: 300/day · Institutional ¥199: 1,000/day).
GET /v1/backtestGET /v1/backtest?mode=singlecurl "https://api.ashareapi.com/v1/backtest?mode=single&key=YOUR_KEY"mode · strategy · code · start · end · initial_cash · weighting · include_equity* = requiredQuick start
Replace the key below with yours (free endpoints need no key):
# 推荐:Authorization 头(密钥不进日志)
curl -H "Authorization: Bearer YOUR_KEY" \
"https://api.ashareapi.com/v1/backtest?mode=single"
# 快速测试:直接浏览器打开(?key= 会留在日志/历史里,别用于生产)
curl "https://api.ashareapi.com/v1/backtest?mode=single&key=YOUR_KEY"import requests
r = requests.get(
"https://api.ashareapi.com/v1/backtest",
headers={"Authorization": "Bearer YOUR_KEY"},
params={},
timeout=30,
)
print(r.json())const r = await fetch("https://api.ashareapi.com/v1/backtest?mode=single", {
headers: { Authorization: "Bearer YOUR_KEY" },
});
console.log(await r.json());Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
| mode | string | No | single = backtest one stock in real time; portfolio = look up a precomputed multi-stock result |
| strategy | string | No | Built-in strategy name (see /v1/strategies for the full list and parameters) |
| code | string | No | Stock code with market prefix: sh600667 (SH), sz000001 (SZ), hk00700 (HK), usAAPL (US) |
| start | string | No | Backtest start date YYYY-MM-DD |
| end | string | No | Backtest end date YYYY-MM-DD (blank = latest) |
| initial_cash | number | No | Initial capital, default 1,000,000 |
| weighting | string | No | mode=portfolio weighting: equal = equal weight (default) / inv_vol = inverse-volatility (Institutional tier) |
| include_equity | boolean | No | Include the daily equity curve in the response |
modestringstrategystringcodestringstartstringendstringinitial_cashnumberweightingstringinclude_equitybooleanResponse fields
The table below is captured from real API calls (not inferred): each row gives the field name, its type and one real sample value, so you can align your parser.
dataEnvelope: ok / endpoint / tier / elapsed_ms / sourceProbed: 2026-10-20| Field | Type | Real sample value |
|---|---|---|
| mode | str | single |
| code | str | sh600519 |
| strategy | str | buy_hold |
| range | — | |
| cost_model | — | |
| metrics | — | |
| n_trades | num | 1 |
Unified envelope: { ok, endpoint, tier, elapsed_ms, source, data }
Rows live in data; when upstream returns nothing you get ok:false and the call is not counted.
Free endpoints need no key (anonymous 5/min — 250 bars per request, 100k rows per day; solve one PoW challenge for 15/min). Paid tiers: Trial 30 · Standard 120 · Pro 300 · Unlimited 600 per minute; buyout packs are capped by total calls and never expire.
Calls per day: Standard 30 calls · Pro 300 calls · Institutional 1000 calls
Daily quota resets at 00:00 (UTC+8). Check today's remaining calls via /v1/usage (heavy_quotas field).
FAQ
General (applies to every endpoint)
Free endpoints do not: health, challenge, quote, kline, hot, market-overview and changedist work anonymously. Paid endpoints do: send Authorization: Bearer <your key>. ⚠️ The anonymous allowance is tiered by caller type: browsers (humans) get 5/min; scripts, SDKs and AI Agents (curl, requests, axios, openai user-agent strings) get 2/min — automated traffic is easier to abuse. Solving one PoW challenge (GET /v1/challenge, then send the X-PoW header) raises it to 15/min regardless of type. ⚠️ Also, anonymous calls are capped at 250 bars per request and 100k rows per day — use an API key for the full 1212 bars or unlimited daily volume.
No. When the upstream fails or returns nothing you get ok:false and the charge for that call is refunded (total_calls / usage_log / ep_log are rolled back together). Only calls that actually returned data count.
Quotes (quote / kline / orderbook / changedist) are real-time or current session; financials, shareholders, dividends and events are within T+1 of source disclosure. Every response carries its own data timestamp — time (HHMM) on intraday bars, date (trading day) on quotes — so you can check exactly which moment a payload reflects.
No. code is a single-value parameter — one stock per call. For batches, issue concurrent calls and respect your tier per-minute limit.
Set up MCP once, then just ask the AI — it calls the endpoint itself. MCP exposes 94 tools covering quotes, financials, screening, sectors and macro.
Related endpoints
Set up MCP once, then just ask the AI — it calls the endpoint itself.