def start(ctx, market, resolution, start, end, automatic, backtest, papertrade, strategy, btc, coins): """Start a new bot on the given market and the given amount of BTC""" try: market = ctx.exchange.get_market(market, backtest, papertrade) except ValueError as ex: click.echo(ex.message) sys.exit(1) strategy = STRATEGIES[strategy]() if start: start = datetime.datetime.strptime(start, "%Y-%m-%d %H:%M:%S") if end: end = datetime.datetime.strptime(end, "%Y-%m-%d %H:%M:%S") if backtest: if start is None or end is None: click.echo( "Error! For backtests you must provide a timeframe by setting start and end!" ) sys.exit(1) bot = get_bot(market, strategy, resolution, start, end, btc, coins) bot.start(backtest, automatic) if backtest: click.echo(render_bot_tradelog(bot.trades)) click.echo(render_bot_statistic(bot.stat(backtest))) db.delete(bot) db.commit()
def start(ctx, market, resolution, start, end, automatic, backtest, papertrade, strategy, btc, coins): """Start a new bot on the given market and the given amount of BTC""" # Check start and end date try: if start: start = datetime.datetime.strptime(start, "%Y-%m-%d %H:%M:%S") if end: end = datetime.datetime.strptime(end, "%Y-%m-%d %H:%M:%S") except ValueError: click.echo( "Date is not valid. Must be in format 'YYYY-mm-dd HH:MM:SS'") sys.exit(1) # Build the market on which the bot will operate # First check if the given market is a valid market. If not exit # here with a error message. # If the market is valid create a real market instance of and # instance for backtests depending on the user input. if ctx.exchange.is_valid_market(market): if backtest: if start is None or end is None: click.echo( "Error! For backtests you must provide a timeframe by setting start and end!" ) sys.exit(1) market = BacktestMarket(ctx.exchange, market) else: market = Market(ctx.exchange, market) else: click.echo("Market {} is not available".format(market)) sys.exit(1) # Check if the given resolution is supported if not ctx.exchange.is_valid_resolution(resolution): valid_resolutions = ", ".join(ctx.exchange.resolutions.keys()) click.echo("Resolution {} is not supported.\n" "Please choose one of the following: {}".format( resolution, valid_resolutions)) sys.exit(1) # Initialise a strategy. strategy = STRATEGIES[strategy]() bot = get_bot(market, strategy, resolution, start, end, btc, coins) bot.start(backtest, automatic) if backtest: click.echo(render_bot_tradelog(bot.trades)) click.echo(render_bot_statistic(bot.stat(backtest))) db.delete(bot) db.commit()
def start(self, backtest=False, automatic=False): """Start the bot and begin trading with given amount of BTC. The bot will trigger a analysis of the chart every N seconds. The default number of seconds is set on initialisation using the `resolution` option. You can overwrite this setting by using the `interval` option. By setting the `backtest` option the trade will be simulated on real chart data. This is useful for testing to see how good your strategy performs. :btc: Amount of BTC to start trading with :backtest: Simulate trading on historic chart data on the given market. :returns: None """ while 1: chart = self._market.get_chart(self._resolution, self._start, self._end) signal = self._strategy.signal(chart) log.debug("{} {}".format(signal.date, signal.value)) interval = self._get_interval(self.automatic, backtest) if not automatic: click.echo(render_bot_title(self, self._market, chart)) click.echo(render_signal_detail(signal)) options = [] if self.btc: options.append(('b', 'Buy')) if self.amount: options.append(('s', 'Sell')) options.append(('l', 'Tradelog')) options.append(('p', 'Performance of bot')) if not automatic: options.append(('d', 'Detach')) options.append(('q', 'Quit')) click.echo(render_user_options(options)) c = click.getchar() if c == 'b' and self.btc: # btc = click.prompt('BTC', default=self.self.btc) if click.confirm('Buy for {} btc?'.format(self.btc)): signal = Signal(BUY, datetime.datetime.utcnow()) elif c == 's' and self.amount: # amount = click.prompt('Amount', default=self.self.amount) if click.confirm('Sell {}?'.format(self.amount)): signal = Signal(SELL, datetime.datetime.utcnow()) elif c == 'l': click.echo(render_bot_tradelog(self.trades)) elif c == 'p': click.echo(render_bot_statistic(self.stat())) elif c == 'd': automatic = True log.info("Bot detached") elif c == 'q': log.info("Bot stopped") sys.exit(0) else: signal = Signal(WAIT, datetime.datetime.utcnow()) if automatic: click.echo( "Press 'A' with in the next {} secods to reattach the bot." .format(TIMEOUT)) i, o, e = select.select([sys.stdin], [], [], TIMEOUT) if (i): value = sys.stdin.readline().strip() print(value) if value == "A": automatic = False if signal: self._handle_signal(signal) if backtest: if not self._market.continue_backtest(): log.info("Backtest finished") break time.sleep(interval)