From 982e48cd57d2be0725414a13469f009764efc048 Mon Sep 17 00:00:00 2001 From: Joshua Date: Fri, 9 Oct 2015 17:29:41 +0800 Subject: [PATCH 1/3] update plotter to leave out days/minites on which there is no data, such as weekends, time between 4:00pm to nextday's 9:30am --- pyalgotrade/plotter.py | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/pyalgotrade/plotter.py b/pyalgotrade/plotter.py index 7baef065d..a15ece875 100644 --- a/pyalgotrade/plotter.py +++ b/pyalgotrade/plotter.py @@ -19,7 +19,7 @@ """ import collections - +import numpy as np import broker import matplotlib.pyplot as plt @@ -74,11 +74,17 @@ def getMarker(self): def needColor(self): raise NotImplementedError() - def plot(self, mplSubplot, dateTimes, color): + + + def plot(self, mplSubplot, dateTimes, ind, color): values = [] for dateTime in dateTimes: values.append(self.getValue(dateTime)) - mplSubplot.plot(dateTimes, values, color=color, marker=self.getMarker()) + mplSubplot.plot(ind, values, color=color, marker=self.getMarker()) + def format_date(x, pos=None): + thisind = np.clip(int(x+0.5), 0, len(dateTimes)-1) + return dateTimes[thisind].strftime('%Y-%m-%d %H:%M:%S') + mplSubplot.xaxis.set_major_formatter(ticker.FuncFormatter(format_date)) class BuyMarker(Series): @@ -264,12 +270,12 @@ def customizeSubplot(self, mplSubplot): # Don't scale the Y axis mplSubplot.yaxis.set_major_formatter(ticker.ScalarFormatter(useOffset=False)) - def plot(self, mplSubplot, dateTimes): + def plot(self, mplSubplot, dateTimes, ind): for series in self.__series.values(): color = None if series.needColor(): color = self.__getColor(series) - series.plot(mplSubplot, dateTimes, color) + series.plot(mplSubplot, dateTimes, ind, color) # Legend mplSubplot.legend(self.__series.keys(), shadow=True, loc="best") @@ -399,6 +405,13 @@ def getPortfolioSubplot(self): def __buildFigureImpl(self, fromDateTime=None, toDateTime=None): dateTimes = _filter_datetimes(self.__dateTimes, fromDateTime, toDateTime) dateTimes.sort() + # we'll write a custom formatter + N = len(dateTimes) + ind = np.arange(N) # the evenly spaced plot indices + def format_date(x, pos=None): + thisind = np.clip(int(x+0.5), 0, N-1) + return r.dateTimes[thisind].strftime('%Y-%mm-%dd %H:%M:%S') + subplots = [] subplots.extend(self.__barSubplots.values()) @@ -413,7 +426,7 @@ def __buildFigureImpl(self, fromDateTime=None, toDateTime=None): axesSubplot = axes[i][0] if not subplot.isEmpty(): mplSubplots.append(axesSubplot) - subplot.plot(axesSubplot, dateTimes) + subplot.plot(axesSubplot,dateTimes,ind) axesSubplot.grid(True) return (fig, mplSubplots) From 2e63a74bf8a7fb1e3f05e25283c4f03931c5bd7a Mon Sep 17 00:00:00 2001 From: Joshua Date: Sat, 10 Oct 2015 09:40:23 +0800 Subject: [PATCH 2/3] modify for compatibility, add para valid_only for series.plot , while it's true, only datetime with data will be plotted, or all the time series will be plotted which is the same as the former edition --- pyalgotrade/plotter.py | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/pyalgotrade/plotter.py b/pyalgotrade/plotter.py index a15ece875..45c93f4eb 100644 --- a/pyalgotrade/plotter.py +++ b/pyalgotrade/plotter.py @@ -76,16 +76,21 @@ def needColor(self): - def plot(self, mplSubplot, dateTimes, ind, color): + def plot(self, mplSubplot, dateTimes, color, valid_only=True): values = [] for dateTime in dateTimes: values.append(self.getValue(dateTime)) - mplSubplot.plot(ind, values, color=color, marker=self.getMarker()) - def format_date(x, pos=None): - thisind = np.clip(int(x+0.5), 0, len(dateTimes)-1) - return dateTimes[thisind].strftime('%Y-%m-%d %H:%M:%S') - mplSubplot.xaxis.set_major_formatter(ticker.FuncFormatter(format_date)) - + if(valid_only): + # we'll write a custom formatter + N = len(dateTimes) + ind = np.arange(N) # the evenly spaced plot indices + mplSubplot.plot(ind, values, color=color, marker=self.getMarker()) + def format_date(x, pos=None): + thisind = np.clip(int(x+0.5), 0, len(dateTimes)-1) + return dateTimes[thisind].strftime('%Y-%m-%d %H:%M:%S') + mplSubplot.xaxis.set_major_formatter(ticker.FuncFormatter(format_date)) + else: + mplSubplot.plot(dateTimes, values, color=color, marker=self.getMarker()) class BuyMarker(Series): def getColor(self): @@ -270,12 +275,12 @@ def customizeSubplot(self, mplSubplot): # Don't scale the Y axis mplSubplot.yaxis.set_major_formatter(ticker.ScalarFormatter(useOffset=False)) - def plot(self, mplSubplot, dateTimes, ind): + def plot(self, mplSubplot, dateTimes): for series in self.__series.values(): color = None if series.needColor(): color = self.__getColor(series) - series.plot(mplSubplot, dateTimes, ind, color) + series.plot(mplSubplot, dateTimes, color) # Legend mplSubplot.legend(self.__series.keys(), shadow=True, loc="best") @@ -405,13 +410,6 @@ def getPortfolioSubplot(self): def __buildFigureImpl(self, fromDateTime=None, toDateTime=None): dateTimes = _filter_datetimes(self.__dateTimes, fromDateTime, toDateTime) dateTimes.sort() - # we'll write a custom formatter - N = len(dateTimes) - ind = np.arange(N) # the evenly spaced plot indices - def format_date(x, pos=None): - thisind = np.clip(int(x+0.5), 0, N-1) - return r.dateTimes[thisind].strftime('%Y-%mm-%dd %H:%M:%S') - subplots = [] subplots.extend(self.__barSubplots.values()) @@ -426,7 +424,7 @@ def format_date(x, pos=None): axesSubplot = axes[i][0] if not subplot.isEmpty(): mplSubplots.append(axesSubplot) - subplot.plot(axesSubplot,dateTimes,ind) + subplot.plot(axesSubplot,dateTimes) axesSubplot.grid(True) return (fig, mplSubplots) From 1508eda949fd7186f055bb827bd08687fe56972b Mon Sep 17 00:00:00 2001 From: Joshua Date: Sat, 10 Oct 2015 18:27:51 +0800 Subject: [PATCH 3/3] clean some warnings --- pyalgotrade/plotter.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/pyalgotrade/plotter.py b/pyalgotrade/plotter.py index 45c93f4eb..7ab0697a8 100644 --- a/pyalgotrade/plotter.py +++ b/pyalgotrade/plotter.py @@ -74,8 +74,6 @@ def getMarker(self): def needColor(self): raise NotImplementedError() - - def plot(self, mplSubplot, dateTimes, color, valid_only=True): values = [] for dateTime in dateTimes: @@ -85,7 +83,7 @@ def plot(self, mplSubplot, dateTimes, color, valid_only=True): N = len(dateTimes) ind = np.arange(N) # the evenly spaced plot indices mplSubplot.plot(ind, values, color=color, marker=self.getMarker()) - def format_date(x, pos=None): + def format_date(x): thisind = np.clip(int(x+0.5), 0, len(dateTimes)-1) return dateTimes[thisind].strftime('%Y-%m-%d %H:%M:%S') mplSubplot.xaxis.set_major_formatter(ticker.FuncFormatter(format_date)) @@ -183,7 +181,7 @@ def needColor(self): def getColorForValue(self, value, default): return default - def plot(self, mplSubplot, dateTimes, color): + def plot(self, mplSubplot, dateTimes, color, valid_onl=True): validDateTimes = [] values = [] colors = [] @@ -424,7 +422,7 @@ def __buildFigureImpl(self, fromDateTime=None, toDateTime=None): axesSubplot = axes[i][0] if not subplot.isEmpty(): mplSubplots.append(axesSubplot) - subplot.plot(axesSubplot,dateTimes) + subplot.plot(axesSubplot, dateTimes) axesSubplot.grid(True) return (fig, mplSubplots)