forked from ericjang/cryptocurrency_arbitrage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBTER.py
143 lines (130 loc) · 4.87 KB
/
BTER.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
135
136
137
138
139
140
141
142
143
from Exchange import Exchange
import bterapi
import os
from order import Order
class BTER(Exchange):
def __init__(self, keyfile):
keyfile = os.path.abspath(keyfile)
self.keyhandler = bterapi.KeyHandler(keyfile)
key = self.keyhandler.getKeys()[0]
self.conn = bterapi.BTERConnection()
self.api = bterapi.TradeAPI(key, self.keyhandler)
super(BTER, self).__init__()
self.name = 'BTER'
self.trading_fee = 0.002
def get_tradeable_pairs(self):
tradeable_pairs = []
for pair in bterapi.all_pairs:
a, b = pair.split("_")
tradeable_pairs.append((a.upper(), b.upper()))
return tradeable_pairs
def get_depth(self, base, alt):
book = {'bids': [], 'asks': []}
pair, swapped = self.get_validated_pair((base, alt))
if pair is None:
return
pairstr = pair[0].lower() + "_" + pair[1].lower()
asks, bids = bterapi.getDepth(pairstr)
book['bids'] = [Order(float(b[0]), float(b[1])) for b in bids]
book['asks'] = [Order(float(a[0]), float(a[1])) for a in asks]
return book
def get_balance(self, currency):
funds = self.api.getFunds(self.conn, error_handler=None)
if currency in funds:
return float(funds[currency])
else:
return 0.0
# data = self.api.getInfo(connection = self.conn)
#return getattr(data, 'balance_' + currency.lower())
def get_all_balances(self):
funds = self.api.getFunds(self.conn, error_handler=None)
return {k:float(v) for k,v in funds.items()}
def submit_order(self, gc, gv, rc, rv):
return NotImplemented
# pair, swapped = self.get_validated_pair((rc, gc))
# print swapped
# if pair is None:
# return
# pairstr = pair[0].lower() + "_" + pair[1].lower()
# if swapped:
# price = rv/gv
# self.api.trade(pairstr, 'sell', price, gv)
# else:
# price = gv/rv
# self.api.trade(pairstr, 'buy', price, rv)
def confirm_order(self, orderID):
pass
# from Exchange import Exchange
# from PairList import PairList
# import bterapi
#
# class BTER(Exchange):
# """docstring for BTER"""
# def __init__(self, mode, keyfile):
# super(BTER, self).__init__(mode)
# self.name = 'BTER'
# self.full_orderbook = False
# self.trading_fee = 0.002
# # bterapi already kindly provides us with a list of supported pairs
# for pair in bterapi.all_pairs:
# a, b = pair.split("_")
# self.supported_pairs.add_pair((a.upper(), b.upper()))
# # set up API
# self.keyhandler = bterapi.KeyHandler(keyfile)
# key = self.keyhandler.getKeys()[0]
# self.conn = bterapi.BTERConnection()
# self.api = bterapi.TradeAPI(key, self.keyhandler)
#
# def live_order(self, recv_currency, recv_volume, give_currency, give_volume):
#
# return NotImplemented
#
# def update_live_balance(self, currency=None):
# data = self.api.getFunds(self.conn, error_handler=None)
# foo = 1
# return NotImplemented
#
# def update_orders(self, pair):
# a, b = pair
# pairstr = a.lower() + "_" + b.lower()
# pairstr, flipped = self.check_swapped(pairstr)
# if flipped:
# alt, base = pair
# else:
# base, alt = pair
# # base, alt not necessarily the same as one dictated in ArbitrageBot class
# asks, bids = bterapi.getDepth(pairstr)
# for bid in bids:
# rate = float(bid[0])
# give_vol = float(bid[1])
# recv_vol = give_vol * rate
# order = {"give_currency" : base, \
# "recv_currency" : alt, \
# "give_volume" : give_vol, \
# "recv_volume" : recv_vol }
# self.orderbook.set_order(order)
# for ask in asks:
# rate = float(ask[0])
# recv_vol = float(ask[1])
# give_vol = recv_vol * rate
# order = {"give_currency" : alt, \
# "recv_currency" : base, \
# "give_volume" : give_vol, \
# "recv_volume" : recv_vol }
# self.orderbook.set_order(order)
#
#
# def check_swapped(self, pairstr):
# '''
# returns swapped_pair, True if pair has been swapped
# else returns pairstr, False
# '''
# if pairstr not in bterapi.all_pairs:
# if "_" in pairstr:
# a, b = pairstr.split("_")
# swapped_pair = "%s_%s" % (b.lower(), a.lower())
# if swapped_pair in bterapi.all_pairs:
# return swapped_pair, True
# msg = "unrecognized pair " + pairstr
# raise Exception(msg)
# return pairstr, False