Notebook
In [1]:
from quantopian.interactive.data.sentdex import sentiment
In [2]:
from quantopian.pipeline.filters.morningstar import Q1500US
In [3]:
type(sentiment)
Out[3]:
<class 'blaze.expr.expressions.Field'>
In [4]:
dir(sentiment)
Out[4]:
['apply',
 'asof_date',
 'cast',
 'count',
 'count_values',
 'distinct',
 'drop_field',
 'dshape',
 'fields',
 'head',
 'isidentical',
 'map',
 'ndim',
 'nelements',
 'nrows',
 'nunique',
 'peek',
 'relabel',
 'sample',
 'schema',
 'sentiment_signal',
 'shape',
 'shift',
 'sid',
 'sort',
 'symbol',
 'tail',
 'timestamp']
In [5]:
BAC = symbols('BAC').sid
bac_sentiment = sentiment[(sentiment.sid==BAC) ]
In [6]:
bac_sentiment.head()
Out[6]:
<quantopian>.sentdex.sentiment[<quantopian>.sentdex.sentiment.sid == 700].head(10)
In [7]:
bac_sentiment.peek()
Out[7]:
symbol sentiment_signal sid asof_date timestamp
0 BAC 6.0 700 2012-11-14 2012-11-15
1 BAC 1.0 700 2012-11-15 2012-11-16
2 BAC -1.0 700 2012-11-16 2012-11-17
3 BAC -1.0 700 2012-11-17 2012-11-18
4 BAC -1.0 700 2012-11-18 2012-11-19
5 BAC 6.0 700 2012-11-19 2012-11-20
6 BAC 6.0 700 2012-11-20 2012-11-21
7 BAC 6.0 700 2012-11-21 2012-11-22
8 BAC 6.0 700 2012-11-22 2012-11-23
9 BAC 6.0 700 2012-11-23 2012-11-24
10 BAC 6.0 700 2012-11-24 2012-11-25
In [8]:
import blaze
In [9]:
bac_sentiment=blaze.compute(bac_sentiment)
In [10]:
type(bac_sentiment)
Out[10]:
<class 'pandas.core.frame.DataFrame'>
In [11]:
bac_sentiment.set_index('asof_date', inplace = True)
In [12]:
bac_sentiment.head()
Out[12]:
symbol sentiment_signal sid timestamp
asof_date
2012-11-14 BAC 6.0 700 2012-11-15
2012-11-15 BAC 1.0 700 2012-11-16
2012-11-16 BAC -1.0 700 2012-11-17
2012-11-17 BAC -1.0 700 2012-11-18
2012-11-18 BAC -1.0 700 2012-11-19
In [13]:
bac_sentiment['sentiment_signal'].plot()
Out[13]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f41d8da4b70>
In [14]:
bac_sentiment = bac_sentiment[ (bac_sentiment.index>'2016-06-01')]
In [15]:
bac_sentiment['sentiment_signal'].plot()
Out[15]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f41d9b5e860>
In [16]:
from quantopian.pipeline import Pipeline
In [17]:
def make_pipeline():
    return Pipeline()
In [18]:
from quantopian.research import run_pipeline
In [19]:
result = run_pipeline(make_pipeline(), start_date = '2015-05-05', end_date='2015-05-05')

Pipeline Execution Time: 0.82 Seconds
In [20]:
type(result)
Out[20]:
<class 'pandas.core.frame.DataFrame'>
In [21]:
result.head()
Out[21]:
2015-05-05 00:00:00+00:00 Equity(2 [HWM])
Equity(21 [AAME])
Equity(24 [AAPL])
Equity(25 [HWM_PR])
Equity(31 [ABAX])
In [22]:
len(result)
from quantopian.pipeline.data.sentdex import sentiment
In [ ]:
 
In [ ]:
 
In [23]:
def make_pipeline():
    sentiment_factor = sentiment.sentiment_signal.latest
    
    universe = (Q1500US() & sentiment_factor.notnull())
    
    pipe = Pipeline(columns={'sentiment': sentiment_factor,
                            'longs': (sentiment_factor>=4),
                            'shorts': (sentiment_factor<=-2)},
                   screen=universe)
    return pipe
In [24]:
result = run_pipeline(make_pipeline(),start_date='2015-01-01', end_date='2016-01-01')

Pipeline Execution Time: 1 Minute, 11.77 Seconds
In [25]:
result.head()
Out[25]:
longs sentiment shorts
2015-01-02 00:00:00+00:00 Equity(2 [HWM]) False 2.0 False
Equity(24 [AAPL]) False 2.0 False
Equity(62 [ABT]) False 1.0 False
Equity(67 [ADSK]) True 6.0 False
Equity(76 [TAP]) False -3.0 True
In [26]:
assets = result.index.get_level_values(level=1).unique()  
len(assets)
Out[26]:
524
In [31]:
price = get_pricing(assets, start_date='2016-03-01', end_date='2016-06-01', fields = 'open_price')
In [ ]:
import alphalens

factor_data = alphalens.utils.get_clean_factor_and_forward_returns(factor = result['sentiment'],
                                        prices = price,
                                        quantiles = 2,
                                        periods = (1,5,10))
alphalens.tears.create_full_tear_sheet(factor_data)
In [ ]:
 
In [ ]:
 
In [ ]: