Risk¶
Risk management components.
- class FixedRiskSizer¶
Bases:
PositionSizerFixed-risk position sizer.
- calculate(entry, stop_loss, equity, risk, commission_rate=Ellipsis, exchange_rate=Ellipsis, hard_limit=None, unit_batch_size=Ellipsis, units=1)¶
Calculates the position size quantity for the given risk parameters.
Returns zero when no position is riskable, including for a zero exchange rate or equal valid entry and stop-loss prices. Rounds the final quantity down within the risk budget and quantity limits. Inverse futures, perpetuals, and futures spreads use reciprocal-price PnL; inverse options and option spreads retain premium-based sizing.
# Parameters
entry: The entry price. Must be positive for reciprocal-price sizing.
stop_loss: The stop-loss price. Must be positive for reciprocal-price sizing.
equity: The account equity.
risk: The positive risk fraction.
commission_rate: The non-negative commission rate.
exchange_rate: The non-negative rate from instrument quote currency to equity currency. Reciprocal-price sizing uses PnL directly when equity uses the instrument’s base currency; otherwise it values PnL in quote currency at the stop-loss price before conversion.
hard_limit: The optional positive limit for the total quantity.
unit_batch_size: The non-negative unit batch size.
units: The positive number of units to divide the position into.
# Errors
Returns an error if: - A decimal argument is not a decimal.Decimal. - A parameter violates the constraints above. - Decimal arithmetic overflows. - The final size rounds to zero or cannot be represented as a Quantity.
- class PositionSizer¶
Bases:
objectBase class for position sizers.
- calculate(entry, stop_loss, equity, risk, commission_rate=Ellipsis, exchange_rate=Ellipsis, hard_limit=None, unit_batch_size=Ellipsis, units=1)¶
Calculates the position size quantity for the given risk parameters.
# Errors
Returns an error if called. Subclasses override this method.
- instrument¶
Returns the instrument used for position sizing.
- update_instrument(instrument)¶
Updates the instrument used for position sizing.
# Errors
Returns an error if instrument is invalid or its ID differs from the current instrument ID.