3.40K 浏览
0

为啥分别赋值DL=30和DL=60/2时,程序运行结果不一样呢?

程序如下:

#函数说明https://doc.shinnytech.com/pysdk/latest/reference/tqsdk.ta.html
#https://doc.shinnytech.com/pysdk/latest/reference/tqsdk.tafunc.html
#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = ‘chengzhi’

from datetime import date
from tqsdk import TqApi, TqBacktest, TargetPosTask,BacktestFinished,TqSim,TqAccount,tafunc
#from tqsdk.ta import *
#from tqsdk.tafunc import *

”’
如果当前价格大于5分钟K线的MA15则开多仓
如果小于则平仓
回测从 2018-05-01 到 2018-10-01
”’
#acc = TqSim()

# 在创建 api 实例时传入 TqBacktest 就会进入回测模式
#api = TqApi(TqAccount(“快期模拟”, “744995808@qq.com”, “wenlhznt”),backtest=TqBacktest(start_dt=date(2018, 5, 1), end_dt=date(2018, 5, 8)))
#acc=TqAccount(“快期模拟”, “744995808@qq.com”, “wenlhznt”)
acc=TqSim()
ZQ=5
#????DL=60/2
DL=30
try:
api = TqApi(acc,backtest=TqBacktest(start_dt=date(2018, 5, 1), end_dt=date(2018, 5, 8)))
account = api.get_account()
# 获得 m1901 5分钟K线的引用
klines = api.get_kline_serial(“DCE.m1901”, ZQ * 60, data_length=DL+2)
# 创建 m1901 的目标持仓 task,该 task 负责调整 m1901 的仓位到指定的目标仓位
target_pos = TargetPosTask(api, “DCE.m1901”)

while True:
api.wait_update()
position = api.get_position(“DCE.m1901”)
if api.is_changing(klines):
ma = sum(klines.close.iloc[-(DL+0):]) / DL
hhv1=tafunc.hhv(klines.high,5)
llv1=tafunc.llv(klines.low,5)
# print(“最新价”, klines.close.iloc[-2], “MA”, ma)
if klines.close.iloc[-1] > ma and position.volume_long==0 and position.volume_short==0:
#if klines.high.iloc[-1] >=hhv1 and position.volume_long==0 and position.volume_short==0:
#HH=tafunc.hhv(klines.high,DL)
#HH1=klines.high.iloc[-1]
#print(“最高价HH1:%.2f”% HH1)
#CC1=klines.close.iloc[-1]
#print(“收盘价CC1:%.2f”% CC1)
print(“最新价:%.2f”% klines.close.iloc[-1], “MA:%.2f”% ma,”最新价大于MA: 目标多头5手”)
# 设置目标持仓为多头5手
target_pos.set_target_volume(5)
api.wait_update()
elif klines.close.iloc[-1] < ma and position.volume_long!=0:
#elif klines.low.iloc[-1] <= llv1 and position.volume_long!=0:
print(“最新价:%.2f” % klines.close.iloc[-1], “MA:%.2f” % ma,”最新价小于MA: 目标空仓”)
# 设置目标持仓为空仓
target_pos.set_target_volume(0)
api.wait_update()
print(“可用资金: %.2f” % (account.available))
print(“账户权益: %.2f” % (account.balance))
if klines.close.iloc[-1] < ma and position.volume_short==0 and position.volume_long==0:
#if klines.low.iloc[-1] <= llv1 and position.volume_short==0 and position.volume_long==0:
print(“最新价:%.2f”% klines.close.iloc[-1], “MA:%.2f”% ma,”最新价小于MA: 目标空头5手”)
# 设置目标持仓为空头5手
target_pos.set_target_volume(-5)
api.wait_update()
elif klines.close.iloc[-1] > ma and position.volume_short!=0:
#elif klines.high.iloc[-1] >= hhv1 and position.volume_short!=0:
print(“最新价:%.2f”% klines.close.iloc[-1], “MA:%.2f”% ma,”最新价大于MA: 目标空仓”)
# 设置目标持仓为空仓
target_pos.set_target_volume(0)
api.wait_update()
print(“可用资金: %.2f” % (account.available))
print(“账户权益: %.2f” % (account.balance))
except BacktestFinished as e:
#if position.volume_short!=0 or position.volume_long!=0:
# 设置目标持仓为空仓
target_pos.set_target_volume(0)
#api.wait_update()
api.close()
#?if api.is_changing(account):
#?elif position.volume_long==0 and position.volume_short==0:
#?if position.volume_long==0 and position.volume_short==0:
print(“测试结束,目标空仓”)
print(“可用资金: %.2f” % (account.available))
print(“账户权益: %.2f” % (account.balance))
print(acc.trade_log)
# 回测结束时会执行这里的代码
#print(“可用资金: %.2f” % (account.available))
#print(acc.trade_log)
##################
DL=30时的运行结果:

