Set Backtrader Feed Timeframe to Match Bar Frequency
Summary
The document explains why a Backtrader backtest using 15-minute bars may print each bar at the end of the day. The CSV feed specifies a date-time format, but leaves the data timeframe at its default, which is daily. As a result, the framework interprets the records using daily bars despite the finer timestamps in the file.
The accepted solution is to configure the feed with a minutes timeframe and a compression matching the 15-minute interval. A separate response reports using a tick timeframe, but the document does not explain why that would suit interval-based bars, and the main answer identifies the daily default as the cause. The guidance concerns feed metadata and timestamp display; it does not evaluate strategy logic or backtest performance.
Key ideas
- A CSV date-time format alone does not tell Backtrader the bar frequency.
- The default daily timeframe can cause intraday records to display at the end of the day.
- Set the feed timeframe to minutes and its compression to match the bar interval.
- A tick-timeframe suggestion is also reported, but its suitability for 15-minute bars is not explained.
Tags
Full text
# Backtrader doesn't display time when backtesting
# Backtrader doesn't display time when backtesting
I am trying to backtest a strategy with Backtrader (not the first time) and have a problem while printing date & time for each iteration (time stay on 23:59:59). Here are the first lines of my dataset:
What is printed on the console :
And finally how I load my data :
```
data = bt.feeds.GenericCSVData(dataname="BTCUSD_15MIN.csv",
datetime=0,
fromdate=datetime.datetime(2015,1,13),
todate=datetime.datetime(2015,1,15),
open=1,
high=2,
low=3,
close=4,
openinterest=-1,
time=-1,
volume=-1,
dtformat="%Y-%m-%d %H:%M:%S")
```
Has someone already got this issue? Thanks a lot!
## Answer by mementum (score 2, accepted)
https://quant.stackexchange.com/a/37090
That for sure only solved your problem by chance (because what you chose is smaller than the actual reality)
Your data is obviously `15-minutes` based. But without specification, you let the default values in place: `bt.TimeFrame.Daily`, which gives you the end of the day for each bar. No surprises there.
The right choice would therefore be:
```
timeframe=bt.TimeFrame.Minutes,
compression=15,
```
This is explained in the backtrader community in several posts and in the FAQ.
- One post with the same content: https://community.backtrader.com/topic/244/backtesting-1-minute-data/
## Answer by AlexM (score 3)
https://quant.stackexchange.com/a/37068
Answer: This is the line that sorted out my issue :
```
timeframe=bt.TimeFrame.Ticks
```
If interested in the strategy results, it's here.Shown in full with attribution under the source's licence. Licence: CC BY-SA 4.0 (Stack Exchange)
This summary was written by Stratmill's research agent from the original; it is not a copy of the source.