def initialize(context): # set_commission(commission.PerShare(cost=0, min_trade_cost=None)) # set_slippage(slippage.FixedSlippage(spread=0)) pipe = Pipeline() attach_pipeline(pipe, 'ranked') dollar_volume = AverageDollarVolume(window_length=1) high_dollar_volume = dollar_volume.percentile_between(95, 100) alpha41 = Alpha41(mask=high_dollar_volume) vwap = VWAP(window_length=1) alpha41 = alpha41**.5 - vwap alpha41_rank = alpha41.rank(mask=high_dollar_volume) roe = ROE(mask=high_dollar_volume) combo_raw = (alpha41_rank) pipe.add(combo_raw, 'combo_raw') pipe.set_screen(roe > .005) schedule_function(func=rebalance, date_rule=date_rules.every_day(), time_rule=time_rules.market_open(hours=0, minutes=1)) context.long_leverage = .5 context.short_leverage = -.5 context.short_num = 20 context.long_num = 20
def make_pipeline(): """ Make a VWAP pipeline. """ vwap = VWAP(inputs=[USEquityPricing.close, USEquityPricing.volume], window_length=14) short_sma = SimpleMovingAverage(inputs=[USEquityPricing.close], window_length=35) long_sma = SimpleMovingAverage(inputs=[USEquityPricing.close], window_length=50) symbol = SidInList(sid_list=(8554)) # Only show values for the S&P500 return Pipeline( columns={ 'vwap': vwap, 'close': USEquityPricing.close.latest, 'short_sma': short_sma, 'long_sma': long_sma, 'symbol': symbol, }, screen=symbol, )
result.head() # factors can also be added to an existing Pipeline instance using the Pipeline.add method my_pipe = Pipeline() f1 = SomeFactor(...) my_pipe.add(f1) # The most commonly used built-in Factor is Latest # Latest factor gets the most recent value of a given data column def make_pipeline(): mean_close_10 = SimpleMovingAverage(inputs=[USEquityPricing.close], window_length=10) latest_close = USEquityPricing.close.latest return Pipeline(columns={ '10_day_mean_close': mean_close_10, 'latest_close_price': latest_close }) result = run_pipeline(make_pipeline(), '2015-05-05', '2015-05-05') result.head() # NB: .latest can sometimes return things other than Factors. We'll see examples of other possible return types in later lessons # Some factors have default inputs that should never be changed. # For example the VWAP built-in factor is always calculated from USEquityPricing.close and USEquityPricing.volume, so no need to specify these default BoundColumns from quantopian.pipeline.factors import VWAP vwap = VWAP(window_length=10)
mean_close_10 = SimpleMovingAverage(inputs=[USEquityPricing.close], window_length=10) # This add another column for pipeline latest_close = USEquityPricing.close.latest return Pipeline(columns={ '10_day_mean_close': mean_close_10, 'latest_close_price': latest_close }) ''' Default Inputs ''' # Some factors have default inputs that should never be changed. # Volume Weighted Average Price from quantopian.pipeline.factors import VWAP vwap = VWAP(window_length=10) # don't need 'input=' here ''' 4. Combining Factors ''' ''' Combining Factors ''' # Using arithemetics f1 = SomeFactor() f2 = SomeOtherFactor() average = (f1 + f2) / 2.0 # e.g. Percent_difference (by combining two factors) def make_pipeline(): mean_close_10 = SimpleMovingAverage(inputs=[USEquityPricing.close], window_length=10)