forked from ericjang/cryptocurrency_arbitrage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCoinsE.py
129 lines (110 loc) · 4.41 KB
/
CoinsE.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
# support code from https://www.coins-e.com/assets/api/coins-e_api_example.py
# -*- coding: utf-8 -*-
import urllib,urllib2
import hmac
import hashlib
import json
from myutils import get_swapped_order
from Exchange import Exchange
from order import Order
class CoinsE(Exchange):
"""docstring for Coins-e"""
def __init__(self, api_key, secret):
self.api_key = api_key
self.secret = secret
self.base_api_url = "https://www.coins-e.com/api/v2"
super(CoinsE, self).__init__()
self.name = 'COINS-E'
self.trading_fee = 0.002
# implemented abstract functions
def get_tradeable_pairs(self):
tradeable_pairs = []
markets = self.unauthenticated_request('markets/list/')
for m in markets['markets']:
tradeable_pairs.append((m['c1'], m['c2']))
return tradeable_pairs
def get_depth(self, base, alt):
book = { "bids" : [], "asks" : [] }
pair0 = (base, alt)
pair, swapped = self.get_validated_pair(pair0)
newbase, newalt = pair
slug = newbase + "_" + newalt
marketdata = self.unauthenticated_request('markets/data/')
depth = marketdata['markets'][slug]['marketdepth']
if swapped:
for bid in depth['bids']:
o = Order(float(bid['r']), float(bid['q']))
ask = get_swapped_order(o)
book['asks'].append(ask)
for ask in depth['asks']:
o = Order(float(ask['r']), float(ask['q']))
bid = get_swapped_order(o)
book['bids'].append(o)
else:
for bid in depth['bids']:
o = Order(float(bid['r']), float(bid['q']))
book['bids'].append(o)
for ask in depth['asks']:
o = Order(float(ask['r']), float(ask['q']))
book['asks'].append(o)
return book
def get_balance(self, currency):
data = self.authenticated_request('wallet/' + currency + '/', "getwallet")
return float(data['wallet']['available'])
def get_all_balances(self):
data = self.authenticated_request('wallet/all/', "getwallets")
balances = {k:float(v["a"]) for k,v in data["wallets"].items()}
return balances
def submit_order(self, gc, gv, rc, rv):
pass
#order_request = self.authenticated_request('market/%s/' % (working_pair), "neworder", {'order_type':order_type, 'rate':rate, 'quantity':amount,})
def confirm_order(self, orderID):
# get_order_request = self.authenticated_request('market/%s/' % (working_pair),"getorder",{'order_id':new_order_request['order']['id']})
# print get_order_request
# TODO
pass
# COINS-E specific methods
def unauthenticated_request(self, url_suffix):
url_request_object = urllib2.Request("%s/%s" % (self.base_api_url, url_suffix))
response = urllib2.urlopen(url_request_object)
response_json = {}
try:
response_content = response.read()
response_json = json.loads(response_content)
return response_json
finally:
response.close()
return "failed"
def authenticated_request(self, url_suffix, method, post_args={}):
nonce = 1000
try:
f = open('coins-e_nonce', 'r')
nonce = int(f.readline())
f.close()
finally:
f = open('coins-e_nonce', 'w')
nonce += 1
f.write(str(nonce))
f.close()
post_args['method'] = method
post_args['nonce'] = nonce
post_data = urllib.urlencode(post_args)
required_sign = hmac.new(self.secret, post_data, hashlib.sha512).hexdigest()
headers = {}
headers['key'] = self.api_key
headers['sign'] = required_sign
url_request_object = urllib2.Request("%s/%s" % (self.base_api_url, url_suffix),
post_data,
headers)
response = urllib2.urlopen(url_request_object)
try:
response_content = response.read()
response_json = json.loads(response_content)
if not response_json['status']:
print response_content
print "request failed"
print response_json['message']
return response_json
finally:
response.close()
return "failed"