<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 EventVestor
Step1:  Let's go over the columns
Step2:  We've done much of the data processing for you. Fields like timestamp and sid are standardized across all our Store Datasets, so the datasets are easy to combine. We have standardized the sid across all our equity databases.
We can select columns and rows with ease. Below, we'll fetch all fifty-cent dividends.
Step3:  Finally, suppose we want a DataFrame of that data, but we only want the sid, timestamp, and div_type
Step4:  <a id='pipeline'></a>
Pipeline Overview
Accessing the data in your algorithms & research
The only method for accessing partner data within algorithms running on Quantopian is via the pipeline API. Different data sets work differently but in the case of this data, you can add this data to your pipeline as follows
Step5:  Now that we've imported the data, let's take a look at which fields are available for each dataset.
You'll find the dataset, the available fields, and the datatypes for each of those fields.
Step6:  Now that we know what fields we have access to, let's see what this data looks like when we run it through Pipeline.
This is constructed the same way as you would in the backtester. For more information on using Pipeline in Research view this thread
Step7:  Taking what we've seen from above, let's see how we'd move that into the backtester.
<ASSISTANT_TASK:>
Python Code:
# import the dataset
# from quantopian.interactive.data.eventvestor import dividends as dataset
# or if you want to import the free dataset, use:
from quantopian.interactive.data.eventvestor import dividends_free  as dataset
# import data operations
from odo import odo
# import other libraries we will use
import pandas as pd
# Let's use blaze to understand the data a bit using Blaze dshape()
dataset.dshape
# And how many rows are there?
# N.B. we're using a Blaze function to do this, not len()
dataset.count()
# Let's see what the data looks like. We'll grab the first three rows.
dataset[:3]
Explanation: EventVestor: Dividend Announcements
In this notebook, we'll take a look at EventVestor's Cash Dividend Announcement dataset, available on the Quantopian Store. This dataset spans January 01, 2007 through the current day, and documents cash dividend announcements, including special dividends.
Notebook Contents
There are two ways to access the data and you'll find both of them listed below. Just click on the section you'd like to read through.
<a href='#interactive'><strong>Interactive overview</strong></a>: This is only available on Research and uses blaze to give you access to large amounts of data. Recommended for exploration and plotting.
<a href='#pipeline'><strong>Pipeline overview</strong></a>: Data is made available through pipeline which is available on both the Research & Backtesting environment. Recommended for custom factor development and moving back & forth between research/backtesting.
Free samples and limits
One key caveat: we limit the number of results returned from any given expression to 10,000 to protect against runaway memory usage. To be clear, you have access to all the data server side. We are limiting the size of the responses back from Blaze.
There is a free version of this dataset as well as a paid one. The free sample includes data until 2 months prior to the current date.
To access the most up-to-date values for this data set for trading a live algorithm (as with other partner sets), you need to purchase acess to the full set.
With preamble in place, let's get started:
<a id='interactive'></a>
Interactive Overview
Accessing the data with Blaze and Interactive on Research
Partner datasets are available on Quantopian Research through an API service known as Blaze. Blaze provides the Quantopian user with a convenient interface to access very large datasets, in an interactive, generic manner.
Blaze provides an important function for accessing these datasets. Some of these sets are many millions of records. Bringing that data directly into Quantopian Research directly just is not viable. So Blaze allows us to provide a simple querying interface and shift the burden over to the server side.
It is common to use Blaze to reduce your dataset in size, convert it over to Pandas and then to use Pandas for further computation, manipulation and visualization.
Helpful links:
* Query building for Blaze
* Pandas-to-Blaze dictionary
* SQL-to-Blaze dictionary.
Once you've limited the size of your Blaze object, you can convert it to a Pandas DataFrames using:
from odo import odo
odo(expr, pandas.DataFrame)
To see how this data can be used in your algorithm, search for the Pipeline Overview section of this notebook or head straight to <a href='#pipeline'>Pipeline Overview</a>
End of explanation
fiftyc = dataset[(dataset.div_amount==0.5) & (dataset['div_currency']=='$')]
# When displaying a Blaze Data Object, the printout is automatically truncated to ten rows.
fiftyc.sort('timestamp')
Explanation: Let's go over the columns:
- event_id: the unique identifier for this event.
- asof_date: EventVestor's timestamp of event capture.
- trade_date: for event announcements made before trading ends, trade_date is the same as event_date. For announcements issued after market close, trade_date is next market open day.
- symbol: stock ticker symbol of the affected company.
- event_type: this should always be Dividend.
- event_headline: a brief description of the event
- event_phase: the inclusion of this field is likely an error on the part of the data vendor. We're currently attempting to resolve this.
- div_type: dividend type. Values include no change, increase, decrease, initiation, defer, suspend, omission, stock, special. 
Note QoQ = quarter-on-quarter.
- div_amount: dividend payment amount in local currency
- div_currency: dividend payment currency code. Values include $, BRL, CAD, CHF, EUR, GBP, JPY.
- div_ex_date: ex-dividend date
- div_record_date: dividend payment record date
- div_pay_date: dividend payment date
- event_rating: this is always 1. The meaning of this is uncertain.
- timestamp: this is our timestamp on when we registered the data.
- sid: the equity's unique identifier. Use this instead of the symbol.
We've done much of the data processing for you. Fields like timestamp and sid are standardized across all our Store Datasets, so the datasets are easy to combine. We have standardized the sid across all our equity databases.
We can select columns and rows with ease. Below, we'll fetch all fifty-cent dividends.
End of explanation
fifty_df = odo(fiftyc, pd.DataFrame)
reduced = fifty_df[['sid','div_type','timestamp']]
# When printed: pandas DataFrames display the head(30) and tail(30) rows, and truncate the middle.
reduced
Explanation: We've done much of the data processing for you. Fields like timestamp and sid are standardized across all our Store Datasets, so the datasets are easy to combine. We have standardized the sid across all our equity databases.
We can select columns and rows with ease. Below, we'll fetch all fifty-cent dividends.
End of explanation
fifty_df = odo(fiftyc, pd.DataFrame)
reduced = fifty_df[['sid','div_type','timestamp']]
# When printed: pandas DataFrames display the head(30) and tail(30) rows, and truncate the middle.
reduced
Explanation: Finally, suppose we want a DataFrame of that data, but we only want the sid, timestamp, and div_type:
End of explanation
# Import necessary Pipeline modules
from quantopian.pipeline import Pipeline
from quantopian.research import run_pipeline
from quantopian.pipeline.factors import AverageDollarVolume
# Import the datasets available
from quantopian.pipeline.data.eventvestor import (
    DividendsByExDate,
    DividendsByPayDate,
    DividendsByAnnouncementDate,
)
from quantopian.pipeline.factors.eventvestor import (
    BusinessDaysSincePreviousExDate,
    BusinessDaysUntilNextExDate,
    BusinessDaysSincePreviousPayDate,
    BusinessDaysUntilNextPayDate,
    BusinessDaysSinceDividendAnnouncement,
)
Explanation: <a id='pipeline'></a>
Pipeline Overview
Accessing the data in your algorithms & research
The only method for accessing partner data within algorithms running on Quantopian is via the pipeline API. Different data sets work differently but in the case of this data, you can add this data to your pipeline as follows:
Import the data set here
from quantopian.pipeline.data.eventvestor import (
DividendsByExDate,
DividendsByPayDate,
DividendsByAnnouncement
)
Then in intialize() you could do something simple like adding the raw value of one of the fields to your pipeline:
pipe.add(DividendsByExDate.next_date.latest, 'next_dividends')
End of explanation
print "Here are the list of available fields per dataset:"
print "---------------------------------------------------\n"
def _print_fields(dataset):
    print "Dataset: %s\n" % dataset.__name__
    print "Fields:"
    for field in list(dataset.columns):
        print "%s - %s" % (field.name, field.dtype)
    print "\n"
