Hello and welcome to part 10 of the Python for Finance with Quantopian tutorials. In this tutorial, we're going to work on a simplified alpha checking notebook.
One warning I will make here is that you should not be using this notebook to look through all of the possible alpha factors you can think of. Instead, you want to use it after having come up with a theory about what would be a good factor, and then test it in here. It's very easy to snoop for a successful alpha that works in the past, but wont perform in the future / out of sample.
To start, we'll keep the sentiment analysis since it's a fairly unique factor, but we need some more. When I personally try to reference my own businesses, I have found that anything that has a large margin, and is also successful in general, is usually an easier business, and one that can take a few hits and downtimes, compared to a low-margin business. This doesn't mean low-margin businesses can't do well, it's just that a higher margin, so long as sales still occur, is easier. Thus, we'll go with operating margin as a second factor, and then revenue growth as a third. Margin is useless if we have no sales, and the value of our company wont go up unless we're growing. We could poke a lot of holes in these assumptions, but we'll go with this for now.
Heading back to the notebooks section on Quantopian, let's make a new one, starting with:
from quantopian.pipeline import Pipeline from quantopian.research import run_pipeline from quantopian.pipeline.filters.morningstar import Q1500US from quantopian.pipeline.data.sentdex import sentiment from quantopian.pipeline.data.morningstar import operation_ratios def make_pipeline(): testing_factor = operation_ratios.operation_margin.latest universe = (Q1500US() & testing_factor.notnull()) testing_factor = testing_factor.rank(mask=universe, method='average') pipe = Pipeline(columns={'testing_factor':testing_factor}, screen=universe) return pipe result = run_pipeline(make_pipeline(), start_date='2015-01-01', end_date='2016-01-01') result.head()
This will get us the operation margin data, now we're needing to grab the pricing data for all stocks in this same time period:
assets = result.index.levels[1].unique() pricing = get_pricing(assets, start_date='2014-12-01', end_date='2016-02-01', fields='open_price') len(assets)
We also want to see the length of assets, since there may be times when the factor is extremely limiting. This will be especially important when we go to compine alpha factors.
Finally, we can analyze the alpha:
import alphalens alphalens.tears.create_factor_tear_sheet(factor = result['testing_factor'], prices = pricing, quantiles = 2, periods = (3, 10, 30))
In the end, we get the following:
Ann. alpha 0.050 0.050 0.048 beta -0.222 -0.222 -0.227 IC Mean 0.027 0.036 0.069
Alright, now we can test revenue growth, by modifying testing_factor
to be testing_factor = operation_ratios.revenue_growth.latest
Ann. alpha 0.074 0.065 0.057 beta -0.046 -0.082 -0.062 IC Mean 0.032 0.042 0.054
Finally, just for the record since we changed the alphalens periods to 3, 10 and 30, let's run sentiment again:
Ann. alpha 0.027 0.026 0.020 beta -0.021 -0.033 -0.071 IC Mean 0.011 0.020 0.030
You can download the notebook for this tutorial: .
In the next tutorial, we're going to combine these three factors into one single factor and see how it compares to the individual alpha factors.