CompositeCost

Defined in fynance.backtest

class CompositeCost(models)[source]

Bases: object

Stack several cost models into one.

Sums the per-step cost of every model in models (each of which must conform to CostModel), so a single CompositeCost instance can be passed to backtest wherever one cost model is expected. Conforms to CostModel itself.

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 components breakdown (or, if a model does not expose components, its total under a key equal to its class name). Component keys are merged in models order; 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 stacked ProportionalCost) is further disambiguated by the model’s index, "ProportionalCost[2].transaction", so no contribution is ever overwritten. The merged values always sum to __call__.