Interface ILiveBracketedOrder
- Namespace
- WhalesSecret.TradeScriptLib.API.TradingV1.Orders.Brackets
- Assembly
- WhalesSecret.TradeScriptLib.dll
Bracketed order is a synthetic order type that opens a trading position with a so called working order, and then attempts to close the position with, so called, bracket orders that either lock a profit or a loss. Bracketed order also includes a closing order request that is executed when the position is to be closed "manually", i.e. not relying on the bracket orders. The closing order is a market order with opposite side to the working order.
public interface ILiveBracketedOrder : IAsyncDisposable
- Inherited Members
- Extension Methods
Remarks
The lifecycle of the bracketed order is as follows:
-
ExecutingWorkingOrder: A trading position is open by placing a working order, which is either a limit order or a market order. Only
the working order exists at this moment. It can either be cancelled without any fills, filled partially, or filled in full.
If the working order is cancelled without any fills, the position is closed and the bracketed order is terminated.
If the working order is cancelled after being partially filled, the closing order request has to have the size of this partial fill. The closing order has to be placed on the exchange. After that the position is closed and the bracketed order is terminated.
In both these situations, the state of the bracketed order is set to WorkingOrderCancelled.
If the working order is executed in full, the status is changed to WorkingOrderFilled, the position is open and the requested bracket orders are placed on the exchange. There can be up to MaxBracketOrders bracket orders in total for a single working order. These bracket orders can have any size, provided that the sum of the size on one side (stop-loss or take-profit) does not exceed size of the working order. If placing all bracket orders succeeds, the state of the bracketed order is set to BracketOrdersPlaced and the position continues to be open. Otherwise, if at least one bracket order fails to be placed, all already placed bracket orders are cancelled, the position is closed and the bracketed order is terminated with state set to BracketOrdersFailed.
-
BracketOrdersPlaced: The working order has been filled in full and the bracket orders have been placed on the exchange. The position is open.
The updates to the live bracket orders are being monitored. If any of the bracket orders is filled (including partially), the filled size is removed from the bracket orders
on the other side of the working order. For example, if a stop-loss bracket order is filled, the size of the fill is removed from the highest take profit orders. The fill size
is also removed from the closing order request.
Removing filled size from existing bracket orders requires canceling these orders and potentially replacing them with new orders with altered size. If this operation fails, the position is closed, all bracket orders are cancelled, and the bracketed order is terminated with state set to BracketOrdersFailed.
If all bracket orders are filled, the position is already closed by definition and the bracketed order is terminated with state set to BracketOrdersFilled.
Status Shutdown is set when the ClosePositionAsync(bool, CancellationToken) is called or when the bracketed order is disposed. Finally, UnhandledException is reserved for situations that should never happened as they indicate that an unexpected exception occurred in the bracketed order's lifetime.
If the live bracketed order is not in a terminal state, it should be disposed before shutting down any component that is needed for order cancellation. When live bracketed order is disposed, all bracket orders are attempted to be cancelled. Note, however, that the position is not automatically close on dispose. It is up to the caller to explicitly close the position if that is needed.
Schema of a bracketed order with a buy limit working order a single take-profit bracket order and a single stop-loss bracket order is as follows:
┌────────────────────────┐
│ Sell take-profit order │
┌──────► │ $80,500 │
│ └────────────────────────┘
│ (Locks profit)
┌─────────────────┐ │ │ Buy limit order │ If filled, place │ │ $80,000 │────────────────────────────┼ └─────────────────┘ bracket orders │ (Working order) │ │ ┌────────────────────────┐ │ │ Sell stop-loss order │ └──────► │ $79,600 │ └────────────────────────┘ (Locks loss).
Definitions of bracket orders specify size of individual bracket orders in percent. The actual size of each bracket order is calculated as the given percentage from the size of the cumulative fills of the working order.
When there are brackets on both sides of the working order (i.e. there are stop-loss bracket orders as well as take-profit bracket orders), a fill of a bracket order on one side removes the same amount from the bracket order on the other side. For example, if there are two stop-loss orders and three take-profit orders. If the innermost stop-loss order is filled, the filled amount is removed from the take-profit bracket orders. The amount is removed from the outermost take-profit bracket order first. If the outermost take-profit bracket order is large enough, it is replaced with a smaller take-profit order. Otherwise, it is canceled fully. If it is canceled fully and there is a fill on the stop-loss size was greater than the size of this outermost take-profit order, the next take-profit order is replaced or completely canceled.
Properties
- OrderSide
Side of the working order .
- OrderType
Type of the working order.
- Status
Status of the bracketed order.
- StatusMessage
Human readable information about the current status of the bracketed order.
- SymbolPair
Order's symbol pair.
- TerminatedEvent
Event that is raised when the bracketed order reaches the terminal state.
- WorkingClientOrderId
Client order ID of the working order.
Methods
- ClosePositionAsync(bool, CancellationToken)
Closes the position by sending a closing order request to the exchange.