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에 넣어주어야 한다.
'Python, API' 카테고리의 다른 글
백테스트 프로그램 개선 (feat. ChatGPT) (0) | 2025.01.08 |
---|---|
[Python] 가까운 호가 단위의 가격으로 조정 (np.ceil, np.floor) (0) | 2024.06.27 |
[Python][Bybit V5 API] Private/Public Websocket 접속 (0) | 2024.05.27 |
[Python] os.walk - 하위폴더 내의 모든 파일 리스트 검색하기 (0) | 2024.05.03 |
[Python] 분봉데이터로 당일의 누적OHLCV 계산하기 (0) | 2023.06.22 |