def g10_plot_gdp_cpi_une(self, start_date, finish_date, data_type = 'cpi'): country_group = 'g10' if data_type == 'cpi': df = self.get_CPI_YoY(start_date, finish_date, country_group) elif data_type == 'gdp': df = self.get_GDP_YoY(start_date, finish_date, country_group) elif data_type == 'une': df = self.get_UNE(start_date, finish_date, country_group) df = self.hist_econ_data_factory.grasp_coded_entry(df, -1) from pythalesians.graphics.graphs.plotfactory import PlotFactory from pythalesians.graphics.graphs.graphproperties import GraphProperties gp = GraphProperties() pf = PlotFactory() gp.plotly_location_mode = 'world' gp.plotly_choropleth_field = 'Val' gp.plotly_scope = 'world' gp.plotly_projection = 'Mercator' gp.plotly_world_readable = False gp.plotly_url = country_group + "-" + data_type gp.title = "G10 " + data_type gp.units = '%' pf.plot_generic_graph(df, type = 'choropleth', adapter = 'plotly', gp = gp)
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_strategy_signals(self, date=None, strip=None): ######## plot signals strategy_signal = self._strategy_signal strategy_signal = 100 * (strategy_signal) if date is None: last_day = strategy_signal.ix[-1].transpose().to_frame() else: last_day = strategy_signal.ix[date].transpose().to_frame() if strip is not None: last_day.index = [x.replace(strip, '') for x in last_day.index] print(last_day) pf = PlotFactory() gp = GraphProperties() gp.title = self.FINAL_STRATEGY + " positions (% portfolio notional)" gp.scale_factor = self.SCALE_FACTOR gp.file_output = self.DUMP_PATH + self.FINAL_STRATEGY + ' (Positions) ' + str( gp.scale_factor) + '.png' pf.plot_generic_graph(last_day, adapter='pythalesians', type='bar', gp=gp)
def usa_plot_une(self, start_date, finish_date): country_group = 'usa-states' source = 'bloomberg' une = self.get_UNE(start_date, finish_date, country_group, source='bloomberg') une = self.hist_econ_data_factory.grasp_coded_entry(une, -1) from pythalesians.graphics.graphs.plotfactory import PlotFactory from pythalesians.graphics.graphs.graphproperties import GraphProperties gp = GraphProperties() pf = PlotFactory() gp.plotly_location_mode = 'USA-states' gp.plotly_choropleth_field = 'Val' gp.plotly_scope = 'usa' gp.plotly_projection = 'albers usa' gp.plotly_world_readable = False gp.plotly_url = country_group + "-unemployment" gp.title = "USA Unemployment" gp.units = 'pc' pf.plot_generic_graph(une, type='choropleth', adapter='plotly', gp=gp)
def g10_line_plot_gdp(self, start_date, finish_date): today_root = datetime.date.today().strftime("%Y%m%d") + " " country_group = 'g10-ez' gdp = self.get_GDP_QoQ(start_date, finish_date, country_group) from pythalesians.graphics.graphs.plotfactory import PlotFactory from pythalesians.graphics.graphs.graphproperties import GraphProperties gp = GraphProperties() pf = PlotFactory() gp.title = "G10 GDP" gp.units = 'Rebased' gp.scale_factor = Constants.plotfactory_scale_factor gp.file_output = today_root + 'G10 UNE ' + str( gp.scale_factor) + '.png' gdp.columns = [x.split('-')[0] for x in gdp.columns] gp.linewidth_2 = 3 gp.linewidth_2_series = ['United Kingdom'] from pythalesians.timeseries.calcs.timeseriescalcs import TimeSeriesCalcs tsc = TimeSeriesCalcs() gdp = gdp / 100 gdp = tsc.create_mult_index_from_prices(gdp) pf.plot_generic_graph(gdp, type='line', adapter='pythalesians', gp=gp)
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 g10_plot_gdp_cpi_une(self, start_date, finish_date, data_type='cpi'): country_group = 'g10' if data_type == 'cpi': df = self.get_CPI_YoY(start_date, finish_date, country_group) elif data_type == 'gdp': df = self.get_GDP_QoQ(start_date, finish_date, country_group) elif data_type == 'une': df = self.get_UNE(start_date, finish_date, country_group) df = self.hist_econ_data_factory.grasp_coded_entry(df, -1) from pythalesians.graphics.graphs.plotfactory import PlotFactory from pythalesians.graphics.graphs.graphproperties import GraphProperties gp = GraphProperties() pf = PlotFactory() gp.plotly_location_mode = 'world' gp.plotly_choropleth_field = 'Val' gp.plotly_scope = 'world' gp.plotly_projection = 'Mercator' gp.plotly_world_readable = False gp.plotly_url = country_group + "-" + data_type gp.title = "G10 " + data_type gp.units = '%' pf.plot_generic_graph(df, type='choropleth', adapter='plotly', gp=gp)
def run_day_of_month_analysis(self, strat): from pythalesians.economics.seasonality.seasonality import Seasonality from pythalesians.timeseries.calcs.timeseriescalcs import TimeSeriesCalcs tsc = TimeSeriesCalcs() seas = Seasonality() strat.construct_strategy() pnl = strat.get_strategy_pnl() # get seasonality by day of the month pnl = pnl.resample('B') rets = tsc.calculate_returns(pnl) bus_day = seas.bus_day_of_month_seasonality(rets, add_average=True) # get seasonality by month pnl = pnl.resample('BM') rets = tsc.calculate_returns(pnl) month = seas.monthly_seasonality(rets) self.logger.info("About to plot seasonality...") gp = GraphProperties() pf = PlotFactory() # Plotting spot over day of month/month of year gp.color = 'Blues' gp.scale_factor = self.scale_factor gp.file_output = self.DUMP_PATH + strat.FINAL_STRATEGY + ' seasonality day of month.png' gp.title = strat.FINAL_STRATEGY + ' day of month seasonality' gp.display_legend = False gp.color_2_series = [bus_day.columns[-1]] gp.color_2 = ['red'] # red, pink gp.linewidth_2 = 4 gp.linewidth_2_series = [bus_day.columns[-1]] gp.y_axis_2_series = [bus_day.columns[-1]] pf.plot_line_graph(bus_day, adapter='pythalesians', gp=gp) gp = GraphProperties() gp.scale_factor = self.scale_factor gp.file_output = self.DUMP_PATH + strat.FINAL_STRATEGY + ' seasonality month of year.png' gp.title = strat.FINAL_STRATEGY + ' month of year seasonality' pf.plot_line_graph(month, adapter='pythalesians', gp=gp) return month
def run_day_of_month_analysis(self, strat): from pythalesians.economics.seasonality.seasonality import Seasonality from pythalesians.timeseries.calcs.timeseriescalcs import TimeSeriesCalcs tsc = TimeSeriesCalcs() seas = Seasonality() strat.construct_strategy() pnl = strat.get_strategy_pnl() # get seasonality by day of the month pnl = pnl.resample('B').mean() rets = tsc.calculate_returns(pnl) bus_day = seas.bus_day_of_month_seasonality(rets, add_average = True) # get seasonality by month pnl = pnl.resample('BM').mean() rets = tsc.calculate_returns(pnl) month = seas.monthly_seasonality(rets) self.logger.info("About to plot seasonality...") gp = GraphProperties() pf = PlotFactory() # Plotting spot over day of month/month of year gp.color = 'Blues' gp.scale_factor = self.scale_factor gp.file_output = self.DUMP_PATH + strat.FINAL_STRATEGY + ' seasonality day of month.png' gp.title = strat.FINAL_STRATEGY + ' day of month seasonality' gp.display_legend = False gp.color_2_series = [bus_day.columns[-1]] gp.color_2 = ['red'] # red, pink gp.linewidth_2 = 4 gp.linewidth_2_series = [bus_day.columns[-1]] gp.y_axis_2_series = [bus_day.columns[-1]] pf.plot_line_graph(bus_day, adapter = 'pythalesians', gp = gp) gp = GraphProperties() gp.scale_factor = self.scale_factor gp.file_output = self.DUMP_PATH + strat.FINAL_STRATEGY + ' seasonality month of year.png' gp.title = strat.FINAL_STRATEGY + ' month of year seasonality' pf.plot_line_graph(month, adapter = 'pythalesians', gp = gp) return month
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_leverage(self): pf = PlotFactory() gp = GraphProperties() gp.title = self.FINAL_STRATEGY + ' Leverage' gp.display_legend = True gp.scale_factor = self.SCALE_FACTOR gp.file_output = self.DUMP_PATH + self.FINAL_STRATEGY + ' (Group Leverage).png' pf.plot_line_graph(self.reduce_plot(self._strategy_group_leverage), adapter = 'pythalesians', gp = gp)
def plot_strategy_pnl(self): 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 PnL).png' try: pf.plot_line_graph(self.reduce_plot(self._strategy_pnl), adapter = 'pythalesians', gp = gp) except: pass
def plot_individual_leverage(self): pf = PlotFactory() gp = GraphProperties() gp.title = self.FINAL_STRATEGY + ' Leverage' gp.display_legend = True gp.scale_factor = self.SCALE_FACTOR gp.file_output = self.DUMP_PATH + self.FINAL_STRATEGY + ' (Individual Leverage).png' try: pf.plot_line_graph(self.reduce_plot(self._individual_leverage), 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_strategy_group_pnl_trades(self): pf = PlotFactory() gp = GraphProperties() gp.title = self.FINAL_STRATEGY + " (bp)" gp.display_legend = True gp.scale_factor = self.SCALE_FACTOR gp.file_output = self.DUMP_PATH + self.FINAL_STRATEGY + ' (Individual Trade PnL).png' # zero when there isn't a trade exit strategy_pnl_trades = self._strategy_pnl_trades.fillna(0) * 100 * 100 try: pf.plot_line_graph(self.reduce_plot(strategy_pnl_trades), adapter = 'pythalesians', gp = gp) except: pass
def plot_strategy_group_benchmark_annualised_pnl(self, cols = None): # TODO - unfinished, needs checking! if cols is None: cols = self._strategy_group_benchmark_annualised_pnl.columns pf = PlotFactory() gp = GraphProperties() gp.title = self.FINAL_STRATEGY gp.display_legend = True gp.scale_factor = self.SCALE_FACTOR gp.color = ['red', 'blue', 'purple', 'gray', 'yellow', 'green', 'pink'] gp.file_output = self.DUMP_PATH + self.FINAL_STRATEGY + ' (Group Benchmark Annualised PnL).png' pf.plot_line_graph(self.reduce_plot(self._strategy_group_benchmark_annualised_pnl[cols]), 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_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 run_arbitrary_sensitivity(self, strat, parameter_list = None, parameter_names = None, pretty_portfolio_names = None, parameter_type = None): asset_df, spot_df, spot_df2, basket_dict = strat.fill_assets() port_list = None for i in range(0, len(parameter_list)): br = strat.fill_backtest_request() current_parameter = parameter_list[i] # for calculating P&L for k in current_parameter.keys(): setattr(br, k, current_parameter[k]) strat.br = br # for calculating signals signal_df = strat.construct_signal(spot_df, spot_df2, br.tech_params) cash_backtest = CashBacktest() self.logger.info("Calculating... " + pretty_portfolio_names[i]) cash_backtest.calculate_trading_PnL(br, asset_df, signal_df) stats = str(cash_backtest.get_portfolio_pnl_desc()[0]) port = cash_backtest.get_cumportfolio().resample('B') port.columns = [pretty_portfolio_names[i] + ' ' + stats] if port_list is None: port_list = port else: port_list = port_list.join(port) pf = PlotFactory() gp = GraphProperties() gp.color = 'Blues' gp.resample = 'B' gp.file_output = self.DUMP_PATH + strat.FINAL_STRATEGY + ' ' + parameter_type + '.png' gp.scale_factor = self.scale_factor gp.title = strat.FINAL_STRATEGY + ' ' + parameter_type pf.plot_line_graph(port_list, adapter = 'pythalesians', gp = gp)
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 g10_line_plot_une(self, start_date, finish_date): today_root = datetime.date.today().strftime("%Y%m%d") + " " country_group = 'g10-ez' une = self.get_UNE(start_date, finish_date, country_group) from pythalesians.graphics.graphs.plotfactory import PlotFactory from pythalesians.graphics.graphs.graphproperties import GraphProperties gp = GraphProperties() pf = PlotFactory() gp.title = "G10 Unemployment Rate (%)" gp.units = '%' gp.scale_factor = Constants.plotfactory_scale_factor gp.file_output = today_root + 'G10 UNE ' + str(gp.scale_factor) + '.png' une.columns = [x.split('-')[0] for x in une.columns] gp.linewidth_2 = 3 gp.linewidth_2_series = ['United States'] pf.plot_generic_graph(une, type = 'line', adapter = 'pythalesians', gp = gp)
def g10_line_plot_cpi(self, start_date, finish_date): today_root = datetime.date.today().strftime("%Y%m%d") + " " country_group = 'g10-ez' cpi = self.get_CPI_YoY(start_date, finish_date, country_group) from pythalesians.graphics.graphs.plotfactory import PlotFactory from pythalesians.graphics.graphs.graphproperties import GraphProperties gp = GraphProperties() pf = PlotFactory() gp.title = "G10 CPI YoY" gp.units = '%' gp.scale_factor = 3 gp.file_output = today_root + 'G10 CPI YoY ' + str(gp.scale_factor) + '.png' cpi.columns = [x.split('-')[0] for x in cpi.columns] gp.linewidth_2 = 3 gp.linewidth_2_series = ['United States'] pf.plot_generic_graph(cpi, type = 'line', adapter = 'pythalesians', gp = gp)
def g10_line_plot_cpi(self, start_date, finish_date): today_root = datetime.date.today().strftime("%Y%m%d") + " " country_group = 'g10-ez' cpi = self.get_CPI_YoY(start_date, finish_date, country_group) from pythalesians.graphics.graphs.plotfactory import PlotFactory from pythalesians.graphics.graphs.graphproperties import GraphProperties gp = GraphProperties() pf = PlotFactory() gp.title = "G10 CPI YoY" gp.units = '%' gp.scale_factor = 3 gp.file_output = today_root + 'G10 CPI YoY ' + str( gp.scale_factor) + '.png' cpi.columns = [x.split('-')[0] for x in cpi.columns] gp.linewidth_2 = 3 gp.linewidth_2_series = ['United States'] pf.plot_generic_graph(cpi, type='line', adapter='pythalesians', gp=gp)
def g10_line_plot_une(self, start_date, finish_date): today_root = datetime.date.today().strftime("%Y%m%d") + " " country_group = 'g10-ez' une = self.get_UNE(start_date, finish_date, country_group) from pythalesians.graphics.graphs.plotfactory import PlotFactory from pythalesians.graphics.graphs.graphproperties import GraphProperties gp = GraphProperties() pf = PlotFactory() gp.title = "G10 Unemployment Rate (%)" gp.units = '%' gp.scale_factor = Constants.plotfactory_scale_factor gp.file_output = today_root + 'G10 UNE ' + str( gp.scale_factor) + '.png' une.columns = [x.split('-')[0] for x in une.columns] gp.linewidth_2 = 3 gp.linewidth_2_series = ['United States'] pf.plot_generic_graph(une, type='line', adapter='pythalesians', gp=gp)
def europe_plot_une(self, start_date, finish_date): country_group = 'all-europe' une = self.get_UNE(start_date, finish_date, country_group) une = self.hist_econ_data_factory.grasp_coded_entry(une, -1) from pythalesians.graphics.graphs.plotfactory import PlotFactory from pythalesians.graphics.graphs.graphproperties import GraphProperties gp = GraphProperties() pf = PlotFactory() gp.plotly_location_mode = 'europe' gp.plotly_choropleth_field = 'Val' gp.plotly_scope = 'europe' gp.plotly_projection = 'Mercator' gp.plotly_world_readable = False gp.plotly_url = country_group + "-unemployment"; gp.title = "Europe Unemployment" gp.units = '%' pf.plot_generic_graph(une, type = 'choropleth', adapter = 'plotly', gp = gp)
def usa_plot_une(self, start_date, finish_date): country_group = 'usa-states'; source = 'bloomberg' une = self.get_UNE(start_date, finish_date, country_group, source = 'bloomberg') une = self.hist_econ_data_factory.grasp_coded_entry(une, -1) from pythalesians.graphics.graphs.plotfactory import PlotFactory from pythalesians.graphics.graphs.graphproperties import GraphProperties gp = GraphProperties() pf = PlotFactory() gp.plotly_location_mode = 'USA-states' gp.plotly_choropleth_field = 'Val' gp.plotly_scope = 'usa' gp.plotly_projection = 'albers usa' gp.plotly_world_readable = False gp.plotly_url = country_group + "-unemployment" gp.title = "USA Unemployment" gp.units = 'pc' pf.plot_generic_graph(une, type = 'choropleth', adapter = 'plotly', gp = gp)
def world_plot_cpi(self, start_date, finish_date): country_group = 'world-liquid' cpi = self.get_CPI_YoY(start_date, finish_date, country_group) cpi = self.hist_econ_data_factory.grasp_coded_entry(cpi, -1) from pythalesians.graphics.graphs.plotfactory import PlotFactory from pythalesians.graphics.graphs.graphproperties import GraphProperties gp = GraphProperties() pf = PlotFactory() gp.plotly_location_mode = 'world' gp.plotly_choropleth_field = 'Val' gp.plotly_scope = 'world' gp.plotly_projection = 'Mercator' gp.plotly_world_readable = False gp.plotly_url = str(country_group) + "-cpi" gp.title = "World Liquid CPI YoY" gp.units = '%' pf.plot_generic_graph(cpi, type = 'choropleth', adapter = 'plotly', gp = gp)
def g10_line_plot_gdp(self, start_date, finish_date): today_root = datetime.date.today().strftime("%Y%m%d") + " " country_group = 'g10-ez' gdp = self.get_GDP_QoQ(start_date, finish_date, country_group) from pythalesians.graphics.graphs.plotfactory import PlotFactory from pythalesians.graphics.graphs.graphproperties import GraphProperties gp = GraphProperties() pf = PlotFactory() gp.title = "G10 GDP" gp.units = 'Rebased' gp.scale_factor = Constants.plotfactory_scale_factor gp.file_output = today_root + 'G10 UNE ' + str(gp.scale_factor) + '.png' gdp.columns = [x.split('-')[0] for x in gdp.columns] gp.linewidth_2 = 3 gp.linewidth_2_series = ['United Kingdom'] from pythalesians.timeseries.calcs.timeseriescalcs import TimeSeriesCalcs tsc = TimeSeriesCalcs() gdp = gdp / 100 gdp = tsc.create_mult_index_from_prices(gdp) pf.plot_generic_graph(gdp, type = 'line', adapter = 'pythalesians', gp = gp)
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 world_plot_cpi(self, start_date, finish_date): country_group = 'world-liquid' cpi = self.get_CPI_YoY(start_date, finish_date, country_group) cpi = self.hist_econ_data_factory.grasp_coded_entry(cpi, -1) from pythalesians.graphics.graphs.plotfactory import PlotFactory from pythalesians.graphics.graphs.graphproperties import GraphProperties gp = GraphProperties() pf = PlotFactory() gp.plotly_location_mode = 'world' gp.plotly_choropleth_field = 'Val' gp.plotly_scope = 'world' gp.plotly_projection = 'Mercator' gp.plotly_world_readable = False gp.plotly_url = str(country_group) + "-cpi" gp.title = "World Liquid CPI YoY" gp.units = '%' pf.plot_generic_graph(cpi, type='choropleth', adapter='plotly', gp=gp)
def europe_plot_une(self, start_date, finish_date): country_group = 'all-europe' une = self.get_UNE(start_date, finish_date, country_group) une = self.hist_econ_data_factory.grasp_coded_entry(une, -1) from pythalesians.graphics.graphs.plotfactory import PlotFactory from pythalesians.graphics.graphs.graphproperties import GraphProperties gp = GraphProperties() pf = PlotFactory() gp.plotly_location_mode = 'europe' gp.plotly_choropleth_field = 'Val' gp.plotly_scope = 'europe' gp.plotly_projection = 'Mercator' gp.plotly_world_readable = False gp.plotly_url = country_group + "-unemployment" gp.title = "Europe Unemployment" gp.units = '%' pf.plot_generic_graph(une, type='choropleth', adapter='plotly', 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): 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_signals(self, date = None, strip = None): ######## plot signals strategy_signal = self._strategy_signal strategy_signal = 100 * (strategy_signal) if date is None: last_day = strategy_signal.ix[-1].transpose().to_frame() else: last_day = strategy_signal.ix[date].transpose().to_frame() if strip is not None: last_day.index = [x.replace(strip, '') for x in last_day.index] print(last_day) pf = PlotFactory() gp = GraphProperties() gp.title = self.FINAL_STRATEGY + " positions (% portfolio notional)" gp.scale_factor = self.SCALE_FACTOR gp.file_output = self.DUMP_PATH + self.FINAL_STRATEGY + ' (Positions) ' + str(gp.scale_factor) + '.png' pf.plot_generic_graph(last_day, adapter = 'pythalesians', type = 'bar', gp = gp)
fields = ['close'], # which fields to download vendor_tickers = vendor_tickers, vendor_fields = ['close'], # which Bloomberg fields to download cache_algo = 'internet_load_return') # how to return data df = ltsf.harvest_time_series(time_series_request) df.columns = [x.replace('.close', '') for x in df.columns.values] # Bloomberg does not give the milisecond field when you make a tick request, so might as well downsample to S df['JPYUSD'] = 1 / df['JPYUSD'] gp = GraphProperties() pf = PlotFactory() gp.scale_factor = 3 gp.title = 'FX around last NFP date' gp.source = 'Thalesians/BBG (created with PyThalesians Python library)' tsc = TimeSeriesCalcs() df = tsc.create_mult_index_from_prices(df) pf.plot_line_graph(df, adapter = 'pythalesians', gp = gp) ###### download tick data from Bloomberg for EUR/USD around last FOMC and then downsample to plot if True: finish_date = datetime.datetime.utcnow() start_date = finish_date - timedelta(days=60) # fetch Fed times from Bloomberg time_series_request = TimeSeriesRequest( start_date = start_date, # start date
df = ltsf.harvest_time_series(time_series_request) df.columns = [x.replace('.close', '') for x in df.columns.values] df = tsc.calculate_returns(df) * 100 df = df.dropna() df_sorted = tsc.get_bottom_valued_sorted(df, "USDBRL", n = 20) # df = tsc.get_top_valued_sorted(df, "USDBRL", n = 20) # get biggest up moves # get values on day after df2 = df.shift(-1) df2 = df2.ix[df_sorted.index] df2.columns = ['T+1'] df_sorted.columns = ['T'] df_sorted = df_sorted.join(df2) df_sorted.index = [str(x.year) + '/' + str(x.month) + '/' + str(x.day) for x in df_sorted.index] gp = GraphProperties() gp.title = 'Largest daily falls in USDBRL' gp.scale_factor = 3 gp.display_legend = True gp.chart_type = 'bar' gp.x_title = 'Dates' gp.y_title = 'Pc' gp.file_output = "usdbrl-biggest-downmoves.png" pf = PlotFactory() pf.plot_line_graph(df_sorted, adapter = 'pythalesians', gp=gp)
tenor = 'ON' # plot total return series comparison for all our crosses # in practice, we would typically make a set of xxxUSD total return indices # and use them to compute all other crosses (assuming we are USD denominated investor) for cross in ['AUDUSD', 'EURUSD', 'GBPUSD']: # create total return index using spot + deposits ind = IndicesFX() ind_df = ind.create_total_return_index(cross, tenor, spot_df, deposit_df) ind_df.columns = [x + '.PYT (with carry)' for x in ind_df.columns] # grab total return index which we downloaded from Bloomberg bbg_ind_df = tot_df[cross + '.close'].to_frame() bbg_ind_df.columns = [x + ".BBG (with carry)" for x in bbg_ind_df.columns] # grab spot data spot_plot_df = spot_df[cross + '.close'].to_frame() spot_plot_df = tsc.create_mult_index_from_prices(spot_plot_df) # combine total return indices (computed by PyThalesians), those from Bloomberg and also spot # with everything already rebased at 100 ind_df = ind_df.join(bbg_ind_df) ind_df = ind_df.join(spot_plot_df) gp = GraphProperties() gp.title = 'Total return indices in FX & comparing with spot' gp.scale_factor = 3 pf = PlotFactory() pf.plot_line_graph(ind_df, adapter = 'pythalesians', gp = gp)
finish_date = datetime.date.today(), # finish date freq = 'daily', # daily data 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) pf = PlotFactory() gp = GraphProperties() gp.title = 'Spot values' gp.file_output = 'demo.png' gp.html_file_output = 'demo.htm' gp.source = 'Thalesians/BBG' # plot using PyThalesians pf.plot_line_graph(daily_vals, adapter = 'pythalesians', gp = gp) # plot using Bokeh (still needs a lot of work!) pf.plot_line_graph(daily_vals, adapter = 'bokeh', gp = gp) # do more complicated charts using several different Matplotib stylesheets (which have been customised) if True: ltsf = LightTimeSeriesFactory() # load market data
df_fred = ltsf.harvest_time_series(time_series_request) df_fred.columns = [ x.replace('.close', '') for x in df_fred.columns.values ] # convert to USD bn # df_fred = (df_fred * 10000000) df = df.join(df_fred, how="outer") df['USDJPY'] = df['USDJPY'].ffill() # data is in 100 million JPY, divide by 10 to get into 1000 million (ie. 1 billion) # divide by USD/JPY spot to get into USD df['USDJPY purchases (bn USD)'] = (df['USDJPY purchases (bn USD)'] / df['USDJPY']) / 10 gp = GraphProperties() gp.scale_factor = 3 gp.title = "BoJ USDJPY buying" gp.file_output = "output_data/" + datetime.date.today().strftime("%Y%m%d") + " USDJPY BoJ intervention " \ + str(gp.scale_factor) + ".png" gp.source = 'Thalesians/BBG (created with PyThalesians Python library)' gp.y_axis_2_series = ['USDJPY purchases (bn USD)'] gp.color_2_series = gp.y_axis_2_series gp.color_2 = ['blue'] pf = PlotFactory() pf.plot_line_graph(df, adapter='pythalesians', gp=gp)
freq="daily", # daily data 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) pf = PlotFactory() gp = GraphProperties() gp.title = "Spot values" gp.file_output = "output_data/demo.png" gp.html_file_output = "output_data/demo.htm" gp.source = "Thalesians/BBG" # plot using PyThalesians pf.plot_line_graph(daily_vals, adapter="pythalesians", gp=gp) # plot using Bokeh (still needs a lot of work!) pf.plot_line_graph(daily_vals, adapter="bokeh", gp=gp) # do more complicated charts using several different Matplotib stylesheets (which have been customised) if False: ltsf = LightTimeSeriesFactory() # load market data
df = tsc.create_mult_index_from_prices(daily_vals) df = df.fillna(method='ffill') # Lastly we can plot the results! We shall plot it with the PlotFactory class using PyThalesians engine (a wrapper on top of matplotlib). We can specify the properties of the plot using a GraphProperties class. The first line, forces matplotlib to give us the plot in the notebook, as opposed to a separate window. # In[ ]: get_ipython().magic(u'matplotlib inline') from pythalesians.graphics.graphs.plotfactory import PlotFactory from pythalesians.graphics.graphs.graphproperties import GraphProperties gp = GraphProperties() gp.source = 'Thalesians/BBG (created with PyThalesians Python library)' gp.title = 'Equity Markets' gp.scale_factor = 1 gp.display_legend = True gp.x_title = 'Dates' gp.y_title = 'Index' pf = PlotFactory() pf.plot_line_graph(df, adapter = 'pythalesians', gp = gp) # In[ ]:
freq = 'daily', # daily data data_source = 'google', # use Bloomberg as data source tickers = ['Apple', 'S&P500 ETF'], # ticker (Thalesians) fields = ['close'], # which fields to download vendor_tickers = ['aapl', 'spy'], # ticker (Google) vendor_fields = ['Close'], # which Bloomberg fields to download cache_algo = 'internet_load_return') # how to return data ltsf = LightTimeSeriesFactory() tsc = TimeSeriesCalcs() df = tsc.create_mult_index_from_prices(ltsf.harvest_time_series(time_series_request)) gp = GraphProperties() gp.html_file_output = "output_data/apple.htm" gp.title = "S&P500 vs Apple" # plot first with PyThalesians and then Bokeh # just needs 1 word to change gp.display_legend = False pf = PlotFactory() pf.plot_generic_graph(df, type = 'line', adapter = 'pythalesians', gp = gp) pf.plot_generic_graph(df, type = 'line', adapter = 'bokeh', gp = gp) # test simple Bokeh bar charts - monthly returns over past 6 months if True: from datetime import timedelta ltsf = LightTimeSeriesFactory() end = datetime.datetime.utcnow()
data_source='google', # use Bloomberg as data source tickers=['Apple', 'S&P500 ETF'], # ticker (Thalesians) fields=['close'], # which fields to download vendor_tickers=['aapl', 'spy'], # ticker (Google) vendor_fields=['Close'], # which Bloomberg fields to download cache_algo='internet_load_return') # how to return data ltsf = LightTimeSeriesFactory() tsc = TimeSeriesCalcs() df = tsc.create_mult_index_from_prices( ltsf.harvest_time_series(time_series_request)) gp = GraphProperties() gp.html_file_output = "output_data/apple.htm" gp.title = "S&P500 vs Apple" # plot first with PyThalesians and then Bokeh # just needs 1 word to change gp.display_legend = False pf = PlotFactory() pf.plot_generic_graph(df, type='line', adapter='pythalesians', gp=gp) pf.plot_generic_graph(df, type='line', adapter='bokeh', gp=gp) # test simple Bokeh bar charts - monthly returns over past 6 months if True: from datetime import timedelta ltsf = LightTimeSeriesFactory() end = datetime.datetime.utcnow()
finish_date = datetime.date.today(), # finish date freq = 'daily', # daily data data_source = 'google', # use Bloomberg as data source tickers = ['Apple', 'S&P500 ETF'], # ticker (Thalesians) fields = ['close'], # which fields to download vendor_tickers = ['aapl', 'spy'], # ticker (Google) vendor_fields = ['Close'], # which Bloomberg fields to download cache_algo = 'internet_load_return') # how to return data ltsf = LightTimeSeriesFactory() tsc = TimeSeriesCalcs() df = tsc.create_mult_index_from_prices(ltsf.harvest_time_series(time_series_request)) gp = GraphProperties() gp.title = "S&P500 vs Apple" # plot first with PyThalesians and then Plotly (via Cufflinks) # just needs 1 word to change # (although, note that AdapterCufflinks does have some extra parameters that can be set in # GraphProperties) gp.plotly_username = '******' gp.plotly_world_readable = True pf = PlotFactory() pf.plot_generic_graph(df, type = 'line', adapter = 'pythalesians', gp = gp) pf.plot_generic_graph(df, type = 'line', adapter = 'cufflinks', gp = gp) # test simple Plotly bar charts - average differences in EURUSDV1M-1Y vol and USDJPYV1M-1Y slope over past sixth months if True: from datetime import timedelta
finish_date=datetime.date.today(), # finish date freq='daily', # daily data 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) pf = PlotFactory() gp = GraphProperties() gp.title = 'Spot values' gp.file_output = 'output_data/demo.png' gp.html_file_output = 'output_data/demo.htm' gp.source = 'Thalesians/BBG' # plot using PyThalesians pf.plot_line_graph(daily_vals, adapter='pythalesians', gp=gp) # plot using Bokeh (still needs a lot of work!) pf.plot_line_graph(daily_vals, adapter='bokeh', gp=gp) # do more complicated charts using several different Matplotib stylesheets (which have been customised) if False: ltsf = LightTimeSeriesFactory() # load market data
data_source="google", # use Bloomberg as data source tickers=["Apple", "S&P500 ETF"], # ticker (Thalesians) fields=["close"], # which fields to download vendor_tickers=["aapl", "spy"], # ticker (Google) vendor_fields=["Close"], # which Bloomberg fields to download cache_algo="internet_load_return", ) # how to return data ltsf = LightTimeSeriesFactory() tsc = TimeSeriesCalcs() df = tsc.create_mult_index_from_prices(ltsf.harvest_time_series(time_series_request)) gp = GraphProperties() gp.html_file_output = "output_data/apple.htm" gp.title = "S&P500 vs Apple" # plot first with PyThalesians and then Bokeh # just needs 1 word to change gp.display_legend = False pf = PlotFactory() pf.plot_generic_graph(df, type="line", adapter="pythalesians", gp=gp) pf.plot_generic_graph(df, type="line", adapter="bokeh", gp=gp) # test simple Bokeh bar charts - monthly returns over past 6 months if True: from datetime import timedelta ltsf = LightTimeSeriesFactory()
start_date="01 Jan 1970", # start date finish_date=datetime.date.today(), # finish date freq='daily', # daily data data_source='quandl', # use Quandl as data source tickers=[ 'EURUSD', # ticker (Thalesians) 'GBPUSD' ], fields=['close'], # which fields to download vendor_tickers=['FRED/DEXUSEU', 'FRED/DEXUSUK'], # ticker (Quandl) vendor_fields=['close'], # which Bloomberg fields to download cache_algo='internet_load_return') # how to return data ltsf = LightTimeSeriesFactory() df = ltsf.harvest_time_series(time_series_request) df_ret = tsc.calculate_returns(df) day_of_month_seasonality = seasonality.bus_day_of_month_seasonality( df_ret, partition_by_month=False) day_of_month_seasonality = tsc.convert_month_day_to_date_time( day_of_month_seasonality) gp = GraphProperties() gp.date_formatter = '%b' gp.title = 'FX spot moves by day of month' gp.scale_factor = 3 gp.file_output = "output_data/FX spot DOM seasonality.png" pf.plot_line_graph(day_of_month_seasonality, adapter='pythalesians', gp=gp)
freq='daily', # daily data data_source='google', # use Bloomberg as data source tickers=['Apple', 'S&P500 ETF'], # ticker (Thalesians) fields=['close'], # which fields to download vendor_tickers=['aapl', 'spy'], # ticker (Google) vendor_fields=['Close'], # which Bloomberg fields to download cache_algo='internet_load_return') # how to return data ltsf = LightTimeSeriesFactory() tsc = TimeSeriesCalcs() df = tsc.create_mult_index_from_prices( ltsf.harvest_time_series(time_series_request)) gp = GraphProperties() gp.title = "S&P500 vs Apple" # plot first with PyThalesians and then Plotly (via Cufflinks) # just needs 1 word to change # (although, note that AdapterCufflinks does have some extra parameters that can be set in # GraphProperties) gp.plotly_username = '******' gp.plotly_world_readable = True pf = PlotFactory() pf.plot_generic_graph(df, type='line', adapter='pythalesians', gp=gp) pf.plot_generic_graph(df, type='line', adapter='cufflinks', gp=gp) # test simple Plotly bar charts - average differences in EURUSDV1M-1Y vol and USDJPYV1M-1Y slope over past sixth months if True: from datetime import timedelta
end = datetime.datetime.utcnow() start_date = end.replace(hour=0, minute=0, second=0, microsecond=0) # Returns a copy time_series_request = TimeSeriesRequest( start_date = start_date, # start date finish_date = datetime.datetime.utcnow(), # finish date freq = 'intraday', # intraday data data_source = 'bloomberg', # use Bloomberg as data source tickers = ['EURUSD'] , # ticker (Thalesians) fields = ['close'], # which fields to download vendor_tickers = ['EURUSD BGN Curncy'], # ticker (Bloomberg) vendor_fields = ['close'], # which Bloomberg fields to download cache_algo = 'internet_load_return') # how to return data ltsf = LightTimeSeriesFactory() df = ltsf.harvest_time_series(time_series_request) df.columns = [x.replace('.close', '') for x in df.columns.values] gp = GraphProperties() gp.title = 'EURUSD stuff!' gp.file_output = 'EURUSD.png' gp.source = 'Thalesians/BBG (created with PyThalesians Python library)' pf = PlotFactory() pf.plot_line_graph(df, adapter = 'pythalesians', gp = gp) pytwitter.update_status("check out my plot of EUR/USD!", picture = gp.file_output)
cache_algo='internet_load_return') # how to return data ltsf = LightTimeSeriesFactory() df = ltsf.harvest_time_series(time_series_request) df_ret = tsc.calculate_returns(df) day_of_month_seasonality = seasonality.bus_day_of_month_seasonality( df_ret, partition_by_month=False) day_of_month_seasonality = tsc.convert_month_day_to_date_time( day_of_month_seasonality) gp = GraphProperties() gp.date_formatter = '%b' gp.title = 'S&P500 seasonality' gp.scale_factor = 3 gp.file_output = "output_data/S&P500 DOM seasonality.png" pf.plot_line_graph(day_of_month_seasonality, adapter='pythalesians', gp=gp) ###### calculate seasonal moves in EUR/USD (using Quandl data) if True: time_series_request = TimeSeriesRequest( start_date="01 Jan 1970", # start date finish_date=datetime.date.today(), # finish date freq='daily', # daily data data_source='quandl', # use Quandl as data source tickers=[ 'EURUSD', # ticker (Thalesians) 'GBPUSD'
def run_arbitrary_sensitivity(self, strat, parameter_list=None, parameter_names=None, pretty_portfolio_names=None, parameter_type=None): asset_df, spot_df, spot_df2, basket_dict = strat.fill_assets() port_list = None tsd_list = [] for i in range(0, len(parameter_list)): br = strat.fill_backtest_request() current_parameter = parameter_list[i] # for calculating P&L for k in current_parameter.keys(): setattr(br, k, current_parameter[k]) strat.br = br # for calculating signals signal_df = strat.construct_signal(spot_df, spot_df2, br.tech_params, br) cash_backtest = CashBacktest() self.logger.info("Calculating... " + pretty_portfolio_names[i]) cash_backtest.calculate_trading_PnL(br, asset_df, signal_df) tsd_list.append(cash_backtest.get_portfolio_pnl_tsd()) stats = str(cash_backtest.get_portfolio_pnl_desc()[0]) port = cash_backtest.get_cumportfolio().resample('B') port.columns = [pretty_portfolio_names[i] + ' ' + stats] if port_list is None: port_list = port else: port_list = port_list.join(port) # reset the parameters of the strategy strat.br = strat.fill_backtest_request() pf = PlotFactory() gp = GraphProperties() ir = [t.inforatio()[0] for t in tsd_list] # gp.color = 'Blues' # plot all the variations gp.resample = 'B' gp.file_output = self.DUMP_PATH + strat.FINAL_STRATEGY + ' ' + parameter_type + '.png' gp.scale_factor = self.scale_factor gp.title = strat.FINAL_STRATEGY + ' ' + parameter_type pf.plot_line_graph(port_list, adapter='pythalesians', gp=gp) # plot all the IR in a bar chart form (can be easier to read!) gp = GraphProperties() gp.file_output = self.DUMP_PATH + strat.FINAL_STRATEGY + ' ' + parameter_type + ' IR.png' gp.scale_factor = self.scale_factor gp.title = strat.FINAL_STRATEGY + ' ' + parameter_type summary = pandas.DataFrame(index=pretty_portfolio_names, data=ir, columns=['IR']) pf.plot_bar_graph(summary, adapter='pythalesians', gp=gp) return port_list
'GBPUSD BGN Curncy', 'AUDUSD BGN Curncy'], vendor_fields = ['PX_LAST'], # which Bloomberg fields to download cache_algo = 'internet_load_return') # how to return data ltsf = LightTimeSeriesFactory() df = None df = ltsf.harvest_time_series(time_series_request) tsc = TimeSeriesCalcs() df = tsc.calculate_returns(df) df = tsc.rolling_corr(df['EURUSD.close'], 20, data_frame2 = df[['GBPUSD.close', 'AUDUSD.close']]) gp = GraphProperties() gp.title = "1M FX rolling correlations" gp.scale_factor = 3 pf = PlotFactory() pf.plot_line_graph(df, adapter = 'pythalesians', gp = gp) ###### download daily data from Bloomberg for AUD/JPY, NZD/JPY spot with S&P500, then calculate correlation if True: time_series_request = TimeSeriesRequest( start_date="01 Jan 2015", # start date finish_date=datetime.date.today(), # finish date freq='daily', # daily data data_source='bloomberg', # use Bloomberg as data source tickers=['AUDJPY', # ticker (Thalesians) 'NZDJPY', 'S&P500'],
###### calculate seasonal moves in EUR/USD and GBP/USD (using Quandl data) if True: time_series_request = TimeSeriesRequest( start_date = "01 Jan 1970", # start date finish_date = datetime.date.today(), # finish date freq = 'daily', # daily data data_source = 'quandl', # use Quandl as data source tickers = ['EURUSD', # ticker (Thalesians) 'GBPUSD'], fields = ['close'], # which fields to download vendor_tickers = ['FRED/DEXUSEU', 'FRED/DEXUSUK'], # ticker (Quandl) vendor_fields = ['close'], # which Bloomberg fields to download cache_algo = 'internet_load_return') # how to return data ltsf = LightTimeSeriesFactory() df = ltsf.harvest_time_series(time_series_request) df_ret = tsc.calculate_returns(df) day_of_month_seasonality = seasonality.bus_day_of_month_seasonality(df_ret, partition_by_month = False) day_of_month_seasonality = tsc.convert_month_day_to_date_time(day_of_month_seasonality) gp = GraphProperties() gp.date_formatter = '%b' gp.title = 'FX spot moves by time of year' gp.scale_factor = 3 gp.file_output = "output_data/20150724 FX spot seas.png" pf.plot_line_graph(day_of_month_seasonality, adapter='pythalesians', gp = gp)