Python Pandas中慢速随机实现

时间:2015-05-15 14:05:41

标签: numpy pandas matplotlib

我是熊猫新手,我需要一个计算慢速随机的函数。我认为应该没有太大困难,但我不熟悉pandas中的高级API。

我的数据框包含“开盘价”,“高价”,“低价”和“收盘价”,并按日期编制索引。这么多的信息应该足以计算慢随机。

Following is the formula for calculating Slow Stochastic:
%K = 100[(C - L14)/(H14 - L14)] 

C = the most recent closing price 
L14 = the low of the 14 previous trading sessions 
H14 = the highest price traded during the same 14-day period.

%D = 3-period moving average of %K 

3 个答案:

答案 0 :(得分:5)

您可以使用rolling_*系列函数执行此操作。

,例如,100[(C - L14)/(H14 - L14)]可以通过以下方式找到:

import pandas as pd

l, h = pd.rolling_min(c, 4), pd.rolling_max(c, 4)
k = 100 * (c - l) / (h - l) 

可以通过以下方式找到滚动平均值:

pd.rolling_mean(k, 3)

此外,如果您使用这些内容,可以查看pandas & econometrics

答案 1 :(得分:1)

我认为我所做的是正确的,有人可以验证:

def simple_moving_average(prices, period=26):
    """
    :param df: pandas dataframe object
    :param period: periods for calculating SMA
    :return: a pandas series
    """
    weights = np.repeat(1.0, period)/period
    sma = np.convolve(prices, weights, 'valid')
    return sma


def fast_stochastic(lowp, highp, closep, period=14, smoothing=3):
    """ calculate slow stochastic
    Fast stochastic calculation
    %K = (Current Close - Lowest Low)/(Highest High - Lowest Low) * 100
    %D = 3-day SMA of %K
    """
    low_min = pd.rolling_min(lowp, period)
    high_max = pd.rolling_max(highp, period)
    k_fast = 100 * (closep - low_min)/(high_max - low_min)
    k_fast = k_fast.dropna()
    d_fast = simple_moving_average(k_fast, smoothing)
    return k_fast, d_fast


def slow_stochastic(lowp, highp, closep, period=14, smoothing=3):
    """ calculate slow stochastic
    Slow stochastic calculation
    %K = %D of fast stochastic
    %D = 3-day SMA of %K
    """
    k_fast, d_fast = fast_stochastic(lowp, highp, closep, period=period, smoothing=smoothing)

    # D in fast stochastic is K in slow stochastic
    k_slow = d_fast
    d_slow = simple_moving_average(k_slow, smoothing)
    return k_slow, d_slow

答案 2 :(得分:0)

您可以使用以下简单功能来处理慢速和快速随机性。

def stochastics( dataframe, low, high, close, k, d ):
    """
    Fast stochastic calculation
    %K = (Current Close - Lowest Low)/
    (Highest High - Lowest Low) * 100
    %D = 3-day SMA of %K

    Slow stochastic calculation
    %K = %D of fast stochastic
    %D = 3-day SMA of %K

    When %K crosses above %D, buy signal 
    When the %K crosses below %D, sell signal
    """

    df = dataframe.copy()

    # Set minimum low and maximum high of the k stoch
    low_min  = df[low].rolling( window = k ).min()
    high_max = df[high].rolling( window = k ).max()

    # Fast Stochastic
    df['k_fast'] = 100 * (df[close] - low_min)/(high_max - low_min)
    df['d_fast'] = df['k_fast'].rolling(window = d).mean()

    # Slow Stochastic
    df['k_slow'] = df["d_fast"]
    df['d_slow'] = df['k_slow'].rolling(window = d).mean()

    return df


stochs = stochastics( df, 'Low', 'High', 'Close', 14, 3 )
slow_k = stochs['k_slow'].values
fast_k = stochs['k_fats'].values