def test_KPI_calmar(self): tickers = ["TSLA", "SPY"] interval = Ct.INTERVAL.DAY conf = { Ct.tickers_key(): tickers, Ct.historical_type_key(): DATASOURCETYPE.YFINANCE, Ct.fundamentals_type_key(): None, Ct.fundamentals_options_key(): [], Ct.force_fundamentals_key(): False, Ct.indicators_key(): [], Ct.start_date_key(): dt.datetime(2021, 3, 7) - dt.timedelta(1825), Ct.end_date_key(): dt.datetime(2021, 3, 7), Ct.interval_key(): interval, Ct.period_key(): None, Ct.bulk_key(): True } stocks = \ StocksFactory.create_stocks( conf=conf ) prices_df = stocks[0].get_prices_data(keys={Ct.adj_close_key(): True}) df = Financials.pct_change(prices_df) params = {Ct.interval_key(): interval} calmar = Calmar() result = calmar.calculate(df, params) self.assertEqual(1.1700790532917946, result[Ct.calmar_key()]['TSLA'])
def test_KPI_max_drawdown(self): tickers = ["TSLA", "SPY"] interval = Ct.INTERVAL.DAY conf = { Ct.tickers_key(): tickers, Ct.historical_type_key(): DATASOURCETYPE.YFINANCE, Ct.fundamentals_type_key(): None, Ct.fundamentals_options_key(): [], Ct.force_fundamentals_key(): False, Ct.indicators_key(): [], Ct.start_date_key(): dt.datetime(2021, 3, 7) - dt.timedelta(1825), Ct.end_date_key(): dt.datetime(2021, 3, 7), Ct.interval_key(): interval, Ct.period_key(): None, Ct.bulk_key(): True } stocks = \ StocksFactory.create_stocks( conf=conf ) prices_df = stocks[0].get_prices_data(keys={Ct.adj_close_key(): True}) df = Financials.pct_change(prices_df) md = MaxDrawdown() result = md.calculate(df) self.assertEqual(0.6062653645917145, result[Ct.max_drawdown_key()]['TSLA']) self.assertEqual(0.3371725544013077, result[Ct.max_drawdown_key()]['SPY'])
def test_portfolio_rebalance(self): # DJI constituent stocks tickers = ["MMM", "AXP", "T", "BA", "CAT", "CSCO", "KO", "XOM", "GE", "GS", "HD", "IBM", "INTC", "JNJ", "JPM", "MCD", "MRK", "MSFT", "NKE", "PFE", "PG", "TRV", "UNH", "VZ", "V", "WMT", "DIS"] ref_tickers = ["^DJI"] end_date = dt.datetime.now() # dt.datetime(2021, 3, 7) interval = Ct.INTERVAL.MONTH conf = { Ct.tickers_key(): tickers, Ct.historical_type_key(): DATASOURCETYPE.YFINANCE, Ct.fundamentals_type_key(): None, Ct.fundamentals_options_key(): [], Ct.force_fundamentals_key(): False, Ct.indicators_key(): [], Ct.start_date_key(): end_date-dt.timedelta(3650), Ct.end_date_key(): end_date, Ct.interval_key(): interval, Ct.period_key(): None, Ct.bulk_key(): True } stocks = \ StocksFactory.create_stocks( conf=conf ) conf[Ct.tickers_key()] = ref_tickers stocks_ref = \ StocksFactory.create_stocks( conf=conf ) prices_df = stocks[0].get_prices_data(keys={Ct.adj_close_key(): True}) ref_prices_df = stocks_ref[0].get_prices_data(keys={Ct.adj_close_key(): True}) pr = PortfolioRebalance() params = {"m": 6, "x": 3, Ct.interval_key(): conf[Ct.interval_key()]} pr.backtest(prices_df, ref_prices_df, params)