99
1010
1111class ReallocationMode (StrEnum ):
12- """Modes for allocating out of and into security sets."""
12+ """Modes for allocating out of and into security sets.
13+
14+ - ``EQUAL_OUT_EQUAL_IN`` sells evenly across ``out_of`` and buys evenly into
15+ ``into``.
16+ - ``PRO_RATA_OUT_EQUAL_IN`` sells in proportion to existing weights and buys
17+ evenly into ``into``.
18+ """
1319
1420 EQUAL_OUT_EQUAL_IN = "equal_out_equal_in"
1521 PRO_RATA_OUT_EQUAL_IN = "pro_rata_out_equal_in"
@@ -30,8 +36,13 @@ class DecisionBase:
3036 form composite decisions.
3137 """
3238
33- def __add__ (self , other : Decision ) -> Decision :
34- """Combine two decisions into a composite decision."""
39+ def __add__ (self : Decision , other : Decision ) -> Decision :
40+ """Combine two decisions into a composite decision.
41+
42+ ``HoldDecision`` acts as the identity element. When either side is a
43+ :class:`HoldDecision`, the other decision is returned. Two composites are
44+ flattened into a single :class:`CompositeDecision`.
45+ """
3546 if isinstance (self , HoldDecision ):
3647 return other
3748 if isinstance (other , HoldDecision ):
@@ -43,7 +54,10 @@ def __add__(self, other: Decision) -> Decision:
4354
4455@dataclass (frozen = True , slots = True )
4556class MakeTradeDecision (DecisionBase ):
46- """Decision returned by :func:`~backtest_lib.engine.decision.trade`."""
57+ """Decision representing an explicit buy or sell instruction.
58+
59+ Use :func:`~backtest_lib.engine.decision.trade` to construct this decision.
60+ """
4761
4862 direction : TradeDirection
4963 qty : int
@@ -57,14 +71,22 @@ def __post_init__(self):
5771
5872@dataclass (frozen = True , slots = True )
5973class CompositeDecision (DecisionBase ):
60- """Decision returned by :func:`~backtest_lib.engine.decision.combine`."""
74+ """Decision containing multiple sub-decisions.
75+
76+ Use :func:`~backtest_lib.engine.decision.combine` to construct this decision
77+ or rely on ``+`` to merge decisions.
78+ """
6179
6280 decisions : tuple [Decision , ...]
6381
6482
6583@dataclass (frozen = True , slots = True )
6684class TargetHoldingsDecision (DecisionBase ):
67- """Decision returned by :func:`~backtest_lib.engine.decision.target_holdings`."""
85+ """Decision targeting discrete share counts for each security.
86+
87+ Use :func:`~backtest_lib.engine.decision.target_holdings` to construct this
88+ decision.
89+ """
6890
6991 target_holdings : Mapping [str , int ]
7092 fill_cash : bool
@@ -73,7 +95,11 @@ class TargetHoldingsDecision(DecisionBase):
7395
7496@dataclass (frozen = True , slots = True )
7597class TargetWeightsDecision (DecisionBase ):
76- """Decision returned by :func:`~backtest_lib.engine.decision.target_weights`."""
98+ """Decision targeting portfolio weights for each security.
99+
100+ Use :func:`~backtest_lib.engine.decision.target_weights` to construct this
101+ decision.
102+ """
77103
78104 target_weights : Mapping [str , float ]
79105 fill_cash : bool
@@ -82,14 +108,21 @@ class TargetWeightsDecision(DecisionBase):
82108
83109@dataclass (frozen = True , slots = True )
84110class HoldDecision (DecisionBase ):
85- """Decision returned by :func:`~backtest_lib.engine.decision.hold`."""
111+ """Decision that leaves the portfolio unchanged.
112+
113+ Use :func:`~backtest_lib.engine.decision.hold` to construct this decision.
114+ """
86115
87116 pass
88117
89118
90119@dataclass (frozen = True , slots = True )
91120class ReallocateDecision (DecisionBase ):
92- """Decision returned by :func:`~backtest_lib.engine.decision.reallocate`."""
121+ """Decision to move exposure from one security set to another.
122+
123+ Use :func:`~backtest_lib.engine.decision.reallocate` to construct this
124+ decision.
125+ """
93126
94127 fraction : float
95128 from_securities : frozenset [str ]
@@ -115,7 +148,9 @@ def reallocate(
115148 fraction: Fraction of holdings to reallocate.
116149 out_of: Securities to reduce positions in.
117150 into: Securities to increase positions in.
118- mode: Allocation mode for distributing sales and buys.
151+ mode: Allocation mode for distributing sales and buys. Accepts
152+ ``"equal_out_equal_in"``, ``"pro_rata_out_equal_in"``, or a
153+ :class:`~backtest_lib.engine.decision.ReallocationMode`.
119154
120155 Returns:
121156 ReallocateDecision describing the move.
@@ -153,8 +188,7 @@ def hold() -> HoldDecision:
153188 """Create a decision that makes no trades.
154189
155190 This is the neutral element of the decision language. When combined with
156- another decision, the other decision is returned unchanged, mirroring
157- :func:`~backtest_lib.engine.decision.combine`.
191+ another decision, the other decision is returned unchanged.
158192 """
159193 return HoldDecision ()
160194
@@ -165,7 +199,8 @@ def target_holdings(
165199 """Create a decision targeting discrete holdings.
166200
167201 This is part of the strategy output language. Use it when your strategy
168- expresses desired positions as share counts.
202+ expresses desired positions as share counts. The holdings keys must be a
203+ subset of the strategy universe; missing securities are treated as zero.
169204
170205 Args:
171206 holdings: Desired integer holdings per security.
@@ -184,7 +219,9 @@ def target_weights(
184219 """Create a decision targeting portfolio weights.
185220
186221 This is part of the strategy output language. Use it when your strategy
187- expresses desired positions as weights that should sum with cash.
222+ expresses desired positions as weights that should sum with cash. The weight
223+ keys must be a subset of the strategy universe; missing securities are
224+ treated as zero.
188225
189226 Args:
190227 weights: Desired weights per security.
@@ -220,10 +257,11 @@ def trade(
220257
221258 This is part of the strategy output language for explicit trades. Use it
222259 when you want a single-buy/sell instruction rather than a target portfolio
223- expressed via :func:`~backtest_lib.engine.decision. target_weights`.
260+ expressed via :func:`~backtest_lib.target_weights`.
224261
225262 Args:
226- direction: Buy or sell direction.
263+ direction: Buy or sell direction. Accepts ``"buy"``, ``"sell"``, or a
264+ :class:`~backtest_lib.engine.decision.TradeDirection`.
227265 qty: Quantity to trade.
228266 security: Security identifier to trade.
229267
0 commit comments