def backtest(args): """ Just a naive backtester It will call the trained strategies on each test data point """ now = du.TIME_TRAVELER.getTime(force=True) t = du.TIME_TRAVELER.getTime() log.info("Test data: from", du.toStr(t), "to", du.toStr(now)) while t < now and not lm.gameOver() and not sig.EXIT: t += du.secToNano(conf.TIME_INTERVAL * 60) # TODO im.timeTravel(t) mm.predictModelsMaybeTrade(since=du.TIME_TRAVELER.nowMinus( days=ib.REAL_TIME_LOOKBACK_DAYS)) score = lm.getGlobalBalanceInQuote() # hodl = price / big_fat_data_prices[0] * 100 log.info("Backtesting done! Score: " + str(round(float(score))) # + "% vs HODL: " + str(round(hodl)) + "%" ) if args.graph: # TODO: exit if graph is closed while not sig.EXIT: time.sleep(0.1)
def trainModels(since): """Train all models and save the awesome result""" for model in mb.MODELS: if model.need_training: score = model.train(since) log.debug("Score:", score) if score: log.info("Saving model") model.save()
def fetch(unused_args): """Fetch raw trade data since the beginning of times""" while not sig.EXIT and not im.fetchInputs(): last_fetch = min((i.current_row.name for i in ib.INPUTS if i.current_row is not None)) log.info("Fetched data till", du.toStr(last_fetch)) if not sig.EXIT: log.debug("Fetching done, optimizing database...") fu.maintenance() log.info("Database up to date!")
def initGraph(log_lock, file_lock): """Launch an awesome matplotlib graph!""" log.setLock(log_lock) fu.setLock(file_lock) du.TIME_TRAVELER.setTime(None) sig.catchSignal() try: _getData() _initGraph() except Exception as e: log.warning("Something's bjorked in your graph :/") log.info("Try to run babao again with the env variable DEBUG_GRAPH set") # traceback.print_exc() raise e sys.exit(0) # we exit explicitly in the subprocess, to avoid double clean
def withdraw(self, ledger, volume, timestamp=None): fee = volume / 100 # 1% hardcoded fee refid = str(du.TIME_TRAVELER.nowMinus(0)) if self.verbose: log.info( "Withdraw", round(volume, 4), "from", ledger.asset.name, "to", self.asset.name, ) ledger.logTransaction(typ=ActionEnum.WITHDRAW.value, volume=volume, refid=refid, product=self.asset.value, timestamp=timestamp) self.logTransaction(typ=ActionEnum.DEPOSIT.value, volume=volume, refid=refid, fee=fee, product=ledger.asset.value, timestamp=timestamp)
def buy(self, ledger, volume_spent, price, timestamp=None): if self.balance < volume_spent: return False volume_bought = volume_spent / price fee = volume_bought / 100 # 1% hardcoded fee refid = str(du.TIME_TRAVELER.nowMinus(0)) if self.verbose: log.info("Bought", round(volume_bought - fee, 4), ledger.asset.name, "for", round(volume_spent, 4), self.asset.name, "@", int(price)) self.logTransaction(typ=ActionEnum.SELL.value, volume=-volume_spent, refid=refid, product=ledger.asset.value, timestamp=timestamp) ledger.logTransaction(typ=ActionEnum.BUY.value, volume=volume_bought, refid=refid, fee=fee, product=self.asset.value, timestamp=timestamp) return True
def test_info(): log.info("info")