Hello,
I'm struggling with where to begin converting the below scanning algorithm from Quantopian to QuantConnect. I would like to use this scanner algorithm every minute to keep an updated list of 10 total stocks to watch for day-trading. Then I would like to long or short some of the 10 stocks on that list depending if a long or short signal shows. I will try to use this migration reference page to convert this from Quantopian to QuantConnect (quantconnect.com/docs/quantopian-migration/quick-reference). Could someone point me in the right direction if this reference page is not the right place to start? And/or tell me if I am trying to build something that cannot be built on QuantConnect?
Thanks!
Sean
"""
Algorithm to log the top gainers and losers every minute
"""
# import pipeline methods
from quantopian.algorithm import attach_pipeline, pipeline_output
from quantopian.pipeline import Pipeline, CustomFactor
# import the built in filters and factors
from quantopian.pipeline.filters import QTradableStocksUS, Q1500US, Q500US, StaticAssets
# import any datasets we need
from quantopian.pipeline.data import USEquityPricing
from quantopian.pipeline.data.morningstar import Fundamentals
# import numpy and pandas
import numpy as np
import pandas as pd
def initialize(context):
"""
Called once at the start of the algorithm.
"""
# Make our pipeline and attach to the algo
# Used to get our universe of stocks and close prices
attach_pipeline(make_stock_pipeline(context), 'my_stocks')
context.yesterdays_data = None
context.minuteCounter = 0
def make_stock_pipeline(context):
"""
Get a list of stocks.
This will be the universe to check for gainers and losers
"""
exchange = Fundamentals.exchange_id.latest
exch_filter = exchange.element_of(['NAS', 'NYS'])
asset_type = Fundamentals.security_type.latest
# Morningstar only has 2 asset types: common and preferred stock.
# All the rest like ETFs would have no data, so we just filter for that and get only stocks.
stock_filter = asset_type.notnull()
mask = exch_filter & stock_filter
return Pipeline(
columns={'yesterdays_close': USEquityPricing.close.latest},
screen=mask)
def rebalance(context, data):
df = pipeline_output('my_stocks')
nasdaq = df.exchange == 'NAS'
nyse = df.exchange == 'NYS'
def before_trading_start(context, data):
# Fetch the pipeline data
pipe_data = pipeline_output('my_stocks')
# Get the universe of stocks as a list
context.stocks = pipe_data.index.tolist()
# Get yesterdays close prices as a series
context.yesterdays_close = pipe_data.yesterdays_close
def handle_data(context, data):
context.minuteCounter += 1
# counter
if context.minuteCounter >= 1:
context.minuteCounter = 0
current_price = (data.current(assets=context.stocks, fields='price'))
# Calculate the percent gain since close yesterday for all the stocks
todays_gain_since_yesterday_close = ((current_price / context.yesterdays_close) - 1) * 100.0
# Get the top 5 gainers and losers for the day
top_gainers = todays_gain_since_yesterday_close.nlargest(5)
top_losers = todays_gain_since_yesterday_close.nsmallest(5)
# Here we log the data but could also do more logic
log.info('top gainers {} top losers {}'.format(top_gainers, top_losers))
Derek Melchin
Hi Sean,
Yes, the reference listed above is a great place to start.
Coarse selection universe data is currently updated once per day. Therefore, creating a scanner like the one above would require:
Best,
Derek Melchin
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 QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect 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. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
S O'Keeffe
Hi Derek!
Thanks so much for your response. I fear the "once per day" is the same problem that partially sent me over to Quantopian from QuantConnect in the first place. I want my universe to effectively be the Wilshire 5000, or essentially all tradable stocks. Applying an ROC indicator to each stock in the Wilshire 5000 would likely not be feasible right? Or maybe I'm wrong about that?
I want to connect this 'scanning algorithm' to feed into the attached backtested code you essentially built for me months ago. When I use the attached backtest code as is, I constantly get timeouts if I go over a few hundred for the number of symbols. So I thought using a 'scanning algorithm' to trim the list down to 10-20 stocks to feed into the attached code would prevent so many timeouts.
Thanks so much!
Sean
Derek Melchin
Hi Sean,
Correct, applying the 3-step process outlined above to hundreds or thousands of securities with a high data resolution will lead to slow execution and timeouts.
Instead of applying the strategy to all the securities in the Wilshire 5000, consider filtering the universe down to a smaller size. We don't currently have the constituent data for indices. Once a universe is selected, we can then set a Scheduled Event with 10-minute interval to evaluate the securities that will trade and place the orders.
Best,
Derek Melchin
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 QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect 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. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
S O'Keeffe
Hi Derek,
Thank you so much as always! I may need to think more creatively. Or maybe this really just won't be possible with QuantConnect. I would like to essentially use the first 5 or so stocks listed on the "Top Stock Gainers Today - Yahoo Finance" as my universe. I think those are most likely to be the stocks that day traders are watching. I can't think of an effective way to select a universe (which can only be done at midnight I believe) that will catch everything that might spike up or down based on news that might come out at 7am. Other than trying to looking at all Nasdaq stocks or all W5000 stocks and picking the biggest movers at the opening bell.
Is there a way to pipe in that list directly from Yahoo finance every 10 minutes or something?
Thanks again
Sean
Derek Melchin
Hi Sean,
To accomplish this, we can save Yahoo's Top Stock Gainers list in a Dropbox file. We can then use a Scheduled Universe Selection to Download the contents of the Dropbox file and specify the universe constituents. See the DropboxUniverseSelectionAlgorithm for reference.
Best,
Derek Melchin
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 QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect 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. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
S O'Keeffe
Awesome thank you Derek! I will check that out
S O'Keeffe
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 QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect 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. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!