|
| 1 | +import numpy as np |
| 2 | +import pandas as pd |
| 3 | +import pytest |
| 4 | + |
| 5 | +from backtest_bay.plot.plot_portfolio import ( |
| 6 | + _calculate_annualized_return, |
| 7 | + _calculate_portfolio_return, |
| 8 | + _calculate_trades, |
| 9 | + _calculate_years, |
| 10 | +) |
| 11 | + |
| 12 | + |
| 13 | +# Tests for _calculate_portfolio_return |
| 14 | +@pytest.mark.parametrize( |
| 15 | + ("stock", "expected_return"), |
| 16 | + [ |
| 17 | + (pd.Series([100]), 0.0), |
| 18 | + (pd.Series([100, 150]), 50.0), |
| 19 | + (pd.Series([200, 100]), -50.0), |
| 20 | + (pd.Series([100, 100]), 0.0), |
| 21 | + (pd.Series([50, 75, 100]), 100.0), |
| 22 | + ], |
| 23 | +) |
| 24 | +def test_calculate_portfolio_return_valid_calculation(stock, expected_return): |
| 25 | + """Check if calculated return equals expected value.""" |
| 26 | + result = _calculate_portfolio_return(stock) |
| 27 | + assert result == expected_return |
| 28 | + |
| 29 | + |
| 30 | +# Tests for _calculate_years |
| 31 | +@pytest.mark.parametrize( |
| 32 | + ("index", "expected"), |
| 33 | + [ |
| 34 | + (pd.to_datetime(["2020-01-01", "2020-12-31"]), 1), |
| 35 | + (pd.to_datetime(["2020-01-01", "2020-07-01"]), 0.5), |
| 36 | + (pd.to_datetime(["2010-01-01", "2020-01-01"]), 10), |
| 37 | + (pd.to_datetime(["2020-01-01", "2020-01-01"]), 0), |
| 38 | + (pd.to_datetime(["2020-01-01 00:00:00", "2020-01-01 12:00:00"]), 0.5 / 365), |
| 39 | + ], |
| 40 | +) |
| 41 | +def test_calculate_years(index, expected): |
| 42 | + result = _calculate_years(index) |
| 43 | + assert np.isclose(result, expected, atol=1e-2) |
| 44 | + |
| 45 | + |
| 46 | +# Tests for _calculate_annualized_return |
| 47 | +@pytest.mark.parametrize( |
| 48 | + ("stock", "expected"), |
| 49 | + [ |
| 50 | + ( |
| 51 | + pd.Series( |
| 52 | + [100, 100, 100], |
| 53 | + index=pd.to_datetime(["2020-01-01", "2020-07-01", "2021-01-01"]), |
| 54 | + ), |
| 55 | + 0, |
| 56 | + ), |
| 57 | + ( |
| 58 | + pd.Series([100, 110], index=pd.to_datetime(["2020-01-01", "2021-01-01"])), |
| 59 | + 10.00, |
| 60 | + ), |
| 61 | + ( |
| 62 | + pd.Series([200, 100], index=pd.to_datetime(["2020-01-01", "2021-01-01"])), |
| 63 | + -50.00, |
| 64 | + ), |
| 65 | + ( |
| 66 | + pd.Series([100, 200], index=pd.to_datetime(["2018-01-01", "2021-01-01"])), |
| 67 | + round(((200 / 100) ** (1 / 3) - 1) * 100, 2), |
| 68 | + ), |
| 69 | + ], |
| 70 | +) |
| 71 | +def test_calculate_annualized_return(stock, expected): |
| 72 | + result = _calculate_annualized_return(stock) |
| 73 | + assert np.isclose(result, expected, atol=1e-1) |
| 74 | + |
| 75 | + |
| 76 | +# Tests for _calculate_trades |
| 77 | +@pytest.mark.parametrize( |
| 78 | + ("shares", "expected"), |
| 79 | + [ |
| 80 | + (pd.Series([10, 10, 10, 10]), 0), |
| 81 | + (pd.Series([0, 10, 0, 10, 0]), 4), |
| 82 | + (pd.Series([0, -10, 0, 0, 0]), 2), |
| 83 | + ], |
| 84 | +) |
| 85 | +def test_calculate_trades(shares, expected): |
| 86 | + result = _calculate_trades(shares) |
| 87 | + assert result == expected |
0 commit comments