Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Getting get_open_orders() to work

Hey guys I am doing an algorithm, and I cannot get_open_orders to work. I have tried to put it several places.

I am also having trouble maintaining my leverage. How can I set a max_leverage?

4 responses

Hey Quantdog,
When using get_open_orders() you should pass the sid as an argument and only skip the sids with open orders. Also, I'm pretty sure that your leverage is high because you are not accounting for the stocks that you own but aren't in context.buys or context.sells. That would mean 90% of the account is split up over the stocks in context.buys, but you already have an unknown % of the account allocated.

I took a quick stab at it, the leverage still creeps around but it does help. I'm sure there's a more robust way to go about it though

David

I think I am confused at what my algorithm is purchasing. I wanted to use my handle data method, to find stocks. Once it found these stocks it would make a purchase through the rebalance everyday. I was experimenting with trying to get different methods to work together.

Could you explain what the holdings are doing to the algorithm? I am confused to how you limited the leverage.

Sure, consider a simple scenario where there is just two stocks A and B.

Day 1:
Stock A: RSI = 24, ema25 = 50, ema50 = 45
Stock B: RSI = 26, ema25 = 50, ema50 = 45

In this situation you would buy stock A, and do nothing with stock B. Your algorithm would allocate 90% of the account to stock A.

Day 2:
Stock A: RSI = 26, ema25 = 50, ema50 = 45
Stock B: RSI = 24, ema25 = 50, ema50 = 45

On this day you don't want stock A, but you do want stock B. Since stock A's RSI is between 25 and 50, it doesn't get sold and it doesn't make it into context.buy. The allocation to B would be 0.9 / len(context.buy), which is 90% of the account again, even though you already have a 90% stake in A from day 1.

The holdings variable is to take this situation into account. It counts how many open positions are currently held (after subtracting out the ones just sold). It would see that I already own A and add 1 to the count variable so that stock B is given 45% instead. This is still wrong because now you have 135% of the account invested, but that's better than the 180% it would have been.

There are several ways to handle this, here's a few that come to mind.

  • Put a ceiling on the allocation to any single name, I would avoid any situation where 90% of your account can end up in one stock
  • Recalculate the % allocations for current holdings because they drift. Use the new numbers when figuring out what to allocate to any new buys.
  • Re-weight current holdings when their allocations are much higher than what you can give to new stocks in your buy list.
  • Take profits and/or cut losers if you need to make room for new stocks. Or simply don't open new positions until capital frees up.

UPDATE: I forgot to mention that I also changed the length of the price series passed to the EMA functions. EMA needs more days than the lookback window in order for the exponential weighting to work correctly. Using just the lookback is basically the same as a simple moving average.

So I changed my algorithm around a bit. I added an SPY and VXX constraint, not sure if it helps at all. It backtested better without those conditions.
But I made it so it buys and sells weekly. I also changed some of the fundamentals around because the count was too low.

Overall, this is much better algorithm, and I am not exceeding leverage.