After a lot of reading in the documentation I have come closer to what I'm trying to get to. I'm getting closer, still struggling with a few things. I'm adding the full code here
class Test_Strategy(bt.Strategy):
params = dict(
pfast=10, # period for the fast moving average
pslow=30 # period for the slow moving average
)
def log(self, txt, dt=None):
''' Logging function for this strategy'''
dt = dt or self.datas[0].datetime.datetime(0)
print('%s, %s' % (dt.strftime("%Y-%m-%d %H:%M"), txt))
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
# To keep track of pending orders and buy price/commission
self.order = None
self.buyprice = None
self.buycomm = None
def notify_order(self, order):
if order.status in [order.Submitted, order.Accepted]:
# Buy/Sell order submitted/accepted to/by broker - Nothing to do
return
# Check if an order has been completed
# Attention: broker could reject order if not enough cash
if order.status in [order.Completed]:
if order.isbuy():
self.log(
'BUY EXECUTED, Price: %.5f, Cost: %.f, Comm %.2f' %
(order.executed.price,
order.executed.value,
order.executed.comm))
self.buyprice = order.executed.price
self.buycomm = order.executed.comm
else: # Sell
self.log('SELL EXECUTED, Price: %.5f, Cost: %.f, Comm %.2f' %
(order.executed.price,
order.executed.value,
order.executed.comm))
self.bar_executed = len(self)
elif order.status in [order.Canceled, order.Margin, order.Rejected]:
self.log('Order Canceled/Margin/Rejected')
self.order = None
def notify_trade(self, trade):
if not trade.isclosed:
return
self.log('OPERATION PROFIT, GROSS %.5f, NET %.5f' %
(trade.pnl, trade.pnlcomm))
def next(self):
# Check if an order is pending ... if yes, we cannot send a 2nd one
if self.order:
if self.order.status == 2 and len(self) == self.bar_order_submitted + 1:
if self.data.high[0] == self.data.high[-1]: # current's bar high is equal to the previous bar's high
self.order = self.buy(exectype=bt.Order.StopLimit, price=self.data.high[0], transmit=False)
self.bar_order_submitted = len(self)
if self.data.low[0] > self.data.low[-1]: # if the current bar's low is higher, we adjust ourstoploss
self.StopLoss = self.sell(price=(self.data.low[0])
,exectype=bt.Order.Stop,transmit=True, size=self.order.size, parent=self.order)
else:
self.broker.cancel(self.order)
self.log("order was cancelled")
# Check if we are in the market
if not self.position:
# Not yet ... we MIGHT BUY if ...
if self.crossover > 0: # if fast crosses slow to the upside
self.order = self.buy(exectype=bt.Order.StopLimit, price=self.data.high[0], transmit=False)
self.StopLoss = self.sell(price=self.data.low[0],exectype=bt.Order.Market,
transmit=False, size=self.order.size,parent=self.order)
self.target = self.sell(price=(self.data.high[0]-self.data.low[0])*1.1+self.data.high[0], exectype=bt.Order.Market,
transmit=True, size=self.order.size, parent=self.order)
self.bar_order_submitted = len(self)
self.log('BUY CREATE, %.5f' % self.order.price)
self.log('SL: %.5f, T: %.5f' %(self.StopLoss.price,self.target.price))
if __name__ == '__main__':
cerebro = bt.Cerebro()
# Add a strategy
cerebro.addstrategy(Test_Strategy)
# Create a Data Feed
data = bt.feeds.PandasData(dataname=df2020)
cerebro.adddata(data)
# Add a FixedSize sizer according to the stake
cerebro.addsizer(bt.sizers.PercentSizer, percents=5000)
#Add Analyzer
cerebro.addanalyzer(bt.analyzers.PyFolio)
cerebro.broker.setcommission(commission=0.0,leverage=500)
# Set our desired cash start
cerebro.broker.setcash(1000000.0)
# Print out the starting conditions
print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue())
HG = cerebro.run()
# Print out the final result
print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue())
The first trade buys correctly, however it immediately sells after without having hit the target / nor the stoploss. I don't know why it just holds on but this seems to be a recurring issue.
Starting Portfolio Value: 1000000.00
2020-08-12 00:36, BUY CREATE, 1.17434
2020-08-12 00:36, SL: 1.17402, T: 1.17469
2020-08-12 00:37, BUY EXECUTED, Price: 1.17434, Cost: 50005962, Comm 0.00
2020-08-12 00:38, SELL EXECUTED, Price: 1.17406, Cost: 50005962, Comm 0.00
2020-08-12 00:38, Order Canceled/Margin/Rejected
Normally, when the trade doesn't get triggered the next bar, it will cancel the order. this seems to work properly. however there are also situations like below:
the order is created at 03:53.
the bar at 03:54 has a low which is lower than the stoploss I set
the bar at 03:55 has a high which exceeds the entry level.
with this setup, somehow it seems to trigger me in a trade.
I don't understand why. if the bar at 03:55 wouldn't have exceeded the entry level, it would have cancelled as normal.
2020-08-12 03:53, BUY CREATE, 1.17352
2020-08-12 03:53, SL: 1.17333, T: 1.17373
2020-08-12 03:55, BUY EXECUTED, Price: 1.17352, Cost: 49405112, Comm 0.00
2020-08-12 03:56, SELL EXECUTED, Price: 1.17361, Cost: 49405112, Comm 0.00
2020-08-12 03:56, Order Canceled/Margin/Rejected
last example:
this trade gets triggered correctly. the bar at 06:29 which triggers the trade in also hits the target. it should buy and take profit the same bar but that doesn't seem to happen. can't figure out why either.
2020-08-12 06:28, BUY CREATE, 1.17185
2020-08-12 06:28, SL: 1.17171, T: 1.17200
2020-08-12 06:29, BUY EXECUTED, Price: 1.17185, Cost: 49345663, Comm 0.00
2020-08-12 06:30, SELL EXECUTED, Price: 1.17203, Cost: 49345663, Comm 0.00
2020-08-12 06:30, Order Canceled/Margin/Rejected
2020-08-12 06:30, OPERATION PROFIT, GROSS 7579.65552, NET 7579.65552
greatly appreciate your help. I've been pretty stuck with this for the past weeks