Example #1
0
    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 chartesians.graphs import PlotFactory
        from chartesians.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)
Example #2
0
    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 chartesians.graphs import PlotFactory
        from chartesians.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_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 chartesians.graphs import PlotFactory
        from chartesians.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)
Example #4
0
    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 chartesians.graphs import PlotFactory
        from chartesians.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)
Example #5
0
    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)
Example #6
0
    def create_graph_properties(self, title, file_add):
        gp = GraphProperties()

        gp.title = self.FINAL_STRATEGY + " " + title
        gp.display_legend = True
        gp.scale_factor = self.SCALE_FACTOR

        if self.DEFAULT_PLOT_ENGINE != 'cufflinks':
            gp.file_output = self.DUMP_PATH + self.FINAL_STRATEGY + ' (' + file_add + ') ' + str(gp.scale_factor) + '.png'

        gp.html_file_output = self.DUMP_PATH + self.FINAL_STRATEGY + ' (' + file_add + ') ' + str(gp.scale_factor) + '.html'

        try:
            gp.silent_display = self.SILENT_DISPLAY
        except: pass

        return gp
Example #7
0
    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 chartesians.graphs import PlotFactory
        from chartesians.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)
Example #8
0
    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 chartesians.graphs import PlotFactory
        from chartesians.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 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 chartesians.graphs import PlotFactory
        from chartesians.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 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 chartesians.graphs import PlotFactory
        from chartesians.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 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 chartesians.graphs import PlotFactory
        from chartesians.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)
Example #12
0
    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 chartesians.graphs import PlotFactory
        from chartesians.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)
Example #13
0
    def plot_2d_graph(self, data_frame, gp, chart_type):
        sns.set()

        if gp is None: gp = GraphProperties()
        if gp.chart_type is None and chart_type is None: chart_type = 'line'

        if gp.resample is not None: data_frame = data_frame.asfreq(gp.resample)

        self.apply_style_sheet(gp)

        if chart_type == 'heatmap':
            # Draw a heatmap with the numeric values in each cell
            sns_plot = sns.heatmap(data_frame,
                                   annot=True,
                                   fmt="d",
                                   linewidths=.5)

            fig = sns_plot.fig

        # TODO
        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 chartesians.graphs import PlotFactory
        from chartesians.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)
Example #15
0
    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 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 chartesians.graphs import PlotFactory
        from chartesians.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)
Example #17
0
                    freq = 'second',                                # second data
                    data_source = 'bloomberg',                      # use Bloomberg as data source
                    tickers = tickers,                              # ticker (Thalesians)
                    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)
Example #18
0
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
from pythalesians.timeseries.calcs.timeseriescalcs import TimeSeriesCalcs

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
    signal_df = tech_ind.get_signal()

    # use the same data for generating signals
    cash_backtest.calculate_trading_PnL(br, asset_df, signal_df)
    port = cash_backtest.get_cumportfolio()
    port.columns = [
        indicator + ' = ' + str(tech_params.sma_period) + ' ' +
        str(cash_backtest.get_portfolio_pnl_desc()[0])
    ]
    signals = cash_backtest.get_porfolio_signal()

    # print the last positions (we could also save as CSV etc.)
    print(signals.tail(1))

    pf = PlotFactory()
    gp = GraphProperties()
    gp.title = "Thalesians FX trend strategy"
    gp.source = 'Thalesians/BBG (calc with PyThalesians Python library)'
    gp.scale_factor = 1
    gp.file_output = 'output_data/fx-trend-example.png'

    pf.plot_line_graph(port, adapter='pythalesians', gp=gp)

