python_计算股票指标
使用python计算常用的股票指标,本文涉及到的指标包括:RSI、OBV、MACD、 KDJ、 SAR、 VOL、 PSY、 ARBR、 CR、 EMV、 BOLL、 TRIX、 DMA、 BIAS、 CCI、 W%R、 ROC、 DMI
注:能使用talib包的,优先使用talib包计算;本文中指标图来自同花顺截图
使用python计算常用的股票指标,本文涉及到的指标包括:RSI、OBV、MACD、 KDJ、 SAR、 VOL、 PSY、 ARBR、 CR、 EMV、 BOLL、 TRIX、 DMA、 BIAS、 CCI、 W%R、 ROC、 DMI
目录
导入需要的包
import pandas as pd
import math
import talib
RSI
中文名:相对强弱指标
计算公式:
RSI有两种计算方法:
第一种方法:
假设A为N日内收盘价涨幅的正数之和,B为N日内收盘价涨幅的负数之和再乘以(-1),这样,A和B均为正,将A,B代入RSI计算公式,则:
RSI(N) = A ÷ (A + B) × 100
第二种方法:
RS(相对强度) = N日内收盘价涨数和之均值 ÷ N日内收盘价跌数和之均值
RSI = 100 - 100 ÷ (1+RS)
df = pd.read_csv('E:/temp005/600660.csv',encoding='utf-8')
# 删除停牌的数据
df = df.loc[df['openPrice']>0].copy()
df['openPrice'] = df['openPrice']*df['accumAdjFactor']
df['closePrice'] = df['closePrice']*df['accumAdjFactor']
df['highestPrice'] = df['highestPrice']*df['accumAdjFactor']
df['lowestPrice'] = df['lowestPrice']*df['accumAdjFactor']
df['rsi'] = talib.RSI(df['closePrice'], timeperiod=14)
OBV
中文名:能量潮
计算公式:
OBV = 前一天的OBV ± 当日成交量
说明:(当日收盘价高于前日收盘价,成交量定位为正值,取加号;当日收盘价低于前日收盘价,成交量定义为负值,取减号;二者相等计为0)
df = pd.read_csv('E:/temp005/600660.csv',encoding='utf-8')
# 删除停牌的数据
df = df.loc[df['openPrice']>0].copy()
df['openPrice'] = df['openPrice']*df['accumAdjFactor']
df['closePrice'] = df['closePrice']*df['accumAdjFactor']
df['highestPrice'] = df['highestPrice']*df['accumAdjFactor']
df['lowestPrice'] = df['lowestPrice']*df['accumAdjFactor']
df['obv'] = talib.OBV(df['closePrice'],df['turnoverVol'])
MACD
中文名:指数平滑异同平均线
计算公式:
1) 简单移动平均MA
说明:P1~PN为交易日的收盘价;N为移动平均的天数
2) 加权移动平均EMA
说明:W1~WN是权重系数,W1>W2>…>WN
3) 快速时间窗口设为12日,慢速时间窗口设为26日,DIF参数设为9日,来完整计算一遍MACD
3.1) 计算指数平滑移动平均值(EMA)
12日EMA的计算公式为:
EMA(12) = 昨日EMA(12) × 11 ÷ 13 + 今日收盘价 × 2 ÷ 13
26日EMA的计算公式为:
EMA(26) = 昨日EMA(26) × 25 ÷ 27 + 今日收盘价 × 2 ÷ 27
3.2) 计算离差值(DIF)
DIF = 今日EMA(12) – 今日EMA(26)
3.3) 计算DIF的9日DEA
根据差值计算其9日的DEA,即差值平均
今日DEA = 昨日DEA × 8 ÷ 10 + 今日DIF × 2 ÷ 10
df = pd.read_csv('E:/temp005/600660.csv',encoding='utf-8')
# 删除停牌的数据
df = df.loc[df['openPrice']>0].copy()
df['openPrice'] = df['openPrice']*df['accumAdjFactor']
df['closePrice'] = df['closePrice']*df['accumAdjFactor']
df['highestPrice'] = df['highestPrice']*df['accumAdjFactor']
df['lowestPrice'] = df['lowestPrice']*df['accumAdjFactor']
df['DIFF'],df['DEA'],df['MACD'] = talib.MACD(close_list,fastperiod=12,slowperiod=26,signalperiod=9)
KDJ
中文名:随机指标
计算公式:
1) 以日KDJ数值的计算为例
N日RSV = (CN – LN)÷(HN-LN) ×100
说明:CN为第N日收盘价;LN为N日内的最低价;HN为N日内的最高价,RSV值始终在1~100间波动
2) 计算K值与D值
当日K值 = 2/3 × 前一日K值 + 1/3 × 当日RSV
当日D值 = 2/3 × 前一日D值 + 1/3 × 当日K值
如果没有前一日K值与D值,则可分别用50来代替
3) 计算J值
J = 3D – 2K
df = pd.read_csv('E:/temp005/600660.csv',encoding='utf-8')
# 删除停牌的数据
df = df.loc[df['openPrice']>0].copy()
df['openPrice'] = df['openPrice']*df['accumAdjFactor']
df['closePrice'] = df['closePrice']*df['accumAdjFactor']
df['highestPrice'] = df['highestPrice']*df['accumAdjFactor']
df['lowestPrice'] = df['lowestPrice']*df['accumAdjFactor']
df['kdj_k'],df['kdj_d'] = talib.STOCH(df['highestPrice'],df['lowestPrice'],df['closePrice'],fastk_period=5,slowk_period=3, slowk_matype=0, slowd_period=3, slowd_matype=0)
df['kdj_j'] = 3*df['kdj_k'] - 2*df['kdj_d']
SAR
中文名:抛物转向
计算过程:
1)先选定一段时间判断为上涨或下跌
2)如果是看涨,则第一天的SAR值必须是近期内的最低价;如果是看跌,则第一天的SAR值必须是近期的最高价。
3)第二天的SAR值,则为第一天的最高价(看涨时)或是最低价(看跌时)与第一天的SAR值的差距乘上加速因子,再加上第一天的SAR值就可以求得。
4)每日的SAR值都可用上述方法类推,公式归纳如下:
SAR(N) = SAR(N-1) + AF × [(EP(N-1) – SAR(N-1))]
SAR(N) = 第N日的SAR值
SAR(N-1) = 第(N-1)日的SAR值
说明:AF表示加速因子;EP表示极点价,如果是看涨一段期间,则EP为这段时间的最高价,如果是看跌一段期间,则EP为这段时间的最低价;EP(N-1)等于第(N-1)日的极点价
5)加速因子第一次取0.02,假若第一天的最高价比前一天的最高价还高,则加速因子增加0.02,如无新高则加速因子沿用前一天的数值,但加速因子最高不能超过0.2。反之,下跌也类推
6)如果是看涨期间,计算出某日的SAR值比当日或前一日的最低价高,则应以当日或前一日的最低价为某日之SAR值;如果是看跌期间,计算某日的SAR值比当日或前一日的最高价低,则应以当日或前一日的最高价为某日的SAR值。
7)SAR指标基准周期的参数为2,如2日、2周、2月等,其计算周期的参数变动范围为2~8
8)SAR指标在股价分析系统的主图上显示为“O”形点状图。
df = pd.read_csv('E:/temp005/600660.csv',encoding='utf-8')
# 删除停牌的数据
df = df.loc[df['openPrice']>0].copy()
df['openPrice'] = df['openPrice']*df['accumAdjFactor']
df['closePrice'] = df['closePrice']*df['accumAdjFactor']
df['highestPrice'] = df['highestPrice']*df['accumAdjFactor']
df['lowestPrice'] = df['lowestPrice']*df['accumAdjFactor']
df['sar'] = talib.SAR(df['highestPrice'],df['lowestPrice'])
VOL
中文:成交量
柱状图是成交量,两条曲线是成交量的移动平均
df = pd.read_csv('E:/temp005/600660.csv',encoding='utf-8')
# 删除停牌的数据
df = df.loc[df['openPrice']>0].copy()
df['openPrice'] = df['openPrice']*df['accumAdjFactor']
df['closePrice'] = df['closePrice']*df['accumAdjFactor']
df['highestPrice'] = df['highestPrice']*df['accumAdjFactor']
df['lowestPrice'] = df['lowestPrice']*df['accumAdjFactor']
df['vol5'] = talib.MA(df['turnoverVol'],timeperiod=5)
df['vol10'] = talib.MA(df['turnoverVol'],timeperiod=10)
PSY
中文名:心理线
计算公式:
PSY(N) = A/N × 100
说明:N为天数,A为在这N天之中股价上涨的天数
df = pd.read_csv('E:/temp005/600660.csv',encoding='utf-8')
# 删除停牌的数据
df = df.loc[df['openPrice']>0].copy()
df['openPrice'] = df['openPrice']*df['accumAdjFactor']
df['closePrice'] = df['closePrice']*df['accumAdjFactor']
df['highestPrice'] = df['highestPrice']*df['accumAdjFactor']
df['lowestPrice'] = df['lowestPrice']*df['accumAdjFactor']
df['ext_0'] = df['closePrice']-df['closePrice'].shift(1)
df['ext_1'] = 0
df.loc[df['ext_0']>0,'ext_1'] = 1
df['ext_2'] = df['ext_1'].rolling(window=12).sum()
df['psy'] = (df['ext_2']/12.0)*100
ARBR
中文:人气和意愿指标 AR:人气指标 BR:买卖意愿指标
计算公式:
AR(N) = N日内(H-O)之和 ÷ N日内(O-L)之和 × 100
说明:H表示当天最高价;L表示当天最低价;O表示当天开盘价;N表示设定的时间参数,一般原始参数日缺省值为26日
BR(N) = N日内(H-CY)之和 ÷ N日内(CY-L)之和 × 100
说明:H表示当天最高价;L表示当天最低价;CY表示前一交易日的收盘价,N表示设定的时间参数,一般原始参数缺省值为26日
df = pd.read_csv('E:/temp005/600660.csv',encoding='utf-8')
# 删除停牌的数据
df = df.loc[df['openPrice']>0].copy()
df['openPrice'] = df['openPrice']*df['accumAdjFactor']
df['closePrice'] = df['closePrice']*df['accumAdjFactor']
df['highestPrice'] = df['highestPrice']*df['accumAdjFactor']
df['lowestPrice'] = df['lowestPrice']*df['accumAdjFactor']
df['h_o'] = df['highestPrice'] - df['openPrice']
df['o_l'] = df['openPrice'] - df['lowestPrice']
df['h_o_sum'] = df['h_o'].rolling(window=26).sum()
df['o_l_sum'] = df['o_l'].rolling(window=26).sum()
df['ar'] = (df['h_o_sum']/df['o_l_sum'])*100
df['h_c'] = df['highestPrice'] - df['closePrice']
df['c_l'] = df['closePrice'] - df['lowestPrice']
df['h_c_sum'] = df['h_c'].rolling(window=26).sum()
df['c_l_sum'] = df['c_l'].rolling(window=26).sum()
df['br'] = (df['h_c_sum']/df['c_l_sum'])*100
CR
中文:带状能力线或中间意愿指标
计算过程:
1)计算中间价,取以下四种中一种,任选:
中间价 = (最高价 + 最低价)÷2
中间价 = (最高价 + 最低价 + 收盘价)÷3
中间价 = (最高价 + 最低价 + 开盘价 + 收盘价)÷4
中间价 = (2倍的开盘价 + 最高价 + 最低价)÷4
2)计算CR:
CR = N日内(当日最高价 – 上个交易日的中间价)之和 ÷ N日内(上个交易日的中间价 – 当日最低价)之和
说明:N为设定的时间周期参数,一般原始参数日设定为26日
3)计算CR值在不同时间周期内的移动平均值:这三条移动平均曲线分别为MA1 MA2 MA3,时间周期分别为5日 10日 20日
df = pd.read_csv('E:/temp005/600660.csv',encoding='utf-8')
# 删除停牌的数据
df = df.loc[df['openPrice']>0].copy()
df['openPrice'] = df['openPrice']*df['accumAdjFactor']
df['closePrice'] = df['closePrice']*df['accumAdjFactor']
df['highestPrice'] = df['highestPrice']*df['accumAdjFactor']
df['lowestPrice'] = df['lowestPrice']*df['accumAdjFactor']
df['m_price'] = (df['highestPrice'] + df['lowestPrice'])/2
df['h_m'] = df['highestPrice']-df['m_price'].shift(1)
df['m_l'] = df['m_price'].shift(1)-df['lowestPrice']
df['h_m_sum'] = df['h_m'].rolling(window=26).sum()
df['m_l_sum'] = df['m_l'].rolling(window=26).sum()
df['cr'] = (df['h_m_sum']/df['m_l_sum'])*100
df['ma1'] = talib.MA(df['cr'],timeperiod=5)
df['ma2'] = talib.MA(df['cr'],timeperiod=10)
df['ma3'] = talib.MA(df['cr'],timeperiod=20)
EMV
中文:简易波动指标
计算方法:
1)先计算出三个因子A B C的数值。
A = (当日最高价 + 当日最低价)÷2
B = (上个交易日最高价 + 上个交易日最低价) ÷2
C = 当日最高价 – 当日最低价
2)求出EM数值
EM = (A-B) ×C÷当日成交额
3)求出EMV数值
EMV = EM数值的N个交易日之和,N为时间周期,一般设为14日
4)求出EMV的移动平均值EMVA
EMVA = EMV的M日移动平均值,M一般设置9日
df = pd.read_csv('E:/temp005/600660.csv',encoding='utf-8')
# 删除停牌的数据
df = df.loc[df['openPrice']>0].copy()
df['openPrice'] = df['openPrice']*df['accumAdjFactor']
df['closePrice'] = df['closePrice']*df['accumAdjFactor']
df['highestPrice'] = df['highestPrice']*df['accumAdjFactor']
df['lowestPrice'] = df['lowestPrice']*df['accumAdjFactor']
df['a'] = (df['highestPrice']+df['lowestPrice'])/2
df['b'] = (df['highestPrice'].shift(1)+df['lowestPrice'].shift(1))/2
df['c'] = df['highestPrice'] - df['lowestPrice']
df['em'] = (df['a']-df['b'])*df['c']/df['turnoverValue']
df['emv'] = df['em'].rolling(window=14).sum()
df['emva'] = talib.MA(df['emv'],timeperiod=9)
BOLL
中文名:布林线指标
计算公式:
中轨线 = N日的移动平均线
上轨线 = 中轨线 + 两倍的标准差
下轨线 = 中轨线 – 两倍的标准差
计算过程:
1)先计算出移动平均值MA
MA = N日内的收盘价之和÷N
2)计算出标准差MD的平方
MD的平方 = 每个交易日的(收盘价-MA)的N日累加之和的两次方 ÷ N
3)求出MD
MD = (MD的平方)的平方根
4)计算MID、UPPER、LOWER的数值
MID = (N-1)日的MA
UPPER = MID + 2×MD
LOWER = MID – 2×MD
说明:N一般原始参数日缺省值为20日
df = pd.read_csv('E:/temp005/600660.csv',encoding='utf-8')
# 删除停牌的数据
df = df.loc[df['openPrice']>0].copy()
df['openPrice'] = df['openPrice']*df['accumAdjFactor']
df['closePrice'] = df['closePrice']*df['accumAdjFactor']
df['highestPrice'] = df['highestPrice']*df['accumAdjFactor']
df['lowestPrice'] = df['lowestPrice']*df['accumAdjFactor']
df['upper'],df['mid'],df['lower'] = talib.BBANDS(df['closePrice'],timeperiod=20,nbdevup=2, nbdevdn=2, matype=0)
TRIX
中文名:三重指数平滑移动平均指标
df = pd.read_csv('E:/temp005/600660.csv',encoding='utf-8')
# 删除停牌的数据
df = df.loc[df['openPrice']>0].copy()
df['openPrice'] = df['openPrice']*df['accumAdjFactor']
df['closePrice'] = df['closePrice']*df['accumAdjFactor']
df['highestPrice'] = df['highestPrice']*df['accumAdjFactor']
df['lowestPrice'] = df['lowestPrice']*df['accumAdjFactor']
df['trix'] = talib.TRIX(df['closePrice'],timeperiod=12)
df['trma'] = talib.MA(df['trix'],timeperiod=20)
DMA
中文名:平均线差
计算公式:
DDD(N) = N日短期平均值 – M日长期平均值
AMA(N) = DDD的N日短期平均值
计算过程:
以求10日、50日为基准周期的DMA指标为例
1)求出周期不等的两条移动平均线MA之间的差值
DDD(10) = MA10 – MA50
2)求DDD的10日移动平均数值
AMA(10) = DDD(10)÷10
df = pd.read_csv('E:/temp005/600660.csv',encoding='utf-8')
# 删除停牌的数据
df = df.loc[df['openPrice']>0].copy()
df['openPrice'] = df['openPrice']*df['accumAdjFactor']
df['closePrice'] = df['closePrice']*df['accumAdjFactor']
df['highestPrice'] = df['highestPrice']*df['accumAdjFactor']
df['lowestPrice'] = df['lowestPrice']*df['accumAdjFactor']
df['ma10'] = talib.MA(df['closePrice'],timeperiod=10)
df['ma50'] = talib.MA(df['closePrice'],timeperiod=50)
df['ddd'] = df['ma10'] - df['ma50']
df['ama'] = talib.MA(df['ddd'],timeperiod=10)
BIAS
中文名:乖离率
计算公式:
N日BIAS = (当日收盘价 – N日移动平均价)÷N日移动平均价×100
df = pd.read_csv('E:/temp005/600660.csv',encoding='utf-8')
# 删除停牌的数据
df = df.loc[df['openPrice']>0].copy()
df['openPrice'] = df['openPrice']*df['accumAdjFactor']
df['closePrice'] = df['closePrice']*df['accumAdjFactor']
df['highestPrice'] = df['highestPrice']*df['accumAdjFactor']
df['lowestPrice'] = df['lowestPrice']*df['accumAdjFactor']
df['ma6'] = talib.MA(df['closePrice'],timeperiod=6)
df['ma12'] = talib.MA(df['closePrice'],timeperiod=12)
df['ma24'] = talib.MA(df['closePrice'],timeperiod=24)
df['bias'] = ((df['closePrice']-df['ma6'])/df['ma6'])*100
df['bias2'] = ((df['closePrice']-df['ma12'])/df['ma12'])*100
df['bias3'] = ((df['closePrice']-df['ma24'])/df['ma24'])*100
CCI
中文名:顺势指标
计算过程:
CCI(N日) = (TP-MA)÷MD÷0.015
说明:TP = (最高价+最低价+收盘价)÷3;MA=最近N日收盘价的累计之和÷N;MD=最近N日(MA-收盘价)的累计之和÷N;0.015为计算系数;N为计算周期,默认为14天
df = pd.read_csv('E:/temp005/600660.csv',encoding='utf-8')
# 删除停牌的数据
df = df.loc[df['openPrice']>0].copy()
df['openPrice'] = df['openPrice']*df['accumAdjFactor']
df['closePrice'] = df['closePrice']*df['accumAdjFactor']
df['highestPrice'] = df['highestPrice']*df['accumAdjFactor']
df['lowestPrice'] = df['lowestPrice']*df['accumAdjFactor']
df['cci'] = talib.CCI(df['highestPrice'],df['lowestPrice'],df['closePrice'],timeperiod=14)
W%R
中文名:威廉指标
计算公式:
df = pd.read_csv('E:/temp005/600660.csv',encoding='utf-8')
# 删除停牌的数据
df = df.loc[df['openPrice']>0].copy()
df['openPrice'] = df['openPrice']*df['accumAdjFactor']
df['closePrice'] = df['closePrice']*df['accumAdjFactor']
df['highestPrice'] = df['highestPrice']*df['accumAdjFactor']
df['lowestPrice'] = df['lowestPrice']*df['accumAdjFactor']
n = 10
df['h_10'] = df['highestPrice'].rolling(window=n).max()
df['l_10'] = df['lowestPrice'].rolling(window=n).min()
df['wr10'] = ((df['h_10']-df['closePrice'])/(df['h_10']-df['l_10']))*100
n2 = 6
df['h_6'] = df['highestPrice'].rolling(window=n2).max()
df['l_6'] = df['lowestPrice'].rolling(window=n2).min()
df['wr6'] = ((df['h_6'] - df['closePrice']) / (df['h_6'] - df['l_6'])) * 100
ROC
中文名:变动速率指标
计算过程:
1)计算出ROC数值
ROC = (当日收盘价 – N日前收盘价)÷N日前收盘价×100
说明:N一般取值为12日
2)计算ROC移动平均线(ROCMA)数值
ROCMA = ROC的M日数值之和÷M
说明:M一般取值为6日
df = pd.read_csv('E:/temp005/600660.csv',encoding='utf-8')
# 删除停牌的数据
df = df.loc[df['openPrice']>0].copy()
df['openPrice'] = df['openPrice']*df['accumAdjFactor']
df['closePrice'] = df['closePrice']*df['accumAdjFactor']
df['highestPrice'] = df['highestPrice']*df['accumAdjFactor']
df['lowestPrice'] = df['lowestPrice']*df['accumAdjFactor']
df['roc'] = talib.ROC(df['closePrice'],timeperiod=12)
df['rocma'] = talib.MA(df['roc'],timeperiod=6)
DMI
中文名:趋向指标
df = pd.read_csv('E:/temp005/600660.csv',encoding='utf-8')
# 删除停牌的数据
df = df.loc[df['openPrice']>0].copy()
df['openPrice'] = df['openPrice']*df['accumAdjFactor']
df['closePrice'] = df['closePrice']*df['accumAdjFactor']
df['highestPrice'] = df['highestPrice']*df['accumAdjFactor']
df['lowestPrice'] = df['lowestPrice']*df['accumAdjFactor']
df['pdi'] = talib.PLUS_DI(df['highestPrice'],df['lowestPrice'],df['closePrice'],timeperiod=14)
df['mdi'] = talib.MINUS_DI(df['highestPrice'],df['lowestPrice'],df['closePrice'],timeperiod=14)
df['adx'] = talib.ADX(df['highestPrice'],df['lowestPrice'],df['closePrice'],timeperiod=6)
df['adxr'] = talib.ADXR(df['highestPrice'],df['lowestPrice'],df['closePrice'],timeperiod=6)
数据
链接:https://pan.baidu.com/s/1HPkMsDDyXTEgffoAVIhbZw
提取码:h80x
更多推荐
所有评论(0)