backtest with tick data, get period datas
-
Hi,
I have tick data in csv file. I want to calculate last n (x minutes, hourly, daily) period avg from this tick data. How I can backtest my strategy and get period value list?
Best
ct
time,last 2016-02-01 17:44:59,90.75 2016-02-01 17:44:59,90.75 2016-02-01 17:44:59,90.75 2016-02-01 17:44:59,90.75 2016-02-01 17:44:59,90.75 2016-02-01 17:44:59,90.72 2016-02-01 17:44:58,90.75 2016-02-01 17:44:58,90.75 2016-02-01 17:44:58,90.75
-
You probably first need to reverse those ticks because lower ticks have an old timestamp.
Tick Data Resampling is covered for example in this Blog - Tick Data and Resampling
Since you only have last, the most obvious choice when passing that to the generic csv parser is:
open=1, high=1, low=1, high=1,
to make sure the data has something that looks normal. You may also want to set
volume
andopeninterest
to-1
since you have none.The generic csv data feed is documented with the others here: Docs - Data Feeds Reference
-
I converted to ohlc tick manually and started cerebro with resampledata. You can see at below. I can't understand, how I can work on period datas in strategy next or other function?
I expected a property on strategy class for periods calculating on the fly for every tick. Next function calling for every tick am I right?
Thank you for quick response and interest
Best
ct
Datetime,Open,High,Low,Close,Volume,OpenInterest 2016-02-01 17:44:59,90.75,90.75,90.75,90.75,1.0,0 2016-02-01 17:44:59,90.75,90.75,90.75,90.75,2.0,0 2016-02-01 17:44:59,90.75,90.75,90.75,90.75,1.0,0 2016-02-01 17:44:59,90.75,90.75,90.75,90.75,40.0,0 2016-02-01 17:44:59,90.75,90.75,90.75,90.75,2.0,0 2016-02-01 17:44:59,90.72,90.72,90.72,90.72,2.0,0 2016-02-01 17:44:58,90.75,90.75,90.75,90.75,4.0,0 2016-02-01 17:44:58,90.75,90.75,90.75,90.75,4.0,0 2016-02-01 17:44:58,90.75,90.75,90.75,90.75,1.0,0
class TestStrategy(bt.Strategy): def log(self, txt, dt=None): ''' Logging function fot this strategy''' dt = dt or self.datas[0].datetime.date(0) print('%s, %s' % (dt.isoformat(), txt)) def __init__(self): # Keep a reference to the "close" line in the data[0] dataseries self.dataclose = self.datas[0].close def next(self): # Simply log the closing price of the series from the reference self.log('Close, %.2f' % self.dataclose[0]) if __name__ == '__main__': # Create a cerebro entity cerebro = bt.Cerebro() # Add a strategy cerebro.addstrategy(TestStrategy) # Load the Data datapath = 'csv/tickdata.csv' data = btfeeds.BacktraderCSVData( dataname=datapath, timeframe=bt.TimeFrame.Ticks, compression=1, # 1 is the default rtbar=True, # use RealTimeBars ) # Add the Data Feed to Cerebro cerebro.resampledata(data, timeframe=bt.TimeFrame.Days, compression=20) # Set our desired cash start cerebro.broker.setcash(100000.0)
-
first need to reverse those ticks because lower ticks have an old timestamp.
Your ticks are in the wrong order. The resampling process can only work forward. That's the reason your ticks are not being resampled.
The more recent timestamps are at the top of the file and most of the world (Yahoo being an exception) works with files which have the more recent timestamps at the bottom.