First, welcome to Quantopian!
You have a great start. It's a good practice to test out your pipeline definition in a notebook first to ensure the data is what you expected. Moreover, define the pipeline as a standalone function so it can then be easily imported into an algo. You did both. While, in theory this works, there are a couple of things to watch out for. One is the symbols
method. In notebooks one can get either a list of symbols or a single symbol by using the one symbols
method. However, in an algo, one must use either the symbols
method (to get a list of assets) or the symbol
method (to get a single asset). So, to migrate the pipeline definition to an algo in this case, change symbols
to symbol
. Then it will work as expected.
I have attached an algo based on a template which I frequently use for a long only strategies. It uses python sets to easily code which groups of assets to open, hold, and close. It also uses the order_optimal_portfolio
method instead of order_target_percent
to place orders. This eliminates looping through each asset one wants to open or close. Additionally, one doesn't need to check if an asset can trade or not. This is really the preferred way to place market orders.
Also, it's fastest to get all the data using pipeline and not with data.history
. Unless one wants minute data (which is only available using data.history
) use pipeline. I made that change, along with some cosmetic changes (consistent use of 'Fundamentals' instead of also 'Morningstar' in pipeline definition).
Check out the attached algo. Reply if you have any questions.
Good luck.
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.