###### backtest simple trend following strategy for FX spot basket
if True:
    # for backtest and loading data
    from pythalesians.market.requests.backtestrequest import BacktestRequest
    from pythalesians.backtest.cash.cashbacktest import CashBacktest
    from pythalesians.market.requests.timeseriesrequest import TimeSeriesRequest
    from pythalesians.market.loaders.lighttimeseriesfactory import LightTimeSeriesFactory
    from pythalesians.util.fxconv import FXConv
    # data_type = 'Total Personal Income'; title = 'Total Personal Income'; freq = 12
    # data_type = 'House Price Index'; title = 'House Price Index (YoY change %)';  yoy_ret = True; freq = 12;

    source = 'fred'

    df = hist.get_economic_data_history(start_date, finish_date, country_group, data_type, source=source)
    df = df.fillna(method='pad')

    if yoy_ret is True: df = 100 * (df / df.shift(freq) - 1)

    df = hist.grasp_coded_entry(df, -1)

    from chartesians.graphs import PlotFactory
    from chartesians.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_url = country_group + data_type.replace(' ', '-')
    gp.plotly_world_readable = False
    gp.title = title
    gp.units = 'Value'

    # do a map plot by US state
    pf.plot_generic_graph(df, type = 'choropleth', adapter = 'cufflinks', gp = gp)

#### uses CommonEconDataFactory to get more common forms of economic data and plot
    # use technical indicator to create signals
    # (we could obviously create whatever function we wanted for generating the signal dataframe)
    tech_ind = TechIndicator()
    tech_ind.create_tech_ind(spot_df, indicator, tech_params); signal_df = tech_ind.get_signal()

    # use the same data for generating signals
    cash_backtest.calculate_trading_PnL(br, asset_df, signal_df)
    port = cash_backtest.get_cumportfolio()
    port.columns = [indicator + ' = ' + str(tech_params.sma_period) + ' ' + str(cash_backtest.get_portfolio_pnl_desc()[0])]
    signals = cash_backtest.get_porfolio_signal()

    # print the last positions (we could also save as CSV etc.)
    print(signals.tail(1))

    pf = PlotFactory()
    gp = GraphProperties()
    gp.title = "Thalesians FX trend strategy"
    gp.source = 'Thalesians/BBG (calc with PyThalesians Python library)'
    gp.scale_factor = 1
    gp.file_output = 'output_data/fx-trend-example.png'

    pf.plot_line_graph(port, adapter = 'pythalesians', gp = gp)

###### backtest simple trend following strategy for FX spot basket
if True:
    # for backtest and loading data
    from pythalesians.market.requests.backtestrequest import BacktestRequest
    from pythalesians.backtest.cash.cashbacktest import CashBacktest
    from pythalesians.market.requests.timeseriesrequest import TimeSeriesRequest
    from pythalesians.market.loaders.lighttimeseriesfactory import LightTimeSeriesFactory
    from pythalesians.util.fxconv import FXConv
Example #22
0
    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
        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)
Example #24
0
    def plot_2d_graph(self, data_frame, gp, chart_type):

        if gp is None: gp = GraphProperties()

        if gp.chart_type is None and chart_type is None: chart_type = 'line'

        scale_factor = gp.scale_factor

        try:
            if gp.bokeh_plot_mode == "offline_jupyter":
                from bokeh.io import output_notebook
                output_notebook()
        except:
            pass

        try:
            html = gp.html_file_output
            if (html is None):
                html = "bokeh.htm"

            output_file(html)
        except:
            pass

        bar_ind = numpy.arange(1, len(data_frame.index) + 1)

        xd, bar_ind, has_bar, no_of_bars = self.get_bar_indices(
            data_frame, gp, chart_type, bar_ind)

        if type(data_frame.index) == pandas.tslib.Timestamp:
            p1 = figure(x_axis_type="datetime",
                        plot_width=int(gp.width * scale_factor),
                        plot_height=int(gp.height * scale_factor),
                        x_range=(xd[0], xd[-1]))

        # if has a bar than categorical axis
        elif has_bar == True:
            p1 = figure(plot_width=int(gp.width * scale_factor),
                        plot_height=int(gp.height * scale_factor),
                        x_range=[str(x) for x in data_frame.index])

            from math import pi
            p1.xaxis.major_label_orientation = pi / 2

        # otherwise numerical axis
        else:
            p1 = figure(plot_width=int(gp.width * scale_factor),
                        plot_height=int(gp.height * scale_factor),
                        x_range=(xd[0], xd[-1]))

        # set the fonts
        p1.axis.major_label_text_font_size = str(10 * scale_factor) + "pt"
        p1.axis.major_label_text_font = Constants().bokeh_font
        p1.axis.major_label_text_font_style = Constants().bokeh_font_style

        p1.xaxis.axis_label_text_font_size = str(10 * scale_factor) + "pt"
        p1.xaxis.axis_label_text_font = Constants().bokeh_font
        p1.xaxis.axis_label_text_font_style = Constants().bokeh_font_style
        p1.xaxis.axis_label = gp.x_title

        p1.yaxis.axis_label_text_font_size = str(10 * scale_factor) + "pt"
        p1.yaxis.axis_label_text_font = Constants().bokeh_font
        p1.yaxis.axis_label_text_font_style = Constants().bokeh_font_style
        p1.yaxis.axis_label = gp.y_title

        p1.legend.label_text_font_size = str(10 * scale_factor) + "pt"
        p1.legend.label_text_font = Constants().bokeh_font
        p1.legend.label_text_font_style = Constants().bokeh_font_style
        p1.legend.fill_alpha = 0

        # set chart outline
        p1.outline_line_width = 0

        p1.title_text_font_size = str(14 * scale_factor) + "pt"
        p1.title_text_font = Constants().bokeh_font

        # TODO fix label
        # if gp.display_source_label:
        #     p1.text([30 * scale_factor, 30 * scale_factor], [0, 0], text = [gp.brand_label],
        #         text_font_size = str(10 * scale_factor) + "pt", text_align = "left",
        #         text_font = Constants().bokeh_font)

        color_spec = self.create_color_list(gp, data_frame)
        import matplotlib

        bar_space = 0.2
        bar_width = (1 - bar_space) / (no_of_bars)
        bar_index = 0

        has_bar = False

        # plot each series in the dataframe separately
        for i in range(0, len(data_frame.columns)):
            label = str(data_frame.columns[i])
            glyph_name = 'glpyh' + str(i)

            # set chart type which can differ for each time series
            if chart_type is not None:
                if gp.chart_type is not None:
                    if isinstance(gp.chart_type, list):
                        chart_type = gp.chart_type[i]
                    else:
                        chart_type = gp.chart_type

            # get the color
            if color_spec[i] is None:
                color_spec[i] = self.get_color_list(i)

            try:
                color_spec[i] = matplotlib.colors.rgb2hex(color_spec[i])
            except:
                pass

            yd = data_frame.ix[:, i]

            # plot each time series as appropriate line, scatter etc.
            if chart_type == 'line':
                linewidth_t = self.get_linewidth(label, gp.linewidth,
                                                 gp.linewidth_2,
                                                 gp.linewidth_2_series)

                if linewidth_t is None: linewidth_t = 1

                if gp.display_legend:
                    p1.line(
                        xd,
                        yd,
                        color=color_spec[i],
                        line_width=linewidth_t,
                        name=glyph_name,
                        legend=label,
                    )
                else:
                    p1.line(xd,
                            data_frame.ix[:, i],
                            color=color_spec[i],
                            line_width=linewidth_t,
                            name=glyph_name)

            elif (chart_type == 'bar'):
                bar_pos = [
                    k - (1 - bar_space) / 2. + bar_index * bar_width
                    for k in range(1,
                                   len(bar_ind) + 1)
                ]
                bar_pos_right = [x + bar_width for x in bar_pos]

                if gp.display_legend:
                    p1.quad(top=yd,
                            bottom=0 * yd,
                            left=bar_pos,
                            right=bar_pos_right,
                            color=color_spec[i],
                            legend=label)
                else:
                    p1.quad(top=yd,
                            bottom=0 * yd,
                            left=bar_pos,
                            right=bar_pos_right,
                            color=color_spec[i])

                bar_index = bar_index + 1
                bar_ind = bar_ind + bar_width

            elif chart_type == 'scatter':
                linewidth_t = self.get_linewidth(label, gp.linewidth,
                                                 gp.linewidth_2,
                                                 gp.linewidth_2_series)

                if linewidth_t is None: linewidth_t = 1

                if gp.display_legend:
                    p1.circle(
                        xd,
                        yd,
                        color=color_spec[i],
                        line_width=linewidth_t,
                        name=glyph_name,
                        legend=label,
                    )
                else:
                    p1.circle(xd,
                              yd,
                              color=color_spec[i],
                              line_width=linewidth_t,
                              name=glyph_name)

            # set common properties
            # glyph = p1.select(name=glyph_name)[0].glyph

        try:
            p1.title = gp.title
        except:
            pass

        p1.grid.grid_line_alpha = 0.3

        show(p1)  # open a browser
    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)
