I increased the Portfolio Value to 1000000, so it should be enough, but it isnt working. I also added the target size which was a good improvment. But still I get this problem as stated above.
class SmaCross(bt.Strategy):
# list of parameters which are configurable for the strategy
params = dict(
pfast=50, # period for the fast moving average
pslow=200 # period for the slow moving average
)
def __init__(self):
sma1 = bt.ind.SMA(period=self.p.pfast) # fast moving average
sma2 = bt.ind.SMA(period=self.p.pslow) # slow moving average
self.crossover = bt.ind.CrossOver(sma1, sma2) # crossover signal
def next(self):
if self.crossover > 0: # if fast crosses slow to the upside
self.order_target_size(target=1)
print("Buy {} shares".format( self.data.close[0]))
print(self.position)
elif self.crossover < 0: # in the market & cross to the downside
self.order_target_size(target=-1)
print("Sale {} shares".format(self.data.close[0]))
print(self.position)
Than the Log has still the same errors. Should I use another csv file? But this cant be the problem in my opinion.
Error Log:Starting Portfolio Value: 1000000.00
Sale 8617.5 shares
--- Position Begin
- Size: 0
- Price: 0.0
- Price orig: 0.0
- Closed: 0
- Opened: 0
- Adjbase: None
--- Position End
Buy 8973.0 shares
--- Position Begin
- Size: -1
- Price: 8718.5
- Price orig: 0.0
- Closed: 0
- Opened: -1
- Adjbase: 8973.0
--- Position End
Sale 9174.0 shares
--- Position Begin
- Size: 1
- Price: 8897.0
- Price orig: 8718.5
- Closed: 1
- Opened: 1
- Adjbase: 9174.0
--- Position End
And thats my code where I start the strategy.
cerebro = backtrader.Cerebro()
cerebro.adddata(data)
cerebro.addstrategy(SmaCross)
cerebro.broker.setcash(1000000.0)
print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue())
cerebro.run()
print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue())