Hi,
I'm pretty new to the programming world and was wondering if someone out there could please give me a helping hand. I'm trying to create an algorithm that relies on fundamental data. The issue that I am facing is that I am not sure whether the universe of stocks output by the get_fundamentals function have the data that I am looking for (or whether these are NA). You can see below that after the function I have used " context.fundamentals=context.fundamentals.dropna()" to try and get rid of any N/A's in the data set. Towards the end of the algo I use " print stock, cash, fundamentals.valuation_ratios.pe_ratio" to see what is happening, a sample of the produced log can be seen directly below. Every other stock is different but still indicates $1,000,000 cash remaining (which tells me that this stock has not been bought, despite meeting my criteria?) and UserInstrumentedAttribute (which I assume is the print off of fundamentals.valuation_ratios.pe_ratio). Assuming that the stocks that have been sorted into my universe don't actually have the data I require how do I ensure that I only get stocks in my universe that have these fundamental values?
Thanks in advance!
Log
2015-07-07PRINTEquity(17920 [RNWK])
2015-07-07PRINT
2015-07-07PRINT1000000.0
2015-07-07PRINT
2015-07-07PRINTUserInstrumentedAttribute
Algo
import pandas as pd
import numpy as np
def initialize(context):
set_max_order_count(50)
def before_trading_start(context):
context.fundamentals=get_fundamentals(
query(
fundamentals.balance_sheet.ordinary_shares_number,
fundamentals.valuation_ratios.book_value_per_share,
fundamentals.earnings_ratios.equity_per_share_growth,
fundamentals.valuation_ratios.pe_ratio,
fundamentals.valuation_ratios.pb_ratio,
fundamentals.balance_sheet.total_assets,
fundamentals.balance_sheet.total_liabilities,
fundamentals.earnings_report.normalized_basic_eps
# put your query in here by typing "fundamentals."
)
.filter(
fundamentals.valuation_ratios.pe_ratio<18
# put your filters here
)
.filter(
fundamentals.valuation_ratios.pb_ratio<2
)
.order_by(
fundamentals.valuation_ratios.pe_ratio.asc()
# sort your query
)
.limit(200)
)
context.fundamentals=context.fundamentals.dropna()
update_universe(context.fundamentals.columns.values)
def handle_data(context, data):
cash = context.portfolio.cash
#current_positions = context.portfolio.positions
for stock in data:
current_position = context.portfolio.positions[stock].amount
stock_price = data[stock].price
#how much of the remaining cash you're willing to invest
plausible_investment = cash/50
# share_amount = int(plausible_investment/stock_price)
try:
if stock_price<plausible_investment:
if current_position==0:
#if context.fundamentals[stock]['normalized_basic_eps']>0:
if context.fundamentals[stock]['pe_ratio']<14:
order_target_percent(stock,5, style=MarketOrder())
print stock, cash, fundamentals.valuation_ratios.pe_ratio
except Exception as e:
print(str(e))