Back to Community
Getting NaN from TALib.STOCH

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.

Clone Algorithm
2
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# This algorithm uses talib's STOCH function to determine entry and exit points.

# When the stochastic oscillator dips below 10, the stock is determined to be oversold
# and a long position is opened. The position is exited when the indicator rises above 90
# because the stock is thought to be overbought.

# Because this algorithm uses the history function, it will only run in minute mode. 
# We will constrain the trading to once per day at market open in this example.

import talib
import numpy as np
import pandas as pd
import datetime
from sqlalchemy import or_


# Setup our variables
def initialize(context):
    context.stocks = [sid(33208),sid(33265),sid(32270),sid(32272)]

    # Set the percent of the account to be invested per stock
    context.long_pct_per_stock = 1.0 / len(context.stocks)
    
    # Create a variable to track the date change
    context.date = None
 
def handle_data(context, data):
    record(lev=context.account.leverage)
    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
    
    # Load historical data for the stocks
    high = history(30, '1d', 'high')
    low = history(30, '1d', 'low')
    close = history(30, '1d', 'close_price')
    
    # Iterate over our list of stocks
    for stock in context.stocks:
        current_position = context.portfolio.positions[stock].amount
        slowk, slowd = talib.STOCH(high[stock],
                                   low[stock],
                                   close[stock],
                                   fastk_period=37,
                                   slowk_period=3,
                                   slowk_matype=0,
                                   slowd_period=3,
                                   slowd_matype=0)

        #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 > 10 and slowd2<=10 and slowk > slowd) and current_position <= 0: 
            order_target_percent(stock, context.long_pct_per_stock)
        
        #sell whenever you have crossover over 80 OR position is down 10%
#        elif current_position > 0 and (((slowk > 80 or slowd > 80) and slowk < slowd) or (context.portfolio.positions[stock].last_sale_price/context.portfolio.positions[stock].cost_basis) < 0.90):    
        elif current_position > 0 and (slowd < 90 and slowk < slowd and slowd2>=90):
            order_target(stock, 0)
    
    if context.account.leverage == 0:
        if not get_open_orders(sid(23870)):
            order_target_percent(sid(23870), 2.0)
            
            
            
There was a runtime error.
2 responses

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  
'''