ml-finance-python

python scripts for finance machine learning

git clone https://9o.is/git/ml-finance-python.git

notebook.ipynb

(29744B)


      1 {
      2  "cells": [
      3   {
      4    "cell_type": "markdown",
      5    "metadata": {
      6     "collapsed": true
      7    },
      8    "source": [
      9     "# EventVestor: Mergers and Acquisitions\n",
     10     "\n",
     11     "In this notebook, we'll take a look at EventVestor's *Mergers and Acquisitions* dataset, available on the [Quantopian Store](https://www.quantopian.com/store). This dataset spans January 01, 2007 through the current day.\n",
     12     "\n",
     13     "## Notebook Contents\n",
     14     "\n",
     15     "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.\n",
     16     "\n",
     17     "- <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.\n",
     18     "- <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.\n",
     19     "\n",
     20     "### Free samples and limits\n",
     21     "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.\n",
     22     "\n",
     23     "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.\n",
     24     "\n",
     25     "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.\n",
     26     "\n",
     27     "With preamble in place, let's get started:\n",
     28     "\n",
     29     "<a id='interactive'></a>\n",
     30     "#Interactive Overview\n",
     31     "### Accessing the data with Blaze and Interactive on Research\n",
     32     "Partner datasets are available on Quantopian Research through an API service known as [Blaze](http://blaze.pydata.org). Blaze provides the Quantopian user with a convenient interface to access very large datasets, in an interactive, generic manner.\n",
     33     "\n",
     34     "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.\n",
     35     "\n",
     36     "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.\n",
     37     "\n",
     38     "Helpful links:\n",
     39     "* [Query building for Blaze](http://blaze.readthedocs.io/en/latest/queries.html)\n",
     40     "* [Pandas-to-Blaze dictionary](http://blaze.readthedocs.io/en/latest/rosetta-pandas.html)\n",
     41     "* [SQL-to-Blaze dictionary](http://blaze.readthedocs.io/en/latest/rosetta-sql.html).\n",
     42     "\n",
     43     "Once you've limited the size of your Blaze object, you can convert it to a Pandas DataFrames using:\n",
     44     "> `from odo import odo`  \n",
     45     "> `odo(expr, pandas.DataFrame)`\n",
     46     "\n",
     47     "\n",
     48     "###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>"
     49    ]
     50   },
     51   {
     52    "cell_type": "code",
     53    "execution_count": 1,
     54    "metadata": {
     55     "collapsed": false
     56    },
     57    "outputs": [],
     58    "source": [
     59     "# import the dataset\n",
     60     "from quantopian.interactive.data.eventvestor import mergers_and_acquisitions_free as dataset\n",
     61     "\n",
     62     "# or if you want to import the free dataset, use:\n",
     63     "#from quantopian.data.eventvestor import buyback_auth_free\n",
     64     "\n",
     65     "# import data operations\n",
     66     "from odo import odo\n",
     67     "# import other libraries we will use\n",
     68     "import pandas as pd\n",
     69     "import matplotlib.pyplot as plt"
     70    ]
     71   },
     72   {
     73    "cell_type": "code",
     74    "execution_count": 2,
     75    "metadata": {
     76     "collapsed": false
     77    },
     78    "outputs": [
     79     {
     80      "data": {
     81       "text/plain": [
     82        "dshape(\"\"\"var * {\n",
     83        "  event_id: float64,\n",
     84        "  mna_type: ?string,\n",
     85        "  trade_date: ?datetime,\n",
     86        "  symbol: string,\n",
     87        "  event_type: ?string,\n",
     88        "  event_headline: ?string,\n",
     89        "  news_type: ?string,\n",
     90        "  firm_type: ?string,\n",
     91        "  payment_mode: ?string,\n",
     92        "  target_type: ?string,\n",
     93        "  is_crossboarder: ?string,\n",
     94        "  deal_amount: float64,\n",
     95        "  deal_currency: ?string,\n",
     96        "  related_ticker: ?string,\n",
     97        "  related_entity: ?string,\n",
     98        "  event_rating: float64,\n",
     99        "  price_pershare: float64,\n",
    100        "  premium_pct: float64,\n",
    101        "  sid: int64,\n",
    102        "  asof_date: datetime,\n",
    103        "  timestamp: datetime\n",
    104        "  }\"\"\")"
    105       ]
    106      },
    107      "execution_count": 2,
    108      "metadata": {},
    109      "output_type": "execute_result"
    110     }
    111    ],
    112    "source": [
    113     "# Let's use blaze to understand the data a bit using Blaze dshape()\n",
    114     "dataset.dshape"
    115    ]
    116   },
    117   {
    118    "cell_type": "code",
    119    "execution_count": 3,
    120    "metadata": {
    121     "collapsed": false
    122    },
    123    "outputs": [
    124     {
    125      "data": {
    126       "text/html": [
    127        "16875"
    128       ],
    129       "text/plain": [
    130        "16875"
    131       ]
    132      },
    133      "execution_count": 3,
    134      "metadata": {},
    135      "output_type": "execute_result"
    136     }
    137    ],
    138    "source": [
    139     "# And how many rows are there?\n",
    140     "# N.B. we're using a Blaze function to do this, not len()\n",
    141     "dataset.count()"
    142    ]
    143   },
    144   {
    145    "cell_type": "code",
    146    "execution_count": 4,
    147    "metadata": {
    148     "collapsed": false
    149    },
    150    "outputs": [
    151     {
    152      "data": {
    153       "text/html": [
    154        "Timestamp('2007-01-01 00:00:00')"
    155       ],
    156       "text/plain": [
    157        "Timestamp('2007-01-01 00:00:00')"
    158       ]
    159      },
    160      "execution_count": 4,
    161      "metadata": {},
    162      "output_type": "execute_result"
    163     }
    164    ],
    165    "source": [
    166     "dataset.asof_date.min()"
    167    ]
    168   },
    169   {
    170    "cell_type": "code",
    171    "execution_count": 5,
    172    "metadata": {
    173     "collapsed": false
    174    },
    175    "outputs": [
    176     {
    177      "data": {
    178       "text/html": [
    179        "<table border=\"1\" class=\"dataframe\">\n",
    180        "  <thead>\n",
    181        "    <tr style=\"text-align: right;\">\n",
    182        "      <th></th>\n",
    183        "      <th>event_id</th>\n",
    184        "      <th>mna_type</th>\n",
    185        "      <th>trade_date</th>\n",
    186        "      <th>symbol</th>\n",
    187        "      <th>event_type</th>\n",
    188        "      <th>event_headline</th>\n",
    189        "      <th>news_type</th>\n",
    190        "      <th>firm_type</th>\n",
    191        "      <th>payment_mode</th>\n",
    192        "      <th>target_type</th>\n",
    193        "      <th>is_crossboarder</th>\n",
    194        "      <th>deal_amount</th>\n",
    195        "      <th>deal_currency</th>\n",
    196        "      <th>related_ticker</th>\n",
    197        "      <th>related_entity</th>\n",
    198        "      <th>event_rating</th>\n",
    199        "      <th>price_pershare</th>\n",
    200        "      <th>premium_pct</th>\n",
    201        "      <th>sid</th>\n",
    202        "      <th>asof_date</th>\n",
    203        "      <th>timestamp</th>\n",
    204        "    </tr>\n",
    205        "  </thead>\n",
    206        "  <tbody>\n",
    207        "    <tr>\n",
    208        "      <th>0</th>\n",
    209        "      <td>148664</td>\n",
    210        "      <td>Acquisition</td>\n",
    211        "      <td>2007-01-03</td>\n",
    212        "      <td>FCS</td>\n",
    213        "      <td>M&amp;A Announcement</td>\n",
    214        "      <td>Fairchild Semiconductor to Acquire System Gene...</td>\n",
    215        "      <td>Announcement</td>\n",
    216        "      <td>Acquirer</td>\n",
    217        "      <td>Mixed Offer</td>\n",
    218        "      <td>Public</td>\n",
    219        "      <td>Cross Border</td>\n",
    220        "      <td>200</td>\n",
    221        "      <td>$M</td>\n",
    222        "      <td>None</td>\n",
    223        "      <td>System General Corporatio</td>\n",
    224        "      <td>1</td>\n",
    225        "      <td>0</td>\n",
    226        "      <td>0</td>\n",
    227        "      <td>20486</td>\n",
    228        "      <td>2007-01-01</td>\n",
    229        "      <td>2007-01-02</td>\n",
    230        "    </tr>\n",
    231        "    <tr>\n",
    232        "      <th>1</th>\n",
    233        "      <td>132422</td>\n",
    234        "      <td>Acquisition</td>\n",
    235        "      <td>2007-01-03</td>\n",
    236        "      <td>NUE</td>\n",
    237        "      <td>M&amp;A Announcement</td>\n",
    238        "      <td>Nucor to Acquire Harris Steel Group for $1B</td>\n",
    239        "      <td>Announcement</td>\n",
    240        "      <td>Acquirer</td>\n",
    241        "      <td>Cash Offer</td>\n",
    242        "      <td>Public</td>\n",
    243        "      <td>Cross Border</td>\n",
    244        "      <td>1070</td>\n",
    245        "      <td>$M</td>\n",
    246        "      <td>None</td>\n",
    247        "      <td>Harris Steel Group</td>\n",
    248        "      <td>1</td>\n",
    249        "      <td>0</td>\n",
    250        "      <td>0</td>\n",
    251        "      <td>5488</td>\n",
    252        "      <td>2007-01-02</td>\n",
    253        "      <td>2007-01-03</td>\n",
    254        "    </tr>\n",
    255        "    <tr>\n",
    256        "      <th>2</th>\n",
    257        "      <td>134823</td>\n",
    258        "      <td>Acquisition</td>\n",
    259        "      <td>2007-01-03</td>\n",
    260        "      <td>BNI</td>\n",
    261        "      <td>M&amp;A Announcement</td>\n",
    262        "      <td>Burlington Northern Unit Acquires Pro-Am Trans...</td>\n",
    263        "      <td>Announcement</td>\n",
    264        "      <td>Acquirer</td>\n",
    265        "      <td>None</td>\n",
    266        "      <td>Private</td>\n",
    267        "      <td>National</td>\n",
    268        "      <td>0</td>\n",
    269        "      <td>None</td>\n",
    270        "      <td>None</td>\n",
    271        "      <td>Pro-Am Transportation Ser</td>\n",
    272        "      <td>1</td>\n",
    273        "      <td>0</td>\n",
    274        "      <td>0</td>\n",
    275        "      <td>995</td>\n",
    276        "      <td>2007-01-02</td>\n",
    277        "      <td>2007-01-03</td>\n",
    278        "    </tr>\n",
    279        "  </tbody>\n",
    280        "</table>"
    281       ],
    282       "text/plain": [
    283        "   event_id     mna_type trade_date symbol        event_type  \\\n",
    284        "0    148664  Acquisition 2007-01-03    FCS  M&A Announcement   \n",
    285        "1    132422  Acquisition 2007-01-03    NUE  M&A Announcement   \n",
    286        "2    134823  Acquisition 2007-01-03    BNI  M&A Announcement   \n",
    287        "\n",
    288        "                                      event_headline     news_type firm_type  \\\n",
    289        "0  Fairchild Semiconductor to Acquire System Gene...  Announcement  Acquirer   \n",
    290        "1        Nucor to Acquire Harris Steel Group for $1B  Announcement  Acquirer   \n",
    291        "2  Burlington Northern Unit Acquires Pro-Am Trans...  Announcement  Acquirer   \n",
    292        "\n",
    293        "  payment_mode target_type    ...     deal_amount  deal_currency  \\\n",
    294        "0  Mixed Offer      Public    ...             200             $M   \n",
    295        "1   Cash Offer      Public    ...            1070             $M   \n",
    296        "2         None     Private    ...               0           None   \n",
    297        "\n",
    298        "  related_ticker             related_entity event_rating  price_pershare  \\\n",
    299        "0           None  System General Corporatio            1               0   \n",
    300        "1           None         Harris Steel Group            1               0   \n",
    301        "2           None  Pro-Am Transportation Ser            1               0   \n",
    302        "\n",
    303        "   premium_pct    sid  asof_date  timestamp  \n",
    304        "0            0  20486 2007-01-01 2007-01-02  \n",
    305        "1            0   5488 2007-01-02 2007-01-03  \n",
    306        "2            0    995 2007-01-02 2007-01-03  \n",
    307        "\n",
    308        "[3 rows x 21 columns]"
    309       ]
    310      },
    311      "execution_count": 5,
    312      "metadata": {},
    313      "output_type": "execute_result"
    314     }
    315    ],
    316    "source": [
    317     "# Let's see what the data looks like. We'll grab the first three rows.\n",
    318     "dataset[:3]"
    319    ]
    320   },
    321   {
    322    "cell_type": "code",
    323    "execution_count": 6,
    324    "metadata": {
    325     "collapsed": false
    326    },
    327    "outputs": [
    328     {
    329      "data": {
    330       "text/html": [
    331        "<table border=\"1\" class=\"dataframe\">\n",
    332        "  <thead>\n",
    333        "    <tr style=\"text-align: right;\">\n",
    334        "      <th></th>\n",
    335        "      <th>is_crossboarder</th>\n",
    336        "    </tr>\n",
    337        "  </thead>\n",
    338        "  <tbody>\n",
    339        "    <tr>\n",
    340        "      <th>0</th>\n",
    341        "      <td>None</td>\n",
    342        "    </tr>\n",
    343        "    <tr>\n",
    344        "      <th>1</th>\n",
    345        "      <td>National</td>\n",
    346        "    </tr>\n",
    347        "    <tr>\n",
    348        "      <th>2</th>\n",
    349        "      <td>Other</td>\n",
    350        "    </tr>\n",
    351        "    <tr>\n",
    352        "      <th>3</th>\n",
    353        "      <td>Cross Border</td>\n",
    354        "    </tr>\n",
    355        "  </tbody>\n",
    356        "</table>"
    357       ],
    358       "text/plain": [
    359        "  is_crossboarder\n",
    360        "0            None\n",
    361        "1        National\n",
    362        "2           Other\n",
    363        "3    Cross Border"
    364       ]
    365      },
    366      "execution_count": 6,
    367      "metadata": {},
    368      "output_type": "execute_result"
    369     }
    370    ],
    371    "source": [
    372     "dataset.is_crossboarder.distinct()"
    373    ]
    374   },
    375   {
    376    "cell_type": "markdown",
    377    "metadata": {},
    378    "source": [
    379     "Let's go over the columns:\n",
    380     "- **event_id**: the unique identifier for this buyback authorization.\n",
    381     "- **asof_date**: EventVestor's timestamp of event capture.\n",
    382     "- **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.\n",
    383     "- **symbol**: stock ticker symbol of the affected company.\n",
    384     "- **event_type**: this should always be *Buyback*.\n",
    385     "- **event_headline**: a short description of the event.\n",
    386     "- **timestamp**: this is our timestamp on when we registered the data.\n",
    387     "- **sid**: the equity's unique identifier. Use this instead of the symbol.\n",
    388     "- **news_type**: the type of news - `Announcement, Close, Proposal, Termination, Rumor, Rejection, None`\n",
    389     "- **firm_type**: either `Target` or `Acquirer`\n",
    390     "- **payment_mode**: the type of offer made - `Mixed Offer, Cash Offer, Other, Stock Offer, None`\n",
    391     "- **target_type**: `Public, Private, PE Holding, VC Funded, None`\n",
    392     "- **is_crossboarder**: `None, National, Other, Cross Border`\n",
    393     "- **deal_amount, deal_currency**: the amount of the deal and its corresponding currency\n",
    394     "- **related_ticker**: if present, this indicates the ticker being acquired or that is acquiring\n",
    395     "- **price_pershare, premium_pct**: the price per share and the premium paid\n",
    396     "\n",
    397     "\n",
    398     "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.\n",
    399     "\n",
    400     "We can select columns and rows with ease. Below, we'll fetch all entries for Microsoft. We're really only interested in the buyback amount, the units, and the date, so we'll display only those columns."
    401    ]
    402   },
    403   {
    404    "cell_type": "code",
    405    "execution_count": 7,
    406    "metadata": {
    407     "collapsed": false
    408    },
    409    "outputs": [
    410     {
    411      "data": {
    412       "text/plain": [
    413        "Equity(5061, symbol=u'MSFT', asset_name=u'MICROSOFT CORP', exchange=u'NASDAQ', start_date=Timestamp('2002-01-01 00:00:00+0000', tz='UTC'), end_date=Timestamp('2016-09-06 00:00:00+0000', tz='UTC'), first_traded=None, auto_close_date=Timestamp('2016-09-09 00:00:00+0000', tz='UTC'), exchange_full=u'NASDAQ GLOBAL SELECT MARKET')"
    414       ]
    415      },
    416      "execution_count": 7,
    417      "metadata": {},
    418      "output_type": "execute_result"
    419     }
    420    ],
    421    "source": [
    422     "# get the sid for MSFT\n",
    423     "symbols('MSFT')"
    424    ]
    425   },
    426   {
    427    "cell_type": "code",
    428    "execution_count": 8,
    429    "metadata": {
    430     "collapsed": false
    431    },
    432    "outputs": [
    433     {
    434      "data": {
    435       "text/html": [
    436        "<table border=\"1\" class=\"dataframe\">\n",
    437        "  <thead>\n",
    438        "    <tr style=\"text-align: right;\">\n",
    439        "      <th></th>\n",
    440        "      <th>event_id</th>\n",
    441        "      <th>mna_type</th>\n",
    442        "      <th>trade_date</th>\n",
    443        "      <th>symbol</th>\n",
    444        "      <th>event_type</th>\n",
    445        "      <th>event_headline</th>\n",
    446        "      <th>news_type</th>\n",
    447        "      <th>firm_type</th>\n",
    448        "      <th>payment_mode</th>\n",
    449        "      <th>target_type</th>\n",
    450        "      <th>is_crossboarder</th>\n",
    451        "      <th>deal_amount</th>\n",
    452        "      <th>deal_currency</th>\n",
    453        "      <th>related_ticker</th>\n",
    454        "      <th>related_entity</th>\n",
    455        "      <th>event_rating</th>\n",
    456        "      <th>price_pershare</th>\n",
    457        "      <th>premium_pct</th>\n",
    458        "      <th>sid</th>\n",
    459        "      <th>asof_date</th>\n",
    460        "      <th>timestamp</th>\n",
    461        "    </tr>\n",
    462        "  </thead>\n",
    463        "  <tbody>\n",
    464        "    <tr>\n",
    465        "      <th>0</th>\n",
    466        "      <td>78373</td>\n",
    467        "      <td>Acquisition</td>\n",
    468        "      <td>2007-05-25</td>\n",
    469        "      <td>MSFT</td>\n",
    470        "      <td>M&amp;A Announcement</td>\n",
    471        "      <td>Microsoft to Acquire aQuantive for $6B</td>\n",
    472        "      <td>Announcement</td>\n",
    473        "      <td>Acquirer</td>\n",
    474        "      <td>Cash Offer</td>\n",
    475        "      <td>Public</td>\n",
    476        "      <td>National</td>\n",
    477        "      <td>6000</td>\n",
    478        "      <td>$M</td>\n",
    479        "      <td>AQNT</td>\n",
    480        "      <td>None</td>\n",
    481        "      <td>1</td>\n",
    482        "      <td>0.00</td>\n",
    483        "      <td>0</td>\n",
    484        "      <td>5061</td>\n",
    485        "      <td>2007-05-24</td>\n",
    486        "      <td>2007-05-25</td>\n",
    487        "    </tr>\n",
    488        "    <tr>\n",
    489        "      <th>1</th>\n",
    490        "      <td>78370</td>\n",
    491        "      <td>None</td>\n",
    492        "      <td>2007-08-13</td>\n",
    493        "      <td>MSFT</td>\n",
    494        "      <td>M&amp;A Announcement</td>\n",
    495        "      <td>Microsoft Completes Acquisition of aQuantive, ...</td>\n",
    496        "      <td>Close</td>\n",
    497        "      <td>None</td>\n",
    498        "      <td>None</td>\n",
    499        "      <td>None</td>\n",
    500        "      <td>None</td>\n",
    501        "      <td>0</td>\n",
    502        "      <td>None</td>\n",
    503        "      <td>None</td>\n",
    504        "      <td>None</td>\n",
    505        "      <td>1</td>\n",
    506        "      <td>0.00</td>\n",
    507        "      <td>0</td>\n",
    508        "      <td>5061</td>\n",
    509        "      <td>2007-08-13</td>\n",
    510        "      <td>2007-08-14</td>\n",
    511        "    </tr>\n",
    512        "    <tr>\n",
    513        "      <th>2</th>\n",
    514        "      <td>125294</td>\n",
    515        "      <td>Acquisition</td>\n",
    516        "      <td>2007-11-12</td>\n",
    517        "      <td>MSFT</td>\n",
    518        "      <td>M&amp;A Announcement</td>\n",
    519        "      <td>Microsoft Intends to Acquire Musiwave</td>\n",
    520        "      <td>Announcement</td>\n",
    521        "      <td>Acquirer</td>\n",
    522        "      <td>Cash Offer</td>\n",
    523        "      <td>Private</td>\n",
    524        "      <td>Cross Border</td>\n",
    525        "      <td>0</td>\n",
    526        "      <td>None</td>\n",
    527        "      <td>None</td>\n",
    528        "      <td>Musiwave SA</td>\n",
    529        "      <td>1</td>\n",
    530        "      <td>0.00</td>\n",
    531        "      <td>0</td>\n",
    532        "      <td>5061</td>\n",
    533        "      <td>2007-11-12</td>\n",
    534        "      <td>2007-11-13</td>\n",
    535        "    </tr>\n",
    536        "    <tr>\n",
    537        "      <th>3</th>\n",
    538        "      <td>134224</td>\n",
    539        "      <td>None</td>\n",
    540        "      <td>2007-12-12</td>\n",
    541        "      <td>MSFT</td>\n",
    542        "      <td>M&amp;A Announcement</td>\n",
    543        "      <td>Microsoft Completes Acquisition of UK based Mu...</td>\n",
    544        "      <td>Close</td>\n",
    545        "      <td>None</td>\n",
    546        "      <td>None</td>\n",
    547        "      <td>None</td>\n",
    548        "      <td>None</td>\n",
    549        "      <td>0</td>\n",
    550        "      <td>None</td>\n",
    551        "      <td>None</td>\n",
    552        "      <td>Multimap</td>\n",
    553        "      <td>1</td>\n",
    554        "      <td>0.00</td>\n",
    555        "      <td>0</td>\n",
    556        "      <td>5061</td>\n",
    557        "      <td>2007-12-12</td>\n",
    558        "      <td>2007-12-13</td>\n",
    559        "    </tr>\n",
    560        "    <tr>\n",
    561        "      <th>4</th>\n",
    562        "      <td>137589</td>\n",
    563        "      <td>Acquisition</td>\n",
    564        "      <td>2008-01-08</td>\n",
    565        "      <td>MSFT</td>\n",
    566        "      <td>M&amp;A Announcement</td>\n",
    567        "      <td>Microsoft to Acquire Norway's Fast Search for ...</td>\n",
    568        "      <td>Announcement</td>\n",
    569        "      <td>Acquirer</td>\n",
    570        "      <td>Cash Offer</td>\n",
    571        "      <td>Public</td>\n",
    572        "      <td>Cross Border</td>\n",
    573        "      <td>1200</td>\n",
    574        "      <td>$M</td>\n",
    575        "      <td>FAST</td>\n",
    576        "      <td>Fast Search &amp; Transfer AS</td>\n",
    577        "      <td>1</td>\n",
    578        "      <td>3.54</td>\n",
    579        "      <td>42</td>\n",
    580        "      <td>5061</td>\n",
    581        "      <td>2008-01-08</td>\n",
    582        "      <td>2008-01-09</td>\n",
    583        "    </tr>\n",
    584        "  </tbody>\n",
    585        "</table>"
    586       ],
    587       "text/plain": [
    588        "   event_id     mna_type trade_date symbol        event_type  \\\n",
    589        "0     78373  Acquisition 2007-05-25   MSFT  M&A Announcement   \n",
    590        "1     78370         None 2007-08-13   MSFT  M&A Announcement   \n",
    591        "2    125294  Acquisition 2007-11-12   MSFT  M&A Announcement   \n",
    592        "3    134224         None 2007-12-12   MSFT  M&A Announcement   \n",
    593        "4    137589  Acquisition 2008-01-08   MSFT  M&A Announcement   \n",
    594        "\n",
    595        "                                      event_headline     news_type firm_type  \\\n",
    596        "0             Microsoft to Acquire aQuantive for $6B  Announcement  Acquirer   \n",
    597        "1  Microsoft Completes Acquisition of aQuantive, ...         Close      None   \n",
    598        "2            Microsoft Intends to Acquire Musiwave    Announcement  Acquirer   \n",
    599        "3  Microsoft Completes Acquisition of UK based Mu...         Close      None   \n",
    600        "4  Microsoft to Acquire Norway's Fast Search for ...  Announcement  Acquirer   \n",
    601        "\n",
    602        "  payment_mode target_type    ...     deal_amount  deal_currency  \\\n",
    603        "0   Cash Offer      Public    ...            6000             $M   \n",
    604        "1         None        None    ...               0           None   \n",
    605        "2   Cash Offer     Private    ...               0           None   \n",
    606        "3         None        None    ...               0           None   \n",
    607        "4   Cash Offer      Public    ...            1200             $M   \n",
    608        "\n",
    609        "  related_ticker             related_entity event_rating  price_pershare  \\\n",
    610        "0           AQNT                       None            1            0.00   \n",
    611        "1           None                       None            1            0.00   \n",
    612        "2           None                Musiwave SA            1            0.00   \n",
    613        "3           None                   Multimap            1            0.00   \n",
    614        "4           FAST  Fast Search & Transfer AS            1            3.54   \n",
    615        "\n",
    616        "   premium_pct   sid  asof_date  timestamp  \n",
    617        "0            0  5061 2007-05-24 2007-05-25  \n",
    618        "1            0  5061 2007-08-13 2007-08-14  \n",
    619        "2            0  5061 2007-11-12 2007-11-13  \n",
    620        "3            0  5061 2007-12-12 2007-12-13  \n",
    621        "4           42  5061 2008-01-08 2008-01-09  \n",
    622        "\n",
    623        "[5 rows x 21 columns]"
    624       ]
    625      },
    626      "execution_count": 8,
    627      "metadata": {},
    628      "output_type": "execute_result"
    629     }
    630    ],
    631    "source": [
    632     "# knowing that the MSFT sid is 5061:\n",
    633     "msft = dataset[dataset.sid==5061]\n",
    634     "msft[:5]"
    635    ]
    636   },
    637   {
    638    "cell_type": "markdown",
    639    "metadata": {},
    640    "source": [
    641     "<a id='pipeline'></a>\n",
    642     "\n",
    643     "#Pipeline Overview\n",
    644     "\n",
    645     "### Accessing the data in your algorithms & research\n",
    646     "The only method for accessing partner data within algorithms running on Quantopian is via the pipeline API.\n",
    647     "\n",
    648     "There are a few factors available using the M&A dataset through Pipeline. **They allow you to identify securities that are the current target of an acquisition.** You can also view the payment mode used in the offer as well as the number of business days since the offer was made."
    649    ]
    650   },
    651   {
    652    "cell_type": "code",
    653    "execution_count": 9,
    654    "metadata": {
    655     "collapsed": true
    656    },
    657    "outputs": [],
    658    "source": [
    659     "# Import necessary Pipeline modules\n",
    660     "from quantopian.pipeline import Pipeline\n",
    661     "from quantopian.research import run_pipeline\n",
    662     "from quantopian.pipeline.factors import AverageDollarVolume"
    663    ]
    664   },
    665   {
    666    "cell_type": "markdown",
    667    "metadata": {},
    668    "source": [
    669     "### Filtering out ANNOUNCED targets\n",
    670     "\n",
    671     "The following code below shows you how to filter out targets of acquisitions."
    672    ]
    673   },
    674   {
    675    "cell_type": "code",
    676    "execution_count": 11,
    677    "metadata": {
    678     "collapsed": false
    679    },
    680    "outputs": [],
    681    "source": [
    682     "from quantopian.pipeline.classifiers.eventvestor import (\n",
    683     "    AnnouncedAcqTargetType,\n",
    684     "    ProposedAcqTargetType,\n",
    685     ")\n",
    686     "from quantopian.pipeline.factors.eventvestor import (\n",
    687     "    BusinessDaysSinceAnnouncedAcquisition,\n",
    688     "    BusinessDaysSinceProposedAcquisition\n",
    689     ")\n",
    690     "from quantopian.pipeline.filters.eventvestor import (\n",
    691     "    IsAnnouncedAcqTarget\n",
    692     ")\n",
    693     "\n",
    694     "from quantopian.pipeline import Pipeline\n",
    695     "from quantopian.research import run_pipeline\n",
    696     "    \n",
    697     "def screen_ma_targets_by_type(target_type='cash'):\n",
    698     "    \"\"\"\n",
    699     "    target_type:\n",
    700     "        (string) Available options are 'cash', 'stock', 'mixed', 'all'.\n",
    701     "        This will filter all offers of type target_type.\n",
    702     "    \"\"\"\n",
    703     "    if target_type == 'all':\n",
    704     "        return (~IsAnnouncedAcqTarget())\n",
    705     "    else:\n",
    706     "        if target_type == 'cash':\n",
    707     "            filter_offer = 'Cash Offer'\n",
    708     "        elif target_type == 'stock':\n",
    709     "            filter_offer = 'Stock Offer'\n",
    710     "        elif target_type == 'mixed':\n",
    711     "            filter_offer = 'Mixed Offer'\n",
    712     "        return (~AnnouncedAcqTargetType().eq(filter_offer))\n",
    713     "    \n",
    714     "def screen_ma_targets_by_days(days=200):\n",
    715     "    \"\"\"\n",
    716     "    days:\n",
    717     "        (int) Filters out securities that have had an announcement\n",
    718     "        less than X days. So if days is 200, all securities\n",
    719     "        that have had an announcement less than 200 days ago will be\n",
    720     "        filtered out.\n",
    721     "    \"\"\"\n",
    722     "    b_days = BusinessDaysSinceAnnouncedAcquisition()\n",
    723     "    return ((b_days > days) | b_days.isnull())\n",
    724     "\n",
    725     "pipe = Pipeline(\n",
    726     "    columns={\n",
    727     "            'AnnouncedAcqTargetType': AnnouncedAcqTargetType(),\n",
    728     "            'BusinessDays': BusinessDaysSinceAnnouncedAcquisition()\n",
    729     "            },\n",
    730     "    screen=(screen_ma_targets_by_days(60) &\n",
    731     "            screen_ma_targets_by_type(target_type='stock'))\n",
    732     ")\n",
    733     "\n",
    734     "output = run_pipeline(pipe, start_date='2016-07-28', end_date='2016-07-28')"
    735    ]
    736   },
    737   {
    738    "cell_type": "markdown",
    739    "metadata": {},
    740    "source": [
    741     "### Filtering out PROPOSED targets\n",
    742     "\n",
    743     "If you'd also like to filter out proposed targets, please view below"
    744    ]
    745   },
    746   {
    747    "cell_type": "code",
    748    "execution_count": 12,
    749    "metadata": {
    750     "collapsed": true
    751    },
    752    "outputs": [],
    753    "source": [
    754     "\"\"\"\n",
    755     "Similar functions for M&A Proposals (different from Announcements)\n",
    756     "\"\"\"\n",
    757     "\n",
    758     "def screen_ma_proposal_targets_by_type(target_type='cash'):\n",
    759     "    \"\"\"\n",
    760     "    target_type:\n",
    761     "        (string) Available options are 'cash', 'stock', 'mixed', 'all'.\n",
    762     "        This will filter all offers of type target_type.\n",
    763     "    \"\"\"\n",
    764     "    if target_type == 'all':\n",
    765     "        return (ProposedAcqTargetType().isnull() &\n",
    766     "                BusinessDaysSinceProposedAcquisition().isnull())\n",
    767     "    if target_type == 'cash':\n",
    768     "        filter_offer = 'Cash Offer'\n",
    769     "    elif target_type == 'stock':\n",
    770     "        filter_offer = 'Stock Offer'\n",
    771     "    elif target_type == 'mixed':\n",
    772     "        filter_offer = 'Mixed Offer'\n",
    773     "    return (~ProposedAcqTargetType().eq(filter_offer))\n",
    774     "    \n",
    775     "def screen_ma_proposal_targets_by_days(days=200):\n",
    776     "    \"\"\"\n",
    777     "    days:\n",
    778     "        (int) Filters out securities that have had an announcement\n",
    779     "        less than X days. So if days is 200, all securities\n",
    780     "        that have had an announcement less than 200 days ago will be\n",
    781     "        filtered out.\n",
    782     "    \"\"\"\n",
    783     "    b_days = BusinessDaysSinceProposedAcquisition()\n",
    784     "    return ((b_days > days) | b_days.isnull())"
    785    ]
    786   }
    787  ],
    788  "metadata": {
    789   "kernelspec": {
    790    "display_name": "Python 2",
    791    "language": "python",
    792    "name": "python2"
    793   },
    794   "language_info": {
    795    "codemirror_mode": {
    796     "name": "ipython",
    797     "version": 2
    798    },
    799    "file_extension": ".py",
    800    "mimetype": "text/x-python",
    801    "name": "python",
    802    "nbconvert_exporter": "python",
    803    "pygments_lexer": "ipython2",
    804    "version": "2.7.12"
    805   }
    806  },
    807  "nbformat": 4,
    808  "nbformat_minor": 0
    809 }