-
Notifications
You must be signed in to change notification settings - Fork 8
/
btcmarkets.py
102 lines (63 loc) · 3.82 KB
/
btcmarkets.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
import base64, hashlib, hmac, urllib2, time, urllib, json
from collections import OrderedDict
base_url = 'https://api.btcmarkets.net'
def request(action, key, signature, timestamp, path, data):
header = {
'accept': 'application/json',
'Content-Type': 'application/json',
'User-Agent': 'btc markets python client',
'accept-charset': 'utf-8',
'apikey': key,
'signature': signature,
'timestamp': timestamp,
}
request = urllib2.Request(base_url + path, data, header)
if action == 'post':
response = urllib2.urlopen(request, data)
else:
response = urllib2.urlopen(request)
return json.load(response)
def get_request(key, secret, path):
nowInMilisecond = str(int(time.time() * 1000))
stringToSign = path + "\n" + nowInMilisecond + "\n"
signature = base64.b64encode(hmac.new(secret, stringToSign, digestmod=hashlib.sha512).digest())
return request('get', key, signature, nowInMilisecond, path, None)
def post_request(key, secret, path, postData):
nowInMilisecond = str(int(time.time() * 1000))
stringToSign = path + "\n" + nowInMilisecond + "\n" + postData
signature = base64.b64encode(hmac.new(secret, stringToSign, digestmod=hashlib.sha512).digest())
return request('post', key, signature, nowInMilisecond, path, postData)
class BTCMarkets:
def __init__(self, key, secret):
self.key = key
self.secret = base64.b64decode(secret)
def trade_history(self, currency, instrument, limit, since):
data = OrderedDict([('currency', currency),('instrument', instrument),('limit', limit),('since', since)])
postData = json.dumps(data, separators=(',', ':'))
return post_request(self.key, self.secret, '/order/trade/history', postData)
def order_create(self, currency, instrument, price, volume, side, order_type, client_request_id):
data = OrderedDict([('currency', currency),('instrument', instrument),
('price', price),('volume', volume),('orderSide', side),('ordertype', order_type),
('clientRequestId', client_request_id)])
postData = json.dumps(data, separators=(',', ':'))
return post_request(self.key, self.secret, '/order/create', postData)
def order_history(self, currency, instrument, limit, since):
data = OrderedDict([('currency', currency),('instrument', instrument),('limit', limit),('since', since)])
postData = json.dumps(data, separators=(',', ':'))
return post_request(self.key, self.secret, '/order/history', postData)
def order_open(self, currency, instrument, limit, since):
data = OrderedDict([('currency', currency),('instrument', instrument),('limit', limit),('since', since)])
postData = json.dumps(data, separators=(',', ':'))
return post_request(self.key, self.secret, '/order/open', postData)
def order_detail(self, order_ids):
data_obj = {'orderIds':order_ids}
postData = json.dumps(data_obj, separators=(',', ':'))
return post_request(self.key, self.secret, '/order/detail', postData)
def account_balance(self):
return get_request(self.key, self.secret, '/account/balance')
def get_market_tick(self,currency_in,currency_out):
return get_request(self.key, self.secret, '/market/%s/%s/tick' % (currency_in,currency_out))
def get_market_orderbook(self,currency_in,currency_out):
return get_request(self.key, self.secret, '/market/%s/%s/orderbook' % (currency_in,currency_out))
def get_market_trades(self,currency_in,currency_out):
return get_request(self.key, self.secret, '/market/%s/%s/trades' % (currency_in,currency_out))