def run_backtest(context): n_bkt, args, mparams = context formatter = logging.Formatter('%(asctime)s - n_bkt=' + str(n_bkt) + ' - %(levelname)s: %(message)s') configure_root_logger(args.logtty, args.logfile, MongoClient(args.host, args.port)[args.db][args.logmongo] if args.logmongo is not None else None, formatter=formatter) db = MongoClient(args.host, args.port)[args.db] where_clause = defaultdict(lambda: {}) country, start_date, end_date = 'GB', parse_date(args.start), parse_date(args.end) if start_date is not None: where_clause['scheduled_off']['$gte'] = start_date if end_date is not None: where_clause['scheduled_off']['$lte'] = end_date if country is not None: where_clause['country'] = country sorted_races = db[args.train].find(where_clause, sort=[('scheduled_off', 1)], timeout=False) exec_services = HistoricalExecutionService(db) strat = strategy.Balius(mu=mparams['mu'], sigma=mparams['sigma'], beta=mparams['beta'], tau=mparams['tau'], draw_probability=mparams['draw_prob'], risk_aversion=mparams['risk_aversion'], min_races=mparams['min_races'], max_exposure=mparams['max_exposure']) st = time.clock() strategy.backtest(exec_services, strat, sorted_races) en = time.clock() logging.info('Backtest finished in %.2f seconds' % (en - st)) strat_dict = strat.to_dict() strat_id = db[STRATEGIES_COLL].insert(strat_dict) logging.info('Strategy serialised to %s with id=%s' % (db[STRATEGIES_COLL], strat_id)) bets = price_historical_bets(db, exec_services.get_mu_bets()[0]) scorecard = make_scorecard(bets) now = datetime.datetime.utcnow() scorecard.update({'params': {'ts': strat_dict['hm']['ts'], 'risk': strat_dict['risk']}, 'timestamp': now, 'run_seconds': en - st, 'strategy_id': strat_id}) scorecard_id = db[SCORECARDS_COLL].insert(scorecard) logging.info('Scorecard inserted in %s with id=%s' % (db[SCORECARDS_COLL], scorecard_id)) db[BETS_COLL].insert(add_scorecard_id_to_dicts(scorecard_id, bets)) logging.info('Associated bets inserted in %s' % db[BETS_COLL]) markets = market_breakdown(bets).reset_index() markets = pandas_to_dicts(markets, {'n_runners': int}) db[MARKETS_COLL].insert(add_scorecard_id_to_dicts(scorecard_id, markets)) logging.info('Associated market breakdown inserted in %s' % db[MARKETS_COLL])
def main(args): db = MongoClient(args.host, args.port)[args.db] now = datetime.datetime.utcnow() markets = list(islice(get_future_markets(hours=args.hours), 0, 1)) logging.info('Found %d horse races in the next %d hours' % (len(markets), args.hours)) strats = get_traded_strategies(db[args.paper_trading], True) for s in strats: strat = Balius.from_dict(db[BKT_STRATEGIES].find_one({'_id': s['strategy_id']})) strat.max_expsoure = 1e6 ex = PaperExecutionService() backtest(ex, strat, markets) bets = ex.get_mu_bets()[0] if len(bets) == 0: logging.info('Strategy with id=%s did not place any bets.' % s['strategy_id']) continue for bet in bets: bet['strategy_id'] = s['strategy_id'] bet['timestamp'] = now db[args.paper_bets].insert(bets) logging.info('Successfully insert %d bets for strategy with id=%s' % (len(bets), s['strategy_id']))
def run_backtest(context): n_bkt, args, mparams = context formatter = logging.Formatter('%(asctime)s - n_bkt=' + str(n_bkt) + ' - %(levelname)s: %(message)s') configure_root_logger( args.logtty, args.logfile, MongoClient(args.host, args.port)[args.db][args.logmongo] if args.logmongo is not None else None, formatter=formatter) db = MongoClient(args.host, args.port)[args.db] where_clause = defaultdict(lambda: {}) country, start_date, end_date = 'GB', parse_date(args.start), parse_date( args.end) if start_date is not None: where_clause['scheduled_off']['$gte'] = start_date if end_date is not None: where_clause['scheduled_off']['$lte'] = end_date if country is not None: where_clause['country'] = country sorted_races = db[args.train].find(where_clause, sort=[('scheduled_off', 1)], timeout=False) exec_services = HistoricalExecutionService(db) strat = strategy.Balius(mu=mparams['mu'], sigma=mparams['sigma'], beta=mparams['beta'], tau=mparams['tau'], draw_probability=mparams['draw_prob'], risk_aversion=mparams['risk_aversion'], min_races=mparams['min_races'], max_exposure=mparams['max_exposure']) st = time.clock() strategy.backtest(exec_services, strat, sorted_races) en = time.clock() logging.info('Backtest finished in %.2f seconds' % (en - st)) strat_dict = strat.to_dict() strat_id = db[STRATEGIES_COLL].insert(strat_dict) logging.info('Strategy serialised to %s with id=%s' % (db[STRATEGIES_COLL], strat_id)) bets = price_historical_bets(db, exec_services.get_mu_bets()[0]) scorecard = make_scorecard(bets) now = datetime.datetime.utcnow() scorecard.update({ 'params': { 'ts': strat_dict['hm']['ts'], 'risk': strat_dict['risk'] }, 'timestamp': now, 'run_seconds': en - st, 'strategy_id': strat_id }) scorecard_id = db[SCORECARDS_COLL].insert(scorecard) logging.info('Scorecard inserted in %s with id=%s' % (db[SCORECARDS_COLL], scorecard_id)) db[BETS_COLL].insert(add_scorecard_id_to_dicts(scorecard_id, bets)) logging.info('Associated bets inserted in %s' % db[BETS_COLL]) markets = market_breakdown(bets).reset_index() markets = pandas_to_dicts(markets, {'n_runners': int}) db[MARKETS_COLL].insert(add_scorecard_id_to_dicts(scorecard_id, markets)) logging.info('Associated market breakdown inserted in %s' % db[MARKETS_COLL])