def checkDayFilterByDay(bars, symbol=None): for i in range(7): msg = str(i) logger.info(msg) bot = MultiStrategyBot(logger=logger, directionFilter=0) bot.add_strategy(SfpStrategy().withEntryFilter( DayOfWeekFilter(1 << i))) b = BackTest(bot, bars, symbol).run()
def runOpti(bars,min,max,steps,symbol= None, randomCount= -1): v= min[:] while True: msg= "" if randomCount > 0: for i in range(len(v)): v[i] = min[i] + random.randint(0, int((max[i] - min[i]) / steps[i])) * steps[i] randomCount = randomCount-1 for i in v: msg += str(i) + " " logger.info(msg) bot = MultiStrategyBot(logger=logger, directionFilter=0) bot.add_strategy(SfpStrategy() ) BackTest(bot, bars,symbol).run() if randomCount == 0 or (randomCount < 0 and not increment(min,max,steps,v)): break
def runOpti(bars, min, max, steps): v = min[:] while True: msg = "" for i in v: msg += str(i) + " " logger.info(msg) bot = MultiStrategyBot(logger=logger, directionFilter=0) bot.add_strategy( SfpStrategy( init_stop_type=1, tp_fac=12, min_wick_fac=0.5, min_swing_length=11, range_length=70, min_rej_length=35, range_filter_fac=0, close_on_opposite=False).withChannel( max_look_back=13, threshold_factor=0.8, buffer_factor=0.05, max_dist_factor=1, max_swing_length=4).withRM( risk_factor=1, max_risk_mul=2, risk_type=0, atr_factor=1).withExitModule( SimpleBE(factor=0.6, buffer=0.4)).withExitModule( SimpleBE(factor=1.6, buffer=0.8)).withExitModule( ParaTrail( accInit=0.007, accInc=0.018, accMax=0.07)).withEntryFilter( DayOfWeekFilter(v[0]))) BackTest(bot, bars).run() if not increment(min, max, steps, v): break
def start_bot(botSettings, telegram: TelegramBot = None): bot = MultiStrategyBot() originalSettings = dotdict(dict(botSettings)) if "strategies" in botSettings.keys(): risk_reference = 1 if "RISK_REFERENCE" in botSettings.keys(): risk_reference = botSettings.RISK_REFERENCE if risk_reference <= 0: logger.error( "if you don't want to risk money, you shouldn't even run this bot!" ) bot.risk_reference = risk_reference strategies = dict(botSettings.strategies) del botSettings.strategies # settings is now just the meta settings for stratId in strategies.keys(): stratSettings = dict(botSettings) stratSettings = dotdict(stratSettings) stratSettings.update(strategies[stratId]) if stratSettings.KB_RISK_FACTOR <= 0: logger.error( "if you don't want to risk money, you shouldn't even run this bot!" ) continue if stratId == "macross": strat = MACross(fastMA=stratSettings.MAC_FAST_MA, slowMA=stratSettings.MAC_SLOW_MA, swingBefore=stratSettings.MAC_SWING_BEFORE, swingAfter=stratSettings.MAC_SWING_AFTER) elif stratId == "kuegi": strat = KuegiStrategy(min_channel_size_factor=stratSettings.KB_MIN_CHANNEL_SIZE_FACTOR, max_channel_size_factor=stratSettings.KB_MAX_CHANNEL_SIZE_FACTOR, entry_tightening=stratSettings.KB_ENTRY_TIGHTENING, bars_till_cancel_triggered=stratSettings.KB_BARS_TILL_CANCEL_TRIGGERED, limit_entry_offset_perc=stratSettings.KB_LIMIT_OFFSET, delayed_entry=stratSettings.KB_DELAYED_ENTRY, delayed_cancel=stratSettings.KB_DELAYED_CANCEL, cancel_on_filter=stratSettings.KB_CANCEL_ON_FILTER, tp_fac=stratSettings.KB_TP_FAC) \ .withChannel(max_look_back=stratSettings.KB_MAX_LOOK_BACK, threshold_factor=stratSettings.KB_THRESHOLD_FACTOR, buffer_factor=stratSettings.KB_BUFFER_FACTOR, max_dist_factor=stratSettings.KB_MAX_DIST_FACTOR, max_swing_length=stratSettings.KB_MAX_SWING_LENGTH) if "KB_TRAIL_TO_SWING" in stratSettings.keys(): strat.withTrail( trail_to_swing=stratSettings.KB_TRAIL_TO_SWING, delayed_swing=stratSettings.KB_DELAYED_ENTRY, trail_back=stratSettings.KB_ALLOW_TRAIL_BACK) elif stratId == "sfp": strat = SfpStrategy(min_stop_diff_perc=stratSettings.SFP_MIN_STOP_DIFF, init_stop_type=stratSettings.SFP_STOP_TYPE, stop_buffer_fac=stratSettings.SFP_STOP_BUFFER_FAC, tp_fac=stratSettings.SFP_TP_FAC, min_wick_fac=stratSettings.SFP_MIN_WICK_FAC, min_air_wick_fac=stratSettings.SFP_MIN_AIR_WICK_FAC, min_wick_to_body=stratSettings.SFP_MIN_WICK_TO_BODY, min_swing_length=stratSettings.SFP_MIN_SWING_LENGTH, range_length=stratSettings.SFP_RANGE_LENGTH, min_rej_length=stratSettings.SFP_MIN_REJ_LENGTH, range_filter_fac=stratSettings.SFP_RANGE_FILTER_FAC, close_on_opposite=stratSettings.SFP_CLOSE_ON_OPPOSITE, tp_use_atr = stratSettings.SFP_USE_ATR, ignore_on_tight_stop = stratSettings.SFP_IGNORE_TIGHT_STOP, entries = stratSettings.SFP_ENTRIES) \ .withChannel(max_look_back=stratSettings.KB_MAX_LOOK_BACK, threshold_factor=stratSettings.KB_THRESHOLD_FACTOR, buffer_factor=stratSettings.KB_BUFFER_FACTOR, max_dist_factor=stratSettings.KB_MAX_DIST_FACTOR, max_swing_length=stratSettings.KB_MAX_SWING_LENGTH) if "KB_TRAIL_TO_SWING" in stratSettings.keys(): strat.withTrail( trail_to_swing=stratSettings.KB_TRAIL_TO_SWING, delayed_swing=stratSettings.KB_DELAYED_ENTRY, trail_back=stratSettings.KB_ALLOW_TRAIL_BACK) else: strat = None logger.warning("unkown strategy: " + stratId) if strat is not None: strat.with_telegram(telegram) strat.withRM(risk_factor=stratSettings.KB_RISK_FACTOR * risk_reference, risk_type=stratSettings.KB_RISK_TYPE, max_risk_mul=stratSettings.KB_MAX_RISK_MUL, atr_factor=stratSettings.KB_RISK_ATR_FAC) if "KB_BE_FACTOR" in stratSettings.keys(): strat.withExitModule( SimpleBE(factor=stratSettings.KB_BE_FACTOR, buffer=stratSettings.KB_BE_BUFFER)) for i in range(1, 8): factorKey = "KB_BE" + str(i) + "_FACTOR" bufferKey = "KB_BE" + str(i) + "_BUFFER" if factorKey in stratSettings.keys(): strat.withExitModule( SimpleBE(factor=stratSettings[factorKey], buffer=stratSettings[bufferKey])) if "EM_PARA_INIT" in stratSettings.keys(): resetToCurrent = False if "EM_PARA_RESET" in stratSettings.keys(): resetToCurrent = stratSettings.EM_PARA_RESET strat.withExitModule( ParaTrail(accInit=stratSettings.EM_PARA_INIT, accInc=stratSettings.EM_PARA_INC, accMax=stratSettings.EM_PARA_MAX, resetToCurrent=resetToCurrent)) if "FILTER_DAYWEEK" in stratSettings.keys(): strat.withEntryFilter( DayOfWeekFilter(stratSettings.FILTER_DAYWEEK)) bot.add_strategy(strat) else: logger.error("only multistrat bot supported") live = LiveTrading(settings=botSettings, trading_bot=bot, telegram=telegram) t = threading.Thread(target=live.run_loop) t.bot: LiveTrading = live t.originalSettings = originalSettings t.start() return t
def start_bot(botSettings): bot = MultiStrategyBot() if "strategies" in botSettings.keys(): strategies = dict(botSettings.strategies) del botSettings.strategies # settings is now just the meta settings for stratId in strategies.keys(): stratSettings = dict(botSettings) stratSettings = dotdict(stratSettings) stratSettings.update(strategies[stratId]) if stratSettings.KB_RISK_FACTOR <= 0: logger.error( "if you don't want to risk money, you shouldn't even run this bot!" ) continue if stratId == "kuegi": strat = KuegiStrategy(min_channel_size_factor=stratSettings.KB_MIN_CHANNEL_SIZE_FACTOR, max_channel_size_factor=stratSettings.KB_MAX_CHANNEL_SIZE_FACTOR, entry_tightening=stratSettings.KB_ENTRY_TIGHTENING, bars_till_cancel_triggered=stratSettings.KB_BARS_TILL_CANCEL_TRIGGERED, stop_entry=stratSettings.KB_STOP_ENTRY, delayed_entry=stratSettings.KB_DELAYED_ENTRY, delayed_cancel=stratSettings.KB_DELAYED_CANCEL, cancel_on_filter=stratSettings.KB_CANCEL_ON_FILTER) \ .withChannel(max_look_back=stratSettings.KB_MAX_LOOK_BACK, threshold_factor=stratSettings.KB_THRESHOLD_FACTOR, buffer_factor=stratSettings.KB_BUFFER_FACTOR, max_dist_factor=stratSettings.KB_MAX_DIST_FACTOR, max_swing_length=stratSettings.KB_MAX_SWING_LENGTH) if "KB_TRAIL_TO_SWING" in stratSettings.keys(): strat.withTrail( trail_to_swing=stratSettings.KB_TRAIL_TO_SWING, delayed_swing=stratSettings.KB_DELAYED_ENTRY, trail_back=stratSettings.KB_ALLOW_TRAIL_BACK) elif stratId == "sfp": strat = SfpStrategy(init_stop_type=stratSettings.SFP_STOP_TYPE, tp_fac=stratSettings.SFP_TP_FAC, min_wick_fac=stratSettings.SFP_MIN_WICK_FAC, min_swing_length=stratSettings.SFP_MIN_SWING_LENGTH, range_length=stratSettings.SFP_RANGE_LENGTH, min_rej_length=stratSettings.SFP_MIN_REJ_LENGTH, range_filter_fac=stratSettings.SFP_RANGE_FILTER_FAC, close_on_opposite=stratSettings.SFP_CLOSE_ON_OPPOSITE) \ .withChannel(max_look_back=stratSettings.KB_MAX_LOOK_BACK, threshold_factor=stratSettings.KB_THRESHOLD_FACTOR, buffer_factor=stratSettings.KB_BUFFER_FACTOR, max_dist_factor=stratSettings.KB_MAX_DIST_FACTOR, max_swing_length=stratSettings.KB_MAX_SWING_LENGTH) if "KB_TRAIL_TO_SWING" in stratSettings.keys(): strat.withTrail( trail_to_swing=stratSettings.KB_TRAIL_TO_SWING, delayed_swing=stratSettings.KB_DELAYED_ENTRY, trail_back=stratSettings.KB_ALLOW_TRAIL_BACK) else: strat = None logger.warn("unkown strategy: " + stratId) if strat is not None: strat.withRM(risk_factor=stratSettings.KB_RISK_FACTOR, risk_type=stratSettings.KB_RISK_TYPE, max_risk_mul=stratSettings.KB_MAX_RISK_MUL, atr_factor=stratSettings.KB_RISK_ATR_FAC) if "KB_BE_FACTOR" in stratSettings.keys(): strat.withExitModule( SimpleBE(factor=stratSettings.KB_BE_FACTOR, buffer=stratSettings.KB_BE_BUFFER)) if "KB_BE2_FACTOR" in stratSettings.keys(): strat.withExitModule( SimpleBE(factor=stratSettings.KB_BE2_FACTOR, buffer=stratSettings.KB_BE2_BUFFER)) if "EM_PARA_INIT" in stratSettings.keys(): strat.withExitModule( ParaTrail(accInit=stratSettings.EM_PARA_INIT, accInc=stratSettings.EM_PARA_INC, accMax=stratSettings.EM_PARA_MAX)) if "FILTER_DAYWEEK" in stratSettings.keys(): strat.withEntryFilter( DayOfWeekFilter(stratSettings.FILTER_DAYWEEK)) bot.add_strategy(strat) else: if botSettings.KB_RISK_FACTOR <= 0: logger.error( "if you don't want to risk money, you shouldn't even run this bot!" ) else: bot.add_strategy( KuegiStrategy( min_channel_size_factor=botSettings. KB_MIN_CHANNEL_SIZE_FACTOR, max_channel_size_factor=botSettings. KB_MAX_CHANNEL_SIZE_FACTOR, entry_tightening=botSettings.KB_ENTRY_TIGHTENING, bars_till_cancel_triggered=botSettings. KB_BARS_TILL_CANCEL_TRIGGERED, stop_entry=botSettings.KB_STOP_ENTRY, delayed_entry=botSettings.KB_DELAYED_ENTRY, delayed_cancel=botSettings.KB_DELAYED_CANCEL, cancel_on_filter=botSettings.KB_CANCEL_ON_FILTER). withChannel( max_look_back=botSettings.KB_MAX_LOOK_BACK, threshold_factor=botSettings.KB_THRESHOLD_FACTOR, buffer_factor=botSettings.KB_BUFFER_FACTOR, max_dist_factor=botSettings.KB_MAX_DIST_FACTOR, max_swing_length=botSettings.KB_MAX_SWING_LENGTH).withRM( risk_factor=botSettings.KB_RISK_FACTOR, risk_type=botSettings.KB_RISK_TYPE, max_risk_mul=botSettings.KB_MAX_RISK_MUL, atr_factor=botSettings.KB_RISK_ATR_FAC).withExitModule( SimpleBE(factor=botSettings.KB_BE_FACTOR, buffer=botSettings.KB_BE_BUFFER)). withTrail(trail_to_swing=botSettings.KB_TRAIL_TO_SWING, delayed_swing=botSettings.KB_DELAYED_ENTRY, trail_back=botSettings.KB_ALLOW_TRAIL_BACK)) live = LiveTrading(settings=botSettings, trading_bot=bot) t = threading.Thread(target=live.run_loop) t.bot: LiveTrading = live t.start() return t
''' runOpti(bars_oos, funding=funding, min= [-5,20], max= [5,27], steps= [1,1], randomCount=-1, symbol=symbol) #''' #''' bot = MultiStrategyBot(logger=logger, directionFilter=0) bot.add_strategy(KuegiStrategy()) bot.add_strategy(SfpStrategy()) b = BackTest(bot, bars_full, funding=funding, symbol=symbol, market_slipage_percent=0.15).run() #performance chart with lots of numbers bot.create_performance_plot(bars).show() # chart with signals: b.prepare_plot().show() #'''