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) ]
bac_sentiment.head()
Out[5]:
<quantopian>.sentdex.sentiment[<quantopian>.sentdex.sentiment.sid == 700].head(10)
In [6]:
bac_sentiment.peek()
Out[6]:
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 [7]:
import blaze
In [8]:
bac_sentiment = blaze.compute(bac_sentiment)
type(bac_sentiment)
Out[8]:
<class 'pandas.core.frame.DataFrame'>
In [9]:
bac_sentiment.set_index('asof_date', inplace=True)
bac_sentiment['sentiment_signal'].plot()
Out[9]:
<matplotlib.axes._subplots.AxesSubplot at 0x7ff0a5e27b38>
In [10]:
bac_sentiment = bac_sentiment[ (bac_sentiment.index > '2016-06-01') ]
bac_sentiment['sentiment_signal'].plot()
Out[10]:
<matplotlib.axes._subplots.AxesSubplot at 0x7ff0aa5009b0>
In [11]:
from quantopian.pipeline import Pipeline
In [12]:
def make_pipeline():
    return Pipeline()
In [13]:
from quantopian.research import run_pipeline
In [14]:
my_pipe = make_pipeline()
result = run_pipeline(my_pipe, start_date='2015-05-05', end_date='2015-05-05')

Pipeline Execution Time: 0.81 Seconds
In [15]:
result.head()
Out[15]:
2015-05-05 00:00:00+00:00 Equity(2 [ARNC])
Equity(21 [AAME])
Equity(24 [AAPL])
Equity(25 [ARNC_PR])
Equity(31 [ABAX])
In [16]:
len(result)
Out[16]:
8253
In [17]:
def make_pipeline_2():
    
    #Factor returns 
    sentiment_factor = sentiment.sentiment_signal.latest
    
    # Our universe is made up of stocks that have a non-null sentiment signal that was updated in
    # the last day, are not within 2 days of an earnings announcement, are not announced acquisition
    # targets, and are in the Q1500US.
    universe = (Q1500US() 
                & sentiment_factor.notnull())
    
    # A classifier to separate the stocks into quantiles based on sentiment rank.

    
    # Go short the stocks in the 0th quantile, and long the stocks in the 2nd quantile.
    pipe = Pipeline(
        columns={
            'sentiment': sentiment_factor,
            'longs': (sentiment_factor >=4),
            'shorts': (sentiment_factor<=2),
        },
        screen=universe
    )
    
    return pipe
In [18]:
result = run_pipeline(make_pipeline_2(), start_date='2015-01-01', end_date='2016-01-01')
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-18-9d2dfd9f3823> in <module>()
----> 1 result = run_pipeline(make_pipeline_2(), start_date='2015-01-01', end_date='2016-01-01')

<ipython-input-17-c6bb317b50ad> in make_pipeline_2()
      2 
      3     #Factor returns
----> 4     sentiment_factor = sentiment.sentiment_signal.latest
      5 
      6     # Our universe is made up of stocks that have a non-null sentiment signal that was updated in

/venvs/py35/src/blaze/blaze/expr/expressions.py in __getattr__(self, key)
    230             '%s should set _hash in _init' % type(self).__name__
    231         try:
--> 232             result = object.__getattribute__(self, key)
    233         except AttributeError:
    234             fields = dict(zip(map(valid_identifier, self.fields), self.fields))

AttributeError: 'Field' object has no attribute 'latest'
In [61]:
sentiment.peek()
Out[61]:
symbol sentiment_signal sid asof_date timestamp
0 AAPL 6.0 24 2012-10-15 2012-10-16
1 AAPL 2.0 24 2012-10-16 2012-10-17
2 AAPL 6.0 24 2012-10-17 2012-10-18
3 AAPL 6.0 24 2012-10-18 2012-10-19
4 AAPL 6.0 24 2012-10-19 2012-10-20
5 AAPL 6.0 24 2012-10-20 2012-10-21
6 AAPL 1.0 24 2012-10-21 2012-10-22
7 AAPL -1.0 24 2012-10-22 2012-10-23
8 MSFT 6.0 5061 2012-10-22 2012-10-23
9 AAPL -3.0 24 2012-10-23 2012-10-24
10 MSFT 6.0 5061 2012-10-23 2012-10-24
In [72]:
sentiment.sentiment_signal.latest
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-72-f20765bea6db> in <module>()
----> 1 sentiment.sentiment_signal.latest

/venvs/py35/src/blaze/blaze/expr/expressions.py in __getattr__(self, key)
    230             '%s should set _hash in _init' % type(self).__name__
    231         try:
--> 232             result = object.__getattribute__(self, key)
    233         except AttributeError:
    234             fields = dict(zip(map(valid_identifier, self.fields), self.fields))

AttributeError: 'Field' object has no attribute 'latest'
In [ ]: