簡體   English   中英

Python:Pandas function KeyError(算法交易)

[英]Python: Pandas function KeyError (algo-trading)

我的問題是關於如何在我的代碼中應用 function 直到現在我一直在嘗試解決它,但我總是會出錯。

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

def signal(Data):
 for i in range(len(Data)):
   if Data.iloc[i]['tenkan_sen'] > Data.iloc[i]['kijun_sen'] and Data.iloc[i - 1]['tenkan_sen'] < Data.iloc[i - 1]['kijun_sen'] and Data.iloc[i]['Close'] > Data[i]['senkou_span_a'] and Data.iloc[i]['Close'] > Data.iloc[i]['senkou_span_b'] and Data.iloc[i - 26]['chikou_span'] > Data.iloc[i - 26]['Close']:
    Data.iloc[i]['buy'] = 1 
   else:
    Data.iloc[i]['buy'] = 0

signal(data)

這是我的“數據”的負責人或 DataFrame

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

這是我在運行此代碼時遇到的錯誤:

/usr/local/lib/python3.7/dist-packages/pandas/core/series.py:1056: SettingWithCopyWarning:


A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy

/usr/local/lib/python3.7/dist-packages/pandas/core/indexing.py:1724: SettingWithCopyWarning:


A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
/usr/local/lib/python3.7/dist-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
   3360             try:
-> 3361                 return self._engine.get_loc(casted_key)
   3362             except KeyError as err:

如何將此代碼應用於我的數據? NAN 是否有缺失值並不重要。

我建議您觀看此視頻,其中包含您嘗試制作的 ichimoku 雲的實現。 我剛才看了它,對我來說很有意義。 如果你不想看它, 這里是源代碼。

至於調試錯誤。

  1. 檢查了 output 的錯誤行。
  2. 由於 NA 是一個問題,我們將所有列的 NA 替換為 0。
  3. 由於信號的條件判斷是個問題,我們就一個個條件拆開檢查。
  4. 我們確認缺少第三個判斷的行規范。
   def signal(Data):
        Data = ichimoku_kinko_hyo(Data)
        #print(Data.iloc[69])
        Data.fillna(0, inplace=True)
        #print(Data.info())
        Data['buy'] = 0
        for i in range(len(Data)):
            print(Data.iloc[i])
            print(Data.iloc[i]['tenkan_sen'] > Data.iloc[i]['kijun_sen'])
            print(Data.iloc[i - 1]['tenkan_sen'] < Data.iloc[i - 1]['kijun_sen'])
            print(Data.iloc[i]['Close'] > Data.iloc[i]['senkou_span_a']) # updated
            print(Data.iloc[i]['Close'] > Data.iloc[i]['senkou_span_b'])
            print(Data.iloc[i - 26]['chikou_span'] > Data.iloc[i - 26]['Close'])
            if Data.iloc[i]['tenkan_sen'] > Data.iloc[i]['kijun_sen'] and Data.iloc[i - 1]['tenkan_sen'] < Data.iloc[i - 1]['kijun_sen'] and Data.iloc[i]['Close'] > Data.loc[i]['senkou_span_a'] and Data.iloc[i]['Close'] > Data.iloc[i]['senkou_span_b'] and Data.iloc[i - 26]['chikou_span'] > Data.iloc[i - 26]['Close']:
                 Data.iloc[i]['buy'] = 1 
            else:
                Data.iloc[i]['buy'] = 0
        return Data

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM