Back to Community
Help! Weird timezone error in notebook using alphalens

Hello all,

First error (seen below) has been resolved. See the update via my replies below.

I am relatively new to coding, and even newer to Quantopian. I am trying to conduct some research on alpha factors using alphalens, but am unable to even get the notebook working.

I've also tried re-writing the code to create the pipeline in initialization but the same error occurred. Any help would be greatly appreciated!

I know you all can view in the attached notebook, but I'll copy+paste below for good measure. Here's the code:

# Normal imports  
from quantopian.pipeline import Pipeline  
from quantopian.research import run_pipeline  
from quantopian.pipeline.filters.morningstar import Q1500US  
from quantopian.pipeline import CustomFactor  
from import USEquityPricing  
import pandas as pd  
import numpy as np

# Testing factor imports  
from import operation_ratios  
from import valuation_ratios  
from import asset_classification  
from import valuation  
from import earnings_ratios


# Single factor  
def custom_factor(CustomFactor):  
    inputs = [morningstar.valuation_ratios.ps_ratio]  
    window_length = 1  
    def compute(self, today, assets, out, ps):  
        table = pd.DataFrame(index=assets)  
        table ["ps"] = ps[-1]  
        out[:] = table.fillna(table.max()).mean(axis=1)  
# Factor requiring industry comparison  
#def custom_factor(CustomFactor):  
# Factor requiring multiple factors  
#def custom_factor(CustomFactor):


def make_pipeline():  
    testing_factor = custom_factor()  
    universe = (Q1500US() & testing_factor.notnull())  
    testing_factor = testing_factor.rank(ascending=true, mask=universe, method='average')  
    pipe = Pipeline(columns={'testing_factor':testing_factor}, screen = universe)  
    return pipe

result = run_pipeline(make_pipeline(), start_date='2014-01-01', end_date='2018-01-01')  

And here's the error:

TypeErrorTraceback (most recent call last)  
<ipython-input-18-fce5c027f49a> in <module>()  
     43     return pipe  
---> 45 result = run_pipeline(make_pipeline(), start_date='2014-01-01', end_date='2018-01-01')  
     46 result.head()

<ipython-input-18-fce5c027f49a> in make_pipeline()  
     37 def make_pipeline():  
---> 39     testing_factor = custom_factor()  
     40     universe = (Q1500US() & testing_factor.notnull())  
     41     testing_factor = testing_factor.rank(ascending=true, mask=universe, method='average')

TypeError: custom_factor() takes exactly 1 argument (0 given)  
Loading notebook preview...
Notebook previews are currently unavailable.
4 responses

class instead of def

Loading notebook preview...
Notebook previews are currently unavailable.

.... I have no idea how I overlooked that. I probably have over 50 custom factors used in various algorithms and somehow glossed right over that mistake.

Thanks Blue!


Moving forward with the notebook creation and I'm one step away from being done, at least I think, and being able to conduct alpha research.

The last hiccup, however, is an error I've not seen before and haven't seen much commentary on:

AttributeErrorTraceback (most recent call last)  
<ipython-input-4-25ec6be0d7e3> in <module>()  
      4                                                                    prices = pricing,  
      5                                                                    quantiles = 5,  
----> 6                                                                    periods = (1,5,10))  
      8 alphalens.tears.create_full_tear_sheet(factor_data, by_group=True)

/usr/local/lib/python2.7/dist-packages/alphalens/utils.pyc in get_clean_factor_and_forward_returns(factor, prices, groupby, by_group, quantiles, bins, periods, filter_zscore, groupby_labels)
    296     """  
--> 298     if factor.index.levels[0].tz !=  
    299         raise NonMatchingTimezoneError("The timezone of 'factor' is not the "  
    300                                        "same as the timezone of 'prices'. See "

AttributeError: 'builtin_function_or_method' object has no attribute 'levels'  

The error is occurring between these two steps, apparently:

assets = result.index.levels[1].unique()

#start and end must be far enough back to collect data properly!!!  
pricing = get_pricing(assets, start_date='2014-12-01', end_date='2018-02-01', fields='close_price')  
import alphalens  
factor_data = alphalens.utils.get_clean_factor_and_forward_returns(factor = 'testing_factor',  
                                                                   prices = pricing,  
                                                                   quantiles = 5,  
                                                                   periods = (1,5,10))

alphalens.tears.create_full_tear_sheet(factor_data, by_group=True)  
Loading notebook preview...
Notebook previews are currently unavailable.

The error is that you are passing a string instead of the result of pipeline to alphalens.utils.get_clean_factor_and_forward_returns.

factor_data = alphalens.utils.get_clean_factor_and_forward_returns(factor = 'testing_factor', <-- ERROR  


factor_data = alphalens.utils.get_clean_factor_and_forward_returns(factor = result['testing_factor'],