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

HI all,

I am confused what was wrong form me, as the following code that why cannot close all open position ?
After do the order function , I recheck position again, But is still exist!

def close_orders(context,data):

    for sec in context.sids:  
        order_share = context.portfolio.positions[sec].amount  
        print("open order_share=",sec.symbol, " share=",order_share)

        if order_share > 0:  
            order(sec,-order_share )  
        elif order_share < 0:  
            order(sec,order_share  )    

    #check all close order success  
    for sec in context.sids:  
        order_share = context.portfolio.positions[sec].amount  
        print("check order_share=",sec.symbol," share=",order_share)

Logs message:

2005-05-12PRINT('open order_share=', u'XLF', ' share=', 0)
2005-05-12PRINT('open order_share=', u'XLE', ' share=', 303)
2005-05-12PRINT('open order_share=', u'XLU', ' share=', -47)
2005-05-12PRINT('open order_share=', u'XLK', ' share=', 0)
2005-05-12PRINT('open order_share=', u'XLB', ' share=', 0)
2005-05-12PRINT('open order_share=', u'XLP', ' share=', 2816)
2005-05-12PRINT('open order_share=', u'XLY', ' share=', 0)
2005-05-12PRINT('open order_share=', u'XLI', ' share=', 0)
2005-05-12PRINT('open order_share=', u'XLV', ' share=', 710)
2005-05-12PRINT('check order_share=', u'XLF', ' share=', 0)
2005-05-12PRINT('check order_share=', u'XLE', ' share=', 303)
2005-05-12PRINT('check order_share=', u'XLU', ' share=', -47)
2005-05-12PRINT('check order_share=', u'XLK', ' share=', 0)
2005-05-12PRINT('check order_share=', u'XLB', ' share=', 0)
2005-05-12PRINT('check order_share=', u'XLP', ' share=', 2816)
2005-05-12PRINT('check order_share=', u'XLY', ' share=', 0)
2005-05-12PRINT('check order_share=', u'XLI', ' share=', 0)
2005-05-12PRINT('check order_share=', u'XLV', ' share=', 710)

2 responses

I know what was wrong for me , that the order method will effect portfolio.position in next trading day!

Hi Devon,

As a suggestion, I'd recommend to use order_target, order_target_value, or order_target_percent to close out your positions. And in general for any ordering call. Using the simple order() function will blindly buy or short your position each bar, without any regard for open orders or your portfolio size.

Take a look at this below, I changed order to order_target

def close_orders(context,data):

    for sec in context.sids:  
        order_share = context.portfolio.positions[sec].amount  
        print("open order_share=",sec.symbol, " share=",order_share)

        if order_share > 0:  
            order_target(sec, 0)  
        elif order_share < 0:  
            order_target(sec,0)    

    #check all close order success  
    for sec in context.sids:  
        order_share = context.portfolio.positions[sec].amount  
        print("check order_share=",sec.symbol," share=",order_share)  

and here are the logs

2008-01-04PRINT('open order_share=', u'AAPL', ' share=', 50)
2008-01-04PRINT('open order_share=', u'AA', ' share=', 50)
2008-01-04PRINT('check order_share=', u'AAPL', ' share=', 50)
2008-01-04PRINT('check order_share=', u'AA', ' share=', 50)
2008-01-04PRINTclosing orders
2008-01-04PRINT('open order_share=', u'AAPL', ' share=', 0)
2008-01-04PRINT('open order_share=', u'AA', ' share=', 0)
2008-01-04PRINT('check order_share=', u'AAPL', ' share=', 0)
2008-01-04PRINT('check order_share=', u'AA', ' share=', 0)
2008-01-04PRINTclosing orders
2008-01-04PRINT('open order_share=', u'AAPL', ' share=', 50)
2008-01-04PRINT('open order_share=', u'AA', ' share=', 50)
2008-01-04PRINT('check order_share=', u'AAPL', ' share=', 50)
2008-01-04PRINT('check order_share=', u'AA', ' share=', 50)
2008-01-04PRINTclosing orders
2008-01-04PRINT('open order_share=', u'AAPL', ' share=', 0)
2008-01-04PRINT('open order_share=', u'AA', ' share=', 0)
2008-01-04PRINT('check order_share=', u'AAPL', ' share=', 0)
2008-01-04PRINT('check order_share=', u'AA', ' share=', 0)

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.