1. 参数说明

r:K线数据,字典或者数组
days:指标长度
name:使用哪一个字段,填’Close’即可,如果不填则代表r是数组而不是字典

变量r 字典结构图如下:

{{'Time': 0,'Close': 0,'Open': 0,'High': 0,'Low': 0,'Volume': 0,}
}

2. ATR 真实波动幅度 (需配合下面的指标)

def ATR(r, days, ma='sma'):tr = [0]for i in range(len(r)):if i:tr.append(max(r[i]['High']-r[i]['Low'], abs(r[i]['High'] - r[i-1]['Close']), abs(r[i]['Low'] - r[i-1]['Close'])))if ma == 'rma':return RMA(tr, days)if ma == 'ema':return EMA(tr, days)return SMA(tr, days)

3. MA 移动平均线

def SMA(r, days, name=0):cps = [ v[name] for v in r ] if name else remas = cps.copy()  # 创造一个和cps一样大小的集合for i in range(len(cps)):if i < days-1:emas[i] = 0else:ma = 0for i2 in range(i-days,i):i2 += 1ma += cps[i2]emas[i] = ma / daysreturn emas

4. EMA 指数移动平均线

def EMA(r, days, name=0):if name == 'o+h+l+c':cps = [ (v['Open'] + v['High'] + v['Low'] + v['Close']) / 4 for v in r ]elif name == 'h+l+c':cps = [ (v['High'] + v['Low'] + v['Close']) / 3 for v in r ]elif name == 'h+l':cps = [ (v['High'] + v['Low']) / 2 for v in r ]else:cps = [ v[name] for v in r ] if name else remas = [0 for i in range(len(cps))]  # 创造一个和cps一样大小的集合for i in range(len(cps)):if i == 0:emas[i] = cps[i]if i > 0:emas[i] = ((days - 1) * emas[i - 1] + 2 * cps[i]) / (days + 1)emas = [v for v in emas]return emas

5. SMMA 平滑移动平均线

def SMMA(r, days, name=0):cps = [ v[name] for v in r ] if name else remas = [0 for i in range(len(cps))]  # 创造一个和cps一样大小的集合ma = 0for i in range(len(cps)):if i < days:ma += cps[i]emas[i] = 0else:if emas[i-1] == 0:emas[i] = ma / dayselse:emas[i] = (emas[i-1] * (days - 1) + cps[i]) / daysemas = [v for v in emas]return emas

6. RMA,RSI中使用的移动平均线,它是指数加权移动平均线

def RMA(r, days, name=0):cps = [ v[name] for v in r ] if name else rrmas = [0 for i in range(len(cps))]  # 创造一个和cps一样大小的集合alpha = 1 / daysfor i in range(len(cps)):if i < days-1:rmas[i] = 0else:if rmas[i-1]:rmas[i] = alpha * cps[i] + (1 - alpha) * rmas[i-1]else:ma = 0for i2 in range(i-days,i):  #求平均值ma += cps[i2+1]rmas[i] = ma / daysreturn rmas

7. TEMA 三重指数平滑移动平均线

def TEMA(r, days, name='Close'):v2 = EMA(r, days, name)v3 = EMA(v2, days)v4 = EMA(v3, days)emas = []for i in range(len(v2)):ok = 3 * (v2[i] - v3[i]) + v4[i]emas.append(ok)return emas

转自博客:【原生代码】Python3 实现ATR、MA、EMA、SMMA、RMA、TEMA指标的计算 – 苏慕白的博客