Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Selling all of a stock not completing. Bug?

I'm seeing an odd behavior that I do not understand. Within my algorithm, if it is to sell a stock, it sells it all using:

          order_target(stock, 0)  

I track in the log output all of the current positions and I see the amount of a sold stock decrease, but never go to 0. Watch symbol TAR decrease in amount to get stuck at 7834:

2003-05-15rebalance:218INFOExiting our positions on TAR, rank 0.813213528749
2003-05-16handle_data:142INFOCurrent positions: Symbol: EMT and Amount: 41666, Symbol: SFP and Amount: 44697, Symbol: USG and Amount: 47755, Symbol: PHS and Amount: 32657, Symbol: TAR and Amount: 40825,
2003-05-23handle_data:142INFOCurrent positions: Symbol: TPC and Amount: 5725, Symbol: SFP and Amount: 44697, Symbol: TAR and Amount: 40593, Symbol: PHS and Amount: 32657, Symbol: EMT and Amount: 41666,
2003-06-02handle_data:142INFOCurrent positions: Symbol: TPC and Amount: 40350, Symbol: SFP and Amount: 44697, Symbol: TAR and Amount: 31293, Symbol: PHS and Amount: 32657, Symbol: EMT and Amount: 41666,
2003-06-09handle_data:142INFOCurrent positions: Symbol: TPC and Amount: 69375, Symbol: SFP and Amount: 44697, Symbol: TAR and Amount: 22918, Symbol: PHS and Amount: 32657, Symbol: EMT and Amount: 41666,
2003-06-16handle_data:142INFOCurrent positions: Symbol: TPC and Amount: 152950, Symbol: SFP and Amount: 44697, Symbol: TAR and Amount: 7834, Symbol: PHS and Amount: 32657, Symbol: EMT and Amount: 16591,
2003-06-23handle_data:142INFOCurrent positions: Symbol: TPC and Amount: 144870, Symbol: SFP and Amount: 44697, Symbol: TAR and Amount: 7834, Symbol: PHS and Amount: 32657, Symbol: NEU and Amount: 11925,

Am I doing this incorrectly? I'm about to share this algorithm, but would like to make sure it's as bug-free as I can first.

Janene

2 responses

I looked at this for a few minutes, but I don't think I can figure it all the way out without seeing the backtest. If you run a full backtest and look at the orders and transactions, it might explain what's going on.

TAR looks like a very thinly traded stock - sometimes with a daily volume of only 1200. My hunch is that you have orders that are open for days and are filling very slowly, and then you're issuing additional orders on top of the open orders, and that's leading to non-intuitive behavior. You might include a check for open orders, and if you find them, bail out or cancel them.

    # consider the status of filled orders  
    if get_open_orders():  
        log.info("Open orders! Run away!")  
        return  
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.

Hi Dan, thanks for your thoughts. I've opened the algorithm to you. I did end up adding some protection in to make sure I wasn't trying to repeatedly sell the stock, but it did not help. The algorithm does quite poorly right now, and I know the model it is based on does very well, so definitely some issues with my implementation.

Janene