コード例 #1
0
    def compare_strategy_vs_benchmark(self, br, strategy_df, benchmark_df):
        """
        compare_strategy_vs_benchmark - Compares the trading strategy we are backtesting against a benchmark

        Parameters
        ----------
        br : BacktestRequest
            Parameters for backtest such as start and finish dates

        strategy_df : pandas.DataFrame
            Strategy time series

        benchmark_df : pandas.DataFrame
            Benchmark time series
        """

        include_benchmark = False
        calc_stats = False

        if hasattr(br, 'include_benchmark'): include_benchmark = br.include_benchmark
        if hasattr(br, 'calc_stats'): calc_stats = br.calc_stats

        if include_benchmark:
            ret_stats = RetStats()
            risk_engine = RiskEngine()
            filter = Filter()
            calculations = Calculations()

            # align strategy time series with that of benchmark
            strategy_df, benchmark_df = strategy_df.align(benchmark_df, join='left', axis = 0)

            # if necessary apply vol target to benchmark (to make it comparable with strategy)
            if hasattr(br, 'portfolio_vol_adjust'):
                if br.portfolio_vol_adjust is True:
                    benchmark_df = risk_engine.calculate_vol_adjusted_index_from_prices(benchmark_df, br = br)

            # only calculate return statistics if this has been specified (note when different frequencies of data
            # might underrepresent vol
            # if calc_stats:
            benchmark_df = benchmark_df.fillna(method='ffill')
            ret_stats.calculate_ret_stats_from_prices(benchmark_df, br.ann_factor)

            if calc_stats:
                benchmark_df.columns = ret_stats.summary()

            # realign strategy & benchmark
            strategy_benchmark_df = strategy_df.join(benchmark_df, how='inner')
            strategy_benchmark_df = strategy_benchmark_df.fillna(method='ffill')

            strategy_benchmark_df = filter.filter_time_series_by_date(br.plot_start, br.finish_date, strategy_benchmark_df)
            strategy_benchmark_df = calculations.create_mult_index_from_prices(strategy_benchmark_df)

            self._benchmark_pnl = benchmark_df
            self._benchmark_ret_stats = ret_stats

            return strategy_benchmark_df

        return strategy_df
コード例 #2
0
    def compare_strategy_vs_benchmark(self, br, strategy_df, benchmark_df):
        """Compares the trading strategy we are backtesting against a benchmark

        Parameters
        ----------
        br : BacktestRequest
            Parameters for backtest such as start and finish dates

        strategy_df : pandas.DataFrame
            Strategy time series

        benchmark_df : pandas.DataFrame
            Benchmark time series
        """

        include_benchmark = False
        calc_stats = False

        if hasattr(br, 'include_benchmark'): include_benchmark = br.include_benchmark
        if hasattr(br, 'calc_stats'): calc_stats = br.calc_stats

        if include_benchmark:
            ret_stats = RetStats()
            risk_engine = RiskEngine()
            filter = Filter()
            calculations = Calculations()

            # align strategy time series with that of benchmark
            strategy_df, benchmark_df = strategy_df.align(benchmark_df, join='left', axis = 0)

            # if necessary apply vol target to benchmark (to make it comparable with strategy)
            if hasattr(br, 'portfolio_vol_adjust'):
                if br.portfolio_vol_adjust is True:
                    benchmark_df = risk_engine.calculate_vol_adjusted_index_from_prices(benchmark_df, br = br)

            # only calculate return statistics if this has been specified (note when different frequencies of data
            # might underrepresent vol
            # if calc_stats:
            benchmark_df = benchmark_df.fillna(method='ffill')
            ret_stats.calculate_ret_stats_from_prices(benchmark_df, br.ann_factor)

            if calc_stats:
                benchmark_df.columns = ret_stats.summary()

            # realign strategy & benchmark
            strategy_benchmark_df = strategy_df.join(benchmark_df, how='inner')
            strategy_benchmark_df = strategy_benchmark_df.fillna(method='ffill')

            strategy_benchmark_df = filter.filter_time_series_by_date(br.plot_start, br.finish_date, strategy_benchmark_df)
            strategy_benchmark_df = calculations.create_mult_index_from_prices(strategy_benchmark_df)

            self._benchmark_pnl = benchmark_df
            self._benchmark_ret_stats = ret_stats

            return strategy_benchmark_df

        return strategy_df
コード例 #3
0
    df_cuemacro_option_put_tc = fx_options_curve.apply_tc_to_total_return_index(cross, df_cuemacro_option_put_tot,
                                                                                 option_tc_bp=5, spot_tc_bp=2)

    # Get total returns for spot
    md_request.abstract_curve = None

    # Get Bloomberg calculated total return indices (for spot)
    md_request.category = 'fx-tot'
    md_request.cut = 'NYC'

    df_bbg_tot = market.fetch_market(md_request)
    df_bbg_tot.columns = [x + '-bbg' for x in df_bbg_tot.columns]

    calculations = Calculations()

    chart.plot(calculations.create_mult_index_from_prices(
        prepare_indices(df_tot=df_cuemacro_option_call_tot, df_tc=df_cuemacro_option_call_tc, df_spot_tot=df_bbg_tot)))

    chart.plot(calculations.create_mult_index_from_prices(
        prepare_indices(df_tot=df_cuemacro_option_put_tot, df_tc=df_cuemacro_option_put_tc, df_spot_tot=df_bbg_tot)))

    chart.plot(calculations.create_mult_index_from_prices(
        prepare_indices(df_tc=df_cuemacro_option_put_tc, df_spot_tot=df_bbg_tot)))


###### Fetch market data for pricing EURUSD options from 2006-2020 (ie. FX spot, FX forwards, FX deposits and FX vol quotes)
###### Construct volatility surface using FinancePy library underneath, using polynomial interpolation
###### Enters a short 1W straddle, and MTM every day, and at expiry rolls into another 1W straddle
if run_example == 2 or run_example == 0:

    # Warning make sure you choose dates, where there is full vol surface! If vol points in the tenors you are looking at
    # are missing then interpolation will fail (or if eg. spot data is missing etc.)
コード例 #4
0
                                   cache_algo='cache_algo_return',
                                   abstract_curve=FXSpotCurve(construct_via_currency='USD', depo_tenor='ON'))

    df_tot = market.fetch_market(md_request=md_request)
    df_tot.columns = [x + '-tot-cuemacro' for x in df_tot.columns]

    # Get spot data
    md_request.abstract_curve = None

    df_spot = market.fetch_market(md_request=md_request)
    df_spot.columns = [x + '-spot' for x in df_spot.columns]

    # Get Bloomberg calculated total return indices (for spot)
    md_request.category = 'fx-tot'

    df_bbg_tot = market.fetch_market(md_request)
    df_bbg_tot.columns = [x + '-bbg' for x in df_bbg_tot.columns]

    # Get Bloomberg calculated total return indices (for 1M forwards rolled)
    md_request.category = 'fx-tot-forwards'

    df_bbg_tot_forwards = market.fetch_market(md_request)
    df_bbg_tot_forwards.columns = [x + '-bbg' for x in df_bbg_tot_forwards.columns]

    # Combine into a single data frame and plot, we note that the Cuemacro constructed indices track the Bloomberg
    # indices relatively well (both from spot and 1M forwards). Also note the large difference with spot indices
    # CAREFUL to fill down, before reindexing because 1M forwards indices are likely to have different publishing dates
    df = calculations.pandas_outer_join([df_tot, df_bbg_tot, df_spot, df_bbg_tot_forwards]).fillna(method='ffill')
    df = calculations.create_mult_index_from_prices(df)

    chart.plot(df)