Strategy¶
Defined in fynance.strategy
- class Strategy(model=None, signal=sign, features=None, cost=None, period=252)[source]
Bases:
objectCompose features, a model, a signal mapper and costs into a backtest.
- Parameters:
- modelSignalModel, optional
Object with
fit(X, y)/predict(X). If omitted, the (featured) input is fed straight to the signal mapper (rule-based strategy).- signalcallable, optional
Maps predictions/features to positions. Defaults to
fynance.signal.sign.- featurescallable, optional
Maps the price series to a feature array used as model/signal input. Defaults to the identity (the price series itself).
- costCostModel, optional
Per-step transaction cost model.
- periodint
Annualization factor passed to the summary.
- run(data, y=None, X=None)[source]
Run the strategy on a price series, returning a backtest result.
- Parameters:
- dataPriceSeries or array-like
Price series (used for the P&L).
- yarray-like, optional
Supervised target for the model (fit on the whole series; for leak-free training use
run_walk_forward).- Xarray-like, optional
Precomputed feature matrix aligned with
data(rows = time). When given it replacesfeatures(prices)— use it to feed exogenous / regime / multi-venue inputs the price-only featurizer cannot build.
- Returns:
- BacktestResult
- run_walk_forward(data, y, train, test, step=None, purge=0, X=None)[source]
Walk-forward run: refit per window on train only, predict on test.
The model and features are fit on each train slice only; out-of-sample positions are stitched together and backtested. Strictly no-lookahead. This per-window refit is the rolling-NN pattern when
modelis a net.- Parameters:
- dataPriceSeries or array-like
Price series (used for the P&L).
- yarray-like
Supervised target aligned with
data.- train, testint
Train and test window lengths.
- stepint, optional
Roll step (defaults to
test).- purgeint
Embargo removed at the train/test boundary.
- Xarray-like, optional
Precomputed feature matrix aligned with
data(rows = time). When given, each window slicesX[train]/X[test]instead of featurizing the price slices — the way to feed exogenous / regime / multi-venue features.Xmust be causal and index-aligned.
- Returns:
- BacktestResult
Backtest of the concatenated out-of-sample positions.