对DCE.j2009合约进行回测,测试日期从2020-7-7到2020-7-10,k线周期为15分钟。详细代码在后面。回测结果如下,在22:00的时候最新的crossdown标志是0,但22:15的时候倒数第二个标志位变为1.有点困惑。
from tqsdk import(TargetPosTask,TqSim,TqApi,TqBacktest,ta,tafunc,TqReplay)
from datetime import date
import datetime
import requests
import threading
import numpy as np
import pandas as pd
import time
from tqsdk.tafunc import barlast
# api = TqApi(web_gui=True)
api = TqApi(TqSim(),web_gui=True,backtest=TqBacktest(start_dt=date(2020,7,7),end_dt=date(2020,7,10)))
kline = api.get_kline_serial(“DCE.j2009”,15*60,data_length=500)
while True:
macd = ta.MACD(kline,12,26,9)
kline[“macd_diff”]=macd[“diff”]
kline[“macd_dea”]=macd[“dea”]
kline[“MACD_BAR”] = macd[“bar”]
kline[“ZERO_AXIS”] = 0
#diff上穿dea,成立返回1
kline[“cross_up”]=tafunc.crossup(kline[“macd_diff”], kline[“macd_dea”])
#diff下穿dea,成立返回1
kline[“cross_down”]=tafunc.crossdown(kline[“macd_diff”], kline[“macd_dea”])
if(api.is_changing(kline.iloc[-1],”datetime”) ):
# print(tafunc.time_to_str(kline.iloc[-1][“datetime”]),”cross down last”, kline.iloc[-1][“cross_down”] )
currenttime = tafunc.time_to_str(kline.iloc[-1][“datetime”])
if(kline.iloc[-1][“cross_down”]==1 or kline.iloc[-1][“cross_up”]==1 ):
print(tafunc.time_to_str(kline.iloc[-1][“datetime”]) )
# print(kline.iloc[-2][“macd_diff”], kline.iloc[-2][“macd_dea”],kline.iloc[-1][“macd_diff”], kline.iloc[-1][“macd_dea”])
if (currenttime == ‘2020-07-08 22:00:00.000000’):
print(tafunc.time_to_str(kline.iloc[-1][“datetime”]) )
print( kline[“cross_down”][-10:] )
print(“diff dea”)
print(kline.iloc[-1][“macd_diff”],kline.iloc[-1][“macd_dea”] )
if (currenttime == ‘2020-07-08 22:15:00.000000’):
print(tafunc.time_to_str(kline.iloc[-1][“datetime”]) )
print( kline[“cross_down”][-10:] )
print(“diff dea”)
print(kline.iloc[-1][“macd_diff”],kline.iloc[-1][“macd_dea”] )
kline[“macd_diff.board”]=”MACD”
kline[“macd_diff.color”]=”blue”
kline[“macd_dea.board”]=”MACD”
kline[“macd_dea.color”]=0xFF9933CC
api.wait_update()
2020-07-08 21:30:00.000000 diff dea: 7.9419 7.8145 cross_down: 0
2020-07-08 21:45:00.000000 diff dea: 8.0319 7.858 cross_down: 0
2020-07-08 22:00:00.000000 diff dea: 7.8114 7.8487 cross_down: 1
2020-07-08 22:15:00.000000 diff dea: 7.5098 7.7809 cross_down: 0
2020-07-08 22:30:00.000000 diff dea: 6.6694 7.5586 cross_down: 0
2020-07-08 22:45:00.000000 diff dea: 6.1343 7.2737 cross_down: 0
如果取上一个的crossdown数据,算是正常的吧。
if(api.is_changing(kline.iloc[-1],”datetime”) ):
macd = ta.MACD(kline,12,26,9)
kline[“macd_diff”]=macd[“diff”]
kline[“macd_dea”]=macd[“dea”]
kline[“MACD_BAR”] = macd[“bar”]
kline[“ZERO_AXIS”] = 0
#diff上穿dea,成立返回1
kline[“cross_up”]=tafunc.crossup(kline[“macd_diff”], kline[“macd_dea”])
#diff下穿dea,成立返回1
kline[“cross_down”]=tafunc.crossdown(kline[“macd_diff”], kline[“macd_dea”])
print(tafunc.time_to_str(kline.iloc[-2][“datetime”]),”diff dea:”,
round(kline.iloc[-2][“macd_diff”],4) , ” “,round(kline.iloc[-2][“macd_dea”],4),
“cross_down:”,round(kline.iloc[-2][“cross_down”],4) )
你可以在输出cross down数据的同时把计算cross down的数据数出来比较一下,
cross down的计算公式也可以在tqsdk开源源码中看到
2020-07-08 21:45:00.000000 diff dea: 7.9521 7.842 cross_down: 0
2020-07-08 22:00:00.000000 diff dea: 8.0108 7.8886 cross_down: 0
2020-07-08 22:15:00.000000 diff dea: 7.5497 7.7889 cross_down: 0
2020-07-08 22:30:00.000000 diff dea: 7.1879 7.6623 cross_down: 0
2020-07-08 22:45:00.000000 diff dea: 5.9349 7.2338 cross_down: 0
输出的没显示交叉
crossdown的计算公式如下
def crossdown(a, b):
“””
向下穿越: 表示当a从上方向下穿b,成立返回1, 否则返回0
Args:
a (pandas.Series): 数据序列1
b (pandas.Series): 数据序列2
Returns:
pandas.Series: 下穿标志序列
Example::
from tqsdk import TqApi, TqSim, tafunc
api = TqApi(TqSim())
klines = api.get_kline_serial(“CFFEX.IF1908”, 24 * 60 * 60)
crossdown = tafunc.crossdown(tafunc.ma(klines.close, 5), tafunc.ma(klines.close, 10))
print(list(crossdown))
“””
crossdown_data = pd.Series(np.where((a = b.shift(1)), 1, 0))
return crossdown_data
测试代码:
macd = ta.MACD(kline,12,26,9)
kline[“macd_diff”]=macd[“diff”]
kline[“macd_dea”]=macd[“dea”]
kline[“MACD_BAR”] = macd[“bar”]
kline[“ZERO_AXIS”] = 0
#diff上穿dea,成立返回1
kline[“cross_up”]=tafunc.crossup(kline[“macd_diff”], kline[“macd_dea”])
#diff下穿dea,成立返回1
kline[“cross_down”]=tafunc.crossdown(kline[“macd_diff”], kline[“macd_dea”])
print(tafunc.time_to_str(kline.iloc[-1][“datetime”]),”diff dea:”,
round(kline.iloc[-1][“macd_diff”],4) , ” “,round(kline.iloc[-1][“macd_dea”],4),
“cross_down:”,round(kline.iloc[-1][“cross_down”],4) )