futures_klines 工作与 get_klines 出现错误 未读取所有发送的参数;读“3”参数但已发送“4”

发布于 2025-01-11 16:25:58 字数 2249 浏览 0 评论 0原文

我正在使用 Python 中的 Binance 客户端来获取 K 线数据,该数据对于期货调用来说效果很好,但我需要使用期货中不可用的其他交易品种的现货数据。有什么想法如何解决这个问题吗?在线示例看起来是相同的,但由于某种原因它说只允许 3 个参数而不是 4 个。这三个是什么?

    from binance.client import Client
    binance = Client(api_key=binance_api_key, api_secret=binance_api_secret)

    df_check=check_binanceklines(symbol=symbol)


    def check_binanceklines(symbol='BTCUSDT',interval='1m',limit=60,since="1 hour ago UTC"):
        klines = binance.futures_klines(symbol=symbol,interval={'1m':Client.KLINE_INTERVAL_1MINUTE}[interval],since=since,limit=limit)
    ***# issue below!***
        klines = binance.get_klines(symbol=symbol,interval={'1m':Client.KLINE_INTERVAL_1MINUTE}[interval],since=since,limit=limit) 




    Traceback (most recent call last):
      File "binance-test.py", line 254, in <module>
        df_check=check_binanceklines(symbol=symbol) # get 1 hour ago, every minute
      File "binance-test.py", line 29, in check_binanceklines
        klines = binance.get_klines(symbol=symbol,interval=        {'1m':Client.KLINE_INTERVAL_1MINUTE}[interval],since=since,limit=limit)
      File "C:\Users\lee\AppData\Local\Programs\Python\Python38\lib\site-        packages\binance\client.py", line 868, in get_klines
        return self._get('klines', data=params, version=self.PRIVATE_API_VERSION)
      File "C:\Users\lee\AppData\Local\Programs\Python\Python38\lib\site-        packages\binance\client.py", line 371, in _get
        return self._request_api('get', path, signed, version, **kwargs)
      File "C:\Users\lee\AppData\Local\Programs\Python\Python38\lib\site-        packages\binance\client.py", line 334, in _request_api
        return self._request(method, uri, signed, **kwargs)
      File "C:\Users\lee\AppData\Local\Programs\Python\Python38\lib\site-packages\binance\client.py", line 315, in _request
        return self._handle_response(self.response)
      File "C:\Users\lee\AppData\Local\Programs\Python\Python38\lib\site-packages\binance\client.py", line 324, in _handle_response
        raise BinanceAPIException(response, response.status_code, response.text)
    binance.exceptions.BinanceAPIException: APIError(code=-1104): Not all sent parameters were read; read '3' parameter(s) but was sent '4'.

I'm using the Binance client in python to get klines data which works fine for the futures call but I need to use the spot data for other symbols not available on futures. any ideas how to fix this? the online examples look to be the same but for some reason it says only 3 params not 4 allowed. what are the 3?

    from binance.client import Client
    binance = Client(api_key=binance_api_key, api_secret=binance_api_secret)

    df_check=check_binanceklines(symbol=symbol)


    def check_binanceklines(symbol='BTCUSDT',interval='1m',limit=60,since="1 hour ago UTC"):
        klines = binance.futures_klines(symbol=symbol,interval={'1m':Client.KLINE_INTERVAL_1MINUTE}[interval],since=since,limit=limit)
    ***# issue below!***
        klines = binance.get_klines(symbol=symbol,interval={'1m':Client.KLINE_INTERVAL_1MINUTE}[interval],since=since,limit=limit) 




    Traceback (most recent call last):
      File "binance-test.py", line 254, in <module>
        df_check=check_binanceklines(symbol=symbol) # get 1 hour ago, every minute
      File "binance-test.py", line 29, in check_binanceklines
        klines = binance.get_klines(symbol=symbol,interval=        {'1m':Client.KLINE_INTERVAL_1MINUTE}[interval],since=since,limit=limit)
      File "C:\Users\lee\AppData\Local\Programs\Python\Python38\lib\site-        packages\binance\client.py", line 868, in get_klines
        return self._get('klines', data=params, version=self.PRIVATE_API_VERSION)
      File "C:\Users\lee\AppData\Local\Programs\Python\Python38\lib\site-        packages\binance\client.py", line 371, in _get
        return self._request_api('get', path, signed, version, **kwargs)
      File "C:\Users\lee\AppData\Local\Programs\Python\Python38\lib\site-        packages\binance\client.py", line 334, in _request_api
        return self._request(method, uri, signed, **kwargs)
      File "C:\Users\lee\AppData\Local\Programs\Python\Python38\lib\site-packages\binance\client.py", line 315, in _request
        return self._handle_response(self.response)
      File "C:\Users\lee\AppData\Local\Programs\Python\Python38\lib\site-packages\binance\client.py", line 324, in _handle_response
        raise BinanceAPIException(response, response.status_code, response.text)
    binance.exceptions.BinanceAPIException: APIError(code=-1104): Not all sent parameters were read; read '3' parameter(s) but was sent '4'.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一影成城 2025-01-18 16:25:58

