Plotting dates range
-
I'm trying to use linear regression algorithm in backtesting. 90% of time strategy uses data for training, then 10% uses to testing model.
How to tell cerebro to plot only last 10% of overall data? In other words, how to tell cerebro to plot data from exact date only and skip data before it? -
You couldn't up until today. This was a particularly little nifty idea which should have probably been there for a long time.
Already commited to the development branch: https://github.com/mementum/backtrader/commit/83fe24237dc406e13b81de9da431766b8c6c28e2
Use
start
andend
as named arguments tocerebro.plot
. The defaults are0
and-1
(standard python for first and last) but you can also passdatetime.date/datetime
instances to restrict the plot -
10x m8 and gg!
Will try it in 1.9.31!
-
Could you suggest please how to convert date time into index using
DataFeed
? -
For starters you can pass a
datetime
instance as the key to decide where to start/stop plotting. But ...No timestamp is guaranteed to be in the data feed (even if in your case you know it is there, but the in most general case, it cannot be guaranteed).
dtnum = datafeed.date2num(dt_timestamp)
And you can then use the
bisect
built-in module to locate the insertion index ofdtnum
indatafeed.datetime
If the timestamp is there, the index will match. If not, it will give you the best possible approximation (left or right, that's a choice)
-
Thanks! Work like a charm!
-
@backtrader if I understand correctly, this may be a more appropriate way to set a closing bar stop for live trading? Or does this only apply to historical data?
-
@RandyT said in Plotting dates range:
@backtrader if I understand correctly, this may be a more appropriate way to set a closing bar stop for live trading? Or does this only apply to historical data?
The final answer in this thread was meant to provide a way to find the closest index to a timestamp to restrict the actual plotting.
May the above question be misplaced? (and therefore intended for another thread)
-
Correct, does not make sense in this context. Please delete.
-
Can you show an example of plotting between to specific dates. ie 01/01/2008 to 12/31/2009
-