使用Kucoin Python SDK获取BTC数据时遇kline_type参数缺失错误
解决Kucoin Python SDK获取K线数据时的TypeError问题
问题原因
- 参数名不匹配:你调用
get_kline时使用了驼峰式的klineType参数名,但该方法要求的参数名是下划线命名的kline_type,这是触发报错的直接原因。 - 日期格式错误: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
相关产品推荐
相关产品推荐

