Skip to content

PositionGroupBuyingPowerModel

QuantConnect.Securities.Positions.PositionGroupBuyingPowerModel

PositionGroupBuyingPowerModel(
    required_free_buying_power_percent: float = 0,
)

Bases: Object, IPositionGroupBuyingPowerModel

Provides a base class for implementations of IPositionGroupBuyingPowerModel

Initializes a new instance of the PositionGroupBuyingPowerModel class

This codeEntityType is protected.

Parameters:

Name Type Description Default
required_free_buying_power_percent float

The percentage of portfolio buying power to leave as a buffer

0

required_free_buying_power_percent

required_free_buying_power_percent: float

Gets the percentage of portfolio buying power to leave as a buffer

This codeEntityType is protected.

equals

equals(obj: Any) -> bool
equals(other: IPositionGroupBuyingPowerModel) -> bool

Signature descriptions:

  • Determines whether the specified object is equal to the current object.

  • Indicates whether the current object is equal to another object of the same type.

Parameters:

Name Type Description Default
obj Optional[Any]

The object to compare with the current object.

None
other Optional[IPositionGroupBuyingPowerModel]

An object to compare with this object.

None

Returns:

Type Description
bool

Depends on the signature used. Case 1: [true if the specified object is equal to the current object; otherwise, false.]; Case 2: [true if the current object is equal to the other parameter; otherwise, false.]

get_contemplated_groups_initial_margin

get_contemplated_groups_initial_margin(
    portfolio: SecurityPortfolioManager,
    contemplated_groups: PositionGroupCollection,
    orders_positions: List[IPosition],
) -> float

Gets the initial margin required for the specified contemplated position group. Used by get_reserved_buying_power_impact to get the contemplated groups margin.

This codeEntityType is protected.

get_hash_code

get_hash_code() -> int

Serves as the default hash function.

Returns:

Type Description
int

A hash code for the current object.

get_initial_margin_required_for_order

get_initial_margin_required_for_order(
    parameters: PositionGroupInitialMarginForOrderParameters,
) -> InitialMargin

Gets the total margin required to execute the specified order in units of the account currency including fees

Parameters:

Name Type Description Default
parameters PositionGroupInitialMarginForOrderParameters

An object containing the portfolio, the security and the order

required

Returns:

Type Description
InitialMargin

The total margin in terms of the currency quoted in the order.

get_initial_margin_requirement

get_initial_margin_requirement(
    parameters: PositionGroupInitialMarginParameters,
) -> InitialMargin

The margin that must be held in order to increase the position by the provided quantity

Parameters:

Name Type Description Default
parameters PositionGroupInitialMarginParameters

An object containing the security and quantity

required

get_maintenance_margin

get_maintenance_margin(
    parameters: PositionGroupMaintenanceMarginParameters,
) -> MaintenanceMargin

Gets the margin currently allocated to the specified holding

Parameters:

Name Type Description Default
parameters PositionGroupMaintenanceMarginParameters

An object containing the security

required

Returns:

Type Description
MaintenanceMargin

The maintenance margin required for the.

get_maximum_lots_for_delta_buying_power

get_maximum_lots_for_delta_buying_power(
    parameters: GetMaximumLotsForDeltaBuyingPowerParameters,
) -> GetMaximumLotsResult

Get the maximum market position group order quantity to obtain a delta in the buying power used by a position group. The deltas sign defines the position side to apply it to, positive long, negative short.

Parameters:

Name Type Description Default
parameters GetMaximumLotsForDeltaBuyingPowerParameters

An object containing the portfolio, the position group and the delta buying power

required

Returns:

Type Description
GetMaximumLotsResult

Returns the maximum allowed market order quantity and if zero, also the reason.

get_maximum_lots_for_target_buying_power

get_maximum_lots_for_target_buying_power(
    parameters: GetMaximumLotsForTargetBuyingPowerParameters,
) -> GetMaximumLotsResult

Get the maximum position group order quantity to obtain a position with a given buying power percentage. Will not take into account free buying power.

Parameters:

Name Type Description Default
parameters GetMaximumLotsForTargetBuyingPowerParameters

An object containing the portfolio, the position group and the target signed buying power percentage

required

Returns:

Type Description
GetMaximumLotsResult

Returns the maximum allowed market order quantity and if zero, also the reason. Since there is no sense of "short" or "long" on position groups with multiple positions, the sign of the returned quantity will indicate the direction of the order regarding the reference position group passed in the parameters: - quantity > 0: the order should be placed in the same direction as the reference position group to increase it, without changing the positions' signs. - quantity < 0: the order should be placed in the opposite direction as the reference position group to reduce it, using each position's opposite sign.

