Order 将Strategy 和Backtrader 系统的其他部分串联起来。Orders将Strategy中逻辑判断的结果转变成Order信息,并执行。
- Creation
Through Strategy’s methods: buy, sell and close(Strategy) which return an order instance as a reference
- Cancellation
Through Strategy’s method: cancel (Strategy) which takes an order instance to operate on
- Notification
To Strategy method: notify_order (Strategy) which reports an order instance
Order notification
- Issued before the strategy's next method is called
- May (and will) happen several times for the same order with the same or different status during the same next cycle.
An order may be submitted to the broker and be accepted and its execution completed before next will be invoked again.
In this case at least 3 notifications will happen with the following status values:
-
Order.Submitted because the order was sent to the broker
-
Order.Accepted because the order was taken by the broker and awaits potential execution
-
Order.Completed because in the example it was quickly matched and completely filled (which may be the case usually for Market orders)
Notifications may happen even several times for the same status in the case of Order.Partial. This status will not be seen in the backtesting broker (which doesn’t consider volume when matching) but it will for sure be set by real brokers.
Real brokers may issue one or more executions before updating a position, and this group of executions will make up for an Order.Partial notification.
Actual execution data is in the attribute: order.executed which is an object of type OrderData (see below for the reference), with usual fields as size and price
The values at the time of creation are stored in order.created which remains unchanged throughout the lifecycle of an order
Order Status values
The following are defined:
Order.Created: set when the Order instance is created. Never to be seen by end-users unless order instances are manually created rather than through buy, sell and close
Order.Submitted: set when the order instance has been transmitted to the broker. This simply means it has been sent. In backtesting mode this will be an immediate action, but it may take actual time with a real broker, which may receive the order and only first notify when it has been forwarded to an exchange
Order.Accepted: the broker has taken the order and it is in the system (or already in a exchange) awaiting execution according to the set parameters like execution type, size, price and validity
Order.Partial: the order has been partially executed. order.executed contains the current filled size and average price.
order.executed.exbits contains a complete list of ExecutionBits detailing the partial fillings
Order.Complete: the order has been completely filled average price.
Order.Rejected: the broker has rejected the order. A parameter (like for example valid to determine its lifetime) may not be accepted by the broker and the order cannot be accepted.
The reason will be notified via the notify_store method of the strategy. Although this may seem awkward, the reason is that real life brokers will notify this over an event, which may or may not be direclty related to the order. But the notification from the broker can still be seen in notify_store.
This status will not be seen in the backtesting broker
Order.Margin: the order execution would imply a margin call and the previously accepted order has been taken off the system
Order.Cancelled (or Order.Canceled): confirmation of the user requested cancellation
It must be taken into account that a request to cancel an order via the cancel method of the strategy is no guarantee of cancellation. The order may have been already executed but such execution may not have yet notified by the broker and/or the notification may not have yet been delivered to the strategy
Order.Expired: a previously accepted order which had a time validity has expired and been taken off the system
网友评论