Created
May 28, 2020 21:56
-
-
Save buddies2705/2ea5d380a8267939c0116d8e52e354a8 to your computer and use it in GitHub Desktop.
Fetching Bitflyer ohlcv data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import os | |
import sys | |
import csv | |
import pandas as pd | |
import datetime | |
# ----------------------------------------------------------------------------- | |
root = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) | |
sys.path.append(root + '/python') | |
import ccxt # noqa: E402 | |
# ----------------------------------------------------------------------------- | |
def retry_fetch_ohlcv(exchange, max_retries, symbol, timeframe, since, limit): | |
num_retries = 0 | |
try: | |
num_retries += 1 | |
ohlcv = exchange.fetch_ohlcv(symbol, timeframe, since, limit) | |
# print('Fetched', len(ohlcv), symbol, 'candles from', exchange.iso8601 (ohlcv[0][0]), 'to', exchange.iso8601 (ohlcv[-1][0])) | |
return ohlcv | |
except Exception: | |
if num_retries > max_retries: | |
raise # Exception('Failed to fetch', timeframe, symbol, 'OHLCV in', max_retries, 'attempts') | |
def scrape_ohlcv(exchange, max_retries, symbol, timeframe, since, limit): | |
earliest_timestamp = exchange.milliseconds() | |
timeframe_duration_in_seconds = exchange.parse_timeframe(timeframe) | |
timeframe_duration_in_ms = timeframe_duration_in_seconds * 1000 | |
timedelta = limit * timeframe_duration_in_ms | |
all_ohlcv = [] | |
while True: | |
fetch_since = earliest_timestamp - timedelta | |
ohlcv = retry_fetch_ohlcv(exchange, max_retries, symbol, timeframe, fetch_since, limit) | |
# if we have reached the beginning of history | |
if ohlcv[0][0] >= earliest_timestamp: | |
break | |
earliest_timestamp = ohlcv[0][0] | |
all_ohlcv = ohlcv + all_ohlcv | |
print(len(all_ohlcv), 'candles in total from', exchange.iso8601(all_ohlcv[0][0]), 'to', exchange.iso8601(all_ohlcv[-1][0])) | |
# if we have reached the checkpoint | |
if fetch_since < since: | |
break | |
return exchange.filter_by_since_limit(all_ohlcv, since, None, key=0) | |
def scrape_candles_to_csv(filename, exchange_id, max_retries, symbol, timeframe, since, limit): | |
# instantiate the exchange by id | |
exchange = getattr(ccxt, exchange_id)({ | |
'enableRateLimit': True, # required by the Manual | |
}) | |
# convert since from string to milliseconds integer if needed | |
if isinstance(since, str): | |
since = exchange.parse8601(since) | |
# preload all markets from the exchange | |
exchange.load_markets() | |
# fetch all candles | |
ohlcv = scrape_ohlcv(exchange, max_retries, symbol, timeframe, since, limit) | |
# save them to csv file | |
df = pd.DataFrame(ohlcv) | |
df.columns = (['Date Time', 'Open', 'High', 'Low', 'Close', 'Volume']) | |
def parse_dates(ts): | |
return datetime.datetime.fromtimestamp(ts/1000.0) | |
df['Date Time'] = df['Date Time'].apply(parse_dates) | |
df.to_csv('sampledata.csv') | |
print('Saved', len(ohlcv), 'candles from', exchange.iso8601(ohlcv[0][0]), 'to', exchange.iso8601(ohlcv[-1][0]), 'to', filename) | |
# ----------------------------------------------------------------------------- | |
def main(): | |
scrape_candles_to_csv('sampledata.csv', 'bitflyer', 5, 'BTC/USD', '1m', '2020-04-26 00:00:00', 1000) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment