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

For practice, I created a new algorithm with only these to line in the handle_data section:

    order_target(sid(8554), 1000, style=LimitOrder(limit_price=180))  
    order_target(sid(8554), -1000, style=LimitOrder(limit_price=190))  

My understanding this that this should try to order 1000 shares of SPY if the market is below $180, and try to short 1000 shares if it rises above 190. Furthermore, if the price stays above 190, the position should stay short @ 1000 shares, only changing if it drops below $180.

Well, it does buy 1000 shares to start (from the Transaction Details) as expected with a price well below $180. and it holds those 1000 shares for a while
2011-01-04 - Buys $127,660.00 (1 transactions)

But later it goes crazy.
On May 22, 2015, when the high was only 189.98 (not $190 in the limit order statement) it goes short. Even MORE strangely, it places one order for -1000, but then enters 850 more orders for -2000!

2014-05-22  
- Sells $323,755,406.00 (851 transactions)  

Then starting on October 12 (a Sunday!) it buys 1699000 shares 28 times! The next few months it continues buying & selling millions of shares (billions of $). Finally in December, 2104, the transactions stop completely. At this point SPY is trading consistently above $200.

What in the world is happening???

3 responses

Have you tried running in minutely mode? I'm not sure how daily mode affects the order execution logic. Daily mode orders execute overnight (this is why you're seeing orders on Sunday 10/12/14... it's the order for Monday 10/13 but it's executing at 0:00 GMT aka 8pm EST the night before). It's possible that limit orders don't jive with daily mode.

As for ordering multiple times in a row, that's because you only have one condition parameter but no limit on leverage or existing positions.

Hope that helps

As Joe says.

Also, you can use get_open_orders:

Thanks!

get_open_orders seems to have worked. I added a few lines of code:

    open_orders = get_open_orders()  
    if open_orders:  
        pass  
    else:  

in front of the orders and that solved the problem.