Sure, to track the open orders in your algorithm you can use the get_open_orders function.
By default it will return all open orders in the algorithm:
# retrieve all the open orders and log the total open amount
# for each order
open_orders = get_open_orders()
# open_orders is a dictionary keyed by sid, with values that are lists of orders.
if open_orders:
# iterate over the dictionary
for security, orders in open_orders.iteritems():
# iterate over the orders
for oo in orders:
message = 'Open order for {amount} shares in {stock}'
message = message.format(amount=oo.amount, stock=security)
log.info(message)
If you want to see the open orders for a specific stock, you can specify the security like: get_open_orders(sid=sid). Here is an example that iterates over open orders in one stock:
# retrieve all the open orders and log the total open amount
# for each order
open_aapl_orders = get_open_orders(context.aapl)
# open_aapl_orders is a list of order objects.
# iterate over the orders in aapl
for oo in open_aapl_orders:
message = 'Open order for {amount} shares in {stock}'
message = message.format(amount=oo.amount, stock=security)
log.info(message)
You can't get the fill price exactly in the backtest, but you can retrieve the cost basis and commissions.
# place an order if we don't own any shares
if context.shares_bought == 0:
context.order_a = order(sid(24), 5)
context.shares_bought = 1
# track the order ID (we need to save it because it gets discarded at the end of each run of handle_data
order_data = get_order(context.order_a)
# get the information when the order is filled
if order_data['status'] == 1:
log.info("Order is filled.")
log.info('Total commission: '+str(order_data['commission']))
log.info('Commission per share: '+str(order_data['commission']/order_data['filled']))
log.info('Cost basis: '+str(context.portfolio.positions[sid(24)].cost_basis))
else:
log.info("Order isn't filled yet.")
log.info('-------------------------------')
While you're working through the bugs and errors in the code, have you tried using the debugger? It's a feature we recently released in the IDE to inspect the code. Check out the announcement (and gif!) and you can read more details in the help doc. Good luck!
Disclaimer
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.