1

我需要从 Kucoin API 下载 2019 年的 1 分钟 kline 数据。它允许每个请求获取 1500 个数据点,因此我创建了一个函数来获取我需要的块长度。问题是对于某些块我得到一个错误。

KucoinAPIException(<响应 [429]>)

这是“请求太多”错误。根据我在线阅读的限制是 10 秒内 30 次调用,我在 10 秒内执行 1 次,所以不太清楚为什么会出现此错误?当我执行 time.sleep(0) 时,我得到了预期的临时块。

如何在对 API 友好的同时获取大量数据?代码中的错误在哪里?

def get_ku_his(sym, tf, str_start, str_end):
    start_ts = int(datetime.strptime(str_start, '%Y-%m-%d %H:%M').replace(tzinfo=timezone.utc).timestamp())
    if str_end==0: 
        end_ts=0
    else:
        end_ts = int(datetime.strptime(str_end, '%Y-%m-%d %H:%M').replace(tzinfo=timezone.utc).timestamp())
    data = kuclient.get_kline_data(sym, tf, start_ts, end_ts)
    data = pd.DataFrame(data, columns = ['timestamp', 'open', 'close', 'high', 'low', 'transaction amount', 'volume'])
    data['timestamp'] = data['timestamp'].astype(float)*1000
    data['timestamp'] = pd.to_datetime(data['timestamp'], unit='ms')
    data.set_index('timestamp', inplace=True)
    data = data[["close","volume"]]
    data.dropna(inplace=True)
    data = data.astype(float)
    data = data.sort_index()
    return data

def get_ku_hp(sym, tf, str_start):
    data = get_ku_his(sym, tf, str_start, 0)
    temp_start = data.index[0].strftime('%Y-%m-%d %H:%M')
    while str_start < temp_start:
        try:
            temp = get_ku_his(sym, tf, str_start, temp_start)
            print("Downloaded part from %s to %s" %(str_start, temp_start))
            temp_start = temp.index[0].strftime('%Y-%m-%d %H:%M')
            data = data.append(temp)
            time.sleep(10)
        except Exception as e: print(repr(e))
    data = data.sort_index()
    print("%s Downloaded %s data for %s from %s to %s" % (datetime.today().strftime("%H:%M:%S"), tf, sym, data.index[0], data.index[-1]))
    return data

test = get_ku_hp("AIOZ-USDT","1min","2019-01-05 22:00")

控制台消息:

*Downloaded part from 2019-01-05 22:00 to 2021-10-19 11:45
Downloaded part from 2019-01-05 22:00 to 2021-10-18 10:45
Downloaded part from 2019-01-05 22:00 to 2021-10-17 09:45
KucoinAPIException(<Response [429]>)
Downloaded part from 2019-01-05 22:00 to 2021-10-16 08:45
Downloaded part from 2019-01-05 22:00 to 2021-10-15 07:45
Downloaded part from 2019-01-05 22:00 to 2021-10-14 06:45
Downloaded part from 2019-01-05 22:00 to 2021-10-13 05:45
Downloaded part from 2019-01-05 22:00 to 2021-10-12 04:45
Downloaded part from 2019-01-05 22:00 to 2021-10-11 03:45
Downloaded part from 2019-01-05 22:00 to 2021-10-10 02:45
Downloaded part from 2019-01-05 22:00 to 2021-10-09 01:45
Downloaded part from 2019-01-05 22:00 to 2021-10-08 00:45
Downloaded part from 2019-01-05 22:00 to 2021-10-06 23:45
Downloaded part from 2019-01-05 22:00 to 2021-10-05 22:45
Downloaded part from 2019-01-05 22:00 to 2021-10-04 21:45
Downloaded part from 2019-01-05 22:00 to 2021-10-03 20:45
KucoinAPIException(<Response [429]>)
KucoinAPIException(<Response [429]>)
Downloaded part from 2019-01-05 22:00 to 2021-10-02 19:45
Downloaded part from 2019-01-05 22:00 to 2021-10-01 18:45
Downloaded part from 2019-01-05 22:00 to 2021-09-30 17:45*
4

1 回答 1

0

做很多请求尝试使用 Websocket,这里是一个带有文档的库。或者,您可以使用 VPN 或代理更改请求 IP。

于 2022-01-14T18:17:22.153 回答