Here's an algo to maybe use as a starter. It's one approach and I'm sure many of our community members have their own. It's also just some random rules I put together for an example so don't be too critical of the results but rather look at the algo framework.
I believe it does what you wanted:
- Gets stock data for a particular stock(s). The ticker(s) can be entered in line 31.
- Use money from a 'bank account'. Quantopian starts with a fixed amount of cash in a portfolio. The amount is not set in the algo but rather in the backtest. Enter the starting cash in the text box near the upper right corner of the IDE before running a backtest.
- Buy/Sell based upon simple rules. The rules are set up in this algo in the
make_pipeline method. These can be arbitrarily complex.
- Run the algo across a historical time slice. This is often referred to a backtest. The backtest dates are set in the two text boxes near the upper right corner of the IDE before running a backtest.
- Print the outcome of the investment. The backtest page displays a wide variety of metrics about the backtest results. There is a pretty good writeup on it's features in this post https://www.quantopian.com/posts/improved-backtest-analysis .
This is just a framework that I've found helpful to start with. Note that orders can be placed one of two ways: 1) using
order methods or 2) using
optimize methods. One can switch between the two methods by putting the respective name in the schedule function. This was only done to show the two options and also a (not too subtle) encouragement to use the
optimize method going forward. It's much more powerful as one begins to impose constraints. It's also required to enter the contest. Check out the docs https://www.quantopian.com/help#optimize-api .
Of course there can be a lot added to make this algo more robust, and to handle trades in a more complex way, and to do better reporting, and on and on... This was mostly meant to be a concise but real and functional algo which can be built upon.
It would be instructional if others in the community could also post their approaches to an algo framework. There really isn't a specific right way and a lot boils down to personal coding style.
Hope this helps.
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.