def test_find_zero_turnover_and_weight(): date = pd.Timestamp("2018-12-18") positions = dict(KAZT=1, KAZTP=0, CHMF=20000, TATN=20000, KZOS=20000, LKOH=20000) port = portfolio.Portfolio(date, 0, positions) tickers = finder.find_zero_turnover_and_weight(port) assert "KAZT" not in tickers assert "KAZTP" in tickers
def test_add_tickers(capsys): date = pd.Timestamp("2018-12-19") positions = dict(KAZT=1, KAZTP=0, CHMF=20000, TATN=20000, KZOS=20000, LKOH=20000) port = portfolio.Portfolio(date, 0, positions) finder.add_tickers(port) captured = capsys.readouterr() assert "МОМЕНТУМ ТИКЕРЫ" in captured.out assert "ДИВИДЕНДНЫЕ ТИКЕРЫ" in captured.out
def make_optimizer(monkeypatch): monkeypatch.setattr(config, "ML_PARAMS", ML_PARAMS) monkeypatch.setattr(config, "TURNOVER_CUT_OFF", 0.0016) date = pd.Timestamp("2018-12-17") positions = dict( KZOS=800, MGNT=0, PIKK=800, MSTT=0, MTLRP=0, GMKN=21, CBOM=0, SNGSP=13000 ) port = portfolio.Portfolio(date, 1000, positions) return optimizer.Optimizer(port, months=11)
def test_remove_tickers(capsys): date = pd.Timestamp("2018-12-19") positions = dict(KAZT=1, KAZTP=0, CHMF=20000, TATN=20000, KZOS=20000, LKOH=20000) port = portfolio.Portfolio(date, 0, positions) opt = optimizer.Optimizer(port, months=11) finder.remove_tickers(opt) captured = capsys.readouterr() assert "БУМАГИ С НУЛЕВЫМ ОБОРОТОМ И ВЕСОМ" in captured.out assert "БУМАГИ С НИЗКИМ ГРАДИЕНТОМ" in captured.out
def test_get_turnover(monkeypatch): monkeypatch.setattr(config, "TURNOVER_CUT_OFF", 0.0012) monkeypatch.setattr(config, "TURNOVER_PERIOD", 21) date = pd.Timestamp("2018-12-18") positions = dict(TATN=20000, KZOS=20000, LKOH=20000) port = portfolio.Portfolio(date, 0, positions) df = finder.get_turnover(port, ("KZOS", "AKRN")) assert isinstance(df, pd.Series) assert df.size == 4 assert df["KZOS"] == pytest.approx(0.986873)
def get_turnover(port, tickers): """Получает фактор оборота для тикеров.""" date = port.date cash = port.shares[CASH] positions = {ticker: port.shares[ticker] for ticker in port.index[:-2]} for ticker in tickers: if ticker not in positions: positions[ticker] = 0 all_tickers_port = portfolio.Portfolio(date, cash, positions) return all_tickers_port.turnover_factor[:-2]
def test_update_data_bad_date(): port = portfolio.Portfolio(date=DATE_OLD, cash=CASH, positions=POSITIONS) with pytest.raises(POptimizerError) as error: pdf.update_data( "test", port.date, port.value[PORTFOLIO], dict(WLMike=1000, Igor=-2000), 1234, ) assert str(error.value) == "В этом месяце данные уже вносились в отчет"
def test_report_new_month(): port = portfolio.Portfolio(date=DATE_NEW, cash=CASH, positions=POSITIONS) pdf.report("test", port, dict(WLMike=10000, Igor=-5000), 4321) df = pdf.read_data("test") date = port.date assert df.index[-1] == date assert df.loc[date, "WLMike"] == pytest.approx(10000) assert df.loc[date, "Igor"] == pytest.approx(-5000) assert df.loc[date, "Value_WLMike"] == pytest.approx(387_550.829_708_377) assert df.loc[date, "Value_Igor"] == pytest.approx(4972.170_292) assert df.loc[date, "Value"] == pytest.approx(392_523) assert df.loc[date, "Dividends"] == pytest.approx(4321)
def test_find_dividends(monkeypatch): monkeypatch.setattr(config, "TURNOVER_CUT_OFF", 0.0022) monkeypatch.setattr(config, "ML_PARAMS", ML_PARAMS) date = pd.Timestamp("2018-12-18") positions = dict(CHMF=20000, TATN=20000, KZOS=20000, LKOH=20000) port = portfolio.Portfolio(date, 0, positions) df = finder.find_dividends(port, 0.02) assert isinstance(df, pd.DataFrame) assert df.shape == (5, 4) assert list(df.columns) == ["DivYield_0", "TURNOVER", "SCORE", "ADD"] assert list(df.index) == ["CHMF", "MTLRP", "MRKV", "MRKP", "LSNGP"] assert df.loc["CHMF", "ADD"] == "" assert df.loc["MTLRP", "ADD"] == "ADD"
def test_find_momentum(monkeypatch): monkeypatch.setattr(config, "TURNOVER_CUT_OFF", 0.0022) monkeypatch.setattr(finder, "feature_params", lambda x: {"days": 252}) date = pd.Timestamp("2018-12-18") positions = dict(TATN=20000, KZOS=20000, LKOH=20000) port = portfolio.Portfolio(date, 0, positions) df = finder.find_momentum(port, 0.02) assert isinstance(df, pd.DataFrame) assert df.shape == (5, 5) assert list(df.columns) == ["Mom12m", "STD", "TURNOVER", "_DRAW_DOWN", "ADD"] assert list(df.index) == ["AKRN", "RTKMP", "BANEP", "KZOS", "CBOM"] assert df.loc["AKRN", "ADD"] == "ADD" assert df.loc["KZOS", "ADD"] == "" assert df.loc["BANEP", "ADD"] == "ADD"
def test_update_data(): port = portfolio.Portfolio(date=DATE_NEW, cash=CASH, positions=POSITIONS) pdf.update_data("test", port.date, port.value[PORTFOLIO], dict(WLMike=1000, Igor=-2000), 1234) df = pdf.read_data("test") assert df.shape == (241, 6) assert df.index[-1] == pd.Timestamp("2018-05-07") assert df.loc["2018-05-07", "WLMike"] == pytest.approx(1000) assert df.loc["2018-05-07", "Igor"] == pytest.approx(-2000) assert df.loc["2018-05-07", "Value_WLMike"] == pytest.approx(384_396.431_074_62) assert df.loc["2018-05-07", "Value_Igor"] == pytest.approx(8126.568_925_380_33) assert df.loc["2018-05-07", "Value"] == pytest.approx(392_523) assert df.loc["2018-05-07", "Dividends"] == pytest.approx(1234)
def test_find_low_gradient(monkeypatch): monkeypatch.setattr(config, "ML_PARAMS", ML_PARAMS) date = pd.Timestamp("2018-12-19") positions = dict( AKRN=563, BANE=236, CHMF=2000, BANEP=1644, KAZT=0, KAZTP=0, KZOS=3400, LKOH=270, TATN=420, MGNT=0, MTLRP=0, ) port = portfolio.Portfolio(date, 0, positions) opt = optimizer.Optimizer(port, months=11) bad_tickers = finder.find_low_gradient(opt) assert len(bad_tickers) == 1
MVID=3260, NMTP=0, PHOR=0, PMSBP=17290, PRTK=7000, RSTIP=87000, RTKM=0, RTKMP=182_600, SNGSP=23500, TTLK=0, UPRO=1_267_000, VSMO=73, ) CASH = 1_548_264 DATE = "2018-04-19" TEST_PORTFOLIO = portfolio.Portfolio(date=DATE, cash=CASH, positions=POSITIONS) def test_drop_small_positions(): df = pdf_lower.drop_small_positions(TEST_PORTFOLIO) index = df.index assert len(df) == pdf_lower.MAX_TABLE_ROWS + 2 assert index[-1] == PORTFOLIO assert df[PORTFOLIO] == pytest.approx(39_226_178) assert index[-2] == OTHER assert df[OTHER] == pytest.approx(6_523_354) assert index[0] == "RTKMP" assert df.iloc[0] == pytest.approx(11_167_816) assert index[-3] == "MVID" assert df.iloc[-3] == pytest.approx(1_310_520)
def test_update_data_bad_investor_name(): port = portfolio.Portfolio(date=DATE_NEW, cash=CASH, positions=POSITIONS) with pytest.raises(POptimizerError) as error: pdf.update_data("test", port.date, port.value[PORTFOLIO], dict(WLMike1=1000), 1234) assert str(error.value) == "Неверное имя инвестора - WLMike1"