def plot_strategy_group_leverage(self): pf = PlotFactory() gp = GraphProperties() gp = self.create_graph_properties("Leverage", "Group Leverage") pf.plot_line_graph(self.reduce_plot(self._strategy_group_leverage), adapter = self.DEFAULT_PLOT_ENGINE, gp = gp)
def plot_strategy_pnl(self): pf = PlotFactory() gp = self.create_graph_properties("", "Strategy PnL") try: pf.plot_line_graph(self.reduce_plot(self._strategy_pnl), adapter = self.DEFAULT_PLOT_ENGINE, gp = gp) except: pass
def plot_individual_leverage(self): pf = PlotFactory() gp = self.create_graph_properties("Leverage", "Individual Leverage") try: pf.plot_line_graph(self.reduce_plot(self._individual_leverage), adapter = self.DEFAULT_PLOT_ENGINE, 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 = self.create_graph_properties("", "Group Benchmark Annualised PnL") gp.color = ['red', 'blue', 'purple', 'gray', 'yellow', 'green', 'pink'] pf.plot_line_graph(self.reduce_plot(self._strategy_group_benchmark_annualised_pnl[cols]), adapter = self.DEFAULT_PLOT_ENGINE, gp = gp)
def plot_strategy_group_pnl_trades(self): pf = PlotFactory() gp = self.create_graph_properties("(bp)", "Individual Trade PnL") # zero when there isn't a trade exit # strategy_pnl_trades = self._strategy_pnl_trades * 100 * 100 # strategy_pnl_trades = strategy_pnl_trades.dropna() # note only works with single large basket trade try: strategy_pnl_trades = self._strategy_pnl_trades.fillna(0) * 100 * 100 pf.plot_line_graph(self.reduce_plot(strategy_pnl_trades), adapter = self.DEFAULT_PLOT_ENGINE, gp = gp) except: pass
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] pf = PlotFactory() gp = self.create_graph_properties("positions (% portfolio notional)", "Positions") pf.plot_generic_graph(last_day, adapter = self.DEFAULT_PLOT_ENGINE, type = 'bar', gp = gp)
def plot_strategy_group_benchmark_pnl(self, strip = None): pf = PlotFactory() gp = self.create_graph_properties("", "Group Benchmark PnL - cumulative") 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 = self.DEFAULT_PLOT_ENGINE, 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) ' + gp.SCALE_FACTOR + '.png' gp.file_output = self.DUMP_PATH + self.FINAL_STRATEGY + ' (Group Benchmark PnL - IR) ' + gp.SCALE_FACTOR + '.html' gp.display_brand_label = False # plot ret stats pf.plot_bar_graph(ret_stats, adapter = self.DEFAULT_PLOT_ENGINE, gp = gp) except: pass
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.html_file_output = self.DUMP_PATH + strat.FINAL_STRATEGY + ' seasonality day of month.html' 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 = self.DEFAULT_PLOT_ENGINE, 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.html_file_output = self.DUMP_PATH + strat.FINAL_STRATEGY + ' seasonality month of year.html' gp.title = strat.FINAL_STRATEGY + ' month of year seasonality' pf.plot_line_graph(month, adapter = self.DEFAULT_PLOT_ENGINE, gp = gp) return month
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 = self.create_graph_properties("", "") try: gp.file_output = self.DUMP_PATH + self.FINAL_STRATEGY + ' (Strategy signal proportion).png' gp.html_file_output = self.DUMP_PATH + self.FINAL_STRATEGY + ' (Strategy signal proportion).html' pf.plot_bar_graph(self.reduce_plot(df), adapter = self.DEFAULT_PLOT_ENGINE, gp = gp) gp.file_output = self.DUMP_PATH + self.FINAL_STRATEGY + ' (Strategy trade no).png' gp.html_file_output = self.DUMP_PATH + self.FINAL_STRATEGY + ' (Strategy trade no).html' pf.plot_bar_graph(self.reduce_plot(df_trades), adapter = self.DEFAULT_PLOT_ENGINE, gp = gp) except: pass
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)
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() daily_vals = ltsf.harvest_time_series(time_series_request) techind = TechIndicator() tech_params = TechParams() tech_params.sma_period = 20 techind.create_tech_ind(daily_vals, 'SMA', tech_params=tech_params) sma = techind.get_techind() signal = techind.get_signal() combine = daily_vals.join(sma, how='outer') pf = PlotFactory() pf.plot_line_graph(combine, adapter='pythalesians')
if True: import pandas df = pandas.read_csv( "volsurface.csv") # load a snapshot for a vol surface from disk gp = GraphProperties() gp.plotly_plot_mode = "offline_html" # render Plotly plot locally (rather than via website) gp.file_output = "volsurface.png" # save as static PNG file gp.html_file_output = "volsurface.html" # save as interactive HTML file gp.title = "GBP/USD vol surface" gp.color = 'Blues' # plot surface with Plotly pf = PlotFactory() pf.plot_generic_graph(df, type='surface', adapter='cufflinks', gp=gp) if True: time_series_request = TimeSeriesRequest( start_date="01 Jan 2013", # start date 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
if True: import pandas df = pandas.read_csv("volsurface.csv") # load a snapshot for a vol surface from disk gp = GraphProperties() gp.plotly_plot_mode = "offline_html" # render Plotly plot locally (rather than via website) gp.file_output = "volsurface.png" # save as static PNG file gp.html_file_output = "volsurface.html" # save as interactive HTML file gp.title = "GBP/USD vol surface" gp.color = 'Blues' # plot surface with Plotly pf = PlotFactory() pf.plot_generic_graph(df, type = 'surface', adapter = 'cufflinks', gp = gp) if True: time_series_request = TimeSeriesRequest( start_date = "01 Jan 2013", # start date 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
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... " + str(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').mean() port.columns = [str(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] # if we have too many combinations remove legend and use scaled shaded colour # if len(port_list) > 10: # gp.color = 'Blues' # gp.display_legend = False # plot all the variations gp.resample = 'B' gp.file_output = self.DUMP_PATH + strat.FINAL_STRATEGY + ' ' + parameter_type + '.png' gp.html_file_output = self.DUMP_PATH + strat.FINAL_STRATEGY + ' ' + parameter_type + '.html' gp.scale_factor = self.SCALE_FACTOR gp.title = strat.FINAL_STRATEGY + ' ' + parameter_type pf.plot_line_graph(port_list, adapter = self.DEFAULT_PLOT_ENGINE, 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.html_file_output = self.DUMP_PATH + strat.FINAL_STRATEGY + ' ' + parameter_type + ' IR.html' 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 = self.DEFAULT_PLOT_ENGINE, gp = gp) return port_list
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() daily_vals = ltsf.harvest_time_series(time_series_request) techind = TechIndicator() tech_params = TechParams() tech_params.sma_period = 20 techind.create_tech_ind(daily_vals, 'SMA', tech_params=tech_params) sma = techind.get_techind() signal = techind.get_signal() combine = daily_vals.join(sma, how='outer') pf = PlotFactory() pf.plot_line_graph(combine, adapter='pythalesians')
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)
start_date = "01 Jan 2014", # start date finish_date = datetime.date.today(), # finish date freq = 'daily', # daily data data_source = 'quandl', # use quandl as data source tickers = ['S&P500'], # ticker (Thalesians) fields = ['close', 'open', 'adjusted-close', 'high'], # which fields to download vendor_tickers = ['YAHOO/INDEX_GSPC'], # ticker (quandl) vendor_fields = ['close', 'open', 'adjusted-close', 'high'], # which quandl fields to download cache_algo = 'internet_load_return') # how to return data ltsf = LightTimeSeriesFactory() df = None df = ltsf.harvest_time_series(time_series_request) pf = PlotFactory() pf.plot_line_graph(df, adapter = 'pythalesians') ###### download monthly quandl data for Total US nonfarm payrolls if True: time_series_request = TimeSeriesRequest( start_date="01 Jan 1940", # start date finish_date=datetime.date.today(), # finish date freq='daily', # daily data data_source='quandl', # use quandl as data source tickers=['US Total Nonfarm Payrolls'], # ticker (Thalesians) fields=['close'], # which fields to download vendor_tickers=['FRED/PAYEMS'], # ticker (quandl) vendor_fields=['close'], # which quandl fields to download cache_algo='internet_load_return') # how to return data
df_event_times.index = df_event_times.index.tz_localize(utc_time) # work in UTC time from pythalesians.economics.events.eventstudy import EventStudy es = EventStudy() # work out cumulative asset price moves moves over the event df_event = es.get_intraday_moves_over_custom_event(df, df_event_times) # create an average move df_event['Avg'] = df_event.mean(axis = 1) # plotting spot over economic data event gp = GraphProperties() gp.scale_factor = 3 gp.title = 'USDJPY spot moves over recent NFP' # plot in shades of blue (so earlier releases are lighter, later releases are darker) gp.color = 'Blues'; gp.color_2 = [] gp.y_axis_2_series = [] gp.display_legend = False # last release will be in red, average move in orange gp.color_2_series = [df_event.columns[-2], df_event.columns[-1]] gp.color_2 = ['red', 'orange'] # red, pink gp.linewidth_2 = 2 gp.linewidth_2_series = gp.color_2_series pf = PlotFactory() pf.plot_line_graph(df_event * 100, adapter = 'pythalesians', gp = gp)
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() start = end - timedelta(days=180) tickers = ['S&P500', 'FTSE', 'Nikkei'] vendor_tickers = ['SPX Index', 'UKX Index', 'NKY Index'] time_series_request = TimeSeriesRequest(
import datetime from chartesians.graphs.plotfactory import PlotFactory from pythalesians.economics.seasonality.seasonality import Seasonality from pythalesians.market.loaders.lighttimeseriesfactory import LightTimeSeriesFactory from pythalesians.market.requests.timeseriesrequest import TimeSeriesRequest from pythalesians.timeseries.calcs.timeseriescalcs import TimeSeriesCalcs from pythalesians.util.loggermanager import LoggerManager from chartesians.graphs.graphproperties import GraphProperties seasonality = Seasonality() tsc = TimeSeriesCalcs() logger = LoggerManager().getLogger(__name__) pf = PlotFactory() # just change "False" to "True" to run any of the below examples ###### calculate seasonal moves in SPX (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 = 'bloomberg', # use Quandl as data source tickers = ['S&P500'], fields = ['close'], # which fields to download vendor_tickers = ['SPX Index'], # ticker (Quandl) 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' ], fields=['close'], # which fields to download
Examples to show how to use PlotFactory to make charts (at present only line charts have a lot of support). """ import datetime from chartesians.graphs.graphproperties import GraphProperties from chartesians.graphs.plotfactory import PlotFactory from pythalesians.market.loaders.lighttimeseriesfactory import LightTimeSeriesFactory from pythalesians.market.requests.timeseriesrequest import TimeSeriesRequest # just change "False" to "True" to run any of the below examples if True: pf = PlotFactory() # test simple PyThalesians/Bokeh time series line charts if False: ltsf = LightTimeSeriesFactory() start = '01 Jan 2000' end = datetime.datetime.utcnow() tickers = ['AUDJPY', 'USDJPY'] vendor_tickers = ['AUDJPY BGN Curncy', 'USDJPY BGN Curncy'] time_series_request = TimeSeriesRequest( start_date=start, # start date finish_date=datetime.date.today(), # finish date freq='daily', # daily data data_source='bloomberg', # use Bloomberg as data source