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
Exemple #2
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')
        rets = tsc.calculate_returns(pnl)
        bus_day = seas.bus_day_of_month_seasonality(rets, add_average=True)

        # get seasonality by month
        pnl = pnl.resample('BM')
        rets = tsc.calculate_returns(pnl)
        month = seas.monthly_seasonality(rets)

        self.logger.info("About to plot seasonality...")
        gp = GraphProperties()
        pf = PlotFactory()

        # Plotting spot over day of month/month of year
        gp.color = 'Blues'
        gp.scale_factor = self.scale_factor
        gp.file_output = self.DUMP_PATH + strat.FINAL_STRATEGY + ' seasonality day of month.png'
        gp.title = strat.FINAL_STRATEGY + ' day of month seasonality'
        gp.display_legend = False
        gp.color_2_series = [bus_day.columns[-1]]
        gp.color_2 = ['red']  # red, pink
        gp.linewidth_2 = 4
        gp.linewidth_2_series = [bus_day.columns[-1]]
        gp.y_axis_2_series = [bus_day.columns[-1]]

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

        gp = GraphProperties()

        gp.scale_factor = self.scale_factor
        gp.file_output = self.DUMP_PATH + strat.FINAL_STRATEGY + ' seasonality month of year.png'
        gp.title = strat.FINAL_STRATEGY + ' month of year seasonality'

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

        return month
        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)
                start_date = "01 Jun 2015",                     # start date
                finish_date = "02 Jun 2015",                    # finish date
                freq = 'tick',                                  # tick data
                data_source = 'dukascopy',                      # use dukascopy as data source
                tickers = ['EURUSD', 'GBPUSD'],                 # ticker (Thalesians)
                fields = ['bid', 'ask'],                        # which fields to download
                vendor_tickers = ['EURUSD', 'GBPUSD'],          # ticker (Dukascopy)
                vendor_fields = ['bid', 'ask'],                 # which Dukascopy fields to download
                cache_algo = 'internet_load_return')            # how to return data

        ltsf = LightTimeSeriesFactory()

        df = ltsf.harvest_time_series(time_series_request)

        gp = GraphProperties()
        gp.y_axis_2_series = ['GBPUSD.bid', 'GBPUSD.ask']

        pf = PlotFactory()
        pf.plot_line_graph(df, adapter = 'pythalesians', gp = gp)

    ###### download daily data from Google for Apple and S&P500 ETF (and then rebase, before plotting)
    if False:

        time_series_request = TimeSeriesRequest(
                start_date = "01 Jan 1970",                     # 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)
            start_date="01 Jun 2015",  # start date
            finish_date="02 Jun 2015",  # finish date
            freq='tick',  # tick data
            data_source='dukascopy',  # use dukascopy as data source
            tickers=['EURUSD', 'GBPUSD'],  # ticker (Thalesians)
            fields=['bid', 'ask'],  # which fields to download
            vendor_tickers=['EURUSD', 'GBPUSD'],  # ticker (Dukascopy)
            vendor_fields=['bid', 'ask'],  # which Dukascopy fields to download
            cache_algo='internet_load_return')  # how to return data

        ltsf = LightTimeSeriesFactory()

        df = ltsf.harvest_time_series(time_series_request)

        gp = GraphProperties()
        gp.y_axis_2_series = ['GBPUSD.bid', 'GBPUSD.ask']

        pf = PlotFactory()
        pf.plot_line_graph(df, adapter='pythalesians', gp=gp)

    ###### download daily data from Google for Apple and S&P500 ETF (and then rebase, before plotting)
    if False:

        time_series_request = TimeSeriesRequest(
            start_date="01 Jan 1970",  # 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)
        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.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)