Back to Community
wrong value type in for loop

in a pipeline algorithm, I am restricting my known universe, then I want to loop over my base_universe. I get the following error from this code. Much of the code was copied from a lecture or tutorial.

TypeError:
zipline.pipeline.term.__getitem__() expected a value of type zipline.assets._assets.Asset for argument 'key', but got int instead.

# Filter for primary share equities. IsPrimaryShare is a built-in filter.  
primary_share = IsPrimaryShare()

# Equities listed as common stock (as opposed to, say, preferred stock).  
# 'ST00000001' indicates common stock.  
common_stock = morningstar.share_class_reference.security_type.latest.eq('ST00000001')

# Non-depositary receipts. Recall that the ~ operator inverts filters,  
# turning Trues into Falses and vice versa  
not_depositary = ~morningstar.share_class_reference.is_depositary_receipt.latest

# Equities not trading over-the-counter.  
not_otc = ~morningstar.share_class_reference.exchange_id.latest.startswith('OTC')

# Not when-issued equities.  
not_wi = ~morningstar.share_class_reference.symbol.latest.endswith('.WI')

# Equities without LP in their name, .matches does a match using a regular  
# expression  
not_lp_name = ~morningstar.company_reference.standard_name.latest.matches('.* L[. ]?P.?$')

# Equities with a null value in the limited_partnership Morningstar  
# fundamental field.  
not_lp_balance_sheet = morningstar.balance_sheet.limited_partnership.latest.isnull()

# Equities whose most recent Morningstar market cap is not null have  
# fundamental data and therefore are not ETFs.  
have_market_cap = morningstar.valuation.market_cap.latest.notnull()

# Filter for stocks that pass all of our previous filters.  
tradeable_stocks = (  
    primary_share  
    & common_stock  
    & not_depositary  
    & not_otc  
    & not_wi  
    & not_lp_name  
    & not_lp_balance_sheet  
    & have_market_cap  
)

days_volume = 20  
# High dollar volume filter.  
base_universe = AverageDollarVolume(window_length=days_volume, mask=tradeable_stocks).top(40)

for symbol in base_universe:  
    print(symbol)  
2 responses

Can anyone help me figure this out? Is this the most appropriate place to come with this sort of issue?

Ben,
That error should point to the line number associated with "for symbol in base_universe"
Within initialize() you are defining a filter called base_universe.
The filtered result does not exist until you call the pipeline in before_trading_start().

I pasted your code above into a file.
See that attached back test.

Clone Algorithm
4
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# Backtest ID: 57e5f286c8cf7c4107b7460b
There was a runtime error.