Example #26
0
    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
Example #27
0
    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
                vendor_tickers = ['EURUSD BGN Curncy',          # ticker (Bloomberg)
                                  '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',
        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)
Example #30
0
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
from pythalesians.timeseries.calcs.timeseriescalcs import TimeSeriesCalcs


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
        time_series_request.vendor_tickers = ['JPINTDUSDJPY']
        time_series_request.data_source = 'fred'

        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)
        df_event_times = pandas.DataFrame(index = df_event_times['NFP.release-date-time-full'])
        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
Example #33
0
            freq='second',  # second data
            data_source='bloomberg',  # use Bloomberg as data source
            tickers=tickers,  # ticker (Thalesians)
            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)
    def plot_2d_graph(self, data_frame, gp, chart_type):

        mode = 'line'

        if gp is None: gp = GraphProperties()

        if gp.chart_type is None:
            if chart_type is None: chart_type = 'line'
        else:
            chart_type = gp.chart_type

        marker_size = 1

        x = ''; y = ''; z = ''
        fig = None

        if chart_type == 'line':
            chart_type = 'scatter'
        elif chart_type == 'scatter':
            mode = 'markers'
            marker_size = 5
        elif chart_type == 'bubble':
            x = data_frame.columns[0]
            y = data_frame.columns[1]
            z = data_frame.columns[2]

        # special case for choropleth which has yet to be implemented in Cufflinks
        # will likely remove this in the future
        elif chart_type == 'choropleth':

            for col in data_frame.columns:
                try:
                    data_frame[col] = data_frame[col].astype(str)
                except:
                    pass

            if gp.color != []:
                color = gp.color
            else:
                color = [[0.0, 'rgb(242,240,247)'],[0.2, 'rgb(218,218,235)'],[0.4, 'rgb(188,189,220)'],\
                [0.6, 'rgb(158,154,200)'],[0.8, 'rgb(117,107,177)'],[1.0, 'rgb(84,39,143)']]

            text = ''

            if 'text' in data_frame.columns:
                text = data_frame['Text']

            data = [ dict(
                    type='choropleth',
                    colorscale = color,
                    autocolorscale = False,
                    locations = data_frame['Code'],
                    z = data_frame[gp.plotly_choropleth_field].astype(float),
                    locationmode = gp.plotly_location_mode,
                    text = text,
                    marker = dict(
                        line = dict (
                            color = 'rgb(255,255,255)',
                            width = 1
                        )
                    ),
                    colorbar = dict(
                        title = gp.units
                    )
                ) ]

            layout = dict(
                    title = gp.title,
                    geo = dict(
                        scope=gp.plotly_scope,
                        projection=dict( type=gp.plotly_projection ),
                        showlakes = True,
                        lakecolor = 'rgb(255, 255, 255)',
                    ),
                )

            fig = dict( data=data, layout=layout )


        scale = 1

        try:
            if (gp.plotly_plot_mode == 'offline_html'):
                scale = 2/3
        except:
            pass

        # check other plots implemented by Cufflinks
        if fig is None:

            # special case for surface (given coloring)
            if chart_type == 'surface':
                # create figure
                fig = data_frame.iplot(kind=chart_type,
                    title=gp.title,
                    xTitle=gp.x_title,
                    yTitle=gp.y_title,
                    x=x, y=y, z=z,
                    mode=mode,
                    size=marker_size,
                    theme=gp.plotly_theme,
                    bestfit=gp.line_of_best_fit,
                    legend=gp.display_legend,
                    colorscale=gp.color,
                    dimensions=(gp.width * gp.scale_factor * scale, gp.height * gp.scale_factor * scale),
                    asFigure=True)
            else:
                # get all the correct colors (and construct gradients if necessary eg. from 'Blues')
                # need to change to strings for cufflinks
                color_spec = []
                color_list = self.create_color_list(gp, data_frame)

                # if no colors are specified strip the list
                if color_list == [None] * len(color_list):
                    color_spec = None

                else:
                    # otherwise assume all the colors are rgba
                    for color in color_list:
                        color = 'rgba' + str(color)
                        color_spec.append(color)

                # create figure
                fig = data_frame.iplot(kind=chart_type,
                    title=gp.title,
                    xTitle=gp.x_title,
                    yTitle=gp.y_title,
                    x=x, y=y, z=z,
                    mode=mode,
                    size=marker_size,
                    theme=gp.plotly_theme,
                    bestfit=gp.line_of_best_fit,
                    legend=gp.display_legend,
                    color=color_spec,
                    dimensions=(gp.width * gp.scale_factor * scale, gp.height * gp.scale_factor * scale),
                    asFigure=True)

        self.publish_plot(fig, gp)
        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
            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()
            index=df_event_times['NFP.release-date-time-full'])
        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
        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)
            fields=['close'],  # which fields to download
            vendor_tickers=[
                'EURUSD BGN Curncy',  # ticker (Bloomberg)
                'GBPUSD BGN Curncy',
                'JPYUSD BGN Curncy',
                'AUDUSD BGN Curncy'
            ],
            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()
        pf = PlotFactory()
        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 daily data from Quandl (via FRED) for EUR/USD and GBP/USD spot and then plot
    if False:

        time_series_request = TimeSeriesRequest(
            start_date="01 Jan 1970",  # start date
            finish_date=datetime.date.today(),  # finish date
            freq='daily',  # daily data
Example #39
0
                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 = 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)
    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)
        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)
    df = hist.get_economic_data_history(start_date,
                                        finish_date,
                                        country_group,
                                        data_type,
                                        source=source)
    df = df.fillna(method='pad')

    if yoy_ret is True: df = 100 * (df / df.shift(freq) - 1)

    df = hist.grasp_coded_entry(df, -1)

    from chartesians.graphs import PlotFactory
    from chartesians.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_url = country_group + data_type.replace(' ', '-')
    gp.plotly_world_readable = False
    gp.title = title
    gp.units = 'Value'

    # do a map plot by US state
    pf.plot_generic_graph(df, type='choropleth', adapter='cufflinks', gp=gp)