WARNING – 回测结束
WARNING – 模拟交易成交记录
WARNING – 时间:2018-04-30 18:00:00.000000,合约:DCE.m1901,开平:OPEN,方向:BUY,手数:5,价格:3203.000,手续费:7.50
WARNING – 时间:2018-05-02 13:59:00.000000,合约:DCE.m1901,开平:CLOSE,方向:SELL,手数:5,价格:3257.000,手续费:7.50
WARNING – 时间:2018-05-02 13:59:00.000000,合约:DCE.m1901,开平:OPEN,方向:SELL,手数:5,价格:3257.000,手续费:7.50
WARNING – 时间:2018-05-02 14:04:00.000000,合约:DCE.m1901,开平:CLOSE,方向:BUY,手数:5,价格:3264.000,手续费:7.50
WARNING – 时间:2018-05-02 14:04:00.000000,合约:DCE.m1901,开平:OPEN,方向:BUY,手数:5,价格:3264.000,手续费:7.50
WARNING – 时间:2018-05-02 14:44:00.000000,合约:DCE.m1901,开平:CLOSE,方向:SELL,手数:5,价格:3256.000,手续费:7.50
WARNING – 时间:2018-05-02 14:44:00.000000,合约:DCE.m1901,开平:OPEN,方向:SELL,手数:5,价格:3256.000,手续费:7.50
WARNING – 时间:2018-05-02 22:14:00.000000,合约:DCE.m1901,开平:CLOSE,方向:BUY,手数:5,价格:3257.000,手续费:7.50
WARNING – 时间:2018-05-02 22:14:00.000000,合约:DCE.m1901,开平:OPEN,方向:BUY,手数:5,价格:3257.000,手续费:7.50
WARNING – 时间:2018-05-02 22:54:00.000000,合约:DCE.m1901,开平:CLOSE,方向:SELL,手数:5,价格:3252.000,手续费:7.50
WARNING – 时间:2018-05-02 22:54:00.000000,合约:DCE.m1901,开平:OPEN,方向:SELL,手数:5,价格:3252.000,手续费:7.50
WARNING – 时间:2018-05-03 10:59:00.000000,合约:DCE.m1901,开平:CLOSE,方向:BUY,手数:5,价格:3255.000,手续费:7.50
WARNING – 时间:2018-05-03 10:59:00.000000,合约:DCE.m1901,开平:OPEN,方向:BUY,手数:5,价格:3255.000,手续费:7.50
WARNING – 时间:2018-05-03 14:34:00.000000,合约:DCE.m1901,开平:CLOSE,方向:SELL,手数:5,价格:3254.000,手续费:7.50
WARNING – 时间:2018-05-03 14:34:00.000000,合约:DCE.m1901,开平:OPEN,方向:SELL,手数:5,价格:3254.000,手续费:7.50
WARNING – 时间:2018-05-03 14:44:00.000000,合约:DCE.m1901,开平:CLOSE,方向:BUY,手数:5,价格:3259.000,手续费:7.50
WARNING – 时间:2018-05-03 14:44:00.000000,合约:DCE.m1901,开平:OPEN,方向:BUY,手数:5,价格:3259.000,手续费:7.50
WARNING – 时间:2018-05-03 14:49:00.000000,合约:DCE.m1901,开平:CLOSE,方向:SELL,手数:5,价格:3255.000,手续费:7.50
WARNING – 时间:2018-05-03 14:49:00.000000,合约:DCE.m1901,开平:OPEN,方向:SELL,手数:5,价格:3255.000,手续费:7.50
WARNING – 时间:2018-05-03 14:54:00.000000,合约:DCE.m1901,开平:CLOSE,方向:BUY,手数:5,价格:3258.000,手续费:7.50
WARNING – 时间:2018-05-03 14:54:00.000000,合约:DCE.m1901,开平:OPEN,方向:BUY,手数:5,价格:3258.000,手续费:7.50
WARNING – 时间:2018-05-03 14:59:00.000000,合约:DCE.m1901,开平:CLOSE,方向:SELL,手数:5,价格:3257.000,手续费:7.50
WARNING – 时间:2018-05-03 14:59:00.000000,合约:DCE.m1901,开平:OPEN,方向:SELL,手数:5,价格:3257.000,手续费:7.50
WARNING – 时间:2018-05-04 11:04:00.000000,合约:DCE.m1901,开平:CLOSE,方向:BUY,手数:5,价格:3230.000,手续费:7.50
WARNING – 时间:2018-05-04 11:04:00.000000,合约:DCE.m1901,开平:OPEN,方向:BUY,手数:5,价格:3230.000,手续费:7.50
WARNING – 时间:2018-05-04 11:19:00.000000,合约:DCE.m1901,开平:CLOSE,方向:SELL,手数:5,价格:3225.000,手续费:7.50
WARNING – 时间:2018-05-04 11:19:00.000000,合约:DCE.m1901,开平:OPEN,方向:SELL,手数:5,价格:3225.000,手续费:7.50
WARNING – 时间:2018-05-04 11:19:00.000000,合约:DCE.m1901,开平:CLOSE,方向:BUY,手数:5,价格:3227.000,手续费:7.50
WARNING – 时间:2018-05-04 11:24:00.000000,合约:DCE.m1901,开平:OPEN,方向:SELL,手数:5,价格:3224.000,手续费:7.50
WARNING – 时间:2018-05-04 11:29:00.000000,合约:DCE.m1901,开平:CLOSE,方向:BUY,手数:5,价格:3229.000,手续费:7.50
WARNING – 时间:2018-05-04 11:29:00.000000,合约:DCE.m1901,开平:OPEN,方向:BUY,手数:5,价格:3229.000,手续费:7.50
WARNING – 时间:2018-05-04 14:39:00.000000,合约:DCE.m1901,开平:CLOSE,方向:SELL,手数:5,价格:3228.000,手续费:7.50
WARNING – 时间:2018-05-04 14:39:00.000000,合约:DCE.m1901,开平:OPEN,方向:SELL,手数:5,价格:3228.000,手续费:7.50
WARNING – 时间:2018-05-04 14:44:00.000000,合约:DCE.m1901,开平:CLOSE,方向:BUY,手数:5,价格:3235.000,手续费:7.50
WARNING – 时间:2018-05-04 14:44:00.000000,合约:DCE.m1901,开平:OPEN,方向:BUY,手数:5,价格:3235.000,手续费:7.50
WARNING – 时间:2018-05-04 22:14:00.000000,合约:DCE.m1901,开平:CLOSE,方向:SELL,手数:5,价格:3242.000,手续费:7.50
WARNING – 时间:2018-05-04 22:14:00.000000,合约:DCE.m1901,开平:OPEN,方向:SELL,手数:5,价格:3242.000,手续费:7.50
WARNING – 时间:2018-05-07 09:54:00.000000,合约:DCE.m1901,开平:CLOSE,方向:BUY,手数:5,价格:3239.000,手续费:7.50
WARNING – 时间:2018-05-07 09:54:00.000000,合约:DCE.m1901,开平:OPEN,方向:BUY,手数:5,价格:3239.000,手续费:7.50
WARNING – 时间:2018-05-07 10:09:00.000000,合约:DCE.m1901,开平:CLOSE,方向:SELL,手数:5,价格:3233.000,手续费:7.50
WARNING – 时间:2018-05-07 10:09:00.000000,合约:DCE.m1901,开平:OPEN,方向:SELL,手数:5,价格:3233.000,手续费:7.50
WARNING – 时间:2018-05-07 13:34:00.000000,合约:DCE.m1901,开平:CLOSE,方向:BUY,手数:5,价格:3232.000,手续费:7.50
WARNING – 时间:2018-05-07 13:39:00.000000,合约:DCE.m1901,开平:OPEN,方向:BUY,手数:5,价格:3233.000,手续费:7.50
WARNING – 时间:2018-05-07 13:49:00.000000,合约:DCE.m1901,开平:CLOSE,方向:SELL,手数:5,价格:3226.000,手续费:7.50
WARNING – 时间:2018-05-07 13:49:00.000000,合约:DCE.m1901,开平:OPEN,方向:SELL,手数:5,价格:3226.000,手续费:7.50
WARNING – 模拟交易账户资金
WARNING – 日期:2018-05-01,账户权益:9999992.50,可用资金:9993220.00,浮动盈亏:0.00,持仓盈亏:0.00,平仓盈亏:0.00,保证金:6772.50,手续费:7.50,风险度:0.07%
WARNING – 日期:2018-05-02,账户权益:10001847.50,可用资金:9995075.00,浮动盈亏:-50.00,持仓盈亏:-50.00,平仓盈亏:1950.00,保证金:6772.50,手续费:45.00,风险度:0.07%
WARNING – 日期:2018-05-03,账户权益:10000627.50,可用资金:9993855.00,浮动盈亏:0.00,持仓盈亏:0.00,平仓盈亏:-1100.00,保证金:6772.50,手续费:120.00,风险度:0.07%
WARNING – 日期:2018-05-04,账户权益:10000887.50,可用资金:9994115.00,浮动盈亏:0.00,持仓盈亏:0.00,平仓盈亏:350.00,保证金:6772.50,手续费:90.00,风险度:0.07%
WARNING – 日期:2018-05-07,账户权益:10001712.50,可用资金:9994940.00,浮动盈亏:1000.00,持仓盈亏:1000.00,平仓盈亏:-100.00,保证金:6772.50,手续费:75.00,风险度:0.07%
WARNING – 日期:2018-05-08,账户权益:10004712.50,可用资金:9997940.00,浮动盈亏:4000.00,持仓盈亏:3000.00,平仓盈亏:0.00,保证金:6772.50,手续费:0.00,风险度:0.07%
WARNING – 胜率:22.73%,盈亏额比例:4.41,收益率:0.05%,年化收益率:1.98%,最大回撤:0.01%,年化夏普率:-2.5096
INFO – 模拟交易下单 497456908c6759c796566c25fc8641c7: 时间:2018-05-08 14:59:00.000000,合约:DCE.m1901,开平:CLOSE,方向:BUY,手数:5,价格:3147.0
INFO – 模拟交易委托单 497456908c6759c796566c25fc8641c7: 全部成交
测试结束,目标空仓
可用资金: 9997940.00
账户权益: 10004712.50

