I generally subscribe to the bogleheads theory when it comes to investing: buy the market and hold. So Quantopian at first wouldn't seem like a great fit for someone like me; but I was and am very excited to use Quantopian for a buy & hold "strategy." Over the last couple years I've invested in the iShares ETFs which are available commission free via Fidelity. I've had a monthly contribution to the Fidelity account which required me to log in periodically and put my cash to work in these core ETFs. The trouble was that I'd forget or be too busy often times to invest the money I was contributing monthly. When I would log in and be ready to invest the money it would take me a little bit of time to calculate how many of each asset class I should buy to keep my asset allocation in line with my long term target. But with quantopian I can automate all of this!
So I developed an algorithm to look for cash in my account. When there is cash it calculates how many shares of each asset class/ETF it should buy to get to my target asset allocation. I'm an engineer so I wanted to have some "fun" with the algorithm; so instead of blindly placing market orders it places limit orders at the 10 day moving average. This helps protect against sudden price surges, and also helps when above the average by waiting to not buy at the peak. Such a script would minimize my cash drag by investing automatically; and it would minimize the number of transactions I need (cutting costs and taxes) by only buying to keep my target allocation = no selling required!
Unfortunately though Quantopian doesn't allow for you to simulate cash contributions into an account. But for backtesting, I added a function which is called monthly that cuts the current allocation in half to simulate a deposit into the account or an addition of cash. After some playing around I was ready to put the strategy to work with a Robinhood account I opened! But then I had a problem with Quantopian thinking a cash contribution was an investment return which I'll cover in the next post...