def make_pipeline(): universe = QTradableStocksUS() # Variables Seleccionadas Del Dataframe de Fundamentals value = Fundamentals.ebit.latest / Fundamentals.enterprise_value.latest working_capital = Fundamentals.working_capital.latest restricted_cash = Fundamentals.restricted_cash.latest cash_and_cash_equivalents = Fundamentals.cash_and_cash_equivalents.latest goodwill = Fundamentals.goodwill.latest capital_stock = Fundamentals.capital_stock.latest total_assets = Fundamentals.total_assets.latest common_stock = Fundamentals.common_stock.latest free_cash_flow = Fundamentals.free_cash_flow.latest recent_returns = Returns(window_length=RETURNS_LOOKBACK_DAYS, mask=universe) # Winsorized - Variables (SIN ATIPICOS) value_winsorized = value.winsorize(min_percentile=0.05, max_percentile=0.95) working_capital = working_capital.winsorize(min_percentile=0.05, max_percentile=0.95) restricted_cash = restricted_cash.winsorize(min_percentile=0.05, max_percentile=0.95) cash_and_cash_equivalents = cash_and_cash_equivalents.winsorize( min_percentile=0.05, max_percentile=0.95) goodwill = goodwill.winsorize(min_percentile=0.05, max_percentile=0.95) capital_stock = capital_stock.winsorize(min_percentile=0.05, max_percentile=0.95) total_assets = total_assets.winsorize(min_percentile=0.05, max_percentile=0.95) common_stock = common_stock.winsorize(min_percentile=0.05, max_percentile=0.95) free_cash_flow = free_cash_flow.winsorize(min_percentile=0.05, max_percentile=0.95) recent_returns = recent_returns.winsorize(min_percentile=0.05, max_percentile=0.95) # FACTOR COMBINADO combined_factor = ( value_winsorized.zscore() * 0.05 + working_capital.zscore() * 0.55 + restricted_cash.zscore() * 0.2 + cash_and_cash_equivalents.zscore() * 0.01 + goodwill.zscore() * 0.01 + capital_stock.zscore() * 0.1 + total_assets.zscore() * 0.01 + common_stock.zscore() * 0.01 + free_cash_flow.zscore() * 0.01 + recent_returns.zscore() * 0.05) longs = combined_factor.top(TOTAL_POSITIONS // 2, mask=universe) shorts = combined_factor.bottom(TOTAL_POSITIONS // 2, mask=universe) long_short_screen = (longs | shorts) pipe = Pipeline(columns={ 'longs': longs, 'shorts': shorts, 'combined_factor': combined_factor }, screen=long_short_screen) return pipe
def make_pipeline(context): universe = QTradableStocksUS() # Mean Average Multiplier calculation mean_close_50 = SimpleMovingAverage(inputs=[USEquityPricing.close], window_length=50, mask=universe) mean_close_200 = SimpleMovingAverage(inputs=[USEquityPricing.close], window_length=200, mask=universe) mean_average_multiplier = mean_close_200 / mean_close_50 #Sentiment Score calculation bull_m_bear = (SimpleMovingAverage(inputs=[stocktwits.bull_minus_bear], window_length=RETURNS_LOOKBACK_DAYS, mask=universe)) total_messages = (SimpleMovingAverage( inputs=[stocktwits.total_scanned_messages], window_length=RETURNS_LOOKBACK_DAYS, mask=universe)) sentiment_score = total_messages / (bull_m_bear) sentiment_score_winsorized = sentiment_score.winsorize(min_percentile=0.01, max_percentile=0.95, mask=universe) #Returns with lookback calculations recent_returns = Returns(window_length=RETURNS_LOOKBACK_DAYS, mask=universe) recent_returns_zscore = recent_returns.zscore() #Universe and low-high calculations low_returns = recent_returns_zscore.percentile_between(0, 25, mask=universe) high_returns = recent_returns_zscore.percentile_between(75, 100, mask=universe) securities_to_trade = (low_returns | high_returns) & universe #Total score calculation total_score = sentiment_score_winsorized * recent_returns_zscore * mean_average_multiplier pipe = Pipeline(columns={ 'total_score': total_score, }, screen=securities_to_trade) return pipe
def make_pipeline(context): """ A function that creates and returns our pipeline. We break this piece of logic out into its own function to make it easier to test and modify in isolation. In particular, this function can be copy/pasted into research and run by itself. Parameters ------- context : AlgorithmContext See description above. Returns ------- pipe : Pipeline Represents computation we would like to perform on the assets that make it through the pipeline screen. """ # Filter for stocks in the QTradableStocksUS universe. For more detail, see # the documentation: # https://www.quantopian.com/help#quantopian_pipeline_filters_QTradableStocksUS universe = QTradableStocksUS() # Create a Returns factor with a 5-day lookback window for all securities # in our QTradableStocksUS Filter. recent_returns = Returns(window_length=RETURNS_LOOKBACK_DAYS, mask=universe) # Turn our recent_returns factor into a z-score factor to normalize the results. recent_returns_zscore = recent_returns.zscore() # Define high and low returns filters to be the bottom 10% and top 10% of # securities in the QTradableStocksUS. low_returns = recent_returns_zscore.percentile_between(0, 10) high_returns = recent_returns_zscore.percentile_between(90, 100) # Add a filter to the pipeline such that only high-return and low-return # securities are kept. securities_to_trade = (low_returns | high_returns) # Create a pipeline object to computes the recent_returns_zscore for securities # in the top 10% and bottom 10% (ranked by recent_returns_zscore) every day. pipe = Pipeline(columns={'recent_returns_zscore': recent_returns_zscore}, screen=securities_to_trade) return pipe
def make_pipeline(context): # Universe of top 2000 liquid stocks universe = QTradableStocksUS() recent_returns = Returns(window_length=lookback_days, mask=universe) # Convert returns into a factor recent_returns_z_score = recent_returns.zscore() low_returns = recent_returns_z_score.percentile_between(0, 10) high_returns = recent_returns_z_score.percentile_between(90, 100) securities_to_trade = (low_returns | high_returns) pipe = Pipeline(columns={'recent_returns_zscore': recent_returns_z_score}, screen=securities_to_trade) return pipe
def make_pipeline(): # Base universe set to the Q500US universe = Q500US() roe = Fundamentals.roe.latest new_returns = Returns(window_length=5, mask=universe) new_returns = new_returns.zscore() returns_range = new_returns.percentile_between(1, 30) new_volatility = AnnualizedVolatility(mask=universe) new_volatility = new_volatility.zscore() volatility_range = new_volatility.percentile_between(1, 30) pipe = Pipeline(columns={ 'roe': roe, 'returns': returns_range, 'volatility': volatility_range }, screen=universe) return pipe
def make_pipeline(): value = Fundamentals.ebit.latest / Fundamentals.enterprise_value.latest quality = Fundamentals.roe.latest total_revenue = Fundamentals.total_revenue.latest yesterday_close = EquityPricing.close.latest yesterday_volume = EquityPricing.volume.latest working_capital_per_share = Fundamentals.working_capital_per_share.latest forward_dividend_yield = Fundamentals.forward_dividend_yield.latest peg_ratio = Fundamentals.peg_ratio.latest trailing_dividend_yield = Fundamentals.trailing_dividend_yield.latest sentiment_score = SimpleMovingAverage(inputs=[stocktwits.bull_minus_bear], window_length=2) test_sentiment = (twitter_sentiment.bull_scored_messages.latest / twitter_sentiment.total_scanned_messages.latest) universe = QTradableStocksUS() #----------------------------------------------------------------- recent_returns = Returns(window_length=RETURNS_LOOKBACK_DAYS, mask=universe) recent_returns_zscore = recent_returns.zscore() #---------------------------------------------------------------- value_winsorized = value.winsorize(min_percentile=0.10, max_percentile=0.90) quality_winsorized = quality.winsorize(min_percentile=0.10, max_percentile=0.90) total_revenue_winsorized = total_revenue.winsorize(min_percentile=0.10, max_percentile=0.90) yesterday_close_winsorized = yesterday_close.winsorize(min_percentile=0.10, max_percentile=0.90) yesterday_volume_winsorized = yesterday_volume.winsorize( min_percentile=0.10, max_percentile=0.90) working_capital_per_share_winsorized = working_capital_per_share.winsorize( min_percentile=0.10, max_percentile=0.90) forward_dividend_yield_winsorized = forward_dividend_yield.winsorize( min_percentile=0.10, max_percentile=0.90) peg_ratio_winsorized = peg_ratio.winsorize(min_percentile=0.10, max_percentile=0.90) trailing_dividend_yield_winsorized = trailing_dividend_yield.winsorize( min_percentile=0.10, max_percentile=0.90) sentiment_score_winsorized = sentiment_score.winsorize(min_percentile=0.10, max_percentile=0.90) #--------------------------------------------------------------- combined_factor = ( value_winsorized.zscore() + quality_winsorized.zscore() + total_revenue_winsorized.zscore() + yesterday_volume_winsorized.zscore() + working_capital_per_share_winsorized.zscore() * 2 + forward_dividend_yield_winsorized.zscore() * 2 + peg_ratio_winsorized.zscore() * 2 + trailing_dividend_yield_winsorized.zscore() + ((sentiment_score_winsorized.zscore() + test_sentiment.zscore()) / 2)) #--------------------------------------------------------------- longs = combined_factor.top(TOTAL_POSITIONS // 2, mask=universe) shorts = combined_factor.bottom(TOTAL_POSITIONS // 2, mask=universe) long_short_screen = (longs | shorts) pipe = Pipeline(columns={ 'longs': longs, 'shorts': shorts, 'recent_returns_zscore': recent_returns_zscore, 'combined_factor': combined_factor, 'total_revenue': total_revenue, 'close': yesterday_close, 'volume': yesterday_volume, }, screen=long_short_screen) return pipe
def make_pipeline(): """ A function that creates and returns our pipeline. We break this piece of logic out into its own function to make it easier to test and modify in isolation. In particular, this function can be copy/pasted into research and run by itself. Returns ------- pipe : Pipeline Represents computation we would like to perform on the assets that make it through the pipeline screen. """ # The factors we create here are based on fundamentals data and a moving # average of sentiment data universe = QTradableStocksUS() fe_rec = fe.ConsensusRecommendations # 5 pe_ratio = Fundamentals.forward_pe_ratio.latest # 4 sentiment_score = SimpleMovingAverage( # 3 inputs=[stocktwits.bullish_intensity], window_length=5, ) mean_close_10 = SimpleMovingAverage(inputs=[USEquityPricing.close], window_length=10) mean_close_30 = SimpleMovingAverage(inputs=[USEquityPricing.close], window_length=30) percent_difference = (mean_close_10 - mean_close_30) / mean_close_30 # We winsorize our factor values in order to lessen the impact of outliers # For more information on winsorization, please see # https://en.wikipedia.org/wiki/Winsorizing pe_ratio_winsorized = pe_ratio.winsorize(min_percentile=0.05, max_percentile=0.95) sentiment_score_winsorized = sentiment_score.winsorize(min_percentile=0.05, max_percentile=0.95) percent_difference_winsorized = percent_difference.winsorize( # 2 min_percentile=0.1, max_percentile=0.9) recent_returns = Returns(window_length=5) # 6 fq1_eps_cons = PeriodicConsensus.slice('EPS', 'qf', 1) # 1 fq2_eps_cons = PeriodicConsensus.slice('EPS', 'qf', 2) fq1_eps_mean = fq1_eps_cons.mean.latest fq2_eps_mean = fq2_eps_cons.mean.latest estimated_growth_factor = (fq2_eps_mean - fq1_eps_mean) / fq1_eps_mean estimated_growth_factor_windsorized = estimated_growth_factor.winsorize( min_percentile=0.01, max_percentile=0.99) # Here we combine our winsorized factors, z-scoring them to equalize their influence combined_factor = (0.01 * fe_rec.total.latest + pe_ratio_winsorized.zscore() + sentiment_score_winsorized.zscore() + 0.005 * percent_difference_winsorized.zscore() + 0.01 * recent_returns.zscore() + estimated_growth_factor_windsorized.zscore()) # Build Filters representing the top and bottom baskets of stocks by our # combined ranking system. We'll use these as our tradeable universe each # day. longs = combined_factor.top(TOTAL_POSITIONS // 2, mask=universe) shorts = combined_factor.bottom(TOTAL_POSITIONS // 2, mask=universe) # The final output of our pipeline should only include # the top/bottom 300 stocks by our criteria long_short_screen = (longs | shorts) # Create pipeline pipe = Pipeline(columns={ 'longs': longs, 'shorts': shorts, 'combined_factor': combined_factor }, screen=long_short_screen) return pipe
# Importar objetos para tuberias de Quantopian from quantopian.pipeline.factors import Returns # Obtiene los retornos porcentuales totales en los ultimos n dias en el universo especificado columna = Returns(window_length=n, mask=universo) # Obtiene los z-score de una columna ((valor-media)/desviación estandar) columna2 = columna.zscore()
def make_pipeline(): """ A function that creates and returns our pipeline. We break this piece of logic out into its own function to make it easier to test and modify in isolation. In particular, this function can be copy/pasted into research and run by itself. Returns ------- pipe : Pipeline Represents computation we would like to perform on the assets that make it through the pipeline screen. """ # The factors we create here are based on fundamentals data and a moving # average of sentiment data """ value = Fundamentals.ebit.latest / Fundamentals.enterprise_value.latest quality = Fundamentals.roe.latest sentiment_score = SimpleMovingAverage( inputs=[stocktwits.bull_minus_bear], window_length=3, ) universe = QTradableStocksUS() # We winsorize our factor values in order to lessen the impact of outliers # For more information on winsorization, please see # https://en.wikipedia.org/wiki/Winsorizing value_winsorized = value.winsorize(min_percentile=0.05, max_percentile=0.95) quality_winsorized = quality.winsorize(min_percentile=0.05, max_percentile=0.95) sentiment_score_winsorized = sentiment_score.winsorize( min_percentile=0.05, max_percentile=0.95 ) # Here we combine our winsorized factors, z-scoring them to equalize their influence combined_factor = ( value_winsorized.zscore() + quality_winsorized.zscore() + sentiment_score_winsorized.zscore() ) """ # Build Filters representing the top and bottom baskets of stocks by our # combined ranking system. We'll use these as our tradeable universe each # day. qtu = QTradableStocksUS() up_and_down_factor, screen_up_and_down = up_and_down() factor_growth, screen_growth = growth() # Create a Returns factor with a 5-day lookback window for all securities # in our QTradableStocksUS Filter. recent_returns = Returns(window_length=5, mask=qtu) # Turn our recent_returns factor into a z-score factor to normalize the results. recent_returns_zscore = recent_returns.zscore() # Define high and low returns filters to be the bottom 10% and top 10% of # securities in the QTradableStocksUS. low_returns = recent_returns_zscore.percentile_between(0, 10) high_returns = recent_returns_zscore.percentile_between(90, 100) factor_last = 2 * factor_growth + recent_returns_zscore + up_and_down_factor # Add a filter to the pipeline such that only high-return and low-return # securities are kept. securities_to_trade = (low_returns | high_returns) # Create a pipeline object to computes the recent_returns_zscore for securities # in the top 10% and bottom 10% (ranked by recent_returns_zscore) every day. pipe = Pipeline(columns={'Factor': factor_last}, screen=screen_growth & screen_up_and_down) return pipe
def make_pipeline(): universe = QTradableStocksUS() # Variables seleccionadas del dataframe de Fundamentals value = Fundamentals.ebit.latest / Fundamentals.enterprise_value.latest sentiment_score = SimpleMovingAverage( inputs=[stocktwits.bull_minus_bear], window_length=3, ) quality = Fundamentals.roe.latest working_capital = Fundamentals.working_capital.latest restricted_cash = Fundamentals.restricted_cash.latest accumulated_depreciation = Fundamentals.accumulated_depreciation.latest dps_growth = Fundamentals.dps_growth.latest capital_stock = Fundamentals.capital_stock.latest mcap = Fundamentals.market_cap.latest daily_returns = Returns(window_length=2) # Variables (SIN ATIPICOS) value_winsorized = value.winsorize(min_percentile=0.05, max_percentile=0.95) sentiment_score_winsorized = sentiment_score.winsorize(min_percentile=0.05, max_percentile=0.95) quality_winsorized = quality.winsorize(min_percentile=0.05, max_percentile=0.95) working_capital = working_capital.winsorize(min_percentile=0.05, max_percentile=0.95) restricted_cash = restricted_cash.winsorize(min_percentile=0.05, max_percentile=0.95) accumulated_depreciation = accumulated_depreciation.winsorize( min_percentile=0.05, max_percentile=0.95) dps_growth = dps_growth.winsorize(min_percentile=0.05, max_percentile=0.95) capital_stock = capital_stock.winsorize(min_percentile=0.05, max_percentile=0.95) mcap = mcap.winsorize(min_percentile=0.05, max_percentile=0.95) daily_returns = daily_returns.winsorize(min_percentile=0.05, max_percentile=0.95) # FACTOR COMBINADO combined_factor = ( quality_winsorized.zscore() * 0.01 + accumulated_depreciation.zscore() * 0.03 + working_capital.zscore() * 0.05 + dps_growth.zscore() * 0.85 + value_winsorized.zscore() * 0.01 + mcap.zscore() * 0.01 + capital_stock.zscore() * 0.01 + sentiment_score_winsorized.zscore() * 0.01 + restricted_cash.zscore() * 0.01 + daily_returns.zscore( groupby=mstar.company_reference.country_id.latest) * 0.01) longs = combined_factor.top(TOTAL_POSITIONS // 2, mask=universe) shorts = combined_factor.bottom(TOTAL_POSITIONS // 2, mask=universe) long_short_screen = (longs | shorts) pipe = Pipeline(columns={ 'longs': longs, 'shorts': shorts, 'combined_factor': combined_factor }, screen=long_short_screen) return pipe