Two lines to a DataFrame
The official SDKs wrap the HTTP endpoints into one-line method calls — no URL building, no retry handling, no response parsing.
pip install ashareapinpm install ashareapifrom ashareapi import AShareAPI
cli = AShareAPI() # free endpoints need no key
df = cli.kline("sh600519", "day", 30)
print(df.tail())import { AShareAPI } from 'ashareapi'
const cli = new AShareAPI() // free endpoints need no key
const rows = await cli.kline('sh600519', 'day', 30)
console.log(rows.slice(-5))Paid endpoints (financials / capital flow / dragon-tiger list / factor screening / backtesting) just need your key — everything else stays the same.
Why use the SDK instead of raw requests
Most endpoints return tables; the SDK turns them into a pandas DataFrame. No pandas? It falls back to a list of dicts.
sh600667 / 600667.SH / 600667 all work — the SDK normalises them.
Auth / rate limit / upstream failure / empty result are distinct — empty is not failure, and each tells you what to do.
The Node build uses only native fetch (no third-party deps) and ships TypeScript types with editor autocomplete.
K-line adjustment is fixed server-side — no double-adjusting.
Multi-source switching, caching and monitoring live on the server; a failed fetch rotates to another source.
What you can build with it
The SDK does one thing: turn data into a structure you can use directly. The rest is up to you — these are the common cases.
After `pip install ashareapi`, one variable is one table — plot it, compute factors, write it up.
Call it server-side and feed the DataFrame or JSON into your frontend or BI stack.
Data fetching stays inside the SDK; your strategy code only handles logic.
Pull capital flow or K-lines for a whole batch at once — good for scanning a pool.
Connecting to QMT / Ptrade
Both terminals ship an older built-in Python (QMT 3.6 · Ptrade 3.5, some brokers 3.11) and enforce broker library whitelists and sandboxes, so third-party packages generally will not install. The workable pattern is "fetch outside, read files inside" — your strategy only reads local files and makes no network calls.
Use the SDK to pull data and write local CSVs — this step runs outside the terminal.
QMT / Ptrade bundle pandas, so read the CSV directly — no third-party installs, no network calls.
Data layer and strategy layer stay separate; strategy code does not change with the data source.
# fetch.py —— 在终端【之外】用普通 Python 跑(3.9+)
from ashareapi import AShareAPI
cli = AShareAPI("ct-你的KEY")
codes = ["sh600519", "sz000001"]
for code in codes:
df = cli.kline(code, "day", 250) # 已返回 DataFrame
df.to_csv("%s.csv" % code, index=False)# strategy.py —— 放进 QMT / Ptrade 的策略文件里(只读本地文件)
# ⚠️ 列名以 SDK 实际返回为准:date / open / last / high / low / volume / amount
import pandas as pd
def initialize(context):
# 终端内置 pandas ⇒ 直接读 CSV,不装第三方包、不联网
context.data = pd.read_csv("sh600519.csv")
context.data["ma20"] = context.data["last"].rolling(20).mean()
def handle_data(context, bar):
row = context.data[context.data.date == bar.time]
if len(row) == 0:
return
ma20 = row.iloc[0]["ma20"]
if bar.close > ma20:
order_target_percent("600519.SS", 1.0)Why not run the SDK inside the terminal: the built-in version is below this package’s 3.9 requirement, and whitelists / sandboxes block installs. File exchange sidesteps both while keeping the strategy side dependency-free.
Common methods
The full list and parameters live in the docs; these are the ones you reach for first.
| Method | What it does |
|---|---|
quote(code) | Realtime quote snapshot |
kline(code, period, count) | K-lines (day / week / month / season / year + intraday) |
klineFull(code) | Full history of daily bars in one call |
minute(code, days) | Intraday (today / last 5 days) |
fund(code) | Capital flow + dragon-tiger list + block trades + margin |
lhb(type) | Dragon-tiger boards (institutions / hot money / active seats) |
finance(code) | The three financial statements |
technical(code) | MA / MACD / KDJ / RSI / BOLL |
screen(expr, preset) | Factor screening |
backtest(opts) | Quant backtesting |