crypto_data / download.py
Sébastien De Greef
Add scripts to create indicators and sequences, and download crypto data
1e1b538
raw
history blame
2.16 kB
# get https://api.bitvavo.com/v2/markets
import requests
import json
import os
import time
import datetime
import pandas as pd
import numpy as np
import sqlite3
def get_markets():
url = 'https://api.bitvavo.com/v2/markets'
response = requests.get(url)
markets = response.json()
return markets
markets = get_markets()
markets = pd.DataFrame(markets)
markets.to_csv('markets.csv', index=False)
# get https://api.bitvavo.com/v2/assets
def get_assets():
url = 'https://api.bitvavo.com/v2/assets'
response = requests.get(url)
assets = response.json()
return assets
assets = get_assets()
assets = pd.DataFrame(assets)
assets.to_csv('assets.csv', index=False)
print('Data downloaded and saved to assets.csv and markets.csv')
# create the candles directory
if not os.path.exists('candles'):
os.makedirs('candles')
for market in markets['market']:
print('Downloading', market)
url = f'https://api.bitvavo.com/v2/{market}/candles?interval=1d&limit=1440'
response = requests.get(url)
data = response.json()
#print(data)
data = pd.DataFrame(data, columns=['time', 'open', 'high', 'low', 'close', 'volume'])
data['market'] = market
# set market as the first column
data = data[['market', 'time', 'open', 'high', 'low', 'close', 'volume']]
data.to_csv(f'candles/{market}.csv', index=False)
time.sleep(0.5)
# print('Ticker data downloaded')
# combine all ticker data into a single file adding the market name as a column
candles = []
for market in markets['market']:
data = pd.read_csv(f'candles/{market}.csv')
data['market'] = market
candles.append(data)
candles = pd.concat(candles)
# convert the time column to a datetime from a unix timestamp
candles['time'] = pd.to_datetime(candles['time'], unit='ms')
# set index to market and time
candles = candles.set_index(['market', 'time'])
candles.to_csv('candles.csv')
conn = sqlite3.connect('crypto_data.db')
candles.to_sql('candles', conn, if_exists='replace')
conn.close()
print('Candles data saved to candles.csv and crypto_data.db')