Notebook
In [8]:
import numpy as np
import pandas as pd
from quantopian.pipeline import Pipeline
from quantopian.pipeline import CustomFactor
from quantopian.research import run_pipeline
from quantopian.pipeline.data.builtin import USEquityPricing
from quantopian.pipeline.factors import (SimpleMovingAverage, AverageDollarVolume, VWAP)
from quantopian.pipeline.data import morningstar
from quantopian.pipeline.filters import Q500US

from datetime import datetime 
from pandas.tseries.offsets import BDay

VWAP Function

In order to try and salvage performance with the use of get_pricing within Pipeline, it's a bit convoluted. This could probably be cleaned up and further optimized.

In [9]:
def vwap(today, assets, n):
   
    class _Prices(object):
        def __init__(self): 
            self.arr = {}

        def group(self, index):
            def _tmp(x):
                try:
                    self.arr[index].append(x)
                except KeyError:
                    self.arr[index] = []
                    self.arr[index].append(x)
                return x

            return _tmp
        
    def _vwap(prices):
        dv = (prices['volume'] * 
              (prices['high'] + prices['low'] + prices['close_price']) / 3)
        vwap = dv.sum(axis=0) / prices['volume'].sum(axis=0)
        return vwap
    
    assets_ = symbols(assets) 
    e = today-BDay(1)
    s = today-BDay(n+1)
    prices = get_pricing(assets_, 
                         fields=['high', 'low', 'close_price', 
                                 'open_price', 'volume'], 
                         end_date=e,
                         start_date=s,
                         frequency='minute')
    
    t = _Prices()
    arr = {}
    
    for i, val in prices.iteritems():
        prices[i].groupby([(prices[i].index.month), 
                           (prices[i].index.day)]).apply(t.group(i))
    vwap_arr = []
    for i in range(1, len(t.arr['high'])):
        d = {}
        for k, v in prices.iteritems():
            d[k] = t.arr[k][i]
        vwap_arr.append(_vwap(pd.Panel(d)))
        
    return np.array(vwap_arr)

Example in Alpha 5

In [10]:
#  (rank((open - (sum(vwap, 10) / 10))) * (-1 * abs(rank(close - vwap))))
class Alpha5(CustomFactor):
    inputs = [USEquityPricing.close, USEquityPricing.open]
    window_length = 10

    def compute(self, today, assets, out, close, open):
        vwap_s = vwap(today, assets, self.window_length)
        vwap_df = pd.DataFrame(vwap_s)
        open_df = pd.DataFrame(open)
        arg1 = (open_df - vwap_df.sum() / 10).rank(axis=1)
        close_df = pd.DataFrame(close)
        arg2 = (close_df - vwap_df).rank(axis=1).abs() * -1
        out[:] = (arg1 * arg2).values[-1]
        

Example CustomFactor

In [11]:
class _VWAP(CustomFactor):
    inputs = [USEquityPricing.close]
    window_length = 1

    def compute(self, today, assets, out, close):
        vwap_s = vwap(today, assets, self.window_length)
        out[:] = vwap_s[-1]

        

Example Use

In [12]:
class ROE(CustomFactor):   
    inputs = [morningstar.operation_ratios.roe] 
    window_length = 1
    def compute(self, today, assets, out, close):        
        out[:] = close[-1]
        
def make_factors():
    factors = {
        'alpha5': Alpha5,
        'vwap': _VWAP,
    }
    return factors
        

high_dollar_volume = AverageDollarVolume(window_length=1).percentile_between(95, 100)
universe = (ROE(mask=high_dollar_volume) > 0.005)

start = '2016-06-01'
end = start

pipe = Pipeline(screen=universe)

factors = make_factors()
for name, f in factors.iteritems():
    pipe.add(f(mask=high_dollar_volume).rank(), name)

result = run_pipeline(pipe, start_date=start, end_date=end)
In [13]:
result.head()
Out[13]:
alpha5 vwap
2016-06-01 00:00:00+00:00 Equity(24 [AAPL]) 41.0 306.0
Equity(62 [ABT]) 75.0 129.0
Equity(114 [ADBE]) 299.0 307.0
Equity(128 [ADM]) 214.0 144.0
Equity(161 [AEP]) 166.0 223.0