def main(): candidates = ['arbitrage.ini', 'basebit.ini' ] if len(sys.argv) > 1: candidates.append(sys.argv[1]) config = ConfigParser.SafeConfigParser({ 'websocket_url': 'wss://127.0.0.1/trade/', 'username': '', 'password': '', 'buy_fee': 0, 'sell_fee': 0, 'api_key': 'KEY', 'api_secret': 'SECRET', 'subscription_api_key':'api_key' }) config.read( candidates ) websocket_url = config.get('bitinvest', 'websocket_url') username = config.get('bitinvest', 'username') password = config.get('bitinvest', 'password') buy_fee = int(config.get('bitinvest', 'buy_fee')) sell_fee = int(config.get('bitinvest', 'sell_fee')) api_key = config.get('bitinvest', 'api_key') api_secret = config.get('bitinvest', 'api_secret') subscription_api_key = config.get('bitinvest', 'subscription_api_key') arbitrator = BlinkTradeArbitrator(username,password,websocket_url, 'BTCBRL') arbitrator.connect() arbitrator.signal_order.connect(send_order_to_BINVEST) while True: try: sleep(15) if arbitrator.is_connected(): arbitrator.send_testRequest() else: try: arbitrator.reconnect() except HandshakeError,e: continue try: # something wrong with urllib2 or bitinvest servers. #raw_data = urllib2.urlopen('https://api.bitinvest.com.br/exchange/orderbook?subscription-key=' + subscription_api_key).read() # curl works. I know, this is ugly, but it works api_url = 'https://api.bitinvest.com.br/exchange/orderbook?subscription-key=' + subscription_api_key raw_data = subprocess.check_output( ['curl', api_url ] ) except Exception: print 'ERROR RETRIEVING ORDER BOOK' continue bids_asks = [] try: bids_asks = json.loads(raw_data) except Exception : pass if bids_asks: ask_list = [ [ int(float(o[0]) * 1e8 * (1. + sell_fee) ) , int(o[1] * 1e8) ] for o in bids_asks['asks'] ] bid_list = [ [ int(float(o[0]) * 1e8 * (1. + buy_fee) ) , int(o[1] * 1e8) ] for o in bids_asks['bids'] ] arbitrator.process_ask_list(ask_list) arbitrator.process_bid_list(bid_list) except urllib2.URLError as e: print datetime.datetime.now(), e
def main(): candidates = ['arbitrage.ini', 'basebit.ini' ] if len(sys.argv) > 1: candidates.append(sys.argv[1]) config = ConfigParser.SafeConfigParser({ 'websocket_url': 'wss://127.0.0.1/trade/', 'username': '', 'password': '', 'buy_fee': 0, 'sell_fee': 0, 'api_key': 'KEY', 'api_secret': 'SECRET' }) config.read( candidates ) websocket_url = config.get('basebit', 'websocket_url') username = config.get('basebit', 'username') password = config.get('basebit', 'password') buy_fee = int(config.get('basebit', 'buy_fee')) sell_fee = int(config.get('basebit', 'sell_fee')) api_key = config.get('basebit', 'api_key') api_secret = config.get('basebit', 'api_secret') broker_id = config.getint('basebit', 'broker_id') dest_market = config.get('basebit', 'dest_market') print 'websocket_url:', websocket_url print 'username:'******'buy_fee:', buy_fee print 'sell_fee:', sell_fee arbitrator = BlinkTradeArbitrator(broker_id, username,password,websocket_url, dest_market ) arbitrator.connect() arbitrator.signal_order.connect(send_order_to_basebit) while True: try: sleep(10) if arbitrator.is_connected(): arbitrator.send_testRequest() else: try: arbitrator.reconnect() except HandshakeError,e: continue try: raw_data = urllib2.urlopen('http://www.basebit.com.br/book-BTC_BRL').read() except Exception: print 'ERROR RETRIEVING ORDER BOOK' continue bids_asks = [] try: bids_asks = json.loads(raw_data) except Exception : pass if bids_asks: ask_list = [ [ int(float(o['price']) * 1e8 * (1. + sell_fee) ) , int(o['quantity'] * 1e8) ] for o in bids_asks['result']['asks'] ] bid_list = [ [ int(float(o['price']) * 1e8 * (1. + buy_fee) ) , int(o['quantity'] * 1e8) ] for o in bids_asks['result']['bids'] ] arbitrator.process_ask_list(ask_list) arbitrator.process_bid_list(bid_list) except urllib2.URLError as e: print datetime.datetime.now(), e
class BitStampClient(object): def __init__(self, broker_id, username, password, websocket_url, bid_fee=0., ask_fee=0., api_key=None, api_secret=None, dest_market='BTCUSD'): self.api_key = api_key self.api_secret = api_secret self.bid_fee = bid_fee self.ask_fee = ask_fee self.broker_id = broker_id self.username = username self.password = password self.websocket_url = websocket_url self.pusher = pusherclient.Pusher(key='de504dc5763aeef9ff52', log_level=logging.ERROR) self.pusher.connection.bind('pusher:connection_established', self.on_bitstamp_connect_handler) self.pusher.connection.bind('pusher:connection_failed', self.on_bitstamp_connect_failed_handler) self.arbitrator = BlinkTradeArbitrator(self.broker_id, self.username, self.password, self.websocket_url, dest_market) self.arbitrator.signal_order.connect(self.on_send_order_to_bitstamp) self.arbitrator.signal_logged.connect(self.on_blinktrade_logged) self.arbitrator.signal_disconnected.connect( self.on_blinktrade_discconnected) self.arbitrator.signal_connected.connect(self.on_blinktrade_connected) def on_blinktrade_discconnected(self, sender, code_reason): print datetime.datetime.now( ), 'CLOSED', 'websocket closed. code:', code_reason[ 0], 'reason', code_reason[1] def on_blinktrade_logged(self, sender, data): print 'logged to blinktrade' self.arbitrator.cancel_all_orders() def on_blinktrade_connected(self, sender, data): print 'Connected to blinktrade' self.arbitrator.send_testRequest() def connect(self): print 'connecting....' self.arbitrator.connect() self.pusher.connect() def cancel_all_orders(self): if self.arbitrator.is_logged(): self.arbitrator.cancel_all_orders() def keep_alive(self): if self.arbitrator.is_connected(): self.arbitrator.send_testRequest() def on_bitstamp_connect_failed_handler(self, data): print 'Disconnected from bitstamp. Trying to reconnect within 10 minutes' if self.arbitrator.is_connected(): self.arbitrator.cancel_all_orders() self.pusher.connect() # reconnect to pusher def on_bitstamp_connect_handler(self, data): print 'connected to bitstamp' channel = self.pusher.subscribe('order_book') channel.bind('data', self.on_bitstamp_order_book_handler) def on_bitstamp_order_book_handler(self, data): if not self.arbitrator.is_logged(): return data = json.loads(data) bid_list = [[ int(float(usd) * 1e8 * (1. - self.bid_fee)), int(float(btc) * 1e8) ] for usd, btc in data['bids']] ask_list = [[ int(float(usd) * 1e8 * (1. + self.ask_fee)), int(float(btc) * 1e8) ] for usd, btc in data['asks']] self.arbitrator.process_ask_list(ask_list) self.arbitrator.process_bid_list(bid_list) def on_send_order_to_bitstamp(self, sender, msg): nonce = datetime.datetime.now().strftime('%s') message = str(nonce) + '.blinktrade.' + str(self.api_key) signature = hmac.new(self.api_secret, msg=message, digestmod=hashlib.sha256).hexdigest().upper() post_params = { 'key': self.api_key, 'signature': signature, 'nonce': nonce, 'amount': float(msg['LastShares'] / 1.e8), 'price': float(msg['Price'] / 1.e8) } if msg['Side'] == '1': print datetime.datetime.now( ), 'POST https://www.bitstamp.net/api/sell/', str(post_params) elif msg['Side'] == '2': print datetime.datetime.now( ), 'POST https://www.bitstamp.net/api/buy/', str(post_params)
def main(): candidates = ['arbitrage.ini', 'mb.ini' ] if len(sys.argv) > 1: candidates.append(sys.argv[1]) config = ConfigParser.SafeConfigParser({ 'websocket_url': 'wss://127.0.0.1/trade/', 'username': '', 'password': '', 'buy_fee': 0, 'sell_fee': 0, 'api_key': 'KEY', 'api_secret': 'SECRET' }) config.read( candidates ) websocket_url = config.get('mb', 'websocket_url') username = config.get('mb', 'username') password = config.get('mb', 'password') buy_fee = int(config.get('mb', 'buy_fee')) sell_fee = int(config.get('mb', 'sell_fee')) api_key = config.get('mb', 'api_key') api_secret = config.get('mb', 'api_secret') print 'websocket_url:', websocket_url print 'username:'******'buy_fee:', buy_fee print 'sell_fee:', sell_fee arbitrator = BlinkTradeArbitrator(username,password,websocket_url, 'BTCUSD' ) #'BTCBRL') arbitrator.connect() arbitrator.signal_order.connect(send_order_to_MB) while True: try: sleep(5) if arbitrator.is_connected(): arbitrator.send_testRequest() else: try: arbitrator.reconnect() except HandshakeError,e: continue try: raw_data = urllib2.urlopen('https://www.mercadobitcoin.com.br/api/orderbook/').read() except Exception: print 'ERROR RETRIEVING ORDER BOOK' continue bids_asks = [] try: bids_asks = json.loads(raw_data) except Exception : pass if bids_asks: ask_list = [ [ int(float(o[0]) * 1e8 * (1. + sell_fee) ) , int(o[1] * 1e8) ] for o in bids_asks['asks'] ] bid_list = [ [ int(float(o[0]) * 1e8 * (1. + buy_fee) ) , int(o[1] * 1e8) ] for o in bids_asks['bids'] ] arbitrator.process_ask_list(ask_list) arbitrator.process_bid_list(bid_list) except urllib2.URLError as e: print datetime.datetime.now(), e
def main(): candidates = ['arbitrage.ini', 'itbit.ini'] if len(sys.argv) > 1: candidates.append(sys.argv[1]) config = ConfigParser.SafeConfigParser({ 'websocket_url': 'wss://127.0.0.1/trade/', 'username': '', 'password': '', 'buy_fee': 0, 'sell_fee': 0, 'api_key': 'KEY', 'api_secret': 'SECRET' }) config.read(candidates) websocket_url = config.get('itbit', 'websocket_url') username = config.get('itbit', 'username') password = config.get('itbit', 'password') buy_fee = float(config.get('itbit', 'buy_fee')) sell_fee = float(config.get('itbit', 'sell_fee')) api_key = config.get('itbit', 'api_key') api_secret = config.get('itbit', 'api_secret') broker_id = config.getint('itbit', 'broker_id') dest_market = config.get('itbit', 'dest_market') arbitrator = BlinkTradeArbitrator(broker_id, username, password, websocket_url, dest_market) arbitrator.connect() arbitrator.signal_order.connect(send_order) while True: try: sleep(1) if arbitrator.is_connected(): arbitrator.send_testRequest() else: try: arbitrator.reconnect() except HandshakeError, e: continue try: raw_data = urllib2.urlopen( 'https://www.itbit.com/api/v2/markets/XBTUSD/orders').read( ) except Exception: print 'ERROR RETRIEVING ORDER BOOK' continue bids_asks = [] try: bids_asks = json.loads(raw_data) except Exception: pass if bids_asks: bid_list = [[ int(float(o[0]) * 1e8 * (1. + buy_fee)), int(float(o[1]) * 1e8) ] for o in bids_asks['bids']] ask_list = [[ int(float(o[0]) * 1e8 * (1. + sell_fee)), int(float(o[1]) * 1e8) ] for o in bids_asks['asks']] arbitrator.process_ask_list(ask_list) arbitrator.process_bid_list(bid_list) except urllib2.URLError as e: print datetime.datetime.now(), e
class BitStampClient(object): def __init__(self, broker_id,username,password,websocket_url, bid_fee=0., ask_fee=0., api_key=None, api_secret=None): self.api_key = api_key self.api_secret = api_secret self.bid_fee = bid_fee self.ask_fee = ask_fee self.broker_id = broker_id self.username = username self.password = password self.websocket_url = websocket_url self.pusher = pusherclient.Pusher(key= 'de504dc5763aeef9ff52', log_level=logging.ERROR) self.pusher.connection.bind('pusher:connection_established', self.on_bitstamp_connect_handler) self.pusher.connection.bind('pusher:connection_failed', self.on_bitstamp_connect_failed_handler) self.arbitrator = BlinkTradeArbitrator( self.broker_id, self.username,self.password,self.websocket_url, 'BTCUSD') self.arbitrator.signal_order.connect(self.on_send_order_to_bitstamp) self.arbitrator.signal_logged.connect(self.on_blinktrade_logged) self.arbitrator.signal_disconnected.connect(self.on_blinktrade_discconnected) self.arbitrator.signal_connected.connect(self.on_blinktrade_connected) def on_blinktrade_discconnected(self, sender, code_reason): print datetime.datetime.now(), 'CLOSED', 'websocket closed. code:', code_reason[0], 'reason', code_reason[1] def on_blinktrade_logged(self, sender, data): print 'logged to blinktrade' self.arbitrator.cancel_all_orders() def on_blinktrade_connected(self, sender, data): print 'Connected to blinktrade' self.arbitrator.send_testRequest() def connect(self): print 'connecting....' self.arbitrator.connect() self.pusher.connect() def cancel_all_orders(self): if self.arbitrator.is_logged(): self.arbitrator.cancel_all_orders() def keep_alive(self): if self.arbitrator.is_connected(): self.arbitrator.send_testRequest() def on_bitstamp_connect_failed_handler(self, data): print 'Disconnected from bitstamp. Trying to reconnect within 10 minutes' if self.arbitrator.is_connected(): self.arbitrator.cancel_all_orders() self.pusher.connect() # reconnect to pusher def on_bitstamp_connect_handler(self, data): print 'connected to bitstamp' channel = self.pusher.subscribe('order_book') channel.bind('data', self.on_bitstamp_order_book_handler ) def on_bitstamp_order_book_handler(self, data): if not self.arbitrator.is_logged(): return data = json.loads(data) bid_list = [ [ int(float(usd)*1e8 * (1. - self.bid_fee) ), int(float(btc) * 1e8) ] for usd,btc in data['bids'] ] ask_list = [ [ int(float(usd)*1e8 * (1. + self.ask_fee) ), int(float(btc) * 1e8) ] for usd,btc in data['asks'] ] self.arbitrator.process_ask_list(ask_list) self.arbitrator.process_bid_list(bid_list) def on_send_order_to_bitstamp(self, sender, msg): nonce = datetime.datetime.now().strftime('%s') message = str(nonce) + '.blinktrade.' + str(self.api_key) signature = hmac.new(self.api_secret, msg=message, digestmod=hashlib.sha256).hexdigest().upper() post_params = { 'key': self.api_key, 'signature': signature, 'nonce': nonce, 'amount': float(msg['LastShares']/1.e8), 'price': float( msg['Price'] / 1.e8) } if msg['Side'] == '1': print datetime.datetime.now(), 'POST https://www.bitstamp.net/api/sell/', str(post_params) elif msg['Side'] == '2': print datetime.datetime.now(), 'POST https://www.bitstamp.net/api/buy/', str(post_params)
def main(): candidates = ['arbitrage.ini', 'hitbtc.ini' ] if len(sys.argv) > 1: candidates.append(sys.argv[1]) config = ConfigParser.SafeConfigParser({ 'websocket_url': 'wss://127.0.0.1/trade/', 'username': '', 'password': '', 'buy_fee': 0, 'sell_fee': 0, 'api_key': 'KEY', 'api_secret': 'SECRET' }) config.read( candidates ) websocket_url = config.get('hitbtc', 'websocket_url') username = config.get('hitbtc', 'username') password = config.get('hitbtc', 'password') buy_fee = int(config.get('hitbtc', 'buy_fee')) sell_fee = int(config.get('hitbtc', 'sell_fee')) api_key = config.get('hitbtc', 'api_key') api_secret = config.get('hitbtc', 'api_secret') broker_id = config.getint('hitbtc', 'broker_id') dest_market = config.get('hitbtc', 'dest_market') arbitrator = BlinkTradeArbitrator(broker_id, username,password,websocket_url, dest_market) arbitrator.connect() arbitrator.signal_order.connect(send_order) while True: try: sleep(1) if arbitrator.is_connected(): arbitrator.send_testRequest() else: try: arbitrator.reconnect() except HandshakeError,e: continue try: raw_data = urllib2.urlopen('https://api.hitbtc.com/api/1/public/BTCUSD/orderbook?format_price=number&format_amount=number').read() except Exception: print 'ERROR RETRIEVING ORDER BOOK' continue bids_asks = [] try: bids_asks = json.loads(raw_data) except Exception : pass if bids_asks: bid_list = [ [ int(float(o[0]) * 1e8 * (1. + buy_fee) ) , int( float(o[1]) * 1e8) ] for o in bids_asks['bids'] ] ask_list = [ [ int(float(o[0]) * 1e8 * (1. + sell_fee) ) , int( float(o[1]) * 1e8) ] for o in bids_asks['asks'] ] arbitrator.process_ask_list(ask_list) arbitrator.process_bid_list(bid_list) except urllib2.URLError as e: print datetime.datetime.now(), e
def main(): candidates = ['arbitrage.ini', 'b2u.ini' ] if len(sys.argv) > 1: candidates.append(sys.argv[1]) config = ConfigParser.SafeConfigParser({ 'websocket_url': 'wss://127.0.0.1/trade/', 'username': '', 'password': '', 'buy_fee': 0, 'sell_fee': 0, 'api_key': 'KEY', 'api_secret': 'SECRET' }) config.read( candidates ) websocket_url = config.get('b2u', 'websocket_url') username = config.get('b2u', 'username') password = config.get('b2u', 'password') buy_fee = int(config.get('b2u', 'buy_fee')) sell_fee = int(config.get('b2u', 'sell_fee')) api_key = config.get('b2u', 'api_key') api_secret = config.get('b2u', 'api_secret') print 'websocket_url:', websocket_url print 'username:'******'buy_fee:', buy_fee print 'sell_fee:', sell_fee arbitrator = BlinkTradeArbitrator(username,password,websocket_url, 'BTCBRL') arbitrator.connect() arbitrator.signal_order.connect(send_order_to_b2u) while True: try: sleep(10) if arbitrator.is_connected(): arbitrator.send_testRequest() else: try: arbitrator.reconnect() except HandshakeError,e: continue try: raw_data = urllib2.urlopen('http://www.bitcointoyou.com/API/orderbook.aspx').read() except Exception: print 'ERROR RETRIEVING ORDER BOOK' continue bids_asks = [] try: bids_asks = json.loads(raw_data) except Exception : try: bids_asks = json.loads(raw_data.replace('][','],[')) # bug with b2u api except Exception : pass pass if bids_asks: ask_list = [ [ int(float(fiat)*1e8 * (1. + sell_fee) ), int(float(btc) * 1e8) ] for fiat,btc in reversed(bids_asks['asks']) ] bid_list = [ [ int(float(fiat)*1e8 * (1. - buy_fee) ), int(float(btc) * 1e8) ] for fiat,btc in bids_asks['bids'] ] number_of_asks_to_remove_due_a_weird_bug = 0 for ask_price, ask_size in ask_list: if ask_price < bid_list[0][0]: number_of_asks_to_remove_due_a_weird_bug += 1 else: break if number_of_asks_to_remove_due_a_weird_bug: print datetime.datetime.now(), 'Those sell orders are weird => ', [ 'BTC {:,.8f}'.format(s/1e8) + ' @ R$ {:,.2f}'.format(p/1e8) for p, s in ask_list[:number_of_asks_to_remove_due_a_weird_bug] ] ask_list = ask_list[number_of_asks_to_remove_due_a_weird_bug:] arbitrator.process_ask_list(ask_list) arbitrator.process_bid_list(bid_list) except urllib2.URLError as e: print datetime.datetime.now(), e
def main(): candidates = ['arbitrage.ini', 'b2u.ini'] if len(sys.argv) > 1: candidates.append(sys.argv[1]) config = ConfigParser.SafeConfigParser({ 'websocket_url': 'wss://127.0.0.1/trade/', 'username': '', 'password': '', 'broker_id': 5, 'buy_fee': 0, 'sell_fee': 0, 'api_key': 'KEY', 'api_secret': 'SECRET' }) config.read(candidates) websocket_url = config.get('b2u', 'websocket_url') username = config.get('b2u', 'username') password = config.get('b2u', 'password') buy_fee = int(config.get('b2u', 'buy_fee')) sell_fee = int(config.get('b2u', 'sell_fee')) api_key = config.get('b2u', 'api_key') api_secret = config.get('b2u', 'api_secret') broker_id = config.getint('b2u', 'broker_id') dest_market = config.get('b2u', 'dest_market') print 'websocket_url:', websocket_url print 'username:'******'buy_fee:', buy_fee print 'sell_fee:', sell_fee arbitrator = BlinkTradeArbitrator(broker_id, username, password, websocket_url, dest_market) arbitrator.connect() #arbitrator.signal_order.connect(send_order_to_b2u) while True: try: sleep(10) if arbitrator.is_connected(): arbitrator.send_testRequest() else: try: arbitrator.reconnect() except HandshakeError, e: continue try: raw_data = urllib2.urlopen( 'https://www.bitcointoyou.com/API/orderbook.aspx').read() except Exception as e: print 'ERROR RETRIEVING ORDER BOOK: ' + str(e) continue bids_asks = [] try: bids_asks = json.loads(raw_data) except Exception: try: bids_asks = json.loads(raw_data.replace( '][', '],[')) # bug with b2u api except Exception: pass pass if bids_asks: ask_list = [[ int(float(fiat) * 1e8 * (1. + sell_fee)), int(float(btc) * 1e8) ] for fiat, btc in bids_asks['asks']] bid_list = [[ int(float(fiat) * 1e8 * (1. - buy_fee)), int(float(btc) * 1e8) ] for fiat, btc in bids_asks['bids']] number_of_asks_to_remove_due_a_weird_bug = 0 for ask_price, ask_size in ask_list: if ask_price < bid_list[0][0]: number_of_asks_to_remove_due_a_weird_bug += 1 else: break if number_of_asks_to_remove_due_a_weird_bug: print datetime.datetime.now( ), 'Those sell orders are weird => ', [ 'BTC {:,.8f}'.format(s / 1e8) + ' @ R$ {:,.2f}'.format(p / 1e8) for p, s in ask_list[:number_of_asks_to_remove_due_a_weird_bug] ] ask_list = ask_list[number_of_asks_to_remove_due_a_weird_bug:] arbitrator.process_ask_list(ask_list) arbitrator.process_bid_list(bid_list) except urllib2.URLError as e: print datetime.datetime.now(), e
def main(): candidates = ["arbitrage.ini", "mb.ini"] if len(sys.argv) > 1: candidates.append(sys.argv[1]) config = ConfigParser.SafeConfigParser( { "websocket_url": "wss://127.0.0.1/trade/", "username": "", "password": "", "buy_fee": 0, "sell_fee": 0, "api_key": "KEY", "api_secret": "SECRET", } ) config.read(candidates) websocket_url = config.get("mb", "websocket_url") username = config.get("mb", "username") password = config.get("mb", "password") buy_fee = int(config.get("mb", "buy_fee")) sell_fee = int(config.get("mb", "sell_fee")) api_key = config.get("mb", "api_key") api_secret = config.get("mb", "api_secret") print "websocket_url:", websocket_url print "username:"******"buy_fee:", buy_fee print "sell_fee:", sell_fee arbitrator = BlinkTradeArbitrator(username, password, websocket_url, "BTCUSD") #'BTCBRL') arbitrator.connect() arbitrator.signal_order.connect(send_order_to_MB) while True: try: sleep(5) if arbitrator.is_connected(): arbitrator.send_testRequest() else: try: arbitrator.reconnect() except HandshakeError, e: continue try: raw_data = urllib2.urlopen("https://www.mercadobitcoin.com.br/api/orderbook/").read() except Exception: print "ERROR RETRIEVING ORDER BOOK" continue bids_asks = [] try: bids_asks = json.loads(raw_data) except Exception: pass if bids_asks: ask_list = [[int(float(o[0]) * 1e8 * (1.0 + sell_fee)), int(o[1] * 1e8)] for o in bids_asks["asks"]] bid_list = [[int(float(o[0]) * 1e8 * (1.0 + buy_fee)), int(o[1] * 1e8)] for o in bids_asks["bids"]] arbitrator.process_ask_list(ask_list) arbitrator.process_bid_list(bid_list) except urllib2.URLError as e: print datetime.datetime.now(), e
def main(): candidates = ['arbitrage.ini', 'basebit.ini' ] if len(sys.argv) > 1: candidates.append(sys.argv[1]) config = ConfigParser.SafeConfigParser({ 'websocket_url': 'wss://127.0.0.1/trade/', 'username': '', 'password': '', 'buy_fee': 0, 'sell_fee': 0, 'api_key': 'KEY', 'api_secret': 'SECRET', 'subscription_api_key':'api_key' }) config.read( candidates ) websocket_url = config.get('bitinvest', 'websocket_url') username = config.get('bitinvest', 'username') password = config.get('bitinvest', 'password') buy_fee = int(config.get('bitinvest', 'buy_fee')) sell_fee = int(config.get('bitinvest', 'sell_fee')) api_key = config.get('bitinvest', 'api_key') api_secret = config.get('bitinvest', 'api_secret') subscription_api_key = config.get('bitinvest', 'subscription_api_key') broker_id = config.getint('bitinvest', 'broker_id') dest_market = config.get('bitinvest', 'dest_market') arbitrator = BlinkTradeArbitrator(broker_id, username,password,websocket_url, dest_market) arbitrator.connect() arbitrator.signal_order.connect(send_order_to_BINVEST) while True: try: sleep(15) if arbitrator.is_connected(): arbitrator.send_testRequest() else: try: arbitrator.reconnect() except HandshakeError,e: continue try: # something wrong with urllib2 or bitinvest servers. #raw_data = urllib2.urlopen('https://api.bitinvest.com.br/exchange/orderbook?subscription-key=' + subscription_api_key).read() # curl works. I know, this is ugly, but it works api_url = 'https://api.bitinvest.com.br/exchange/orderbook?subscription-key=' + subscription_api_key raw_data = subprocess.check_output( ['curl', api_url ] ) except Exception: print 'ERROR RETRIEVING ORDER BOOK' continue bids_asks = [] try: bids_asks = json.loads(raw_data) except Exception : pass if bids_asks: ask_list = [ [ int(float(o[0]) * 1e8 * (1. + sell_fee) ) , int(o[1] * 1e8) ] for o in bids_asks['asks'] ] bid_list = [ [ int(float(o[0]) * 1e8 * (1. + buy_fee) ) , int(o[1] * 1e8) ] for o in bids_asks['bids'] ] arbitrator.process_ask_list(ask_list) arbitrator.process_bid_list(bid_list) except urllib2.URLError as e: print datetime.datetime.now(), e