【代码报错】配对交易报错求帮助
由bqtbe3e7创建,最终由small_q 被浏览 6 用户
import os
from bigmodule import M
import numpy as np
from bigtrader import PerOrder
import pandas as pd
from bigtrader import Direction
from bigtrader import OrderType
from datetime import datetime,timedelta
'''中国平安:A 股代码 601318.SS,H 股代码02318.HK。
中石化:A 股代码 600028.SS,H 股代码00386.HK。
中海油:A 股代码 600938.SS,H 股代码00883.HK。
中国人寿:A 股代码 601628.SS,H 股代码02628.HK。
中国铁建:A 股代码 601186.SS,H 股代码01186.HK。
招商银行:A 股代码 600036.SS,H 股代码03968.HK。
交通银行:A 股代码 601328.SS,H 股代码03328.HK。
中国电信:A 股代码 601728.SS,H 股代码00728.HK。
中国联通:A 股代码 600050.SS,H 股代码00762.HK。
中国神华:A 股代码 601088.SS,H 股代码01088.HK。
中国太保:A 股代码 601601.SS,H 股代码02601.HK。
中信证券:A 股代码 600030.SS,H 股代码06030.HK。
国泰君安:A 股代码 601211.SS,H 股代码02611.HK。
光大证券:A 股代码 601788.SS,H 股代码06178.HK。
中广核电力:A 股代码 601985.SS,H 股代码01816.HK。'''
def initialize(context):
# 系统已经设置了默认的交易手续费和滑点,要修改手续费可使用如下函数
context.set_commission(PerOrder(buy_cost=0.0003, sell_cost=0.0013, min_cost=5))
context.instrument1 = '601318.SH'
context.instrument2 = '02318.HK'
# 定义变量:持仓数量
context.hold_count = 4
context.order_num=1000
# 定义变量:等权重持有
context.weight = 1.0 / context.hold_count
def handle_data(context, data):
instrument1 = context.instrument1
instrument2 = context.instrument2
# 获取过去的历史价格
try:
df = data.history(instrument1, "close", 240, "1m")
df1 = data.history(instrument2, "close", 240, "1m")
# 检查数据是否为空
if df.empty or df1.empty:
return
dfx = np.array(df)[0:-2]
df1x = np.array(df1)[0:-2]
delta = dfx - df1x
meandelta = np.mean(delta)
sigmadelta = np.std(delta)
n = len(df)
sk = (len(df) * (np.sum(delta - meandelta) ** 3)) / ((len(df) - 1) * (len(df) - 2) * sigmadelta ** 3)
kurt = (n * (n + 1) / ((n - 1) * (n - 2) * (n - 3))) * np.sum((delta - meandelta) ** 4) / (sigmadelta ** 4)
upperbound = meandelta + (sigmadelta * 4 / kurt) - sk
lowerbound = meandelta - (sigmadelta * 4 / kurt) - sk
p1 = data.current(instrument1, "close")
p2 = data.current(instrument2, "close")
currentdata = p1 - p2
if currentdata >= upperbound and (currentdata / upperbound) <= 1.05:
rv1 = context.sell_open(instrument1, context.order_num, p1, order_type=OrderType.MARKET)
rv2 = context.buy_open(instrument2, context.order_num, p2, order_type=OrderType.MARKET)
elif currentdata < meandelta:
if context.get_account_position(instrument1, direction=Direction.SHORT).avail_qty > 0:
rv1 = context.buy_close(instrument1, context.order_num, p1, order_type=OrderType.MARKET)
if context.get_account_position(instrument2, direction=Direction.LONG).avail_qty > 0:
rv2 = context.sell_close(instrument2, context.order_num, p2, order_type=OrderType.MARKET)
elif currentdata <= lowerbound and (lowerbound / currentdata) <= 1.05:
rv1 = context.buy_open(instrument1, context.order_num, p1, order_type=OrderType.MARKET)
rv2 = context.sell_open(instrument2, context.order_num, p2, order_type=OrderType.MARKET)
elif currentdata > meandelta:
if context.get_account_position(instrument1, direction=Direction.LONG).avail_qty > 0:
rv1 = context.sell_close(instrument1, context.order_num, p1, order_type=OrderType.MARKET)
if context.get_account_position(instrument2, direction=Direction.SHORT).avail_qty > 0:
rv2 = context.buy_close(instrument2, context.order_num, p2, order_type=OrderType.MARKET)
except Exception as e:
print(f"Error in handle_data: {e}")
m5 = M.bigtrader.v35(
start_date="2025-01-01",
end_date="2025-04-14",
initialize=initialize,
handle_data=handle_data,
capital_base=1000000,
frequency="""minute""",
benchmark="""沪深300指数"""
)
报错信息如下:
[2025-04-15 10:32:01] INFO: bigtrader.v35 开始运行 ..[2025-04-15 10:32:01] INFO: pybacktest run ~ , , equity, instruments=0
您可以去社区论坛问答交流板块反馈咨询 去发帖>>
---------------------------------------------------------------------------
InvalidInputException Traceback (most recent call last)
Cell In[6], line 81
78 except Exception as e:
79 print(f"Error in handle_data: {e}")
---> 81 m5 = M.bigtrader.v35(
82 start_date="2025-01-01",
83 end_date="2025-04-14",
84 initialize=initialize,
85 handle_data=handle_data,
86 capital_base=1000000,
87 frequency="""minute""",
88 benchmark="""沪深300指数"""
89 )
File /opt/pyenv/versions/3.11.8/lib/python3.11/site-packages/bigmodule/modules.py:28, in __call__(self, **kwargs)
File /opt/pyenv/versions/3.11.8/lib/python3.11/site-packages/bigmodule/moduleinvoker.py:203, in module_invoke(name, version, kwargs)
File /opt/pyenv/versions/3.11.8/lib/python3.11/site-packages/bigmodule/moduleinvoker.py:169, in _module_invoke(name, version, kwargs)
File /opt/pyenv/versions/3.11.8/lib/python3.11/site-packages/bigmodule/moduleinvoker.py:41, in _module_run(module, kwargs)
File dist/build/bigtrader/v35/__init__.py:400, in v35.run()
File dist/build/bigtrader/v35/__init__.py:194, in v35._run()
File dist/build/bigtrader/v35/__init__.py:175, in v35._run_backtest()
File dist/build/bigtrader/v35/core/pybacktest/__init__.py:172, in v35.core.pybacktest.BigQuantModule.__init__()
File /opt/pyenv/versions/3.11.8/lib/python3.11/site-packages/vectortrader/rebalanceperiod.py:48, in build(self, start_date, end_date, extra_days)
File /var/app/enabled/dai/_telemetry.py:128, in wrapper(*args, **kwargs)
File /var/app/enabled/dai/_functions.py:152, in df(self)
InvalidInputException: Invalid Input Error: Attempting to execute an unsuccessful or closed pending query result
Error: Invalid Input Error: Failed to cast value: Could not convert string '' to INVALID
\