Ejemplo n.º 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)
Ejemplo n.º 2
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)
Ejemplo n.º 3
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"])
Ejemplo n.º 4
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)
Ejemplo n.º 5
0
    def test_ts(self):
        universe = make_randomwalk()
        strategy = RandomStrategy().run(universe)
        trades = strategy.trades

        assert_equal(strategy.wealth().values, ts.wealth(trades, universe))
        assert_equal(strategy.drawdown().values, ts.drawdown(trades, universe))
Ejemplo n.º 6
0
def test_init():
    """
    Test if `Wealth(strategy) == strategy.wealth`.
    """
    universe = make_randomwalk()
    strategy = RandomTrader().run(universe)

    assert_result_equal(Wealth(strategy), strategy.wealth)
Ejemplo n.º 7
0
    def test_score(self, metric):
        np.random.seed(42)
        universe = make_randomwalk()
        strategy = RandomStrategy().run(universe)
        result = strategy.score(metric.__name__)
        expected = metric(strategy.trades, universe)

        assert result == expected
Ejemplo n.º 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)
Ejemplo n.º 9
0
    def test_linearity_mul(self, a):
        universe = make_randomwalk()
        strategy = RandomStrategy().run(universe)

        result = a * ts.wealth(strategy.trades, universe)
        expected = ts.wealth([a * t for t in strategy.trades], universe)

        assert_allclose(result, expected)
Ejemplo n.º 10
0
    def test(self):
        np.random.seed(42)
        universe = make_randomwalk()
        strategy = RandomStrategy().run(universe)

        drawdown = ts.drawdown(strategy.trades, universe)

        assert np.min(drawdown) == max_drawdown(strategy.trades, universe)
Ejemplo n.º 11
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()
Ejemplo n.º 12
0
    def test_score(self, metric):
        np.random.seed(42)
        universe = make_randomwalk()
        strategy = RandomStrategy(n_trades=1000).run(universe)
        result = strategy.score(metric.__name__)
        expected = metric(strategy.trades, universe)

        np.testing.assert_allclose(result, expected, equal_nan=True)
Ejemplo n.º 13
0
    def test(self):
        np.random.seed(42)
        universe = make_randomwalk()
        strategy = RandomStrategy().run(universe)

        result = final_wealth(strategy.trades, universe)
        expected = strategy.wealth().iat[-1]

        np.testing.assert_allclose(result, expected)
Ejemplo n.º 14
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
Ejemplo n.º 15
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()
Ejemplo n.º 16
0
    def test_linearity_add(self):
        universe = make_randomwalk()
        s0 = RandomStrategy().run(universe)
        s1 = RandomStrategy().run(universe)

        result = ts.wealth(s0.trades, universe) + ts.wealth(s1.trades, universe)
        expected = ts.wealth(s0.trades + s1.trades, universe)

        assert_allclose(result, expected)
Ejemplo n.º 17
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()
Ejemplo n.º 18
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)
Ejemplo n.º 19
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)
Ejemplo n.º 20
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)
Ejemplo n.º 21
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)
Ejemplo n.º 22
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"]))
Ejemplo n.º 23
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)
Ejemplo n.º 24
0
    def test_load(self):
        np.random.seed(42)
        universe = make_randomwalk()
        strategy = RandomStrategy().run(universe, verbose=False)
        history = strategy.history()

        strategy_load = RandomStrategy().load(history, universe)

        pd.testing.assert_frame_equal(history, strategy_load.history())
        pd.testing.assert_series_equal(strategy.wealth(),
                                       strategy_load.wealth())
Ejemplo n.º 25
0
    def test_zero(self):
        """
        result = 0 if trade is zero
        """
        universe = make_randomwalk()
        strategy = RandomStrategy(min_lot=0, max_lot=0).run(universe)

        result = ts.drawdown(strategy.trades, universe)
        expected = np.zeros_like(result)

        assert_equal(result, expected)
Ejemplo n.º 26
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)
Ejemplo n.º 27
0
    def test_wealth_sanity(self):
        """
        wealth[-1] == sum(pnl)
        """
        np.random.seed(42)
        universe = make_randomwalk()
        strategy = RandomStrategy(max_n_assets=5)

        strategy.run(universe)

        assert np.isclose(sum(strategy.history().pnl),
                          strategy.wealth().values[-1])
Ejemplo n.º 28
0
    def test_run_execution(self):
        strategy = RandomStrategy()
        universe = make_randomwalk()

        np.random.seed(42)
        trades = strategy.run(universe)
        result = [t.close for t in strategy.trades]

        np.random.seed(42)
        expected = [t.execute(universe).close for t in strategy(universe)]

        assert result == expected
Ejemplo n.º 29
0
    def test_load_trades_json(self):
        np.random.seed(42)

        universe = make_randomwalk()
        strategy = RandomStrategy().run(universe, verbose=False)

        strategy_load = RandomStrategy().load_universe(universe)
        strategy_load.load_trades_json(strategy.trades_to_json())

        result = strategy_load.history()
        expect = strategy.history()

        pd.testing.assert_frame_equal(result, expect)
Ejemplo n.º 30
0
 def _get_df_history(self):
     strategy = RandomTrader(seed=42).run(make_randomwalk(42))
     return History(strategy).to_dataframe()