def ldevi(slow, ref_quick, ref_slow, sbase=None, covered=20, distance=1, delta=0): ''' 底背离 ref_quick,ref_slow用于确认顶底 sbase是比较线,即顶底确认后的比较 distance表示与前面第几个底背离 delta:平滑系数,低点如果高delta也算新低 这个算法根据macd下叉来确认底,可能更好? ''' if sbase == None: sbase = ref_quick sc = gand(cross(ref_slow, ref_quick) > 0) xlow = tmin(slow, covered) dlow = tmin(sbase, covered) #print sc,xlow.tolist(),dlow.tolist() dxlow = rsub2(xlow, sc, distance) ddlow = rsub2(dlow, sc, distance) #print dxlow,ddlow signal = gand(dxlow - delta < 0, ddlow > 0) return signal
def uplines(*args): ''' args[0]...args[-1]各个序列多头排列,其中args[0]为最快速线 ''' trends = [trend(s) > 0 for s in args] sdiff = np.diff(args, axis=0) sdiff0 = [s < 0 for s in sdiff] strend = gand(*trends) return gand(strend, *sdiff0)
def uplines(*args): ''' args[0]...args[-1]各个序列多头排列,其中args[0]为最快速线 ''' trends = [ trend(s)>0 for s in args ] sdiff = np.diff(args,axis=0) sdiff0 = [ s<0 for s in sdiff] strend = gand(*trends) return gand(strend,*sdiff0)
def dm(shigh, slow): ''' 动向计算 通达信公式 HD :=HIGH-REF(HIGH,1); LD :=REF(LOW,1)-LOW; DMP:=EXPMEMA(IF(HD>0&&HD>LD,HD,0),N); DMM:=EXPMEMA(IF(LD>0&&LD>HD,LD,0),N); 这里取消了N的EXP ''' tpdm = subd(shigh) tndm = -subd(slow) pdm = np.select([gand(tpdm > 0, tpdm > tndm)], [tpdm], default=0) ndm = np.select([gand(tndm > 0, tndm > tpdm)], [tndm], default=0) return pdm, ndm
def dm(shigh,slow): ''' 动向计算 通达信公式 HD :=HIGH-REF(HIGH,1); LD :=REF(LOW,1)-LOW; DMP:=EXPMEMA(IF(HD>0&&HD>LD,HD,0),N); DMM:=EXPMEMA(IF(LD>0&&LD>HD,LD,0),N); 这里取消了N的EXP ''' tpdm = subd(shigh) tndm = -subd(slow) pdm = np.select([gand(tpdm>0,tpdm>tndm)],[tpdm],default=0) ndm = np.select([gand(tndm>0,tndm>tpdm)],[tndm],default=0) return pdm,ndm
def devi(shigh, sdiff, regr=96): ''' 背离,是指shigh新高头部形成时,sdiff没有创相应周期的新高 shigh:高点序列 sdiff:macd的diff序列 regr:回撤比例 ''' xhigh = tmax(shigh, 7) xhighl = xhigh * regr / 100 xhighA = gand(shigh < xhighl, rollx(shigh, 6) < xhigh) xhigh60 = tmax(shigh, 60) xdiff7 = tmax(sdiff, 7) xdiff60 = tmax(sdiff, 60) xdev = gand(xhighA, xhigh == xhigh60, xdiff7 < xdiff60) return xdev
def devi(shigh,sdiff,regr=96): ''' 背离,是指shigh新高头部形成时,sdiff没有创相应周期的新高 shigh:高点序列 sdiff:macd的diff序列 regr:回撤比例 ''' xhigh = tmax(shigh,7) xhighl = xhigh * regr/100 xhighA = gand(shigh < xhighl,rollx(shigh,6)<xhigh) xhigh60 =tmax(shigh,60) xdiff7 = tmax(sdiff,7) xdiff60 = tmax(sdiff,60) xdev = gand(xhighA,xhigh == xhigh60,xdiff7<xdiff60) return xdev
def decover1(source, interval=1): ''' 去除间隔期内!=0数值的重复出现,并将信号标准化为1 新的信号会增强interval. 去除效率大于derepeatc ''' nsource = nequals(source, 0) ms = msum2(nsource, interval + 1) #间隔0为本位和,间隔1位本左邻和 return gand(equals(ms, 1), nsource)
def decover1(source,interval=1): ''' 去除间隔期内!=0数值的重复出现,并将信号标准化为1 新的信号会增强interval. 去除效率大于derepeatc ''' nsource = nequals(source,0) ms = msum2(nsource,interval+1) #间隔0为本位和,间隔1位本左邻和 return gand(equals(ms,1),nsource)
def gtrend1(shigh, slow): ''' 江恩1周期趋势线 #返回trend线,top,bottom线 返回trend线,使用时可用strend2来判断当前趋势,当up时当前点为高点,down时为低点 如果要找到高点之前的地点,可以用strend2作为选择,找到这些点,然后extend2next 如: t1=strend2(gtrend) thigh = np.select([t1>0],[gtrend]) thigh = extend2next(thigh) 如此就取到了高点序列,同理可得到低点序列 一日摆动由江恩定义. 1日转向图. 上升日(Up down:UD): 高点高,低点高 下降日(Down day:DD): 高点低,低点低 外延日(Outside day,OD):高点高,低点低 内移日(Inside day:ID):高点低,低点高 上升日则趋势线移动到高点,下降日则趋势线移动到低点 上升趋势中的外延日,如果先高后低,则移动到低点,否则移动到高点,#这里简单起见,都算延续趋势 下降趋势中的外延日,如果先低后高,则移动到高点,否则移动到高点,#这里简单起见,都算延续趋势 忽略内移日 top:上行中的高点,或转为下行前的上一个高点 bottom: 下行中的低点,或转为上行前的上一个低点 ''' dup = gand(shigh > rollx(shigh), slow > rollx(slow)) ddown = gand(shigh < rollx(shigh), slow < rollx(slow)) dexpand = gand(shigh > rollx(shigh), slow < rollx(slow)) #dinside = gand(shigh<rollx(shigh),slow>rollx(slow)) #忽略 #先不计扩张日,得到原始趋势 st = np.select([dup, ddown], [shigh, slow], 0) st = extend2next(st) t1 = strend2(st) st2 = np.select( [dup, gand(dexpand, t1 > 0), ddown, gand(dexpand, t1 < 0)], [shigh, shigh, slow, slow]) #st_top = np.select([dup,gand(dexpand,t1>0)],[shigh,shigh]) #st_bottom = np.select([ddown,gand(dexpand,t1<0)],[slow,slow]) st2 = extend2next(st2) #st_top = extend2next(st_top) #st_bottom = extend2next(st_bottom) return st2 #,st_top,st_bottom
def lpeak(slow,ref_quick,ref_slow,covered=10): ''' 寻找最近低点 ref_quick,ref_slow(skdj/k,skdj/d)用于确认顶底 用一分钟skdj的时候,比较快捷,所以covered默认为10 ''' sc = gand(cross(ref_slow,ref_quick)>0) xlow = tmin(slow,covered) return np.select([sc],[xlow],0)
def hpeak(shigh,ref_quick,ref_slow,covered=10): ''' 寻找最近高点 ref_quick,ref_slow(skdj/k,skdj/d)用于确认顶底 用一分钟skdj的时候,比较快捷,所以covered默认为10 ''' sc = gand(cross(ref_slow,ref_quick)<0) xhigh = tmax(shigh,covered) return np.select([sc],[xhigh],0)
def hdevi(shigh,ref_quick,ref_slow,sbase=None,covered=20,distance=1,delta=0): ''' 顶背离 ref_quick,ref_slow(sdiff,ref_slow)用于确认顶底 sbase是比较线,即顶底确认后的比较 distance表示与前面第几个顶背离 delta:平滑系数,高点如果低delta也算新高 这个算法根据macd下叉来确认顶,可能更好? ''' if sbase == None: sbase = ref_quick sc = gand(cross(ref_slow,ref_quick)<0) xhigh = tmax(shigh,covered) dhigh = tmax(sbase,covered) dxhigh = rsub2(xhigh,sc,distance) ddhigh = rsub2(dhigh,sc,distance) signal = gand(dxhigh + delta>0,ddhigh<0) return signal
def lpeak(slow, ref_quick, ref_slow, covered=10): ''' 寻找最近低点 ref_quick,ref_slow(skdj/k,skdj/d)用于确认顶底 用一分钟skdj的时候,比较快捷,所以covered默认为10 ''' sc = gand(cross(ref_slow, ref_quick) > 0) xlow = tmin(slow, covered) return np.select([sc], [xlow], 0)
def hpeak(shigh, ref_quick, ref_slow, covered=10): ''' 寻找最近高点 ref_quick,ref_slow(skdj/k,skdj/d)用于确认顶底 用一分钟skdj的时候,比较快捷,所以covered默认为10 ''' sc = gand(cross(ref_slow, ref_quick) < 0) xhigh = tmax(shigh, covered) return np.select([sc], [xhigh], 0)
def gtrend1(shigh,slow): ''' 江恩1周期趋势线 #返回trend线,top,bottom线 返回trend线,使用时可用strend2来判断当前趋势,当up时当前点为高点,down时为低点 如果要找到高点之前的地点,可以用strend2作为选择,找到这些点,然后extend2next 如: t1=strend2(gtrend) thigh = np.select([t1>0],[gtrend]) thigh = extend2next(thigh) 如此就取到了高点序列,同理可得到低点序列 一日摆动由江恩定义. 1日转向图. 上升日(Up down:UD): 高点高,低点高 下降日(Down day:DD): 高点低,低点低 外延日(Outside day,OD):高点高,低点低 内移日(Inside day:ID):高点低,低点高 上升日则趋势线移动到高点,下降日则趋势线移动到低点 上升趋势中的外延日,如果先高后低,则移动到低点,否则移动到高点,#这里简单起见,都算延续趋势 下降趋势中的外延日,如果先低后高,则移动到高点,否则移动到高点,#这里简单起见,都算延续趋势 忽略内移日 top:上行中的高点,或转为下行前的上一个高点 bottom: 下行中的低点,或转为上行前的上一个低点 ''' dup = gand(shigh>rollx(shigh),slow>rollx(slow)) ddown = gand(shigh<rollx(shigh),slow<rollx(slow)) dexpand = gand(shigh>rollx(shigh),slow<rollx(slow)) #dinside = gand(shigh<rollx(shigh),slow>rollx(slow)) #忽略 #先不计扩张日,得到原始趋势 st = np.select([dup,ddown],[shigh,slow],0) st = extend2next(st) t1 = strend2(st) st2 = np.select([dup,gand(dexpand,t1>0),ddown,gand(dexpand,t1<0)],[shigh,shigh,slow,slow]) #st_top = np.select([dup,gand(dexpand,t1>0)],[shigh,shigh]) #st_bottom = np.select([ddown,gand(dexpand,t1<0)],[slow,slow]) st2 = extend2next(st2) #st_top = extend2next(st_top) #st_bottom = extend2next(st_bottom) return st2 #,st_top,st_bottom
def ldevi(slow,ref_quick,ref_slow,sbase=None,covered=20,distance=1,delta=0): ''' 底背离 ref_quick,ref_slow用于确认顶底 sbase是比较线,即顶底确认后的比较 distance表示与前面第几个底背离 delta:平滑系数,低点如果高delta也算新低 这个算法根据macd下叉来确认底,可能更好? ''' if sbase == None: sbase = ref_quick sc = gand(cross(ref_slow,ref_quick)>0) xlow = tmin(slow,covered) dlow = tmin(sbase,covered) #print sc,xlow.tolist(),dlow.tolist() dxlow = rsub2(xlow,sc,distance) ddlow = rsub2(dlow,sc,distance) #print dxlow,ddlow signal = gand(dxlow-delta<0,ddlow>0) return signal
def gsyntony(*args): ''' 简单共振的多参数版本 为保持接口的一致性,如果args[-1]为整数,则为covered参数,否则covered=1. 其余的args都是序列 args中的序列信号不论先后,在covered天中同时出现,则以匹配日(后个信号出现的那一天)为信号发出日 covered =1 为同一天发出信号,<1视同为1 要求args的元素在本序列内部都是同号的(但args之间可以不同号) 注意:args各序列非零为有信号 ''' assert len(args) >= 2 sources,covered = _iargsparse(1,*args) ss = [msum2(s,covered) for s in sources] return gand(*ss)
def gsyntony(*args): ''' 简单共振的多参数版本 为保持接口的一致性,如果args[-1]为整数,则为covered参数,否则covered=1. 其余的args都是序列 args中的序列信号不论先后,在covered天中同时出现,则以匹配日(后个信号出现的那一天)为信号发出日 covered =1 为同一天发出信号,<1视同为1 要求args的元素在本序列内部都是同号的(但args之间可以不同号) 注意:args各序列非零为有信号 ''' assert len(args) >= 2 sources, covered = _iargsparse(1, *args) ss = [msum2(s, covered) for s in sources] return gand(*ss)
def hdevi(shigh, ref_quick, ref_slow, sbase=None, covered=20, distance=1, delta=0): ''' 顶背离 ref_quick,ref_slow(sdiff,ref_slow)用于确认顶底 sbase是比较线,即顶底确认后的比较 distance表示与前面第几个顶背离 delta:平滑系数,高点如果低delta也算新高 这个算法根据macd下叉来确认顶,可能更好? ''' if sbase == None: sbase = ref_quick sc = gand(cross(ref_slow, ref_quick) < 0) xhigh = tmax(shigh, covered) dhigh = tmax(sbase, covered) dxhigh = rsub2(xhigh, sc, distance) ddhigh = rsub2(dhigh, sc, distance) signal = gand(dxhigh + delta > 0, ddhigh < 0) return signal
pre_v = source[0] cur = 0 for i in xrange(1,len(source)): cur_v = source[i] if cur_v > pre_v: cur = cur + 1 if cur > 0 else 1 elif cur_v < pre_v: cur = cur - 1 if cur < 0 else -1 else: #curv == pre_v cur = cur + 1 if cur >= 0 else cur-1 #最初为0时,也算上升 rev[i] = cur pre_v = cur_v return rev #趋势的翻转次数 rturn = lambda sx:sum(gor(gand(rollx(sx)>0,sx<0),gand(rollx(sx)<0,sx>0))) def cross(target,follow): ''' 交叉计算: target: 参照系,follow: 追击者 状态: 1 Follow上叉Target 0 无交叉状态 -1 Follow下叉 粘合一次后按趋势发散仍然算叉,即追击--追平--超越也算,但追击--追平--平--....--超越不算 这里不保证Target在上叉(下叉)时的趋势是向上(向下)的 ''' assert len(target) == len(follow) if(len(target) == 0): return target.copy() s = np.sign(follow - target)
for i in xrange(1, len(source)): cur_v = source[i] if cur_v > pre_v: cur = cur + 1 if cur > 0 else 1 elif cur_v < pre_v: cur = cur - 1 if cur < 0 else -1 else: #curv == pre_v cur = cur + 1 if cur >= 0 else cur - 1 #最初为0时,也算上升 rev[i] = cur pre_v = cur_v return rev #趋势的翻转次数 rturn = lambda sx: sum( gor(gand(rollx(sx) > 0, sx < 0), gand(rollx(sx) < 0, sx > 0))) def cross(target, follow): ''' 交叉计算: target: 参照系,follow: 追击者 状态: 1 Follow上叉Target 0 无交叉状态 -1 Follow下叉 粘合一次后按趋势发散仍然算叉,即追击--追平--超越也算,但追击--追平--平--....--超越不算 这里不保证Target在上叉(下叉)时的趋势是向上(向下)的 ''' assert len(target) == len(follow) if (len(target) == 0): return target.copy() s = np.sign(follow - target)