当前位置: 首页>>代码示例>>Python>>正文


Python TimeSeriesCalcs.average_by_annualised_year方法代码示例

本文整理汇总了Python中pythalesians.timeseries.calcs.timeseriescalcs.TimeSeriesCalcs.average_by_annualised_year方法的典型用法代码示例。如果您正苦于以下问题:Python TimeSeriesCalcs.average_by_annualised_year方法的具体用法?Python TimeSeriesCalcs.average_by_annualised_year怎么用?Python TimeSeriesCalcs.average_by_annualised_year使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在pythalesians.timeseries.calcs.timeseriescalcs.TimeSeriesCalcs的用法示例。


在下文中一共展示了TimeSeriesCalcs.average_by_annualised_year方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: construct_strategy

# 需要导入模块: from pythalesians.timeseries.calcs.timeseriescalcs import TimeSeriesCalcs [as 别名]
# 或者: from pythalesians.timeseries.calcs.timeseriescalcs.TimeSeriesCalcs import average_by_annualised_year [as 别名]
    def construct_strategy(self, br = None):
        """
        construct_strategy - Constructs the returns for all the strategies which have been specified.

        - gets parameters form fill_backtest_request
        - market data from fill_assets

        """

        time_series_calcs = TimeSeriesCalcs()

        # get the parameters for backtesting
        if hasattr(self, 'br'):
            br = self.br
        elif br is None:
            br = self.fill_backtest_request()

        # get market data for backtest
        asset_df, spot_df, spot_df2, basket_dict = self.fill_assets()

        if hasattr(br, 'tech_params'):
            tech_params = br.tech_params
        else:
            tech_params = TechParams()

        cumresults = pandas.DataFrame(index = asset_df.index)
        portleverage = pandas.DataFrame(index = asset_df.index)

        from collections import OrderedDict
        tsdresults = OrderedDict()

        # each portfolio key calculate returns - can put parts of the portfolio in the key
        for key in basket_dict.keys():
            asset_cut_df = asset_df[[x +'.close' for x in basket_dict[key]]]
            spot_cut_df = spot_df[[x +'.close' for x in basket_dict[key]]]

            self.logger.info("Calculating " + key)

            results, cash_backtest = self.construct_individual_strategy(br, spot_cut_df, spot_df2, asset_cut_df, tech_params, key)

            cumresults[results.columns[0]] = results
            portleverage[results.columns[0]] = cash_backtest.get_porfolio_leverage()
            tsdresults[key] = cash_backtest.get_portfolio_pnl_tsd()

            # for a key, designated as the final strategy save that as the "strategy"
            if key == self.FINAL_STRATEGY:
                self._strategy_pnl = results
                self._strategy_pnl_tsd = cash_backtest.get_portfolio_pnl_tsd()
                self._strategy_leverage = cash_backtest.get_porfolio_leverage()
                self._strategy_signal = cash_backtest.get_porfolio_signal()
                self._strategy_pnl_trades = cash_backtest.get_pnl_trades()

        # get benchmark for comparison
        benchmark = self.construct_strategy_benchmark()

        cumresults_benchmark = self.compare_strategy_vs_benchmark(br, cumresults, benchmark)

        self._strategy_group_benchmark_tsd = tsdresults

        if hasattr(self, '_benchmark_tsd'):
            tsdlist = tsdresults
            tsdlist['Benchmark'] = (self._benchmark_tsd)
            self._strategy_group_benchmark_tsd = tsdlist

        # calculate annualised returns
        years = time_series_calcs.average_by_annualised_year(time_series_calcs.calculate_returns(cumresults_benchmark))

        self._strategy_group_pnl = cumresults
        self._strategy_group_pnl_tsd = tsdresults
        self._strategy_group_benchmark_pnl = cumresults_benchmark
        self._strategy_group_leverage = portleverage
        self._strategy_group_benchmark_annualised_pnl = years
开发者ID:expressoman,项目名称:pythalesians,代码行数:74,代码来源:strategytemplate.py


注:本文中的pythalesians.timeseries.calcs.timeseriescalcs.TimeSeriesCalcs.average_by_annualised_year方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。