diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a9cce02..0cfad35 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -11,5 +11,11 @@ jobs: steps: - uses: actions/checkout@v4 - uses: dtolnay/rust-toolchain@stable + - uses: actions/setup-python@v5 + with: + python-version: "3.12" - run: cargo check - run: cargo test + - run: python -m pip install . + - run: python -m unittest discover -s "$GITHUB_WORKSPACE/tests" + working-directory: /tmp diff --git a/tests/test_backtest.py b/tests/test_backtest.py new file mode 100644 index 0000000..4fb3813 --- /dev/null +++ b/tests/test_backtest.py @@ -0,0 +1,40 @@ +import unittest + +import pandas as pd + +from backtestingfx import Backtest, Strategy + + +class BuyAndHold(Strategy): + def next(self): + self.buy(1.0) + + +class BacktestTest(unittest.TestCase): + def test_run_returns_stats_from_python_strategy(self): + data = pd.DataFrame( + { + "open": [1.1], + "high": [1.1], + "low": [1.1], + "close": [1.1], + }, + index=pd.to_datetime(["2026-01-01"], utc=True), + ) + + stats = Backtest( + data, + BuyAndHold, + cash=10_000.0, + commission=7.0, + spread=0.0, + ).run() + + self.assertEqual(stats.initial_cash, 10_000.0) + self.assertEqual(stats.final_cash, 9_986.0) + self.assertEqual(stats.num_trades, 1) + self.assertEqual(stats.avg_pnl, -14.0) + + +if __name__ == "__main__": + unittest.main()