Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Overselling or keeping track of number of shares problem

In this test, on 2017-08-08 (second day), it bought 100 shares each of UGAZ and DGAZ, but eventually sold:
100 shares of UGAZ (this is ok)
173 (-27, -73, -73) shares of DGAZ (not ok...)
Then proceeded to buy 246 shares of DGAZ at the market open of the next day. It was supposed to only buy 100.

I suspect something is wrong with the counting/tracking of shares, but I cannot see it. I also have the "long only" setting which seems to not trigger an error during the backtest.

The idea with this algo is to buy both stocks at the market open and sell each position with a trailing stop, or at the end of the day, whichever happens first. Mostly an experiment/curiosity with Robinhood/algorithm. This is my first attempt here, and with the python language.

After that gets sorted, I will be looking into confirming trade prices/quantities for an accurate log. Presently the log is terribly off.

2 responses

in handle_data() you need to check for get_open_orders(stock) before placing the order, and for that matter, just make a habit of doing it everywhere. Since if you execute an order before the last one was filled, it'll send two orders.

To add to Viridian, one trick that I use is that I create an empty list under initialize called context.owned.

When I loop to see if criteria is met to buy, and I buy a security, I .append to context.owned.
When I loop to see if criteria is met to sell, I loop through the context.owned list, and if it sells, I .remove it.
Additionally, I have checks in place throughout my algo to cross check between context.portfolio.positions and my list, context.owned.

Let me know if you'd like any sample code for what I referenced above.