韭菜成长记

韭菜成长记

一、超买超卖型指标

顺势指标(CCI)
CCI = talib.CCI(high, low, close, timeperiod=14)

资金流量指标(MFI)
MFI = talib.MFI(high, low, close, volume, timeperiod=14)

动力指标(MTM)
n 一般取12
def MTM(close, n):
    mtm = []
    for i in range(len(close)):
        if i < n:
            mtm.append(0)
        else:
            mtm.append(close[i] - close[i-n])
    return mtm
m一般取6
def MTMMA(mtm, m):
    return talib.SMA(mtm,m)

变动速率线
N一般取20
def OSC(close,n):
    ma = talib.MA(close,n)
    return [100 * (close[i] - ma[i]) for i in range(len(close))]
M一般取6
def OSCMA(OSC,m):
    return talib.SMA(OSC,m)

变动率指标
ROC = talib.ROC(close, timeperiod=10)
def ROCMA(roc,m):
    return talib.SMA(roc,m)
    
相对强弱指标RSI
RS = sum(n期内上涨幅度)/sum(n期内下跌幅度)
RSI = 100 - 100/(1 + RS) = 100RS/(1 + RS)
def RS(close,n):
    value_list= []
    lenght = len(close)
    for i in range(lenght):
        if i==0:
            value_list.append(0)
        else:
            value_list.append(close[i]-close[i-1])
    rs_list = []
    for v_id in range(lenght):
        if v_id<n:
            rs_list.append(0)
        else:
            p_sum = 0
            p_count = 0
            n_sum = 0
            n_count = 0
            for j in range(v_id-n,v_id):
                if value_list[j]>0:
                    p_sum += value_list[j]
                    p_count +=1
                else:
                    n_sum += value_list[j]* (-1)
                    n_count += 1
            if p_count == 0:
                rs_list.append(0)
            if n_count == 0:
                rs_list.append(0)
            if p_count != 0 and n_count != 0:
                rs_list.append(round((p_sum)/(n_sum),6))
    return rs_list
def RSI(rs_list):
    return [100 - 100/(1+rs) for rs in rs_list]
RSI = talib.RSI(close, timeperiod=14)

