def score(sprice, svolume): ''' 对当日进行评分 按照价格变化和成交量变化打分 价格上升 量上升 2分 否则 1分 价格下降 量上升 -2分 否则 -1分 价格平 0分 ''' fprice = np.sign(subd(sprice)) fvolume = np.choose(subd(svolume) > 0, [1, 2]) return fprice * fvolume
def score(sprice,svolume): ''' 对当日进行评分 按照价格变化和成交量变化打分 价格上升 量上升 2分 否则 1分 价格下降 量上升 -2分 否则 -1分 价格平 0分 ''' fprice = np.sign(subd(sprice)) fvolume = np.choose(subd(svolume) > 0,[1,2]) return fprice * fvolume
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 semv(shigh,slow,sweight,length=13):#标准化后的emv算法,length为均线长度,sweight即为成交量(权重) ''' 标准化后的波动难易度 mid = (max + min)/2 mid_rate = (mid(n) - mid(n-1)) / mid(n) * BASE svolume_rate = svolume(n)/MA(svolume,length) * BASE wave_rate = (max-min)/ma(max-min,length) * BASE box_rate = svolume_rate / wave_rate semv = mid_rate / box_rate ''' assert len(shigh) == len(sweight) == len(sweight) if(len(shigh)<length): return np.zeros_like(shigh) wma = np.roll(ma(sweight,length),1) dma = np.roll(ma(shigh - slow,length),1) mid = (shigh + slow)/2 mid_diff = subd(mid,1) mid_rate = np.cast['int64'](mid_diff) * BASE * BASE / mid swb = np.cast['int64'](sweight) * BASE swr = np.where(wma,swb/wma,swb*10) ssb = (shigh-slow)*BASE wr = np.where(dma,ssb/dma,ssb*10) #dma必然大于等于0,等于0时相当于乘10 rev = np.where(swr>0,mid_rate * wr/swr,mid_rate*wr*10) rev[:length] = 0 return np.cast['int32'](rev)
def prepare_common_old(sdata,ref): for s in sdata: #print s.code s.ref = ref c = s.transaction[CLOSE] v = s.transaction[VOLUME] s.ma10 = ma(c,10) s.ma20 = ma(c,20) s.ma60 = ma(c,60) s.ma120 = ma(c,120) s.t120 = strend(s.ma120) > 0 s.t60 = strend(s.ma60) > 0 s.t20 = strend(s.ma20) > 0 s.above = gand(s.ma10>=s.ma20,s.ma20>=s.ma60,s.ma60>=s.ma120) #将golden和above分开 s.golden = gand(s.g20 >= s.g60+1000,s.g60 >= s.g120+1000,s.g20>=3000,s.g20<=8000) s.thumb = gand(s.g20 >= s.g60,s.g60 >= s.g120,s.g120 >= s.g250,s.g20>=3000,s.g20<=8000) s.magic = gand(s.g5>s.g60,s.g20 >= s.g60,s.g60 >= s.g120,s.g120 >= s.g250,s.g20<8000) s.svap_ma_67 = svap_ma(v,c,67) s.svap_ma_67_2 = svap_ma(v,c,67,weight=2) #s.vap_ma_67 = vap_pre(v,c,67) s.ks = subd(c) * BASE / rollx(c) try: #计算 s.silver = catalog_signal_cs(s.c60,csilver) except: s.silver = cached_zeros(len(c))
def semv(shigh, slow, sweight, length=13): #标准化后的emv算法,length为均线长度,sweight即为成交量(权重) ''' 标准化后的波动难易度 mid = (max + min)/2 mid_rate = (mid(n) - mid(n-1)) / mid(n) * BASE svolume_rate = svolume(n)/MA(svolume,length) * BASE wave_rate = (max-min)/ma(max-min,length) * BASE box_rate = svolume_rate / wave_rate semv = mid_rate / box_rate ''' assert len(shigh) == len(sweight) == len(sweight) if (len(shigh) < length): return np.zeros_like(shigh) wma = np.roll(ma(sweight, length), 1) dma = np.roll(ma(shigh - slow, length), 1) mid = (shigh + slow) / 2 mid_diff = subd(mid, 1) mid_rate = np.cast['int64'](mid_diff) * BASE * BASE / mid swb = np.cast['int64'](sweight) * BASE swr = np.where(wma, swb / wma, swb * 10) ssb = (shigh - slow) * BASE wr = np.where(dma, ssb / dma, ssb * 10) #dma必然大于等于0,等于0时相当于乘10 rev = np.where(swr > 0, mid_rate * wr / swr, mid_rate * wr * 10) rev[:length] = 0 return np.cast['int32'](rev)
def efficient_rate(source, covered=10): ''' 效率函数. 来源: smarter trading (略有不同,书中效率值为abs值,而此函数以负表示下降效率) 根据单一source计算(存在其他方式的效率计算方法,但在此如此计算) 先计算n日内每日波动幅度amplitude = abs(source[i]-source[i-1]),然后根据n求和为s_amplitude 然后计算n日总波幅t_amplitude = source[i]-source[i-n] 当日效率系数 = t_amplitude / s_amplitude 第covered+1个数据(下标为covered)开始有效,之前的全部置0 效率值有正有负,为负表明是下降效率 ''' assert covered > 0 if (len(source) <= covered): return np.zeros_like(source) sdiff = np.abs(subd(source)) ssum = msum(sdiff, covered) sdiffc = subd(source, covered) rev = sdiffc * BASE / ssum rev[:covered] = 0 return rev
def efficient_rate(source,covered=10): ''' 效率函数. 来源: smarter trading (略有不同,书中效率值为abs值,而此函数以负表示下降效率) 根据单一source计算(存在其他方式的效率计算方法,但在此如此计算) 先计算n日内每日波动幅度amplitude = abs(source[i]-source[i-1]),然后根据n求和为s_amplitude 然后计算n日总波幅t_amplitude = source[i]-source[i-n] 当日效率系数 = t_amplitude / s_amplitude 第covered+1个数据(下标为covered)开始有效,之前的全部置0 效率值有正有负,为负表明是下降效率 ''' assert covered > 0 if(len(source) <= covered): return np.zeros_like(source) sdiff = np.abs(subd(source)) ssum = msum(sdiff,covered) sdiffc = subd(source,covered) rev = sdiffc * BASE / ssum rev[:covered] = 0 return rev
def emv(shigh,slow,sweight):#经典emv算法,sweight即为成交量(权重) assert len(shigh) == len(sweight) == len(sweight) if(len(shigh)<1): return np.array([]) mid_diff = subd((shigh + slow) / 2,1) box = shigh - slow swb = np.cast['int64'](sweight) * BASE md_b2 = np.cast['int64'](mid_diff) * BASE * BASE ratio = np.where(box>0,swb/box,swb*10) #box不可能小于0 rev = np.where(ratio>0,md_b2/ratio,md_b2*10) rev[0] = 0 return np.cast['int32'](rev)
def emv(shigh, slow, sweight): #经典emv算法,sweight即为成交量(权重) assert len(shigh) == len(sweight) == len(sweight) if (len(shigh) < 1): return np.array([]) mid_diff = subd((shigh + slow) / 2, 1) box = shigh - slow swb = np.cast['int64'](sweight) * BASE md_b2 = np.cast['int64'](mid_diff) * BASE * BASE ratio = np.where(box > 0, swb / box, swb * 10) #box不可能小于0 rev = np.where(ratio > 0, md_b2 / ratio, md_b2 * 10) rev[0] = 0 return np.cast['int32'](rev)
def asi(sopen, sclose, shigh, slow): ''' 1.A=∣当天最高价-前一天收盘价∣ B=∣当天最低价-前一天收盘价∣ C=∣当天最高价-前一天最低价∣ D=∣前一天收盘价-前一天开盘价∣ 2.比较A、B、C三数值:若A最大,R=A+1/2B+1/4D;若B最大,R=B+1/2A十1/4D;若C最大,R=C+1/4D 3. E=当天收盘价-前一天收盘价 F=当天收盘价-当天开盘价 G=前一天收盘价-前一天开盘价 4.X=E+1/2F+G 5.K=A、B之间的最大值 6.L=3;SI=50*X/R*K/L;ASI=累计每日之SI值 1.ASI指标大部分时机都是和股价走势同步的,投资者仅能从众多股票中寻找少数产生领先突破的个股。若ASI指标领先股价,提早突破前次ASI高点或低点,则次一日之后的股价必然能突破前次高点或低点。 2.股价由上往下,欲穿越前一波低点的密集支撑区时,于接近低点处,尚未确定股价是否会跌破支撑之际,如果ASI领先股价,提早一步,跌破相对股价的前一波ASI低点,则次一日之后,股价将随后跌破低点支撑区。投资人可以早一步卖出股票,减少不必要的损失。 3.股价由下往上,欲穿越前一波的高点套牢区时,于接近高点处,尚未确定股价能否顺利穿越之际,如果ASI领先股价,提早一步,通过相对股价的前一波ASI低点,则次一日之后,股价必然能够顺利突破高点套牢区。股民可以把握ASI的领先作用,提前买入股票。 4.股价走势一波比一波高,而ASI却未相对创新高点形成“顶背离”时,应卖出;股价走势一波比一波低,而ASI却未相对创新低点形成“底背离”时,应买进。 5.ASI指标和OBV指标同样维持“N”字型的波动,并且也以突破或跌破“N”字型高低点,为观察ASI指标的主要方法。向上爬升的ASI,一旦向下跌破其前一次显著的N型转折点,一律可视为停损卖出的讯号;向下滑落的ASI,一旦向上突破其前一次的N型转折点,一律可视为果断买进的讯号。 #这些使用很难度量,所以其实asi只能在肉眼使用 ''' a = np.abs(shigh - roll0(sclose)) b = np.abs(slow - roll0(sclose)) c = np.abs(shigh - roll0(slow)) e = subd(sclose) f = sclose - sopen g = roll0(f) d = np.abs(g) l = 3 x = e + f / 2 + g k = np.choose(a > b, [b, a]) #True=1,False=0,因此a>b时True=1 m = np.select([(a > b) & (b > c), (b > c) & (b > a), (c > a) & (c > b)], [a, b, c]) r = np.select([m == a, m == b, m == c], [a + b / 2 + d / 4, b + a / 2 + d / 4, c + d / 4]) si = 50 * x / r * k / l return si.cumsum()
def asi(sopen,sclose,shigh,slow): ''' 1.A=∣当天最高价-前一天收盘价∣ B=∣当天最低价-前一天收盘价∣ C=∣当天最高价-前一天最低价∣ D=∣前一天收盘价-前一天开盘价∣ 2.比较A、B、C三数值:若A最大,R=A+1/2B+1/4D;若B最大,R=B+1/2A十1/4D;若C最大,R=C+1/4D 3. E=当天收盘价-前一天收盘价 F=当天收盘价-当天开盘价 G=前一天收盘价-前一天开盘价 4.X=E+1/2F+G 5.K=A、B之间的最大值 6.L=3;SI=50*X/R*K/L;ASI=累计每日之SI值 1.ASI指标大部分时机都是和股价走势同步的,投资者仅能从众多股票中寻找少数产生领先突破的个股。若ASI指标领先股价,提早突破前次ASI高点或低点,则次一日之后的股价必然能突破前次高点或低点。 2.股价由上往下,欲穿越前一波低点的密集支撑区时,于接近低点处,尚未确定股价是否会跌破支撑之际,如果ASI领先股价,提早一步,跌破相对股价的前一波ASI低点,则次一日之后,股价将随后跌破低点支撑区。投资人可以早一步卖出股票,减少不必要的损失。 3.股价由下往上,欲穿越前一波的高点套牢区时,于接近高点处,尚未确定股价能否顺利穿越之际,如果ASI领先股价,提早一步,通过相对股价的前一波ASI低点,则次一日之后,股价必然能够顺利突破高点套牢区。股民可以把握ASI的领先作用,提前买入股票。 4.股价走势一波比一波高,而ASI却未相对创新高点形成“顶背离”时,应卖出;股价走势一波比一波低,而ASI却未相对创新低点形成“底背离”时,应买进。 5.ASI指标和OBV指标同样维持“N”字型的波动,并且也以突破或跌破“N”字型高低点,为观察ASI指标的主要方法。向上爬升的ASI,一旦向下跌破其前一次显著的N型转折点,一律可视为停损卖出的讯号;向下滑落的ASI,一旦向上突破其前一次的N型转折点,一律可视为果断买进的讯号。 #这些使用很难度量,所以其实asi只能在肉眼使用 ''' a = np.abs(shigh - roll0(sclose)) b = np.abs(slow - roll0(sclose)) c = np.abs(shigh - roll0(slow)) e = subd(sclose) f = sclose-sopen g = roll0(f) d = np.abs(g) l = 3 x = e + f/2 + g k = np.choose(a>b,[b,a]) #True=1,False=0,因此a>b时True=1 m = np.select([(a>b) & (b>c),(b>c) & (b>a),(c>a) & (c>b)],[a,b,c]) r = np.select([m==a,m==b,m==c],[a+b/2+d/4,b+a/2+d/4,c+d/4]) si = 50 * x / r * k / l return si.cumsum()
def prepare_common_common(s): o = s.transaction[OPEN] c = s.transaction[CLOSE] v = s.transaction[VOLUME] s.open = s.transaction[OPEN] s.close = s.transaction[CLOSE] s.vol = s.transaction[VOLUME] s.high = s.transaction[HIGH] s.low = s.transaction[LOW] s.ma0 = ma(c,3) s.ma1= ma(c,7) s.ma2 = ma(c,13) s.ma3 = ma(c,30) s.ma4 = ma(c,60) s.ma5 = ma(c,120) s.t5 = strend(s.ma5) > 0 s.t4 = strend(s.ma4) > 0 s.t3 = strend(s.ma3) > 0 s.t2 = strend(s.ma2) > 0 s.t1 = strend(s.ma1) > 0 s.t0 = strend(s.ma0) > 0 s.ksize = np.array(list(d1k.ksize(o,c))) s.ksign = np.array(list(d1k.ksign(o,c))) s.above = gand(s.ma2>s.ma3,s.ma3>s.ma4,s.ma4>s.ma5) #将golden和above分开 s.golden = gand(s.g20 >= s.g60+1000,s.g60 >= s.g120+1000,s.g20>=3000,s.g20<=8000) s.thumb = gand(s.g20 >= s.g60,s.g60 >= s.g120,s.g120 >= s.g250,s.g20>=3000,s.g20<=8000) s.magic = gand(s.g5>s.g60,s.g20 >= s.g60,s.g60 >= s.g120,s.g120 >= s.g250,s.g20<8000) s.svap_ma_67 = svap_ma(v,c,67) #s.vap_ma_67 = vap_pre(v,c,67) #s.svap_ma_67_1 = svap_ma(v,c,67,weight=1) s.svap_ma_67_2 = svap_ma(v,c,67,weight=2) s.ks = subd(c) * BASE / rollx(c) s.diff,s.dea = cmacd(c) s.atr = atr(c,s.transaction[HIGH],s.transaction[LOW],20) s.atr2 = atr2(c,s.transaction[HIGH],s.transaction[LOW],20)
def derepeatc(source): ''' 去除!=0数值的连续出现(只剩下第一个),正规化为1 c是consecutive的意思 ''' t = subd(nequals(source,0)) return equals(t,1)
def trend(source,interval=1): assert interval >= 0 return np.sign(subd(source,interval))
def score2(sprice, svolume): si = subd(sprice) * BASE / rollx(sprice) fprice = np.select([si > 5, si < -5], [1, -1], default=0) fvolume = np.choose(subd(svolume) > 0, [1, 2]) return fprice * fvolume
def subd2(source, distance=1): #偏移减法,distance必须大于0,返回结果中前distance个元素为0 if source.ndim == 1: return d1.subd(source) rs = nsubd2(source, distance) rs[:, :distance] = 0 return rs
def score2(sprice,svolume): si = subd(sprice) * BASE / rollx(sprice) fprice = np.select([si>5,si<-5],[1,-1],default=0) fvolume = np.choose(subd(svolume) > 0,[1,2]) return fprice * fvolume
def trend(source, interval=1): assert interval >= 0 return np.sign(subd(source, interval))
def derepeatc(source): ''' 去除!=0数值的连续出现(只剩下第一个),正规化为1 c是consecutive的意思 ''' t = subd(nequals(source, 0)) return equals(t, 1)
def subd2(source,distance=1): #偏移减法,distance必须大于0,返回结果中前distance个元素为0 if source.ndim == 1: return d1.subd(source) rs = nsubd2(source,distance) rs[:,:distance] = 0 return rs