reducre complexity

This commit is contained in:
gcarq 2017-05-12 22:41:49 +02:00
parent 61664fcae4
commit 55b3c13798

48
main.py
View File

@ -68,37 +68,37 @@ class TradeThread(threading.Thread):
try: try:
# Query trades from persistence layer # Query trades from persistence layer
trade = Trade.query.filter(Trade.is_open.is_(True)).first() trade = Trade.query.filter(Trade.is_open.is_(True)).first()
if trade: if not trade:
# Check if there is already an open order for this pair # Create entity and execute trade
open_orders = api_wrapper.get_open_orders(trade.pair)
if open_orders:
msg = 'There is already an open order for this trade. (total: {}, remaining: {}, type: {})'\
.format(
round(open_orders[0]['amount'], 8),
round(open_orders[0]['remaining'], 8),
open_orders[0]['type']
)
logger.info(msg)
elif close_trade_if_fulfilled(trade):
logger.info('No open orders found and close values are set. Marking trade as closed ...')
else:
# Maybe sell with current rate
handle_trade(trade)
else:
# Prepare entity and execute trade
Session.add(create_trade(float(conf['stake_amount']), api_wrapper.exchange)) Session.add(create_trade(float(conf['stake_amount']), api_wrapper.exchange))
continue
# Check if there is already an open order for this pair
open_orders = api_wrapper.get_open_orders(trade.pair)
if open_orders:
msg = 'There is already an open order for this trade. (total: {}, remaining: {}, type: {})'\
.format(
round(open_orders[0]['amount'], 8),
round(open_orders[0]['remaining'], 8),
open_orders[0]['type']
)
logger.info(msg)
elif close_trade_if_fulfilled(trade):
logger.info('No open orders found and close values are set. Marking trade as closed ...')
else:
# Maybe sell with current rate
handle_trade(trade)
except ValueError: except ValueError:
logger.exception('ValueError') logger.exception('ValueError')
except RuntimeError:
TelegramHandler.send_msg('RuntimeError. Stopping trader ...'.format(traceback.format_exc()))
logger.exception('RuntimeError. Stopping trader ...')
Session.flush()
return
finally: finally:
Session.flush() Session.flush()
time.sleep(25) time.sleep(25)
except RuntimeError:
TelegramHandler.send_msg('*Status:* Got RuntimeError: ```\n{}\n```'.format(traceback.format_exc()))
logger.exception('RuntimeError. Stopping trader ...')
finally: finally:
TelegramHandler.send_msg('*Status:* `trader has stopped`') Session.flush()
TelegramHandler.send_msg('*Status:* `Trader has stopped`')
def close_trade_if_fulfilled(trade): def close_trade_if_fulfilled(trade):