The installation command is as follows:

pip install stockstats
Copy the code
conda install -c conda-forge ta-lib

Copy the code

You can find out what each indicator means on the Mbalib website. For example: wiki.mbalib.com/wiki/ triple exponential smoothing…

abbreviations describe
K The K value in KDJ
D The D value in KDJ
J The J value in KDJ
MACD Similarities and differences moving average
MOM Momentum of the line
BIAS Good rate
CMO Chandler momentum swing indicator
TRIX Triple exponential smooth average
OBV The energy boom
ROC Index of variation rate
AMA Moving average parallel line difference indicator
VR Volume variation rate
PSY Psychological line index
Force Index Strength index
DPO Interval oscillation line
VHF Index of cross filter wire
RVI Relative vitality index

implementation

Lead to several packages, including stockSTATS and pandas_talib, in addition to Talib, numpy, and Pandas

import pandas as pd
import numpy as np
import talib
import stockstats
import pandas_talib
The df variable is not defined here, but it is clearly a dateframe format for a stock. The underlying data includes the opening price, closing price, high price, low price, and volume.
stockStat = stockstats.StockDataFrame.retype(df)
close = df.close
highPrice = df.high
lowPrice = df.low
volume = df.volume

Copy the code

Then put out some of the good indicators that other libraries have achieved

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']
Copy the code

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

Copy the code

AMA

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

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
Copy the code

DPO

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

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)
Copy the code
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
Copy the code

Refer to the blog

Pypi.org/project/sto…

Anaconda.org/conda-forge…

www.geek-share.com/detail/2749…