I've been struggling to solve a problem involving resampling from daily to weekly data. My issue is that I need to generate my trading signals for an asset using the close price of a given week, and this signal should be available right after the market closes so that I could execute this order on the following week open price.
EXAMPLE: Signal to buy 50 AAPL shares triggered on friday's close, and order executed on monday's opening.
However, the way I'm implementing "resampledata" makes the weekly close price of the asset only available on the first day of the following week.
EXAMPLE: Signal to buy 50 AAPL shares should be issued on friday's close, but is being triggered only on monday's close, to be executed on tuesday's opening.
I generated a daily log of the week's closing price, and as you can check out below, the transition from one close price to the following one is being done from friday to monday, instead of thursday to friday.
Starting Portfolio Value: 100000.00
2006-01-09, Close, 3666.99
2006-01-10, Close, 3666.99
2006-01-11, Close, 3666.99
2006-01-12, Close, 3666.99
2006-01-13, Close, 3666.99 <--- Friday
2006-01-16, Close, 3629.25 <--- Monday
2006-01-17, Close, 3629.25
2006-01-18, Close, 3629.25
If you guys wanna check the script I used to generate the logs, check out below:
# Create a Stratey
def log(self, txt, dt=None):
''' Logging function fot this strategy'''
dt = dt or self.datas.datetime.date(0)
print('%s, %s' % (dt.isoformat(), txt))
# Keep a reference to the "close" line in the data dataseries
self.dataclose = self.datas.close
# Simply log the closing price of the series from the reference
self.log('Close, %.2f' % self.dataclose)
if self.dataclose < self.dataclose[-1]:
# current close less than previous close
if self.dataclose[-1] < self.dataclose[-2]:
# previous close less than the previous close
# BUY, BUY, BUY!!! (with all possible default parameters)
self.log('BUY CREATE, %.2f' % self.dataclose)
# Create a cerebro entity
cerebro = bt.Cerebro(stdstats = False)
# Add a strategy
# Datas are in a subfolder of the samples.
datapath = 'datas/2006-day-001.txt'
# Create a Data Feed
data = btfeeds.BacktraderCSVData(dataname=datapath, timeframe = bt.TimeFrame.Days)
# Add the Data Feed to Cerebro
cerebro.resampledata(data, timeframe = bt.TimeFrame.Weeks, compression = 1)
# Set our desired cash start
# Print out the starting conditions
print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue())
# Run over everything
strats = cerebro.run(runonce = False)
# Print out the final result
print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue())
# Plot the result
cerebro.plot(style = 'bar', volume = False)
strat0 = strats
Thanks in advance!!