File size: 2,162 Bytes
0776603
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1e1b538
 
 
 
 
 
 
 
 
 
 
 
0776603
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80


# 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')