Skip to content

Instantly share code, notes, and snippets.

@skeller88
Created September 25, 2019 19:30
Show Gist options
  • Save skeller88/8dc5fd99a260acdca300dc7e357a15e3 to your computer and use it in GitHub Desktop.
Save skeller88/8dc5fd99a260acdca300dc7e357a15e3 to your computer and use it in GitHub Desktop.
def main(start_datetime_str: str, end_datetime_str: Optional[str],
base_currency: str, quote_currency: str):
url = f"https://api-pub.bitfinex.com/v2/candles/trade:1h:t{base_currency}{quote_currency}/hist"
params = {
"limit": 5000,
"sort": 1,
"start": datetime.datetime.strptime(start_datetime_str, strftime_days).timestamp() * 1000,
"end": datetime.datetime.strptime(end_datetime_str, strftime_days).timestamp() * 1000
}
bars = requests.get(url, params=params).json()
with open(f'bitfinex_bars_{base_currency}{quote_currency}.csv', 'w+') as output:
writer = csv.writer(output)
writer.writerow(["timestamp", "open", "close", "high", "low", "volume_base"])
while len(bars) > 0:
writer.writerows(bars)
params["start"] = bars[-1][0]
print('first', microsecond_timestamp_to_utc_datetime(bars[0][0]), 'last', microsecond_timestamp_to_utc_datetime(bars[-1][0]))
bars = requests.get(url, params=params).json()
if bars[0] == 'error':
print('done')
return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment