Python 交易信号名称错误
Ichimoku Kinko Hyo 或 Ichimoku 不仅仅是一种策略,而且是一个完整的交易系统。它建立在价格柱的基础上来生成信号。该系统由 Goichi Hosoda 于 1930 年代末左右开发,在日本和世界其他地区广泛流行。
def ichimoku_kinko_hyo(Data):
# Tenkan-sen (Conversion Line): (9-period high + 9-period low)/2))
nine_period_high = Data['High'].rolling(window= 9).max()
nine_period_low = Data['Low'].rolling(window= 9).min()
Data['tenkan_sen'] = (nine_period_high + nine_period_low) /2
tenkan_sen= Data['tenkan_sen']
# Kijun-sen (Base Line): (26-period high + 26-period low)/2))
period26_high = Data['High'].rolling(window=26).max()
period26_low = Data['Low'].rolling(window=26).min()
Data['kijun_sen'] = (period26_high + period26_low) / 2
kijun_sen= Data['kijun_sen']
# Senkou Span A (Leading Span A): (Conversion Line + Base Line)/2))
Data['senkou_span_a'] = ((Data['tenkan_sen'] + Data['kijun_sen']) / 2).shift(26)
senkou_span_a= Data['senkou_span_a']
# Senkou Span B (Leading Span B): (52-period high + 52-period low)/2))
period52_high = Data['High'].rolling(window=52).max()
period52_low = Data['Low'].rolling(window=52).min()
Data['senkou_span_b'] = ((period52_high + period52_low) / 2).shift(26)
senkou_span_b= Data['senkou_span_b']
# The most current closing price plotted 26 time periods behind (optional)
Data['chikou_span'] = Data['Close'].shift(-26)
chikou_span= Data['chikou_span']
return Data
#our Dataset of "ethereum" downloaded from Yahoo Finance
ETH = pd.read_csv("ETH-USD.csv",index_col="Date", parse_dates=True)
ichimoku_kinko_hyo(ETH)
def signal(Data, buy, sell):
for i in range(len(Data)):
if Data[i, tenkan_column] > Data[i, kijun_column] and Data[i - 1, tenkan_column] < Data[i - 1, kijun_column] and \
Data[i, closing_price_column] > Data[i, senkou_span_a_column] and Data[i, closing_price_column] > Data[i, senkou_span_b_column] and \
Data[i - 26, chikou_column] > Data[i - 26, closing_price_column]:
Data[i, buy] = 1
if Data[i, tenkan_column] < Data[i, kijun_column] and Data[i - 1, tenkan_column] > Data[i - 1, kijun_column] and \
Data[i, closing_price_column] < Data[i, senkou_span_a_column] and Data[i, closing_price_column] < Data[i, senkou_span_b_column] and \
Data[i - 26, chikou_column] < Data[i - 26, closing_price_column]:
Data[i, sell] = -1
# The tenkan_column is the index of the Tenkan-Sen Column
# The kijun_column is the index of the Kijun-Sen Column
# The senkou_span_a_column is the index of the Senkou-Span A Column
# The senkou_span_b_column is the index of the Senkou-Span B Column
# The chikou_column is the index of the Chikou-Span Column
# The closing_price_column is the index of the Close Column
Here are the conditions of a trade based on the Ichimoku system, followed by the signal function that tells Python to print 1 in case a bullish signal is generated and to print -1 in case a bearish signal is generated.
Go long (Buy): Whenever the Tenkan-sen crosses the Kijun-sen from the below to the above while the market price is above the Ichimoku cloud. And finally, the Chikou span’s last value must be higher than the corresponding market price in the same point in time.
Go short (Sell): Whenever the Tenkan-sen crosses the Kijun-sen from the above to the below while the market price is below the Ichimoku cloud. And finally, the Chikou span’s last value must be lower than the corresponding market price in the same point in time.
ichimoku_kinko_hyo(ETH)
signal(ETH)
我的问题是如何解决我遇到的这个 keyError 如果有人能帮助我,我将非常感激!
如果您想知道,这是我的数据集头:
Date Open High Low Close Adj Close Volume
2021-02-22 1935.557861 1936.453735 1580.626587 1781.992920 1781.992920 42409646036
2021-02-23 1781.409058 1781.409058 1378.840942 1570.203979 1570.203979 52029864713
2021-02-24 1571.476440 1710.983765 1511.018921 1626.575684 1626.575684 31329000537
2021-02-25 1625.393921 1670.224121 1465.058960 1475.703735 1475.703735 24481681873
2021-02-26 1478.653320 1559.028931 1407.979248 1446.033691 1446.033691 31435997881
Ichimoku Kinko Hyo or Ichimoku is not just a strategy, but a full trading system. It builds on price bars to generate its signals. Developed around the late 1930’s by Goichi Hosoda, the system grew to be widely popular in Japan and other locations around the world.
def ichimoku_kinko_hyo(Data):
# Tenkan-sen (Conversion Line): (9-period high + 9-period low)/2))
nine_period_high = Data['High'].rolling(window= 9).max()
nine_period_low = Data['Low'].rolling(window= 9).min()
Data['tenkan_sen'] = (nine_period_high + nine_period_low) /2
tenkan_sen= Data['tenkan_sen']
# Kijun-sen (Base Line): (26-period high + 26-period low)/2))
period26_high = Data['High'].rolling(window=26).max()
period26_low = Data['Low'].rolling(window=26).min()
Data['kijun_sen'] = (period26_high + period26_low) / 2
kijun_sen= Data['kijun_sen']
# Senkou Span A (Leading Span A): (Conversion Line + Base Line)/2))
Data['senkou_span_a'] = ((Data['tenkan_sen'] + Data['kijun_sen']) / 2).shift(26)
senkou_span_a= Data['senkou_span_a']
# Senkou Span B (Leading Span B): (52-period high + 52-period low)/2))
period52_high = Data['High'].rolling(window=52).max()
period52_low = Data['Low'].rolling(window=52).min()
Data['senkou_span_b'] = ((period52_high + period52_low) / 2).shift(26)
senkou_span_b= Data['senkou_span_b']
# The most current closing price plotted 26 time periods behind (optional)
Data['chikou_span'] = Data['Close'].shift(-26)
chikou_span= Data['chikou_span']
return Data
#our Dataset of "ethereum" downloaded from Yahoo Finance
ETH = pd.read_csv("ETH-USD.csv",index_col="Date", parse_dates=True)
ichimoku_kinko_hyo(ETH)
def signal(Data, buy, sell):
for i in range(len(Data)):
if Data[i, tenkan_column] > Data[i, kijun_column] and Data[i - 1, tenkan_column] < Data[i - 1, kijun_column] and \
Data[i, closing_price_column] > Data[i, senkou_span_a_column] and Data[i, closing_price_column] > Data[i, senkou_span_b_column] and \
Data[i - 26, chikou_column] > Data[i - 26, closing_price_column]:
Data[i, buy] = 1
if Data[i, tenkan_column] < Data[i, kijun_column] and Data[i - 1, tenkan_column] > Data[i - 1, kijun_column] and \
Data[i, closing_price_column] < Data[i, senkou_span_a_column] and Data[i, closing_price_column] < Data[i, senkou_span_b_column] and \
Data[i - 26, chikou_column] < Data[i - 26, closing_price_column]:
Data[i, sell] = -1
# The tenkan_column is the index of the Tenkan-Sen Column
# The kijun_column is the index of the Kijun-Sen Column
# The senkou_span_a_column is the index of the Senkou-Span A Column
# The senkou_span_b_column is the index of the Senkou-Span B Column
# The chikou_column is the index of the Chikou-Span Column
# The closing_price_column is the index of the Close Column
Here are the conditions of a trade based on the Ichimoku system, followed by the signal function that tells Python to print 1 in case a bullish signal is generated and to print -1 in case a bearish signal is generated.
Go long (Buy): Whenever the Tenkan-sen crosses the Kijun-sen from the below to the above while the market price is above the Ichimoku cloud. And finally, the Chikou span’s last value must be higher than the corresponding market price in the same point in time.
Go short (Sell): Whenever the Tenkan-sen crosses the Kijun-sen from the above to the below while the market price is below the Ichimoku cloud. And finally, the Chikou span’s last value must be lower than the corresponding market price in the same point in time.
ichimoku_kinko_hyo(ETH)
signal(ETH)
my question is about how to solve this keyError I am getting
I'll be super greatful if someone could help me!
here's my Dataset head if you wonder:
Date Open High Low Close Adj Close Volume
2021-02-22 1935.557861 1936.453735 1580.626587 1781.992920 1781.992920 42409646036
2021-02-23 1781.409058 1781.409058 1378.840942 1570.203979 1570.203979 52029864713
2021-02-24 1571.476440 1710.983765 1511.018921 1626.575684 1626.575684 31329000537
2021-02-25 1625.393921 1670.224121 1465.058960 1475.703735 1475.703735 24481681873
2021-02-26 1478.653320 1559.028931 1407.979248 1446.033691 1446.033691 31435997881
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您无法使用
Data[i, tenkan_column]
进行行/列引用。 Pandas 的索引规则与 numpy 不同。它的作用是尝试使用元组(0, "tenkan_sen")
作为索引。您可以使用 iloc 成员按行号进行行索引。所以,这是可行的:
但是,您将立即遇到麻烦:
即使我们将其更改为:
在第一次迭代期间,
i
将为零,因此您将尝试使用< code>Data.iloc[-1],并且没有行-1。如果您需要将此行与前一行进行比较,则必须从第 1 行开始。但是,当我们到达以下位置时,即使这样也会爆炸:如果您确实需要引用前 26 行,那么您将必须从第 26 行开始。
You can't do row/column references by using
Data[i, tenkan_column]
. Pandas has different rules for indexing than numpy. What that does is try to use the tuple(0, "tenkan_sen")
as an index.You can do row indexing by row number using the
iloc
member. So, this works:However, you are immediately going to run into trouble with this:
Even if we change that to:
during your first iteration,
i
will be zero, so you'll try to useData.iloc[-1]
, and there is no row -1. If you need to compare this row to the previous row, you'll have to start at row 1. However, even that is going to explode when we get to:If you really need to refer to the previous 26 rows, then you will have to start with row number 26.