spend sometime look thru old posts here, figure it out how to get historic price and also how to set a range of security list (a rough example here in notebook). but the sid seems not working by looking up symbol.. how can I convert a symbol list into a sid list ?
from quantopian.pipeline import Pipeline, CustomFactor, CustomFilter
from quantopian.research import run_pipeline
from quantopian.pipeline.filters import StaticAssets
import math
import numpy as np
from quantopian.pipeline.factors import SimpleMovingAverage
from quantopian.pipeline.data import morningstar
from quantopian.pipeline.data.builtin import USEquityPricing
import datetime
import pytz
import pandas as pd
class SidInList(CustomFilter):
"""
Filter returns True for any SID included in parameter tuple passed at creation.
Usage: my_filter = SidInList(sid_list=(23911, 46631))
"""
inputs = []
window_length = 1
params = ('sid_list',)
def compute(self, today, assets, out, sid_list):
out[:] = np.in1d(assets, sid_list)
class CloseOnN(CustomFactor):
# Define inputs
inputs = [USEquityPricing.close]
# Set window_length to whatever number of days to lookback as a default
# in the case where no window_length is given when instantiated.
# This can also be set/over-ridden as shown below:
# my_close_on_10 = CloseOnN(window_length = 10)
#window_length = 2
outputs = ['ret', 'latest', 'prv', 'threedayago']
def compute(self, today, assets, out, close):
out.ret[:] = (close[0]-close[-1])/close[-1]
out.latest[:]=close[3]
out.prv[:]=close[2]
out.threedayago[:]=close[1]
class MarketCap(CustomFactor):
# Pre-declare inputs and window_length
inputs = [USEquityPricing.close, morningstar.valuation.shares_outstanding]
window_length = 1
# Compute market cap value
def compute(self, today, assets, out, close, shares):
out[:] = close[-1] * shares[-1]
def create_pipeline():
# Create a Pipeline computing the previous close factor.
ret,latest,prv,threedayago = CloseOnN(window_length = 4)
mkt_cap = MarketCap()
top_20 = mkt_cap.top(50)
#nasdaq_100
include_filter = SidInList(sid_list = (24,114,122,630, 67,20680, 328,14328, 368,16841, 9883, 337, 38650, 739, 27533, 3806, 18529, 1209, 40207, 1419, 15101, 17632, 39095, 1637, 1900, 32301, 18870, 14014, 25317, 36930, 12652, 26111, 24819, 24482, 2618, 2663, 27543, 1787 , 2696, 42950, 20208, 2853, 8816, 5530, 3212, 9736, 23906, 26578, 22316, 13862, 3951, 8655, 25339, 4246, 43405, 27357, 32046, 4485, 43919, 4668, 8677, 22802, 3450, 5061, 5121, 5149, 5166, 23709, 13905, 19926, 19725, 8857, 5767, 5787, 19917, 6295, 6413, 6546, 20281, 6683, 26169, 6872, 11901, 13940, 7061, 15581, 24518, 7272, 39840, 7671, 27872, 8017, 38817, 8045, 8132, 8158, 24124, 8344, 8352, 14848)) #
p = Pipeline()
p.set_screen(include_filter)
p.add(ret, 'ret')
p.add(latest, 'latest')
p.add(prv, 'prv')
p.add(threedayago, 'threedayago')
p.add(mkt_cap, 'mkt_cap')
#name_filter = (primary_symbol == 'SPY')
#pipe.set_screen(market_cap_filter)
#p.set_screen(name_filter)
#base_universe = StaticAssets([sid(24)])
return p
results = run_pipeline(create_pipeline(), '8-19-2017', '8-19-2017')
results