Skip to content

Commit

Permalink
bitmex logging. error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
benjyz committed Jan 11, 2019
1 parent 40c27fc commit fa70a50
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 37 deletions.
14 changes: 14 additions & 0 deletions archon/broker.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,19 +233,32 @@ def global_tradehistory(self):
x["exchange"] = n
txlist.append(x)
return txlist

def log_submit_order(self, order):
with open ('submit_orders.csv','a') as f:
f.write(str(order) + '\n')

def log_cancel_order(self, orderid):
with open ('cancel_orders.csv','a') as f:
f.write(str(orderid) + '\n')


def submit_order(self, order, exchange=None):
if exchange is None: exchange=self.selected_exchange
if exchange!=exc.BITMEX:
#TODO check balance before submit
#market,ttype,order_price,qty = order
self.log_submit_order(order)

self.submitted_orders.append(order)
[order_result,order_success] = self.afacade.submit_order(order, exchange)
logger.info("order result %s"%order_result)
else:
[order_result,order_success] = self.afacade.submit_order(order, exchange)
logger.info("order result %s"%order_result)

return [order_result,order_success]


def __old_cancel_order(self, oid):
logger.debug("cancel %s"%str(oid))
Expand All @@ -258,6 +271,7 @@ def __old_cancel_order(self, oid):

def cancel_order(self, oid, exchange):
logger.debug("cancel %s"%str(oid))
self.log_cancel_order(oid)
result = self.afacade.cancel_id(oid, exchange=exchange)
return result

Expand Down
45 changes: 25 additions & 20 deletions archon/exchange/bitmex/bitmex.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
from .accessTokenAuth import AccessTokenAuth
from .apiKeyAuthWithExpires import APIKeyAuthWithExpires

from loguru import logger

API_BASE = 'https://www.bitmex.com/api/v1/'
# https://www.bitmex.com/api/explorer/

Expand All @@ -22,6 +24,9 @@ class BitMEX(object):

def __init__(self, base_url=API_BASE, symbol=None, login=None, password=None, otpToken=None,
apiKey=None, apiSecret=None, orderIDPrefix='mm_bitmex_'):

logger.start("log/bitmex.log", rotation="500 MB")
logger.debug("init bitmex")
self.base_url = base_url
self.symbol = symbol
self.token = None
Expand Down Expand Up @@ -60,13 +65,13 @@ def get_instrument(self, symbol):
path = "instrument"
instruments = self._query_bitmex(path=path, query={'filter': json.dumps({'symbol': symbol})})
if len(instruments) == 0:
print("Instrument not found: %s." % self.symbol)
exit(1)
logger.error("Instrument not found: %s." % self.symbol)
#logger.error

instrument = instruments[0]
if instrument["state"] != "Open":
print("The instrument %s is no longer open. State: %s" % (self.symbol, instrument["state"]))
exit(1)
logger.error("The instrument %s is no longer open. State: %s" % (self.symbol, instrument["state"]))
#logger.error

# tickLog is the log10 of tickSize
instrument['tickLog'] = int(math.fabs(math.log10(instrument['tickSize'])))
Expand Down Expand Up @@ -100,7 +105,7 @@ def recent_trades(self, symbol):
#'start': 0,
#'filter':
}
print ("query ",query)
logger.debug("query ",query)
result = self._query_bitmex(path=path,query=query)
return result

Expand Down Expand Up @@ -177,7 +182,7 @@ def place_order(self, symbol, quantity, price):
'price': price,
'clOrdID': clOrdID
}
print ("post dict ",postdict)
logger.debug("post dict ",postdict)
return self._query_bitmex(path=endpoint, postdict=postdict, verb="POST")

@authentication_required
Expand Down Expand Up @@ -235,42 +240,42 @@ def _query_bitmex(self, path, query=None, postdict=None, timeout=3, verb=None):
# 401 - Auth error. Re-auth and re-run this request.
if response.status_code == 401:
if self.token is None:
print("Login information or API Key incorrect, please check and restart.")
print("Error: " + response.text)
logger.error("Login information or API Key incorrect, please check and restart.")
logger.error("Error: " + response.text)
if postdict:
print(postdict)
exit(1)
print("Token expired, reauthenticating...")
logger.error(postdict)
#logger.error
logger.error("Token expired, reauthenticating...")
sleep(1)
self.authenticate()
return self._query_bitmex(path, query, postdict, timeout, verb)

