def make_pipeline(): testing_factor1 = operation_ratios.operation_margin.latest testing_factor2 = operation_ratios.revenue_growth.latest testing_factor3 = sentiment.sentiment_signal.latest universe = (Q1500US() & testing_factor1.notnull() & testing_factor2.notnull() & testing_factor3.notnull() ) testing_factor1 = testing_factor1.ranl(mask=universe, method='average') testing_factor2 = testing_factor2.ranl(mask=universe, method='average') testing_factor3 = testing_factor3.ranl(mask=universe, method='average') testing_factor = testing_factor1 + testing_factor2 + testing_factor3 testing_quantiles = testing_factor.quantiles(2) pipe = Pipeline(column={ 'testing_factor': testing_factor, 'shorts': testing_quantiles.eq(0), 'longs': testing_quantiles.eq(1)}, screen=universe) repr(pipe) result = run_pipeline(make_pipeline(), start_date='2015-01-01', end_date='2016-01-01') result.head() assets = result.index.levels[1].unique() pricing = get_pricing(assets, start_date='2014-01-01', end_date='2016-01-01', fields='open_price') len(assets) alphalens.tears.create_factor_tear_sheet(factor=result['testing_factor'], prices=pricing['open_price'], quantiles=3, periods=(3, 10, 30))
def qtus_returns(start_date, end_date): pipe = Pipeline(columns={'Close': USEquityPricing.close.latest}, screen=QTradableStocksUS()) stocks = run_pipeline(pipe, start_date, end_date) unstacked_results = stocks.unstack() prices = (unstacked_results['Close'].fillna(method='ffill').fillna( method='bfill').dropna(axis=1, how='any').shift(periods=-1).dropna()) qus_returns = prices.pct_change()[1:] return qus_returns
def get_returns(start_date, end_date): pipeline = Pipeline( columns={'Close': USEP.close.latest}, screen=QTSU() ) stocks = run_pipeline(pipeline, start, end) unstacked = stocks.unstack() prices = unstacked['Close'].fillna(method='ffill').fillna( method='bfill').dropna(axis=1, how='any') perc_returns = prices.pct_change() return perc_returns
def make_pipeline(): testing_factor = operation_ratios.revenue_growth.latest universe = ((Q1500US) & testing_factor.notnull()) testing_factor = testing_factor.ranl(mask=universe, method='average') pipe = Pipeline(column={'testing_factor': testing_factor}, screen=universe) repr(pipe) result = run_pipeline(make_pipeline(), start_date='2015-01-01', end_date='2016-01-01') result.head() assets = result.index.levels[1].unique() pricing = get_pricing(assets, start_date='2014-01-01', end_date='2016-01-01', fields='open_price') len(assets) alphalens.tears.create_factor_tear_sheet(factor=result['testing_factor'], prices=pricing['open_price'], quantiles=3, periods=(3, 10, 30))
pipe = Pipeline( columns={ 'yersterday_price': price, 'ma_fast': fast_ma, 'ma_slow': slow_ma, #'ema': ema, 'rsi': rsi, 'signal_sell': sell, 'signal_buy': buy }, screen=base_universe) end = (date.today() - timedelta(days=0)).strftime('%Y-%m-%d') start = (date.today() - timedelta(days=100)).strftime('%Y-%m-%d') # print(start,end) my_pipeline_result = run_pipeline(pipe, start, end) # show results of last two days #retro_days = 1 #indecies = [] #for i in range(len(assets)): # indecies.append(-1*(i+1)) # indecies.append(-1*(i+1) - len(assets)*retro_days) #my_pipeline_result.iloc[indecies] # display summarized result # these dates are used as the first index all_dates = my_pipeline_result.index.get_level_values(0) unique_dates = sorted(list(dict.fromkeys(all_dates))) current_date = unique_dates[-1] previous_date = unique_dates[-2]
def make_pipeline(): # Create a reference to our trading universe base_universe = QTradableStocksUS() # get latest closing price close_price = USEquityPricing.close.latest # calculate 3 day MA of bull-minus-bear scores sentiment_score = SimpleMovingAverage( inputs=[stocktwits.bull_minus_bear], window_length=3, ) # Return Pipeline containing close_price and sentiment_score return Pipeline( columns={'close_price': close_price, 'sentiment_score': sentiment_score, } screen=base_universe ) ## Execute the Pipeline pipeline_output = run_pipeline( make_pipeline(), start_date='2013-01-01', end_date='2013-12-31' ) # Display last 10 rows pipeline_output.tail(10)
R = qtus_returns(start, end) assets = R.columns def make_pipeline(): market_cap = Foundamentals.shares_outstanding.latest * USEquityPricing.close.latest book_to_price = 1 / Foundamentals.pb_ratio.latest biggest = market_cap.top(500, mask=QTradableStocksUS()) smallest = market_cap.bottom(500, mask=QTradableStocksUS()) highpb = book_to_price.top(500, mask=QTradableStocksUS()) lowpb = book_to_price.bottom(500, mask=QTradableStocksUS()) universe = biggest | smallest | highpb | lowpb pipe = Pipeline(columns={ 'returns': Returns(window_length=2), 'market_cap': market_cap, 'book_to_price': book_to_price, 'biggest': biggest, 'smallest': smallest, 'highpb': highpb, 'lowpb': lowpb }, screen=universe) return pipe pipe = make_pipeline() results = run_pipeline(pipe, start, end) ###########################################
import matplotlib.gridspec as gridspec import matplotlib.pyplot as plt import pandas as pd from . import plotting from . import performance as perf from . import utils def make_pipeline(): sentiment_factor = sentiment.sentiment_signal.last universe = (Q1500US()&sentiment_factor.not_null()) pipe = Pipeline(columns={'sentiment':sentiment_factor, 'longs':(sentiment_factor>=4), 'shorts':(sentiment_factor<=-2)}, screen = universe) return pipe result = run_pipeline(make_pipeline(),start_date = '2015-05-05',end_date = '2016-05-05') type(result) result.head() len(result) assets = result.index.levels[1].unique() len(assets) pricing = =get_pricing(assets,start_date = '2014-05-05',end_date = '2016-05-05',fields = 'open_price') alphalens.tears.create_factor_tear_sheet(factor = result['sentiment'], prices =pricing, quantiles = 2, periods = (1,5,10))
def __init__(self, df, index, data_pull_results): self.df = df self.index = index self.data_pull_results = run_pipeline(Data_Pull_TVF(), start_date = s_date, end_date = e_date)
# Because of problem with time when taken a lot of data divide time to periods end_full = pd.Timestamp("2017-08-07") period = pd.DateOffset(50) number_of_periods = 1 results = pd.DataFrame() start = end_full - number_of_periods * (period) - (number_of_periods - 1) * pd.DateOffset(1) # In[7]: # Run pipeline start_timer = time() while end_full > start: print 'start', start, 'end', start + period results_period = run_pipeline(history_pipe, start_date=start, end_date=start + period) start += (period + pd.DateOffset(1)) results_period.index.names = ['date', 'security'] results = pd.concat([results, results_period]) end_timer = time() print "Time to run pipeline %.2f secs" % (end_timer - start_timer) # In[8]: results.head() # In[9]: # Sometimes there are duplicated indexis results = results[~results.index.duplicated(keep='first')]
percent_diff_filter = percent_diff > 0 small_price = latest_close < 5 # COMBINING FILTERS NOW .. combo_filter = perc_filter & small_price # OR '|' NOT 'and' / 'or' KEYWORDS def make_pipeline(): return Pipeline(columns={ '30 Day Mean Close': mean_close_30, 'Percent Diff': percent_diff, 'Latest Close': latest_close, 'Percent Diff Filter': percent_diff_filter }, # OR USE COMBINATION FILTER combo_filter screen=percent_diff_filter) # OR '~percent_diff_filter' FOR OPPOSITE OF THE FILTER pipe = make_pipeline() # start_date == end_date, THEREFORE THE SAME DAY result = run_pipeline(pipe, start_date='2017-01-03', end_date='2017-01-03') result.head() # NOW, USING MASKS latest_close = USEquityPricing.close.latest small_price = latest_close < 5 mean_close_30 = SimpleMovingAverage(inputs=[USEquityPricing.close], window_length=30, mask=small_price) mean_close_10 = SimpleMovingAverage(inputs=[USEquityPricing.close], window_length=10, mask=small_price) # mask=val APPLIES A FILTER 1ST BEFORE CALC'ING THIS SMA FACTOR (SAVES COMPUTATIONAL COMPLEXITY) # NOW USE FACTORS (mean_close_30, mean_close_10) HOWEVER # NOW, USING CLASSIFIERS from quantopian.pipeline.data import morningstar from quantopian.pipeline.classifiers.morningstar import Sector
# In[5]: pipe = Pipeline(columns={ 'Market Cap': morningstar.valuation.market_cap.latest.quantiles(5), 'Industry': morningstar.asset_classification.morningstar_industry_group_code.latest, 'Financial Health': morningstar.asset_classification.financial_health_grade.latest }, screen=universe) # In[6]: res = run_pipeline(pipe, study_date, study_date) res.index = res.index.droplevel(0) # drop the single date from the multi-index # In[7]: print res.shape res.head() # In[8]: # remove stocks in Industry "Conglomerates" res = res[res['Industry'] != 31055] print res.shape # In[9]:
# ------- Measurement inteval for the analysis ------- #start = pd.Timestamp("2015-01-01") start = pd.Timestamp("2015-01-01") #end = pd.Timestamp("2017-06-30") #start = pd.Timestamp("2017-06-30") #start = pd.Timestamp("2017-08-01") end = pd.Timestamp("2017-08-29") # ------- Run the pipeline and measure runtime ------- start_timer = time() #results = run_pipeline(pipe, start_date=start, end_date=end).dropna() results = run_pipeline(pipe, start_date=start, end_date=end).replace([np.inf, -np.inf], np.nan).dropna() end_timer = time() # In[ ]: print "Time to run pipeline %.2f secs" % (end_timer - start_timer) # In[ ]: #results.count() #results.first_valid_index() #results.get_ftype_counts() #results.abs() #results.add_prefix("Z99-") #results.add_suffix("-Z99") #results.describe() #results.sort #results.tail(50) # ------- Run the pipeline and measure Runtime to construct pipeline wiht pricing and factor ------- #results.head(100)
import numpy as np from quantopian.pipeline import CustomFactor, Pipeline from quantopian.research import run_pipeline from quantopian.pipeline.filters import Q1500US from quantopian.pipeline.data.builtin import USEquityPricing from quantopian.pipeline.factors import SimpleMovingAverage def make_pipeline(): momentum = Momentum(inputs=[USEquityPricing.close], window_length=250) high = momentum.percentile_between(80, 100) low = momentum.percentile_between(0, 20) return Pipeline(columns={ 'Momentum': momentum, 'long': high, 'short': low, }, screen=Q1500US()) class Momentum(CustomFactor): def compute(self, today, asset_ids, out, values): out[:] = ((values[-1] - values[0]) / values[0]) my_pipe = make_pipeline() result = run_pipeline(make_pipeline(), '2016-09-25', '2016-09-25') result.head(1500)
# Get cumulative returns over last n_fwd_days days. We will later shift these. factor_ranks['Returns'] = Returns(inputs=[USEquityPricing.open], mask=universe, window_length=n_fwd_days) pipe = Pipeline(screen=universe, columns=factor_ranks) return pipe history_pipe = make_history_pipeline(factors, universe, n_fwd_days=n_fwd_days) start_timer = time() start = pd.Timestamp("2016-01-01") end = pd.Timestamp("2017-06-01") results = run_pipeline(history_pipe, start_date=start, end_date=end) results.index.names = ['date', 'security'] end_timer = time() print "Time to run pipeline %.2f secs" % (end_timer - start_timer) results.head() def shift_mask_data(X, Y, upper_percentile=70, lower_percentile=30, n_fwd_days=1): # Shift X to match factors at t to returns at t+n_fwd_days (we want to predict future returns after all) shifted_X = np.roll(X, n_fwd_days + 1, axis=0) # Slice off rolled elements
from quantopian.pipeline.filters import QTradableStocksUS from quantopian.pipeline.factors import SimpleMovingAverage #Instead of this line, import whatever data you need def alpha_factor_function(): return 10 def make_pipeline(): return Pipeline(columns={'column_name': alpha_factor_function()}) my_pipe = make_pipeline() pipeline_data = run_pipeline(my_pipe, start_date='2014-1-1', end_date='2016-1-1').dropna() # Alphalens (second cell) from alphalens.utils import get_clean_factor_and_forward_returns from alphalens.tears import create_full_tear_sheet pricing_data = get_pricing( symbols=pipeline_data.index. levels[1], # Finds all assets that appear at least once in the pipeline start_date='2014-1-1', end_date='2016-2-1', #1 trading day after end date of pipeline fields='open_price') merged_data = get_clean_factor_and_forward_returns(
""" from quantopian.pipeline import Pipeline from quantopian.research import run_pipeline from quantopian.pipeline.data.builtin import USEquityPricing from quantopian.pipeline.factors import SimpleMovingAverage, AverageDollarVolume def make_pipeline(): 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 dollar_volume = AverageDollarVolume(window_length=30) high_dollar_volume = dollar_volume.percentile_between(90, 100) latest_close = USEquityPricing.close.latest above_20 = latest_close > 20 tradeable_filter = high_dollar_volume & above_20 return Pipeline(columns={'percent_difference': percent_difference}, screen=tradeable_filter) result = run_pipeline(make_pipeline(), '2015-05-05', '2015-05-05') print 'Number of securities that passed the filter: %d' % len(result) result
from quantopian.research import run_pipeline from quantopian.pipeline import Pipeline from quantopian.pipeline.factors import Returns from quantopian.pipeline.data.psychsignal import stocktwits # Pipeline definition def make_pipeline(): returns = Returns(window_length=2) sentiment = stocktwits.bull_minus_bear.latest msg_volume = stocktwits.total_scanned_messages.latest return Pipeline(columns={ 'daily_returns': returns, 'sentiment': sentiment, 'msg_volume': msg_volume, }, ) # Pipeline execution data_output = run_pipeline(make_pipeline(), start_date=period_start, end_date=period_end) # Filter results for AAPL aapl_output = data_output.xs(symbols('AAPL'), level=1) # Plot results for AAPL aapl_output.plot(subplots=True)
# Tuberia: Permite realizar cálculos de activos a través del tiempo # - Calcula un valor para cada activo # Importar Tuberia y ejecución from quantopian.pipeline import Pipeline from quantopian.research import run_pipeline # Instanciar tuberia con un filtro en un dominio especifico tuberia = Pipeline( columns={ # Asignación de columnas 'nombre1': columna1, 'nombre2': columna2, 'nombreN': columnaN }, screen=filtro, # Se puede omitir domain=dominio) # Se puede omitir # Ejecutar tuberia en el periodo de tiempo especificado (Retorna un dataFrame) dataFrame = run_pipeline( tuberia, 'fecha_inicio', # 'AAAA-MM-DD' 'fecha_fin', # 'AAAA-MM-DD' )
# Definir tuberia def tuberia(): # Contenido return Pipeline( columns={ # Asignación de columnas 'nombre1': columna1, 'nombre2': columna2, 'nombreN': columnaN }) # Definir tuberia con un filtro def tuberia(): # Contenido return Pipeline( columns={ # Asignación de columnas 'nombre1': columna1, 'nombre2': columna2, 'nombreN': columnaN }, screen=filtro) # Ejecutar tuberia en el periodo de tiempo especificado dataFrame = run_pipeline( tuberia(), start='fecha_inicio', # 'AAAA-MM-DD' end='fecha_fin', # 'AAAA-MM-DD' )
return Pipeline( screen = base_universe, columns={ 'daily_returns': returns, 'sentiment_stocktwits': sentiment_stocktwits, 'sentiment_twitter': sentiment_twitter, 'msg_volume_stocktwits' : msg_volume_stocktwits, 'msg_volume_twitter' : msg_volume_twitter}) # In[5]: # Technicals Pipeline execution - Short Term technicals_output = run_pipeline( make_technicals_pipeline(), start_date = analysis_start_short, end_date = analysis_end) # In[6]: # Fundamentals Pipelien execution - Long Term fundamentals_output = run_pipeline( make_fundamentals_pipeline(), start_date = analysis_start, end_date = analysis_end) # ## Correlation # In this section, The Pearson Correlation Coefficient (PPMCC) is used to validate the observed plotted and tablulated results. PPMCC or the bivariate correlation, is a measure of the linear correlation between two variables sets of data. The p-value roughly indicates the probability of an uncorrelated system producing datasets that have a Pearson correlation at least as extreme as the one computed from these datasets.
for t in tickers: assets.append(symbols(t)) return_6m = PercentChange(inputs=[EquityPricing.close], window_length=126) return_1m = PercentChange(inputs=[EquityPricing.close], window_length=21) return_2w = PercentChange(inputs=[EquityPricing.close], window_length=10) stoch_k = FastStochasticOscillator( [EquityPricing.close, EquityPricing.low, EquityPricing.high]) stoch_d = SimpleMovingAverage(inputs=[stoch_k], window_length=4) rsi = RSI(inputs=[EquityPricing.close]) base_universe = StaticAssets(assets) pipe = Pipeline( columns={ 'return_6m': return_6m, 'return_1m': return_1m, 'return_2w': return_2w, 'stoch_d': stoch_d, 'rsi': rsi }, screen=base_universe & (return_2w >= return_1m) #& (return_2w > 0) & (stoch_d > 0) ) pipeline_result = run_pipeline(pipe, '2020-08-25', '2020-08-25') m = np.median(pipeline_result['return_6m']) candidates = pipeline_result[pipeline_result['return_6m'] > m] m = np.median(candidates['return_1m']) candidates = candidates[candidates['return_1m'] > m] m = np.median(candidates['return_2w']) candidates = candidates[candidates['return_2w'] > m] candidates['rsi'].nsmallest(5)
#### Pipeline: providing a uniform API for expressing computations on a diverse collection of datasets. ## Factors: a function from an asset and a moment in time to a numerical value ## Filters: used for describing sets of assets to include or exclude for some particular purpose ## Classifiers: categorical output. used for grouping assets for complex transformations on Factor outputs. from quantopian.pipeline import Pipeline def make_pipeline(): return Pipeline() pipe = make_pipeline() from quantopian.research import run_pipeline result = run_pipeline(pipe,'2017-01-01','2017-01-01') result.head(10) result.info() # Data from quantopian.pipeline.data.builtin import USEquityPricing # Factors from quantopian.pipeline.factors import BollingerBands,SimpleMovingAverage,EWMA SimpleMovingAverage(inputs=[USEquityPricing.close],window_length=30) def make_pipeline(): mean_close_30 = SimpleMovingAverage(inputs=[USEquityPricing.close],window_length=30) return Pipeline(columns={ '30 Day Mean Close':mean_close_30 }) results = run_pipeline(make_pipeline(),'2017-01-01','2017-01-01') results.head(20)
def __init__(self, df, index, data_pull_results): self.df = df self.index = index # DEFINE START AND END DATE self.data_pull_results = run_pipeline(Data_Pull_FFM(), start_date = s_date, end_date = e_date)
# Call .rank() on all factors and mask out the universe factor_ranks = {name: f().rank(mask=universe) for name, f in factors.iteritems()} # Get cumulative returns over last n_fwd_days days. We will later shift these. factor_ranks['Returns'] = Returns(inputs=[USEquityPricing.open], mask=universe, window_length=n_fwd_days) pipe = Pipeline(screen=universe, columns=factor_ranks) return pipe history_pipe = make_history_pipeline(factors, universe, n_fwd_days=n_fwd_days) start_timer = time() start = pd.Timestamp("2016-01-01") end = pd.Timestamp("2017-06-01") results = run_pipeline(history_pipe, start_date=start, end_date=end) results.index.names = ['date', 'security'] end_timer = time() print "Time to run pipeline %.2f secs" % (end_timer - start_timer) results.head() def shift_mask_data(X, Y, upper_percentile=70, lower_percentile=30, n_fwd_days=1): # Shift X to match factors at t to returns at t+n_fwd_days (we want to predict future returns after all) shifted_X = np.roll(X, n_fwd_days+1, axis=0) # Slice off rolled elements X = shifted_X[n_fwd_days+1:] Y = Y[n_fwd_days+1:] n_time, n_stocks, n_factors = X.shape
# ------- Adding Factor Rank to Pipeline as separate column ------- pipe.add(factor_rank, 'factor_rank') # ------- Measurement inteval for the analysis ------- #start = pd.Timestamp("2015-01-01") start = pd.Timestamp("2015-01-01") #end = pd.Timestamp("2017-06-30") #start = pd.Timestamp("2017-06-30") #start = pd.Timestamp("2017-08-01") end = pd.Timestamp("2017-08-29") # ------- Run the pipeline and measure runtime ------- start_timer = time() results = run_pipeline(pipe, start_date=start, end_date=end) end_timer = time() #results.fillna(value=0); #results.count() #results.first_valid_index() #results.get_ftype_counts() #results.abs() #results.add_prefix("Z99-") #results.add_suffix("-Z99") #results.describe() #results.sort #results.tail(50) # ------- Run the pipeline and measure Runtime to construct pipeline wiht pricing and factor ------- results.head(100) # A MultiIndex DataFrame indexed by date (level 0) and asset (level 1) # [n Date rows x m Equity columns] assets = results.index.levels[1].unique() # ------ Get pricing for dates and equities --------- pricing = get_pricing(assets, start - pd.Timedelta(days=30), end + pd.Timedelta(days=30), fields="close_price") # FYI... end + pd.Timedelta(days=30) = Timestamp('2017-07-30 00:00:00')
is_primary = EquityMetadata.is_primary.latest primary_shares = (is_share & is_primary) market_cap = Fundamentals.mkt_val.latest universe = market_cap.top(1000, mask=primary_shares) # 1-month moving average factor. fast_ma = SimpleMovingAverage(inputs=[EquityPricing.close], window_length=21) # 6-month moving average factor. slow_ma = SimpleMovingAverage(inputs=[EquityPricing.close], window_length=126) # Divide fast_ma by slow_ma to get momentum factor and z-score. momentum = fast_ma / slow_ma momentum_factor = momentum.zscore() # Create a US equities pipeline with our momentum factor, screening down to our universe. pipe = Pipeline( columns={ 'momentum_factor': momentum_factor, }, screen=universe, domain=US_EQUITIES, ) # Run the pipeline from 2016 to 2019 and display the first few rows of output. from quantopian.research import run_pipeline factor_data = run_pipeline(pipe, '2016-01-01', '2019-01-01') print("Result contains {} rows of output.".format(len(factor_data))) factor_data.head()
### this algorithm was written and run in the Quantopian Research Notebook Environment ## some functions to demonstrate pipelines and related concepts - factors, filters, screens, masks and classifiers ## basic pipeline from quantopian.pipeline import Pipeline from quantopian.research import run_pipeline # function that returns a pipeline def new_pipeline(): return Pipeline() # runnning the pipeline function results = run_pipeline(new_pipeline(), '2019-01-01', '2019-01-1') print(result.head(10)) ## pipeline with factors # factors take in an asset and a timestamp and return some numerical value # USEquityPricing contains pricing info on all the equities that we got above from quantopian.pipeline.data.builtin import USEquityPricing from quantopian.pipeline.factors import BollingerBands, SimpleMovingAverage, EWMA def new_pipeline(): mean_close_30 = SimpleMovingAverage(inputs=[USEquityPricing.close], window_length=30) return Pipeline(columns={'30-Day Mean Close Price': mean_close_30})
roa = morningstar.operation_ratios.roa.latest factor_to_analyze = gross_margin.zscore() + roa.zscore() sector = Sector() return Pipeline(columns={ 'factor to analyze': factor_to_analyze, 'sector': sector }, screen=base_universe & sector.notnull() & factor_to_analyze.notnull()) factor_data = run_pipeline(make_pipeline(), '2012-1-1', '2019-1-1') pricing_data = get_pricing(factor_data.index.levels[1], '2012-1-1', '2020-6-1', fields='open_price') sector_labels, sector_labels[-1] = dict(Sector.SECTOR_NAMES), "Unknown" merged_data = get_clean_factor_and_forward_returns( factor=factor_data['factor to analyze'], prices=pricing_data, quantiles=5, groupby=factor_data['sector'], groupby_labels=sector_labels, binning_by_group=True, periods=(198, 252)) # week = 5, month = 21, quarter = 63, year = 252
'weak': weak, 'conservative': conservative, 'aggressive': aggressive }, screen=universe) return pipe # Initializing the pipe pipe = make_pipeline() # Now let's start the pipeline start_date, end_date = '2018-01-01', '2018-12-30' results = run_pipeline(pipe, start_date, end_date) results.head() # In the next part we will use Research to explore Quantopian's datasets. Then, we will define our trading strategy and test the return of our strategy compared to the SPY based on historical data. # In[17]: # group_by(level=0).mean() gives you the average return of each day for a particular group of stocks R_biggest = results[results.biggest]['Returns'].groupby(level=0).mean() R_smallest = results[results.smallest]['Returns'].groupby(level=0).mean() R_highpb = results[results.highpb]['Returns'].groupby(level=0).mean() R_lowpb = results[results.lowpb]['Returns'].groupby(level=0).mean() R_robust = results[results.robust]['Returns'].groupby(level=0).mean()
mean_close_30 = SimpleMovingAverage(inputs=[USEquityPricing.close], window_length=30, mask=small_price) mean_close_10 = SimpleMovingAverage(inputs=[USEquityPricing.close], window_length=10, mask=small_price) #*please note the mask. it is applied in simple moving average for the reason of saving computational #efforts percent_diff = (mean_close_10 - mean_close_30) / mean_close_30 perc_filter = percent_diff > 0 #final filter: final_filter = perc_filter & small_price & nyse_filter return Pipeline(columns={ '30 Day Mean Close': mean_close_30, 'Percent Diff': percent_diff, 'Latest Close': latest_close, 'Percent Filter': perc_filter }, screen=final_filter) results = run_pipeline(make_pipeline(), '2019-09-03', '2019-09-03') #check and see: results.head() results.info()