103 views
0

from datetime import date, datetime
 from tqsdk import BacktestFinished, TqSim, TqBacktest, TqApi
api = TqApi()
# api = TqApi(TqSim(), backtest=TqBacktest(start_dt=datetime(2019, 11, 1,), end_dt=datetime(2019, 11, 11)))
# 获得 m1901 5分钟K线的引用
import time
klines = api.get_kline_serial("DCE.i2001", 86400)
# 处理时间
date1 = time.strptime('2019-10-02', "%Y-%m-%d")
date2 = time.strptime(str(datetime.now().date()), "%Y-%m-%d")
date1 = datetime(date1[0], date1[1], date1[2])
date2 = datetime(date2[0], date2[1], date2[2])
dy = (date2 - date1).days
dt1 = '2019-10-02' + ' 00:00:00'
ts1 = int(time.mktime(time.strptime(dt1, "%Y-%m-%d %H:%M:%S")))
dt2 = '2019-11-02' + ' 00:00:00'
ts2 = int(time.mktime(time.strptime(dt2, "%Y-%m-%d %H:%M:%S")))
 # a = int(time_type)*60
klines = api.get_kline_serial('DCE.i2001', 86400, dy)
# klines1 = klines.set_index('datetime')
print(ts1)
print(ts2)
klines['datetime'] = klines['datetime'].apply(lambda x: float(x)*10**(-9))
print(klines)
a = klines[(klines['datetime'] >= ts1) & (klines['datetime'] <= ts2)]
print(a)

自己写的自定义回测历史数据

Asked question