|
| 1 | +from unittest import TestCase |
| 2 | +from datetime import datetime, timezone, timedelta, date |
| 3 | +from investing_algorithm_framework.services import get_sharpe_ratio |
| 4 | +from investing_algorithm_framework.domain import PortfolioSnapshot |
| 5 | + |
| 6 | + |
| 7 | +class TestVolatilityMetrics(TestCase): |
| 8 | + |
| 9 | + def test_sharpe_ratio_metrics_calculation(self): |
| 10 | + total_values = [ |
| 11 | + 100000, |
| 12 | + 101154, |
| 13 | + 102949, |
| 14 | + 101090, |
| 15 | + 99487, |
| 16 | + 99936, |
| 17 | + 101026, |
| 18 | + 103974, |
| 19 | + 105320, |
| 20 | + 105384, |
| 21 | + 103333, |
| 22 | + 106602, |
| 23 | + 108397, |
| 24 | + 107243, |
| 25 | + 107373, |
| 26 | + 106666, |
| 27 | + 107756, |
| 28 | + 107307, |
| 29 | + 109615, |
| 30 | + 108846, |
| 31 | + 108589, |
| 32 | + 107372 |
| 33 | + ] |
| 34 | + |
| 35 | + dates = [ |
| 36 | + date(year=2010, month=9, day=10), |
| 37 | + date(year=2010, month=9, day=13), |
| 38 | + date(year=2010, month=9, day=14), |
| 39 | + date(year=2010, month=9, day=15), |
| 40 | + date(year=2010, month=9, day=16), |
| 41 | + date(year=2010, month=9, day=17), |
| 42 | + date(year=2010, month=9, day=20), |
| 43 | + date(year=2010, month=9, day=21), |
| 44 | + date(year=2010, month=9, day=22), |
| 45 | + date(year=2010, month=9, day=23), |
| 46 | + date(year=2010, month=9, day=24), |
| 47 | + date(year=2010, month=9, day=27), |
| 48 | + date(year=2010, month=9, day=28), |
| 49 | + date(year=2010, month=9, day=29), |
| 50 | + date(year=2010, month=9, day=30), |
| 51 | + date(year=2010, month=10, day=1), |
| 52 | + date(year=2010, month=10, day=4), |
| 53 | + date(year=2010, month=10, day=5), |
| 54 | + date(year=2010, month=10, day=6), |
| 55 | + date(year=2010, month=10, day=7), |
| 56 | + date(year=2010, month=10, day=8), |
| 57 | + date(year=2010, month=10, day=12) |
| 58 | + ] |
| 59 | + |
| 60 | + snapshots = [ |
| 61 | + PortfolioSnapshot( |
| 62 | + portfolio_id="test_portfolio", |
| 63 | + total_value=val, |
| 64 | + created_at=datetime.combine(d, datetime.min.time()).replace(tzinfo=timezone.utc) |
| 65 | + ) |
| 66 | + for d, val in zip(dates, total_values) |
| 67 | + ] |
| 68 | + |
| 69 | + sharpe_ratio = get_sharpe_ratio(snapshots, risk_free_rate=0.024) |
| 70 | + self.assertAlmostEqual(sharpe_ratio, 3.386, places=1) |
0 commit comments