Notebook

Volume and Dollar-Volume factors

In [1]:
# First, we need to import the basic pipeline methods
from quantopian.pipeline import Pipeline, CustomFactor
from quantopian.research import run_pipeline

# Also get the built-in filters and/or factors to use
from quantopian.pipeline.filters import QTradableStocksUS, Q500US, StaticAssets
from quantopian.pipeline.factors import SimpleMovingAverage, AverageDollarVolume

# Finally get any data we want to use
from quantopian.pipeline.data import EquityPricing
from quantopian.pipeline.domain import US_EQUITIES

# Import numpy and pandas because they rock
import numpy as np
import pandas as pd
In [2]:
def make_pipeline_using_volume():
    # Universe we wish to trade
    # Here we just use something for testing 
    base_universe = QTradableStocksUS()
    spy = StaticAssets(symbols(['SPY']))
    base_and_spy = base_universe | spy

    # Create any needed factors.
    last_10_days_each_sma = SimpleMovingAverage(inputs=[EquityPricing.volume], window_length=10, mask=base_and_spy)
    # To get a single value (ie SPY) from a factor we can 'slice' the factor (ie grab a single security)
    last_10_days_spy_sma = last_10_days_each_sma[symbols('SPY')]
    # We can now find the ratio. For technical reasons the first operand must be a factor and not a slice
    ratio_10_days = last_10_days_each_sma / last_10_days_spy_sma
    
    yesterday_each_sma = EquityPricing.volume.latest
    yesterday_spy_sma = yesterday_each_sma[symbols('SPY')]
    ratio_1_days = yesterday_each_sma / yesterday_spy_sma
    
    volume_momentum = ratio_10_days-ratio_1_days

    # Create any filters or signals based upon these factors
    pass

    # Create our pipeline
    pipe = Pipeline(
        columns={
            'last_10_days_each_sma': last_10_days_each_sma,
            'ratio_10_days':ratio_10_days,
            'yesterday_each_sma':yesterday_each_sma,
            'ratio_1_days':ratio_1_days,
            'volume_momentum': volume_momentum,
        },
        screen=base_and_spy,
    )
    
    return pipe 
In [3]:
def make_pipeline_using_dollarvolume():
    # Universe we wish to trade
    # Here we just use something for testing 
    base_universe = QTradableStocksUS()
    spy = StaticAssets(symbols(['SPY']))
    base_and_spy = base_universe | spy

    # Create any needed factors.
    last_10_days_each_sma = AverageDollarVolume(window_length=10, mask=base_and_spy)
    # To get a single value (ie SPY) from a factor we can 'slice' the factor (ie grab a single security)
    last_10_days_spy_sma = last_10_days_each_sma[symbols('SPY')]
    # We can now find the ratio. For technical reasons the first operand must be a factor and not a slice
    ratio_10_days = last_10_days_each_sma / last_10_days_spy_sma
    
    yesterday_each_sma = AverageDollarVolume(window_length=1, mask=base_and_spy)
    yesterday_spy_sma = yesterday_each_sma[symbols('SPY')]
    ratio_1_days = yesterday_each_sma / yesterday_spy_sma
    
    volume_momentum = ratio_10_days-ratio_1_days

    # Create any filters or signals based upon these factors
    pass

    # Create our pipeline
    pipe = Pipeline(
        columns={
            'last_10_days_each_sma': last_10_days_each_sma,
            'ratio_10_days':ratio_10_days,
            'yesterday_each_sma':yesterday_each_sma,
            'ratio_1_days':ratio_1_days,
            'volume_momentum': volume_momentum,
        },
        screen=base_and_spy,
    )
    
    return pipe 
In [4]:
# Set a common time period to run our different pipes
start = '2019-10-28'
end = '2019-10-28'
In [5]:
# Look at the pipe using volume
results = run_pipeline(make_pipeline_using_volume(), start, end)

# Display the first 3 rows and then just the values for SPY
display(results.head(3), results.xs(symbols('SPY'), level=1))

Pipeline Execution Time: 1.78 Seconds
last_10_days_each_sma ratio_10_days ratio_1_days volume_momentum yesterday_each_sma
2019-10-28 00:00:00+00:00 Equity(2 [ARNC]) 1710611.1 0.055169 0.044180 0.010990 1393362.0
Equity(24 [AAPL]) 15065428.2 0.485880 0.428709 0.057171 13520887.0
Equity(41 [ARCB]) 106515.6 0.003435 0.002749 0.000686 86695.0
last_10_days_each_sma ratio_10_days ratio_1_days volume_momentum yesterday_each_sma
2019-10-28 00:00:00+00:00 31006488.0 1.0 1.0 0.0 31538608.0
In [6]:
# Look at the pipe using dollarvolume
results = run_pipeline(make_pipeline_using_dollarvolume(), start, end)

# Display the first 3 rows and then just the values for SPY
display(results.head(3), results.xs(symbols('SPY'), level=1))

Pipeline Execution Time: 0.83 Seconds
last_10_days_each_sma ratio_10_days ratio_1_days volume_momentum yesterday_each_sma
2019-10-28 00:00:00+00:00 Equity(2 [ARNC]) 4.679919e+07 0.005046 0.004047 0.000999 3.849859e+07
Equity(24 [AAPL]) 3.600102e+09 0.388179 0.350454 0.037724 3.333980e+09
Equity(41 [ARCB]) 3.206947e+06 0.000346 0.000280 0.000066 2.664137e+06
last_10_days_each_sma ratio_10_days ratio_1_days volume_momentum yesterday_each_sma
2019-10-28 00:00:00+00:00 9.274339e+09 1.0 1.0 0.0 9.513306e+09
In [7]:
results.xs(symbols('SPY'), level=1)
Out[7]:
last_10_days_each_sma ratio_10_days ratio_1_days volume_momentum yesterday_each_sma
2019-10-28 00:00:00+00:00 9.274339e+09 1.0 1.0 0.0 9.513306e+09
In [ ]: