Simple Ma Crossover
-
Hello Guys, I tried this simple cross-over strategy but it isnt working as I expect it to. I want to switch Postions when a crossover happens.
The thing is it Buys at the exact Price it should buy or sell, but I can see in the Postiontext, that the price where it opens the postion, is an completely different price and I dont understand why.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.close() print(self.position) self.buy() # enter long print("Buy {} shares".format( self.data.close[0])) print(self.position) elif self.crossover < 0: # in the market & cross to the downside self.close()# close long position print(self.position) self.sell() print("Sale {} shares".format(self.data.close[0])) print(self.position)
Sale 8617.5 shares
--- Position Begin- Size: 0
- Price: 0.0
- Price orig: 0.0
- Closed: 0
- Opened: 0
- Adjbase: None
--- Position End
--- Position Begin - Size: -1
- Price: 8718.5
- Price orig: 0.0
- Closed: 0
- Opened: -1
- Adjbase: 8973.0
and Sometimes it doesnt even switch
Buy 9622.5 shares
--- Position Begin- Size: -1
- Price: 9174.0
- Price orig: 0.0
- Closed: 0
- Opened: -1
- Adjbase: 9622.5
--- Position End
--- Position Begin - Size: 0
- Price: 0.0
- Price orig: 9174.0
- Closed: 1
- Opened: 0
- Adjbase: 9685.0
--- Position End
Thank you in advance
-
I've tested your code - works well in my case. I think you might have no cash to buy - probably you started with default initial capital of 10k, and your prices are 8-9k.
-
also if you want to change positions and be in the market, it will be better to use target orders:
if self.crossover > 0: self.order_target_size(size=1) elif self.crossover < 0: self.order_target_size(size-1)
Check out Docs - Target Orders
-
it will be not
size
parameter, buttarget
. -
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())
-
I tried now the example Crossover Code from the Backtrader Home.
https://www.backtrader.com/home/helloalgotrading/I got everything in the same python File and I didnt change the code and this comes out. Its so weird. I dont know what could be wrong.
https://imgur.com/a/tZgAzPj
It didnt work to include the picture here so I included the link. Hopefully its no Problem -
okay I found out how to upload it -
Okay I hopefully dont annoy anybody but I found something else. I think the problem was that it was intraday but I am not sure. The Log still seems weird, because as I understannd it, it buys and than there should be a postion in the postion log. But it comes a bit too late, but the price where it opens the postion now fits the cross after I changed the Timeframe from my csv to daily.
Sale 9264.5 shares
--- Position Begin- Size: 0
- Price: 0.0
- Price orig: 0.0
- Closed: 0
- Opened: 0
- Adjbase: None
--- Position End
Buy 10195.5 shares
--- Position Begin - Size: -1
- Price: 9264.5
- Price orig: 0.0
- Closed: 0
- Opened: -1
- Adjbase: 10195.5
--- Position End
Sale 6685.5 shares
--- Position Begin - Size: 1
- Price: 10195.5
- Price orig: 9264.5
- Closed: 1
- Opened: 1
- Adjbase: 6685.5
--- Position End
-
Okay I found the Error. Sorry for the inconvenience