Пример #1
0
 def test_linearity_mul(self, a, seed):
     universe = make_randomwalk(seed=seed)
     trade0 = RandomTrader(n_trades=1, seed=seed).run(universe).trades[0]
     tradeA = a * trade0
     result0 = trade0._array_value(universe)
     resultA = tradeA._array_value(universe)
     assert np.allclose(a * result0, resultA)
Пример #2
0
    def test_random(self, seed):
        universe = make_randomwalk(n_bars=100, n_assets=10, seed=seed)
        trade = RandomTrader(n_trades=1, seed=seed).run(universe).trades[0]
        result = trade.final_pnl(universe)
        expected = trade.array_pnl(universe)[-1]

        assert np.allclose(result, expected)
Пример #3
0
 def test_strategy_evaluate(self, MetricClass, seed):
     """
     Test if `strategy.evaluate(metric) == metric.result(strategy)`
     """
     m = MetricClass()
     strategy = RandomTrader(seed=seed).run(make_randomwalk(seed=seed))
     result0 = np.array(m.result(strategy))  # from metric method
     result1 = np.array(strategy.evaluate(m))  # from strategy method
     assert np.equal(result0, result1).all()
Пример #4
0
 def test_linearity_mul(self, a, net, seed):
     universe = make_randomwalk(seed=seed)
     trade0 = RandomTrader(n_trades=1, seed=seed).run(universe).trades[0]
     tradeA = a * trade0
     result0 = trade0.series_exposure(universe, net=net)
     resultA = tradeA.series_exposure(universe, net=net)
     if net:
         assert np.allclose(a * result0, resultA)
     else:
         assert np.allclose(abs(a) * result0, resultA)
Пример #5
0
def test_series_type():
    universe = make_randomwalk()
    strategy = RandomTrader().run(universe)
    wealth = Wealth(strategy)
    series_wealth = wealth.to_series()

    assert isinstance(series_wealth, pd.Series)
Пример #6
0
def test_series_value():
    universe = make_randomwalk()
    strategy = RandomTrader().run(universe)
    wealth = Wealth(strategy)
    series_wealth = wealth.to_series()

    assert np.array_equal(series_wealth, wealth["wealth"])
Пример #7
0
 def test_notrunerror(self, MetricClass):
     """
     Metric is supposed to raise NotRunError when one tries to evaluate it
     for a strategy which has not been run yet.
     """
     m = MetricClass()
     with pytest.raises(NotRunError):
         RandomTrader(seed=42).evaluate(m)
Пример #8
0
def test_init():
    """
    Test if `History(strategy) == strategy.history`.
    """
    universe = make_randomwalk()
    strategy = RandomTrader().run(universe)

    assert_result_equal(History(strategy), strategy.history)
Пример #9
0
    def test_result(self, seed, rate):
        m = self.MetricClass(rate=rate)
        strategy = RandomTrader(seed=seed).run(make_randomwalk(seed=seed))

        result0 = m.result(strategy)
        result1 = m._result_from_wealth(strategy.wealth.wealth)

        assert np.equal(result0, result1).all()
Пример #10
0
def test_init():
    """
    Test if `Wealth(strategy) == strategy.wealth`.
    """
    universe = make_randomwalk()
    strategy = RandomTrader().run(universe)

    assert_result_equal(Wealth(strategy), strategy.wealth)
Пример #11
0
 def test_random(self, rate, n, risk_free_return, seed, init_wealth):
     strategy = RandomTrader(seed=seed).run(make_randomwalk(seed=seed),
                                            budget=init_wealth)
     result = self.MetricClass(
         rate=rate, n=n, risk_free_return=risk_free_return).result(strategy)
     r = AverageReturn(rate=rate, n=n).result(strategy)
     s = Volatility(rate=rate, n=n).result(strategy)
     expected = (r - risk_free_return) / s
     assert result == expected
Пример #12
0
 def test_call(self, MetricClass, seed):
     """
     Test if `metric.result(strategy) == metric(strategy)`.
     """
     m = MetricClass()
     strategy = RandomTrader(seed=seed).run(make_randomwalk(seed=seed))
     result0 = np.array(m.result(strategy))  # from `result` method
     result1 = np.array(m(strategy))  # from __call__
     assert np.equal(result0, result1).all()
Пример #13
0
def test_dataframe_type():
    """
    Test if history.to_dataframe is `pandas.DataFrame`.
    """
    universe = make_randomwalk()
    strategy = RandomTrader().run(universe)
    wealth = Wealth(strategy)
    df_wealth = wealth.to_dataframe()

    assert isinstance(df_wealth, pd.DataFrame)
Пример #14
0
def test_dataframe_index():
    """
    Test if value of history.to_dataframe is correct
    """
    universe = make_randomwalk()
    strategy = RandomTrader().run(universe)
    wealth = Wealth(strategy)
    df_wealth = wealth.to_dataframe()

    assert_index_equal(df_wealth.index, universe.bars, check_names=False)
Пример #15
0
def test_dataframe_value():
    """
    Test if value of history.to_dataframe is correct
    """
    universe = make_randomwalk()
    strategy = RandomTrader().run(universe)
    wealth = Wealth(strategy)
    df_wealth = wealth.to_dataframe()

    assert array_equal(df_wealth["wealth"].values, wealth.wealth)
Пример #16
0
    def test_strategy_attr(self):
        """
        `History.__init__` and `strategy.history` are supposed to give the same results.
        """
        universe = make_randomwalk(seed=42)
        strategy = RandomTrader(seed=42).run(universe)

        result0 = pd.DataFrame(History(strategy))  # from History.__init__
        result1 = pd.DataFrame(strategy.history)  # from strategy.history

        assert_frame_equal(result0, result1)
Пример #17
0
def test_dataframe_columns():
    """
    Test if history.to_dataframe has the expected columns.
    """
    universe = make_randomwalk()
    strategy = RandomTrader().run(universe)
    wealth = Wealth(strategy)
    df_wealth = wealth.to_dataframe()

    assert df_wealth.index.name == "bars"
    assert_index_equal(df_wealth.columns, pd.Index(["wealth"]))
Пример #18
0
 def test_result_from_wealth(self, rate, n, init_wealth):
     """
     `m._result_from_wealth(series_wealth) == m.result(strategy.wealth.wealth)`
     """
     m = self.MetricClass(rate=rate, n=n)
     strategy = RandomTrader(seed=42).run(make_randomwalk(seed=42),
                                          budget=init_wealth)
     series_wealth = strategy.budget + strategy.wealth.wealth
     result = m.result(strategy)
     result_from_wealth = m._result_from_wealth(series_wealth)
     assert np.allclose(result, result_from_wealth)
Пример #19
0
 def _get_df_history(self):
     strategy = RandomTrader(seed=42).run(make_randomwalk(42))
     return History(strategy).to_dataframe()
Пример #20
0
 def test_result(self, seed):
     m = self.MetricClass()
     strategy = RandomTrader(seed=seed).run(make_randomwalk(seed=seed))
     result0 = m.result(strategy)
     result1 = m._result_from_wealth(strategy.wealth.wealth)
     assert result0 == result1
Пример #21
0
 def test_random(self, rate, seed, init_wealth):
     strategy = RandomTrader(seed=seed).run(make_randomwalk(seed=seed),
                                            budget=init_wealth)
     result = self.MetricClass(rate=rate, ).result(strategy)
     expected = np.min(Drawdown(rate=rate).result(strategy))
     assert result == expected
Пример #22
0
 def _get_history(self):
     universe = make_randomwalk(seed=42)
     strategy = RandomTrader(seed=42).run(universe)
     return History(strategy)