交易行情数据API

交易数据接口

history_bars -获取BAR数据(日线/分钟线)

history_bars(order_book_ids: str, bar_count: int, frequency: str, dt: datetime.datetime, fields: List[str] = None, skip_suspended: bool = True, include_now: bool = False, adjust_type: str = 'pre', adjust_orig: datetime.datetime = None) → pandas.core.frame.DataFrame[source]

获取指定合约的历史 k 线行情,支持任意日频率xd(1d,5d)和任意分钟频率xm(1m,3m,5m,15m)的历史数据。

Parameters
  • order_book_ids – 多个标的合约代码

  • bar_count – 获取的历史数据数量,必填项

  • frequency – 获取数据什么样的频率进行。’1d’或’1m’分别表示每日和每分钟,必填项

  • fields – 返回数据字段。必填项。见下方列表。

  • skip_suspended – 是否跳过停牌数据

  • include_now – 是否包含当前数据

  • adjust_type – 复权类型,默认为前复权 pre;可选 pre, none, post

fields

字段名

datetime

时间戳

open

开盘价

high

最高价

low

最低价

close

收盘价

volume

成交量

total_turnover

成交额

open_interest

持仓量(期货专用)

basis_spread

期现差(股指期货专用)

settlement

结算价(期货日线专用)

prev_settlement

结算价(期货日线专用)

Example1:

获取中国平安和万科 2020-04-20之前10天的交易数据

import pandas as pd
from simons.api import history_bars

# 
>>> dt = pd.Timestamp("2020-04-20")
>>> fields=["datetime","open","high","low","close","volume"]
>>> data = history_bars(order_book_ids=["000001.XSHE", "000002.XSHE"], dt=dt, bar_count=10, frequency="1d", fields=fields)
>>> print(data)

                               open   high    low  close     volume
order_book_id datetime                                  
000001.XSHE   2020-04-07      12.89  12.94  12.81  12.88    87031371.0
              2020-04-08      12.88  12.92  12.72  12.78    52871614.0
              2020-04-09      12.88  12.89  12.72  12.74    40855377.0
              2020-04-10      12.76  12.98  12.65  12.79    66667495.0
              2020-04-13      12.67  12.71  12.47  12.59    44621440.0
              2020-04-14      12.65  12.86  12.57  12.86    68608687.0
              2020-04-15      12.86  12.93  12.78  12.87    65639640.0
              2020-04-16      12.79  12.79  12.54  12.68    78915498.0
              2020-04-17      12.77  13.04  12.65  12.89   133116477.0
              2020-04-20      12.86  13.05  12.77  12.99    81845583.0
000002.XSHE   2020-04-07      27.34  27.42  26.80  27.07    67154006.0
              2020-04-08      26.90  27.25  26.75  26.96    41251395.0
              2020-04-09      27.10  27.16  26.60  26.69    38726254.0
              2020-04-10      26.84  27.34  26.59  26.88    62460322.0
              2020-04-13      26.74  27.13  26.61  27.04    43264902.0
              2020-04-14      27.10  27.75  27.02  27.35    64241868.0
              2020-04-15      27.20  27.23  26.55  26.70    70359257.0
              2020-04-16      26.52  26.76  26.40  26.58    50238931.0
              2020-04-17      26.78  27.03  26.55  26.72    83813322.0
              2020-04-20      26.78  26.81  26.05  26.58    85012343.0

history_snapshot -获取level2快照数据

history_snapshot(order_book_id: str, bar_count: int, dt: datetime.datetime, fields: List[str] = None, skip_suspended: bool = True, include_now: bool = False, adjust_type: str = 'none', adjust_orig: <module 'datetime' from '/home/docs/.pyenv/versions/3.7.9/lib/python3.7/datetime.py'> = None) → pandas.core.frame.DataFrame[source]

获取指定合约的历史快照数据

