본문 바로가기
Python, API

[Python][Bitstamp API] OHLCV 요청

by 오늘밤날다 2024. 6. 7.

 

https://www.bitstamp.net/api/#tag/Market-info/operation/GetOHLCData

 

Bitstamp Trusted Crypto Exchange | Buy & Sell Cryptocurrencies

Buy and sell leading cryptocurrencies at Bitstamp – the world's longest-standing crypto exchange. Low fees, reliable service and simple setup. Open a free account.

www.bitstamp.net

 

 

import requests
import pandas as pd


def get_ohlcv_data(symbol, step, start=None, end=None):
    url = f"https://www.bitstamp.net/api/v2/ohlc/{symbol}/"
    params = {'step': step, 'limit': 1000}
    if start is not None:
        params = params | {'start': start}
    if end is not None:
        params = params | {'end': end}

    rawdata = requests.get(url, params=params)
    rawdata = rawdata.json()
    df = pd.DataFrame(rawdata['data']['ohlc'])
    df['datetime_utc'] = pd.to_datetime(df['timestamp'].astype('int64'), unit='s')
    df.sort_values('timestamp', ascending=False, inplace=True)
    df.reset_index(inplace=True, drop=True)
    return df


df = get_ohlcv_data('btcusd', 60)
print(df)

end = int(df['timestamp'].iat[-1]) - 60
df = get_ohlcv_data('btcusd', 60, end=end)
print(df)

 

 

 

현재 시점부터 거꾸로 자료가 출력되므로 연속조회 시에는 마지막으로 출력된 자료의 timestamp를 end값으로 넣어서 다시 조회하면 된다. 다만 이 경우 해당 시간대의 자료가 다시 출력되니까 조회하고자 하는 step에 맞춰서 timestamp값을 한번 더 차감한 숫자를 end에 넣어주어야 한다.