def _plot(self, x, y, labels=None, x_label='Return [%]', y_label='-', title='Backtesting results', plot_title='', xlim=None, ylim=None, pres_name=None): if pres_name is None: pres_name = self.presname pres = Presenter(pres_name) fig = plt.figure() plt.scatter(x, y) if xlim is not None: plt.xlim(xlim) if ylim is not None: plt.ylim(ylim) plt.xlabel(x_label) plt.ylabel(y_label) plt.title(plot_title) plt.grid() img_path = 'temp.png' plt.savefig(img_path) plt.close(fig) pres.add_picture_slide(img_path, title=title) pres.save_presentation(pres_name)
def _cm_plot(self, x, y, labels=None, x_label='Transaction Value [monetary unit]', y_label='Transaction Costs [monetary unit]', title='Transaction costs', plot_title=''): pres = Presenter(self.presname) fig = plt.figure() if isinstance(y[0], np.ndarray) | isinstance(y[0], list): for i in range(0, len(y)): plt.plot(x, y[i], label=labels[i]) plt.legend(loc='upper left') else: plt.plot(x, y, 'b', label=labels) if labels is not None: plt.legend(loc='upper left') plt.xlabel(x_label) plt.ylabel(y_label) plt.title(plot_title) img_path = 'temp.png' plt.savefig(img_path) plt.close(fig) pres.add_picture_slide(img_path, title=title) pres.save_presentation(self.presname) os.remove(img_path)
def compare_to_moving_average(self, window): if not os.path.exists('Data_Checks'): os.makedirs('Data_Checks') location = 'Data_Checks' pres = Presenter() pres.start_presentation( title=''.join(['Data Checks ', self.ticker]), subtitle=dt.datetime.today().date().isoformat()) mov_df = pd.DataFrame(index=[], columns=[ 'id', 'price_date', 'item', 'value', 'lower_limit', 'upper_limit' ]) for name in self.orig_names: mov_avg = pd.rolling_mean(self.unchecked_data[name], window=window, min_periods=1) mov_std = pd.rolling_std(self.unchecked_data[name], window=window, min_periods=1) num_std = 3 # Determine range up_lim = mov_avg + num_std * mov_std low_lim = mov_avg - num_std * mov_std # Plot it dates = pd.to_datetime(self.unchecked_data['price_date']) fig = plt.figure() plt.xticks(rotation=45) plt.gcf().subplots_adjust(bottom=0.15) plt.plot(dates.values, self.unchecked_data[name], 'r', label='Raw data') plt.plot(dates.values, mov_avg, 'b', label='Moving average') plt.plot(dates.values, up_lim, 'g', label=''.join([str(num_std), 'std'])) plt.plot(dates.values, low_lim, 'g') plt.xlabel('Time') plt.ylabel('Value') plt.legend(loc='upper left') img_path = ''.join(['temp', self.ticker, '.png']) plt.savefig(img_path) plt.close(fig) pres.add_picture_slide(img_path, title=name) # Find all values that lie outside of this range extr = self.unchecked_data.loc[ (self.unchecked_data[name] > up_lim) | (self.unchecked_data[name] < low_lim)] # Add to list extr_select = extr.ix[:, ['id', 'price_date', name]] extr_select.loc[:, 'item'] = [name] * len(extr_select) extr_select.loc[:, 'upper_limit'] = up_lim[extr_select.index] extr_select.loc[:, 'lower_limit'] = low_lim[extr_select.index] extr_select.rename(columns={name: 'value'}, inplace=True) mov_df = pd.concat( [mov_df, extr_select.reindex_axis(mov_df.columns, axis=1)]) # Print list mov_df.to_csv(''.join([ location, '/', self.ticker, '_', self.symbol_id, '_moving_avg_data.csv' ]), index=False) pres.save_presentation(''.join( [location, '/', self.ticker, '_', self.symbol_id, '_plots.pptx'])) os.remove(img_path) return mov_df