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))
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
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)
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)
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)
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
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)
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)
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)
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())
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)
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])
def test_score_notrunerror(self, metric): strategy = RandomStrategy() with pytest.raises(NotRunError): strategy.score(metric)
def test_notrunerror(self): strategy = RandomStrategy() with pytest.raises(NotRunError): strategy.wealth() with pytest.raises(NotRunError): strategy.history() with pytest.raises(NotRunError): strategy.drawdown() with pytest.raises(NotRunError): strategy.net_exposure() with pytest.raises(NotRunError): strategy.abs_exposure()
def test_history_notrunerror(self): strategy = RandomStrategy() with pytest.raises(NotRunError): # epymetheus.exceptions.NotRunError: Strategy has not been run strategy.history()