This exercise notebook refers to this lecture. Please use the lecture for explanations and sample code.
https://www.quantopian.com/lectures#Universe-Selection
Part of the Quantopian Lecture Series:
In [ ]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from quantopian.pipeline.classifiers.morningstar import Sector
from quantopian.pipeline import Pipeline
from quantopian.pipeline.data.builtin import USEquityPricing
from quantopian.pipeline.filters import QTradableStocksUS, AtLeastN
from quantopian.research import run_pipeline
from quantopian.pipeline.data import morningstar
from quantopian.pipeline.factors import CustomFactor, AverageDollarVolume
In [ ]:
def calculate_daily_turnover(unstacked):
return (unstacked
.diff() # Get True/False showing where values changed from previous day.
.iloc[1:] # Drop first row, which is meaningless after diff().
.astype(bool) # diff() coerces from bool -> object :(. Undo that.
.groupby(axis=1, level=0)
.sum())
In [ ]:
# Your code goes here
In [ ]:
# Your code goes here
In [ ]:
SECTOR_CODE_NAMES = {
Sector.BASIC_MATERIALS: 'Basic Materials',
Sector.CONSUMER_CYCLICAL: 'Consumer Cyclical',
Sector.FINANCIAL_SERVICES: 'Financial Services',
Sector.REAL_ESTATE: 'Real Estate',
Sector.CONSUMER_DEFENSIVE: 'Consumer Defensive',
Sector.HEALTHCARE: 'Healthcare',
Sector.UTILITIES: 'Utilities',
Sector.COMMUNICATION_SERVICES: 'Communication Services',
Sector.ENERGY: 'Energy',
Sector.INDUSTRIALS: 'Industrials',
Sector.TECHNOLOGY: 'Technology',
-1 : 'Misc'
}
# Your code goes here
In [ ]:
# Your code goes here
In [ ]:
# Your code goes here
In [ ]:
# Your code goes here
In [ ]:
# Your code goes here
In [ ]:
SECTOR_CODE_NAMES = {
Sector.BASIC_MATERIALS: 'Basic Materials',
Sector.CONSUMER_CYCLICAL: 'Consumer Cyclical',
Sector.FINANCIAL_SERVICES: 'Financial Services',
Sector.REAL_ESTATE: 'Real Estate',
Sector.CONSUMER_DEFENSIVE: 'Consumer Defensive',
Sector.HEALTHCARE: 'Healthcare',
Sector.UTILITIES: 'Utilities',
Sector.COMMUNICATION_SERVICES: 'Communication Services',
Sector.ENERGY: 'Energy',
Sector.INDUSTRIALS: 'Industrials',
Sector.TECHNOLOGY: 'Technology',
-1 : 'Misc'
}
# Your code goes here
In [ ]:
# Your code goes here
In [ ]:
# Your code goes here
In [ ]:
# Your code goes here
Congratulations on completing the Universe Selection exercises!
As you learn more about writing trading models and the Quantopian platform, enter a daily Quantopian Contest. Your strategy will be evaluated for a cash prize every day.
Start by going through the Writing a Contest Algorithm tutorial.
This presentation is for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation for any security; nor does it constitute an offer to provide investment advisory or other services by Quantopian, Inc. ("Quantopian"). Nothing contained herein constitutes investment advice or offers any opinion with respect to the suitability of any security, and any views expressed herein should not be taken as advice to buy, sell, or hold any security or as an endorsement of any security or company. In preparing the information contained herein, Quantopian, Inc. has not taken into account the investment needs, objectives, and financial circumstances of any particular investor. Any views expressed and data illustrated herein were prepared based upon information, believed to be reliable, available to Quantopian, Inc. at the time of publication. Quantopian makes no guarantees as to their accuracy or completeness. All information is subject to change and may quickly become unreliable for various reasons, including changes in market conditions or economic circumstances.