class Worker: """ Freqtradebot worker class """ def __init__(self, args: Dict[str, Any], config: Dict[str, Any] = None) -> None: """ Init all variables and objects the bot needs to work """ logger.info(f"Starting worker {__version__}") self._args = args self._config = config self._init(False) self.last_throttle_start_time: float = 0 self._heartbeat_msg: float = 0 # Tell systemd that we completed initialization phase self._notify("READY=1") def _init(self, reconfig: bool) -> None: """ Also called from the _reconfigure() method (with reconfig=True). """ if reconfig or self._config is None: # Load configuration self._config = Configuration(self._args, None).get_config() # Init the instance of the bot self.freqtrade = FreqtradeBot(self._config) internals_config = self._config.get('internals', {}) self._throttle_secs = internals_config.get( 'process_throttle_secs', constants.PROCESS_THROTTLE_SECS) self._heartbeat_interval = internals_config.get( 'heartbeat_interval', 60) self._sd_notify = sdnotify.SystemdNotifier() if \ self._config.get('internals', {}).get('sd_notify', False) else None def _notify(self, message: str) -> None: """ Removes the need to verify in all occurances if sd_notify is enabled :param message: Message to send to systemd if it's enabled. """ if self._sd_notify: logger.debug(f"sd_notify: {message}") self._sd_notify.notify(message) def run(self) -> None: state = None while True: state = self._worker(old_state=state) if state == State.RELOAD_CONFIG: self._reconfigure() def _worker(self, old_state: Optional[State]) -> State: """ The main routine that runs each throttling iteration and handles the states. :param old_state: the previous service state from the previous call :return: current service state """ state = self.freqtrade.state # Log state transition if state != old_state: self.freqtrade.notify_status(f'{state.name.lower()}') logger.info(f"Changing state to: {state.name}") if state == State.RUNNING: self.freqtrade.startup() if state == State.STOPPED: self.freqtrade.check_for_open_trades() # Reset heartbeat timestamp to log the heartbeat message at # first throttling iteration when the state changes self._heartbeat_msg = 0 if state == State.STOPPED: # Ping systemd watchdog before sleeping in the stopped state self._notify("WATCHDOG=1\nSTATUS=State: STOPPED.") self._throttle(func=self._process_stopped, throttle_secs=self._throttle_secs) elif state == State.RUNNING: # Ping systemd watchdog before throttling self._notify("WATCHDOG=1\nSTATUS=State: RUNNING.") self._throttle(func=self._process_running, throttle_secs=self._throttle_secs) if self._heartbeat_interval: now = time.time() if (now - self._heartbeat_msg) > self._heartbeat_interval: logger.info(f"Bot heartbeat. PID={getpid()}, " f"version='{__version__}', state='{state.name}'") self._heartbeat_msg = now return state def _throttle(self, func: Callable[..., Any], throttle_secs: float, *args, **kwargs) -> Any: """ Throttles the given callable that it takes at least `min_secs` to finish execution. :param func: Any callable :param throttle_secs: throttling interation execution time limit in seconds :return: Any (result of execution of func) """ self.last_throttle_start_time = time.time() logger.debug("========================================") result = func(*args, **kwargs) time_passed = time.time() - self.last_throttle_start_time sleep_duration = max(throttle_secs - time_passed, 0.0) logger.debug( f"Throttling with '{func.__name__}()': sleep for {sleep_duration:.2f} s, " f"last iteration took {time_passed:.2f} s.") time.sleep(sleep_duration) return result def _process_stopped(self) -> None: self.freqtrade.process_stopped() def _process_running(self) -> None: try: self.freqtrade.process() except TemporaryError as error: logger.warning( f"Error: {error}, retrying in {constants.RETRY_TIMEOUT} seconds..." ) time.sleep(constants.RETRY_TIMEOUT) except OperationalException: tb = traceback.format_exc() hint = 'Issue `/start` if you think it is safe to restart.' self.freqtrade.notify_status( f'OperationalException:\n```\n{tb}```{hint}') logger.exception('OperationalException. Stopping trader ...') self.freqtrade.state = State.STOPPED def _reconfigure(self) -> None: """ Cleans up current freqtradebot instance, reloads the configuration and replaces it with the new instance """ # Tell systemd that we initiated reconfiguration self._notify("RELOADING=1") # Clean up current freqtrade modules self.freqtrade.cleanup() # Load and validate config and create new instance of the bot self._init(True) self.freqtrade.notify_status('config reloaded') # Tell systemd that we completed reconfiguration self._notify("READY=1") def exit(self) -> None: # Tell systemd that we are exiting now self._notify("STOPPING=1") if self.freqtrade: self.freqtrade.notify_status('process died') self.freqtrade.cleanup()
def plot_profit(args: Namespace) -> None: """ Plots the total profit for all pairs. Note, the profit calculation isn't realistic. But should be somewhat proportional, and therefor useful in helping out to find a good algorithm. """ # We need to use the same pairs, same tick_interval # and same timeperiod as used in backtesting # to match the tickerdata against the profits-results timerange = Arguments.parse_timerange(args.timerange) config = Configuration(args).get_config() # Init strategy try: strategy = StrategyResolver({ 'strategy': config.get('strategy') }).strategy except AttributeError: logger.critical( 'Impossible to load the strategy. Please check the file "user_data/strategies/%s.py"', config.get('strategy')) exit(1) # Load the profits results try: filename = args.exportfilename with open(filename) as file: data = json.load(file) except FileNotFoundError: logger.critical( 'File "backtest-result.json" not found. This script require backtesting ' 'results to run.\nPlease run a backtesting with the parameter --export.' ) exit(1) # Take pairs from the cli otherwise switch to the pair in the config file if args.pair: filter_pairs = args.pair filter_pairs = filter_pairs.split(',') else: filter_pairs = config['exchange']['pair_whitelist'] tick_interval = strategy.ticker_interval pairs = config['exchange']['pair_whitelist'] if filter_pairs: pairs = list(set(pairs) & set(filter_pairs)) logger.info('Filter, keep pairs %s' % pairs) tickers = history.load_data(datadir=Path(config.get('datadir')), pairs=pairs, ticker_interval=tick_interval, refresh_pairs=False, timerange=timerange) dataframes = strategy.tickerdata_to_dataframe(tickers) # NOTE: the dataframes are of unequal length, # 'dates' is an merged date array of them all. dates = misc.common_datearray(dataframes) min_date = int(min(dates).timestamp()) max_date = int(max(dates).timestamp()) num_iterations = define_index(min_date, max_date, tick_interval) + 1 # Make an average close price of all the pairs that was involved. # this could be useful to gauge the overall market trend # We are essentially saying: # array <- sum dataframes[*]['close'] / num_items dataframes # FIX: there should be some onliner numpy/panda for this avgclose = np.zeros(num_iterations) num = 0 for pair, pair_data in dataframes.items(): close = pair_data['close'] maxprice = max(close) # Normalize price to [0,1] logger.info('Pair %s has length %s' % (pair, len(close))) for x in range(0, len(close)): avgclose[x] += close[x] / maxprice # avgclose += close num += 1 avgclose /= num # make an profits-growth array pg = make_profit_array(data, num_iterations, min_date, tick_interval, filter_pairs) # # Plot the pairs average close prices, and total profit growth # avgclose = go.Scattergl( x=dates, y=avgclose, name='Avg close price', ) profit = go.Scattergl( x=dates, y=pg, name='Profit', ) fig = tools.make_subplots(rows=3, cols=1, shared_xaxes=True, row_width=[1, 1, 1]) fig.append_trace(avgclose, 1, 1) fig.append_trace(profit, 2, 1) for pair in pairs: pg = make_profit_array(data, num_iterations, min_date, tick_interval, pair) pair_profit = go.Scattergl( x=dates, y=pg, name=pair, ) fig.append_trace(pair_profit, 3, 1) plot(fig, filename=str( Path('user_data').joinpath('freqtrade-profit-plot.html')))
class Worker(object): """ Freqtradebot worker class """ def __init__(self, args: Namespace, config=None) -> None: """ Init all variables and objects the bot needs to work """ logger.info('Starting worker %s', __version__) self._args = args self._config = config self._init(False) # Tell systemd that we completed initialization phase if self._sd_notify: logger.debug("sd_notify: READY=1") self._sd_notify.notify("READY=1") def _init(self, reconfig: bool) -> None: """ Also called from the _reconfigure() method (with reconfig=True). """ if reconfig or self._config is None: # Load configuration self._config = Configuration(self._args, None).get_config() # Init the instance of the bot self.freqtrade = FreqtradeBot(self._config) self._throttle_secs = self._config.get('internals', {}).get( 'process_throttle_secs', constants.PROCESS_THROTTLE_SECS) self._sd_notify = sdnotify.SystemdNotifier() if \ self._config.get('internals', {}).get('sd_notify', False) else None @property def state(self) -> State: return self.freqtrade.state @state.setter def state(self, value: State) -> None: self.freqtrade.state = value def run(self) -> None: state = None while True: state = self._worker(old_state=state) if state == State.RELOAD_CONF: self._reconfigure() def _worker(self, old_state: Optional[State], throttle_secs: Optional[float] = None) -> State: """ Trading routine that must be run at each loop :param old_state: the previous service state from the previous call :return: current service state """ state = self.freqtrade.state if throttle_secs is None: throttle_secs = self._throttle_secs # Log state transition if state != old_state: self.freqtrade.rpc.send_msg({ 'type': RPCMessageType.STATUS_NOTIFICATION, 'status': f'{state.name.lower()}' }) logger.info('Changing state to: %s', state.name) if state == State.RUNNING: self.freqtrade.startup() if state == State.STOPPED: # Ping systemd watchdog before sleeping in the stopped state if self._sd_notify: logger.debug("sd_notify: WATCHDOG=1\\nSTATUS=State: STOPPED.") self._sd_notify.notify("WATCHDOG=1\nSTATUS=State: STOPPED.") time.sleep(throttle_secs) elif state == State.RUNNING: # Ping systemd watchdog before throttling if self._sd_notify: logger.debug("sd_notify: WATCHDOG=1\\nSTATUS=State: RUNNING.") self._sd_notify.notify("WATCHDOG=1\nSTATUS=State: RUNNING.") self._throttle(func=self._process, min_secs=throttle_secs) return state def _throttle(self, func: Callable[..., Any], min_secs: float, *args, **kwargs) -> Any: """ Throttles the given callable that it takes at least `min_secs` to finish execution. :param func: Any callable :param min_secs: minimum execution time in seconds :return: Any """ start = time.time() result = func(*args, **kwargs) end = time.time() duration = max(min_secs - (end - start), 0.0) logger.debug('Throttling %s for %.2f seconds', func.__name__, duration) time.sleep(duration) return result def _process(self) -> None: logger.debug("========================================") try: self.freqtrade.process() except TemporaryError as error: logger.warning( f"Error: {error}, retrying in {constants.RETRY_TIMEOUT} seconds..." ) time.sleep(constants.RETRY_TIMEOUT) except OperationalException: tb = traceback.format_exc() hint = 'Issue `/start` if you think it is safe to restart.' self.freqtrade.rpc.send_msg({ 'type': RPCMessageType.STATUS_NOTIFICATION, 'status': f'OperationalException:\n```\n{tb}```{hint}' }) logger.exception('OperationalException. Stopping trader ...') self.freqtrade.state = State.STOPPED def _reconfigure(self) -> None: """ Cleans up current freqtradebot instance, reloads the configuration and replaces it with the new instance """ # Tell systemd that we initiated reconfiguration if self._sd_notify: logger.debug("sd_notify: RELOADING=1") self._sd_notify.notify("RELOADING=1") # Clean up current freqtrade modules self.freqtrade.cleanup() # Load and validate config and create new instance of the bot self._init(True) self.freqtrade.rpc.send_msg({ 'type': RPCMessageType.STATUS_NOTIFICATION, 'status': 'config reloaded' }) # Tell systemd that we completed reconfiguration if self._sd_notify: logger.debug("sd_notify: READY=1") self._sd_notify.notify("READY=1") def exit(self) -> None: # Tell systemd that we are exiting now if self._sd_notify: logger.debug("sd_notify: STOPPING=1") self._sd_notify.notify("STOPPING=1") if self.freqtrade: self.freqtrade.rpc.send_msg({ 'type': RPCMessageType.STATUS_NOTIFICATION, 'status': 'process died' }) self.freqtrade.cleanup()
class Worker: """ Freqtradebot worker class """ def __init__(self, args: Dict[str, Any], config=None) -> None: """ Init all variables and objects the bot needs to work """ logger.info('Starting worker %s', __version__) self._args = args self._config = config self._init(False) # Tell systemd that we completed initialization phase if self._sd_notify: logger.debug("sd_notify: READY=1") self._sd_notify.notify("READY=1") def _init(self, reconfig: bool) -> None: """ Also called from the _reconfigure() method (with reconfig=True). """ # Salvar arquivo de Configurações JSON do Banco de Dados try: connection = psycopg2.connect(user=os.getenv('DB_USER'), password=os.getenv('DB_PASSWORD'), host=os.getenv('DB_HOST'), port=os.getenv('DB_PORT'), database=os.getenv('DB_NAME')) cursor = connection.cursor() query = "SELECT config_json FROM usuarios WHERE id={};".format( str(os.getenv('id_usuario'))) # Pega o resultado do Banco de Dados e salva em um arquivo JSON cursor.execute(query) record = cursor.fetchone() tempString = str(record) tempString2 = tempString.lstrip("(") tempString3 = tempString2.rstrip(",)") resultado = ast.literal_eval(tempString3) with open('config.json', 'w') as outfile: json.dump(resultado, outfile, indent=4) print("JSON Saved") except (Exception, psycopg2.Error) as error: print("Error while connecting to PostgreSQL", error) finally: # Closing database connection. if (connection): cursor.close() connection.close() if reconfig or self._config is None: # Load configuration self._config = Configuration(self._args, None).get_config() # Init the instance of the bot self.freqtrade = FreqtradeBot(self._config) self._throttle_secs = self._config.get('internals', {}).get( 'process_throttle_secs', constants.PROCESS_THROTTLE_SECS) self._sd_notify = sdnotify.SystemdNotifier() if \ self._config.get('internals', {}).get('sd_notify', False) else None @property def state(self) -> State: return self.freqtrade.state @state.setter def state(self, value: State) -> None: self.freqtrade.state = value def run(self) -> None: state = None while True: state = self._worker(old_state=state) if state == State.RELOAD_CONF: self._reconfigure() def _worker(self, old_state: Optional[State], throttle_secs: Optional[float] = None) -> State: """ Trading routine that must be run at each loop :param old_state: the previous service state from the previous call :return: current service state """ state = self.freqtrade.state if throttle_secs is None: throttle_secs = self._throttle_secs # Log state transition if state != old_state: self.freqtrade.rpc.send_msg({ 'type': RPCMessageType.STATUS_NOTIFICATION, 'status': f'{state.name.lower()}' }) logger.info('Changing state to: %s', state.name) if state == State.RUNNING: self.freqtrade.startup() if state == State.STOPPED: # Ping systemd watchdog before sleeping in the stopped state if self._sd_notify: logger.debug("sd_notify: WATCHDOG=1\\nSTATUS=State: STOPPED.") self._sd_notify.notify("WATCHDOG=1\nSTATUS=State: STOPPED.") time.sleep(throttle_secs) elif state == State.RUNNING: # Ping systemd watchdog before throttling if self._sd_notify: logger.debug("sd_notify: WATCHDOG=1\\nSTATUS=State: RUNNING.") self._sd_notify.notify("WATCHDOG=1\nSTATUS=State: RUNNING.") self._throttle(func=self._process, min_secs=throttle_secs) return state def _throttle(self, func: Callable[..., Any], min_secs: float, *args, **kwargs) -> Any: """ Throttles the given callable that it takes at least `min_secs` to finish execution. :param func: Any callable :param min_secs: minimum execution time in seconds :return: Any """ start = time.time() result = func(*args, **kwargs) end = time.time() duration = max(min_secs - (end - start), 0.0) logger.debug('Throttling %s for %.2f seconds', func.__name__, duration) time.sleep(duration) return result def _process(self) -> None: logger.debug("========================================") try: self.freqtrade.process() except TemporaryError as error: logger.warning( f"Error: {error}, retrying in {constants.RETRY_TIMEOUT} seconds..." ) time.sleep(constants.RETRY_TIMEOUT) except OperationalException: tb = traceback.format_exc() hint = 'Issue `/start` if you think it is safe to restart.' self.freqtrade.rpc.send_msg({ 'type': RPCMessageType.STATUS_NOTIFICATION, 'status': f'OperationalException:\n```\n{tb}```{hint}' }) logger.exception('OperationalException. Stopping trader ...') self.freqtrade.state = State.STOPPED def _reconfigure(self) -> None: """ Cleans up current freqtradebot instance, reloads the configuration and replaces it with the new instance """ # Tell systemd that we initiated reconfiguration if self._sd_notify: logger.debug("sd_notify: RELOADING=1") self._sd_notify.notify("RELOADING=1") # Clean up current freqtrade modules self.freqtrade.cleanup() # Load and validate config and create new instance of the bot self._init(True) self.freqtrade.rpc.send_msg({ 'type': RPCMessageType.STATUS_NOTIFICATION, 'status': 'config reloaded' }) # Tell systemd that we completed reconfiguration if self._sd_notify: logger.debug("sd_notify: READY=1") self._sd_notify.notify("READY=1") def exit(self) -> None: # Tell systemd that we are exiting now if self._sd_notify: logger.debug("sd_notify: STOPPING=1") self._sd_notify.notify("STOPPING=1") if self.freqtrade: self.freqtrade.rpc.send_msg({ 'type': RPCMessageType.STATUS_NOTIFICATION, 'status': 'process died' }) self.freqtrade.cleanup()