3.48K 浏览
0

我是小白,仿照双均线写了一个,运行时程序异常,提示是:

2019/10/29-22:39:03,”程序异常Traceback (most recent call last):
File “”C:\TianQin\python\lib\site-packages\pandas\core\indexes\base.py””, line 2890, in get_loc
return self._engine.get_loc(key)
File “”pandas\_libs\index.pyx””, line 107, in pandas._libs.index.IndexEngine.get_loc
File “”pandas\_libs\index.pyx””, line 131, in pandas._libs.index.IndexEngine.get_loc
File “”pandas\_libs\hashtable_class_helper.pxi””, line 1607, in pandas._libs.hashtable.PyObjectHashTable.get_item
File “”pandas\_libs\hashtable_class_helper.pxi””, line 1614, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: (<built-in function min>, 420)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File “”C:\TianQin\strategies\TEST.py””, line 28, in <module>
RSV=(klines[“”close””]-klines[min,M])/(klines[max,M]-klines[min,M])
File “”C:\TianQin\python\lib\site-packages\pandas\core\frame.py””, line 2975, in __getitem__
indexer = self.columns.get_loc(key)
File “”C:\TianQin\python\lib\site-packages\”
2019/10/29-22:38:23,策略开始运行

代码部分是:
找不到错误原因,有劳大神指点修改一下。

双均线策略
'''
from tqsdk import TqApi, TqSim, TargetPosTask#种类:调整目标持仓任务
from tqsdk.tafunc import sma
 M = 60*7  # 参数M
N = 60*9  # 参数N
W = 60*2  # 参数W
SYMBOL = "SHFE.bu2001"  # 合约代码
 api = TqApi()
print("策略开始运行")
 data_length = N + 2  # k线数据长度
# "duration_seconds=60"为一分钟线, 日线的duration_seconds参数为: 24*60*60
klines = api.get_kline_serial(SYMBOL, duration_seconds=60, data_length=data_length)
target_pos = TargetPosTask(api, SYMBOL)
 while True:
    api.wait_update()
     if api.is_changing(klines.iloc[-1], "datetime"):  # 产生新k线:重新计算sma
        RSV=(klines["close"]-klines[min,M])/(klines[max,M]-klines[min,M])
        Y0 = sma(RSV,N,1)  # 短周期
        Y1 = sma(Y0,W,1)  # 长周期
         # 均线下穿,做空
        if Y1.iloc[-2] < Y0.iloc[-2] and Y1.iloc[-1] > Y0.iloc[-1]:
            target_pos.set_target_volume(-3)
            print("均线下穿,做空")
         # 均线上穿,做多
        if Y0.iloc[-2] < Y1.iloc[-2] and Y0.iloc[-1] > Y1.iloc[-1]:
            target_pos.set_target_volume(3)
            print("均线上穿,做多")

west 已回答的问题 2019年11月5日
0

是这句话出现问题

RSV=(klines["close"]-klines[min,M])/(klines[max,M]-klines[min,M])

klines是DataFrame类型,检查、改一下它的取值方式

west 编辑答案 2019年11月5日