I am having problems getting the following fundamental algo to run. Does someone mind helping me?
import pandas as pd
import numpy as np
def initialize(context):
context.stocks = []
def before_trading_start(context, data):
fundamental_df = get_fundamentals(
query(
fundamentals.asset_classification.morningstar_industry_group_code,
fundamentals.company_reference.country_id,
fundamentals.income_statement.operating_revenue,
fundamentals.income_statement.other_operating_revenue,
fundamentals.income_statement.non_operating_income,
fundamentals.income_statement.general_and_administrative_expense,
fundamentals.income_statement.cost_of_revenue,
fundamentals.balance_sheet.real_estate,
fundamentals.balance_sheet.stockholders_equity,
fundamentals.balance_sheet.preferred_securities_outside_stock_equity,
fundamentals.balance_sheet.preferred_stock_equity,
fundamentals.balance_sheet.ordinary_shares_number
)
.filter(fundamentals.asset_classification.morningstar_industry_group_code == 10428)
.filter(fundamentals.company_reference.country_id == "USA")
.filter(fundamentals.balance_sheet.ordinary_shares_number > 0)
)
context.stocks = []
for stock in fundamental_df:
OR = fundamental_df[stock]['operating_revenue'] or 0
CR = fundamental_df[stock]['cost_of_revenue'] or 0
SE = fundamental_df[stock]['stockholders_equity'] or 0
PSOSE = fundamental_df[stock]['preferred_securities_outside_stock_equity'] or 0
PSE = fundamental_df[stock]['preferred_stock_equity'] or 0
OSN = fundamental_df[stock]['ordinary_shares_number'] or 0
cashFlow = (OR - CR)*4
NAV = cashFlow + SE - PSOSE - PSE
intrinsicValue = NAV/float(OSN)
last = data[stock][symbol].price
if last <= .8*intrinsicValue:
context.stock.ammend(stock)
context.fundamental_df = fundamental_df[context.stocks]
update_universe(context.fundamental_df.columns.values)
def handle_data(context, data):
if len(context.stocks) == 0:
weight = 0
else:
weight = 1.0/len(context.stocks)
for stock in context.portfolio.positions:
order_target_percent(stock, 0)
for stock in context.stock:
if weight != 0:
order_target_percent(stock, weight)