随机指标KD
RSV = 100 * (close - min(最近n日内low)/ ( max(最近n日内high)-min(最近n日内low))
K线:RSV的M1日移动平均线
D线:K值的M2日移动平均线
参数一般取值:n=9,m1=3,m2=3
talib.STOCH(high, low, close, fastk_period=9, slowk_period=3, slowk_matype=0, slowd_period=3, slowd_matype=0)

随机指标KDJ
RSV = 100 * (close - min(最近n日内low)/ ( max(最近n日内high)-min(最近n日内low))
K线:RSV的M1日移动平均线
D线:K值的M2日移动平均线
J线:3k - 2d
参数一般取值:n=9,m1=3,m2=3
def KDJ(high, low, close, fastk_period=9, slowk_period=3, slowk_matype=0, slowd_period=3, slowd_matype=0):
    k,d = talib.STOCH(high, low, close, fastk_period, slowk_period, slowk_matype, slowd_period, slowd_matype)
    j = []
    for i in range(len(k)):
        j.append(3*k[i] - 2*d[i])
    return k,d,j

威廉指标W%R
WR1 = 100*(N日内max(high) - 当前close)/(N日内max(high) - N日内min(low))¶
N 一般取6或者10
talib.WILLR(high, low, close, timeperiod = 6)

乖离率(BIAS)
def BIAS(close,n):
    ma = talib.MA(close,n)
    bias = []
    for i in range(len(close)):
        bias.append(100 * (close[i] - ma[i])/ma[i])
    return bias

唐奇安通道(DC)
up = 过去20日内的最高价
down = 过去20日内的最低价
mid = (up + down)/2
def DC(close,n):
    up = []
    down = []
    mid = []
    for i in range(len(close)):
        if i<n:
            up.append(max(close[:i]))
            down.append(min(close[:i]))
            mid.append(0.5*(up[i]+down[i]))
        else:
            up.append(max(close[i-n:i]))
            down.append(min(close[i-n:i]))
            mid.append(0.5*(up[i]+down[i]))
    return down,mid,up

布林极限(BB)
布林极限 = (c - down)/ (up - down)
def BB(close,n,bias_n):
    up,mid,low = talib.BBANDS(df.close_price,5,matype = talib.MA_Type.SMA)
    bb = []
    for i in range(len(close)):
        bb.append((close[i]-down[i])/(up[i] - down[i]))
    return bb

真实波幅通道  ATR_CHANNEL
TR = MAX(high - low, abs(pre_close - high), abs(pre_close - low))¶
ATR =SMA(TR)
up = open + ATR * M
down = open - ATR * M
def ATR_CHANNEL(open_price,atr,m=1):
    up = []
    down = []
    for i in range(len(open_price)):
        up.append(open_price[i] + m * atr[i])
        down.append(open_price[i] - m * atr[i])
    return up,down

二、能量型指标

人气指标AR
def AR(o,h,l,n):
    h_o = h - o
    o_l = o - l
    ar = []
    for i in range(len(o)):
        if i<n:
            ar.append(1)
        else:
            ar.append(100 * sum(h_o[i-n:i])/sum(o_l[i-n:i]))
    return ar

心理线PSY
# PSY = 100 * SUM(UP)/N
def PSY(open_price, close_price,n):
    is_up = (close_price >  open_price).astype('int')
    psy = []
    for i in range(len(open_price)):
        if i<n:
            psy.append(0)
        else:
            psy.append(100 * sum(is_up[i-n:i])/n)
    return psy


成交变异率VR
def VR(o,c,v,n):
    vr = []
    for i in range(len(o)):
        if i<n-1:
            vr.append(0)
        else:
            sv = 0
            wv = 0
            for j in range(n):
                if o[i+1-n+j]<c[i+1-n+j]:
                    sv += v[i+1-n+j]
                elif o[i-n+j]>c[i+1-n+j]:
                    wv += v[i-n+j]
                else:
                    sv += v[i+1-n+j]*0.5
                    wv+= v[i+1-n+j]*0.5
            if wv == 0:
                vr.append(1000)
            else:
                vr.append(100 * sv/wv)
    return vr
VR(open,close,volume,5)

威廉多空力度线(WAD)
结合价格来进行使用
def WAD(close,high,low):
    wad = []
    for i in range(len(close)):
        if i==0:
            wad.append(0)
            continue
        trl = min(close[i-1],low[i])
        trh = max(close[i-1],high)
        if close[i-1]<close[i]:
            ad = close[i] - trl
        elif close[i-1]> close[i]:
            ad = close[i] - trh
        else:
            ad = 0
        if i == 1:
            wad.append(ad)
        else:
            wad.append(wad[-1] + ad)
    return wad

# 幅度比(PCNT)
def PCNT(close):
    pcnt = []
    for i in range(len(close)):
        if i==0:
            pcnt.append(0)
        else:
            pcnt.append(100*(close[i]-close[i-1])/close[i])
    return pcnt

三、趋势型技术指标

平均差(DMA)
dif向上交ama
dif向下交ama
def DMA(close,n_1,n_2,m):
    dif = talib.MA(close,n_1) - talib.MA(close,n_2)
    ama = talib.MA(dif,m)
    return dif,ama

简易波动指标(EMV)
def EMV(high,low,volume,n):
    em = []
    for i in range(len(low)):
        if i == 0:
            em.append(0)
        else:
            a = (high[i] + low[i])/2
            b = (high[i-1] + low[i-1])/2
            c = high[i] - low[i]
            em.append((a-b)*c/volume[i])
    emv = []
    for i in range(len(low)):
        if i<n:
            emv.append(sum(em[:i]))
        else:
            emv.append(sum(em[i-n:i]))
    return emv
def EMVMA(emv,m):
    return talib.SMA(emv,m)

平滑异同平均(MACD)
dif = ema(close,short) - ema(close,long)
dea = ema(dif,mid)
macd = dif - dea
macd, dif, dea = talib.MACD(df.close_price, fastperiod=12, slowperiod=26, signalperiod=9)

三重平滑指标(TRIX)
TR = close的N日异同移动平均的N日异同移动平均的N日异同移动平均(三重)
TRIX = 100 * (TR - pre_TR)/pre_TR
TMA = MA(TRIX)
trix = talib.TRIX(df.close_price, timeperiod=12)
def TMA(trix,m):
    return talib.SMA(trix,m)

威廉变异离散量(WVAD)
A = C-O
B = H - L
C = (A/B)*V
WVAD = SUM(C,N)
def WVAD(o,h,l,c,v,n):
    wvad = []
    c_list = []
    for i in range(len(o)):
        a = c[i] - o[i]
        b = h[i] - l[i]
        c_list.append(a/b*v[i])
        if i < n:
            wvad.append(sum(c_list[:-1]))
        else:
            wvad.append(sum(c_list[i-n:i]))
    return wvad

加速线(JS)
def JS(close,n):
    js = []
    for i in range(len(close)):
        if i<n:
            if i==0:
                js.append(0)
            else:
                js.append(100 * (close[i]-close[0])/((i-0)*close[0]))
        else:
            js.append(100 * (close[i]-close[i-n])/(n*close[i-n]))
    return js       
def JSMA(js,m):
    return talib.MA(js,m)

四、量价指标

量价指标AD
AD = talib.AD(high, low, close, volume)

震荡指标ADOSC
ADOSC = talib.ADOSC(high, low, close, volume, fastperiod=3, slowperiod=10)

能量潮OBV
OBV = talib.OBV(close, volume)

五、均线型指标

指数平均线
talib.EMA(close, timeperiod = 5)

移动平均线MA
MA = talib.MA(close, timeperiod = 5)

高价平均线HMA
HMA = talib.MA(high, timeperiod = 5)
低价平均线LMA
LMA = talib.MA(low, timeperiod = 5)

变异平均线(VMA)
def VMA(open_price,close_price,high_price,low_price):
    return [(open_price[i]+close_price[i] + high_price[i] + low_price[i])/4 for i in range(len(open_price))]

多空指数(BBI)
def BBI(close):
    bbi = []
    ma3 = talib.MA(close,3)
    ma6 = talib.MA(close,6)
    ma12 = talib.MA(close,12)
    ma24 = talib.MA(close,24)
    return [(ma3[i] + ma6[i] + ma12[i] + ma24[i])/4 for i in range(len(close))]

多空布林线(BBIBOLL)
def BBIBOLL(bbi,n,m):
    mid = talib.MA(np.array(bbi),n)
    df = pd.DataFrame(bbi)
    up = []
    down = []
    for i in range(len(bbi)):
        if i < n-1:
            up.append(0)
            down.append(0)
        else:
            up.append(m * df.iloc[i-n+1:i].std() + mid[i])
            down.append(m * df.iloc[i-n+1:i].std() + mid[i])
    return up,mid,down

六、路径型指标

布林线(BOLL)
中线:n日收盘价均值
上轨:中线加偏移值(偏移值一般取若干倍的标准差)
下轨:中线减偏移值(偏移值一般取若干倍的标准差)
def BOLL(close,n,bias_n=2):
    mid = talib.MA(close,n)
    up = []
    down = []
    for i in range(len(close)):
        if i<n-1:
            up.append(0)
            down.append(0)
        else:
            std = np.array(close[i-n+1:i+1]).std()
            up.append(round(mid[i]+bias_n*std,6))
            down.append(round(mid[i] - bias_n*std,6))
    return up,mid,down
upper,middle,lower = talib.BBANDS(df.close_price,5,matype = talib.MA_Type.SMA)

轨道线(ENE)
def ENE(close,n,m_1,m_2):
    mid = talib.MA(close,n)
    up = [(1+m_1/100)*mid[i] for i in range(len(mid))]
    down = [(1-m_2/100)*mid[i] for i in range(len(mid))]
    return up,mid,down

逆势操作(CDP)
def CDP(close,high,low):
    cdp_list = []
    ah_list = []
    nh_list = []
    al_list = []
    nl_list = []
    for i in range(len(close)):
        cdp = (high[i]+low[i] + 2*close[i])/4
        ah = cdp + high[i] - low[i]
        nh = cdp*2 - low[i]
        al = cdp - (high[i] - low[i])
        nl = cdp*2 - high[i]
        cdp_list.append(cdp)
        ah_list.append(ah)
        nh_list.append(nh)
        al_list.append(al)
        nl_list.append(nl)
    # 从高到低降序输出
    return ah_list,nh_list,cdp_list,nl_list,al_list

七、停损型技术指标

抛物线指标SAR
SAR(n) = SAR(n-1) * AF*[EP(n-1) - SAR(n-1)]
AF:调整系数
EP:极值
sar = talib.SAR(df.high_price, df.low_price, acceleration=0, maximum=0)

八、统计学指标

β系数
talib.BETA(high, low, timeperiod = 5)

皮尔逊相关系数CORR
talib.CORREL(high, low, timeperiod = 30)

线性回归LR
talib.LINEARREG(close, timeperiod=14)

线性回归的角度LRA
talib.LINEARREG_ANGLE(close, timeperiod=14)

线性回归截距LRI
talib.LINEARREG_INTERCEPT(close, timeperiod=14)

标准偏差STD
talib.STDDEV(close, timeperiod=5, nbdev=1)

方差VAR
talib.VAR(close, timeperiod=5, nbdev=1)
上一篇:Operating System(贪心、堆)


下一篇:Postgresql 教程