使用ta库计算MACD,KDJ,RSI,ADX指标
时间: 2023-12-26 15:04:52 浏览: 203
好的,我可以帮你实现这些指标的计算。首先需要安装 ta 库,可以使用以下命令进行安装:
```
pip install ta
```
安装完成后,可以按照以下代码示例来计算指标:
```python
import pandas as pd
import ta
# 读取数据,假设数据保存在 data.csv 文件中
df = pd.read_csv('data.csv', index_col=0)
# 计算 MACD 指标
df['macd'], df['macd_signal'], df['macd_hist'] = ta.macd(df['close'])
# 计算 KDJ 指标
df['kdj_k'], df['kdj_d'], df['kdj_j'] = ta.stoch(df['high'], df['low'], df['close'])
# 计算 RSI 指标
df['rsi'] = ta.rsi(df['close'])
# 计算 ADX 指标
df['adx'] = ta.adx(df['high'], df['low'], df['close'])
```
其中 `df` 是保存数据的 Pandas DataFrame,`df['close']` 是收盘价的列,`df['high']` 是最高价的列,`df['low']` 是最低价的列。计算结果会保存在新的列中,例如 MACD 的结果会保存在 `df['macd']`、`df['macd_signal']`、`df['macd_hist']` 中。
相关问题
不调用库,计算MACD,KDJ,ADX,RSI指标,python
计算技术指标需要一定的数学和编程基础,下面是一些常见指标的计算方法和Python代码实现:
1. MACD指标
MACD指标是由两条指数移动平均线(EMA)和一条信号线组成的,计算方法如下:
- 计算12日EMA和26日EMA
$EMA_{12}=前一日EMA_{12}×11/13+今日收盘价×2/13$
$EMA_{26}=前一日EMA_{26}×25/27+今日收盘价×2/27$
- 计算DIF线
$DIF=EMA_{12}-EMA_{26}$
- 计算DEA线
$DEA=前一日DEA×8/10+今日DIF×2/10$
- 计算MACD线
$MACD=(DIF-DEA)×2$
Python代码实现:
```python
def MACD(data, fast=12, slow=26, signal=9):
ema_fast = data['Close'].ewm(span=fast).mean()
ema_slow = data['Close'].ewm(span=slow).mean()
dif = ema_fast - ema_slow
dea = dif.ewm(span=signal).mean()
macd = (dif - dea) * 2
return dif, dea, macd
```
2. KDJ指标
KDJ指标是一种反映股票价格波动趋势的技术指标,计算方法如下:
- 计算当日的RSV值
$RSV=\frac{收盘价-最低价}{最高价-最低价}×100$
- 计算当日的K值、D值和J值
$K=前一日K×2/3+今日RSV×1/3$
$D=前一日D×2/3+今日K×1/3$
$J=3K-2D$
Python代码实现:
```python
def KDJ(data, n=9):
high = data['High'].rolling(n).max()
low = data['Low'].rolling(n).min()
rsv = (data['Close'] - low) / (high - low) * 100
k = rsv.ewm(com=2).mean()
d = k.ewm(com=2).mean()
j = 3 * k - 2 * d
return k, d, j
```
3. ADX指标
ADX指标是一个复合指标,由+DI和-DI两条线和ADX线组成,计算方法如下:
- 计算+DI和-DI
$TR=max(H-L, |H-前一日收盘价|, |L-前一日收盘价|)$
$+DM=H-前一日高点$
$-DM=前一日低点-L$
$+DI=+DM÷TR×100$
$-DI=-DM÷TR×100$
- 计算DX
$DX=|+DI--DI|÷(+DI+-DI)×100$
- 计算ADX
$ADX=前一日ADX×13/14+今日DX×1/14$
Python代码实现:
```python
def ADX(data, n=14):
h, l, c = data['High'], data['Low'], data['Close']
tr = pd.DataFrame({'tr': np.nan}, index=data.index)
tr['tr'] = np.max([h-l, np.abs(h-c.shift()), np.abs(l-c.shift())], axis=0)
tr['+dm'] = (h - h.shift()).where((h - h.shift()) > (l.shift() - l), 0)
tr['-dm'] = (l.shift() - l).where((l.shift() - l) > (h - h.shift()), 0)
tr['+di'] = tr['+dm'].rolling(n).sum() / tr['tr'].rolling(n).sum() * 100
tr['-di'] = tr['-dm'].rolling(n).sum() / tr['tr'].rolling(n).sum() * 100
tr['dx'] = np.abs(tr['+di'] - tr['-di']) / (tr['+di'] + tr['-di']) * 100
tr['adx'] = tr['dx'].ewm(alpha=1/n, min_periods=n).mean() * 100
return tr['+di'], tr['-di'], tr['adx']
```
4. RSI指标
RSI指标是通过比较一段时间内股票的平均涨幅和平均跌幅,来判断股票是否处于超买或超卖状态,计算方法如下:
- 计算当日涨跌幅
$delta=收盘价-前一日收盘价$
- 计算当日上涨和下跌的平均值
$gain=\max(delta,0)$
$loss=\max(-delta,0)$
$AvgGain=\frac{\sum_{i=1}^n gain_i}{n}$
$AvgLoss=\frac{\sum_{i=1}^n loss_i}{n}$
- 计算RSI
$RS=\frac{AvgGain}{AvgLoss}$
$RSI=100-\frac{100}{1+RS}$
Python代码实现:
```python
def RSI(data, n=14):
delta = data['Close'].diff()
gain = delta.where(delta > 0, 0)
loss = -delta.where(delta < 0, 0)
avg_gain = gain.rolling(n).mean()
avg_loss = loss.rolling(n).mean()
rs = avg_gain / avg_loss
rsi = 100 - 100 / (1 + rs)
return rsi
```
以上是常见技术指标的计算方法和Python实现,需要注意不同的股票和交易品种可能需要不同的参数,需要根据实际情况进行调整。
用java写一个 股票dmi指标中的adx ,adxr计算
好的,以下是用 Java 编写股票 DMI 指标中的 ADX 和 ADXR 计算的代码:
```java
public class DMIIndicator {
// 计算ADX
public static double[] adx(double[] high, double[] low, double[] close, int period) {
double[] tr = new double[high.length];
double[] atr = new double[high.length];
double[] pdi = new double[high.length];
double[] mdi = new double[high.length];
double[] dx = new double[high.length];
double[] adx = new double[high.length];
// 计算TR和ATR
for (int i = 1; i < high.length; i++) {
double h_l = high[i] - low[i];
double h_pc = Math.abs(high[i] - close[i - 1]);
double l_pc = Math.abs(low[i] - close[i - 1]);
tr[i] = Math.max(h_l, Math.max(h_pc, l_pc));
atr[i] = ((period - 1) * atr[i - 1] + tr[i]) / period;
}
// 计算PDI和MDI
for (int i = period + 1; i < high.length; i++) {
double pdm = high[i] - high[i - 1];
double mdm = low[i - 1] - low[i];
pdi[i] = 100 * ((period - 1) * pdi[i - 1] + pdm) / (period * atr[i]);
mdi[i] = 100 * ((period - 1) * mdi[i - 1] + mdm) / (period * atr[i]);
}
// 计算DX
for (int i = 2 * period; i < high.length; i++) {
double di_diff = Math.abs(pdi[i] - mdi[i]);
double di_sum = pdi[i] + mdi[i];
dx[i] = 100 * (period * dx[i - 1] + di_diff / di_sum) / (period + 1);
}
// 计算ADX
for (int i = 2 * period; i < high.length; i++) {
adx[i] = ((period - 1) * adx[i - 1] + dx[i]) / period;
}
return adx;
}
// 计算ADXR
public static double[] adxr(double[] high, double[] low, double[] close, int period) {
double[] adx = adx(high, low, close, period);
double[] adxr = new double[high.length];
for (int i = 2 * period; i < high.length; i++) {
adxr[i] = (adx[i] + adx[i - period]) / 2;
}
return adxr;
}
}
```
使用方法:
```java
double[] high = {10.0, 11.0, 12.0, 13.0, 14.0};
double[] low = {9.0, 10.0, 11.0, 12.0, 13.0};
double[] close = {9.5, 10.5, 11.5, 12.5, 13.5};
int period = 14;
double[] adx = DMIIndicator.adx(high, low, close, period);
double[] adxr = DMIIndicator.adxr(high, low, close, period);
```
其中,`high`、`low`、`close` 分别是股票的最高价、最低价和收盘价,`period` 是计算周期。`adx` 和 `adxr` 分别是计算出来的 ADX 和 ADXR 数组。
阅读全文