Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Simple example investment program available to share?

I read the entire getting started guide twice, and I still can’t build the program I want to build.
The tutorial really leaves a lot to be desired.
I’m looking for a program with examples of basic functions:

  • get stock data for a particular stock
    • use money from a ‘bank account’
    • buy the stock based on some kind of simple rule
    • sell based on some kind of simple rule
    • run the algorithm across a historical time slice of a few years with repeated buys and sells
    • print the outcome of the investment

That really shouldn’t be that hard right? It would be greatly appreciated if someone could share a simple example so that I have a foundation to work from.

3 responses

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.

Disclaimer

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.

Here is the same framework but modified to trade a universe of securities rather than pre-defined ones. This is the more common approach. It's also required for algos entered into the contest.

Again this is 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.

Great thank you! Good to have a comprehensive starting point.