8.47K 浏览
3

根据https://doc.shinnytech.com/tqsdk/latest/usage/mddatas.html#k

简单实验一下:

from datetime import datetime, date
from contextlib import closing
from tqsdk import TqApi, TqSim
from tqsdk.tools import DataDownloader
import time
api = TqApi(TqSim())
 symbol = 'cu1901'
market = 'SHFE'
scale = 'day'
code_tq = f'{market}.{symbol}'
 df = api.get_kline_serial('SHFE.cu1901', 24*60*60) # 86400秒线, 即日线
with closing(api):
df['datetime'] = df['datetime'].apply(lambda x: datetime.fromtimestamp(x/1e9))
print(df)

稍微转换了一下datetime字段显示,得到的结果是:

    id datetime open … close_oi symbol duration
0 43.0 2018-03-23 52410.0 … 17084.0 SHFE.cu1901 86400
1 44.0 2018-03-26 51580.0 … 16762.0 SHFE.cu1901 86400
2 45.0 2018-03-27 50410.0 … 16444.0 SHFE.cu1901 86400
3 46.0 2018-03-28 50980.0 … 16420.0 SHFE.cu1901 86400
4 47.0 2018-03-29 51030.0 … 16282.0 SHFE.cu1901 86400
5 48.0 2018-03-30 51200.0 … 16216.0 SHFE.cu1901 86400
6 49.0 2018-04-02 51760.0 … 16390.0 SHFE.cu1901 86400
7 50.0 2018-04-03 51680.0 … 16504.0 SHFE.cu1901 86400
8 51.0 2018-04-04 51790.0 … 16432.0 SHFE.cu1901 86400
9 52.0 2018-04-09 51390.0 … 20968.0 SHFE.cu1901 86400

为什么id从43开始,而不是从0 1开始?

而且实际上2018-03-23 也确实是cu1901 开盘后的42天了

这是bug,还是有意为之呢? 如何得到每个合约从挂牌日当天开始后的全部行情?

看了一下https://doc.shinnytech.com/tqsdk/latest/reference/tqsdk.api.html

搞定了,需要设定data_length。 但是设定成最大的8964(好奇怪的数字-_-)

df = api.get_kline_serial('SHFE.cu1901', duration_seconds=24*60*60, data_length=8964) # 86400秒线, 即日线
#df = api.get_kline_serial('SHFE.cu1901', 60*60) # 3600秒线, 即1h
with closing(api):
df['datetime'] = df['datetime'].apply(lambda x: datetime.fromtimestamp(x/1e9))
df = df.dropna()
print(df)

但是还需要dropna  终于得到了从头开始的结果

   id datetime open … close_oi symbol duration
8721 0.0 2018-01-16 56210.0 … 142.0 SHFE.cu1901 86400
8722 1.0 2018-01-17 55340.0 … 140.0 SHFE.cu1901 86400
8723 2.0 2018-01-18 55240.0 … 134.0 SHFE.cu1901 86400
8724 3.0 2018-01-19 55080.0 … 166.0 SHFE.cu1901 86400
8725 4.0 2018-01-22 54980.0 … 304.0 SHFE.cu1901 86400
8726 5.0 2018-01-23 55110.0 … 416.0 SHFE.cu1901 86400
8727 6.0 2018-01-24 54510.0 … 378.0 SHFE.cu1901 86400
8728 7.0 2018-01-25 54500.0 … 458.0 SHFE.cu1901 86400
8729 8.0 2018-01-26 55390.0 … 482.0 SHFE.cu1901 86400
8730 9.0 2018-01-29 55020.0 … 516.0 SHFE.cu1901 86400
8731 10.0 2018-01-30 55200.0 … 496.0 SHFE.cu1901 86400

庆晗 许 编辑问题 2019年11月9日