for data in (DividendsByExDate, DividendsByPayDate, DividendsByAnnouncementDate):
    _print_fields(data)
print "---------------------------------------------------\n"
Explanation: Now that we've imported the data, let's take a look at which fields are available for each dataset.
You'll find the dataset, the available fields, and the datatypes for each of those fields.
End of explanation
# Let's see what this data looks like when we run it through Pipeline
# This is constructed the same way as you would in the backtester. For more information
# on using Pipeline in Research view this thread:
# https://www.quantopian.com/posts/pipeline-in-research-build-test-and-visualize-your-factors-and-filters
pipe = Pipeline()
       
pipe.add(DividendsByExDate.next_date.latest, 'next_ex_date')
pipe.add(DividendsByExDate.previous_date.latest, 'prev_ex_date')
pipe.add(DividendsByExDate.next_amount.latest, 'next_amount')
pipe.add(DividendsByExDate.previous_amount.latest, 'prev_amount')
pipe.add(DividendsByExDate.next_currency.latest, 'next_currency')
pipe.add(DividendsByExDate.previous_currency.latest, 'prev_currency')
pipe.add(DividendsByExDate.next_type.latest, 'next_type')
pipe.add(DividendsByExDate.previous_type.latest, 'prev_type')
# Setting some basic liquidity strings (just for good habit)
dollar_volume = AverageDollarVolume(window_length=20)
top_1000_most_liquid = dollar_volume.rank(ascending=False) < 1000
pipe.set_screen(top_1000_most_liquid & DividendsByExDate.previous_amount.latest.notnan())
# The show_graph() method of pipeline objects produces a graph to show how it is being calculated.
pipe.show_graph(format='png')
# run_pipeline will show the output of your pipeline
pipe_output = run_pipeline(pipe, start_date='2013-11-01', end_date='2013-11-25')
pipe_output
Explanation: Now that we know what fields we have access to, let's see what this data looks like when we run it through Pipeline.
This is constructed the same way as you would in the backtester. For more information on using Pipeline in Research view this thread:
https://www.quantopian.com/posts/pipeline-in-research-build-test-and-visualize-your-factors-and-filters
End of explanation
# This section is only importable in the backtester
from quantopian.algorithm import attach_pipeline, pipeline_output
# General pipeline imports
from quantopian.pipeline import Pipeline
from quantopian.pipeline.factors import AverageDollarVolume
# Import the datasets available
from quantopian.pipeline.data.eventvestor import (
    DividendsByExDate,
    DividendsByPayDate,
    DividendsByAnnouncementDate,
)
from quantopian.pipeline.factors.eventvestor import (
    BusinessDaysSincePreviousExDate,
    BusinessDaysUntilNextExDate,
    BusinessDaysSinceDividendAnnouncement,
)
def make_pipeline():
    # Create our pipeline
    pipe = Pipeline()
    
    # Screen out penny stocks and low liquidity securities.
    dollar_volume = AverageDollarVolume(window_length=20)
    is_liquid = dollar_volume.rank(ascending=False) < 1000
    
    # Create the mask that we will use for our percentile methods.
    base_universe = (is_liquid)
    # Add pipeline factors
    pipe.add(DividendsByExDate.next_date.latest, 'next_ex_date')
    pipe.add(DividendsByExDate.previous_date.latest, 'prev_ex_date')
    pipe.add(DividendsByExDate.next_amount.latest, 'next_amount')
    pipe.add(DividendsByExDate.previous_amount.latest, 'prev_amount')
    pipe.add(DividendsByExDate.next_currency.latest, 'next_currency')
    pipe.add(DividendsByExDate.previous_currency.latest, 'prev_currency')
    pipe.add(DividendsByExDate.next_type.latest, 'next_type')
    pipe.add(DividendsByExDate.previous_type.latest, 'prev_type')
    pipe.add(BusinessDaysUntilNextExDate(), 'business_days')
    # Set our pipeline screens
    pipe.set_screen(is_liquid)
    return pipe
def initialize(context):
    attach_pipeline(make_pipeline(), "pipeline")
    
def before_trading_start(context, data):
    results = pipeline_output('pipeline')
Explanation: Taking what we've seen from above, let's see how we'd move that into the backtester.
End of explanation
<END_TASK>