Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Current open position

Is there a way to find out what my current open position is? I am looking to close out any open position at the end of the day. Thanks!

1 response

Hi Amrita,

Just to confirm you're looking to close out all open positions, not just open orders at the end of the day correct?

This is a simple 'purge' algo that I use to close all existing positions in my test IB paper accounts in between algos. This should show you the basic idea of how to get at the list of all your positions from the context.portfolio.positions object and then sell them. Let me know if you have any questions!

Best wishes,
Jess

def num_open_orders():  
    """  
    Return the number of open orders right now.  
    """  
    open_orders = get_open_orders()  
    count_open = 0  
    if open_orders:  
        for _, orders in open_orders.iteritems():  
            for oo in orders:  
                if oo.amount != oo.filled:  
                    count_open += 1  
    return count_open  


def initialize(context):  
    context.sid = sid(13775)  
    context.done_purge = False

def handle_data(context, data):  
    needs_purge = []  
    for psid, pos in context.portfolio.positions.iteritems():  
        if pos.amount != 0 :  
            needs_purge.append(psid)  
    if len(needs_purge) > 0:  
        log.info("Need to purge %s in portfolio." % (len(needs_purge)))  
        ok_to_place = 100 - num_open_orders()  
        if ok_to_place > 0:  
            log.info("Can place %s more orders." % (ok_to_place))  
            for psid in needs_purge:  
                if ok_to_place > 0:  
                    log.info("Purging %s" % (psid.symbol))  
                    order_target(psid, 0)  
                    ok_to_place -= 1  
        else:  
            log.info("Cannot place more orders - 100 orders still open.")  
    else:  
        if not context.done_purge:  
            log.info("Done purging.")  
            context.done_purge = True
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.