mirror of
https://github.com/KhizarImran/backtestingfx.git
synced 2026-08-13 20:18:04 +00:00
feat: strategy data access, sharpe ratio, and correctness fixes
- Trade.pnl now stores net pnl (after exit commission) so per-trade stats are accurate - Position pyclass uses from_py_object to fix deprecation warning - Removed dead AttributeError swallow in engine.rs - Strategy gains self.data, self.index, self.cash, self.equity properties - Broker.cash exposed to Python via pyo3(get) - Sharpe ratio added to Stats (unannualized) - Added examples/sma_cross.py and examples/compare_bt.py - Logic verified against backtesting.py: 34 trades, 29.4% win rate match
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
import pandas as pd
|
||||
from backtestingfx import Backtest, Strategy
|
||||
|
||||
|
||||
class SmaCross(Strategy):
|
||||
fast = 10
|
||||
slow = 50
|
||||
|
||||
def next(self):
|
||||
if self.index < self.slow:
|
||||
return
|
||||
|
||||
closes = [b.close for b in self.data[-self.slow :]]
|
||||
fast_sma = sum(closes[-self.fast :]) / self.fast
|
||||
slow_sma = sum(closes) / self.slow
|
||||
|
||||
if not self.positions:
|
||||
if fast_sma > slow_sma:
|
||||
self.buy(0.1)
|
||||
else:
|
||||
if fast_sma < slow_sma:
|
||||
self.close_all()
|
||||
|
||||
|
||||
df = pd.read_csv("data/EURUSD_1H.csv")
|
||||
|
||||
stats = Backtest(
|
||||
df,
|
||||
SmaCross,
|
||||
cash=10_000,
|
||||
commission=3.5,
|
||||
spread=0.00002,
|
||||
contract_size=100_000,
|
||||
).run()
|
||||
|
||||
print(stats)
|
||||
Reference in New Issue
Block a user