I figured a way to do that but the progress bar is being printed after every callback (i.e after every iteration of the optimization). Is there a way I could restrict it to one instance when it begins? I believe the reason is that I used a GLOBAL variable so that the function optimizer_callbacks(cb) would be able to recognize it. Is there a way I could pass the pbar to optimizer_callbacks without actually using it as a global variable?
def optimizer_callbacks(cb):
pbar.update()
def strategy_optim(**kwargs):
total = np.prod([len(value) for key,value in kwargs.items()])
csv_file = FDaxCSVData(---data---)
cerebro = bt.Cerebro()
cerebro.adddata(csv_file)
cerebro.broker.setcash(500000.0)
cerebro.broker.setcommission(commission=2.0)
strats = cerebro.optstrategy(strategy_name, printlog = False, **kwargs)
global pbar
pbar = tqdm.tqdm(smoothing=0.05, desc='Optimization Runs', total=total)
cerebro.optcallback(optimizer_callbacks)
runnings = cerebro.run(optreturn=False, maxcpus=2)
if __name__=="__main__":
strategy_optim(periods = [100, 200, 300], abs_margin= [25, 50, 75], trail_stop=[10, 20, 30, 40])