I simplified the example so it's really easy to read and follows most of the conventions that the Backtrader's samples also take.
Here's the simplified code with the same problems as above. Why does this happen?
from __future__ import (absolute_import, division, print_function, unicode_literals)
import datetime
import backtrader as bt
import backtrader.feeds as btfeeds
import backtrader.indicators as btind
def run():
cerebro = bt.Cerebro(optreturn=False)
StrategyContainer.register(SMA_CrossOver)
cerebro.optstrategy(StrategyContainer, strategy_index=StrategyContainer.indices())
data0 = btfeeds.YahooFinanceCSVData(dataname='../../datas/daily-PEP.csv', fromdate=datetime.datetime(1997, 1, 1), todate=datetime.datetime(1998, 1, 1))
cerebro.adddata(data0)
cerebro.adddata(data0)
# run
results = cerebro.run()
class StrategyContainer(object):
_strategies = []
@classmethod
def register(cls, strategy):
cls._strategies.append(strategy)
@classmethod
def indices(cls):
return range(len(cls._strategies))
def __new__(cls, *args, **kwargs):
strategy_index = kwargs.pop('strategy_index')
obj = cls._strategies[strategy_index](*args, **kwargs)
return obj
class SMA_CrossOver(bt.Strategy):
pass
run()