-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathgdax_reader.py
More file actions
146 lines (127 loc) · 4.98 KB
/
gdax_reader.py
File metadata and controls
146 lines (127 loc) · 4.98 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import datetime
from dateutil import parser as date_parser
import cbpro as gdax
from exchanges import Exchange
class Gdax(Exchange):
client = None
def __init__(self, config):
"""
:param config:
"""
# connect to GDAX on import
try:
self.client = gdax.AuthenticatedClient(
config['key'], config['secret'], config['passphrase']
)
print('Connected to GDAX.')
except:
print('Could not connect to GDAX.')
def get_order_ids(self, history, ignore_products=[]):
"""
Get all of the order ids by looping through transaction history.
:param history:
:param ignore_products:
:return:
"""
order_ids = {}
for transaction in history:
if 'order_id' in transaction['details'] and 'product_id' in transaction['details']:
if transaction['details']['order_id'] not in order_ids and \
transaction['details']['product_id'] not in ignore_products:
order_ids[transaction['details']['order_id']] = 1
elif 'source' in transaction['details'] and transaction['details']['source'] == 'fork':
# this was a forked coin deposit
print(transaction['amount'] + transaction['details']['ticker'] + " obtained from a fork.")
elif 'transfer_id' not in transaction['details']:
print("No order_id or transfer_id in details for the following order (WEIRD!)")
print(transaction)
return list(order_ids.keys())
def parse_order(self, order):
"""
Normalizes the order.
:param order: Passed in from client.get_order(order_id)
:return:
"""
if order['status'] == 'done':
fees = float(order['fill_fees'])
# currency pair
pair = order['product_id'].split('-')
product = pair[0]
exchange_currency = pair[1]
amount = float(order['filled_size'])
cost = float(order['executed_value'])
if order['side'] == 'sell':
cost -= fees
else:
cost += fees
cost_per_coin = cost / amount
else:
print('Order status is not done! (WEIRD)')
print(order)
return {
'order_time': date_parser.parse(order['done_at']),
'product': product,
'currency': exchange_currency,
'currency_pair': order['product_id'],
'buysell': order['side'],
'cost': cost,
'amount': amount,
'cost_per_coin': cost_per_coin,
}
# get the buys and sells for an account
def get_account_transactions(self, account, ignore_products=[]):
"""
:param account:
:param ignore_products:
:return:
"""
# Get the account history
history = self.client.get_account_history(account['id'])
# Get all order ids from the account
order_ids = self.get_order_ids(history, ignore_products=ignore_products)
# Get the information from each order_id
sells = []
buys = []
for order_id in order_ids:
order = self.parse_order(self.client.get_order(order_id))
if len(order['product']) < 3:
print('WEIRD ORDER. NO PRODUCT!!!')
# put order in a buy or sell list
if order['buysell'] == 'sell':
sells.append(order)
elif order['buysell'] == 'buy':
buys.append(order)
else:
print('order is not buy or sell! WEIRD')
print(order)
return buys, sells
def get_price(self, order_time, product='BTC-USD'):
"""
Get the price of a specific pair from an hour before the order took place.
:param order_time:
:param product: currency pair
:return:
"""
history = self.client.get_product_historic_rates(
product_id=product,
start=order_time - datetime.timedelta(hours=1),
end=order_time
)
price = history[0][4]
return price
def get_buys_sells(self):
"""
Get the buys and sells for all orders.
:return:
"""
# Get the GDAX accounts
accounts = self.client.get_accounts()
for account in accounts:
# Only use the USD and BTC accounts since they will contain all transaction ids
if account['currency'] == 'USD':
[buys_usd, sells_usd] = self.get_account_transactions(account)
elif account['currency'] == 'BTC':
[buys_btc, sells_btc] = self.get_account_transactions(account, ignore_products=['BTC-USD'])
# elif account['currency'] == 'ETH':
# [buys_btc, sells_btc] = get_account_transactions(client, account, ignore_products=['ETH-USD'])
return buys_usd + buys_btc, sells_usd + sells_btc