def _logging_entry(self): log_frame = "Entry: {{trade_id: {}, currency_pair: {}, action: {}," \ " amount: {}, entry_price: {}, entry_datetime: {}}}" log_msg = log_frame.format(self.id_, self.currency_pair, self.action, self.amount, self.entry_price, self.entry_datetime) trade_logger.info(log_msg, extra={'strategyid': self._strategy_descriptor()})
def stop(self): if self._status.is_stopped(): trade_logger.info('process already stopped', extra={'strategyid': self._descriptor()}) return self._status.to_stopped() self._wake_up_and_next_loop()
def restart(self): if self._status.is_running(): trade_logger.info('process already running', extra={'strategyid': self._descriptor()}) return self._status.to_running() trade_logger.info('process restarted', extra={'strategyid': self._descriptor()}) self._wake_up_and_next_loop()
def _main_loop(self, sec_wait): try: while self._status.is_alive(): self._status.wait_until_continuable() if self._need_stop(): self.stop() break trade_logger.info('process running', extra={'strategyid': self._descriptor()}) self._trading_routine() self._sleep(sec_wait) except Exception as e: trade_logger.exception(e, extra={'strategyid': self._descriptor()}) trade_logger.error('exception occurred, process will stop', extra={'strategyid': self._descriptor()}) self.stop() finally: trade_logger.info('process stopped', extra={'strategyid': self._descriptor()})
def _logging_exit(self): log_frame = "Exit: {{trade_id: {}, currency_pair: {}, exit_price: {}, exit_datetime: {}}}" log_msg = log_frame.format(self.id_, self.currency_pair, self.exit_price, self.exit_datetime) trade_logger.info(log_msg, extra={'strategyid': self._strategy_descriptor()})
def start(self, *, sec_wait=1): self.started = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") self._status.to_running() trade_logger.info('process started', extra={'strategyid': self._descriptor()}) self._main_loop(sec_wait=sec_wait)
def _need_stop(self): if self.stop_rule is None: return False trade_logger.info('check stop', extra={'strategyid': self._descriptor()}) return self.stop_rule.need_stop(self._trade)
def _check_exit(self): trade_logger.info('check exit', extra={'strategyid': self._descriptor()}) if self.exit_rule.can_exit(self._trade): self._exit()
def _check_entry(self): trade_logger.info('check entry', extra={'strategyid': self._descriptor()}) if self.entry_rule.can_entry(): self._entry()