股票20个常用指标python代码
时间: 2023-10-26 20:23:10 浏览: 232
python股票MACD,KDJ,LSTM,MA等多种技术指标分析预测完整代码
以下是股票常用指标的 Python 代码示例:
1. 移动平均线(MA)
```python
import pandas as pd
# 读取股票数据
df = pd.read_csv('stock.csv', index_col='date', parse_dates=True)
# 计算5日移动平均线
df['MA5'] = df['close'].rolling(5).mean()
# 计算10日移动平均线
df['MA10'] = df['close'].rolling(10).mean()
# 计算20日移动平均线
df['MA20'] = df['close'].rolling(20).mean()
```
2. 指数移动平均线(EMA)
```python
import pandas as pd
# 读取股票数据
df = pd.read_csv('stock.csv', index_col='date', parse_dates=True)
# 计算12日指数移动平均线
df['EMA12'] = df['close'].ewm(span=12, adjust=False).mean()
# 计算26日指数移动平均线
df['EMA26'] = df['close'].ewm(span=26, adjust=False).mean()
```
3. 相对强弱指标(RSI)
```python
import pandas as pd
import talib
# 读取股票数据
df = pd.read_csv('stock.csv', index_col='date', parse_dates=True)
# 计算14日相对强弱指标
df['RSI'] = talib.RSI(df['close'].values, timeperiod=14)
```
4. 布林带(BOLL)
```python
import pandas as pd
import talib
# 读取股票数据
df = pd.read_csv('stock.csv', index_col='date', parse_dates=True)
# 计算布林带
df['upper'], df['middle'], df['lower'] = talib.BBANDS(df['close'].values, timeperiod=20)
```
5. 随机指标(KDJ)
```python
import pandas as pd
import talib
# 读取股票数据
df = pd.read_csv('stock.csv', index_col='date', parse_dates=True)
# 计算KDJ指标
high = df['high'].values
low = df['low'].values
close = df['close'].values
df['K'], df['D'] = talib.STOCH(high, low, close, fastk_period=9, slowk_period=3, slowk_matype=0, slowd_period=3, slowd_matype=0)
df['J'] = 3 * df['K'] - 2 * df['D']
```
6. 平均真实波幅(ATR)
```python
import pandas as pd
import talib
# 读取股票数据
df = pd.read_csv('stock.csv', index_col='date', parse_dates=True)
# 计算14日平均真实波幅
df['ATR'] = talib.ATR(df['high'].values, df['low'].values, df['close'].values, timeperiod=14)
```
7. 动量指标(MOM)
```python
import pandas as pd
import talib
# 读取股票数据
df = pd.read_csv('stock.csv', index_col='date', parse_dates=True)
# 计算12日动量指标
df['MOM'] = talib.MOM(df['close'].values, timeperiod=12)
```
8. 相对强弱指数(RSI)
```python
import pandas as pd
import talib
# 读取股票数据
df = pd.read_csv('stock.csv', index_col='date', parse_dates=True)
# 计算14日相对强弱指数
df['RSI'] = talib.RSI(df['close'].values, timeperiod=14)
```
9. 指数平滑移动平均线(MACD)
```python
import pandas as pd
import talib
# 读取股票数据
df = pd.read_csv('stock.csv', index_col='date', parse_dates=True)
# 计算MACD指标
macd, macdsignal, macdhist = talib.MACD(df['close'].values, fastperiod=12, slowperiod=26, signalperiod=9)
df['MACD'] = macd
df['MACD_SIGNAL'] = macdsignal
df['MACD_HIST'] = macdhist
```
10. 相对强弱平均线(RMI)
```python
import pandas as pd
import talib
# 读取股票数据
df = pd.read_csv('stock.csv', index_col='date', parse_dates=True)
# 计算RMI指标
df['RMI'] = talib.RMI(df['close'].values, timeperiod=14)
```
11. 能量潮指标(OBV)
```python
import pandas as pd
import talib
# 读取股票数据
df = pd.read_csv('stock.csv', index_col='date', parse_dates=True)
# 计算OBV指标
df['OBV'] = talib.OBV(df['close'].values, df['volume'].values)
```
12. 三重指数平滑移动平均线(TRIX)
```python
import pandas as pd
import talib
# 读取股票数据
df = pd.read_csv('stock.csv', index_col='date', parse_dates=True)
# 计算TRIX指标
df['TRIX'] = talib.TRIX(df['close'].values, timeperiod=30)
```
13. 移动平均线收敛/发散指标(MACD)
```python
import pandas as pd
import talib
# 读取股票数据
df = pd.read_csv('stock.csv', index_col='date', parse_dates=True)
# 计算MACD指标
macd, macdsignal, macdhist = talib.MACD(df['close'].values, fastperiod=12, slowperiod=26, signalperiod=9)
df['MACD'] = macd
df['MACD_SIGNAL'] = macdsignal
df['MACD_HIST'] = macdhist
```
14. 顺势指标(CCI)
```python
import pandas as pd
import talib
# 读取股票数据
df = pd.read_csv('stock.csv', index_col='date', parse_dates=True)
high = df['high'].values
low = df['low'].values
close = df['close'].values
# 计算CCI指标
df['CCI'] = talib.CCI(high, low, close, timeperiod=20)
```
15. 平均趋向指标(ADX)
```python
import pandas as pd
import talib
# 读取股票数据
df = pd.read_csv('stock.csv', index_col='date', parse_dates=True)
high = df['high'].values
low = df['low'].values
close = df['close'].values
# 计算ADX指标
df['ADX'] = talib.ADX(high, low, close, timeperiod=14)
```
16. 相对强弱指标(ROC)
```python
import pandas as pd
import talib
# 读取股票数据
df = pd.read_csv('stock.csv', index_col='date', parse_dates=True)
# 计算ROC指标
df['ROC'] = talib.ROC(df['close'].values, timeperiod=10)
```
17. 心理线指标(PSY)
```python
import pandas as pd
import talib
# 读取股票数据
df = pd.read_csv('stock.csv', index_col='date', parse_dates=True)
# 计算PSY指标
df['PSY'] = talib.PSY(df['close'].values, timeperiod=12)
```
18. 威廉指标(WILLR)
```python
import pandas as pd
import talib
# 读取股票数据
df = pd.read_csv('stock.csv', index_col='date', parse_dates=True)
high = df['high'].values
low = df['low'].values
close = df['close'].values
# 计算WILLR指标
df['WILLR'] = talib.WILLR(high, low, close, timeperiod=14)
```
19. 相对强弱动量指标(RMI)
```python
import pandas as pd
import talib
# 读取股票数据
df = pd.read_csv('stock.csv', index_col='date', parse_dates=True)
# 计算RMI指标
df['RMI'] = talib.RMI(df['close'].values, timeperiod=14)
```
20. 梅斯线指标(MASS)
```python
import pandas as pd
import talib
# 读取股票数据
df = pd.read_csv('stock.csv', index_col='date', parse_dates=True)
high = df['high'].values
low = df['low'].values
close = df['close'].values
# 计算MASS指标
df['MASS'] = talib.MASS(high, low, close)
```
阅读全文