get_order_fee_in_account_currency

get_order_fee_in_account_currency(
    portfolio: SecurityPortfolioManager,
    position_group: IPositionGroup,
) -> float

Helper function to compute the order fees associated with executing market orders for the specified position_group

This codeEntityType is protected.

get_position_group_buying_power

get_position_group_buying_power(
    parameters: PositionGroupBuyingPowerParameters,
) -> PositionGroupBuyingPower

Gets the buying power available for a position group trade

Parameters:

Name Type Description Default
parameters PositionGroupBuyingPowerParameters

A parameters object containing the algorithm's portfolio, security, and order direction

required

Returns:

Type Description
PositionGroupBuyingPower

The buying power available for the trade.

get_position_group_order_quantity

get_position_group_order_quantity(
    portfolio: SecurityPortfolioManager,
    current_position_group: IPositionGroup,
    current_used_margin: float,
    target_final_margin: float,
    group_unit: IPositionGroup,
    unit_margin: float,
    final_margin: Optional[float],
) -> Tuple[float, float]

Helper method that determines the amount to order to get to a given target safely. Meaning it will either be at or just below target always.

Parameters:

Name Type Description Default
portfolio SecurityPortfolioManager

Current portfolio

required
current_position_group IPositionGroup

Current position group

required
current_used_margin float

Current margin reserved for the position

required
target_final_margin float

The target margin

required
group_unit IPositionGroup

Unit position group corresponding to the current_position_group

required
unit_margin float

Margin required for the group_unit

required
final_margin Optional[float]

Output the final margin allocated for the position group

required

Returns:

Type Description
Tuple[float, float]

The size of the order to get safely to our target.

get_reserved_buying_power_for_position_group

get_reserved_buying_power_for_position_group(
    parameters: ReservedBuyingPowerForPositionGroupParameters,
) -> ReservedBuyingPowerForPositionGroup

Computes the amount of buying power reserved by the provided position group

get_reserved_buying_power_impact

get_reserved_buying_power_impact(
    parameters: ReservedBuyingPowerImpactParameters,
) -> ReservedBuyingPowerImpact

Computes the impact on the portfolio's buying power from adding the position group to the portfolio. This is a 'what if' analysis to determine what the state of the portfolio would be if these changes were applied. The delta (before - after) is the margin requirement for adding the positions and if the margin used after the changes are applied is less than the total portfolio value, this indicates sufficient capital.

Parameters:

Name Type Description Default
parameters ReservedBuyingPowerImpactParameters

An object containing the portfolio and a position group containing the contemplated changes to the portfolio

required

Returns:

Type Description
ReservedBuyingPowerImpact

Returns the portfolio's total portfolio value and margin used before and after the position changes are applied.

has_sufficient_buying_power_for_order

Check if there is sufficient buying power for the position group to execute this order.

Parameters:

Name Type Description Default
parameters HasSufficientPositionGroupBuyingPowerForOrderParameters

An object containing the portfolio, the position group and the order

required

Returns:

Type Description
HasSufficientBuyingPowerForOrderResult

Returns buying power information for an order against a position group.

passes_position_group_specific_buying_power_for_order_checks

passes_position_group_specific_buying_power_for_order_checks(
    parameters: HasSufficientPositionGroupBuyingPowerForOrderParameters,
    available_buying_power: float,
) -> HasSufficientBuyingPowerForOrderResult

Provides a mechanism for derived types to add their own buying power for order checks without needing to recompute the available buying power. Implementations should return null if all checks pass and should return an instance of HasSufficientBuyingPowerForOrderResult with IsSufficient=false if it fails.

This codeEntityType is protected.

to_account_currency

to_account_currency(
    portfolio: SecurityPortfolioManager, cash: CashAmount
) -> float

Helper function to convert a CashAmount to the account currency

This codeEntityType is protected.

unable_to_converge

unable_to_converge(
    current_margin_difference: float,
    last_margin_difference: float,
    group_unit: IPositionGroup,
    portfolio: SecurityPortfolioManager,
    position_group_quantity: float,
    target_margin: float,
    current_margin: float,
    abs_unit_margin: float,
    error: Optional[ArgumentException],
) -> Tuple[bool, ArgumentException]

Checks if the margin difference is not growing in final margin calculation, just making sure we don't end up in an infinite loop. This function was split out to support derived types using the same error message as well as removing the added noise of the check and message creation.

This codeEntityType is protected.