-
Notifications
You must be signed in to change notification settings - Fork 18
/
coinbase_reader.py
59 lines (51 loc) · 2.06 KB
/
coinbase_reader.py
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
# This will read in coinbase transactions
import credentials
from coinbase.wallet.client import Client
import dateutil.parser
import datetime
import time
def get_client():
client = Client(credentials.coinbase_key, credentials.coinbase_secret)
return client
def get_accounts(client):
accounts = client.get_accounts()
return accounts
# get the buys and sells for an account
def get_account_transactions(client, account):
buys = []
sells = []
buys_complex = client.get_buys(account['id'])
sells_complex = client.get_sells(account['id'])
for order in buys_complex['data']:
order_time = dateutil.parser.parse(order['payout_at'])
product = order['amount']['currency']
cost = float(order['total']['amount'])
amount = float(order['amount']['amount'])
cost_per_coin = cost/amount
exchange_currency = order['total']['currency']
buys.append([order_time, product, 'buy', cost, amount, cost_per_coin, exchange_currency])
for order in sells_complex['data']:
# WARNING! This is not tested since I never sell on coinbase (use GDAX instead!)
order_time = dateutil.parser.parse(order['payout_at'])
product = order['amount']['currency']
cost = float(order['total']['amount'])
amount = float(order['amount']['amount'])
cost_per_coin = cost/amount
exchange_currency = order['total']['currency']
sells.append([order_time, product, 'sell', cost, amount, cost_per_coin, exchange_currency])
return buys, sells
def get_buys_sells():
print('Connecting to Coinbase...')
client = get_client()
print('Connected to Coinbase!')
# Get the Coinbase accounts
accounts = client.get_accounts()
buys = []
sells = []
for account in accounts['data']:
# Only use the USD and BTC accounts since they will contain all transaction ids
if account['currency'] != 'USD':
buys_dummy, sells_dummy = get_account_transactions(client, account)
buys += buys_dummy
sells += sells_dummy
return buys, sells