{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Universe Selection\n", "\n", "by Gil Wassermann, Maxwell Margenot\n", "\n", "Part of the Quantopian Lecture Series:\n", "\n", "* [www.quantopian.com/lectures](https://www.quantopian.com/lectures)\n", "* [github.com/quantopian/research_public](https://github.com/quantopian/research_public)\n", "\n", "Notebook released under the Creative Commons Attribution 4.0 License.\n", "\n", "---\n", "\n", "Selecting the product space in which an algorithm trades can be as important as, if not more than, the strategy itself. In this lecture, we will walk through the basics of constructing a universe and introduce Quantopian's new tools that allow you to accomplish these often tricky and subtle calculations with ease." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## What is a Universe?\n", "\n", "On a high level, universe selection is the process of choosing the pool of securities upon which your algorithm will trade. For example, an algorithm designed to play with the characteristics of a universe consisting of technology equities may perform exceptionally well in that universe with the tradeoff of falling flat in other sectors. Experimenting with different universes by tweaking their components is an essential part of developing a trading strategy.\n", "\n", "Using Pipeline, we have access to around 8300 securities to choose from each day. However, the securities within this basket are markedly different. Some are different asset classes, some belong to different sectors and super-sectors, some employ different business models, some practice different management styles, and so on. By defining a universe, a trader can narrow in on securities with one or more of these attributes in order to craft a strategy that is most effective for that subset of the population.\n", "\n", "Without a properly-constructed universe, your algorithm may be exposed to risks that you just aren't aware of. For example, it could be possible that your universe selection methodology only selects a stock basket whose constituents do not trade very often. Let's say that your algorithm wants to place an order of 100,000 shares for a company that only trades 1,000 on a given day. The inability to fill this order or others might prevent you from achieving the optimal weights for your portfolio, thereby undermining your strategy. These risks can be controlled for by careful and thoughtful universe slection.\n", "\n", "On the Quantopian platform, universes are often implemented as a Pipeline screen. If you are not familiar with Pipeline, feel free to check out the [tutorial](https://www.quantopian.com/tutorials/pipeline). Below is an example implementation of a universe that limits Pipeline output to the 500 securities with the largest revenue each day. This can be seen as a naive implementation of the Fortune500." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import numpy as np\n", "import pandas as pd\n", "import matplotlib.pyplot as plt\n", "from quantopian.pipeline.classifiers.morningstar import Sector\n", "from quantopian.pipeline import Pipeline\n", "from quantopian.pipeline.data.builtin import USEquityPricing\n", "from quantopian.research import run_pipeline\n", "from quantopian.pipeline.data import morningstar\n", "from quantopian.pipeline.factors import CustomFactor" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "There are 500 assets in this universe.\n" ] }, { "data": { "text/html": [ "
| \n", " | \n", " | Revenue | \n", "
|---|---|---|
| 2016-01-04 00:00:00+00:00 | \n", "Equity(2 [ARNC]) | \n", "5.573000e+09 | \n", "
| Equity(24 [AAPL]) | \n", "5.150100e+10 | \n", "|
| Equity(25 [ARNC_PR]) | \n", "5.573000e+09 | \n", "|
| Equity(62 [ABT]) | \n", "5.150000e+09 | \n", "|
| Equity(64 [ABX]) | \n", "2.315000e+09 | \n", "|
| Equity(100 [IEP]) | \n", "3.212000e+09 | \n", "|
| Equity(128 [ADM]) | \n", "1.656500e+10 | \n", "|
| Equity(161 [AEP]) | \n", "4.432000e+09 | \n", "|
| Equity(166 [AES]) | \n", "3.721000e+09 | \n", "|
| Equity(168 [AET]) | \n", "1.495300e+10 | \n", "