Skip to content

Commit 83fe242

Browse files
committed
Allow plotting specific date ranges with start and end named arguments to plot
1 parent 6ed7413 commit 83fe242

2 files changed

Lines changed: 22 additions & 29 deletions

File tree

backtrader/cerebro.py

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -613,7 +613,7 @@ def getbroker(self):
613613

614614
broker = property(getbroker, setbroker)
615615

616-
def plot(self, plotter=None, numfigs=1, iplot=True, useplotly=False,
616+
def plot(self, plotter=None, numfigs=1, iplot=True, start=0, end=-1,
617617
**kwargs):
618618
'''
619619
Plots the strategies inside cerebro
@@ -627,6 +627,13 @@ def plot(self, plotter=None, numfigs=1, iplot=True, useplotly=False,
627627
``iplot``: if ``True`` and running in a ``notebook`` the charts will be
628628
displayed inline
629629
630+
``start``: An index to the datetime line array of the strategy or a
631+
``datetime.date``, ``datetime.datetime`` instance indicating the start
632+
of the plot
633+
634+
``end``: An index to the datetime line array of the strategy or a
635+
``datetime.date``, ``datetime.datetime`` instance indicating the end
636+
of the plot
630637
'''
631638
if self._exactbars > 0:
632639
return
@@ -638,14 +645,6 @@ def plot(self, plotter=None, numfigs=1, iplot=True, useplotly=False,
638645
else:
639646
plotter = plot.Plot(**kwargs)
640647

641-
if useplotly:
642-
try:
643-
from plotly import __version__ as plyversion
644-
except ImportError:
645-
useplotly = False
646-
else:
647-
numfigs = 1 # Let plotly manage zoom, panning ... only 1 fig
648-
649648
# pfillers = {self.datas[i]: self._plotfillers[i]
650649
# for i, x in enumerate(self._plotfillers)}
651650

@@ -657,29 +656,14 @@ def plot(self, plotter=None, numfigs=1, iplot=True, useplotly=False,
657656
for si, strat in enumerate(stratlist):
658657
rfig = plotter.plot(strat, figid=si * 100,
659658
numfigs=numfigs, iplot=iplot,
660-
useplotly=useplotly)
659+
start=start, end=end)
661660
# pfillers=pfillers2)
662661

663662
figs.append(rfig)
664663

665664
plotter.show()
666665
return figs
667666

668-
def plotly(self, plotter=None, numfigs=1, **kwargs):
669-
'''
670-
Plots the strategies inside cerebro in plotly offline if available
671-
672-
If ``plotter`` is None a default ``Plot`` instance is created and
673-
``kwargs`` are passed to it during instantiation.
674-
675-
``numfigs`` split the plot in the indicated number of charts reducing
676-
chart density if wished
677-
678-
If ``plotly`` is really available this will be capped down to 1 to
679-
let plotly take over and control those features
680-
'''
681-
self.plot(plotter=plotter, numfigs=numfigs, useplotly=True, **kwargs)
682-
683667
def __call__(self, iterstrat):
684668
'''
685669
Used during optimization to pass the cerebro over the multiprocesing

backtrader/plot/plot.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import bisect
2525
import collections
26+
import datetime
2627
import itertools
2728
import math
2829
import operator
@@ -36,7 +37,7 @@
3637
import matplotlib.ticker as mticker
3738

3839
from ..utils.py3 import range, with_metaclass
39-
from .. import AutoInfoClass, MetaParams, TimeFrame
40+
from .. import AutoInfoClass, MetaParams, TimeFrame, date2num
4041

4142
from .finance import plot_candlestick, plot_ohlc, plot_volume, plot_lineonclose
4243
from .formatters import (MyVolFormatter, MyDateFormatter, getlocator)
@@ -111,7 +112,8 @@ def drawtag(self, ax, x, y, facecolor, edgecolor, alpha=0.9, **kwargs):
111112
zorder=self.pinf.zorder[ax] + 3.0,
112113
**kwargs)
113114

114-
def plot(self, strategy, figid=0, numfigs=1, iplot=True, useplotly=False):
115+
def plot(self, strategy, figid=0, numfigs=1, iplot=True,
116+
start=0, end=-1, **kwargs):
115117
# pfillers={}):
116118
if not strategy.datas:
117119
return
@@ -131,11 +133,18 @@ def plot(self, strategy, figid=0, numfigs=1, iplot=True, useplotly=False):
131133
self.sortdataindicators(strategy)
132134
self.calcrows(strategy)
133135

134-
slen = len(strategy)
136+
st_dtime = strategy.lines.datetime.plot()
137+
if isinstance(start, datetime.date):
138+
start = bisect.bisect_left(st_dtime, date2num(start))
139+
140+
if isinstance(end, datetime.date):
141+
end = bisect.bisect_right(st_dtime, date2num(end))
142+
143+
slen = len(st_dtime[start:end])
135144
d, m = divmod(slen, numfigs)
136145
pranges = list()
137146
for i in range(numfigs):
138-
a = d * i
147+
a = d * i + start
139148
if i == (numfigs - 1):
140149
d += m # add remainder to last stint
141150
b = a + d

0 commit comments

Comments
 (0)