You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Kucoin Python SDK获取BTC数据时遇kline_type参数缺失错误

解决Kucoin Python SDK获取K线数据时的TypeError问题

问题原因

  1. 参数名不匹配:你调用get_kline时使用了驼峰式的klineType参数名,但该方法要求的参数名是下划线命名的kline_type,这是触发报错的直接原因。
  2. 日期格式错误:Kucoin API的startAt和endAt参数需要传入秒级Unix时间戳(整数类型),而非字符串格式的日期,直接传字符串会引发后续解析错误。

修正后的代码

import kucoin.client as kc
import pandas as pd
from datetime import datetime

api_key = '***6120**********'
api_secret = '****-0078**************'
# 若你的API密钥设置了passphrase,需添加该参数:passphrase='your_passphrase'
client = kc.Market(url='https://api.kucoin.com', key=api_key, secret=api_secret)

symbol = 'BTC-USDT'
interval = '1day'
# 将字符串日期转换为秒级时间戳
start_time = int(datetime.strptime('2022-01-01', '%Y-%m-%d').timestamp())
end_time = int(datetime.strptime('2022-04-29', '%Y-%m-%d').timestamp())
kline_type = 'spot'

# 修正参数名为kline_type
klines = client.get_kline(
    symbol=symbol,
    interval=interval,
    startAt=start_time,
    endAt=end_time,
    kline_type=kline_type
)

# 可选:将返回数据转为DataFrame便于分析
df = pd.DataFrame(klines, columns=['时间戳', '开盘价', '收盘价', '最高价', '最低价', '成交量', '成交额'])
df['时间'] = pd.to_datetime(df['时间戳'], unit='s')
print(df.head())

额外说明

如果你的Kucoin API密钥设置了交易密码(passphrase),初始化Market对象时必须添加passphrase参数,否则会触发权限验证错误。

内容的提问来源于stack exchange,提问作者hdsouza

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 01:12:09