记录一些股票常用的指标计算——python实现

做毕设要用到一些股票指标作为ELM的输入值,一共17个。稍微找了一下talib里的一些函数,跟我需要用到的指标还差那么一点,只好自己找公式实现了。但其实那些指标背后的公式都很简单。这里顺便把用到的talib里的函数也一起放上了。

指标

先把要用指标放上来

缩写 描述
K KDJ中的K值
D KDJ中的D值
J KDJ中的J值
MACD 异同移动平均线
MOM 动量线
BIAS 乖离率
CMO 钱德动量摆动指标
TRIX 三重指数平滑平均线
OBV 能量潮
ROC 变动率指标
AMA 移动平均平行线差指标
VR 成交量变异率
PSY 心理线指标
Force Index 强力指数指标
DPO 区间震荡线
VHF 十字过滤线指标
RVI 相对活力指数

实现

先导入几个包,除了talib、numpy和pandas以外还有stockstats、pandas_talib

import pandas as pd
import numpy as np
import talib
import stockstats
import pandas_talib
'''
	这里虽然没有定义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.concat([sig_k, sig_d, sig_j], axis=1, keys=['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(close, timeperiod=10)
sig['TRIX'] = talib.TRIX(close, timeperiod=14)
sig['OBV'] = talib.OBV(close, volume)
sig['ROC'] = talib.ROC(close, timeperiod=10)
sig['VR'] = stockStat['vr']
sig['Force_Index'] = pandas_talib.FORCE(df, 12)['Force_12']

然后就是自己实现的指标了。

BIAS

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

AMA

def AMA(stockStat):
    return talib.MA(stockStat['dma'],  timeperiod=10)

PSY

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

DPO

def DPO(close):
    p = talib.MA(close, timeperiod=11)
    p.shift()
    return close-p

VHF

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)

RVI

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

你可能感兴趣的:(毕设)