python代码实现RSI、MACD、KDJ
时间: 2023-09-14 21:08:32 浏览: 228
下面是Python实现RSI、MACD、KDJ指标的代码:
## RSI指标
def RSI(close, n = 14):
dif = np.diff(close)
up, down = dif.copy(), dif.copy()
up[up < 0] = 0
down[down > 0] = 0
gain = pd.DataFrame(up).rolling(n).mean()
loss = pd.DataFrame(down).rolling(n).mean().abs()
RS = gain / loss
RSI = 100 - 100 / (1 + RS)
return RSI
## MACD指标
def MACD(close, fast = 12, slow = 26, signal = 9):
EMAfast = close.ewm(span = fast, min_periods = fast - 1).mean()
EMAslow = close.ewm(span = slow, min_periods = slow - 1).mean()
DIF = EMAfast - EMAslow
DEA = DIF.ewm(span = signal, min_periods = signal - 1).mean()
MACD = (DIF - DEA) * 2
return MACD, DIF, DEA
## KDJ指标
def KDJ(high, low, close, n = 9, m1 = 3, m2 = 3):
rsv = (close - pd.DataFrame(low).rolling(n).min()) / (pd.DataFrame(high).rolling(n).max() - pd.DataFrame(low).rolling(n).min()) * 100
K = rsv.ewm(com = 2).mean()
D = K.ewm(com = 2).mean()
J = m1 * K - m2 * D
return K, D, J
以上代码实现了RSI、MACD、KDJ三种常见的技术指标,可以根据需要进行调用。
阅读全文