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, (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(KuegiStrategy( min_channel_size_factor=v[2], max_channel_size_factor=v[3], entry_tightening=v[1]/10, bars_till_cancel_triggered=5, stop_entry=True, delayed_entry=v[0] == 1, delayed_cancel=True, cancel_on_filter= False) .withChannel(max_look_back=v[4], threshold_factor=v[5]/10, buffer_factor=v[6]*0.01, max_dist_factor=2,max_swing_length=4) .withRM(risk_factor=1, max_risk_mul=2, risk_type=1, atr_factor=2) #.withExitModule(SimpleBE(factor=0.5, buffer=-0.1)) #.withExitModule(SimpleBE(factor=1, buffer=0.5)) .withExitModule(ParaTrail(accInit=0.015, accInc=0.015, accMax=0.03)) #.withEntryFilter(DayOfWeekFilter(55)) ) BackTest(bot, bars,symbol).run() if randomCount == 0 or (randomCount < 0 and not increment(min,max,steps,v)): break
def runOpti(bars, funding, min, max, steps, symbol=None, randomCount=-1): v = min[:] total = 1 while len(steps) < len(min): steps.append(1) for i in range(len(min)): total *= 1 + (max[i] - min[i]) / steps[i] logger.info("running %d combinations" % total) 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(KuegiStrategy()) BackTest(bot, bars=bars, funding=funding, symbol=symbol).run() if randomCount == 0 or (randomCount < 0 and not increment(min, max, steps, v)): break
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(KuegiStrategy( min_channel_size_factor=0, max_channel_size_factor=16, entry_tightening=1, bars_till_cancel_triggered=5, stop_entry=True, delayed_entry=True, delayed_cancel=True, cancel_on_filter= False) .withChannel(max_look_back=13, threshold_factor=2.6, buffer_factor=0.05,max_dist_factor=2,max_swing_length=4) .withRM(risk_factor=2000, max_risk_mul=2, risk_type=1, atr_factor=2) .withExitModule(SimpleBE(factor=0.5, buffer=-0.1)) .withExitModule(SimpleBE(factor=1, buffer=0.5)) .withExitModule(ParaTrail(accInit=0.015, accInc=0.015, accMax=0.03)) .withEntryFilter(DayOfWeekFilter(55)) ) b= BackTest(bot, bars,symbol).run()
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
p.print_callers('<functionName>') ''' ''' 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()