CompositeCost¶
Defined in fynance.backtest
- class CompositeCost(models)[source]
Bases:
objectStack several cost models into one.
Sums the per-step cost of every model in
models(each of which must conform toCostModel), so a singleCompositeCostinstance can be passed tobacktestwherever onecostmodel is expected. Conforms toCostModelitself.- Parameters:
- modelssequence of CostModel
The cost models to stack, in the order their components are merged by
components.
Examples
>>> import numpy as np >>> cost = CompositeCost( ... [ProportionalCost(fee=0.001), HoldingCost(borrow=0.02, period=250)] ... ) >>> w = np.array([[1.0, 0.0], [-0.5, 0.5], [-0.5, 0.5]]) >>> cost(w) array([1.00e-03, 2.04e-03, 4.00e-05])
- __call__(weights)[source]
Return the per-step cost summed over every stacked model.
- components(weights)[source]
Merge the per-step components of every stacked model.
Each model contributes its own
componentsbreakdown (or, if a model does not exposecomponents, its total under a key equal to its class name). Component keys are merged inmodelsorder; when a key already produced by an earlier model recurs, the later occurrence is disambiguated by prefixing it with its owning class name, e.g."transaction"then"MarketImpactCost.transaction". A third collision on the same class+key (e.g. several stackedProportionalCost) is further disambiguated by the model’s index,"ProportionalCost[2].transaction", so no contribution is ever overwritten. The merged values always sum to__call__.