-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrader.py
134 lines (91 loc) · 3.5 KB
/
trader.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
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
import requests
import os
import json
import datetime
import matplotlib.pyplot as plt
import private
import hmac, hashlib, time, requests, base64
from requests.auth import AuthBase
# Create custom authentication for Exchange
class CoinbaseExchangeAuth(AuthBase):
def __init__(self, api_key, secret_key, passphrase):
self.api_key = api_key
self.secret_key = secret_key
self.passphrase = passphrase
def __call__(self, request):
timestamp = str(time.time())
message = timestamp + request.method + request.path_url + (request.body or '')
hmac_key = base64.b64decode(self.secret_key)
signature = hmac.new(hmac_key, message, hashlib.sha256)
signature_b64 = signature.digest().encode('base64').rstrip('\n')
request.headers.update({
'CB-ACCESS-SIGN': signature_b64,
'CB-ACCESS-TIMESTAMP': timestamp,
'CB-ACCESS-KEY': self.api_key,
'CB-ACCESS-PASSPHRASE': self.passphrase,
'Content-Type': 'application/json'
})
return request
class gdax_interface:
def __init__(self):
self.url = 'https://api.gdax.com'
self.product_id = 'LTC-USD'
def set_product_id(self, id):
self.product_id = id
def get(self, path):
response = requests.get(self.url + path)
return response.json()
def post(self, path, content, auth=False):
# response = requests.post(self.url + path, auth=auth)
response = requests.post(self.url + '/orders', json=content, auth=auth)
return response.json()
def get_products(self):
return self.get('/products')
def get_order_book(self, level = False):
request_url = '/products/' + self.product_id + '/book'
if level:
request_url += '?level=' + str(level)
return self.get(request_url)
def get_historic_rates(self, start, stop, granularity = '60'):
request_url = '/products/' + self.product_id + '/candles'
request_url += '?start=' + str(start)
request_url += '?stop=' + str(stop)
request_url += '?granularity=' + str(granularity)
return self.get(request_url)
def plot_historic_rates(self, json_response):
keys = ['time', 'low', 'high', 'open', 'close', 'volume']
arrs = zip(*json_response)
f, axarr = plt.subplots(len(keys)-1, sharex=True)
for idx, val in enumerate(keys):
if idx == 0:
continue
time = arrs[0]
axarr[idx-1].plot(time, arrs[idx])
axarr[idx-1].set_title(val)
axarr[idx-1].grid()
plt.show()
def post_limit_order(self, side, price, size):
key, secret, auth_pass = private.get_auth()
auth = CoinbaseExchangeAuth(key, secret, auth_pass)
order = {
'size': size,
'price': price,
'side': side,
'product_id': 'BTC-USD',
}
return self.post('/orders', order, auth=auth)
if __name__ == '__main__':
script_dir = os.path.dirname(os.path.realpath(__file__))
gdax = gdax_interface()
gdax.set_product_id('BTC-USD')
json_response = gdax.post_limit_order('sell', 100000, 0.002)
print json.dumps(json_response, indent=4)
# now = datetime.datetime.now()
# delta = datetime.timedelta(days=1)
#
# start = (now - delta).isoformat()
# stop = now.isoformat()
#
# json_response = gdax.get_historic_rates(start,stop)
#
# gdax.plot_historic_rates(json_response)