-
Notifications
You must be signed in to change notification settings - Fork 0
/
mtgoxcore.py
127 lines (106 loc) · 3.76 KB
/
mtgoxcore.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
import urllib, urllib2, ssl, post, time, hmac, json, logging
from base64 import b64encode, b64decode
from hashlib import sha512 as hash
logger = logging.getLogger('MtGoxCore')
def _hmac_digest(data, key):
return hmac.new(key, data, hash).digest()
def _get_nonce():
nonce = str(int(time.time() * 1000))
logger.info('Produced nonce: %s' % nonce)
return nonce
class MtGoxCore:
URL = 'https://mtgox.com/api/0/%s.php?%s'
def __init__(self, key = '', secret = ''):
self.key = key
self.sec = secret
def req(self, url, values = {'nonce': None}):
while True:
nonce = _get_nonce()
values['nonce'] = nonce
post_data = urllib.urlencode(values)
headers = {
'Rest-Key': self.key,
'Rest-Sign': b64encode(_hmac_digest(post_data, b64decode(self.sec)))
}
js = post.post(url, post_data, headers)
try:
dt = json.loads(js)
except:
logger.info('Got invalid JSON; retrying')
logger.debug(js)
continue
if 'error' in dt:
logger.info('MtGox reported an error; retrying')
logger.debug(str(dt))
continue
return dt
def ticker(self):
"""Output:
{ticker: {buy: float,
sell: float,
high: float,
low: float,
last: float,
avg: float,
vol: float,
vwap: float}
}"""
# 'vwap' is weighed average
logger.info('ticker()')
return self.req(MtGoxCore.URL % ('data/ticker', ''))
def depth(self):
"""Output:
{bids: [option]],
asks: [option]}
Where 'option' is a list of two floats;
first one is price, second is amount.
Bids and asks are ordered by increasing price."""
logger.info('depth()')
return self.req(MtGoxCore.URL % ('data/getDepth', ''))
def trades(self, since = None):
"""Output:
[{amount: float,
amount_int: int,
price: float,
price_int: int,
date: int,
tid: int,
trade_type: string ('bid' or 'ask'),
item: string (always 'BTC'),
price_currency: string (always 'USD')
}]"""
if since is not None:
param = 'since=' + str(since)
else:
param = ''
logger.info('trades(%s)' % since)
return self.req(MtGoxCore.URL % ('data/getTrades', param))
def balance(self):
"""Output:
{btcs: string,
usds: string}"""
logger.info('balance()')
return self.req(MtGoxCore.URL % ('getFunds', ''))
def __mkOrder(self, kind, amount, price):
return self.req(MtGoxCore.URL % (kind + 'BTC', ''),
{'amount': str(amount), 'price': str(price)}
)
def buy(self, amount, price):
logger.info('buy(%s, %s)' % (amount, price))
return self.__mkOrder('buy', amount, price)
def sell(self, amount, price):
logger.info('sell(%s, %s)' % (amount, price))
return self.__mkOrder('sell', amount, price)
def cancel(self, oid):
"""Output:
{btcs: string,
usds: string,
orders: [order]}"""
logger.info('cancel(%s)' % oid)
return self.req(MtGoxCore.URL % ('cancelOrder', ''), {'oid': oid})
def orders(self):
logger.info('orders()')
return self.req(MtGoxCore.URL % ('getOrders', ''))
def withdraw(self, amount, address):
logger.info('withdraw(%s, %s)' % (amount, address))
return self.req(MtGoxCore.URL % ('withdraw', ''))