Strategy

Defined in fynance.strategy

class Strategy(model=None, signal=sign, features=None, cost=None, period=252)[source]

Bases: object

Compose 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)[source]

Run the strategy on a price series, returning a backtest result.

Parameters:
dataPriceSeries or array-like

Price series.

yarray-like, optional

Supervised target for the model (fit on the whole series; for leak-free training use run_walk_forward).

Returns:
BacktestResult
run_walk_forward(data, y, train, test, step=None, purge=0)[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.

Parameters:
dataPriceSeries or array-like

Price series.

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.

Returns:
BacktestResult

Backtest of the concatenated out-of-sample positions.