Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Newbie looking for some help

Hey Quant Community,

Been lurking for a couple months now. I'm looking to get some help on an algo idea for more longer term investing. It's been backtested to 2007 and does a good job of hedging against down markets. The algo is a blend of:
- 30% tech
- 30% Materials
- 20% finance
- 20% pharma. The portfolio rebalances every 150 days.

I'm very new to Python (and programming in general) so I've only thrown bits together at this point from other algos I've seen. I'm looking to get help on the following:

  1. When the portfolio rebalances, I'd like to take realized profits and reinvest into the portfolio with the correct percentages as above. Could use some guidance on what that might look like?

  2. Need help understanding how to write in capital injection on a monthly basis with the algo purchasing accordingly. (ie. on the 30th of every month, add $10k & distribute based on above %s)

  3. Just some general looking at what I've written so far and get some feedback. Again, very new to this so feedback is much appreciated!

As a side note, this isn't quite an algo but more so just general stock picking. I'd like to try and use it as a stepping stone to some more advanced stuff. Thanks !

1 response

When the portfolio rebalances, I'd like to take realized profits and reinvest into the portfolio with the correct percentages as above. Could use some guidance on what that might look like?

calling order_target_percent for old and new positions should do what you want: It's already used in your code as far as I can see.

Need help understanding how to write in capital injection on a monthly basis with the algo purchasing accordingly. (ie. on the 30th of every month, add $10k & distribute based on above %s)

It's not possible as far as I know. At least increasing context.portfolio.cash has no effect on the amount of cash used by the algo.
Perhaps cash injection can be implemented in a hackish way using custom slippage model.

Just some general looking at what I've written so far and get some feedback. Again, very new to this so feedback is much appreciated!

OK, here are my suggestions:
- use symbol(ticker) instead of sid(). This will increase readability of your code.
- check quality of your code with pylint (http://www.pylint.org/) tool. Currently pylint rate for your code is -36.89. Make it at least 9 and your code will look much better.
- use schedule_function API to rebalance: https://www.quantopian.com/help#ide-schedulefunction
- do you really need custom slippage? use custom slippage models https://www.quantopian.com/help#ide-slippage if you do.
- get rid of has_open_orders. it's useless considering your rebalance period

hope it helps.

Ed