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))

 

 

Author