本文整理汇总了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