Parameters
  • order_book_id – 合约代码

  • bar_count – 获取的历史数据数量,必填项

  • fields – 返回数据字段。必填项。见下方列表。

  • skip_suspended – 是否跳过停牌数据

  • include_now – 是否包含当前数据

  • adjust_type – 复权类型,默认为前复权 pre;可选 pre, none, post

bbla

from simonsc.api import history_snapshot

>>> dt = pd.Timestamp("2020-07-24 14:55:00")
>>> fields=["datetime","last","buy_price_1","buy_volume_1","sell_price_1","sell_volume_1","sell_price_10"]
>>> data = history_snapshot(order_book_id="600446.XSHG", dt=dt, bar_count=10, fields=fields)
>>> print(data)

                                    last   buy_price_1   buy_volume_1   sell_price_1  sell_volume_1  sell_price_10
order_book_id   datetime                                   
600446.XSHG   2020-07-24 14:54:32   19.12     19.12         1100.         19.13,         1500.         19.26
              2020-07-24 14:54:35   19.12     19.11         6600.         19.12,         57900.        19.25
              2020-07-24 14:54:38   19.12     19.11         6800.         19.12,         57800.        19.25
              2020-07-24 14:54:41   19.12     19.11        36400.         19.12,         57200.        19.25
              2020-07-24 14:54:44   19.11     19.11        21200.         19.12,         55900.        19.25
              2020-07-24 14:54:47   19.11     19.11         7400.         19.12,         52200.        19.25
              2020-07-24 14:54:50   19.12     19.11         4700.         19.12,         40800.        19.25
              2020-07-24 14:54:53   19.12     19.12        41800.         19.13,         9700.         19.26
              2020-07-24 14:54:56   19.12     19.12        40900.         19.13,         9700.         19.26
              2020-07-24 14:54:59   19.13     19.12        44000.         19.13,         9600.         19.26

交易日期接口

get_trading_dates - 获取A股某个区间的交易日期

get_trading_dates(start_date: Union[str, datetime.date, datetime.datetime, pandas._libs.tslibs.timestamps.Timestamp], end_date: Union[str, datetime.date, datetime.datetime, pandas._libs.tslibs.timestamps.Timestamp]) → pandas.core.indexes.datetimes.DatetimeIndex[source]

获取A股某个区间的交易日期 :param start_date: 开始日期 :param end_date: 结束如期

Example:

获取2020-05-10至2020-05-20之间的交易日期

from simonsc.api import get_trading_dates

# 
>>> trading_dates = get_trading_dates(start_date = "2020-05-11", end_date="2020-05-20")
>>> print(trading_dates)
DatetimeIndex(['2020-05-11', '2020-05-12', '2020-05-13', '2020-05-14',
       '2020-05-15', '2020-05-18', '2020-05-19', '2020-05-20'],
      dtype='datetime64[ns]', freq=None)

get_previous_trading_date - 获取指定日期的之前的第 n 个交易日

get_previous_trading_date(date: Union[str, datetime.date, datetime.datetime, pandas._libs.tslibs.timestamps.Timestamp], n: int = 1) → pandas._libs.tslibs.timestamps.Timestamp[source]

获取指定日期的之前的第 n 个交易日

Parameters
  • date – 指定日期

  • n – 第 n 个交易日

Example:

2020-05-18之前3天的交易日

from simonsc.api import get_previous_trading_date

>>> get_previous_trading_date(date='2020-05-18', n=3)
Timestamp('2020-05-13 00:00:00')

get_next_trading_date - 获取指定日期之后的第 n 个交易日

get_next_trading_date(date: Union[str, datetime.date, datetime.datetime, pandas._libs.tslibs.timestamps.Timestamp], n: int = 1) → pandas._libs.tslibs.timestamps.Timestamp[source]

获取指定日期之后的第 n 个交易日

Parameters
  • date – 指定日期

  • n – 第 n 个交易日

Example

from simonsc.api import get_next_trading_date

>>> get_next_trading_date(date='2020-05-13', n=3)
Timestamp('2020-05-18 00:00:00')

日期接口