public static IEnumerable<StochResult> GetStoch<TQuote>(this IEnumerable<TQuote> quotes, int lookbackPeriods = 14, int signalPeriods = 3, int smoothPeriods = 3) where TQuote : IQuote
描述
KDJ 指标,也称为随机振荡指标(Stochastic Oscillator),由 George Lane 创立。它是一个动量指标,通过比较收盘价与一定时间周期内的价格范围,来判断市场的超买超卖状态和趋势转折点。
参数
参数名
类型
描述
lookbackPeriods
Int
回顾周期
signalPeriods
Int
信号周期(M1)
smoothPeriods
Int
平滑周期(M2)
返回值
返回值
类型
描述
Date
DateTime
日期
Oscillator
decimal
K值
PercentJ
decimal
J值
Signal
decimal
D值
///指标数据
QuoteHistoryDay(10, (dic) =>
{
if (dic.Count > 0)
{
foreach (var item in dic.Keys)
{
///获取指标结果
var resp = dic[item].GetStoch(14,3,3);
Console.WriteLine(resp.ToJson());
}
}
});
def CalculateKDJ(self, data):
"""计算KDJ指标"""
if len(data) < self.kdj_period:
return None, None, None
# 获取最近N天的数据
recent_data = data[-self.kdj_period:]
# 计算最高价和最低价
highest_high = max([bar.High for bar in recent_data])
lowest_low = min([bar.Low for bar in recent_data])
current_close = recent_data[-1].Close
# 避免除零错误
if highest_high == lowest_low:
return 50, 50, 50
# 计算RSV
rsv = ((current_close - lowest_low) / (highest_high - lowest_low)) * 100
# 计算K值(快速随机值)
if len(self.k_values) == 0:
k = rsv
else:
k = (2/3) * self.k_values[-1] + (1/3) * rsv
# 计算D值(慢速随机值)
if len(self.d_values) == 0:
d = k
else:
d = (2/3) * self.d_values[-1] + (1/3) * k
# 计算J值
j = 3 * k - 2 * d
# 限制值在0-100范围内
k = max(0, min(100, k))
d = max(0, min(100, d))
j = max(0, min(100, j))
return k, d, j