在双均线这里我老是报如下的错误,老师能帮忙看看吗,谢谢

来源:4-5 双均线策略:生成交易信号

慕神7555198

2022-04-07

#生成信号:金叉买入,死叉卖出
data["buy_singal"] = np.where(data["short_ma"] > data["long_ma"], 1, 0)
data["sell_singal"] = np.where(data["short_ma"] < data["long_ma"], -1, 0)"sell_singal"]].head())
data = sw.compose_signal(data)   ##执行这步时出错

报错如下,pandas的版本为1.3.5
Traceback (most recent call last):
File “D:\Program Files\Python37\lib\site-packages\pandas\core\indexes\base.py”, line 3361, in get_loc
return self._engine.get_loc(casted_key)
File “pandas_libs\index.pyx”, line 76, in pandas._libs.index.IndexEngine.get_loc
File “pandas_libs\index.pyx”, line 108, in pandas._libs.index.IndexEngine.get_loc
File “pandas_libs\hashtable_class_helper.pxi”, line 5198, in pandas._libs.hashtable.PyObjectHashTable.get_item
File “pandas_libs\hashtable_class_helper.pxi”, line 5206, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: ‘buy_signal’

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File “D:/study/python/project/pyprogram/strategy/ma_strategy.py”, line 39, in
ma_strategy(df)
File “D:/study/python/project/pyprogram/strategy/ma_strategy.py”, line 32, in ma_strategy
data = sw.compose_signal(data)
File “D:\study\python\project\pyprogram\strategy\week_strategy.py”, line 67, in compose_signal
data[‘buy_signal’] = np.where((data[‘buy_signal’] == 1)
File “D:\Program Files\Python37\lib\site-packages\pandas\core\frame.py”, line 3458, in getitem
indexer = self.columns.get_loc(key)
File “D:\Program Files\Python37\lib\site-packages\pandas\core\indexes\base.py”, line 3363, in get_loc
raise KeyError(key) from err
KeyError: ‘buy_signal’

Process finished with exit code 1

写回答

2回答

慕神7555198

提问者

2022-04-07

谢谢老师指点,的确是这样,前面的列名叫 "buy_singal",但是在compose_signal中叫"buy_signal"

0
0

DeltaF

2022-04-07

说是没有这个字段,打印data看下,会不会拼错之类

0
0

程序员理财课 Python量化交易系统实战

打造一个自动交易平台,新手也能提升理财收益

1987 学习 · 405 问题

查看课程