I'm posting a bunch of my code.
from quantopian.algorithm import attach_pipeline, pipeline_output
from quantopian.pipeline import Pipeline
from quantopian.pipeline.data.builtin import USEquityPricing
from quantopian.pipeline.factors import CustomFactor, SimpleMovingAverage
import pandas as pd
import numpy as np
from scipy import stats
#========================================================================================
#GLOBAL VARIABLES
days = 30
#========================================================================================
#PIPELINE CUSTOM FACTORS
class AvgDailyDollarVolumeTraded(CustomFactor):
inputs = [USEquityPricing.close, USEquityPricing.volume]
window_length = days
def compute(self, today, assets, out, close_price, volume):
out[:] = np.mean(close_price * volume, axis=0)
#=========================================================================================
#MAIN PROGRAM FUNCTIONS
#initialization function
def initialize(context):
pipe = Pipeline()
pipe = attach_pipeline(pipe, name='stock_screen')
#create filters
sma_200 = SimpleMovingAverage(inputs=[USEquityPricing.close], window_length=200)
dollar_volume = AvgDailyDollarVolumeTraded()
#screen out penny stocks and small stocks
pipe.set_screen((sma_200 > 5) & (dollar_volume > 10**7))
context.spy = sid(8554)
schedule_function(rebalance, date_rules.month_start())
#.........................................................................................
def before_trading_start(context, data):
context.output= pipeline_output('stock_screen')
# context.my_universe = [] #put your beta list here
# update_universe(context.my_universe)
# .......................................................................................
#main function
def handle_data(context, data):
for stock in context.output:
function(stock)
def function():
#function things go here, blah blah blah, secret sauce, etc.
But it doesn't seem to work. Or it gives me a runtime error. I also tried using context.output.index instead, that didn't work either. So there's probably something really obvious that I'm doing wrong, so if someone could please enlighten me that would be wonderful.