可以使用以下参数调用 binance.client.Client.get_klines()

Signature: Client.get_klines(self, **params) -> Dict
Docstring:
Kline/candlestick bars for a symbol. Klines are uniquely identified by their open time.

https://binance-docs.github.io/apidocs/spot/en/#kline-candlestick-data

:param symbol: required
:type symbol: str
:param interval: -
:type interval: str
:param limit: - Default 500; max 1000.
:type limit: int
:param startTime:
:type startTime: int
:param endTime:
:type endTime: int

:returns: API response

因此 since 是无效参数。请改用 startTimeendTime。这些参数要求以毫秒为单位的时间戳。下面是一个工作示例,它提供了过去 60 分钟(包括当前分钟)的信息:

import datetime as dt

from binance.client import Client


def check_binanceklines(
        symbol = 'BTCUSDT',
        interval = Client.KLINE_INTERVAL_1MINUTE,
        limit = 60,
        end = int(dt.datetime.now(dt.timezone.utc).timestamp() * 1000)
):
    binance = Client()
    return binance.get_klines(symbol=symbol,interval=interval,endTime=end,limit=limit)


if __name__ == '__main__':
    klines = check_binanceklines()

    # Print len, the first and last elemnt of returned klines list.
    print(f'Length of the returned klines list: {len(klines)}')
    print(klines[1])
    print(klines[-1])

输出:

Length of the returned klines list: 60
[1647377880000, '39416.16000000', '39418.45000000', '39377.71000000', '39389.27000000', '25.79166000', 1647377939999, '1016095.94556700', 951, '8.45500000', '333088.81485350', '0']
[1647381360000, '39613.93000000', '39642.67000000', '39611.01000000', '39632.97000000', '14.80226000', 1647381419999, '586574.96479330', 452, '7.66347000', '303656.99947290', '0']

binance.client.Client.get_klines() can be called with the following parameters:

Signature: Client.get_klines(self, **params) -> Dict
Docstring:
Kline/candlestick bars for a symbol. Klines are uniquely identified by their open time.

https://binance-docs.github.io/apidocs/spot/en/#kline-candlestick-data

:param symbol: required
:type symbol: str
:param interval: -
:type interval: str
:param limit: - Default 500; max 1000.
:type limit: int
:param startTime:
:type startTime: int
:param endTime:
:type endTime: int

:returns: API response

Therefore since is an invalid parameter. Use startTime or endTime instead. These parameters ask for a timestamp in milliseconds. Here is a working example which gives you the last 60 minutes (including the current minute):

import datetime as dt

from binance.client import Client


def check_binanceklines(
        symbol = 'BTCUSDT',
        interval = Client.KLINE_INTERVAL_1MINUTE,
        limit = 60,
        end = int(dt.datetime.now(dt.timezone.utc).timestamp() * 1000)
):
    binance = Client()
    return binance.get_klines(symbol=symbol,interval=interval,endTime=end,limit=limit)


if __name__ == '__main__':
    klines = check_binanceklines()

    # Print len, the first and last elemnt of returned klines list.
    print(f'Length of the returned klines list: {len(klines)}')
    print(klines[1])
    print(klines[-1])

Output:

Length of the returned klines list: 60
[1647377880000, '39416.16000000', '39418.45000000', '39377.71000000', '39389.27000000', '25.79166000', 1647377939999, '1016095.94556700', 951, '8.45500000', '333088.81485350', '0']
[1647381360000, '39613.93000000', '39642.67000000', '39611.01000000', '39632.97000000', '14.80226000', 1647381419999, '586574.96479330', 452, '7.66347000', '303656.99947290', '0']
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文