I appreciate the help guys, unfortunately I am still running into issues. I decided to use the fundamentals.valuation.sid in my query of fundamentals data, then feed that list of SID's into the update_universe method. Now, when I try to query the price of a stock in that list I have created, I get hit with this error:
TypeError: 'zipline.assets._assets.Equity' object has no attribute '__getitem__'
USER ALGORITHM:72, in handle_data
log.info(stock[data].price)
Here is a sample of my code. Thanks for all the help in advance!
import pandas as pd
import numpy as np
def initialize(context):
# Dictionary of stocks
context.stocks = []
context.fun_stocks = []
# Count of days before rebalancing
context.days = 0
set_benchmark(sid(8554))
# Sector mappings
context.sector_mappings = {
101.0: "Basic Materials",
102.0: "Consumer Cyclical",
103.0: "Financial Services",
104.0: "Real Estate",
205.0: "Consumer Defensive",
206.0: "Healthcare",
207.0: "Utilites",
308.0: "Communication Services",
309.0: "Energy",
310.0: "Industrials",
311.0: "Technology"
}
context.sectors = context.sector_mappings.keys()
def before_trading_start(context, data):
num_stocks = 100
fundamental_df = get_fundamentals(
query(
fundamentals.valuation.sid, #get stock ID
fundamentals.company_reference.primary_symbol,
fundamentals.valuation_ratios.pe_ratio, # Price to Earnings ratio
fundamentals.asset_classification.morningstar_sector_code
)
.filter(fundamentals.valuation_ratios.pe_ratio != None)
.filter(fundamentals.asset_classification.morningstar_sector_code != None)
.filter(fundamentals.asset_classification.morningstar_sector_code != 309.0) # filter out utilities
.filter(fundamentals.asset_classification.morningstar_sector_code != 103.0) # filter out finance
.filter(fundamentals.asset_classification.morningstar_sector_code != 206.0) # filter out healthcare
.filter(fundamentals.valuation.market_cap >= 50000000)
.filter(fundamentals.valuation.shares_outstanding != None)
.order_by(fundamentals.valuation.market_cap.desc())
.limit(num_stocks)
)
sectors = [101.0, 102.0, 104.0, 205.0, 207.0, 308.0, 310.0, 311.0]
context.fun_stocks = [stock for stock in fundamental_df if fundamental_df[stock]['morningstar_sector_code'] in sectors]
for stock in context.fun_stocks:
stock = fundamental_df[stock]['sid']
context.stocks.append(stock) #iterate through list of SIDs and append them to context.stocks
update_universe(context.stocks)
def handle_data(context, data):
for stock in context.stocks: #loop through context.stocks and log their price
log.info(stock[data].price)