Getting NaN from TALib Stochastic

I'm getting NaN from some of my stocks, but not all of them? What gives? I'm trying to compute the top 50 market capitalizations of the day and buy when stochastic crosses a certain level.

"""
Buys top market capitalization stocks that are listed as "oversold" and are exiting their oversold region.  Sell when they enter or exit their oversold region.
"""
from quantopian.algorithm import attach_pipeline, pipeline_output
from quantopian.pipeline import Pipeline
from quantopian.pipeline import CustomFactor
from quantopian.pipeline.data import morningstar
from quantopian.pipeline.data.builtin import USEquityPricing
from quantopian.pipeline.factors import AverageDollarVolume
import random
import talib
import numpy as np
import pandas as pd
import datetime
from sqlalchemy import or_

#How many stocks?
numstock = 10
stochl = 37
stochd = 3
stochk = 3
bars=(stochl+stochd+stochk)*2

osold = 10
obought = 90

class MarketCap(CustomFactor):
# Pre-declare inputs and window_length
inputs = [USEquityPricing.close, morningstar.valuation.shares_outstanding]
window_length = 1
# Compute market cap value
def compute(self, today, assets, out, close, shares):
out[:] = close[-1] * shares[-1]

def initialize(context):
# Create a variable to track the date change
context.date = None
pipe=Pipeline()
# Construct the custom factor
mkt_cap = MarketCap()

# Use screen to narrow the universe
pipe.set_screen(mkt_cap.top(50))
# Attach to the pipeline
attach_pipeline(pipe, 'my_pipeline')

context.stocks = pipeline_output('my_pipeline').index.tolist()
update_universe(context.stocks)
todays_date = get_datetime().date()
# Do nothing unless the date has changed
if todays_date == context.date:
return
# Set the new date
context.date = todays_date
print "Cash: " + str(context.portfolio.cash)
#Iterate and look for oversold
stockcnt = 0
for stock in context.stocks:
stockcnt = stockcnt + 1
# Load historical data for the stocks
#data.history(sid(3149), 'open', 10, '1d')
high = data.history(stock, 'high', bars, '1d')
low = data.history(stock, 'low', bars, '1d')
close = data.history(stock, 'close', bars, '1d')
#Get out current position on the stock in question
current_position = context.portfolio.positions[stock].amount
#Look for oversold region
slowk, slowd = talib.STOCH(high,low,close,
fastk_period=stochl,
slowk_period=stochk,
slowk_matype=0,
slowd_period=stochd,
slowd_matype=0)

print "Stock#: " + str(stockcnt) + " StochK: " + str(slowk[-1])
#Get from two periods ago
slowk2 = slowk[-2]
slowd2 = slowd[-2]
#Get the most recent value
slowk = slowk[-1]
slowd = slowd[-1]
# Open the order on oversold crossover
if (slowd > osold and slowd2<=osold and slowk > slowd) and current_position <= 0:
order_target_percent(stock, 1.0/numstock)
elif current_position > 0 and (slowd < obought and slowk < slowd and slowd2>=obought):
order_target(stock, 0)


I'm new, but I'd love some help!

This is what I'm seeing in the logs:

2005-02-16 09:31 PRINT Cash: 399276.927299 2005-02-16 09:31 PRINT
Stock#: 1 StochK: 89.8345153664 2005-02-16 09:31 PRINT Stock#: 2
StochK: nan 2005-02-16 09:31 PRINT Stock#: 3 StochK: nan 2005-02-16
09:31 PRINT Stock#: 4 StochK: 40.203562341 2005-02-16 09:31 PRINT
Stock#: 5 StochK: 24.255952381 2005-02-16 09:31 PRINT Stock#: 6
StochK: 67.4501273491 2005-02-16 09:31 PRINT Stock#: 7 StochK: nan
2005-02-16 09:31 PRINT Stock#: 8 StochK: 36.9827774144 2005-02-16
09:31 PRINT Stock#: 9 StochK: 93.4289486681 2005-02-16 09:31 PRINT
Stock#: 10 StochK: nan 2005-02-16 09:31 PRINT Stock#: 11 StochK:
90.7552083333 2005-02-16 09:31 PRINT Stock#: 12 StochK: 69.3137324865 2005-02-16 09:31 PRINT Stock#: 13 StochK: 23.6225731993 2005-02-16
09:31 PRINT Stock#: 14 StochK: nan 2005-02-16 09:31 PRINT Stock#: 15
StochK: nan 2005-02-16 09:31 PRINT Stock#: 16 StochK: 35.2607709751
2005-02-16 09:31 PRINT Stock#: 17 StochK: nan 2005-02-16 09:31 PRINT
Stock#: 18 StochK: nan 2005-02-16 09:31 PRINT Stock#: 19 StochK: nan
2005-02-16 09:31 PRINT Stock#: 20 StochK: nan 2005-02-16 09:31 PRINT
Stock#: 21 StochK: 92.9268292683

1 response

For best response, run a full backtest and attach.