股票指标“乖离率、心理线、相对强弱指标、异同移动平均线、随机指标、BOLL、趋向指标”的python代码
时间: 2023-09-23 20:06:41 浏览: 236
### 回答1:
以下是这些股票指标在Python中的实现:
1. 乖离率
```python
def bias(close, n=5, m=10):
ma_n = close.rolling(n).mean()
ma_m = close.rolling(m).mean()
return (close - ma_m) / ma_m * 100 - (ma_n - ma_m) / ma_m * 100
```
2. 心理线
```python
def psy(close, n=12):
diff = np.diff((close > close.shift()).astype(int))
diff = np.concatenate(([0], diff))
psy = pd.Series(np.where(diff == 1, n, np.where(diff == -1, 100 - n, np.nan)), index=close.index)
psy = psy.fillna(method='ffill')
return psy
```
3. 相对强弱指标(RSI)
```python
def rsi(close, n=14):
diff = close.diff()
up = diff.where(diff > 0, 0)
dn = -diff.where(diff < 0, 0)
ma_up = up.rolling(n).mean()
ma_dn = dn.rolling(n).mean()
rs = ma_up / ma_dn
rsi = 100 - 100 / (1 + rs)
return rsi
```
4. 异同移动平均线(MACD)
```python
def macd(close, n_fast=12, n_slow=26, n_signal=9):
ema_fast = close.ewm(span=n_fast, adjust=False).mean()
ema_slow = close.ewm(span=n_slow, adjust=False).mean()
diff = ema_fast - ema_slow
dea = diff.ewm(span=n_signal, adjust=False).mean()
macd = 2 * (diff - dea)
return macd
```
5. 随机指标(KDJ)
```python
def kdj(high, low, close, n=9, m1=3, m2=3):
low_list = low.rolling(n, min_periods=1).min()
high_list = high.rolling(n, min_periods=1).max()
rsv = (close - low_list) / (high_list - low_list) * 100
k = rsv.ewm(alpha=1/m1, adjust=False).mean()
d = k.ewm(alpha=1/m2, adjust=False).mean()
j = 3 * k - 2 * d
return k, d, j
```
6. BOLL
```python
def boll(close, n=20, k=2):
ma = close.rolling(n).mean()
std = close.rolling(n).std()
up = ma + k * std
dn = ma - k * std
return ma, up, dn
```
7. 趋向指标(DMI)
```python
def dmi(high, low, close, n=14, m=6):
tr = pd.concat([high - low, (high - close.shift()).abs(), (low - close.shift()).abs()], axis=1).max(axis=1)
di_plus = (high.diff().clip(lower=0) / tr).rolling(n).sum() * 100
di_minus = (low.diff().clip(lower=0) / tr).rolling(n).sum() * 100
dx = ((di_plus - di_minus
### 回答2:
股票指标是投资者用于分析和预测股票价格走势的工具。下面是使用Python编写的一些常用股票指标的代码示例:
1. 乖离率(BR)指标:
```
import pandas as pd
def calculate_br(close, ma):
return (close - ma) / ma * 100
# 示例:
closing_prices = [10, 12, 11, 13, 14]
moving_average = pd.Series(closing_prices).rolling(window=5).mean() # 计算5日均线
br = calculate_br(pd.Series(closing_prices), moving_average)
print(br)
```
2. 心理线(PSY)指标:
```
def calculate_psy(close, n):
diff = close.diff() # 计算每日价格变动
up_count = diff[diff > 0].count() # 计算上涨天数
psy = up_count / n * 100
return psy
# 示例:
closing_prices = [10, 12, 11, 13, 14]
psy = calculate_psy(pd.Series(closing_prices), 5) # 计算5日心理线
print(psy)
```
3. 相对强弱指标(RSI):
```
def calculate_rsi(close, n):
diff = close.diff() # 计算每日价格变动
up_changes = diff[diff > 0].fillna(0) # 计算上涨幅度
down_changes = -diff[diff < 0].fillna(0) # 计算下跌幅度
avg_up = up_changes.rolling(window=n).mean() # 计算上涨平均值
avg_down = down_changes.rolling(window=n).mean() # 计算下跌平均值
rsi = 100 - (100 / (1 + avg_up / avg_down))
return rsi
# 示例:
closing_prices = [10, 12, 11, 13, 14]
rsi = calculate_rsi(pd.Series(closing_prices), 14) # 计算14日RSI
print(rsi)
```
类似的,可以根据需求编写异同移动平均线、随机指标、BOLL指标、趋向指标等其他指标的Python代码。这些代码示例仅作为简单的参考,实际使用时应根据具体需求进行修改和完善。
### 回答3:
对于股票指标"乖离率、心理线、相对强弱指标 (RSI)、异同移动平均线 (MACD)、随机指标 (KDJ)、BOLL、趋向指标 (ADX)" 需要的python代码如下所示:
1. 乖离率(BIAS):
```python
import pandas as pd
def BIAS(data, n):
MA = data['close'].rolling(n).mean()
BIAS = (data['close'] - MA) / MA
return BIAS
```
2. 心理线(PSY):
```python
def PSY(data, n):
diff = data['close'].diff()
diff[diff > 0] = 1
diff[diff < 0] = 0
PSY = diff.rolling(n).mean() * 100
return PSY
```
3. 相对强弱指标 (RSI):
```python
def RSI(data, n):
diff = data['close'].diff()
diff[diff > 0] = 1
diff[diff < 0] = -1
up = diff.copy()
down = diff.copy()
up[up < 0] = 0
down[down > 0] = 0
average_up = up.rolling(n).mean()
average_down = down.abs().rolling(n).mean()
RSI = average_up / (average_up + average_down) * 100
return RSI
```
4. 异同移动平均线 (MACD):
```python
def MACD(data, short_n, long_n, m):
short_ema = data['close'].ewm(span=short_n, adjust=False).mean()
long_ema = data['close'].ewm(span=long_n, adjust=False).mean()
diff = short_ema - long_ema
dea = diff.ewm(span=m, adjust=False).mean()
macd = (diff - dea) * 2
return macd
```
5. 随机指标 (KDJ):
```python
def KDJ(data, n, m1, m2):
L_n = data['low'].rolling(n).min()
H_n = data['high'].rolling(n).max()
RSV = (data['close'] - L_n) / (H_n - L_n) * 100
K = RSV.ewm(com=m1 - 1, adjust=False).mean()
D = K.ewm(com=m2 - 1, adjust=False).mean()
J = 3 * K - 2 * D
return K, D, J
```
6. BOLL指标:
```python
def BOLL(data, n, k):
MA = data['close'].rolling(n).mean()
MD = data['close'].rolling(n).std()
UP = MA + k * MD
DN = MA - k * MD
return MA, UP, DN
```
7. 趋向指标 (ADX):
```python
def TR(data):
TR = pd.concat([data['high'] - data['low'],
abs(data['high'] - data['close'].shift()),
abs(data['low'] - data['close'].shift())], axis=1).max(axis=1)
return TR
def DM(data):
UpMove = data['high'].diff()
DownMove = data['low'].diff().abs()
UpMove[UpMove <= 0] = 0
DownMove[DownMove <= 0] = 0
return UpMove, DownMove
def DX(data, n):
TR_n = TR(data.iloc[-n:])
UpMove_n, DownMove_n = DM(data.iloc[-n:])
Sum_TR_n, Sum_UpMove_n, Sum_DownMove_n = TR_n.sum(), UpMove_n.sum(), DownMove_n.sum()
DIp = Sum_UpMove_n / Sum_TR_n * 100
DIn = Sum_DownMove_n / Sum_TR_n * 100
DX = abs(DIp - DIn) / (DIp + DIn) * 100
return DX
def ADX(data, n, m):
DX_n = DX(data, n)
ADX = DX_n.ewm(com=m - 1, adjust=False).mean()
return ADX
```
以上是使用Python编写的一些常见股票指标的代码,可以根据需要在自己的程序中调用。请注意,这些代码可作为参考,具体在实际使用中可能需要根据不同的数据格式和计算方法进行适当的调整和优化。
阅读全文