How to use the finmarketpy.backtest.backtestengine.RiskEngine function in finmarketpy

To help you get started, we’ve selected a few finmarketpy examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github cuemacro / finmarketpy / finmarketpy / backtest / backtestengine.py View on Github external
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
        """

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

            # align strategy time series with that of benchmark
            benchmark_df.columns = [x + ' be' for x in benchmark_df.columns]
            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 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)
github cuemacro / finmarketpy / finmarketpy / backtest / backtestengine.py View on Github external
----------
        br : BacktestRequest
            Parameters for the backtest specifying start date, finish data, transaction costs etc.

        asset_a_df : pandas.DataFrame
            Asset prices to be traded

        signal_df : pandas.DataFrame
            Signals for the trading strategy

        contract_value_df : pandas.DataFrame
            Daily size of contracts
        """

        calculations = Calculations()
        risk_engine = RiskEngine()

        # # do an outer join first, so can fill out signal and fill it down
        # # this captures the case where the signal changes on an asset holiday
        # # it will just get delayed till the next tradable day when we do this
        # asset_df_2, signal_df_2 = asset_a_df.align(signal_df, join='outer', axis='index')
        # signal_df = signal_df_2.fillna(method='ffill')
        #
        # # now make sure the dates of both traded asset and signal are aligned properly
        # # and use as reference only those days where we have asset information
        # asset_df, signal_df = asset_a_df.align(signal_df, join='left', axis = 'index')

        logger = LoggerManager().getLogger(__name__)

        logger.info("Calculating trading P&L...")

        signal_df = signal_df.shift(br.signal_delay)