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_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_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_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_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
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')
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 start = "01 Jan 1970" end = datetime.datetime.utcnow() tickers = ["AUDJPY", "USDJPY"] vendor_tickers = ["AUDJPY BGN Curncy", "USDJPY BGN Curncy"] time_series_request = TimeSeriesRequest(
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 start = '01 Jan 1970' end = datetime.datetime.utcnow() tickers = ['AUDJPY', 'USDJPY'] vendor_tickers = ['AUDJPY BGN Curncy', 'USDJPY BGN Curncy'] 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')
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[ ]:
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 start = '01 Jan 1970' end = datetime.datetime.utcnow() tickers = ['AUDJPY', 'USDJPY'] vendor_tickers = ['AUDJPY BGN Curncy', 'USDJPY BGN Curncy'] time_series_request = TimeSeriesRequest(
freq="daily", # daily data data_source="bloomberg", # use Bloomberg as data source tickers=["EURUSD", "GBPUSD"], # ticker (Thalesians) fields=["close", "high", "low"], # which fields to download vendor_tickers=["EURUSD BGN Curncy", "GBPUSD BGN Curncy"], # ticker (Bloomberg) vendor_fields=["PX_LAST", "PX_HIGH", "PX_LOW"], # 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) pf = PlotFactory() pf.plot_line_graph(df, adapter="pythalesians") ###### download event dates for non farm payrolls and then print if False: time_series_request = TimeSeriesRequest( start_date="01 Jan 2014", # start date finish_date=datetime.date.today(), # finish date category="events", freq="daily", # daily data data_source="bloomberg", # use Bloomberg as data source tickers=["FOMC", "NFP"], fields=["release-date-time-full", "release-dt", "actual-release"], # which fields to download vendor_tickers=["FDTR Index", "NFP TCH Index"], # ticker (Bloomberg) vendor_fields=[ "ECO_FUTURE_RELEASE_DATE_LIST",
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)
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)
###### 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)
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)