For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/
Strategy optimization never finishes
-
Re: Optstrategy
HI,
When trying to use the optstrategy method, it never finish... run forever. No error is observed.
My code:from datetime import datetime import backtrader as bt import matplotlib.pyplot as plt %matplotlib inline class SmaCross(bt.Strategy): params = (('fast', 10), ('slow', 30) ) def __init__(self): self.sma_fast = bt.ind.SMA(period = self.params.fast) self.sma_slow = bt.ind.SMA(period = self.params.slow) self.crossover = bt.ind.CrossOver(self.sma_fast, self.sma_slow) def start(self): #runs after the init in the beginning pass def next(self): #runs in each iteration if self.position.size == 0: if self.sma_fast > self.sma_slow: self.buy() elif self.position.size != 0: if self.sma_fast < self.sma_slow: self.sell() def stop(self): #runs at the end pass contract_history = pd.read_csv('APPL-hourly.csv') contract_history['dt'] = pd.to_datetime(contract_history['date']) contract_history = contract_history.set_index('dt') data_pd = bt.feeds.PandasData(dataname = contract_history, fromdate = datetime(2020, 11, 11), todate = datetime(2020,12,1)) cerebro = bt.Cerebro() cerebro.adddata(data_pd) cerebro.addsizer(bt.sizers.PercentSizer, percents = 90) cerebro.optstrategy(SmaCross, fast = (5, 10), slow = (20, 25)) cerebro.run()
Does someone maybe know how to resolve this issue?
Thanks,
Amit -
@amit-kehat said in Strategy optimization never finishes:
Does someone maybe know how to resolve this issue?
Thanks,
AmitI just ran this using daily data and it's fine. You may wish to add in timeframe and compression. Also, there are no outputs in your code. Perhaps this is why you are not seeing anything?
timeframe (default: TimeFrame.Days) Potential values: Ticks, Seconds, Minutes, Days, Weeks, Months and Years compression (default: 1) Number of actual bars per bar. Informative. Only effective in Data Resampling/Replaying.