def plot_strategy_signal_proportion(self, strip = None): signal = self._strategy_signal long = signal[signal > 0].count() short = signal[signal < 0].count() flat = signal[signal == 0].count() keys = long.index df = pandas.DataFrame(index = keys, columns = ['Long', 'Short', 'Flat']) df['Long'] = long df['Short'] = short df['Flat'] = flat if strip is not None: keys = [k.replace(strip, '') for k in keys] df.index = keys df = df.sort_index() pf = PlotFactory() gp = GraphProperties() gp.title = self.FINAL_STRATEGY gp.display_legend = True gp.scale_factor = self.SCALE_FACTOR gp.file_output = self.DUMP_PATH + self.FINAL_STRATEGY + ' (Strategy signal proportion).png' try: pf.plot_bar_graph(self.reduce_plot(df), adapter = 'pythalesians', gp = gp) except: pass
def plot_strategy_group_benchmark_pnl(self): pf = PlotFactory() gp = GraphProperties() gp.title = self.FINAL_STRATEGY gp.display_legend = True gp.scale_factor = self.SCALE_FACTOR #gp.color = 'RdYlGn' gp.file_output = self.DUMP_PATH + self.FINAL_STRATEGY + ' (Group Benchmark PnL - cumulative).png' # plot cumulative line of returns pf.plot_line_graph(self.reduce_plot(self._strategy_group_benchmark_pnl), adapter = 'pythalesians', gp = gp) # needs write stats flag turned on try: keys = self._strategy_group_benchmark_tsd.keys() ir = [] for key in keys: ir.append(self._strategy_group_benchmark_tsd[key].inforatio()[0]) ret_stats = pandas.DataFrame(index = keys, data = ir, columns = ['IR']) ret_stats = ret_stats.sort_index() gp.file_output = self.DUMP_PATH + self.FINAL_STRATEGY + ' (Group Benchmark PnL - IR).png' gp.display_brand_label = False # plot ret stats pf.plot_bar_graph(ret_stats, adapter = 'pythalesians', gp = gp) except: pass
def plot_strategy_group_benchmark_pnl(self): pf = PlotFactory() gp = GraphProperties() gp.title = self.FINAL_STRATEGY gp.display_legend = True gp.scale_factor = self.SCALE_FACTOR #gp.color = 'RdYlGn' gp.file_output = self.DUMP_PATH + self.FINAL_STRATEGY + ' (Group Benchmark PnL - cumulative).png' # plot cumulative line of returns pf.plot_line_graph(self.reduce_plot( self._strategy_group_benchmark_pnl), adapter='pythalesians', gp=gp) keys = self._strategy_group_benchmark_tsd.keys() ir = [] for key in keys: ir.append(self._strategy_group_benchmark_tsd[key].inforatio()[0]) ret_stats = pandas.DataFrame(index=keys, data=ir, columns=['IR']) ret_stats = ret_stats.sort() gp.file_output = self.DUMP_PATH + self.FINAL_STRATEGY + ' (Group Benchmark PnL - IR).png' gp.display_brand_label = False # plot ret stats pf.plot_bar_graph(ret_stats, adapter='pythalesians', gp=gp)
def plot_single_var_regression( self, y, x, y_variable_names, x_variable_names, statistic, tag='stats', title=None, pretty_index=None, output_path=None, scale_factor=Constants.plotfactory_scale_factor, silent_plot=False, shift=[0]): if not (isinstance(statistic, list)): statistic = [statistic] # TODO optimise loop so that we are calculating each regression *once* at present calculating it # for each statistic, which is redundant for st in statistic: stats_df = [] for sh in shift: x_sh = x.shift(sh) stats_temp = self.report_single_var_regression( y, x_sh, y_variable_names, x_variable_names, st, pretty_index) stats_temp.columns = [ x + "_" + str(sh) for x in stats_temp.columns ] stats_df.append(stats_temp) stats_df = pandas.concat(stats_df, axis=1) stats_df = stats_df.dropna(how='all') if silent_plot: return stats_df pf = PlotFactory() gp = GraphProperties() if title is None: title = statistic gp.title = title gp.display_legend = True gp.scale_factor = scale_factor # gp.color = ['red', 'blue', 'purple', 'gray', 'yellow', 'green', 'pink'] if output_path is not None: gp.file_output = output_path + ' (' + tag + ' ' + st + ').png' pf.plot_bar_graph(stats_df, adapter='pythalesians', gp=gp) return stats_df
def plot_strategy_signal_proportion(self, strip=None): signal = self._strategy_signal # count number of long, short and flat periods in our sample long = signal[signal > 0].count() short = signal[signal < 0].count() flat = signal[signal == 0].count() keys = long.index # how many trades have there been (ignore size of the trades) trades = abs(signal - signal.shift(-1)) trades = trades[trades > 0].count() df_trades = pandas.DataFrame(index=keys, columns=['Trades'], data=trades) df = pandas.DataFrame(index=keys, columns=['Long', 'Short', 'Flat']) df['Long'] = long df['Short'] = short df['Flat'] = flat if strip is not None: keys = [k.replace(strip, '') for k in keys] df.index = keys df_trades.index = keys # df = df.sort_index() pf = PlotFactory() gp = GraphProperties() gp.title = self.FINAL_STRATEGY gp.display_legend = True gp.scale_factor = self.SCALE_FACTOR try: gp.file_output = self.DUMP_PATH + self.FINAL_STRATEGY + ' (Strategy signal proportion).png' pf.plot_bar_graph(self.reduce_plot(df), adapter='pythalesians', gp=gp) gp.file_output = self.DUMP_PATH + self.FINAL_STRATEGY + ' (Strategy trade no).png' pf.plot_bar_graph(self.reduce_plot(df_trades), adapter='pythalesians', gp=gp) except: pass
def plot_single_var_regression(self, y, x, y_variable_names, x_variable_names, statistic, tag = 'stats', title = None, pretty_index = None, output_path = None, scale_factor = Constants.plotfactory_scale_factor, silent_plot = False, shift=[0]): if not(isinstance(statistic, list)): statistic = [statistic] # TODO optimise loop so that we are calculating each regression *once* at present calculating it # for each statistic, which is redundant for st in statistic: stats_df = [] for sh in shift: x_sh = x.shift(sh) stats_temp = self.report_single_var_regression(y, x_sh, y_variable_names, x_variable_names, st, pretty_index) stats_temp.columns = [ x + "_" + str(sh) for x in stats_temp.columns] stats_df.append(stats_temp) stats_df = pandas.concat(stats_df, axis=1) stats_df = stats_df.dropna(how='all') if silent_plot: return stats_df pf = PlotFactory() gp = GraphProperties() if title is None: title = statistic gp.title = title gp.display_legend = True gp.scale_factor = scale_factor # gp.color = ['red', 'blue', 'purple', 'gray', 'yellow', 'green', 'pink'] if output_path is not None: gp.file_output = output_path + ' (' + tag + ' ' + st + ').png' pf.plot_bar_graph(stats_df, adapter = 'pythalesians', gp = gp) return stats_df
def plot_strategy_group_benchmark_pnl(self, strip=None): pf = PlotFactory() gp = GraphProperties() gp.title = self.FINAL_STRATEGY gp.display_legend = True gp.scale_factor = self.SCALE_FACTOR #gp.color = 'RdYlGn' gp.file_output = self.DUMP_PATH + self.FINAL_STRATEGY + ' (Group Benchmark PnL - cumulative).png' strat_list = self._strategy_group_benchmark_pnl.columns.sort_values() for line in strat_list: self.logger.info(line) # plot cumulative line of returns pf.plot_line_graph(self.reduce_plot( self._strategy_group_benchmark_pnl), adapter='pythalesians', gp=gp) # needs write stats flag turned on try: keys = self._strategy_group_benchmark_tsd.keys() ir = [] for key in keys: ir.append( self._strategy_group_benchmark_tsd[key].inforatio()[0]) if strip is not None: keys = [k.replace(strip, '') for k in keys] ret_stats = pandas.DataFrame(index=keys, data=ir, columns=['IR']) ret_stats = ret_stats.sort_index() gp.file_output = self.DUMP_PATH + self.FINAL_STRATEGY + ' (Group Benchmark PnL - IR).png' gp.display_brand_label = False # plot ret stats pf.plot_bar_graph(ret_stats, adapter='pythalesians', gp=gp) except: pass
def plot_strategy_signal_proportion(self, strip = None): signal = self._strategy_signal # count number of long, short and flat periods in our sample long = signal[signal > 0].count() short = signal[signal < 0].count() flat = signal[signal == 0].count() keys = long.index # how many trades have there been (ignore size of the trades) trades = abs(signal - signal.shift(-1)) trades = trades[trades > 0].count() df_trades = pandas.DataFrame(index = keys, columns = ['Trades'], data = trades) df = pandas.DataFrame(index = keys, columns = ['Long', 'Short', 'Flat']) df['Long'] = long df['Short'] = short df['Flat'] = flat if strip is not None: keys = [k.replace(strip, '') for k in keys] df.index = keys df_trades.index = keys # df = df.sort_index() pf = PlotFactory() gp = GraphProperties() gp.title = self.FINAL_STRATEGY gp.display_legend = True gp.scale_factor = self.SCALE_FACTOR try: gp.file_output = self.DUMP_PATH + self.FINAL_STRATEGY + ' (Strategy signal proportion).png' pf.plot_bar_graph(self.reduce_plot(df), adapter = 'pythalesians', gp = gp) gp.file_output = self.DUMP_PATH + self.FINAL_STRATEGY + ' (Strategy trade no).png' pf.plot_bar_graph(self.reduce_plot(df_trades), adapter = 'pythalesians', gp = gp) except: pass
def plot_single_var_regression( self, y, x, y_variable_names, x_variable_names, statistic, tag='stats', title=None, pretty_index=None, output_path=None, scale_factor=Constants.plotfactory_scale_factor, silent_plot=False): stats_df = self.report_single_var_regression(y, x, y_variable_names, x_variable_names, statistic, pretty_index) if silent_plot: return stats_df pf = PlotFactory() gp = GraphProperties() if title is None: title = statistic gp.title = title gp.display_legend = True gp.scale_factor = scale_factor # gp.color = ['red', 'blue', 'purple', 'gray', 'yellow', 'green', 'pink'] if output_path is not None: gp.file_output = output_path + ' (' + tag + ').png' pf.plot_bar_graph(stats_df, adapter='pythalesians', gp=gp) return stats_df
def plot_strategy_signal_proportion(self, strip=None): signal = self._strategy_signal long = signal[signal > 0].count() short = signal[signal < 0].count() flat = signal[signal == 0].count() keys = long.index df = pandas.DataFrame(index=keys, columns=['Long', 'Short', 'Flat']) df['Long'] = long df['Short'] = short df['Flat'] = flat if strip is not None: keys = [k.replace(strip, '') for k in keys] df.index = keys df = df.sort_index() pf = PlotFactory() gp = GraphProperties() gp.title = self.FINAL_STRATEGY gp.display_legend = True gp.scale_factor = self.SCALE_FACTOR gp.file_output = self.DUMP_PATH + self.FINAL_STRATEGY + ' (Strategy signal proportion).png' try: pf.plot_bar_graph(self.reduce_plot(df), adapter='pythalesians', gp=gp) except: pass
data_source='bloomberg', # use Bloomberg as data source tickers=tickers, # ticker (Thalesians) fields=['close'], # which fields to download vendor_tickers=vendor_tickers, # ticker (Bloomberg) vendor_fields=['PX_LAST'], # which Bloomberg fields to download cache_algo='internet_load_return') # how to return data daily_vals = ltsf.harvest_time_series(time_series_request) # resample for year end daily_vals = daily_vals.resample('A') daily_vals = daily_vals / daily_vals.shift(1) - 1 daily_vals.index = daily_vals.index.year daily_vals = daily_vals.drop(daily_vals.head(1).index) pf = PlotFactory() gp = GraphProperties() gp.source = 'Thalesians/BBG' gp.title = 'Yearly changes in spot' gp.scale_factor = 3 gp.y_title = "Percent Change" daily_vals = daily_vals * 100 # plot using PyThalesians (stacked & then bar graph) pf.plot_stacked_graph(daily_vals, adapter='pythalesians', gp=gp) pf.plot_bar_graph(daily_vals, adapter='pythalesians', gp=gp)
vendor_tickers=vendor_tickers, # ticker (Bloomberg) vendor_fields=["PX_LAST"], # which Bloomberg fields to download cache_algo="internet_load_return", ) # how to return data daily_vals = ltsf.harvest_time_series(time_series_request) # resample for end of month daily_vals = daily_vals.resample("BM") daily_vals = daily_vals / daily_vals.shift(1) - 1 daily_vals.index = [str(x.year) + "/" + str(x.month) for x in daily_vals.index] daily_vals = daily_vals.drop(daily_vals.head(1).index) pf = PlotFactory() gp = GraphProperties() gp.source = "Thalesians/BBG" gp.html_file_output = "output_data/equities.htm" gp.title = "Recent monthly changes in equity markets" gp.scale_factor = 2 gp.display_legend = True gp.chart_type = ["bar", "scatter", "line"] gp.x_title = "Dates" gp.y_title = "Pc" # plot using Bokeh then PyThalesians pf.plot_bar_graph(daily_vals * 100, adapter="bokeh", gp=gp) pf.plot_bar_graph(daily_vals * 100, adapter="pythalesians", gp=gp)
vendor_tickers=vendor_tickers, # ticker (Bloomberg) vendor_fields=['PX_LAST'], # which Bloomberg fields to download cache_algo='internet_load_return') # how to return data daily_vals = ltsf.harvest_time_series(time_series_request) # resample for end of month daily_vals = daily_vals.resample('BM') daily_vals = daily_vals / daily_vals.shift(1) - 1 daily_vals.index = [ str(x.year) + '/' + str(x.month) for x in daily_vals.index ] daily_vals = daily_vals.drop(daily_vals.head(1).index) pf = PlotFactory() gp = GraphProperties() gp.source = 'Thalesians/BBG' gp.html_file_output = "output_data/equities.htm" gp.title = 'Recent monthly changes in equity markets' gp.scale_factor = 2 gp.display_legend = True gp.chart_type = ['bar', 'scatter', 'line'] gp.x_title = 'Dates' gp.y_title = 'Pc' # plot using Bokeh then PyThalesians pf.plot_bar_graph(daily_vals * 100, adapter='bokeh', gp=gp) pf.plot_bar_graph(daily_vals * 100, adapter='pythalesians', gp=gp)
df.columns = [x.replace(".close", "") for x in df.columns.values] short_dates = df[["EURUSDV1M", "USDJPYV1M"]] long_dates = df[["EURUSDV1Y", "USDJPYV1Y"]] short_dates, long_dates = short_dates.align(long_dates, join="left", axis=0) slope = pandas.DataFrame( data=short_dates.values - long_dates.values, index=short_dates.index, columns=["EURUSDV1M-1Y", "USDJPYV1M-1Y"] ) # resample fand calculate average over month slope_monthly = slope.resample("M", how="mean") slope_monthly.index = [str(x.year) + "/" + str(x.month) for x in slope_monthly.index] pf = PlotFactory() gp = GraphProperties() gp.source = "Thalesians/BBG" gp.title = "Vol slopes in EUR/USD and USD/JPY recently" gp.scale_factor = 2 gp.display_legend = True gp.chart_type = "bar" gp.x_title = "Dates" gp.y_title = "Pc" # plot using Cufflinks pf.plot_bar_graph(slope_monthly, adapter="cufflinks", gp=gp)
tickers=tickers, # ticker (Thalesians) fields=["close"], # which fields to download vendor_tickers=vendor_tickers, # ticker (Bloomberg) vendor_fields=["PX_LAST"], # which Bloomberg fields to download cache_algo="internet_load_return", ) # how to return data daily_vals = ltsf.harvest_time_series(time_series_request) # resample for year end daily_vals = daily_vals.resample("A") daily_vals = daily_vals / daily_vals.shift(1) - 1 daily_vals.index = daily_vals.index.year daily_vals = daily_vals.drop(daily_vals.head(1).index) pf = PlotFactory() gp = GraphProperties() gp.source = "Thalesians/BBG" gp.title = "Yearly changes in spot" gp.scale_factor = 3 gp.y_title = "Percent Change" daily_vals = daily_vals * 100 # plot using PyThalesians (stacked & then bar graph) pf.plot_stacked_graph(daily_vals, adapter="pythalesians", gp=gp) pf.plot_bar_graph(daily_vals, adapter="pythalesians", gp=gp)
pf = PlotFactory() gp = GraphProperties() gp.source = 'Thalesians/BBG (created with PyThalesians Python library)' gp.html_file_output = "output_data/equities.htm" gp.title = 'Recent monthly changes in equity markets' gp.scale_factor = 2 gp.display_legend = True gp.chart_type = ['bar', 'scatter', 'line'] gp.x_title = 'Dates' gp.y_title = 'Pc' # plot using Bokeh then PyThalesians pf.plot_bar_graph(daily_vals * 100, adapter = 'bokeh', gp = gp) pf.plot_bar_graph(daily_vals * 100, adapter = 'pythalesians', gp = gp) # plot daily changes in FX if True: from datetime import timedelta ltsf = LightTimeSeriesFactory() end = datetime.datetime.utcnow() start = end - timedelta(days=5) tickers = ['EUR', 'GBP', 'AUD', 'NZD', 'CAD', 'CHF', 'NOK', 'SEK', 'JPY'] vendor_tickers = [x + 'USD BGN Curncy' for x in tickers] time_series_request = TimeSeriesRequest( start_date = start, # start date
import pandas df.columns = [x.replace('.close', '') for x in df.columns.values] short_dates = df[["EURUSDV1M", "USDJPYV1M"]] long_dates = df[["EURUSDV1Y", "USDJPYV1Y"]] short_dates, long_dates = short_dates.align(long_dates, join='left', axis = 0) slope = pandas.DataFrame(data = short_dates.values - long_dates.values, index = short_dates.index, columns = ["EURUSDV1M-1Y", "USDJPYV1M-1Y"]) # resample fand calculate average over month slope_monthly = slope.resample('M', how='mean') slope_monthly.index = [str(x.year) + '/' + str(x.month) for x in slope_monthly.index] pf = PlotFactory() gp = GraphProperties() gp.source = 'Thalesians/BBG' gp.title = 'Vol slopes in EUR/USD and USD/JPY recently' gp.scale_factor = 2 gp.display_legend = True gp.chart_type = 'bar' gp.x_title = 'Dates' gp.y_title = 'Pc' # plot using Cufflinks pf.plot_bar_graph(slope_monthly, adapter = 'bokeh', gp = gp)
long_dates = df[["EURUSDV1Y", "USDJPYV1Y"]] short_dates, long_dates = short_dates.align(long_dates, join='left', axis=0) slope = pandas.DataFrame(data=short_dates.values - long_dates.values, index=short_dates.index, columns=["EURUSDV1M-1Y", "USDJPYV1M-1Y"]) # resample fand calculate average over month slope_monthly = slope.resample('M', how='mean') slope_monthly.index = [ str(x.year) + '/' + str(x.month) for x in slope_monthly.index ] pf = PlotFactory() gp = GraphProperties() gp.source = 'Thalesians/BBG' gp.title = 'Vol slopes in EUR/USD and USD/JPY recently' gp.scale_factor = 2 gp.display_legend = True gp.chart_type = 'bar' gp.x_title = 'Dates' gp.y_title = 'Pc' # plot using Cufflinks pf.plot_bar_graph(slope_monthly, adapter='bokeh', gp=gp)
long_dates = df[["EURUSDV1Y", "USDJPYV1Y"]] short_dates, long_dates = short_dates.align(long_dates, join='left', axis=0) slope = pandas.DataFrame(data=short_dates.values - long_dates.values, index=short_dates.index, columns=["EURUSDV1M-1Y", "USDJPYV1M-1Y"]) # resample fand calculate average over month slope_monthly = slope.resample('M', how='mean') slope_monthly.index = [ str(x.year) + '/' + str(x.month) for x in slope_monthly.index ] pf = PlotFactory() gp = GraphProperties() gp.source = 'Thalesians/BBG' gp.title = 'Vol slopes in EUR/USD and USD/JPY recently' gp.scale_factor = 2 gp.display_legend = True gp.chart_type = 'bar' gp.x_title = 'Dates' gp.y_title = 'Pc' # plot using Cufflinks pf.plot_bar_graph(slope_monthly, adapter='cufflinks', gp=gp)