Skip to content

Instantly share code, notes, and snippets.

@shinseitaro
Last active May 25, 2020 09:07
Show Gist options
  • Save shinseitaro/b84242888c6a491a4004431179b4f72a to your computer and use it in GitHub Desktop.
Save shinseitaro/b84242888c6a491a4004431179b4f72a to your computer and use it in GitHub Desktop.
2020 YTD stock change by sector

第一波で大きく下落したセクターを調べる

MORNINGSTAR_INDUSTRY_GROUP_CODES_after_Nov2019 =  {10110: 'Agricultural Inputs',
 10120: 'Building Materials',
 10130: 'Chemicals,Specialty Chemicals',
 10140: 'Lumber & Wood Production,Paper & Paper Products',
 10150: 'Aluminum,Copper,Other Industrial Metals & Mining,Gold,Silver,Other Precious Metals & Mining',
 10160: 'Coking Coal,Steel',
 10200: 'Auto & Truck Dealerships,Auto Manufacturers,Auto Parts,Recreational Vehicles',
 10220: 'Furnishings, Fixtures & Appliances',
 10230: 'Residential Construction',
 10240: 'Textile Manufacturing,Apparel Manufacturing,Footwear & Accessories',
 10250: 'Packaging & Containers',
 10260: 'Personal Services',
 10270: 'Restaurants',
 10280: 'Apparel Retail,Department Stores,Home Improvement Retail,Luxury Goods,Internet Retail,Specialty Retail',
 10290: 'Gambling,Leisure,Lodging,Resorts & Casinos,Travel Services',
 10310: 'Asset Management',
 10320: 'Banks—Diversified,Banks—Regional,Mortgage Finance',
 10330: 'Capital Markets,Financial Data & Stock Exchanges',
 10340: 'Insurance—Life,Insurance—Property & Casualty,Insurance—Reinsurance,Insurance—Specialty,Insurance Brokers,Insurance—Diversified',
 10350: 'Shell Companies,Financial Conglomerates',
 10360: 'Credit Services',
 10410: 'Real Estate—Development,Real Estate Services,Real Estate—Diversified',
 10420: 'REIT—Healthcare Facilities,REIT—Hotel & Motel,REIT—Industrial,REIT—Office,REIT—Residential,REIT—Retail,REIT—Mortgage,REIT—Specialty,REIT—Diversified',
 20510: 'Beverages—Brewers,Beverages—Wineries & Distilleries',
 20520: 'Beverages—Non-Alcoholic',
 20525: 'Confectioners,Farm Products,Household & Personal Products,Packaged Foods',
 20540: 'Education & Training Services',
 20550: 'Discount Stores,Food Distribution,Grocery Stores',
 20560: 'Tobacco',
 20610: 'Biotechnology',
 20620: 'Drug Manufacturers—General,Drug Manufacturers—Specialty & Generic',
 20630: 'Healthcare Plans',
 20645: 'Medical Care Facilities,Pharmaceutical Retailers,Health Information Services',
 20650: 'Medical Devices,Medical Instruments & Supplies',
 20660: 'Diagnostics & Research',
 20670: 'Medical Distribution',
 20710: 'Utilities—Independent Power Producers,Utilities—Renewable',
 20720: 'Utilities—Regulated Water,Utilities—Regulated Electric,Utilities—Regulated Gas,Utilities—Diversified',
 30810: 'Telecom Services',
 30820: 'Advertising Agencies,Publishing,Broadcasting,Entertainment',
 30830: 'Internet Content & Information,Electronic Gaming & Multimedia',
 30910: 'Oil & Gas Drilling,Oil & Gas E&P,Oil & Gas Integrated,Oil & Gas Midstream,Oil & Gas Refining & Marketing,Oil & Gas Equipment & Services',
 30920: 'Thermal Coal,Uranium',
 31010: 'Aerospace & Defense',
 31020: 'Specialty Business Services,Consulting Services,Rental & Leasing Services,Security & Protection Services,Staffing & Employment Services',
 31030: 'Conglomerates',
 31040: 'Engineering & Construction,Infrastructure Operations,Building Products & Equipment',
 31050: 'Farm & Heavy Construction Machinery',
 31060: 'Industrial Distribution',
 31070: 'Business Equipment & Supplies,Specialty Industrial Machinery,Metal Fabrication,Pollution & Treatment Controls,Tools & Accessories,Electrical Equipment & Parts',
 31080: 'Airports & Air Services,Airlines,Railroads,Marine Shipping,Trucking,Integrated Freight & Logistics',
 31090: 'Waste Management',
 31110: 'Information Technology Services,Software—Application,Software—Infrastructure',
 31120: 'Communication Equipment,Computer Hardware,Consumer Electronics,Electronic Components,Electronics & Computer Distribution,Scientific & Technical Instruments',
 31130: 'Semiconductor Equipment & Materials,Semiconductors,Solar'}
import numpy as np

from quantopian.pipeline import Pipeline
from quantopian.pipeline.data.morningstar import Fundamentals
from quantopian.research import run_pipeline
from quantopian.pipeline.filters import QTradableStocksUS
from quantopian.pipeline.data import USEquityPricing
from quantopian.pipeline.factors import Returns



def make_pipeline():
    
    base_universe = QTradableStocksUS()
    close_price = USEquityPricing.close.latest

    morningstar_industry_group_code = Fundamentals.morningstar_industry_group_code.latest
    prev_change = Returns(inputs = [USEquityPricing.close], window_length=2)
    
    
    pipe = Pipeline(
        columns={
            'morningstar_industry_group_code':morningstar_industry_group_code, 
            'close_price':close_price, 
            "prev_change": prev_change,
            
        },
        
        screen=base_universe,
    )
    return pipe 

df = run_pipeline(make_pipeline(), '2020-01-01', '2020-05-22')
df["sector_name"] = df["morningstar_industry_group_code"].replace(MORNINGSTAR_INDUSTRY_GROUP_CODES_after_Nov2019)

df.head()

Pipeline Execution Time: 1.14 Seconds

close_price morningstar_industry_group_code prev_change sector_name
2020-01-02 00:00:00+00:00 Equity(2 [HWM]) 30.76 31070 -0.003563 Business Equipment & Supplies,Specialty Indust...
Equity(24 [AAPL]) 293.41 31120 0.006045 Communication Equipment,Computer Hardware,Cons...
Equity(41 [ARCB]) 27.60 31080 0.013216 Airports & Air Services,Airlines,Railroads,Mar...
Equity(52 [ABM]) 37.71 31020 -0.013215 Specialty Business Services,Consulting Service...
Equity(53 [ABMD]) 170.58 20650 0.020093 Medical Devices,Medical Instruments & Supplies
df.groupby("sector_name")["prev_change"].sum().sort_values().plot(kind="bar")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment