This is a continuation of Last time.
LESSON 10 Managing Orders When using Quantopian's slippage model, not all orders placed will be filled. Unfilled orders remain until they are filled or canceled, so you need to consider this situation.
ʻOrder_target_percent ()` does not consider open orders. The order will be canceled at the close of the day, but if you place an additional order with unfilled orders remaining during the day, you will trade over the original funds (over order).
To avoid overorders, use the get_open_orders ()
function to check for unfilled orders. By passing an instance of the stock as an argument, the open order of the target stock is returned in dictionary type.
In the example below, XTL is long when there are no unfilled orders and it is possible to trade. You can clone the code from here.
def initialize(context):
# Relatively illiquid stock.
context.xtl = sid(40768)
def handle_data(context, data):
# Get all open orders.
open_orders = get_open_orders()
if context.xtl not in open_orders and data.can_trade(context.xtl):
order_target_percent(context.xtl, 1.0)
LESSON 11 Putting It All Together
Recommended Posts