Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Need help connecting screener to trading algorithm

I have created a simple stock screener using pipeline in my notebook. Now, I want to somehow integrate (if that's the right word?) into a simple moving average algorithm. My goal is to run each stock that passes through the screener through the algorithm and eventually end up with a trade.

So far I have:
context.pipe = pipeline_output('my_pipe')

def open_positions(context,data):
ma17 = data.history(context.pipe, 'price', 17, '1d').mean()
ma40 = data.history(context.pipe, 'price', 40, '1d').mean()
current_positions = context.portfolio.positions.amount
if (ma17 > ma40) and current_positions == 0:
order_target_percent(stock, 1.0)
This doesn't work whatsoever and I don't know how to find the moving average for each individual stock that passes through the screener.

Any help or sources that can help would be greatly appreciated
Also, I am very new to Quantopian so please, tread lightly.

3 responses

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.

Thank you so much! This helps a lot!

@Dan is it possible to change your p/e ratio screen to be less than the current p/e ratio of the S&P500 or industry average? I would also like to try using the RSI as an oversold of 30 or lower and overbought as 70 or higher as an indicator to enter or exit certain positions with a 14 day look back window and with a 4 hour timeframe.