#### uses CommonEconDataFactory to get more common forms of economic data and plot
Example #43
0
    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').mean()
            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
Example #44
0
    def plot_2d_graph(self, data_frame, gp, chart_type):

        if gp is None: gp = GraphProperties()
        if gp.chart_type is None and chart_type is None: chart_type = 'line'

        if gp.resample is not None: data_frame = data_frame.asfreq(gp.resample)

        self.apply_style_sheet(gp)

        # create figure & add a subplot
        fig = plt.figure(figsize=((gp.width * gp.scale_factor) / gp.dpi,
                                  (gp.height * gp.scale_factor) / gp.dpi),
                         dpi=gp.dpi)

        ax = fig.add_subplot(111)

        if gp.x_title != '': ax.set_xlabel(gp.x_title)
        if gp.y_title != '': ax.set_ylabel(gp.y_title)

        plt.xlabel(gp.x_title)
        plt.ylabel(gp.y_title)

        fig.suptitle(gp.title, fontsize=14 * gp.scale_factor)

        # format Y axis
        y_formatter = matplotlib.ticker.ScalarFormatter(useOffset=False)
        ax.yaxis.set_major_formatter(y_formatter)

        # create a second y axis if necessary
        ax2 = []

        if gp.y_axis_2_series != []:
            ax2 = ax.twinx()

            # do not use a grid with multiple y axes
            ax.yaxis.grid(False)
            ax2.yaxis.grid(False)

        # matplotlib 1.5
        try:
            cyc = matplotlib.rcParams['axes.prop_cycle']
            color_cycle = [x['color'] for x in cyc]
        except KeyError:
            # pre 1.5
            pass
            # color_cycle =  matplotlib.rcParams['axes.color_cycle']

        bar_ind = np.arange(0, len(data_frame.index))

        # for bar charts, create a proxy x-axis (then relabel)
        xd, bar_ind, has_bar, no_of_bars = self.get_bar_indices(
            data_frame, gp, chart_type, bar_ind)

        # plot the lines (using custom palettes as appropriate)
        try:
            # get all the correct colors (and construct gradients if necessary eg. from 'blues')
            color_spec = self.create_color_list(gp, data_frame)

            # for stacked bar
            yoff_pos = np.zeros(len(data_frame.index.values)
                                )  # the bottom values for stacked bar chart
            yoff_neg = np.zeros(len(data_frame.index.values)
                                )  # the bottom values for stacked bar chart

            zeros = np.zeros(len(data_frame.index.values))

            # for bar chart
            bar_space = 0.2
            bar_width = (1 - bar_space) / (no_of_bars)
            bar_index = 0

            has_matrix = False

            # some lines we should exclude from the color and use the default palette
            for i in range(0, len(data_frame.columns.values)):

                if gp.chart_type is not None:
                    if isinstance(gp.chart_type, list):
                        chart_type = gp.chart_type[i]
                    else:
                        chart_type = gp.chart_type

                if chart_type == 'heatmap':
                    # TODO experimental!
                    # ax.set_frame_on(False)
                    ax.pcolor(data_frame, cmap=plt.cm.Blues, alpha=0.8)
                    # plt.colorbar()
                    has_matrix = True
                    break

                label = str(data_frame.columns[i])

                ax_temp = self.get_axis(ax, ax2, label, gp.y_axis_2_series)

                yd = data_frame.ix[:, i]

                if color_spec[i] is None:
                    color_spec[i] = color_cycle[i % len(color_cycle)]

                if (chart_type == 'line'):
                    linewidth_t = self.get_linewidth(label, gp.linewidth,
                                                     gp.linewidth_2,
                                                     gp.linewidth_2_series)

                    if linewidth_t is None:
                        linewidth_t = matplotlib.rcParams['axes.linewidth']

                    ax_temp.plot(xd,
                                 yd,
                                 label=label,
                                 color=color_spec[i],
                                 linewidth=linewidth_t)

                elif (chart_type == 'bar'):
                    # for multiple bars we need to allocate space properly
                    bar_pos = [
                        k - (1 - bar_space) / 2. + bar_index * bar_width
                        for k in range(0, len(bar_ind))
                    ]

                    ax_temp.bar(bar_pos,
                                yd,
                                bar_width,
                                label=label,
                                color=color_spec[i])

                    bar_index = bar_index + 1

                elif (chart_type == 'stacked'):
                    bar_pos = [
                        k - (1 - bar_space) / 2. + bar_index * bar_width
                        for k in range(0, len(bar_ind))
                    ]

                    yoff = np.where(yd > 0, yoff_pos, yoff_neg)

                    ax_temp.bar(bar_pos,
                                yd,
                                label=label,
                                color=color_spec[i],
                                bottom=yoff)

                    yoff_pos = yoff_pos + np.maximum(yd, zeros)
                    yoff_neg = yoff_neg + np.minimum(yd, zeros)

                    # bar_index = bar_index + 1

                elif (chart_type == 'scatter'):
                    ax_temp.scatter(xd, yd, label=label, color=color_spec[i])

                    if gp.line_of_best_fit is True:
                        self.trendline(ax_temp,
                                       xd.values,
                                       yd.values,
                                       order=1,
                                       color=color_spec[i],
                                       alpha=1,
                                       scale_factor=gp.scale_factor)

            # format X axis
            self.format_x_axis(ax, data_frame, gp, has_bar, bar_ind,
                               has_matrix)

        except:
            pass

        if gp.display_source_label == True:
            ax.annotate('Source: ' + gp.source,
                        xy=(1, 0),
                        xycoords='axes fraction',
                        fontsize=7 * gp.scale_factor,
                        xytext=(-5 * gp.scale_factor, 10 * gp.scale_factor),
                        textcoords='offset points',
                        ha='right',
                        va='top',
                        color=gp.source_color)

        if gp.display_brand_label == True:
            self.create_brand_label(ax,
                                    anno=gp.brand_label,
                                    scale_factor=gp.scale_factor)

        leg = []
        leg2 = []

        loc = 'best'

        # if we have two y-axis then make sure legends are in opposite corners
        if ax2 != []: loc = 2

        try:
            leg = ax.legend(loc=loc, prop={'size': 10 * gp.scale_factor})
            leg.get_frame().set_linewidth(0.0)
            leg.get_frame().set_alpha(0)

            if ax2 != []:
                leg2 = ax2.legend(loc=1, prop={'size': 10 * gp.scale_factor})
                leg2.get_frame().set_linewidth(0.0)
                leg2.get_frame().set_alpha(0)
        except:
            pass

        try:
            if gp.display_legend is False:
                if leg != []: leg.remove()
                if leg2 != []: leg.remove()
        except:
            pass

        try:
            plt.savefig(gp.file_output, transparent=False)
        except:
            pass

        ####### various matplotlib converters are unstable
        # convert to D3 format with mpld3
        try:
            # output matplotlib charts externally to D3 based libraries
            import mpld3

            if gp.display_mpld3 == True:
                mpld3.save_d3_html(fig, gp.html_file_output)
                mpld3.show(fig)
        except:
            pass

        # FRAGILE! convert to Bokeh format
        # better to use direct Bokeh renderer
        try:
            if (gp.convert_matplotlib_to_bokeh == True):
                from bokeh.plotting import output_file, show
                from bokeh import mpl

                output_file(gp.html_file_output)
                show(mpl.to_bokeh())
        except:
            pass

        # FRAGILE! convert matplotlib chart to Plotly format
        # recommend using AdapterCufflinks instead to directly plot to Plotly
        try:
            import plotly.plotly as py
            import plotly
            import plotly.tools as tls

            if gp.convert_matplotlib_to_plotly == True:
                plotly.tools.set_credentials_file(username=gp.plotly_username,
                                                  api_key=gp.plotly_api_key)

                py_fig = tls.mpl_to_plotly(fig, strip_style=True)
                plot_url = py.plot_mpl(py_fig, filename=gp.plotly_url)
        except:
            pass

        # display in matplotlib window
        try:
            if GraphicsConstants.plotfactory_silent_display == True:
                return fig
            elif gp.silent_display == False:
                plt.show()
            else:
                return fig

        except:
            pass
Example #45
0
            ],
            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)
Example #46
0
                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

    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()