Constraints
qcs.Constraint
Bases: BaseModel
Defines a general constraint for portfolio assets. It is not specific to any given snapshot. Used as input for snapshot.build_portfolio_constraints()
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
type
|
Literal['limit_min', 'limit_max', 'target']
|
The constraint type. |
required |
value
|
float
|
The constraint value. |
required |
filter
|
Callable[[Asset], bool]
|
Function to filter applicable assets. Receives an Asset object as argument and returns a boolean. |
required |
exposure
|
Callable[[Asset], float] | float = 1
|
Function to calculate the exposure of an asset, or a fixed exposure value. Default is 1. If given a function: This function receives an Asset object as argument and should return a float or None. If it returns None, then exposure=0 will be used for that position |
required |
asset_exposure_type
|
Literal['weight', 'abs_weight', 'threshold']
|
The type of asset exposure. Defaults to "weight". |
required |
label
|
str
|
Constraint label, default is "". |
required |
Examples:
>>> Constraint(
... type="limit_min",
... value=0.1,
... filter=lambda asset: asset.type == "EQT",
... exposure=lambda asset: asset.othernum
... )
qcs.OptimizerConstraint
Bases: BaseModel
Defines a global constraint for the optimizer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
constraint_type
|
Literal['std', 'std=', 'std.bm', 'var', 'cvar', 'mdd', 'cmdd', 'turnover', 'min_return', 'scr']
|
Specifies the type of constraint. Can be one of the following:
- |
required |
target
|
float
|
The target value for the constraint. Required for all constraint types. |
required |
probability
|
float | None
|
The probability level for the constraint. Only applicable for var, cvar, mdd, and cmdd constraint types. |
required |
Examples:
>>> OptimizerConstraint(constraint_type="std", target=0.2)
OptimizerConstraint(constraint_type='std', target=0.2, probability=None)
>>> OptimizerConstraint(constraint_type="var", target=0.05, probability=0.95)
OptimizerConstraint(constraint_type='var', target=0.05, probability=0.95)
qcs.PortfolioConstraint
Bases: BaseModel
Specific portfolio constraint definition. Must be in line with a given snapshot.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
exposure_coefficients
|
List[float]
|
A list of exposure coefficients, where each coefficient must be a float. Must have the same length as positions. Typically exposure is 1 or 0, but can be any number, including nan for subportfolio constraints. |
required |
limit_min
|
float = 0.0
|
The minimum limit. Defaults to 0.0 |
required |
limit_max
|
float = 1.0
|
The maximum limit. Defaults to 1.0 |
required |
label
|
str = ""
|
Constraint label. Not used in calculations. Defaults to "". |
required |
asset_exposure_type
|
Literal["weight", "abs_weight", "threshold"] = "weight"
|
The type of asset exposure. Defaults to "weight". weight: check if weights ⋅ exposure_coefficients are in [limit_min, limit_max] abs_weight: check if abs(weights) ⋅ exposure_coefficients are in [limit_min, limit_max] threshold: check if weights[weights > limit_min] ⋅ exposure_coefficients are < limit_max |
required |
Examples:
>>> positions = [
... {"ticker": "MSFT", "allocation": 200.0},
... {"ticker": "AAPL", "allocation": 100.0},
... ]
>>> snapshot = AllocationSnapshot(currency="EUR", positions=positions)
>>> portfolio_constraint = PortfolioConstraint(
... limit_min=0.5, limit_max=0.7, exposure_coefficients=[1.0, 0.0]
... )
qcs.PositionConstraint
Bases: BaseModel
Defines a position level constraint for a portfolio. Used as input for snapshot.apply_position_constraints()
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
type
|
The constraint type. |
required | |
filter
|
Callable[[Asset], bool]
|
Function to filter applicable assets. Receives an Asset object as argument and returns a boolean. |
required |
value
|
float | None
|
The constraint value. Used and required only for type="turnover" |
required |
Examples:
>>> # Locks all EQT positions
>>> PositionConstraint(type="lock", filter=lambda asset: asset.type == "EQT")
>>> # All positions can be long/short
>>> PositionConstraint(type="long_short", filter=lambda _asset: True)
>>> # Sets the max turnover to 10% for each USA asset
>>> PositionConstraint(
... type="turnover", value=0.1, filter=lambda asset: asset.country == "USA"
... )