曾氏通道的python实现
1、概述
网上关于曾氏通道的描述并不多,似乎应用也很少,还没有深入研究,只是转成了python形式
2、公式
excel格式的:https://share.weiyun.com/fciZSeNL
3、python实现,基于极星量化
import talib from scipy import stats import math import numpy from scipy.stats import norm XseqG = [] YlogG = [] Regression_Line_yG = [] DiffG = [] per95G = [] per75G = [] fper75G = [] fper95G = [] hisSize = '20220105' backSize = 1000 # 策略开始运行时执行该函数一次 def initialize(context): SetBarInterval('HKEX|F|HSI|2202','M',10,hisSize,backSize) SetTriggerType(5) SetOrderWay(2) pass # 策略触发事件每次触发时都会执行该函数 def handle_data(context): global XseqG,YlogG,Regression_Line_yG,DiffG,per95G,per75G,fper75G,fper95G if len(Close())<1: return #根据close的数量,每次都重新生成Xseq count = 0 XseqG = [] for c in Close(): count = count + 1 XseqG.append(count) #根据Close的最新值,重新计算Ylog YlogG = [] for c in Close(): #Ylog = LN(Close) Ylog = math.log(c,math.e) YlogG.append(Ylog) #Slope = SLOPE(Ylog,Close) slope1, intercept1, r_value1, p_value1, std_err1 = stats.linregress(Close(),YlogG) Slope = slope1 #y-intercept = INTERCEPT(Ylog,Xseq) slope2, intercept2, r_value2, p_value2, std_err2 = stats.linregress(XseqG, YlogG) y_intercept = intercept2 #Regression_Line_y = Slope*Xseq + y_intercept 注意每次都需要根据最新的Slope和y_intercept做重新计算 Regression_Line_yG = [] for Xseq in XseqG: Regression_Line_y = Slope*Xseq + y_intercept Regression_Line_yG.append(Regression_Line_y) #Diff = Regression_Line_y - Ylog 注意每次都需要根据最新的Regression_Line_y做重新计算 DiffG = [] for i in range(0,len(Regression_Line_yG)): Diff = Regression_Line_yG[i] - YlogG[i] DiffG.append(Diff) #SD = STDEV(Diff) SD = numpy.std(DiffG,ddof=1) #95% = NORMINV(0.95,Regression_Line_y,SD) 注意每次根据最新的Regression_Line_yG重新计算 per95G = [] for Regression_Line_y in Regression_Line_yG: per95 = norm.ppf(0.95,Regression_Line_y, SD) per95G.append(per95) #75% = NORMINV(0.75,Regression_Line_y,SD) per75G = [] for Regression_Line_y in Regression_Line_yG: per75 = norm.ppf(0.75,Regression_Line_y, SD) per75G.append(per75) #-75% = NORMINV(0.25,Regression_Line_y,SD) fper75G = [] for Regression_Line_y in Regression_Line_yG: fper75 = norm.ppf(0.25,Regression_Line_y, SD) fper75G.append(fper75) #-95% = NORMINV(0.05,Regression_Line_y,SD) fper95G = [] for Regression_Line_y in Regression_Line_yG: fper95 = norm.ppf(0.05,Regression_Line_y, SD) fper95G.append(fper95) PlotNumeric('Ylog',YlogG[-1],RGB_Red(),False) PlotNumeric('Regression_Line_y',Regression_Line_yG[-1],RGB_Blue(),False) PlotNumeric('per95',per95G[-1],RGB_Brown(),False) PlotNumeric('per75',per75G[-1],RGB_Green(),False) PlotNumeric('fper75',fper75G[-1],RGB_Purple(),False) PlotNumeric('fper95',fper95G[-1],RGB_Yellow(),False) #回溯一遍,重新画 for X in XseqG: i = X*-1 barsBack = X-1 UnPlotNumeric('Regression_Line_y',False,barsBack) PlotNumeric('Regression_Line_y',Regression_Line_yG[i],RGB_Blue(),False,False,barsBack) UnPlotNumeric('per95',False,barsBack) PlotNumeric('per95',per95G[i],RGB_Brown(),False,False,barsBack) UnPlotNumeric('per75',False,barsBack) PlotNumeric('per75',per75G[i],RGB_Green(),False,False,barsBack) UnPlotNumeric('fper75',False,barsBack) PlotNumeric('fper75',fper75G[i],RGB_Purple(),False,False,barsBack) UnPlotNumeric('fper95',False,barsBack) PlotNumeric('fper95',fper95G[i],RGB_Yellow(),False,False,barsBack) pass # 历史回测阶段结束时执行该函数一次 def hisover_callback(context): pass # 策略退出前执行该函数一次 def exit_callback(context): pass
4、运行结果
5、要点
需要注意,excel计算时是静态数据,python计算是动态数据,为了与excel结果保持一致,每当有close更新时都重新将所有数据计算一遍,否则无法保持一致。
依赖scipy这个包,需要安装,网络不好还装不上。
量化交流群:1064240775