首页 > 解决方案 > 熊猫数据框中的最大行数是 999 吗?

问题描述

我正在尝试用 python 制作一个加密交易机器人,我使用名为ccxt的库收集历史数据(价格和东西) ,并将其存储在pandas数据框中。当我尝试存储超过 999 行时,我收到如下所示的错误 (KeyError: 999)。基本上它不会让我存储超过 999 行。有什么方法可以在 pandas 数据框中获取超过 999 个数据?

代码非常大,但这里有一些片段:

数据框构建:

exchange = ccxt.binance()
bars = exchange.fetch_ohlcv(pair_to_trade, timeframe=timeframe, limit=amount_of_timeframe)
df = pd.DataFrame(bars[:-1], columns=['timestamp', 'open', 'high', 'low', 'close', 'volume'])

Last_row_index:

 for i in range(1, amount_of_timeframe-1):  # where amount_of_timeframe > 1000
        last_row_index = i
        previous_row_index = last_row_index - 1

数据框如下所示:

              timestamp     open     high  ...    upperband    lowerband  in_uptrend
0   2021-09-05 16:27:00  3913.51  3914.97  ...          NaN          NaN        True
1   2021-09-05 16:28:00  3914.04  3914.22  ...          NaN          NaN        True
2   2021-09-05 16:29:00  3910.01  3912.00  ...          NaN          NaN        True
3   2021-09-05 16:30:00  3911.84  3911.85  ...          NaN          NaN        True
...
995 2021-09-06 09:02:00  3956.10  3956.36  ...  3967.517857  3942.412143       False
996 2021-09-06 09:03:00  3954.32  3954.32  ...  3964.035000  3941.775000       False
997 2021-09-06 09:04:00  3951.52  3955.85  ...  3964.035000  3941.610714       False
998 2021-09-06 09:05:00  3955.58  3955.58  ...  3964.035000  3943.252857       False

错误:

File "D:/myfolders/supertrendStrategy/main.py", line 105, in testStrategy
    if not stdf['in_uptrend'][previous_row_index] and stdf['in_uptrend'][last_row_index]:
  File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\series.py", line 882, in __getitem__
    return self._get_value(key)
  File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\series.py", line 989, in _get_value
    loc = self.index.get_loc(label)
  File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\indexes\range.py", line 357, in get_loc
    raise KeyError(key) from err
KeyError: 999

标签: pythonpandasdataframeccxt

解决方案


ccxt.binance() won't let you fetch more than 1000 candles. Try another API, that works with bigger data


推荐阅读