Hi All,
I wanted to share a fun little algorithm that I wrote up as a Pipeline exercise, and point out an interesting problem/bug that I found. This is my first algo on Quantopian, and would really welcome any feedback on the structure and syntax of the code (maybe to make it more "pythonic")!
I've made the structure of the code somewhat generic, and contained options to switch to longing the biggest winner by selecting a different Pipeline stream in the 'rebalance(...)' or to increase the basket of longs by adjusting the number of stocks taken from Pipeline. It can also be switched to shorting by replacing the signs in the 'order_target_percent(...)'.
The idea is simple, where the algo would long the biggest 'loser', i.e. the stock with the lowest daily return, from the previous day and hold it for 5 trading days, creating a revolving 5-stock flat-weighted portfolio that swap out one stock every day of the week (E.g. Buy Monday's biggest loser on Tuesday and sell it on the following Tuesday). In theory, this is similar to a contrarian strategy that essentially expects a "rebound" after a big loss for a particular stock. In practice, the strategy's performance is pretty lackluster, as seen in the attached backtest. The fact that there are such limited number of stocks in the portfolio means that the strategy's beta is surprisingly high.
Also, I wanted to point out a problem that I was having with 'get_open_orders()', where sometimes the result is not a collection of tuple(stock, order), but rather a list(stock). This seems to only result when 'order_target_percent(...)' cannot create a order (NOT cannot fill an order) even though the 'can_trade(security)' check is passed, thus creating an open order with no actual order_id associated to it. My guess is that this is due to the target weight being impossible due to portfolio and unit size (as it does occur with Berkshire shares), but it does occur with other shares as well.
I was using a snippet taken from John Fawcett (https://www.quantopian.com/posts/how-can-i-convert-unfilled-limit-orders-to-market-orders) to cancel and re-place my orders at the end of day, and the above erro causes the IDE to throw a 'not iterable' error where it is actually because the output does not match the tuple form (stock, order).
I hope this is useful to somebody and, again, would really like some feedback on how to improve!