Skip to content

Commit 559e638

Browse files
authored
Merge pull request #21 from AKKI0511/expand-integration-tests-for-trading
Add integration tests for backtesting pipeline
2 parents 9a67cca + 64d6857 commit 559e638

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

tests/integration/__init__.py

Whitespace-only changes.
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import unittest
2+
import os
3+
import sys
4+
import tempfile
5+
import yaml
6+
import pandas as pd
7+
from unittest.mock import patch
8+
9+
# add src to path
10+
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..", "src")))
11+
12+
from backtest.backtester import simulate_trades, compute_metrics # noqa: E402
13+
from data.processor import DataProcessor # noqa: E402
14+
from main import run_pipeline # noqa: E402
15+
16+
17+
class TestPipelineIntegration(unittest.TestCase):
18+
def setUp(self):
19+
self.df = pd.DataFrame(
20+
{
21+
"Open": [10, 11, 12, 13, 14, 15],
22+
"High": [11, 12, 13, 14, 15, 16],
23+
"Low": [9, 10, 11, 12, 13, 14],
24+
"Close": [10, 11, 12, 13, 14, 15],
25+
"Volume": [100] * 6,
26+
}
27+
)
28+
29+
def test_end_to_end_backtest_metrics(self):
30+
processor = DataProcessor()
31+
processor.pipeline = []
32+
with patch.object(processor, "_clean_data", lambda x: x):
33+
processed = processor.process_data(self.df)
34+
labeled = processor.generate_labels(processed, forward_returns=1, threshold=0.05)
35+
simulated = simulate_trades(labeled)
36+
metrics = compute_metrics(simulated)
37+
self.assertAlmostEqual(metrics["cumulative_return"], 0.4, places=6)
38+
self.assertIn("sharpe_ratio", metrics)
39+
self.assertIn("max_drawdown", metrics)
40+
41+
def test_stop_loss_trigger(self):
42+
df = pd.DataFrame({"Close": [100, 95, 96], "label": [1, 1, 1]})
43+
res = simulate_trades(df, stop_loss_pct=0.05)
44+
self.assertEqual(res["label"].tolist(), [1, 0, 0])
45+
self.assertAlmostEqual(res["equity_curve"].iloc[-1], 0.95)
46+
47+
def test_invalid_config_file(self):
48+
cfg = {"data": {"start_date": "2020-01-01"}}
49+
fd, path = tempfile.mkstemp(suffix=".yaml")
50+
os.write(fd, yaml.dump(cfg).encode())
51+
os.close(fd)
52+
with self.assertRaises(ValueError):
53+
run_pipeline(path)
54+
os.remove(path)
55+
56+
57+
if __name__ == "__main__":
58+
unittest.main()

0 commit comments

Comments
 (0)