python assignment using quantopian
Project 2.ipynb { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Project 2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Import Packages" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "# Data Science Packages\n", "import numpy as np\n", "import pandas as pd\n", "import matplotlib.pyplot as plt\n", "import seaborn as sns\n", "import scipy as sp\n", "\n", "# Pipeline\n", "from quantopian.pipeline import Pipeline, CustomFactor\n", "from quantopian.pipeline.data.builtin import USEquityPricing\n", "from quantopian.pipeline.domain import US_EQUITIES\n", "from quantopian.research import run_pipeline\n", "from quantopian.pipeline.factors import AverageDollarVolume, DailyReturns\n", "\n", "# Fundamentals\n", "from quantopian.pipeline.data.morningstar import Fundamentals\n", "\n", "# Morningstar\n", "from quantopian.pipeline.data import morningstar as ms" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Create Pipeline" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "", "version_major": 2, "version_minor": 0 }, "text/plain": [] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
Pipeline Execution Time:53.51 Seconds" ], "text/plain": [ "
" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# Initialize fundamental metrics\n", "current_price = USEquityPricing.close.latest\n", "book_value_per_share = Fundamentals.book_value_per_share.latest\n", "price_book_ratio = Fundamentals.pb_ratio.latest\n", "debt_to_equity = Fundamentals.long_term_debt_equity_ratio.latest\n", "gross_profit = Fundamentals.gross_profit.latest\n", "gross_margin = Fundamentals.gross_margin.latest\n", "market_cap = ms.valuation.market_cap.latest\n", "\n", "# Initialize Daily Returns\n", "returns = DailyReturns(inputs=[USEquityPricing.close], window_length=2)\n", "\n", "# Initialize Moningstar Classifications\n", "sphere = ms.asset_classification.morningstar_economy_sphere_code.latest \n", "industry_code = ms.asset_classification.morningstar_industry_code.latest \n", "sector = ms.asset_classification.morningstar_sector_code.latest\n", "\n", "\n", "# Build Pipeline\n", "pipe = Pipeline(\n", " columns={\n", " 'price': current_price,\n", " 'book_value_per_share' : book_value_per_share,\n", " 'price_to_book': price_book_ratio,\n", " 'debt_to_equity' : debt_to_equity,\n", " 'gross_profit' : gross_profit,\n", " 'gross_margin' : gross_margin,\n", " 'returns': returns,\n", " 'sphere': sphere,\n", " 'industry' : industry_code,\n", " 'sector': sector,\n", " 'market_cap': market_cap\n", " },\n", " screen=market_cap.top(500),\n", " domain=US_EQUITIES,\n", ")\n", "\n", "# Run the pipeline\n", "df = run_pipeline(pipe, '2018-01-01', '2020-03-31').reset_index(drop=False)\n", "df = df.rename(columns={'level_0':'datetime', 'level_1':'equity'})\n", "df['equity'] = df['equity'].apply(lambda x: x.symbol)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Reformat DataFrame" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "MORNINGSTAR_SECTOR_CODES = { \n", " -1: 'Misc', \n", " 101: 'Basic Materials', \n", " 102: 'Consumer Cyclical', \n", " 103: 'Financial Services', \n", " 104: 'Real Estate', \n", " 205: 'Consumer Defensive', \n", " 206: 'Healthcare', \n", " 207: 'Utilities', \n", " 308: 'Communication Services', \n", " 309: 'Energy', \n", " 310: 'Industrials', \n", " 311: 'Technology' , \n", "}\n", "\n", "df['sector'] = df['sector'].replace(MORNINGSTAR_SECTOR_CODES)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "MORNINGSTAR_INDUSTRY_CODES = { \n", " 10101001: \"Agricultural Inputs\", \n", " 10102002: \"Building Materials\", \n", " 10103003: \"Chemicals\", \n", " 10103004: \"Specialty Chemicals\", \n", " 10104005: \"Coal\", \n", " 10105006: \"Lumber & Wood Production\", \n", " 10105007: \"Paper & Paper Products\", \n", " 10106008: \"Aluminum\", \n", " 10106009: \"Copper\", \n", " 10106010: \"Gold\", \n", " 10106011: \"Industrial Metals & Minerals\", \n", " 10106012: \"Silver\", \n", " 10107013: \"Steel\", \n", " 10208014: \"Advertising Agencies\", \n", " 10208015: \"Marketing Services\", \n", " 10209016: \"Auto & Truck Dealerships\", \n", " 10209017: \"Auto Manufacturers\", \n", " 10209018: \"Auto Parts\", \n", " 10209019: \"Recreational Vehicles\", \n", " 10209020: \"Rubber & Plastics\", \n", " 10210021: \"Broadcasting - Radio\", \n", " 10210022: \"Broadcasting - TV\", \n", " 10210023: \"Media - Diversified\", \n", " 10211024: \"Residential Construction\", \n", " 10211025: \"Textile Manufacturing\", \n", " 10212026: \"Apparel Manufacturing\", \n", " 10212027: \"Footwear & Accessories\", \n", " 10212028: \"Home Furnishings & Fixtures\", \n", " 10213029: \"Packaging & Containers\", \n", " 10214030: \"Personal Services\", \n", " 10215031: \"Publishing\", \n", " 10216032: \"Restaurants\", \n", " 10217033: \"Apparel Stores\", \n", " 10217034: \"Department Stores\", \n", " 10217035: \"Home Improvement Stores\", \n", " 10217036: \"Luxury Goods\", \n", " 10217037: \"Specialty Retail\", \n", " 10218038: \"Gambling\", \n", " 10218039: \"Leisure\", \n", " 10218040: \"Lodging\", \n", " 10218041: \"Resorts & Casinos\", \n", " 10319042: \"Asset Management\", \n", " 10320043: \"Banks - Global\", \n", " 10320044: \"Banks - Regional - Africa\", \n", " 10320045: \"Banks - Regional - Asia\", \n", " 10320046: \"Banks - Regional - Australia\", \n", " 10320047: \"Banks - Regional - Canada\", \n", " 10320048: \"Banks - Regional - Europe\", \n",