3.72K 浏览
0
def tqFirstTrade():
    api = TqApi(backtest=TqBacktest(start_dt=date(2019, 12, 1), end_dt=date(2020, 1, 9)),web_gui=True)
    account = api.get_account()
    order = None
    # 一分钟K线
    klines = api.get_kline_serial("SHFE.fu2005", 1800)
    while True:
        while True:
            api.wait_update()
            if api.is_changing(klines):
                ma = sum(klines.close.iloc[-55:]) / 55
                print("最新价", klines.close.iloc[-1], "55倍 MA", ma)
                if klines.close.iloc[-1] > ma:
                    print("最新价大于55倍 MA: 市价开仓")
                    order = api.insert_order(symbol="SHFE.fu2005", direction="BUY", offset="OPEN", volume=1)
                    print("账户权益-------------: %.2f" % (account.balance))
                    break

        while True:
            api.wait_update()
            if api.is_changing(klines):
                ma = sum(klines.close.iloc[-30:]) / 30
                print("最新价", klines.close.iloc[-1], "30倍 MA", ma)
                if klines.close.iloc[-1] < ma:
                    print("最新价小于30倍MA: 市价平仓")
                    order = api.insert_order(symbol="SHFE.fu2005", direction="SELL", offset="CLOSE", volume=1)
                    print("账户权益-------------: %.2f" % (account.balance))
                    break
    pass
    # 关闭api,释放相应资源
    api.close()

最新价 2389.0 30倍 MA 2414.5333333333333
最新价小于30倍MA: 市价平仓
账户权益————-: 10018774.91
最新价 2372.0 55倍 MA 2373.4363636363637
最新价 2372.0 55倍 MA 2376.090909090909
最新价 2332.0 55倍 MA 2375.3636363636365
最新价 2332.0 55倍 MA 2377.490909090909
最新价 2348.0 55倍 MA 2377.7818181818184
WARNING – 回测结束
WARNING – ———– Backtest finished, press [Ctrl + C] to exit. ———–
WARNING – 模拟交易成交记录
WARNING – 时间:2019-11-29 18:00:00.000000,合约:SHFE.fu2005,开平:OPEN,方向:BUY,手数:1,价格:1936.000,手续费:1.20
WARNING – 时间:2019-11-29 21:59:00.000000,合约:SHFE.fu2005,开平:OPEN,方向:BUY,手数:1,价格:1931.000,手续费:1.20
WARNING – 时间:2019-12-04 09:59:00.000000,合约:SHFE.fu2005,开平:OPEN,方向:BUY,手数:1,价格:1911.000,手续费:1.20
WARNING – 时间:2019-12-13 10:14:59.999999,合约:SHFE.fu2005,开平:CLOSE,方向:SELL,手数:1,价格:2064.000,手续费:1.20
WARNING – 时间:2019-12-13 10:14:59.999999,合约:SHFE.fu2005,开平:OPEN,方向:BUY,手数:1,价格:2066.000,手续费:1.20
WARNING – 时间:2019-12-13 11:29:00.000000,合约:SHFE.fu2005,开平:CLOSE,方向:SELL,手数:1,价格:2068.000,手续费:1.20
Error in sys.excepthook:
Traceback (most recent call last):
File “D:\software\Anaconda3\lib\site-packages\tqsdk\exceptions.py”, line 26, in _except_catcher
api.wait_update()
File “D:\software\Anaconda3\lib\site-packages\tqsdk\api.py”, line 877, in wait_update
self._run_until_idle()
File “D:\software\Anaconda3\lib\site-packages\tqsdk\api.py”, line 1555, in _run_until_idle
self._run_once()
File “D:\software\Anaconda3\lib\site-packages\tqsdk\api.py”, line 1548, in _run_once
raise self._exceptions.pop(0)
File “D:\software\Anaconda3\lib\site-packages\tqsdk\sim.py”, line 104, in _run
await self._send_stat_report()
File “D:\software\Anaconda3\lib\site-packages\tqsdk\sim.py”, line 137, in _send_stat_report
self._report()
File “D:\software\Anaconda3\lib\site-packages\tqsdk\sim.py”, line 407, in _report
while cur_close_volume > 0 and opposite_list[0]:
IndexError: list index out of range

Original exception was:
Traceback (most recent call last):
File “D:/data/learn/trade/stockPro/djangoManager/djangoManager/tq.py”, line 202, in <module>
tqFirstTrade()
File “D:/data/learn/trade/stockPro/djangoManager/djangoManager/tq.py”, line 72, in tqFirstTrade
api.wait_update()
File “D:\software\Anaconda3\lib\site-packages\tqsdk\api.py”, line 894, in wait_update
self._run_once()
File “D:\software\Anaconda3\lib\site-packages\tqsdk\api.py”, line 1548, in _run_once
raise self._exceptions.pop(0)
File “D:\software\Anaconda3\lib\site-packages\tqsdk\backtest.py”, line 123, in _run
await self._send_diff()
File “D:\software\Anaconda3\lib\site-packages\tqsdk\backtest.py”, line 217, in _send_diff
raise BacktestFinished(self._api) from None
tqsdk.exceptions.BacktestFinished: 回测结束

Process finished with exit code 1

ringo 已回答的问题 2020年1月10日
0

已知晓bug 下周修复

ding yi 发表新评论 2020年1月10日

那修复了之后,我们是要重新install 天勤的sdk吗?