|
| 1 | +import unittest |
| 2 | +from datetime import datetime |
| 3 | +from unittest.mock import patch, MagicMock |
| 4 | +from investing_algorithm_framework.domain import BacktestReport |
| 5 | + |
| 6 | +from investing_algorithm_framework import get_calmar_ratio |
| 7 | + |
| 8 | + |
| 9 | +class TestGetCalmarRatio(unittest.TestCase): |
| 10 | + |
| 11 | + def setUp(self): |
| 12 | + # Generate mocked equity curve: net_size over time |
| 13 | + self.timestamps = [ |
| 14 | + datetime(2024, 1, 1), |
| 15 | + datetime(2024, 1, 2), |
| 16 | + datetime(2024, 1, 3), |
| 17 | + datetime(2024, 1, 4), |
| 18 | + datetime(2024, 1, 5), |
| 19 | + ] |
| 20 | + |
| 21 | + self.net_sizes = [1000, 1200, 900, 1100, 1300] # Simulates rise, fall, recovery, new high |
| 22 | + |
| 23 | + # Create mock snapshot objects |
| 24 | + self.snapshots = [] |
| 25 | + for ts, net_size in zip(self.timestamps, self.net_sizes): |
| 26 | + snapshot = MagicMock() |
| 27 | + snapshot.created_at = ts |
| 28 | + snapshot.total_value = net_size |
| 29 | + self.snapshots.append(snapshot) |
| 30 | + |
| 31 | + # Create a mocked BacktestReport |
| 32 | + self.backtest_report = MagicMock() |
| 33 | + self.backtest_report.get_snapshots.return_value = self.snapshots |
| 34 | + |
| 35 | + def _create_report(self, total_size_series, timestamps): |
| 36 | + report = MagicMock(spec=BacktestReport) |
| 37 | + report.get_snapshots.return_value = [ |
| 38 | + MagicMock(created_at=ts, total_value=size) |
| 39 | + for ts, size in zip(timestamps, total_size_series) |
| 40 | + ] |
| 41 | + return report |
| 42 | + |
| 43 | + def test_typical_case(self): |
| 44 | + # Create a report with total sizes for a whole year, with intervals of 31 days. |
| 45 | + report = self._create_report( |
| 46 | + [1000, 1200, 900, 1100, 1300], |
| 47 | + [datetime(2024, i, 1) for i in range(1, 6)] |
| 48 | + ) |
| 49 | + ratio = get_calmar_ratio(report) |
| 50 | + self.assertEqual(ratio, 4.8261927891975365) # Expected ratio based on the mock data |
| 51 | + |
| 52 | + def test_calmar_ratio_zero_drawdown(self): |
| 53 | + # Create a report with total sizes for a whole year, with intervals of 31 days, and no drawdowns. |
| 54 | + report = self._create_report( |
| 55 | + [1000, 1200, 1300, 1400, 1500, 1600, 1700, 1800, 1900, 2000], |
| 56 | + [datetime(2024, 1, i) for i in range(1, 11)] |
| 57 | + ) |
| 58 | + ratio = get_calmar_ratio(report) |
| 59 | + self.assertEqual(ratio, 0.0) |
| 60 | + |
| 61 | + def test_calmar_ratio_with_only_drawdown(self): |
| 62 | + # Create a report with total sizes for a whole year, with intervals of 31 days, and no drawdowns. |
| 63 | + report = self._create_report( |
| 64 | + [1000, 900, 800, 700, 600, 500, 400, 300, 200, 100], |
| 65 | + [datetime(2024, 1, i) for i in range(1, 11)] |
| 66 | + ) |
| 67 | + ratio = get_calmar_ratio(report) |
| 68 | + self.assertEqual(ratio, -1.1111111111111112) |
0 commit comments