跳转到主要内容

获取日K线并计算均线

from alphafeed import AlphaFeed

af = AlphaFeed(api_key="your-api-key")

df = af.klines.get("600519.SH", period="1d", count=120, to_dataframe=True)
df["ma5"] = df["close"].rolling(5).mean()
df["ma20"] = df["close"].rolling(20).mean()
print(df[["trade_date", "close", "ma5", "ma20"]].tail(5))

批量获取日K线(带进度条)

symbols = ["600519.SH", "000001.SZ", "601318.SH", "600036.SH", "000858.SZ"]
dfs = af.klines.batch(symbols, period="1d", count=60, to_dataframe=True, show_progress=True)

# 汇总最新收盘价
import pandas as pd
latest = {sym: df["close"].iloc[-1] for sym, df in dfs.items()}
print(pd.Series(latest, name="close").sort_values(ascending=False))
600519.SH    1262.98
000858.SZ     106.83
601318.SH      53.38
600036.SH      35.53
000001.SZ      11.03
Name: close, dtype: float64

获取分钟K线

df = af.klines.get("600519.SH", period="5m", count=5, to_dataframe=True)
print(df[["trade_time", "open", "high", "low", "close", "volume"]])
          trade_time    open    high     low   close  volume
2026-06-08 14:40:00 1263.21 1263.86 1262.11 1262.81     502
2026-06-08 14:45:00 1262.93 1263.48 1260.00 1260.80    1307
2026-06-08 14:50:00 1261.40 1261.80 1260.80 1261.00     544
2026-06-08 14:55:00 1260.88 1261.25 1260.42 1260.78     810
2026-06-08 15:00:00 1260.80 1263.34 1260.80 1262.98     577

全 A 股实时行情

# 支持标的池:CN_Stock / CN_ETF / US_Stock / HK_Stock
df = af.quotes.get(universes=["CN_Stock"], to_dataframe=True)
print(df)
         symbol region  last_price  prev_close    open  ...  ext.name  ext.change_pct  ext.change_amount  ext.amplitude  ext.turnover_rate
0     300522.SZ     CN       11.13       11.61   11.34  ...      世名科技       -0.041344              -0.48       0.068045           0.027931
1     688283.SH     CN       36.00       37.42   36.46  ...      坤恒顺维       -0.037948              -1.42       0.070818           0.013895
2     301509.SZ     CN       26.21       27.44   27.15  ...      金凯生科       -0.044825              -1.23       0.051020           0.035865
3     688183.SH     CN      112.11      117.85  110.00  ...      生益电子       -0.048706              -5.74       0.085023           0.030515
4     300830.SZ     CN        8.94        9.06    8.80  ...       金现代       -0.013245              -0.12       0.052980           0.030392
...         ...    ...         ...         ...     ...  ...       ...             ...                ...            ...                ...
5512  300531.SZ     CN       16.50       15.82   15.98  ...       优博讯        0.042984               0.68       0.068268           0.085419
5513  300078.SZ     CN        3.31        3.33    3.22  ...      思创智联       -0.006006              -0.02       0.066066           0.027491
5514  002638.SZ     CN        4.18        4.31    4.15  ...      勤上股份       -0.030162              -0.13       0.058005           0.020665
5515  300972.SZ     CN      180.80      185.00  184.00  ...      万辰集团       -0.022703              -4.20       0.047568           0.012079
5516  603170.SH     CN       11.89       12.17   11.96  ...      宝立食品       -0.023007              -0.28       0.036976           0.006025

[5517 rows x 18 columns]

按标的查询实时行情

df = af.quotes.get(symbols=["600519.SH", "000001.SZ"], to_dataframe=True)
print(df[["symbol", "last_price", "prev_close", "volume", "ext.name", "ext.change_pct"]])
   symbol  last_price  prev_close  volume ext.name  ext.change_pct
600519.SH     1262.98     1272.86   30828     贵州茅台       -0.007762
000001.SZ       11.03       10.98 1090926     平安银行        0.004554

获取日内分时

df = af.klines.intraday("600519.SH", count=5, to_dataframe=True)
print(df[["symbol", "name", "trade_time", "close", "volume"]])
   symbol name          trade_time   close  volume
600519.SH 贵州茅台 2026-06-08 14:56:00 1263.00     191
600519.SH 贵州茅台 2026-06-08 14:57:00 1262.99     126
600519.SH 贵州茅台 2026-06-08 14:58:00 1263.00       6
600519.SH 贵州茅台 2026-06-08 14:59:00 1263.00       0
600519.SH 贵州茅台 2026-06-08 15:00:00 1262.98     254

批量日内分时

dfs = af.klines.intraday_batch(["600519.SH", "000001.SZ"], to_dataframe=True)
for sym, df in dfs.items():
    print(f"{sym} ({df['name'].iloc[0]}): {len(df)} 条分钟线")

获取标的信息

# 单个标的
inst = af.instruments.get("600519.SH")
print(f"{inst['symbol']}: {inst['name']}")
print(f"  交易所: {inst['exchange']}, 类型: {inst['type']}")
print(f"  上市日期: {inst['ext']['listing_date']}")
print(f"  总股本: {inst['ext']['total_shares']:,.0f}")
600519.SH: 贵州茅台
  交易所: SH, 类型: stock
  上市日期: 2001-08-27
  总股本: 1,250,081,601

批量查询

insts = af.instruments.get(["600519.SH", "000001.SZ", "00700.HK"])
for i in insts:
    print(f"{i['symbol']:>10s}  {i['name']:<6s}  交易所={i['exchange']}  类型={i['type']}  上市={i['ext'].get('listing_date','')}")
 600519.SH  贵州茅台    交易所=SH  类型=stock  上市=2001-08-27
 000001.SZ  平安银行    交易所=SZ  类型=stock  上市=1991-04-03
  00700.HK  腾讯控股    交易所=HK  类型=stock  上市=2004-06-16

获取除权因子

df = af.klines.ex_factors(["600519.SH"], to_dataframe=True)
print(df[["symbol", "trade_date", "ex_factor"]].tail(5))
   symbol trade_date  ex_factor
600519.SH 2023-12-20   1.011540
600519.SH 2024-06-19   1.020716
600519.SH 2024-12-20   1.015637
600519.SH 2025-06-26   1.019649
600519.SH 2025-12-19   1.017003

五档盘口

depth = af.depth.get("600519.SH")
print(f"标的: {depth['symbol']}  地区: {depth['region']}")
for i in range(5):
    bid = f"买{i+1}: {depth['bid_prices'][i]:>10.2f} x {depth['bid_volumes'][i]}"
    ask = f"卖{i+1}: {depth['ask_prices'][i]:>10.2f} x {depth['ask_volumes'][i]}"
    print(f"  {bid}  |  {ask}")
标的: 600519.SH  地区: CN
  买1:    1262.98 x 2  |  卖1:    1262.99 x 7
  买2:    1262.65 x 1  |  卖2:    1263.00 x 44
  买3:    1262.31 x 1  |  卖3:    1263.02 x 1
  买4:    1262.20 x 2  |  卖4:    1263.20 x 2
  买5:    1262.03 x 2  |  卖5:    1263.34 x 1