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.
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.
Instead of 30, since fastk is 37, how about this (is also the up-to-date method)
high = data.history(context.stocks, 'high', 44, '1d')
low = data.history(context.stocks, 'low', 44, '1d')
close = data.history(context.stocks, 'price', 44, '1d')
Could also then check, skip each of them like this example, although nan should no longer occur
if np.isnan(slowk ):
log.info('skip nan slowk {}'.format(stock.symbol))
continue
Benjamin,
You may also try this simplified bull market setup:
# TALib.STOCH portfolio bull market setup
import talib
# -----------------------------------------
stocks = symbols('UWM', 'UYG','SSO', 'QLD')
fk, sk, sk_ma, sd, sd_ma = 36, 3, 0, 3, 0
LB, UB, lev = 20, 80, 1.0
# -----------------------------------------
bars = fk + sk + sd
wt = lev/len(stocks)
def initialize(context):
schedule_function(trade, date_rules.every_day(), time_rules.market_open(minutes = 65))
def trade(context, data):
if get_open_orders(): return
for stock in stocks:
if not data.can_trade(stock): continue
H = data.history(stock, 'high', bars, '1d')
L = data.history(stock, 'low', bars, '1d')
C = data.history(stock, 'close', bars, '1d')
slowk, slowd = talib.STOCH(H, L, C, fk, sk, sk_ma, sd, sd_ma)
if (slowd[-1] > LB and slowd[-2] <= LB and slowk[-1] > slowd[-1]):
order_target_percent(stock, wt)
elif (slowd[-1] < UB and slowd[-2] >= UB and slowk[-1] < slowd[-1]):
order_target(stock, 0)
record(lev = context.account.leverage)
'''
START
06/01/2009
END
07/28/2017
'''