def cpeaks(cdf,sidx,linecords): cpeak_out = [] maxtab, mintab = peakdet(cdf[-sidx:],1) cxrange = np.arange(len(cdf) - sidx,len(cdf)) lst_regression = [] for cor in linecords: regression = basic_linear_regression(cor[0],cor[1]) lst_regression.append(regression) if maxtab.any() or mintab.any(): for m in enumerate(np.vstack((maxtab,mintab))): m[1][0] = cxrange[m[1][0]] for i,r in enumerate(lst_regression): #new_y = r[1][0] * m[1][0] + r[1][1] new_y = r[0] * m[1][0] + r[1] #print (new_y - (new_y * 0.001)), m[1][1],(new_y + (new_y * #0.001)) if (new_y - (new_y * 0.001)) <= m[1][1] <= (new_y + (new_y * 0.001)): plt.scatter(m[1][0], m[1][1], color = 'green') cpeak_out.append([m[1],linecords[i]]) #print cpeak_out return cpeak_out
def run(self): deltastop = 55 linecords = [] touch_down = [] percent_return = 0 for d in self.delta: maxtab, mintab = peakdet(self.df['p'],d) if len(maxtab) > 2 and len(mintab) > 2 : #print 'delta stop -- {0}'.format(d) deltastop = d break bothpeak = [] if len(maxtab) > 0 and len(mintab) > 0 : if maxtab[0][1] > mintab[0][1]: bothpeak = self.array_series(maxtab,mintab,-3) else: bothpeak = self.array_series(mintab,maxtab,-3) if len(bothpeak) == 3: rtn = self.gen_afp(bothpeak) if rtn == 0: rtn = self.gen_afp(bothpeak,Schiff = True) if rtn <> 0: self.is_Schiff = True return rtn return 0
def apf_draw(df,symbol): pmin = df.p.min() pmax = df.p.max() formatter = MyFormatter(df.index) touch_down = [] percent_return = 0 fig, ax = plt.subplots() ax.xaxis.set_major_formatter(formatter) ax.plot(np.arange(len(df)), df['p']) #detect peak # 1 1 2 3 5 8 13 21 34 maxtab = [] mintab = [] delta = [55,34,21,13,8,5,3,2,1] #delta = [5,3,2,1] deltastop = 55 for d in delta: maxtab, mintab = peakdet(df['p'],d) if len(maxtab) > 1 and len(mintab) > 1 : print 'delta stop -- {0}'.format(d) deltastop = d break #draw APF if maxtab.any() and mintab.any(): #plot peak plt.scatter(array(maxtab)[:,0], array(maxtab)[:,1], color = 'black') plt.scatter(array(mintab)[:,0], array(mintab)[:,1], color='red') sets = -3 if maxtab[0][1] > mintab[0][1]: bothpeak = array_series(maxtab,mintab,sets) else: bothpeak = array_series(mintab,maxtab,sets) for i,apoint in enumerate(bothpeak): try: if (i + 2) < len(bothpeak): linecords = [] bpoint = bothpeak[i + 1] cpoint = bothpeak[i + 2] mnmidx = (cpoint[0] - bpoint[0]) / 2 mnmidy = (cpoint[1] - bpoint[1]) / 2 percent_return = 100 * ((bpoint[1] - cpoint[1]) / 2) / apoint[1] plt.plot([bpoint[0],cpoint[0]],[bpoint[1],cpoint[1]],color='yellow') mnpoint = [mnmidx + bpoint[0],mnmidy + bpoint[1]] plt.plot(mnpoint[0],mnpoint[1],'om') slope, intercept = np.polyfit([apoint[0],mnpoint[0]],[apoint[1],mnpoint[1]],1) prdictY = slope * apoint[1] + intercept plt.plot(apoint[1],prdictY,'om') print slope, intercept mnline = [[apoint[1],apoint[0]],[prdictY,apoint[1]]] plt.plot(mnline[0],mnline[1],color='orange') linecords.append(mnline) plt.plot(apoint[1] - mnmidx,prdictY - mnmidy,'om') bmnline = [[bpoint[0],apoint[1] - mnmidx],[bpoint[1],prdictY - mnmidy]] plt.plot(bmnline[0],bmnline[1],color='blue') linecords.append(bmnline) plt.plot(apoint[1] + mnmidx,prdictY + mnmidy,'om') cmnline = [[cpoint[0],apoint[1] + mnmidx],[cpoint[1],prdictY + mnmidy]] plt.plot(cmnline[0],cmnline[1],color='purple') linecords.append(cmnline) mn2midx = (mnpoint[0] - bpoint[0]) / 2 mn2midy = (mnpoint[1] - bpoint[1]) / 2 plt.plot(mn2midx + bpoint[0],mn2midy + bpoint[1],'om') plt.plot(mn2midx + mnpoint[0],mn2midy + mnpoint[1],'om') mnbline = [[mn2midx + bpoint[0],apoint[1] - mn2midx],[mn2midy + bpoint[1],prdictY - mn2midy]] plt.plot(mnbline[0],mnbline[1],'--',color='black') linecords.append(mnbline) mncline = [[mn2midx + mnpoint[0],apoint[1] + mn2midx],[mn2midy + mnpoint[1],prdictY + mn2midy]] plt.plot(mncline[0],mncline[1],'--',color='black') linecords.append(mncline) plt.xlabel('slope:{0} intercept:{1} delta:{2} '.format(slope,intercept,deltastop)) #print cpoint #touch_down = cpeaks(df['p'],int(len(df['p']) - #cpoint[0]),linecords) touch_down = cpeaks2(df['p'],int(len(df['p']) - cpoint[0]),linecords) #print 'touch down',touch_down #break except Exception, e: print e
def apf_draw(fn): # fn = ".\\data\\lastweek\\HHM_20141219.csv" # line_prepender(fn,"d,t,p,na") df = pd.read_csv(fn, parse_dates={"Timestamp": ["d", "t"]}, index_col="Timestamp") df = df.ix[1:, ["p"]] # resample to 15 min df = df.resample("20min", how="first") # drop na df = df.dropna() # price min, max pmin = df.p.min() pmax = df.p.max() formatter = MyFormatter(df.index) fig, ax = plt.subplots() ax.xaxis.set_major_formatter(formatter) ax.plot(np.arange(len(df)), df.p) # detect peak # 1 1 2 3 5 8 13 21 34 maxtab = [] mintab = [] delta = [34, 21, 13, 8, 5, 3, 2, 1] for d in delta: maxtab, mintab = peakdet(df["p"], d) if len(maxtab) > 1 and len(mintab) > 1: print "delta stop -- {0}".format(d) break else: print "min delta -- {0}".format(d) # draw APF if maxtab.any() and mintab.any(): # plot peak plt.scatter(array(maxtab)[:, 0], array(maxtab)[:, 1], color="black") plt.scatter(array(mintab)[:, 0], array(mintab)[:, 1], color="red") sets = -3 if maxtab[0][1] > mintab[0][1]: bothpeak = array_series(maxtab, mintab, sets) else: bothpeak = array_series(mintab, maxtab, sets) for i, apoint in enumerate(bothpeak): try: if (i + 2) < len(bothpeak): bpoint = bothpeak[i + 1] cpoint = bothpeak[i + 2] mnmidx = (cpoint[0] - bpoint[0]) / 2 mnmidy = (cpoint[1] - bpoint[1]) / 2 plt.plot([bpoint[0], cpoint[0]], [bpoint[1], cpoint[1]], color="yellow") mnpoint = [mnmidx + bpoint[0], mnmidy + bpoint[1]] mnline = [[apoint[0], mnpoint[0]], [apoint[1], mnpoint[1]]] plt.plot(mnline[0], mnline[1], color="orange") slope, intercept = np.polyfit(mnline[0], mnline[1], 1) prdictY = slope * apoint[1] + intercept plt.plot(apoint[1], prdictY, "om") mnline = [[mnpoint[1], mnpoint[0]], [prdictY, mnpoint[1]]] plt.plot(mnline[0], mnline[1], color="orange") bmnline = [[bpoint[0], mnpoint[1] - mnmidx], [bpoint[1], prdictY - mnmidy]] plt.plot(bmnline[0], bmnline[1], color="blue") cmnline = [[cpoint[0], mnpoint[1] + mnmidx], [cpoint[1], prdictY + mnmidy]] plt.plot(cmnline[0], cmnline[1], color="purple") mn2midx = (mnpoint[0] - bpoint[0]) / 2 mn2midy = (mnpoint[1] - bpoint[1]) / 2 plt.plot(mn2midx + bpoint[0], mn2midy + bpoint[1], "om") plt.plot(mn2midx + mnpoint[0], mn2midy + mnpoint[1], "om") mnbline = [[mn2midx + bpoint[0], mnpoint[1] - mn2midx], [mn2midy + bpoint[1], prdictY - mn2midy]] plt.plot(mnbline[0], mnbline[1], "--", color="black") mncline = [[mn2midx + mnpoint[0], mnpoint[1] + mn2midx], [mn2midy + mnpoint[1], prdictY + mn2midy]] plt.plot(mncline[0], mncline[1], "--", color="black") break except Exception, e: print e