# 404, can be thrown if order canceled does not exist.
elif response.status_code == 404:
if verb == 'DELETE':
print("Order not found: %s" % postdict['orderID'])
logger.error("Order not found: %s" % postdict['orderID'])
return
print("Unable to contact the BitMEX API (404). Request: %s \n %s" % (url, json.dumps(postdict)))
exit(1)
logger.error("Unable to contact the BitMEX API (404). Request: %s \n %s" % (url, json.dumps(postdict)))
raise Exception("bitmex connection")

# 503 - BitMEX temporary downtime, likely due to a deploy. Try again
elif response.status_code == 503:
print("Unable to contact the BitMEX API (503), retrying. Request: %s \n %s" % (url, json.dumps(postdict)))
logger.error("Unable to contact the BitMEX API (503), retrying. Request: %s \n %s" % (url, json.dumps(postdict)))
sleep(1)
return self._query_bitmex(path, query, postdict, timeout, verb)
# Unknown Error
else:
print("Unhandled Error:", e, response.text)
print("Endpoint was: %s %s" % (verb, path))
exit(1)
logger.error("Unhandled Error:", e, response.text)
logger.error("Endpoint was: %s %s" % (verb, path))
raise Exception("bitmex connection")

except requests.exceptions.Timeout as e:
# Timeout, re-run this request
print("Timed out, retrying...")
logger.error("Timed out, retrying...")
return self._query_bitmex(path, query, postdict, timeout, verb)

except requests.exceptions.ConnectionError as e:
print("Unable to contact the BitMEX API (ConnectionError). Please check the URL. Retrying. Request: %s \n %s" % (url, json.dumps(postdict)))
logger.error("Unable to contact the BitMEX API (ConnectionError). Please check the URL. Retrying. Request: %s \n %s" % (url, json.dumps(postdict)))
sleep(1)
return self._query_bitmex(path, query, postdict, timeout, verb)

Expand Down
17 changes: 10 additions & 7 deletions archon/facade.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,12 +190,12 @@ def get_orderbook(self, market, exchange=None):
except Exception:
raise Exception

elif exchange==exc.BITMEX:
logger.info("get orderbook %s"%(market))
bookdepth = 10
elif exchange==exc.BITMEX:
logger.debug("get orderbook %s"%(market))
bookdepth = 20
ob = client.market_depth(market,depth=bookdepth)
logger.debug("book %s"%ob)
book = models.conv_orderbook(ob, exchange)
#print (book)
return book

def get_market_summary(self, market, exchange):
Expand Down Expand Up @@ -673,9 +673,12 @@ def open_orders(self, exchange=None):

elif exchange==exc.BITMEX:
#TODO
symbol = "XBTUSD"
oo = client.open_orders(symbol=symbol)
logger.info("open orders %s"%str(oo))
try:
symbol = "XBTUSD"
oo = client.open_orders(symbol=symbol)
logger.debug("open orders %s"%str(oo))
except Exception as e:
logger.error("error %s"%str(e))
n = exc.NAMES[exchange]
#logger.info("open orders " + str(n) + " " + str(oo))
return oo
Expand Down
11 changes: 1 addition & 10 deletions archon/workers.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def sync_job(self):
col = db.orderbooks #['bitmex_orderbook']

#i = 0
logger.debug('sync orderbook in the background')
logger.debug('sync in the background')
#while True:
market = m.market_from("XBT","USD")
smarket = models.conv_markets_to(market, exc.BITMEX)
Expand All @@ -91,12 +91,3 @@ def sync_job(self):
market = models.market_from("XBT","USD")
self.broker.sync_orderbook(market, exc.BITMEX)
"""

def some_job():
t = datetime.datetime.now()
print ("job %s"%t)

def start_schedule():
scheduler = BlockingScheduler()
scheduler.add_job(some_job, 'interval', seconds=5)
scheduler.start()

0 comments on commit fa70a50

Please sign in to comment.