Back to Community
Question on Stochastics Parameters

I cannot make sense of the parameters below... what must I adjust to get a %K that spans a 14 day period and a %D that's a 3 period moving average of my %K ?

slowk, slowd = talib.STOCH(high[stock],  
                                   low[stock],  
                                   close[stock],  
                                   fastk_period=5,  
                                   slowk_period=3,  
                                   slowk_matype=0,  
                                   slowd_period=3,  
                                   slowd_matype=0)

Quantopian provides this sample code demonstrating usage of the TAlib function:

# 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

# Setup our variables  
def initialize(context):  
    context.stocks = symbols('SPY', 'AAPL', 'GLD', 'AMZN')  
    # 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):  
    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=5,  
                                   slowk_period=3,  
                                   slowk_matype=0,  
                                   slowd_period=3,  
                                   slowd_matype=0)

        # get the most recent value  
        slowk = slowk[-1]  
        slowd = slowd[-1]  
        # If either the slowk or slowd are less than 10, the stock is  
        # 'oversold,' a long position is opened if there are no shares  
        # in the portfolio.  
        if slowk < 10 or slowd < 10 and current_position <= 0:  
            order_target_percent(stock, context.long_pct_per_stock)  
        # If either the slowk or slowd are larger than 90, the stock is  
        # 'overbought' and the position is closed.  
        elif slowk > 90 or slowd > 90 and current_position >= 0:  
            order_target(stock, 0)  
1 response

I had the same problem.

Many of the parameters are optional. The default settings are probably what you are looking for (14 day %K 3 day smoothing %D)

slowk, slowd = talib.STOCH(hist['high'],  
                               hist['low'],  
                               hist['close'])  
                               #fastk_period=14,  
                               #slowk_period=3,  
                               #slowk_matype=0,  
                               #slowd_period=3,  
                               #slowd_matype=0)

Remove them and test...