股票常用指标
指标
先把要用指标放上来
缩写 | 描述 |
---|---|
K | KDJ中的K值 |
D | KDJ中的D值 |
J | KDJ中的J值 |
MACD | 异同移动平均线 |
MOM | 动量线 |
BIAS | 乖离率 |
CMO | 钱德动量摆动指标 |
TRIX | 三重指数平滑平均线 |
OBV | 能量潮 |
ROC | 变动率指标 |
AMA | 移动平均平行线差指标 |
VR | 成交量变异率 |
PSY | 心理线指标 |
Force Index | 强力指数指标 |
DPO | 区间震荡线 |
VHF | 十字过滤线指标 |
RVI | 相对活力指数 |
实现
import pandas as pd
import numpy as np
import talib
import stockstats
import pandas_talib
import tushare as ts
df=ts.get_hist_data('601857')
df1=df.copy()
'''
这里虽然没有定义df这个变量,但这很明显就是dateframe格式的某只股票基础数据
包括开盘价、收盘价、最高价、最低价和成交量
建议用tushare来获取数据(当然仅限日数据)
'''
stockStat = stockstats.StockDataFrame.retype(df)
close = df.close
highPrice = df.high
lowPrice = df.low
volume = df.volume
df.rename(columns={'close': 'Close', 'volume': 'Volume'}, inplace=True)
sig_k , sig_d = talib.STOCH(np.array(highPrice), np.array(lowPrice),
np.array(close), fastk_period=9,slowk_period=3,
slowk_matype=0, slowd_period=3, slowd_matype=0)
sig_j = sig_k * 3 - sig_d * 2
sig = pd.DataFrame()
sig['K']=list(sig_k)
sig['D']=list(sig_d)
sig['J']=list(sig_j)
#sig = pd.DataFrame([list(sig_k), list(sig_d), list(sig_j)], columns=['K', 'D', 'J'])
sig['MACD'], MACDsignal, MACDhist = talib.MACD(np.array(close), fastperiod=6,
slowperiod=12, signalperiod=9)
sig['MOM'] = talib.MOM(np.array(close), timeperiod=5)
sig['CMO'] = talib.CMO(np.array(close), timeperiod=10)
sig['TRIX'] = talib.TRIX(np.array(close), timeperiod=14)
sig['OBV'] = talib.OBV(np.array(close), volume)
sig['ROC'] = talib.ROC(np.array(close), timeperiod=10)
sig['VR'] = list(stockStat['vr'])
sig['Force_Index'] = list(pandas_talib.FORCE(df, 12)['Force_12'])
def BIAS(close, timeperiod=20):
if isinstance(close,np.ndarray):
pass
else:
close = np.array(close)
MA = talib.MA(close,timeperiod=timeperiod)
return (close-MA)/MA
bias=BIAS(close, timeperiod=20)
def AMA(stockStat):
return talib.MA(stockStat['dma'], timeperiod=10)
ama=AMA(stockStat)
def PSY(priceData, period):
difference = priceData[1:] - priceData[:-1]
difference = np.append(0, difference)
difference_dir = np.where(difference > 0, 1, 0)
psy = np.zeros((len(priceData),))
psy[:period] *= np.nan
for i in range(period, len(priceData)):
psy[i] = (difference_dir[i-period+1:i+1].sum()) / period
return psy*100
def DPO(close):
p = talib.MA(close, timeperiod=11)
p.shift()
return close-p
def VHF(close):
LCP = talib.MIN(close, timeperiod=28)
HCP = talib.MAX(close, timeperiod=28)
NUM = HCP - LCP
pre = close.copy()
pre = pre.shift()
DEN = abs(close-close.shift())
DEN = talib.MA(DEN, timeperiod=28)*28
return NUM.div(DEN)
vhf=VHF(close)
def RVI(df):
close = df.close
open = df.open
high = df.high
low = df.low
X = close-open+2*(close.shift()-open.shift())+2*(close.shift(periods=2)-open.shift(periods=2))*(close.shift(periods=3)-
open.shift(periods=3))/6
Y = high-low+2*(high.shift()-low.shift())+2*(high.shift(periods=2)-low.shift(periods=2))*(high.shift(periods=3)-
low.shift(periods=3))/6
Z = talib.MA(X, timeperiod=10)*10
D = talib.MA(Y, timeperiod=10)*10
return Z/D
rvi=RVI(df1)