Backtesting 1 minute data
-
Hello - I've been able to successfully backtest 1 day and 1 hour data and for some reason when I try to backtest on 1 minute data no trades get executed when the backtest completes. I'm loading the appropriate CSV file with GenericCSVData for each test and not using the resampling feature provided by BackTrader. I'm assuming this should theoretically work for 1 min or 5 min.. The data looks like the following:
1 hour
2017-01-27 00:00:00+00:00,1.07246,1.06928,1.06791,1.06576,107892.01 min
2017-01-27 22:59:00+00:00,1.06954,1.06928,1.06948,1.0691,10.0and the dtformat is set as:
dtformat=('%Y-%m-%d %H:%M:%S+00:00')Any suggestions?
Thanks
-
The platform executes trades even with isolated ticks. Unfortunately the information provided above is insufficient to say why you may not be executing.
Long shot: you should always tell the platform which
timeframe
andcompression
the data feed has by specifying both during data feed creation.
-
Thanks, it working now on 1 min data - BackTrader is simply awesome!
-
@Z03 can you please post one example how use intraday timeframes? thx
-
Without knowing what you may be looking for, using intraday timeframes is not different from daily timeframes. In the question above, the problem is that the creation of the data feed was not specifying the
timeframe
andcompression
parameters. As such the platform uses the defaults (timeframe=bt.TimeFrame.Days
)See this Blog - Timers post. The intraday script uses de
5-minutes
data sample available in the sources.An excerpt where the parameters are specified
# Data feed kwargs kwargs = dict( timeframe=bt.TimeFrame.Minutes, compression=5, sessionstart=datetime.time(9, 0), sessionend=datetime.time(17, 30), )
-
I have a data in this format:
.......................................................................................................
.......................................................................................................
I am using this method but although trying of different type of parameters as "fromdate,todate and reverse" the printed dataset is not changingdata = bt.feeds.GenericCSVData(
dataname='data.csv',
datetime=0,
fromdate=datetime.datetime(2017, 12, 28),
timeframe=bt.TimeFrame.Minutes,
dtformat=('%d-%m-%Y %H:%M'),
open=1,
high=2,
low=3,
close=4,
volume=5,
openinterest=-1,
reverse=True)Printed Result:
Starting Portfolio Value: 100000.00
2018-04-27, Close, 996.50
2018-04-27, Close, 994.90
2018-04-27, Close, 998.15
...........................................
2017-12-28, Close, 920.80
2017-12-28, Close, 920.90
2017-12-28, Close, 920.20
2017-12-28, Close, 923.25
2017-12-28, Close, 923.20
Final Portfolio Value: 100000.00I want the data as usual upside down.
-
@ry93 said in Backtesting 1 minute data:
reverse
reverse
is not a parameter ofGenericCSVData
-See Docs - Data Feeds Reference@ry93 said in Backtesting 1 minute data:
I want the data as usual upside down.
Don't know exactly what the upside is in this case. The data has to be chronologically ordered from oldest to newest. As it happens in real life. The file with your data is read line by line.
You will have to reverse the data before passing it to the platform.
-
@backtrader Thanks I got it.
-
@backtrader How to read dataset in following cases:
-
When there is no time column:
Date,Open,High,Low,Close,Volume
12/29/2000,30.87,31.31,28.69,29.06,31655500
12/28/2000,30.56,31.12,30.37,31.06,25055600
12/27/2000,30.37,31.06,29.37,30.69,26441700 -
When time is in separate column:
"Date","Time","O","H","L","C","U","D"
01/02/2006,1530,2821.10,2836.70,2804.00,2819.80,0,0
01/03/2006,1530,2823.00,2872.00,2815.10,2868.70,0,0
01/04/2006,1530,2885.00,2896.00,2873.30,2890.40,0,0
01/05/2006,1530,2892.30,2895.80,2868.00,2886.00,0,0
For "1" case I am trying in this way but is it not working:
bt.feeds.GenericCSVData(
dataname='orcl-2000.csv',
fromdate=datetime.datetime(2000,12,29),
dtformat=('%m/%d/%y'),
datetime=0,
open=1,
high=2,
low=3,
close=4,
volume=5,
openinterest=-1)It is giving me this error:
data_string[found.end():])
ValueError: unconverted data remains: 00
-
-
I was trying to backtest a 5 minute data feed and this didn't completely clarify it so I'll just leave here what I ended up doing to have as reference. My data is in the following format:
time,open,high,low,close,volume. And it goes from 2018-12-19 09:35:00. The datetime format is '%Y-%m-%d %H:%M:%S'.I was trying to do it like this
data = bt.feeds.GenericCSVData( dataname=datapath, timeframe=bt.TimeFrame.Minutes, compression=5, sessionstart=datetime.datetime(2018, 12, 19, 9, 35, 0), sessionend=datetime.datetime(2019, 5, 22, 16, 30, 0), datetime=0, open=1, high=2, low=3, close=4, volume=5, )
and I was getting an
IndexError: list index out of range
. Doing it like this solved the problem:data = bt.feeds.GenericCSVData( dataname=datapath, timeframe=bt.TimeFrame.Minutes, compression=1, # The data is already at 5 minute intervals fromdate=datetime.datetime(2018, 12, 20, 9, 30), todate=datetime.datetime(2019, 5, 22, 16, 30), sessionstart=datetime.time(9, 30), sessionend=datetime.time(16, 30), dtformat='%Y-%m-%d %H:%M:%S', datetime=0, time=-1, open=1, high=2, low=3, close=4, volume=5, openinterest=-1, headers=1, separator=",", reverse=True ) data.addfilter(bt.filters.SessionFilter(data)) cerebro.adddata(data)