DL=60/2时的运行结果:

============== RESTART: E:\Python\Python37-32\示例\backtest-A.py ==============
Traceback (most recent call last):
File “E:\Python\Python37-32\示例\backtest-A.py”, line 38, in <module>
ma = sum(klines.close.iloc[-(DL+0):]) / DL
File “E:\Python\Python37-32\lib\site-packages\pandas\core\indexing.py”, line 1424, in __getitem__
return self._getitem_axis(maybe_callable, axis=axis)
File “E:\Python\Python37-32\lib\site-packages\pandas\core\indexing.py”, line 2137, in _getitem_axis
return self._get_slice_axis(key, axis=axis)
File “E:\Python\Python37-32\lib\site-packages\pandas\core\indexing.py”, line 1307, in _get_slice_axis
indexer = self._convert_slice_indexer(slice_obj, axis)
File “E:\Python\Python37-32\lib\site-packages\pandas\core\indexing.py”, line 279, in _convert_slice_indexer
return ax._convert_slice_indexer(key, kind=self.name)
File “E:\Python\Python37-32\lib\site-packages\pandas\core\indexes\base.py”, line 3167, in _convert_slice_indexer
self._validate_indexer(“slice”, key.start, kind),
File “E:\Python\Python37-32\lib\site-packages\pandas\core\indexes\base.py”, line 5071, in _validate_indexer
self._invalid_indexer(form, key)
File “E:\Python\Python37-32\lib\site-packages\pandas\core\indexes\base.py”, line 3340, in _invalid_indexer
form=form, klass=type(self), key=key, kind=type(key)
TypeError: cannot do slice indexing on <class ‘pandas.core.indexes.range.RangeIndex’> with these indexers [-30.0] of <class ‘float’>
>>>

west 已回答的问题 2020年3月10日
0

在最后一句有报错信息的:

TypeError: cannot do slice indexing on <class ‘pandas.core.indexes.range.RangeIndex’> with these indexers [-30.0] of <class ‘float’>

类型错误,60/2是float类型不是int

hhv()第二个参数接收的是int类型数字,参考文档:

https://doc.shinnytech.com/tqsdk/latest/reference/tqsdk.tafunc.html#tqsdk.tafunc.hhv

west 已回答的问题 2020年3月10日
您正在查看1个答案中的1个,单击此处查看所有答案。