def test_bias(self): """ Test forward bias of algo. Test on a portion of given data set, then add several data points and see if weights has changed. """ S = self.random_portfolio k = 10 B1 = CRP().run(S.iloc[:-k]).B B2 = CRP().run(S).B self.assertTrue((B1 == B2.iloc[:-k]).all().all())
def test_equity(self): """ Make sure that equity of a portfolio [1,0,...,0] with NaN values is the same as asset itself. """ S = self.random_portfolio b = [1.] + [0.] * (len(S.columns) - 1) result = CRP(b).run(S) self.assertAlmostEqual(result.total_wealth, S[S.columns[0]].iget(-1), 10)
def hedge(self, result=None): """ Hedge results with results of other strategy (subtract weights). :param result: Other result object. Default is UCRP. :return: New AlgoResult object. """ if result is None: from algos import CRP result = CRP().run(self.X.cumprod()) return AlgoResult(self.X, self.B - result.B)
def plot(self, ucrp=False, bah=False, residual=False, assets=False, **kwargs): """ Plot strategy equity. :param ucrp: Add uniform CRP as a benchmark. :param bah: Add Buy-And-Hold portfolio as a benchmark. :param residual: Add portfolio minus UCRP as a benchmark. :param assets: Add asset prices. :param kwargs: Additional arguments for pd.DataFrame.plot """ # NOTE: order of plotting is important because of coloring # plot portfolio d = self.to_dataframe() D = d.copy() # add individual assets if isinstance(assets, bool): if assets: assets = self[0].asset_equity.columns else: assets = [] if list(assets): D = D.join(self[0].asset_equity) ax = D.plot(color=_colors_hash(D.columns), **kwargs) kwargs['ax'] = ax ax.set_ylabel('Total wealth') # plot residual strategy if residual: d['RESIDUAL'] = self[0].residual_r.cumprod() d[['RESIDUAL']].plot(**kwargs) # plot uniform constant rebalanced portfolio if ucrp: from .algos import CRP crp_algo = CRP().run(self[0].X.cumprod()) crp_algo.fee = self[0].fee d['UCRP'] = crp_algo.equity d[['UCRP']].plot(**kwargs) # add bah if bah: from .algos import BAH bah_algo = BAH().run(self[0].X.cumprod()) bah_algo.fee = self[0].fee d['BAH'] = bah_algo.equity d[['BAH']].plot(**kwargs) return ax
def plot(self, ucrp=False, bah=False, assets=False, **kwargs): """ Plot strategy equity. :param ucrp: Add uniform CRP as a benchmark. :param bah: Add Buy-And-Hold portfolio as a benchmark. :param assets: Add asset prices. :param kwargs: Additional arguments for pd.DataFrame.plot """ # NOTE: order of plotting is important because of coloring # plot portfolio d = self.to_dataframe() portfolio = d.copy() ax = portfolio.plot(linewidth=3., legend=False, **kwargs) kwargs['ax'] = ax ax.set_ylabel('Total wealth') # plot uniform constant rebalanced portfolio if ucrp: from universal.algos import CRP crp_algo = CRP().run(self[0].X.cumprod()) crp_algo.fee = self[0].fee d['UCRP'] = crp_algo.equity d[['UCRP']].plot(**kwargs) # add bah if bah: from universal.algos import BAH bah_algo = BAH().run(self[0].X.cumprod()) bah_algo.fee = self[0].fee d['BAH'] = bah_algo.equity d[['BAH']].plot(**kwargs) # add individual assets if isinstance(assets, bool): if assets: assets = self[0].asset_equity.columns else: assets = [] if list(assets): self[0].asset_equity.sort_index(axis=1).plot( color=_colors(len(assets) + 1), **kwargs) # plot portfolio again to highlight it kwargs['color'] = 'blue' portfolio.plot(linewidth=3., **kwargs) return ax
def ucrp_sharpe(self): from universal.algos import CRP result = CRP().run(self.X.cumprod()) return result.sharpe
def ucrp_sharpe(self): from .algos import CRP result = CRP().run(self.X.cumprod()) result.set_rf_rate(self.rf_rate) return result.sharpe