Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Check if there is an open order before order is placed if not ignore

I am trying to check if there is an open order for a security before the order is placed.
I seem to be able to get the security from context.security.symbol but having issues comparing it to open_orders.

 open_orders = get_open_orders()  
            if open_orders.symbol== context.security  
             #ignore order  
4 responses

Hi JC,

Thanks for posting. get_open_orders() with no arguments returns a dictionary of all your open orders, keyed by sid. You can get just the orders for a particular equity by passing its sid as an argument. That returns a list of the open orders for that equity. After that you can just check that this list is empty before placing your order.

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.

So if you wanted to check for open orders of specific_sid before ordering, you would do:

if len(get_open_orders(specific_sid)) == 0:  
    # place order  

Thank you Nathan I was able to add this to my check

for stock in context.results.index:  

        #check in stock_traded for stock  
        Stock_traded = {}  
        stock_TX =stock in Stock_traded.values()  
        order_check=len(get_open_orders(stock))  
        Stock_traded.update({'Stock': stock})  

        if ((stock_TX is False) & (order_check ==0)):  

context.results.index is unknown, how to find context. Results?

context.security =symbol('CASH, EUR, USD')
def handle_data(context, data):
orderId = order(context.security, 10)

for stock in context.results.index: