Пример #1
0
 def run(self):
     """
     Threaded main function
     :return: None
     """
     try:
         TelegramHandler.send_msg('*Status:* `trader started`')
         logger.info('Trader started')
         while not _should_stop:
             try:
                 self._process()
             except (ConnectionError, JSONDecodeError, ValueError) as error:
                 msg = 'Got {} during _process()'.format(
                     error.__class__.__name__)
                 logger.exception(msg)
             finally:
                 Session.flush()
                 time.sleep(25)
     except (RuntimeError, JSONDecodeError) as e:
         TelegramHandler.send_msg(
             '*Status:* Got RuntimeError: ```\n{}\n```'.format(
                 traceback.format_exc()))
         logger.exception('RuntimeError. Stopping trader ...')
     finally:
         TelegramHandler.send_msg('*Status:* `Trader has stopped`')
Пример #2
0
def close_trade_if_fulfilled(trade: Trade) -> bool:
    """
    Checks if the trade is closable, and if so it is being closed.
    :param trade: Trade
    :return: True if trade has been closed else False
    """
    # If we don't have an open order and the close rate is already set,
    # we can close this trade.
    if trade.close_profit and trade.close_date and trade.close_rate and not trade.open_order_id:
        trade.is_open = False
        Session.flush()
        return True
    return False