Python Pandas 中的慢速随机实现
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30261541/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Slow Stochastic Implementation in Python Pandas
提问by Vivek Jha
I am new to pandas and I need a function for calculating slow stochastic. I think it should be possible without much difficulty but I am not familiar with advanced APIs in pandas.
我是Pandas的新手,我需要一个计算慢随机的函数。我认为应该没有太多困难,但我不熟悉 Pandas 中的高级 API。
My data frame contains, 'open', 'high', 'low' and 'close' prices and it is indexed on dates. This much information should be enough to calculate slow stochastic.
我的数据框包含“开盘价”、“最高价”、“最低价”和“收盘价”,并按日期编制索引。这么多信息应该足以计算慢随机。
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
回答by Ami Tavory
You can do this with the rolling_*family of functions.
您可以使用rolling_*函数族来做到这一点。
E.g., 100[(C - L14)/(H14 - L14)]can be found by:
例如,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)
and the rolling mean can be found by:
并且滚动平均值可以通过以下方式找到:
pd.rolling_mean(k, 3)
Moreover, if you're into this stuff, you can check out pandas & econometrics.
此外,如果你喜欢这个东西,你可以查看pandas & econometrics。
回答by Vivek Jha
I think what I have done is correct, can someone please verify:
我认为我所做的是正确的,有人可以验证:
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
回答by Mott The Tuple
You can use the following simple function to handle both slow and fast stochastics.
您可以使用以下简单函数来处理慢速和快速随机数。
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

