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. |
remove_locate_from_non_short_order
remove_locate_from_non_short_order(
properties: IOrderProperties,
quantity: float,
holdings_quantity: float,
) -> IOrderProperties
Removes the locate from an order that does not open a short position. A locate belongs only on short sells; brokers reject it on any other order. A position side set in the properties wins over the holdings check. The edit goes into a copy; the given instance is never touched.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
properties
|
IOrderProperties
|
The order properties to check |
required |
quantity
|
float
|
The order quantity; the sign gives the order direction |
required |
holdings_quantity
|
float
|
The current holdings quantity |
required |
Returns:
| Type | Description |
|---|---|
IOrderProperties
|
A copy of the properties without the locate; null when there is nothing to remove. |
unsupported_contingent_orders_shape
unsupported_contingent_orders_shape(
brokerage_model: IBrokerageModel, reason: str
) -> BrokerageMessageEvent
Helper to create the message of a set of contingent orders with a shape not supported by the brokerage
validate_contingent_order
validate_contingent_order(
brokerage_model: IBrokerageModel,
order: Order,
supported_contingency_types: IReadOnlySet[
ContingencyType
],
message: Optional[BrokerageMessageEvent],
supports_combo_orders: bool = True,
supports_multiple_symbols: bool = True,
supports_nesting: bool = True,
maximum_order_count: int = ...,
) -> Tuple[bool, BrokerageMessageEvent]
Validates the contingencies of the given order are of a type supported by the brokerage
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
brokerage_model
|
IBrokerageModel
|
The brokerage model |
required |
order
|
Order
|
The order to validate |
required |
supported_contingency_types
|
IReadOnlySet[ContingencyType]
|
The contingency types supported by the brokerage |
required |
message
|
Optional[BrokerageMessageEvent]
|
If this function returns false, a brokerage message detailing why the order may not be submitted |
required |
supports_combo_orders
|
bool
|
True if combo orders can be part of a set of contingent orders |
True
|
supports_multiple_symbols
|
bool
|
True if the orders in the set can be for different symbols |
True
|
supports_nesting
|
bool
|
True if an order triggered by another can trigger others in turn |
True
|
maximum_order_count
|
int
|
The maximum number of orders in the set |
...
|
Returns:
| Type | Description |
|---|---|
Tuple[bool, BrokerageMessageEvent]
|
True if the order is not a contingent order or all its contingencies are supported. |
validate_contingent_orders_not_supported
validate_contingent_orders_not_supported(
brokerage_model: IBrokerageModel,
order: Order,
message: Optional[BrokerageMessageEvent],
) -> Tuple[bool, BrokerageMessageEvent]
Rejects contingent orders (OCO, OTO, OUO, brackets), for the brokerage models of brokerages which don't support them
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
brokerage_model
|
IBrokerageModel
|
The brokerage model |
required |
order
|
Order
|
The order to validate |
required |
message
|
Optional[BrokerageMessageEvent]
|
If this function returns false, a brokerage message detailing why the order may not be submitted |
required |
Returns:
| Type | Description |
|---|---|
Tuple[bool, BrokerageMessageEvent]
|
False if the order is a contingent order. |
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. |