def make_pipeline(context): base_universe = Q500US() # 昨日の終値 yesterday_close = USEquityPricing.close.latest yesterday_volume = USEquityPricing.volume.latest dollar_volume = AverageDollarVolume(window_length=30) high_dollar_volume = dollar_volume.percentile_between( context.high_dollar_volume_thresh_min, context.high_dollar_volume_thresh_max) sector = Sector() rsi = RSI(inputs=[USEquityPricing.close]) pipe = Pipeline(screen=base_universe & high_dollar_volume, columns={ 'high_dollar_volume': high_dollar_volume, 'sector': sector, 'rsi': rsi, 'roa': ROA(), 'roe': ROE(), 'normalized_basic_eps': NormalizedBasicEps(), 'net_income_growth': NetIncomeGrowth(), 'pe': PE(), 'book_value_yield': BookValueYield(), 'dividend_yield': DividendYield(), 'period_ending_date': PeriodEndingDate(), 'prev_close': yesterday_close, }) return pipe
def make_pipeline(): base_universe = Q500US() yesterday_close = USEquityPricing.close.latest returns = Returns(window_length=2) returns_over_5p = returns > 0.05 pipe = Pipeline(screen=base_universe & returns_over_5p, columns={ 'close': yesterday_close, 'return': returns }) return pipe
def make_pipeline(): #context.features = ['RSI', 'MACD', 'EMA','SMA_5','SMA_10','ADX'] base_universe = Q500US() sector = mstar.asset_classification.morningstar_sector_code.latest sectors_311 = sector.eq(311) returns_1 = Returns(window_length=2) rsi = RSI(inputs=[USEquityPricing.close]) macd = MovingAverageConvergenceDivergenceSignal( mask=base_universe ) ema = ExponentialWeightedMovingAverage( mask=base_universe, inputs=[USEquityPricing.close], window_length=30, decay_rate=(1 - (2.0 / (1 + 15.0))) ) mean_5 = SimpleMovingAverage( inputs=[USEquityPricing.close], window_length=5, mask=base_universe ) mean_10 = SimpleMovingAverage( inputs=[USEquityPricing.close], window_length=10, mask=base_universe ) bb = BollingerBands( inputs=[USEquityPricing.close], window_length=20, k=2 ) diluted_eps = Fundamentals.diluted_eps_earnings_reports.latest growth_score = Fundamentals.growth_score.latest tangible_bv = Fundamentals.tangible_book_value.latest return Pipeline( columns={ 'returns_1': returns_1, 'RSI': rsi, 'MACD': macd, 'EMA': ema, 'SMA_5': mean_5, 'SMA_10': mean_10, 'bb_upper': bb.upper, 'bb_middle': bb.middle, 'bb_lower': bb.lower, 'diluted_eps': diluted_eps, 'growth_score': growth_score, 'tangible_bv': tangible_bv }, screen=(base_universe & sectors_311), )
def make_pipeline(): """ A function to create our dynamic stock selector (pipeline). Documentation on pipeline can be found here: https://www.quantopian.com/help#pipeline-title """ # Base universe set to the Q1500US # base_universe = Q1500US() # initial asset funnel # base_universe = Q1500US() base_universe = Q500US() yesterday_close = USEquityPricing.close.latest # roll_mean = SimpleMovingAverage(inputs=[USEquityPricing.close], window_length=10, mask=base_universe) bb = BollingerBands(inputs=[USEquityPricing.close], window_length=10, k=1, mask=base_universe) # bb = BollingerBands(inputs = context.aapl, window_length=10, k=1) upper_band = bb.upper mean_band = bb.middle lower_band = bb.lower # print bb # print upper_band percent_diff_upper = (upper_band - mean_band) / mean_band # shorts = percent_diff_upper.top(30) percent_diff_lower = (lower_band - mean_band) / mean_band longs = lower_band.top(20) shorts = upper_band.bottom(30) # print shorts # print longs securities_to_trade = (shorts | longs) #------------------------------------------------- # mean_10 = SimpleMovingAverage(inputs=[USEquityPricing.close],window=10,mask=base_universe) # mean_30 = SimpleMovingAverage(inputs=[USEquityPricing.close],window=10,mase=base_universe) # percent_diff = (mean_10 - mean_30)/mean_30 # shorts = percent_diff.top(25) # longs = percent_diff.bottom(25) # securities_to_trade = (shorts | longs) #------------------------------------------------- # Factor of yesterday's close price. pipe = Pipeline( columns={ 'longs': longs, 'shorts': shorts, }, screen=(securities_to_trade), ) return pipe
def make_pipeline(): """ A function to create our dynamic stock selector (pipeline). Documentation on pipeline can be found here: https://www.quantopian.com/help#pipeline-title """ # Base universe set to the Q500US base_universe = Q500US() # Factor of yesterday's close price. yesterday_close = USEquityPricing.close.latest pipe = Pipeline(screen=base_universe, columns={ 'close': yesterday_close, }) return pipe
def make_pipeline(context): mask = Q500US() #lb_13 = -Returns(window_length=13, mask=mask) weekly_return = Returns(window_length=6, mask=mask) pipe = Pipeline( columns={ 'weekly_return': weekly_return, 'sector': Sector(), }, # combined_alpha will be NaN for all stocks not in our universe, # but we also want to make sure that we have a sector code for everything # we trade. screen=weekly_return.notnull() & Sector().notnull(), ) return pipe
def make_pipeline(): base_universe = Q500US() yesterday_close = PrevClose() yesterday_volume = PrevVolume() dollar_volume = AverageDollarVolume(window_length=30) #ToDo この範囲を色々変えてみる. high_dollar_volume = dollar_volume.percentile_between(98, 100) pipe = Pipeline(columns={ 'yesterday_close': yesterday_close, 'yesterday_volume': yesterday_volume, 'yesterday_turnover': yesterday_close * yesterday_volume, 'dollar_volume': dollar_volume, 'high_dollar_volume': high_dollar_volume, }, screen=base_universe & high_dollar_volume) return pipe
def make_pipeline(): """ A function to create our dynamic stock selector (pipeline). Documentation on pipeline can be found here: https://www.quantopian.com/help#pipeline-title """ # Base universe set to the QTradableStocksUS base_universe = QTradableStocksUS()#Q500US() base_universe = (base_universe & Q500US()) #base_universe = (base_universe & Fundamentals.market_cap.latest.top(150)) # Factor of yesterday's close price. #yesterday_close = USEquityPricing.close.latest pipe = Pipeline( columns={ #'close': yesterday_close, 'sector': Sector(), }, screen=base_universe ) return pipe
def initialize(context): """ Setting our variables. Modify here to test different iterations of the model. """ # Opt Method 1 = original, 2 = opt api context.opt_method = 1 # Investment Universe 1 = Q500US, 2 = Q1500US context.investment_set = 1 # This version uses the average of two momentum slopes. # Want just one? Set them both to the same number. context.momentum_window = 128 #88 # first momentum window. context.momentum_window2 = 172 #128 # second momentum window # Limit minimum slope. Keep in mind that shorter momentum windows # yield more extreme slope numbers. Adjust one, and you may want # to adjust the other. context.minimum_momentum = 88 #78 #70 #88 # momentum score cap # Fixed number of stocks in the portfolio. How diversified # do you want to be? context.number_of_stocks = 50 #150 #100 #64 # portfolio size context.index_id = sid( 8554) # identifier for the SPY. used for trend filter. context.index_average_window = 150 # moving average periods for index filter # enable/disable trend filter. context.index_trend_filter = True # Most momentum research excludes most recent data. context.exclude_days = 5 # excludes most recent days from momentum calculation # Set trading frequency here. #context.trading_frequency = date_rules.week_start(days_offset=3) context.trading_frequency = date_rules.month_start(days_offset=3) # identifier for the cash management etf, if used. context.use_bond_etf = True #context.bond_etf = sid(23870) #ief context.bond_etf = sid(23921) #tlt # 1 = inv.vola. 2 = equal size. Suggest to implement # market cap and inverse market cap as well. There's # lots of room for development here. context.size_method = 1 if (context.opt_method == 1): rebalance = my_rebalance elif (context.investment_set == 2): rebalance = my_rebalance_opt # Schedule rebalance schedule_function(rebalance, context.trading_frequency, time_rules.market_open(hours=1)) # Schedule daily recording of number of positions - For display in back # test results only. schedule_function(my_record_vars, date_rules.every_day(), time_rules.market_close()) # Create our dynamic stock selector - getting the top 500 most liquid US # stocks. if (context.investment_set == 1): inv_set = Q500US() elif (context.investment_set == 2): inv_set = Q1500US() attach_pipeline(make_pipeline(inv_set), 'investment_universe')
def initialize(context): """ Setting our variables. Modify here to test different iterations of the model. """ # Investment Universe 1 = Q500US, 2 = Q1500US context.investment_set = 1 # This version uses the average of two momentum slopes. # Want just one? Set them both to the same number. context.momentum_window = 60 # first momentum window. context.momentum_window2 = 90 # second momentum window # Limit minimum slope. Keep in mind that shorter momentum windows # yield more extreme slope numbers. Adjust one, and you may want # to adjust the other. context.minimum_momentum = 60 # momentum score cap # Fixed number of stocks in the portfolio. How diversified # do you want to be? context.number_of_stocks = 25 # portfolio size context.index_id = sid( 8554) # identifier for the SPY. used for trend filter. context.index_average_window = 100 # moving average periods for index filter # enable/disable trend filter. context.index_trend_filter = True # Most momentum research excludes most recent data. context.exclude_days = 5 # excludes most recent days from momentum calculation # Set trading frequency here. context.trading_frequency = date_rules.month_start() # identifier for the cash management etf, if used. context.use_bond_etf = True context.bond_etf = sid(23870) # 1 = inv.vola. 2 = equal size. Suggest to implement # market cap and inverse market cap as well. There's # lots of room for development here. context.size_method = 2 # Schedule rebalance schedule_function(my_rebalance, context.trading_frequency, time_rules.market_open(hours=1)) # Schedule daily recording of number of positions - For display in back # test results only. schedule_function(my_record_vars, date_rules.every_day(), time_rules.market_close()) # Create our dynamic stock selector - getting the top 500 most liquid US # stocks. if (context.investment_set == 1): inv_set = Q500US() elif (context.investment_set == 2): inv_set = Q1500US() attach_pipeline(make_pipeline(inv_set), 'investment_universe') set_slippage( slippage.VolumeShareSlippage(volume_limit=0.025, price_impact=0.1)) # Default set_commission(commission.PerShare(cost=0.005, min_trade_cost=1.0)) # FSC for IB
def make_pipeline(): Universe = Q500US() pipe = Pipeline(screen=Universe) return pipe