Notebook
In [1]:
from quantopian.pipeline import Pipeline, CustomFilter
from quantopian.research import run_pipeline
from quantopian.pipeline.data.builtin import USEquityPricing

import numpy as np
In [2]:
# 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)
        
In [3]:
# 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
Out[3]:
current_price exec_price stop_loss
2015-01-02 00:00:00+00:00 Equity(3212 [GILD]) 94.34 94.34 92.4532
Equity(8554 [SPY]) 205.53 205.53 201.4194
Equity(42950 [FB]) 78.02 78.02 76.4596
Equity(46631 [GOOG]) 526.41 526.41 515.8818
Equity(47208 [GPRO]) 63.17 63.17 61.9066
In [4]:
# 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
Out[4]:
level_0 level_1 current_price exec_price stop_loss
0 2015-01-02 00:00:00+00:00 Equity(3212 [GILD]) 94.34 94.34 92.4532
1 2015-01-02 00:00:00+00:00 Equity(8554 [SPY]) 205.53 205.53 201.4194
2 2015-01-02 00:00:00+00:00 Equity(42950 [FB]) 78.02 78.02 76.4596
3 2015-01-02 00:00:00+00:00 Equity(46631 [GOOG]) 526.41 526.41 515.8818
4 2015-01-02 00:00:00+00:00 Equity(47208 [GPRO]) 63.17 63.17 61.9066
In [5]:
# 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
Out[5]:
current_price exec_price stop_loss
equity
Equity(3212 [GILD]) 94.34 94.34 92.4532
Equity(8554 [SPY]) 205.53 205.53 201.4194
Equity(42950 [FB]) 78.02 78.02 76.4596
Equity(46631 [GOOG]) 526.41 526.41 515.8818
Equity(47208 [GPRO]) 63.17 63.17 61.9066
In [21]:
result['long_short'] = 'long'
result
Out[21]:
current_price exec_price stop_loss long_short
equity
Equity(3212 [GILD]) 94.34 94.34 92.4532 long
Equity(8554 [SPY]) 205.53 205.53 201.4194 long
Equity(42950 [FB]) 78.02 78.02 76.4596 long
Equity(46631 [GOOG]) 526.41 526.41 515.8818 long
Equity(47208 [GPRO]) 63.17 63.17 61.9066 long
In [22]:
# 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)]
Out[22]:
current_price     526.41
exec_price        526.41
stop_loss        515.882
long_short          long
Name: Equity(46631 [GOOG]), dtype: object
In [23]:
# To make it a bit more readible a variable can be used and does the same thing
goog = symbols(46631)
result.loc[goog]
Out[23]:
current_price     526.41
exec_price        526.41
stop_loss        515.882
long_short          long
Name: Equity(46631 [GOOG]), dtype: object
In [24]:
# 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']
Out[24]:
526.40999999999997
In [25]:
# Finally, if you want to find out the long_short value, simply use that column name
result.loc[goog, 'long_short']
Out[25]:
'long'
In [ ]: