Skip to content

BrokerageExtensions

QuantConnect.Brokerages.BrokerageExtensions

Bases: Object

Provides extension methods for handling brokerage operations.

get_order_position

get_order_position(
    order_direction: OrderDirection,
    holdings_quantity: float,
) -> OrderPosition

Gets the position that might result given the specified order direction and the current holdings quantity. This is useful for brokerages that require more specific direction information than provided by the OrderDirection enum (e.g. Tradier differentiates Buy/Sell and BuyToOpen/BuyToCover/SellShort/SellToClose)

Parameters:

Name Type Description Default
order_direction OrderDirection

The order direction

required
holdings_quantity float

The current holdings quantity

required

Returns:

Type Description
OrderPosition

The order position.

order_crosses_zero

order_crosses_zero(
    holding_quantity: float, order_quantity: float
) -> bool

Determines if executing the specified order will cross the zero holdings threshold.

Parameters:

Name Type Description Default
holding_quantity float

The current quantity of holdings.

required
order_quantity float

The quantity of the order to be evaluated.

required

Returns:

Type Description
bool

true if the order will change the holdings from positive to negative or vice versa; otherwise, false.

validate_cross_zero_order

validate_cross_zero_order(
    brokerage_model: IBrokerageModel,
    security: Security,
    order: Order,
    message: Optional[BrokerageMessageEvent],
    not_supported_types: IReadOnlySet[OrderType] = None,
) -> Tuple[bool, BrokerageMessageEvent]

Determines whether an order that crosses zero holdings is permitted for the specified brokerage model and order type.

Parameters:

Name Type Description Default
brokerage_model IBrokerageModel

The brokerage model performing the validation.

required
security Security

The security associated with the order.

required
order Order

The order to validate.

required
not_supported_types IReadOnlySet[OrderType]

The set of order types that cannot cross zero holdings.

None
message Optional[BrokerageMessageEvent]

When the method returns false, contains a BrokerageMessageEvent explaining why the order is not supported; otherwise null.

required

Returns:

Type Description
Tuple[bool, BrokerageMessageEvent]

true if the order is valid to submit; false if crossing zero is not supported for the given order type.

validate_market_on_open_order

validate_market_on_open_order(
    security: Security,
    order: Order,
    get_market_on_open_allowed_window: Callable[
        [MarketHoursSegment], ValueTuple[TimeOnly, TimeOnly]
    ],
    supported_security_types: IReadOnlySet[SecurityType],
    message: Optional[BrokerageMessageEvent],
) -> Tuple[bool, BrokerageMessageEvent]

Validates whether a OrderType.MARKET_ON_OPEN order.

Parameters:

Name Type Description Default
security Security

The security associated with the order.

required
order Order

The order to validate.

required
get_market_on_open_allowed_window Callable[[MarketHoursSegment], ValueTuple[TimeOnly, TimeOnly]]

A delegate that takes a MarketHoursSegment and returns the allowed Market-on-Open submission window as a TimeOnly tuple (start, end).

required
supported_security_types IReadOnlySet[SecurityType]

The set of SecurityType values allowed for OrderType.MARKET_ON_OPEN orders.

required
message Optional[BrokerageMessageEvent]

An output BrokerageMessageEvent containing the reason the order is invalid if the check fails; otherwise null.

required

Returns:

Type Description
Tuple[bool, BrokerageMessageEvent]

true if the order may be submitted within the given window; otherwise false.