ml-finance-python

python scripts for finance machine learning

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

notebook.ipynb

(17477B)


      1 {
      2  "cells": [
      3   {
      4    "cell_type": "markdown",
      5    "metadata": {},
      6    "source": [
      7     "##Combining Factors\n",
      8     "Factors can be combined, both with other Factors and with scalar values, via any of the builtin mathematical operators (+, -, *, etc). This makes it easy to write complex expressions that combine multiple Factors. For example, constructing a Factor that computes the average of two other Factors is simply:\n",
      9     "```\n",
     10     ">>> f1 = SomeFactor(...)\n",
     11     ">>> f2 = SomeOtherFactor(...)\n",
     12     ">>> average = (f1 + f2) / 2.0\n",
     13     "```\n",
     14     "In this lesson, we will create a pipeline that creates a `relative_difference` factor by combining a 10-day average factor and a 30-day average factor. \n",
     15     "\n",
     16     "As usual, let's start with our imports:"
     17    ]
     18   },
     19   {
     20    "cell_type": "code",
     21    "execution_count": 1,
     22    "metadata": {
     23     "collapsed": true
     24    },
     25    "outputs": [],
     26    "source": [
     27     "from quantopian.pipeline import Pipeline\n",
     28     "from quantopian.research import run_pipeline\n",
     29     "from quantopian.pipeline.data.builtin import USEquityPricing\n",
     30     "from quantopian.pipeline.factors import SimpleMovingAverage"
     31    ]
     32   },
     33   {
     34    "cell_type": "markdown",
     35    "metadata": {},
     36    "source": [
     37     "For this example, we need two factors: a 10-day mean close price factor, and a 30-day one:"
     38    ]
     39   },
     40   {
     41    "cell_type": "code",
     42    "execution_count": 2,
     43    "metadata": {
     44     "collapsed": false
     45    },
     46    "outputs": [],
     47    "source": [
     48     "mean_close_10 = SimpleMovingAverage(inputs=[USEquityPricing.close], window_length=10)\n",
     49     "mean_close_30 = SimpleMovingAverage(inputs=[USEquityPricing.close], window_length=30)"
     50    ]
     51   },
     52   {
     53    "cell_type": "markdown",
     54    "metadata": {},
     55    "source": [
     56     "Then, let's create a percent difference factor by combining our `mean_close_30` factor with our `mean_close_10` factor."
     57    ]
     58   },
     59   {
     60    "cell_type": "code",
     61    "execution_count": 3,
     62    "metadata": {
     63     "collapsed": false
     64    },
     65    "outputs": [],
     66    "source": [
     67     "percent_difference = (mean_close_10 - mean_close_30) / mean_close_30"
     68    ]
     69   },
     70   {
     71    "cell_type": "markdown",
     72    "metadata": {},
     73    "source": [
     74     "In this example, `percent_difference` is still a `Factor` even though it's composed as a combination of more primitive factors. We can add `percent_difference` as a column in our pipeline. Let's define `make_pipeline` to create a pipeline with `percent_difference` as a column (and not the mean close factors):"
     75    ]
     76   },
     77   {
     78    "cell_type": "code",
     79    "execution_count": 4,
     80    "metadata": {
     81     "collapsed": true
     82    },
     83    "outputs": [],
     84    "source": [
     85     "def make_pipeline():\n",
     86     "\n",
     87     "    mean_close_10 = SimpleMovingAverage(inputs=[USEquityPricing.close], window_length=10)\n",
     88     "    mean_close_30 = SimpleMovingAverage(inputs=[USEquityPricing.close], window_length=30)\n",
     89     "\n",
     90     "    percent_difference = (mean_close_10 - mean_close_30) / mean_close_30\n",
     91     "\n",
     92     "    return Pipeline(\n",
     93     "        columns={\n",
     94     "            'percent_difference': percent_difference\n",
     95     "        }\n",
     96     "    )"
     97    ]
     98   },
     99   {
    100    "cell_type": "markdown",
    101    "metadata": {},
    102    "source": [
    103     "Let's see what the new output looks like:"
    104    ]
    105   },
    106   {
    107    "cell_type": "code",
    108    "execution_count": 6,
    109    "metadata": {
    110     "collapsed": false
    111    },
    112    "outputs": [
    113     {
    114      "data": {
    115       "text/html": [
    116        "<div style=\"max-height:1000px;max-width:1500px;overflow:auto;\">\n",
    117        "<table border=\"1\" class=\"dataframe\">\n",
    118        "  <thead>\n",
    119        "    <tr style=\"text-align: right;\">\n",
    120        "      <th></th>\n",
    121        "      <th></th>\n",
    122        "      <th>percent_difference</th>\n",
    123        "    </tr>\n",
    124        "  </thead>\n",
    125        "  <tbody>\n",
    126        "    <tr>\n",
    127        "      <th rowspan=\"61\" valign=\"top\">2015-05-05 00:00:00+00:00</th>\n",
    128        "      <th>Equity(2 [AA])</th>\n",
    129        "      <td>0.017975</td>\n",
    130        "    </tr>\n",
    131        "    <tr>\n",
    132        "      <th>Equity(21 [AAME])</th>\n",
    133        "      <td>-0.002325</td>\n",
    134        "    </tr>\n",
    135        "    <tr>\n",
    136        "      <th>Equity(24 [AAPL])</th>\n",
    137        "      <td>0.016905</td>\n",
    138        "    </tr>\n",
    139        "    <tr>\n",
    140        "      <th>Equity(25 [AA_PR])</th>\n",
    141        "      <td>0.021544</td>\n",
    142        "    </tr>\n",
    143        "    <tr>\n",
    144        "      <th>Equity(31 [ABAX])</th>\n",
    145        "      <td>-0.019639</td>\n",
    146        "    </tr>\n",
    147        "    <tr>\n",
    148        "      <th>Equity(39 [DDC])</th>\n",
    149        "      <td>0.074730</td>\n",
    150        "    </tr>\n",
    151        "    <tr>\n",
    152        "      <th>Equity(41 [ARCB])</th>\n",
    153        "      <td>0.007067</td>\n",
    154        "    </tr>\n",
    155        "    <tr>\n",
    156        "      <th>Equity(52 [ABM])</th>\n",
    157        "      <td>0.003340</td>\n",
    158        "    </tr>\n",
    159        "    <tr>\n",
    160        "      <th>Equity(53 [ABMD])</th>\n",
    161        "      <td>-0.024682</td>\n",
    162        "    </tr>\n",
    163        "    <tr>\n",
    164        "      <th>Equity(62 [ABT])</th>\n",
    165        "      <td>0.014385</td>\n",
    166        "    </tr>\n",
    167        "    <tr>\n",
    168        "      <th>Equity(64 [ABX])</th>\n",
    169        "      <td>0.046963</td>\n",
    170        "    </tr>\n",
    171        "    <tr>\n",
    172        "      <th>Equity(66 [AB])</th>\n",
    173        "      <td>0.013488</td>\n",
    174        "    </tr>\n",
    175        "    <tr>\n",
    176        "      <th>Equity(67 [ADSK])</th>\n",
    177        "      <td>-0.003921</td>\n",
    178        "    </tr>\n",
    179        "    <tr>\n",
    180        "      <th>Equity(69 [ACAT])</th>\n",
    181        "      <td>-0.007079</td>\n",
    182        "    </tr>\n",
    183        "    <tr>\n",
    184        "      <th>Equity(70 [VBF])</th>\n",
    185        "      <td>0.005507</td>\n",
    186        "    </tr>\n",
    187        "    <tr>\n",
    188        "      <th>Equity(76 [TAP])</th>\n",
    189        "      <td>-0.008759</td>\n",
    190        "    </tr>\n",
    191        "    <tr>\n",
    192        "      <th>Equity(84 [ACET])</th>\n",
    193        "      <td>-0.056139</td>\n",
    194        "    </tr>\n",
    195        "    <tr>\n",
    196        "      <th>Equity(86 [ACG])</th>\n",
    197        "      <td>0.010096</td>\n",
    198        "    </tr>\n",
    199        "    <tr>\n",
    200        "      <th>Equity(88 [ACI])</th>\n",
    201        "      <td>-0.022089</td>\n",
    202        "    </tr>\n",
    203        "    <tr>\n",
    204        "      <th>Equity(100 [IEP])</th>\n",
    205        "      <td>0.011293</td>\n",
    206        "    </tr>\n",
    207        "    <tr>\n",
    208        "      <th>Equity(106 [ACU])</th>\n",
    209        "      <td>0.003306</td>\n",
    210        "    </tr>\n",
    211        "    <tr>\n",
    212        "      <th>Equity(110 [ACXM])</th>\n",
    213        "      <td>-0.029551</td>\n",
    214        "    </tr>\n",
    215        "    <tr>\n",
    216        "      <th>Equity(112 [ACY])</th>\n",
    217        "      <td>-0.057763</td>\n",
    218        "    </tr>\n",
    219        "    <tr>\n",
    220        "      <th>Equity(114 [ADBE])</th>\n",
    221        "      <td>0.009499</td>\n",
    222        "    </tr>\n",
    223        "    <tr>\n",
    224        "      <th>Equity(117 [AEY])</th>\n",
    225        "      <td>0.012543</td>\n",
    226        "    </tr>\n",
    227        "    <tr>\n",
    228        "      <th>Equity(122 [ADI])</th>\n",
    229        "      <td>0.009271</td>\n",
    230        "    </tr>\n",
    231        "    <tr>\n",
    232        "      <th>Equity(128 [ADM])</th>\n",
    233        "      <td>0.015760</td>\n",
    234        "    </tr>\n",
    235        "    <tr>\n",
    236        "      <th>Equity(134 [SXCL])</th>\n",
    237        "      <td>NaN</td>\n",
    238        "    </tr>\n",
    239        "    <tr>\n",
    240        "      <th>Equity(149 [ADX])</th>\n",
    241        "      <td>0.007232</td>\n",
    242        "    </tr>\n",
    243        "    <tr>\n",
    244        "      <th>Equity(153 [AE])</th>\n",
    245        "      <td>-0.112999</td>\n",
    246        "    </tr>\n",
    247        "    <tr>\n",
    248        "      <th>...</th>\n",
    249        "      <td>...</td>\n",
    250        "    </tr>\n",
    251        "    <tr>\n",
    252        "      <th>Equity(48961 [NYMT_O])</th>\n",
    253        "      <td>NaN</td>\n",
    254        "    </tr>\n",
    255        "    <tr>\n",
    256        "      <th>Equity(48962 [CSAL])</th>\n",
    257        "      <td>0.000000</td>\n",
    258        "    </tr>\n",
    259        "    <tr>\n",
    260        "      <th>Equity(48963 [PAK])</th>\n",
    261        "      <td>0.000000</td>\n",
    262        "    </tr>\n",
    263        "    <tr>\n",
    264        "      <th>Equity(48969 [NSA])</th>\n",
    265        "      <td>0.000000</td>\n",
    266        "    </tr>\n",
    267        "    <tr>\n",
    268        "      <th>Equity(48971 [BSM])</th>\n",
    269        "      <td>0.000000</td>\n",
    270        "    </tr>\n",
    271        "    <tr>\n",
    272        "      <th>Equity(48972 [EVA])</th>\n",
    273        "      <td>0.000000</td>\n",
    274        "    </tr>\n",
    275        "    <tr>\n",
    276        "      <th>Equity(48981 [APIC])</th>\n",
    277        "      <td>0.000000</td>\n",
    278        "    </tr>\n",
    279        "    <tr>\n",
    280        "      <th>Equity(48989 [UK])</th>\n",
    281        "      <td>0.000000</td>\n",
    282        "    </tr>\n",
    283        "    <tr>\n",
    284        "      <th>Equity(48990 [ACWF])</th>\n",
    285        "      <td>0.000000</td>\n",
    286        "    </tr>\n",
    287        "    <tr>\n",
    288        "      <th>Equity(48991 [ISCF])</th>\n",
    289        "      <td>0.000000</td>\n",
    290        "    </tr>\n",
    291        "    <tr>\n",
    292        "      <th>Equity(48992 [INTF])</th>\n",
    293        "      <td>0.000000</td>\n",
    294        "    </tr>\n",
    295        "    <tr>\n",
    296        "      <th>Equity(48993 [JETS])</th>\n",
    297        "      <td>0.000000</td>\n",
    298        "    </tr>\n",
    299        "    <tr>\n",
    300        "      <th>Equity(48994 [ACTX])</th>\n",
    301        "      <td>0.000000</td>\n",
    302        "    </tr>\n",
    303        "    <tr>\n",
    304        "      <th>Equity(48995 [LRGF])</th>\n",
    305        "      <td>0.000000</td>\n",
    306        "    </tr>\n",
    307        "    <tr>\n",
    308        "      <th>Equity(48996 [SMLF])</th>\n",
    309        "      <td>0.000000</td>\n",
    310        "    </tr>\n",
    311        "    <tr>\n",
    312        "      <th>Equity(48997 [VKTX])</th>\n",
    313        "      <td>0.000000</td>\n",
    314        "    </tr>\n",
    315        "    <tr>\n",
    316        "      <th>Equity(48998 [OPGN])</th>\n",
    317        "      <td>NaN</td>\n",
    318        "    </tr>\n",
    319        "    <tr>\n",
    320        "      <th>Equity(48999 [AAPC])</th>\n",
    321        "      <td>0.000000</td>\n",
    322        "    </tr>\n",
    323        "    <tr>\n",
    324        "      <th>Equity(49000 [BPMC])</th>\n",
    325        "      <td>0.000000</td>\n",
    326        "    </tr>\n",
    327        "    <tr>\n",
    328        "      <th>Equity(49001 [CLCD])</th>\n",
    329        "      <td>NaN</td>\n",
    330        "    </tr>\n",
    331        "    <tr>\n",
    332        "      <th>Equity(49004 [TNP_PRD])</th>\n",
    333        "      <td>0.000000</td>\n",
    334        "    </tr>\n",
    335        "    <tr>\n",
    336        "      <th>Equity(49005 [ARWA_U])</th>\n",
    337        "      <td>NaN</td>\n",
    338        "    </tr>\n",
    339        "    <tr>\n",
    340        "      <th>Equity(49006 [BVXV])</th>\n",
    341        "      <td>NaN</td>\n",
    342        "    </tr>\n",
    343        "    <tr>\n",
    344        "      <th>Equity(49007 [BVXV_W])</th>\n",
    345        "      <td>NaN</td>\n",
    346        "    </tr>\n",
    347        "    <tr>\n",
    348        "      <th>Equity(49008 [OPGN_W])</th>\n",
    349        "      <td>NaN</td>\n",
    350        "    </tr>\n",
    351        "    <tr>\n",
    352        "      <th>Equity(49009 [PRKU])</th>\n",
    353        "      <td>NaN</td>\n",
    354        "    </tr>\n",
    355        "    <tr>\n",
    356        "      <th>Equity(49010 [TBRA])</th>\n",
    357        "      <td>NaN</td>\n",
    358        "    </tr>\n",
    359        "    <tr>\n",
    360        "      <th>Equity(49131 [OESX])</th>\n",
    361        "      <td>NaN</td>\n",
    362        "    </tr>\n",
    363        "    <tr>\n",
    364        "      <th>Equity(49259 [ITUS])</th>\n",
    365        "      <td>NaN</td>\n",
    366        "    </tr>\n",
    367        "    <tr>\n",
    368        "      <th>Equity(49523 [TLGT])</th>\n",
    369        "      <td>NaN</td>\n",
    370        "    </tr>\n",
    371        "  </tbody>\n",
    372        "</table>\n",
    373        "<p>8235 rows × 1 columns</p>\n",
    374        "</div>"
    375       ],
    376       "text/plain": [
    377        "                                                   percent_difference\n",
    378        "2015-05-05 00:00:00+00:00 Equity(2 [AA])                     0.017975\n",
    379        "                          Equity(21 [AAME])                 -0.002325\n",
    380        "                          Equity(24 [AAPL])                  0.016905\n",
    381        "                          Equity(25 [AA_PR])                 0.021544\n",
    382        "                          Equity(31 [ABAX])                 -0.019639\n",
    383        "                          Equity(39 [DDC])                   0.074730\n",
    384        "                          Equity(41 [ARCB])                  0.007067\n",
    385        "                          Equity(52 [ABM])                   0.003340\n",
    386        "                          Equity(53 [ABMD])                 -0.024682\n",
    387        "                          Equity(62 [ABT])                   0.014385\n",
    388        "                          Equity(64 [ABX])                   0.046963\n",
    389        "                          Equity(66 [AB])                    0.013488\n",
    390        "                          Equity(67 [ADSK])                 -0.003921\n",
    391        "                          Equity(69 [ACAT])                 -0.007079\n",
    392        "                          Equity(70 [VBF])                   0.005507\n",
    393        "                          Equity(76 [TAP])                  -0.008759\n",
    394        "                          Equity(84 [ACET])                 -0.056139\n",
    395        "                          Equity(86 [ACG])                   0.010096\n",
    396        "                          Equity(88 [ACI])                  -0.022089\n",
    397        "                          Equity(100 [IEP])                  0.011293\n",
    398        "                          Equity(106 [ACU])                  0.003306\n",
    399        "                          Equity(110 [ACXM])                -0.029551\n",
    400        "                          Equity(112 [ACY])                 -0.057763\n",
    401        "                          Equity(114 [ADBE])                 0.009499\n",
    402        "                          Equity(117 [AEY])                  0.012543\n",
    403        "                          Equity(122 [ADI])                  0.009271\n",
    404        "                          Equity(128 [ADM])                  0.015760\n",
    405        "                          Equity(134 [SXCL])                      NaN\n",
    406        "                          Equity(149 [ADX])                  0.007232\n",
    407        "                          Equity(153 [AE])                  -0.112999\n",
    408        "...                                                               ...\n",
    409        "                          Equity(48961 [NYMT_O])                  NaN\n",
    410        "                          Equity(48962 [CSAL])               0.000000\n",
    411        "                          Equity(48963 [PAK])                0.000000\n",
    412        "                          Equity(48969 [NSA])                0.000000\n",
    413        "                          Equity(48971 [BSM])                0.000000\n",
    414        "                          Equity(48972 [EVA])                0.000000\n",
    415        "                          Equity(48981 [APIC])               0.000000\n",
    416        "                          Equity(48989 [UK])                 0.000000\n",
    417        "                          Equity(48990 [ACWF])               0.000000\n",
    418        "                          Equity(48991 [ISCF])               0.000000\n",
    419        "                          Equity(48992 [INTF])               0.000000\n",
    420        "                          Equity(48993 [JETS])               0.000000\n",
    421        "                          Equity(48994 [ACTX])               0.000000\n",
    422        "                          Equity(48995 [LRGF])               0.000000\n",
    423        "                          Equity(48996 [SMLF])               0.000000\n",
    424        "                          Equity(48997 [VKTX])               0.000000\n",
    425        "                          Equity(48998 [OPGN])                    NaN\n",
    426        "                          Equity(48999 [AAPC])               0.000000\n",
    427        "                          Equity(49000 [BPMC])               0.000000\n",
    428        "                          Equity(49001 [CLCD])                    NaN\n",
    429        "                          Equity(49004 [TNP_PRD])            0.000000\n",
    430        "                          Equity(49005 [ARWA_U])                  NaN\n",
    431        "                          Equity(49006 [BVXV])                    NaN\n",
    432        "                          Equity(49007 [BVXV_W])                  NaN\n",
    433        "                          Equity(49008 [OPGN_W])                  NaN\n",
    434        "                          Equity(49009 [PRKU])                    NaN\n",
    435        "                          Equity(49010 [TBRA])                    NaN\n",
    436        "                          Equity(49131 [OESX])                    NaN\n",
    437        "                          Equity(49259 [ITUS])                    NaN\n",
    438        "                          Equity(49523 [TLGT])                    NaN\n",
    439        "\n",
    440        "[8235 rows x 1 columns]"
    441       ]
    442      },
    443      "execution_count": 6,
    444      "metadata": {},
    445      "output_type": "execute_result"
    446     }
    447    ],
    448    "source": [
    449     "result = run_pipeline(make_pipeline(), '2015-05-05', '2015-05-05')\n",
    450     "result"
    451    ]
    452   },
    453   {
    454    "cell_type": "markdown",
    455    "metadata": {},
    456    "source": [
    457     "In the next lesson, we will learn about filters."
    458    ]
    459   }
    460  ],
    461  "metadata": {
    462   "kernelspec": {
    463    "display_name": "Python 2",
    464    "language": "python",
    465    "name": "python2"
    466   },
    467   "language_info": {
    468    "codemirror_mode": {
    469     "name": "ipython",
    470     "version": 2
    471    },
    472    "file_extension": ".py",
    473    "mimetype": "text/x-python",
    474    "name": "python",
    475    "nbconvert_exporter": "python",
    476    "pygments_lexer": "ipython2",
    477    "version": "2.7.11"
    478   }
    479  },
    480  "nbformat": 4,
    481  "nbformat_minor": 0
    482 }