from quantopian.pipeline import Pipeline, CustomFilter
from quantopian.research import run_pipeline
from quantopian.pipeline.data.builtin import USEquityPricing
import numpy as np
# Going to use pipline to get data and want specific securities
# So, create a custom filter to do just that
class Specific_Stocks(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)
# Make factors to get specific columns of data
current_price = USEquityPricing.close.latest
exec_price = USEquityPricing.close.latest
stop_loss = current_price * .98
# Make a filter to limit results to our specific securities
my_stocks = Specific_Stocks(sid_list=(42950, 47208, 46631, 3212, 8554))
# Make the pipeline with the specified factors as columns
my_pipe = Pipeline(columns={'current_price': current_price, 'exec_price': exec_price, 'stop_loss':stop_loss})
# Screen (or filter) the pipeline to return just the desired stocks
my_pipe.set_screen(my_stocks)
# Run the pipeline for a specific date to get that dates data
result = run_pipeline(my_pipe, '2015-01-01', '2015-01-01')
# Let's see the results
result
# A pipline in the research platform returns a dataframe with a multi-index which includes the date
# Let's get rid of that so we simply have a dataframe indexed by the security
result = result.reset_index()
result
# Now rename the level_1 column to something more appropriate and make it the index and drop the date column
result.rename(columns= {'level_1':'equity'}, inplace=True)
result = result.drop('level_0', axis=1)
result = result.set_index('equity')
result
result['long_short'] = 'long'
result
# That looks prettier and is in the same format as the posted data
# Notice that the index is an equity object
# There are a lot of ways to index/get specific data. Let's use the loc method
# Just use the equity object as the index (symbols reurns an equity object)
result.loc[symbols(46631)]
# To make it a bit more readible a variable can be used and does the same thing
goog = symbols(46631)
result.loc[goog]
# The above return the entire google row as a series
# If we want a specific column in that series then put the column name
result.loc[goog, 'current_price']
# Finally, if you want to find out the long_short value, simply use that column name
result.loc[goog, 'long_short']