def run_predict1(self, imf_list, current_price_index, date, datafile, residual, imf_open, imf_macd): for i in range(len(imf_list)): imfp = imf_list[i] data = np.array(imfp) max_index = list(argrelextrema(data, np.greater)[0]) min_index = list(argrelextrema(data, np.less)[0]) print "imf %s" % i print "max %s" % max_index print "min %s" % min_index imf2_max_index = list( argrelextrema(np.array(imf_list[2]), np.greater)[0]) print " imf max index %s" % imf2_max_index imf2_min_index = list(argrelextrema(np.array(imf_list[2]), np.less)[0]) print " imf min index %s" % imf2_min_index imf2_or_flag = 0 if imf2_min_index[-1] > imf2_max_index[-1] and imf2_min_index[ -1] > 997 and imf2_min_index[-1] - imf2_max_index[-1] > 3: imf2_or_flag = 1 if imf2_min_index[-1] < imf2_max_index[-1] and imf2_max_index[ -1] > 995 and imf2_max_index[-1] - imf2_min_index[-1] > 3: imf2_or_flag = 2 if imf2_min_index[-1] < imf2_max_index[-1] and imf2_max_index[ -1] < 994 and imf2_max_index[-1] - imf2_min_index[ -1] > 3 and self.close_price[ current_price_index - 1] < self.close_price[current_price_index]: imf2_or_flag = 0 if imf2_min_index[-1] > imf2_max_index[-1] and imf2_min_index[ -1] < 995 and imf2_min_index[-1] - imf2_max_index[-1] > 3: imf2_or_flag = 0 #if imf2_min_index[-1]>imf2_max_index[-1] and imf2_min_index[-1]<990 : # imf2_or_flag=1 residual_max_index = list( argrelextrema(np.array(residual), np.greater)[0]) residual_min_index = list( argrelextrema(np.array(residual), np.less)[0]) print "residual" residual_max_index.insert(0, 0) residual_min_index.insert(0, 0) print "max %s" % residual_max_index print "min %s" % residual_min_index residual_flag = 0 #if (residual_max_index[-1]<residual_min_index[-1] and residual_min_index[-1]<45): if (residual_max_index[-1] < residual_min_index[-1] and residual_min_index[-1] > 46): residual_flag = 1 print "residual %s" % residual_flag process_data = self.close_price[current_price_index - 99:current_price_index + 1] data = np.array(process_data) max_index = list(argrelextrema(data, np.greater)[0]) min_index = list(argrelextrema(data, np.less)[0]) ###### imf1_max_index = list( argrelextrema(np.array(imf_list[1]), np.greater)[0]) imf1_min_index = list(argrelextrema(np.array(imf_list[1]), np.less)[0]) imf1_flag = 1 if imf1_max_index[-1] > imf1_min_index[-1] and imf1_max_index[-1] > 996: imf1_flag = 0 if imf1_max_index[-1] < imf1_min_index[-1] and 1.2 > ( (999 - imf1_min_index[-1]) / abs(imf1_max_index[-1] - imf1_min_index[-1])) > 0.8: imf1_flag = 0 tmp = imf1_max_index + imf1_min_index tmp.sort() trust1 = tmp[-2] ############################################################## if len(imf_list) < 2: imf_process = list(imf_list[0]) else: imf_process = list(imf_list[1]) imf_process = list(imf_list[2]) imf_process3 = list(imf_list[2]) # print "imf process len%s"%len(imf_process) # #tmp = matchlist(imf_process,10,20) # print "imf process%s"%imf_process[-10:] # (tmp,extenflag1) = matchlist3(imf_process) # print "tmp len%s"%len(tmp) # imf_process=tmp # print "imf process%s"%imf_process[-10:] imf_max_index = list( argrelextrema(np.array(imf_process), np.greater)[0]) print " imf max index %s" % imf_max_index imf_min_index = list(argrelextrema(np.array(imf_process), np.less)[0]) print " imf min index %s" % imf_min_index imf_min_index2 = filter(lambda n: n <= trust1, imf_min_index) imf_max_index2 = filter(lambda n: n <= trust1, imf_max_index) # tmp = imf_max_index+imf_min_index # tmp.sort() # i=1 # while tmp[-i]>trust: # i+=1 # print "use %s "%i # if i==1 :#or tmp[-2]<990: # (imf_process,extenflag1,ex_num) = matchlist1(imf_process) # elif i==2: # (imf_process,extenflag1,ex_num) = matchlist2(imf_process) # else: # (imf_process,extenflag1,ex_num) = matchlist3(imf_process) tmp = imf_max_index2 + imf_min_index2 tmp.sort() trust = tmp[-2] tmp = imf_max_index + imf_min_index tmp.sort() i = 1 while tmp[-i] > trust: i += 1 print "use %s " % i print "first begin%s" % i if i == 1: #or tmp[-2]<990: (imf_process, extenflag1, ex_num, cha) = matchlist1(imf_list[2]) elif i == 2: (imf_process, extenflag1, ex_num, cha) = matchlist2(imf_list[2]) else: (imf_process, extenflag1, ex_num, cha) = matchlist3(imf_list[2]) print "exnum %s" % ex_num if ex_num > 3 or tmp[-i] < 980: i = i - 1 if i == 1: #or tmp[-2]<990: (imf_process, extenflag1, ex_num, cha) = matchlist1(imf_list[2]) elif i == 2: (imf_process, extenflag1, ex_num, cha) = matchlist2(imf_list[2]) else: (imf_process, extenflag1, ex_num, cha) = matchlist3(imf_list[2]) print "exnum %s" % ex_num if ex_num > 3 or tmp[-i] < 980: i = i - 1 if i == 1: #or tmp[-2]<990: (imf_process, extenflag1, ex_num, cha) = matchlist1(imf_list[2]) elif i == 2: (imf_process, extenflag1, ex_num, cha) = matchlist2(imf_list[2]) else: (imf_process, extenflag1, ex_num, cha) = matchlist3(imf_list[2]) print "exnum %s" % ex_num print "one %s" % i last_power = imf_process[-2] - (np.mean(imf_process[-6:-1])) current_power = imf_process[-1] - (np.mean(imf_process[-5:])) power_de = 0 if last_power < 0 and current_power > 0: power_de = 1 if last_power > 0 and current_power < 0: power_de = 2 imf_max_index = list( argrelextrema(np.array(imf_process), np.greater)[0]) print " imf max index %s" % imf_max_index imf_min_index = list(argrelextrema(np.array(imf_process), np.less)[0]) print " imf min index %s" % imf_min_index print "imf process" print "imf process extenflag%s " % extenflag1 print imf_process[-1] print "-1imf_min_index %s" % imf_process[imf_min_index[-1]] print "-1imf_max_index %s" % imf_process[imf_max_index[-1]] ex_flag = 1 if ex_num > 2: ex_flag = 0 print "ex flag%s num %s" % (ex_flag, ex_num) self.extenflag.append(extenflag1) print "extenflag %s" % self.extenflag[-10:] self.matchcha.append(cha) print "matchcha%s" % self.matchcha[-10:] imf2_flag = 0 print imf_min_index[-1] print imf_max_index[-1] if imf_min_index[-1] > imf_max_index[-1] and imf_min_index[-1] > len( imf_process) - 5 and imf_min_index[-1] - imf_max_index[-1] > 3: imf2_flag = 1 if imf_min_index[-1] < imf_max_index[-1] and imf_max_index[-1] >= len( imf_process) - 5: imf2_flag = 2 ########### print "trust 985" imf_max_index = list( argrelextrema(np.array(imf_list[2]), np.greater)[0]) print " imf max index %s" % imf_max_index imf_min_index = list(argrelextrema(np.array(imf_list[2]), np.less)[0]) print " imf min index %s" % imf_min_index imf_min_index2 = filter(lambda n: n <= trust1, imf_min_index) imf_max_index2 = filter(lambda n: n <= trust1, imf_max_index) print "twotrust%s" % trust1 tmp = imf_max_index2 + imf_min_index2 tmp.sort() trust = tmp[-2] print "twotrust%s" % trust tmp = imf_max_index + imf_min_index tmp.sort() i = 1 while tmp[-i] > trust: i += 1 print "use %s " % i print "sec begin%s" % i if i == 1: #or tmp[-2]<990: (imf_process, extenflag1, ex_num, cha) = matchlist1(imf_list[2]) elif i == 2: (imf_process, extenflag1, ex_num, cha) = matchlist2(imf_list[2]) else: (imf_process, extenflag1, ex_num, cha) = matchlist3(imf_list[2]) print "exnum %s" % ex_num if ex_num > 3 or tmp[-i] < 985: i = i - 1 if i == 1: #or tmp[-2]<990: (imf_process, extenflag1, ex_num, cha) = matchlist1(imf_list[2]) elif i == 2: (imf_process, extenflag1, ex_num, cha) = matchlist2(imf_list[2]) else: (imf_process, extenflag1, ex_num, cha) = matchlist3(imf_list[2]) print "exnum %s" % ex_num if ex_num > 3 or tmp[-i] < 985: i = i - 1 if i == 1: #or tmp[-2]<990: (imf_process, extenflag1, ex_num, cha) = matchlist1(imf_list[2]) elif i == 2: (imf_process, extenflag1, ex_num, cha) = matchlist2(imf_list[2]) else: (imf_process, extenflag1, ex_num, cha) = matchlist3(imf_list[2]) print "two %s" % i imf_max_index = list( argrelextrema(np.array(imf_process), np.greater)[0]) print " imf max index %s" % imf_max_index imf_min_index = list(argrelextrema(np.array(imf_process), np.less)[0]) print " imf min index %s" % imf_min_index imf2_flag2 = 0 if imf_min_index[-1] > imf_max_index[-1] and imf_min_index[-1] > len( imf_process) - 5 and imf_min_index[-1] - imf_max_index[-1] > 3: imf2_flag2 = 1 if imf_min_index[-1] < imf_max_index[-1] and imf_max_index[-1] >= len( imf_process) - 5: imf2_flag2 = 2 print "imf flag%s %s" % (imf2_flag, imf2_flag2) ############# ########## decision = 0 if (imf2_flag == 1 and imf2_flag2 == 1 and imf2_or_flag != 2) or ( imf2_or_flag == 1 ): #and imf2_flag6==1:#or self.close_flag==1:#and imf2_open_flag!=2: #if imf2_flag==1 and imf2_open_flag!=2 and imf2_macd_flag!=2 and imf2_or_flag!=2:#and imf2_open_flag==1:#and (imf_process3[-1]<0 or imf_process[-1]<0) :#and (extenflag1+extenflag3)<3:#and imf2_open_flag!=2: # if (imf2_flag==1 and self.open_flag==1) or (imf2_flag==1 and imf2_macd_flag==1) or (imf2_macd_flag==1 and self.close_flag==1) or (imf2_macd_flag==1 and imf2_flag==1):#and imf2_macd_flag==1:#and power_de==1:#and imf3_flag==1: decision = 1 #if (imf2_flag==2 or imf2_macd_flag==2) and imf2_or_flag==2 :#and imf2_open_flag!=2: # if imf2_flag==2 and imf2_open_flag==2 : #if (imf2_flag==2 and self.open_flag==2) or (imf2_flag==2 and imf2_open_flag==2) or (imf2_open_flag==2 and self.close_flag==2) : if (imf2_flag == 2): #imf2_flag==2 and imf2_open_flag==2: decision = 2 if imf2_flag == 1: self.close_flag = 1 else: self.close_flag = 0 return decision
def run_predict1(self, imf_list, current_price_index, date, datafile, residual, imf_open, imf_macd): print "\n\n begin" for i in range(len(imf_list)): imfp = imf_list[i] data = np.array(imfp) max_index = list(argrelextrema(data, np.greater)[0]) min_index = list(argrelextrema(data, np.less)[0]) print "imf %s" % i print "max %s" % max_index print "min %s" % min_index #### imf0_max_index = list( argrelextrema(np.array(imf_list[0]), np.greater)[0]) imf0_min_index = list(argrelextrema(np.array(imf_list[0]), np.less)[0]) imf0_flag = 0 if imf0_min_index[-1] > imf0_max_index[-1]: imf0_flag = 1 ### imf2_max_index = list( argrelextrema(np.array(imf_list[2]), np.greater)[0]) print " imf max index %s" % imf2_max_index imf2_min_index = list(argrelextrema(np.array(imf_list[2]), np.less)[0]) print " imf min index %s" % imf2_min_index imf2_or_flag = 0 if imf2_min_index[-1] > imf2_max_index[-1] and imf2_min_index[ -1] > 997 and imf2_min_index[-1] - imf2_max_index[-1] > 3: imf2_or_flag = 1 if imf2_min_index[-1] < imf2_max_index[-1] and imf2_max_index[ -1] > 995 and imf2_max_index[-1] - imf2_min_index[-1] > 3: imf2_or_flag = 2 print "imf2 or flag%s" % imf2_or_flag #imf2_max_index = list(argrelextrema(np.array(imf_list[2][:-4]),np.greater)[0]) #imf2_min_index = list(argrelextrema(np.array(imf_list[2][:-4]),np.less)[0]) #if imf2_min_index[-1]<imf2_max_index[-1] and imf2_max_index[-1]<995 and imf2_max_index[-1]-imf2_min_index[-1]>3: # imf2_or_flag=11 #if imf2_min_index[-1]>imf2_max_index[-1] and imf2_min_index[-1]<995 and imf2_min_index[-1]-imf2_max_index[-1]>3: # imf2_or_flag=0 #if imf2_min_index[-1]>imf2_max_index[-1] and imf2_min_index[-1]<990 : # imf2_or_flag=1 residual_max_index = list( argrelextrema(np.array(residual), np.greater)[0]) residual_min_index = list( argrelextrema(np.array(residual), np.less)[0]) print "residual" residual_max_index.insert(0, 0) residual_min_index.insert(0, 0) print "max %s" % residual_max_index print "min %s" % residual_min_index residual_flag = 0 #if (residual_max_index[-1]<residual_min_index[-1] and residual_min_index[-1]<45): if (residual_max_index[-1] < residual_min_index[-1] and residual_min_index[-1] > 46): residual_flag = 1 print "residual %s" % residual_flag process_data = self.close_price[current_price_index - 99:current_price_index + 1] data = np.array(process_data) max_index = list(argrelextrema(data, np.greater)[0]) min_index = list(argrelextrema(data, np.less)[0]) ###### imf0_max_index = list( argrelextrema(np.array(imf_list[0]), np.greater)[0]) imf0_min_index = list(argrelextrema(np.array(imf_list[0]), np.less)[0]) tmp = imf0_max_index + imf0_min_index tmp.sort() trust0 = tmp[-2] imf1_max_index = list( argrelextrema(np.array(imf_list[1]), np.greater)[0]) imf1_min_index = list(argrelextrema(np.array(imf_list[1]), np.less)[0]) imf1_min_index = filter(lambda n: n <= trust0, imf1_min_index) imf1_max_index = filter(lambda n: n <= trust0, imf1_max_index) tmp = imf1_max_index + imf1_min_index tmp.sort() trust1 = tmp[-2] imf1_min_index = filter(lambda n: n < trust0, imf1_min_index) imf1_max_index = filter(lambda n: n < trust0, imf1_max_index) tmp = imf1_max_index + imf1_min_index tmp.sort() trust11 = tmp[-1] use = 2 #if trust11<993 and tmp[-1]-tmp[-2]>3: # use=1 imf2_max_index = list( argrelextrema(np.array(imf_list[2]), np.greater)[0]) imf2_min_index = list(argrelextrema(np.array(imf_list[2]), np.less)[0]) imf2_min_index = filter(lambda n: n <= trust1, imf2_min_index) imf2_max_index = filter(lambda n: n <= trust1, imf2_max_index) tmp = imf2_max_index + imf2_min_index tmp.sort() trust2 = tmp[-1] ############################################################## if len(imf_list) < 2: imf_process = list(imf_list[0]) else: imf_process = list(imf_list[1]) imf_process = list(imf_list[2]) imf_process3 = list(imf_list[2]) # print "imf process len%s"%len(imf_process) # #tmp = matchlist(imf_process,10,20) # print "imf process%s"%imf_process[-10:] # (tmp,extenflag1) = matchlist3(imf_process) # print "tmp len%s"%len(tmp) # imf_process=tmp # print "imf process%s"%imf_process[-10:] # tmp = imf_max_index+imf_min_index # tmp.sort() # i=1 # while tmp[-i]>trust: # i+=1 # print "use %s "%i # if i==1 :#or tmp[-2]<990: # (imf_process,extenflag1,ex_num) = matchlist1(imf_process) # elif i==2: # (imf_process,extenflag1,ex_num) = matchlist2(imf_process) # else: # (imf_process,extenflag1,ex_num) = matchlist3(imf_process) imf2_max_index = list( argrelextrema(np.array(imf_list[2]), np.greater)[0]) imf2_min_index = list(argrelextrema(np.array(imf_list[2]), np.less)[0]) tmp = imf2_max_index + imf2_min_index tmp.sort() i = 1 while tmp[-i] > trust2: i += 1 print "haha %s" % i if i == 1: #or tmp[-2]<990: (imf_process, extenflag1, ex_num, cha) = matchlist1(imf_list[2], 3) elif i == 2: (imf_process, extenflag1, ex_num, cha) = matchlist2(imf_list[2], 3) else: (imf_process, extenflag1, ex_num, cha) = matchlist3(imf_list[2], 3) ##### ##### last_power = imf_process[-2] - (np.mean(imf_process[-4:-1])) current_power = imf_process[-1] - (np.mean(imf_process[-3:])) power_de = 0 if last_power < 0 and current_power > 0: power_de = 1 if last_power > 0 and current_power < 0: power_de = 2 imf_max_index = list( argrelextrema(np.array(imf_process), np.greater)[0]) print " imf max index %s" % imf_max_index imf_min_index = list(argrelextrema(np.array(imf_process), np.less)[0]) print " imf min index %s" % imf_min_index print "imf process %s" % imf_process[-10:] print "imf process extenflag%s " % extenflag1 print imf_process[-1] print "-1imf_min_index %s" % imf_process[imf_min_index[-1]] print "-1imf_max_index %s" % imf_process[imf_max_index[-1]] ex_flag = 1 if ex_num > 2: ex_flag = 0 print "ex flag%s num %s" % (ex_flag, ex_num) self.extenflag.append(extenflag1) print "extenflag %s" % self.extenflag[-10:] self.matchcha.append(cha) print "matchcha%s" % self.matchcha[-10:] imf2_flag = 0 print imf_min_index[-1] print imf_max_index[-1] last_max = current_price_index - (999 - imf_max_index[-1]) + 1 if imf_min_index[-1] > imf_max_index[-1] and imf_min_index[ -1] > 996 and extenflag1 < 2: #and ex_flag==1:#and imf_process[-1]<0 : imf2_flag = 1 if imf_min_index[-1] < imf_max_index[-1] and imf_max_index[-1] > 996: imf2_flag = 2 print "pwoer de %s" % power_de ########### #imf1_max_index = list(argrelextrema(np.array(imf_list[1]),np.greater)[0]) #imf1_min_index = list(argrelextrema(np.array(imf_list[1]),np.less)[0]) #tmp = imf1_max_index+imf1_min_index #tmp.sort() #i=1 #while tmp[-i]>trust11: # i+=1 #print "1haha %s"%i #if i==1 :#or tmp[-2]<990: # (imf_process,extenflag1,ex_num,cha) = matchlist1(imf_list[1],2) #elif i==2: # (imf_process,extenflag1,ex_num,cha) = matchlist2(imf_list[1],2) #else: # (imf_process,extenflag1,ex_num,cha) = matchlist3(imf_list[1],2) #imf_max_index = list(argrelextrema(np.array(imf_process),np.greater)[0]) #print " imf1 max index %s"%imf_max_index #imf_min_index = list(argrelextrema(np.array(imf_process),np.less)[0]) #print " imf1 min index %s"%imf_min_index #imf1_flag=0 #if imf_min_index[-1]>imf_max_index[-1] and imf_min_index[-1]>len(imf_process)-5 and extenflag1<2:#and imf_process[-1]<0 : # imf1_flag=1 #if imf_min_index[-1]<imf_max_index[-1] and imf_max_index[-1]>=len(imf_process)-5: # imf1_flag=2 #print "imf1flag %s"%imf1_flag ############# ########## decision = 0 #if ((imf2_flag==1 and imf1_flag==1 and imf2_or_flag==11) or (imf2_or_flag==1 and imf1_flag==1)) and (imf0_flag==1) :#and imf2_flag6==1:#or self.close_flag==1:#and imf2_open_flag!=2: if ( (imf2_flag == 1 and imf2_or_flag != 2) or imf2_or_flag == 1 ): # and len(self.imf_flag_list)>1 and self.imf_flag_list[-1]==1:#and imf2_or_flag!=2:# and imf1_flag==1:#and imf2_flag6==1:#or self.close_flag==1:#and imf2_open_flag!=2: #if imf2_flag==1 and imf2_flag_open_flag!=2 and imf2_macd_flag!=2 and imf2_or_flag!=2:#and imf2_open_flag==1:#and (imf_process3[-1]<0 or imf_process[-1]<0) :#and (extenflag1+extenflag3)<3:#and imf2_open_flag!=2: # if (imf2_flag==1 and self.open_flag==1) or (imf2_flag==1 and imf2_macd_flag==1) or (imf2_macd_flag==1 and self.close_flag==1) or (imf2_macd_flag==1 and imf2_flag==1):#and imf2_macd_flag==1:#and power_de==1:#and imf3_flag==1: decision = 1 #elif ((imf2_flag==1 and imf2_or_flag==11 ) or (imf2_or_flag==1)) and (imf0_flag==1) :#and imf2_flag6==1:#or self.close_flag==1:#and imf2_open_flag!=2: # decision=3 #if (imf2_flag==2 or imf2_macd_flag==2) and imf2_or_flag==2 :#and imf2_open_flag!=2: # if imf2_flag==2 and imf2_open_flag==2 : #if (imf2_flag==2 and self.open_flag==2) or (imf2_flag==2 and imf2_open_flag==2) or (imf2_open_flag==2 and self.close_flag==2) : elif ( imf2_flag == 2 and imf2_or_flag != 2 ) or imf2_or_flag == 2: #and imf1_flag==2:#imf2_flag==2 and imf2_open_flag==2: decision = 2 if imf2_flag == 1: self.close_flag = 1 else: self.close_flag = 0 self.imf_flag_list.append(imf2_flag) print "flaglist %s" % self.imf_flag_list[-5:] print "decision%s" % decision return (decision, last_max)
def run_predict1(self,imf_list,current_price_index,date,datafile,residual,imf_open,imf_macd): for i in range(len(imf_list)): imfp = imf_list[i] data = np.array(imfp) max_index = list(argrelextrema(data,np.greater)[0]) min_index = list(argrelextrema(data,np.less)[0]) print "imf %s"%i print "max %s"%max_index print "min %s"%min_index imf2_max_index = list(argrelextrema(np.array(imf_list[2]),np.greater)[0]) print " imf max index %s"%imf2_max_index imf2_min_index = list(argrelextrema(np.array(imf_list[2]),np.less)[0]) print " imf min index %s"%imf2_min_index imf2_or_flag=0 if imf2_min_index[-1]>imf2_max_index[-1] and imf2_min_index[-1]>996 and imf2_min_index[-1]-imf2_max_index[-1]>3: imf2_or_flag=1 if imf2_min_index[-1]<imf2_max_index[-1] and imf2_max_index[-1]>995 and imf2_max_index[-1]-imf2_min_index[-1]>3: imf2_or_flag=2 if imf2_min_index[-1]<imf2_max_index[-1] and imf2_max_index[-1]<994 and imf2_max_index[-1]-imf2_min_index[-1]>3 and self.close_price[current_price_index -1]<self.close_price[current_price_index]: imf2_or_flag=1 if imf2_min_index[-1]>imf2_max_index[-1] and imf2_min_index[-1]<995 and imf2_min_index[-1]-imf2_max_index[-1]>3: imf2_or_flag=0 #if imf2_min_index[-1]>imf2_max_index[-1] and imf2_min_index[-1]<990 : # imf2_or_flag=1 residual_max_index = list(argrelextrema(np.array(residual),np.greater)[0]) residual_min_index = list(argrelextrema(np.array(residual),np.less)[0]) print "residual" residual_max_index.insert(0,0) residual_min_index.insert(0,0) print "max %s"%residual_max_index print "min %s"%residual_min_index residual_flag=0 #if (residual_max_index[-1]<residual_min_index[-1] and residual_min_index[-1]<45): if (residual_max_index[-1]<residual_min_index[-1] and residual_min_index[-1]>46): residual_flag=1 print "residual %s"%residual_flag process_data = self.close_price[current_price_index-99:current_price_index+1] data = np.array(process_data) max_index = list(argrelextrema(data,np.greater)[0]) min_index = list(argrelextrema(data,np.less)[0]) ###### imf1_max_index = list(argrelextrema(np.array(imf_list[1]),np.greater)[0]) imf1_min_index = list(argrelextrema(np.array(imf_list[1]),np.less)[0]) imf1_flag=1 if imf1_max_index[-1]>imf1_min_index[-1] and imf1_max_index[-1]>996: imf1_flag=0 if imf1_max_index[-1]<imf1_min_index[-1] and 1.2>((999-imf1_min_index[-1])/abs(imf1_max_index[-1]-imf1_min_index[-1]))>0.8: imf1_flag=0 ############################################################## if len(imf_list)<2: imf_process = list(imf_list[0]) else: imf_process = list(imf_list[1]) imf_process = list(imf_list[2]) imf_process3 = list(imf_list[2]) # print "imf process len%s"%len(imf_process) # #tmp = matchlist(imf_process,10,20) # print "imf process%s"%imf_process[-10:] # (tmp,extenflag1) = matchlist3(imf_process) # print "tmp len%s"%len(tmp) # imf_process=tmp # print "imf process%s"%imf_process[-10:] imf_max_index = list(argrelextrema(np.array(imf_process),np.greater)[0]) print " imf max index %s"%imf_max_index imf_min_index = list(argrelextrema(np.array(imf_process),np.less)[0]) print " imf min index %s"%imf_min_index tmp = imf_max_index+imf_min_index tmp.sort() i=1 while tmp[-i]>995: i+=1 print "use %s "%i if i==1 :#or tmp[-2]<990: (imf_process,extenflag1) = matchlist1(imf_process) elif i==2: (imf_process,extenflag1) = matchlist2(imf_process) else: (imf_process,extenflag1) = matchlist3(imf_process) last_power=imf_process[-2]-(np.mean(imf_process[-6:-1])) current_power=imf_process[-1]-(np.mean(imf_process[-5:])) power_de=0 if last_power<0 and current_power>0: power_de=1 if last_power>0 and current_power<0: power_de=2 imf_max_index = list(argrelextrema(np.array(imf_process),np.greater)[0]) print " imf max index %s"%imf_max_index imf_min_index = list(argrelextrema(np.array(imf_process),np.less)[0]) print " imf min index %s"%imf_min_index print "imf process" print imf_process[-1] print "-1imf_min_index %s"%imf_process[imf_min_index[-1]] print "-1imf_max_index %s"%imf_process[imf_max_index[-1]] print "cha%s"%abs(imf_process[imf_min_index[-1]]-imf_process[imf_max_index[-1]]) diff_peak = [abs(imf_max_index[i]-imf_min_index[i]) for i in range(len(imf_max_index)-2)] print "mean cha %s"%np.mean(diff_peak) imf2_flag=0 print imf_min_index[-1] print imf_max_index[-1] if imf_min_index[-1]>imf_max_index[-1] and imf_min_index[-1]>len(imf_process)-4 :#and imf_process[-1]<0 : imf2_flag=1 if imf_min_index[-1]<imf_max_index[-1] and imf_max_index[-1]>=len(imf_process)-4: imf2_flag=2 ########### imf_process=imf_list[2] imf_max_index = list(argrelextrema(np.array(imf_process),np.greater)[0]) print " imf max index %s"%imf_max_index imf_min_index = list(argrelextrema(np.array(imf_process),np.less)[0]) print " imf min index %s"%imf_min_index tmp = imf_max_index+imf_min_index tmp.sort() i=1 while tmp[-i]>994: i+=1 print "use %s "%i if i==1 :#or tmp[-2]<990: (imf_process,extenflag1) = matchlist1(imf_process) elif i==2: (imf_process,extenflag1) = matchlist2(imf_process) else: (imf_process,extenflag1) = matchlist3(imf_process) imf_max_index = list(argrelextrema(np.array(imf_process),np.greater)[0]) print " imf max index %s"%imf_max_index imf_min_index = list(argrelextrema(np.array(imf_process),np.less)[0]) print " imf min index %s"%imf_min_index imf2_flag2=0 if imf_min_index[-1]>imf_max_index[-1] and imf_min_index[-1]>len(imf_process)-4 :#and imf_process[-1]<0 : imf2_flag2=1 if imf_min_index[-1]<imf_max_index[-1] and imf_max_index[-1]>len(imf_process)-4: imf2_flag2=2 ##### imf_process=imf_list[2] imf_max_index = list(argrelextrema(np.array(imf_process),np.greater)[0]) print " imf max index %s"%imf_max_index imf_min_index = list(argrelextrema(np.array(imf_process),np.less)[0]) print " imf min index %s"%imf_min_index tmp = imf_max_index+imf_min_index tmp.sort() i=1 while tmp[-i]>993: i+=1 print "use %s "%i if i==1 :#or tmp[-2]<990: (imf_process,extenflag1) = matchlist1(imf_process) elif i==2: (imf_process,extenflag1) = matchlist2(imf_process) else: (imf_process,extenflag1) = matchlist3(imf_process) imf_max_index = list(argrelextrema(np.array(imf_process),np.greater)[0]) print " imf max index %s"%imf_max_index imf_min_index = list(argrelextrema(np.array(imf_process),np.less)[0]) print " imf min index %s"%imf_min_index imf2_flag3=0 if imf_min_index[-1]>imf_max_index[-1] and imf_min_index[-1]>len(imf_process)-4 :#and imf_process[-1]<0 : imf2_flag3=1 if imf_min_index[-1]<imf_max_index[-1] and imf_max_index[-1]>len(imf_process)-4: imf2_flag3=2 ####### ##### imf_process=imf_list[2] imf_max_index = list(argrelextrema(np.array(imf_process),np.greater)[0]) print " imf max index %s"%imf_max_index imf_min_index = list(argrelextrema(np.array(imf_process),np.less)[0]) print " imf min index %s"%imf_min_index tmp = imf_max_index+imf_min_index tmp.sort() i=1 while tmp[-i]>992: i+=1 print "use %s "%i if i==1 :#or tmp[-2]<990: (imf_process,extenflag1) = matchlist1(imf_process) elif i==2: (imf_process,extenflag1) = matchlist2(imf_process) else: (imf_process,extenflag1) = matchlist3(imf_process) imf_max_index = list(argrelextrema(np.array(imf_process),np.greater)[0]) print " imf max index %s"%imf_max_index imf_min_index = list(argrelextrema(np.array(imf_process),np.less)[0]) print " imf min index %s"%imf_min_index imf2_flag4=0 if imf_min_index[-1]>imf_max_index[-1] and imf_min_index[-1]>len(imf_process)-4 :#and imf_process[-1]<0 : imf2_flag4=1 if imf_min_index[-1]<imf_max_index[-1] and imf_max_index[-1]>len(imf_process)-4: imf2_flag4=2 ####### imf_process=imf_list[2] imf_max_index = list(argrelextrema(np.array(imf_process),np.greater)[0]) print " imf max index %s"%imf_max_index imf_min_index = list(argrelextrema(np.array(imf_process),np.less)[0]) print " imf min index %s"%imf_min_index tmp = imf_max_index+imf_min_index tmp.sort() i=1 while tmp[-i]>996: i+=1 print "use %s "%i if i==1 :#or tmp[-2]<990: (imf_process,extenflag1) = matchlist1(imf_process) elif i==2: (imf_process,extenflag1) = matchlist2(imf_process) else: (imf_process,extenflag1) = matchlist3(imf_process) imf_max_index = list(argrelextrema(np.array(imf_process),np.greater)[0]) print " imf max index %s"%imf_max_index imf_min_index = list(argrelextrema(np.array(imf_process),np.less)[0]) print " imf min index %s"%imf_min_index imf2_flag5=0 if imf_min_index[-1]>imf_max_index[-1] and imf_min_index[-1]>len(imf_process)-4 :#and imf_process[-1]<0 : imf2_flag5=1 if imf_min_index[-1]<imf_max_index[-1] and imf_max_index[-1]>len(imf_process)-4: imf2_flag5=2 ####### ####### imf_process=imf_list[3] imf_max_index = list(argrelextrema(np.array(imf_process),np.greater)[0]) print " imf max index %s"%imf_max_index imf_min_index = list(argrelextrema(np.array(imf_process),np.less)[0]) print " imf min index %s"%imf_min_index tmp = imf_max_index+imf_min_index tmp.sort() if tmp[-1]<992 and tmp[-1]-tmp[-2]<4: (imf_process,extenflag1) = matchlist1(imf_process) imf_max_index = list(argrelextrema(np.array(imf_process),np.greater)[0]) print " imf max index %s"%imf_max_index imf_min_index = list(argrelextrema(np.array(imf_process),np.less)[0]) print " imf min index %s"%imf_min_index imf2_flag6=1 if imf_min_index[-1]>imf_max_index[-1] and imf_min_index[-1]>len(imf_process)-4 :#and imf_process[-1]<0 : imf2_flag6=1 else: imf2_flag6=0 ############# ########## decision=0 if imf2_flag==1 and imf2_flag2==1 and imf2_flag3==1 and imf2_flag4==1 and imf2_or_flag!=2 :#and imf2_flag6==1:#or self.close_flag==1:#and imf2_open_flag!=2: #if imf2_flag==1 and imf2_open_flag!=2 and imf2_macd_flag!=2 and imf2_or_flag!=2:#and imf2_open_flag==1:#and (imf_process3[-1]<0 or imf_process[-1]<0) :#and (extenflag1+extenflag3)<3:#and imf2_open_flag!=2: # if (imf2_flag==1 and self.open_flag==1) or (imf2_flag==1 and imf2_macd_flag==1) or (imf2_macd_flag==1 and self.close_flag==1) or (imf2_macd_flag==1 and imf2_flag==1):#and imf2_macd_flag==1:#and power_de==1:#and imf3_flag==1: decision=1 #if (imf2_flag==2 or imf2_macd_flag==2) and imf2_or_flag==2 :#and imf2_open_flag!=2: # if imf2_flag==2 and imf2_open_flag==2 : #if (imf2_flag==2 and self.open_flag==2) or (imf2_flag==2 and imf2_open_flag==2) or (imf2_open_flag==2 and self.close_flag==2) : if imf2_flag==2 :#imf2_flag==2 and imf2_open_flag==2: decision=2 if imf2_flag==1: self.close_flag=1 else: self.close_flag=0 return decision
def run_predict1(self,imf_list,current_price_index,date,datafile,residual,imf_open,imf_macd,emd_data): for i in range(len(imf_list)): imfp = imf_list[i] data = np.array(imfp) max_index = list(argrelextrema(data,np.greater)[0]) min_index = list(argrelextrema(data,np.less)[0]) print "imf %s"%i print "max %s"%max_index print "min %s"%min_index imf2_max_index = list(argrelextrema(np.array(imf_list[2]),np.greater)[0]) print " imf max index %s"%imf2_max_index imf2_min_index = list(argrelextrema(np.array(imf_list[2]),np.less)[0]) print " imf min index %s"%imf2_min_index imf2_or_flag=0 if imf2_min_index[-1]>imf2_max_index[-1] and imf2_min_index[-1]>997 and imf2_min_index[-1]-imf2_max_index[-1]>3: imf2_or_flag=1 if imf2_min_index[-1]<imf2_max_index[-1] and imf2_max_index[-1]>995 and imf2_max_index[-1]-imf2_min_index[-1]>3: imf2_or_flag=2 if imf2_min_index[-1]<imf2_max_index[-1] and imf2_max_index[-1]<994 and imf2_max_index[-1]-imf2_min_index[-1]>3 and self.close_price[current_price_index -1]<self.close_price[current_price_index]: imf2_or_flag=0 if imf2_min_index[-1]>imf2_max_index[-1] and imf2_min_index[-1]<995 and imf2_min_index[-1]-imf2_max_index[-1]>3: imf2_or_flag=0 #if imf2_min_index[-1]>imf2_max_index[-1] and imf2_min_index[-1]<990 : # imf2_or_flag=1 residual_max_index = list(argrelextrema(np.array(residual),np.greater)[0]) residual_min_index = list(argrelextrema(np.array(residual),np.less)[0]) print "residual" residual_max_index.insert(0,0) residual_min_index.insert(0,0) print "max %s"%residual_max_index print "min %s"%residual_min_index residual_flag=0 #if (residual_max_index[-1]<residual_min_index[-1] and residual_min_index[-1]<45): if (residual_max_index[-1]<residual_min_index[-1] and residual_min_index[-1]>46): residual_flag=1 print "residual %s"%residual_flag process_data = self.close_price[current_price_index-99:current_price_index+1] data = np.array(process_data) max_index = list(argrelextrema(data,np.greater)[0]) min_index = list(argrelextrema(data,np.less)[0]) ###### imf1_max_index = list(argrelextrema(np.array(imf_list[1]),np.greater)[0]) imf1_min_index = list(argrelextrema(np.array(imf_list[1]),np.less)[0]) imf1_flag=1 if imf1_max_index[-1]>imf1_min_index[-1] and imf1_max_index[-1]>996: imf1_flag=0 if imf1_max_index[-1]<imf1_min_index[-1] and 1.2>((999-imf1_min_index[-1])/abs(imf1_max_index[-1]-imf1_min_index[-1]))>0.8: imf1_flag=0 tmp = imf1_max_index+imf1_min_index tmp.sort() trust=tmp[-2] ############################################################## if len(imf_list)<2: imf_process = list(imf_list[0]) else: imf_process = list(imf_list[1]) imf_process = list(imf_list[2]) imf_process3 = list(imf_list[2]) # print "imf process len%s"%len(imf_process) # #tmp = matchlist(imf_process,10,20) # print "imf process%s"%imf_process[-10:] # (tmp,extenflag1) = matchlist3(imf_process) # print "tmp len%s"%len(tmp) # imf_process=tmp # print "imf process%s"%imf_process[-10:] imf_max_index = list(argrelextrema(np.array(imf_process),np.greater)[0]) print " imf max index %s"%imf_max_index imf_min_index = list(argrelextrema(np.array(imf_process),np.less)[0]) print " imf min index %s"%imf_min_index imf_min_index2=filter(lambda n:n<=trust,imf_min_index) imf_max_index2=filter(lambda n:n<=trust,imf_max_index) # tmp = imf_max_index+imf_min_index # tmp.sort() # i=1 # while tmp[-i]>trust: # i+=1 # print "use %s "%i # if i==1 :#or tmp[-2]<990: # (imf_process,extenflag1,ex_num) = matchlist1(imf_process) # elif i==2: # (imf_process,extenflag1,ex_num) = matchlist2(imf_process) # else: # (imf_process,extenflag1,ex_num) = matchlist3(imf_process) tmp = imf_max_index2+imf_min_index2 tmp.sort() trust=tmp[-2] tmp = imf_max_index+imf_min_index tmp.sort() i=1 while tmp[-i]>trust: i+=1 print "use %s "%i if i==1 :#or tmp[-2]<990: (imf_process,extenflag1,ex_num,cha,cha2) = matchlist1(imf_list[2],3,emd_data) elif i==2: (imf_process,extenflag1,ex_num,cha,cha2) = matchlist2(imf_list[2],3,emd_data) else: (imf_process,extenflag1,ex_num,cha,cha2) = matchlist3(imf_list[2],3,emd_data) if ex_num>3 or tmp[-i]<980: i=i-1 if i==1 :#or tmp[-2]<990: (imf_process,extenflag1,ex_num,cha,cha2) = matchlist1(imf_list[2],3,emd_data) elif i==2: (imf_process,extenflag1,ex_num,cha,cha2) = matchlist2(imf_list[2],3,emd_data) else: (imf_process,extenflag1,ex_num,cha,cha2) = matchlist3(imf_list[2],3,emd_data) if ex_num>3 or tmp[-i]<980: i=i-1 if i==1 :#or tmp[-2]<990: (imf_process,extenflag1,ex_num,cha,cha2) = matchlist1(imf_list[2],3,emd_data) elif i==2: (imf_process,extenflag1,ex_num,cha,cha2) = matchlist2(imf_list[2],3,emd_data) else: (imf_process,extenflag1,ex_num,cha,cha2) = matchlist3(imf_list[2],3,emd_data) last_power=imf_process[-2]-(np.mean(imf_process[-6:-1])) current_power=imf_process[-1]-(np.mean(imf_process[-5:])) power_de=0 if last_power<0 and current_power>0: power_de=1 if last_power>0 and current_power<0: power_de=2 imf_max_index = list(argrelextrema(np.array(imf_process),np.greater)[0]) print " imf max index %s"%imf_max_index imf_min_index = list(argrelextrema(np.array(imf_process),np.less)[0]) print " imf min index %s"%imf_min_index print "imf process" print "imf process extenflag%s "%extenflag1 print imf_process[-1] print "-1imf_min_index %s"%imf_process[imf_min_index[-1]] print "-1imf_max_index %s"%imf_process[imf_max_index[-1]] ex_flag=1 if ex_num>2: ex_flag=0 print "ex flag%s num %s"%(ex_flag,ex_num) self.extenflag.append(extenflag1) print "extenflag %s"%self.extenflag[-10:] self.matchcha.append(cha) print "matchcha%s"%self.matchcha[-10:] imf2_flag=0 print imf_min_index[-1] print imf_max_index[-1] if imf_min_index[-1]>imf_max_index[-1] and imf_min_index[-1]>len(imf_process)-5 :#and imf_process[-1]<0 : imf2_flag=1 if imf_min_index[-1]<imf_max_index[-1] and imf_max_index[-1]>=len(imf_process)-5: imf2_flag=2 ########### ############# ########## decision=0 if (imf2_flag==1 and imf2_or_flag!=2) or (imf2_or_flag==1) :#and imf2_flag6==1:#or self.close_flag==1:#and imf2_open_flag!=2: #if imf2_flag==1 and imf2_open_flag!=2 and imf2_macd_flag!=2 and imf2_or_flag!=2:#and imf2_open_flag==1:#and (imf_process3[-1]<0 or imf_process[-1]<0) :#and (extenflag1+extenflag3)<3:#and imf2_open_flag!=2: # if (imf2_flag==1 and self.open_flag==1) or (imf2_flag==1 and imf2_macd_flag==1) or (imf2_macd_flag==1 and self.close_flag==1) or (imf2_macd_flag==1 and imf2_flag==1):#and imf2_macd_flag==1:#and power_de==1:#and imf3_flag==1: decision=1 #if (imf2_flag==2 or imf2_macd_flag==2) and imf2_or_flag==2 :#and imf2_open_flag!=2: # if imf2_flag==2 and imf2_open_flag==2 : #if (imf2_flag==2 and self.open_flag==2) or (imf2_flag==2 and imf2_open_flag==2) or (imf2_open_flag==2 and self.close_flag==2) : if imf2_flag==2 :#imf2_flag==2 and imf2_open_flag==2: decision=2 if imf2_flag==1: self.close_flag=1 else: self.close_flag=0 return decision
def run_predict1(self,imf_list,current_price_index,date,datafile,residual,imf_open,imf_macd,emd_data): print "\n\n begin" for i in range(len(imf_list)): imfp = imf_list[i] data = np.array(imfp) max_index = list(argrelextrema(data,np.greater)[0]) min_index = list(argrelextrema(data,np.less)[0]) print "imf %s"%i print "max %s"%max_index print "min %s"%min_index #### imf0_max_index = list(argrelextrema(np.array(imf_list[0]),np.greater)[0]) imf0_min_index = list(argrelextrema(np.array(imf_list[0]),np.less)[0]) imf0_flag=0 if imf0_min_index[-1]>imf0_max_index[-1] : imf0_flag=1 ### imf2_max_index = list(argrelextrema(np.array(imf_list[2]),np.greater)[0]) print " imf max index %s"%imf2_max_index imf2_min_index = list(argrelextrema(np.array(imf_list[2]),np.less)[0]) print " imf min index %s"%imf2_min_index imf2_or_flag=0 if imf2_min_index[-1]>imf2_max_index[-1] and imf2_min_index[-1]>997 and imf2_min_index[-1]-imf2_max_index[-1]>3: imf2_or_flag=1 if imf2_min_index[-1]<imf2_max_index[-1] and imf2_max_index[-1]>995 and imf2_max_index[-1]-imf2_min_index[-1]>3: imf2_or_flag=2 print "imf2 or flag%s"%imf2_or_flag #imf2_max_index = list(argrelextrema(np.array(imf_list[2][:-4]),np.greater)[0]) #imf2_min_index = list(argrelextrema(np.array(imf_list[2][:-4]),np.less)[0]) #if imf2_min_index[-1]<imf2_max_index[-1] and imf2_max_index[-1]<995 and imf2_max_index[-1]-imf2_min_index[-1]>3: # imf2_or_flag=11 #if imf2_min_index[-1]>imf2_max_index[-1] and imf2_min_index[-1]<995 and imf2_min_index[-1]-imf2_max_index[-1]>3: # imf2_or_flag=0 #if imf2_min_index[-1]>imf2_max_index[-1] and imf2_min_index[-1]<990 : # imf2_or_flag=1 residual_max_index = list(argrelextrema(np.array(residual),np.greater)[0]) residual_min_index = list(argrelextrema(np.array(residual),np.less)[0]) print "residual" residual_max_index.insert(0,0) residual_min_index.insert(0,0) print "max %s"%residual_max_index print "min %s"%residual_min_index residual_flag=0 #if (residual_max_index[-1]<residual_min_index[-1] and residual_min_index[-1]<45): if (residual_max_index[-1]<residual_min_index[-1] and residual_min_index[-1]>46): residual_flag=1 print "residual %s"%residual_flag process_data = self.close_price[current_price_index-99:current_price_index+1] data = np.array(process_data) max_index = list(argrelextrema(data,np.greater)[0]) min_index = list(argrelextrema(data,np.less)[0]) ###### imf0_max_index = list(argrelextrema(np.array(imf_list[0]),np.greater)[0]) imf0_min_index = list(argrelextrema(np.array(imf_list[0]),np.less)[0]) tmp = imf0_max_index+imf0_min_index tmp.sort() trust0=tmp[-2] imf1_max_index = list(argrelextrema(np.array(imf_list[1]),np.greater)[0]) imf1_min_index = list(argrelextrema(np.array(imf_list[1]),np.less)[0]) imf1_min_index=filter(lambda n:n<=trust0,imf1_min_index) imf1_max_index=filter(lambda n:n<=trust0,imf1_max_index) tmp = imf1_max_index+imf1_min_index tmp.sort() trust1=tmp[-2] imf1_min_index=filter(lambda n:n<trust0,imf1_min_index) imf1_max_index=filter(lambda n:n<trust0,imf1_max_index) tmp = imf1_max_index+imf1_min_index tmp.sort() trust11=tmp[-1] use=2 #if trust11<993 and tmp[-1]-tmp[-2]>3: # use=1 imf2_max_index = list(argrelextrema(np.array(imf_list[2]),np.greater)[0]) imf2_min_index = list(argrelextrema(np.array(imf_list[2]),np.less)[0]) imf2_min_index=filter(lambda n:n<=trust1,imf2_min_index) imf2_max_index=filter(lambda n:n<=trust1,imf2_max_index) tmp = imf2_max_index+imf2_min_index tmp.sort() trust2=tmp[-1] ############################################################## if len(imf_list)<2: imf_process = list(imf_list[0]) else: imf_process = list(imf_list[1]) imf_process = list(imf_list[2]) imf_process3 = list(imf_list[2]) # print "imf process len%s"%len(imf_process) # #tmp = matchlist(imf_process,10,20) # print "imf process%s"%imf_process[-10:] # (tmp,extenflag1) = matchlist3(imf_process) # print "tmp len%s"%len(tmp) # imf_process=tmp # print "imf process%s"%imf_process[-10:] # tmp = imf_max_index+imf_min_index # tmp.sort() # i=1 # while tmp[-i]>trust: # i+=1 # print "use %s "%i # if i==1 :#or tmp[-2]<990: # (imf_process,extenflag1,ex_num) = matchlist1(imf_process) # elif i==2: # (imf_process,extenflag1,ex_num) = matchlist2(imf_process) # else: # (imf_process,extenflag1,ex_num) = matchlist3(imf_process) imf2_max_index = list(argrelextrema(np.array(imf_list[2]),np.greater)[0]) imf2_min_index = list(argrelextrema(np.array(imf_list[2]),np.less)[0]) tmp = imf2_max_index+imf2_min_index tmp.sort() i=1 while tmp[-i]>trust2: i+=1 print "haha %s"%i if i==1 :#or tmp[-2]<990: (imf_process,extenflag1,ex_num,cha,cha2) = matchlist1(imf_list[2],3,emd_data) elif i==2: (imf_process,extenflag1,ex_num,cha,cha2) = matchlist2(imf_list[2],3,emd_data) else: (imf_process,extenflag1,ex_num,cha,cha2) = matchlist3(imf_list[2],3,emd_data) ##### ##### last_power=imf_process[-2]-(np.mean(imf_process[-4:-1])) current_power=imf_process[-1]-(np.mean(imf_process[-3:])) power_de=0 if last_power<0 and current_power>0: power_de=1 if last_power>0 and current_power<0: power_de=2 imf_max_index = list(argrelextrema(np.array(imf_process),np.greater)[0]) print " imf max index %s"%imf_max_index imf_min_index = list(argrelextrema(np.array(imf_process),np.less)[0]) print " imf min index %s"%imf_min_index print "imf process %s"%imf_process[-10:] print "imf process extenflag%s "%extenflag1 print imf_process[-1] print "-1imf_min_index %s"%imf_process[imf_min_index[-1]] print "-1imf_max_index %s"%imf_process[imf_max_index[-1]] ex_flag=1 if ex_num>2: ex_flag=0 print "ex flag%s num %s"%(ex_flag,ex_num) self.extenflag.append(extenflag1) print "extenflag %s"%self.extenflag[-10:] self.matchcha.append(cha) print "matchcha%s"%self.matchcha[-10:] imf2_flag=0 print imf_min_index[-1] print imf_max_index[-1] last_max=current_price_index-(999-imf_max_index[-1])+1 if imf_min_index[-1]>imf_max_index[-1] and imf_min_index[-1]>996 and extenflag1<2 :#and ex_flag==1:#and imf_process[-1]<0 : imf2_flag=1 if imf_min_index[-1]<imf_max_index[-1] and imf_max_index[-1]>996: imf2_flag=2 print "pwoer de %s"%power_de ########### #imf1_max_index = list(argrelextrema(np.array(imf_list[1]),np.greater)[0]) #imf1_min_index = list(argrelextrema(np.array(imf_list[1]),np.less)[0]) #tmp = imf1_max_index+imf1_min_index #tmp.sort() #i=1 #while tmp[-i]>trust11: # i+=1 #print "1haha %s"%i #if i==1 :#or tmp[-2]<990: # (imf_process,extenflag1,ex_num,cha) = matchlist1(imf_list[1],2) #elif i==2: # (imf_process,extenflag1,ex_num,cha) = matchlist2(imf_list[1],2) #else: # (imf_process,extenflag1,ex_num,cha) = matchlist3(imf_list[1],2) #imf_max_index = list(argrelextrema(np.array(imf_process),np.greater)[0]) #print " imf1 max index %s"%imf_max_index #imf_min_index = list(argrelextrema(np.array(imf_process),np.less)[0]) #print " imf1 min index %s"%imf_min_index #imf1_flag=0 #if imf_min_index[-1]>imf_max_index[-1] and imf_min_index[-1]>len(imf_process)-5 and extenflag1<2:#and imf_process[-1]<0 : # imf1_flag=1 #if imf_min_index[-1]<imf_max_index[-1] and imf_max_index[-1]>=len(imf_process)-5: # imf1_flag=2 #print "imf1flag %s"%imf1_flag ############# ########## decision=0 #if ((imf2_flag==1 and imf1_flag==1 and imf2_or_flag==11) or (imf2_or_flag==1 and imf1_flag==1)) and (imf0_flag==1) :#and imf2_flag6==1:#or self.close_flag==1:#and imf2_open_flag!=2: if ((imf2_flag==1 and imf2_or_flag!=2) or imf2_or_flag==1):# and len(self.imf_flag_list)>1 and self.imf_flag_list[-1]==1:#and imf2_or_flag!=2:# and imf1_flag==1:#and imf2_flag6==1:#or self.close_flag==1:#and imf2_open_flag!=2: #if imf2_flag==1 and imf2_flag_open_flag!=2 and imf2_macd_flag!=2 and imf2_or_flag!=2:#and imf2_open_flag==1:#and (imf_process3[-1]<0 or imf_process[-1]<0) :#and (extenflag1+extenflag3)<3:#and imf2_open_flag!=2: # if (imf2_flag==1 and self.open_flag==1) or (imf2_flag==1 and imf2_macd_flag==1) or (imf2_macd_flag==1 and self.close_flag==1) or (imf2_macd_flag==1 and imf2_flag==1):#and imf2_macd_flag==1:#and power_de==1:#and imf3_flag==1: decision=1 #elif ((imf2_flag==1 and imf2_or_flag==11 ) or (imf2_or_flag==1)) and (imf0_flag==1) :#and imf2_flag6==1:#or self.close_flag==1:#and imf2_open_flag!=2: # decision=3 #if (imf2_flag==2 or imf2_macd_flag==2) and imf2_or_flag==2 :#and imf2_open_flag!=2: # if imf2_flag==2 and imf2_open_flag==2 : #if (imf2_flag==2 and self.open_flag==2) or (imf2_flag==2 and imf2_open_flag==2) or (imf2_open_flag==2 and self.close_flag==2) : elif (imf2_flag==2 and imf2_or_flag!=2) or imf2_or_flag==2 :#and imf1_flag==2:#imf2_flag==2 and imf2_open_flag==2: decision=2 if imf2_flag==1: self.close_flag=1 else: self.close_flag=0 self.imf_flag_list.append(imf2_flag) print "flaglist %s"%self.imf_flag_list[-5:] print "decision%s"%decision return (decision,last_max)
def run_predict1(self, imf_list, current_price_index, date, datafile, residual, imf_open, imf_macd): for i in range(len(imf_list)): imfp = imf_list[i] data = np.array(imfp) max_index = list(argrelextrema(data, np.greater)[0]) min_index = list(argrelextrema(data, np.less)[0]) print "imf %s" % i print "max %s" % max_index print "min %s" % min_index imf2_max_index = list( argrelextrema(np.array(imf_list[2]), np.greater)[0]) print " imf max index %s" % imf2_max_index imf2_min_index = list(argrelextrema(np.array(imf_list[2]), np.less)[0]) print " imf min index %s" % imf2_min_index imf2_or_flag = 0 if imf2_min_index[-1] > imf2_max_index[-1] and imf2_min_index[ -1] > 996 and imf2_min_index[-1] - imf2_max_index[-1] > 3: imf2_or_flag = 1 if imf2_min_index[-1] < imf2_max_index[-1] and imf2_max_index[ -1] > 995 and imf2_max_index[-1] - imf2_min_index[-1] > 3: imf2_or_flag = 2 if imf2_min_index[-1] < imf2_max_index[-1] and imf2_max_index[ -1] < 994 and imf2_max_index[-1] - imf2_min_index[ -1] > 3 and self.close_price[ current_price_index - 1] < self.close_price[current_price_index]: imf2_or_flag = 1 if imf2_min_index[-1] > imf2_max_index[-1] and imf2_min_index[ -1] < 995 and imf2_min_index[-1] - imf2_max_index[-1] > 3: imf2_or_flag = 2 #if imf2_min_index[-1]>imf2_max_index[-1] and imf2_min_index[-1]<990 : # imf2_or_flag=1 residual_max_index = list( argrelextrema(np.array(residual), np.greater)[0]) residual_min_index = list( argrelextrema(np.array(residual), np.less)[0]) print "residual" residual_max_index.insert(0, 0) residual_min_index.insert(0, 0) print "max %s" % residual_max_index print "min %s" % residual_min_index residual_flag = 0 #if (residual_max_index[-1]<residual_min_index[-1] and residual_min_index[-1]<45): if (residual_max_index[-1] < residual_min_index[-1] and residual_min_index[-1] > 46): residual_flag = 1 print "residual %s" % residual_flag process_data = self.close_price[current_price_index - 99:current_price_index + 1] data = np.array(process_data) max_index = list(argrelextrema(data, np.greater)[0]) min_index = list(argrelextrema(data, np.less)[0]) ###### imf1_max_index = list( argrelextrema(np.array(imf_list[1]), np.greater)[0]) imf1_min_index = list(argrelextrema(np.array(imf_list[1]), np.less)[0]) imf1_flag = 1 if imf1_max_index[-1] > imf1_min_index[-1] and imf1_max_index[-1] > 996: imf1_flag = 0 if imf1_max_index[-1] < imf1_min_index[-1] and 1.2 > ( (999 - imf1_min_index[-1]) / abs(imf1_max_index[-1] - imf1_min_index[-1])) > 0.8: imf1_flag = 0 ############################################################## if len(imf_list) < 2: imf_process = list(imf_list[0]) else: imf_process = list(imf_list[1]) imf_process = list(imf_list[2]) imf_process3 = list(imf_list[2]) # print "imf process len%s"%len(imf_process) # #tmp = matchlist(imf_process,10,20) # print "imf process%s"%imf_process[-10:] # (tmp,extenflag1) = matchlist3(imf_process) # print "tmp len%s"%len(tmp) # imf_process=tmp # print "imf process%s"%imf_process[-10:] imf_max_index = list( argrelextrema(np.array(imf_process), np.greater)[0]) print " imf max index %s" % imf_max_index imf_min_index = list(argrelextrema(np.array(imf_process), np.less)[0]) print " imf min index %s" % imf_min_index tmp = imf_max_index + imf_min_index tmp.sort() i = 1 while tmp[-i] > 995: i += 1 print "use %s " % i if i == 1: #or tmp[-2]<990: (imf_process, extenflag1) = matchlist1(imf_process) elif i == 2: (imf_process, extenflag1) = matchlist2(imf_process) else: (imf_process, extenflag1) = matchlist3(imf_process) last_power = imf_process[-2] - (np.mean(imf_process[-6:-1])) current_power = imf_process[-1] - (np.mean(imf_process[-5:])) power_de = 0 if last_power < 0 and current_power > 0: power_de = 1 if last_power > 0 and current_power < 0: power_de = 2 imf_max_index = list( argrelextrema(np.array(imf_process), np.greater)[0]) print " imf max index %s" % imf_max_index imf_min_index = list(argrelextrema(np.array(imf_process), np.less)[0]) print " imf min index %s" % imf_min_index print "imf process" print imf_process[-1] print "-1imf_min_index %s" % imf_process[imf_min_index[-1]] print "-1imf_max_index %s" % imf_process[imf_max_index[-1]] print "cha%s" % abs(imf_process[imf_min_index[-1]] - imf_process[imf_max_index[-1]]) diff_peak = [ abs(imf_max_index[i] - imf_min_index[i]) for i in range(len(imf_max_index) - 2) ] print "mean cha %s" % np.mean(diff_peak) imf2_flag = 0 print imf_min_index[-1] print imf_max_index[-1] if imf_min_index[-1] > imf_max_index[-1] and imf_min_index[-1] > len( imf_process) - 4: #and imf_process[-1]<0 : imf2_flag = 1 if imf_min_index[-1] < imf_max_index[-1] and imf_max_index[-1] > len( imf_process) - 4: imf2_flag = 2 ########### imf_process = imf_list[2] imf_max_index = list( argrelextrema(np.array(imf_process), np.greater)[0]) print " imf max index %s" % imf_max_index imf_min_index = list(argrelextrema(np.array(imf_process), np.less)[0]) print " imf min index %s" % imf_min_index tmp = imf_max_index + imf_min_index tmp.sort() i = 1 while tmp[-i] > 994: i += 1 print "use %s " % i if i == 1: #or tmp[-2]<990: (imf_process, extenflag1) = matchlist1(imf_process) elif i == 2: (imf_process, extenflag1) = matchlist2(imf_process) else: (imf_process, extenflag1) = matchlist3(imf_process) imf_max_index = list( argrelextrema(np.array(imf_process), np.greater)[0]) print " imf max index %s" % imf_max_index imf_min_index = list(argrelextrema(np.array(imf_process), np.less)[0]) print " imf min index %s" % imf_min_index imf2_flag2 = 0 if imf_min_index[-1] > imf_max_index[-1] and imf_min_index[-1] > len( imf_process) - 4: #and imf_process[-1]<0 : imf2_flag2 = 1 if imf_min_index[-1] < imf_max_index[-1] and imf_max_index[-1] > len( imf_process) - 4: imf2_flag2 = 2 ##### imf_process = imf_list[2] imf_max_index = list( argrelextrema(np.array(imf_process), np.greater)[0]) print " imf max index %s" % imf_max_index imf_min_index = list(argrelextrema(np.array(imf_process), np.less)[0]) print " imf min index %s" % imf_min_index tmp = imf_max_index + imf_min_index tmp.sort() i = 1 while tmp[-i] > 993: i += 1 print "use %s " % i if i == 1: #or tmp[-2]<990: (imf_process, extenflag1) = matchlist1(imf_process) elif i == 2: (imf_process, extenflag1) = matchlist2(imf_process) else: (imf_process, extenflag1) = matchlist3(imf_process) imf_max_index = list( argrelextrema(np.array(imf_process), np.greater)[0]) print " imf max index %s" % imf_max_index imf_min_index = list(argrelextrema(np.array(imf_process), np.less)[0]) print " imf min index %s" % imf_min_index imf2_flag3 = 0 if imf_min_index[-1] > imf_max_index[-1] and imf_min_index[-1] > len( imf_process) - 4: #and imf_process[-1]<0 : imf2_flag3 = 1 if imf_min_index[-1] < imf_max_index[-1] and imf_max_index[-1] > len( imf_process) - 4: imf2_flag3 = 2 ####### ##### imf_process = imf_list[2] imf_max_index = list( argrelextrema(np.array(imf_process), np.greater)[0]) print " imf max index %s" % imf_max_index imf_min_index = list(argrelextrema(np.array(imf_process), np.less)[0]) print " imf min index %s" % imf_min_index tmp = imf_max_index + imf_min_index tmp.sort() i = 1 while tmp[-i] > 992: i += 1 print "use %s " % i if i == 1: #or tmp[-2]<990: (imf_process, extenflag1) = matchlist1(imf_process) elif i == 2: (imf_process, extenflag1) = matchlist2(imf_process) else: (imf_process, extenflag1) = matchlist3(imf_process) imf_max_index = list( argrelextrema(np.array(imf_process), np.greater)[0]) print " imf max index %s" % imf_max_index imf_min_index = list(argrelextrema(np.array(imf_process), np.less)[0]) print " imf min index %s" % imf_min_index imf2_flag4 = 0 if imf_min_index[-1] > imf_max_index[-1] and imf_min_index[-1] > len( imf_process) - 4: #and imf_process[-1]<0 : imf2_flag4 = 1 if imf_min_index[-1] < imf_max_index[-1] and imf_max_index[-1] > len( imf_process) - 4: imf2_flag4 = 2 ####### imf_process = imf_list[2] imf_max_index = list( argrelextrema(np.array(imf_process), np.greater)[0]) print " imf max index %s" % imf_max_index imf_min_index = list(argrelextrema(np.array(imf_process), np.less)[0]) print " imf min index %s" % imf_min_index tmp = imf_max_index + imf_min_index tmp.sort() i = 1 while tmp[-i] > 996: i += 1 print "use %s " % i if i == 1: #or tmp[-2]<990: (imf_process, extenflag1) = matchlist1(imf_process) elif i == 2: (imf_process, extenflag1) = matchlist2(imf_process) else: (imf_process, extenflag1) = matchlist3(imf_process) imf_max_index = list( argrelextrema(np.array(imf_process), np.greater)[0]) print " imf max index %s" % imf_max_index imf_min_index = list(argrelextrema(np.array(imf_process), np.less)[0]) print " imf min index %s" % imf_min_index imf2_flag5 = 0 if imf_min_index[-1] > imf_max_index[-1] and imf_min_index[-1] > len( imf_process) - 4: #and imf_process[-1]<0 : imf2_flag5 = 1 if imf_min_index[-1] < imf_max_index[-1] and imf_max_index[-1] > len( imf_process) - 4: imf2_flag5 = 2 ######## imf_open_max_index = list( argrelextrema(np.array(imf_open[2]), np.greater)[0]) print " imf open max index %s" % imf_open_max_index imf_open_min_index = list( argrelextrema(np.array(imf_open[2]), np.less)[0]) print " imf open min index %s" % imf_open_min_index tmp = imf_open_max_index + imf_open_min_index tmp.sort() i = 1 while tmp[-i] > 994: i += 1 print "use %s " % i if i == 1: #or tmp[-2]<990: (imf_open_process, extenflag1) = matchlist1(imf_open[2]) elif i == 2: (imf_open_process, extenflag1) = matchlist2(imf_open[2]) else: (imf_open_process, extenflag1) = matchlist3(imf_open[2]) ### imf_open_max_index = list( argrelextrema(np.array(imf_open_process), np.greater)[0]) print " imf open max index %s" % imf_open_max_index imf_open_min_index = list( argrelextrema(np.array(imf_open_process), np.less)[0]) print " imf open min index %s" % imf_open_min_index imf2_open_flag = 0 if imf_open_min_index[-1] > imf_open_max_index[ -1] and imf_open_min_index[-1] > len( imf_open_process ) - 5: #and extenflag2<0.6 :#and imf_open_min_index[-1]-imf_open_max_index[-1]>3: imf2_open_flag = 1 if imf_open_min_index[-1] < imf_open_max_index[ -1] and imf_open_max_index[-1] > len(imf_open_process) - 5: imf2_open_flag = 2 ##### imf_macd_max_index = list( argrelextrema(np.array(imf_macd[2]), np.greater)[0]) print " imf macd max index %s" % imf_macd_max_index imf_macd_min_index = list( argrelextrema(np.array(imf_macd[2]), np.less)[0]) print " imf macd min index %s" % imf_macd_min_index tmp = imf_macd_max_index + imf_macd_min_index tmp.sort() i = 1 while tmp[-i] > 994: i += 1 print "use %s " % i if i == 1: #or tmp[-2]<990: (imf_macd_process, extenflag1) = matchlist1(imf_macd[2]) elif i == 2: (imf_macd_process, extenflag1) = matchlist2(imf_macd[2]) else: (imf_macd_process, extenflag1) = matchlist3(imf_macd[2]) ### imf_macd_max_index = list( argrelextrema(np.array(imf_macd_process), np.greater)[0]) print " imf macd max index %s" % imf_macd_max_index imf_macd_min_index = list( argrelextrema(np.array(imf_macd_process), np.less)[0]) print " imf macd min index %s" % imf_macd_min_index ##### imf2_macd_flag = 0 if imf_macd_min_index[-1] > imf_macd_max_index[ -1] and imf_macd_min_index[-1] > len( imf_macd_process ) - 5: #and imf_process3[-1]<0:#and imf_open_min_index[-1]-imf_open_max_index[-1]>3: imf2_macd_flag = 1 if imf_macd_min_index[-1] < imf_macd_max_index[ -1] and imf_macd_max_index[-1] > len(imf_macd_process) - 5: imf2_macd_flag = 2 #### imf_4_max_index = list( argrelextrema(np.array(imf_list[3]), np.greater)[0]) print " imf 4 max index %s" % imf_4_max_index imf_4_min_index = list( argrelextrema(np.array(imf_list[3]), np.less)[0]) print " imf 4 min index %s" % imf_4_min_index tmp = imf_4_max_index + imf_4_min_index tmp.sort() i = 1 while tmp[-i] > 990: i += 1 print "use %s " % i if i == 1: #or tmp[-2]<990: (imf_4_process, extenflag1) = matchlist1(imf_list[3]) elif i == 2: (imf_4_process, extenflag1) = matchlist2(imf_list[3]) else: (imf_4_process, extenflag1) = matchlist3(imf_list[3]) ### imf_4_max_index = list( argrelextrema(np.array(imf_4_process), np.greater)[0]) print " imf 4 max index %s" % imf_4_max_index imf_4_min_index = list( argrelextrema(np.array(imf_4_process), np.less)[0]) print " imf 4 min index %s" % imf_4_min_index imf_4_min_index = filter(lambda n: n < 998, imf_4_min_index) imf4_flag = 0 if imf_4_min_index[-1] > imf_4_max_index[-1] and imf_4_min_index[ -1] >= 990: #and imf_process3[-1]<0:#and imf_open_min_index[-1]-imf_open_max_index[-1]>3: imf4_flag = 1 ####### print "\n\n" print "imf flag2 %s" % imf2_flag print imf_process[-5:] print "imf open flag2 %s" % imf2_open_flag print "imf macd flag2 %s" % imf2_macd_flag print imf_process3[-5:] print "imf or flag2 %s" % imf2_or_flag print "exten flag%s" % extenflag1 print "\n\n" ############# print "power de%s" % power_de ########## decision = 0 if imf2_flag == 1 and imf2_flag2 == 1 and imf2_flag3 == 1 and imf2_flag4 == 1 and imf2_flag5 == 1: #or self.close_flag==1:#and imf2_open_flag!=2: #if imf2_flag==1 and imf2_open_flag!=2 and imf2_macd_flag!=2 and imf2_or_flag!=2:#and imf2_open_flag==1:#and (imf_process3[-1]<0 or imf_process[-1]<0) :#and (extenflag1+extenflag3)<3:#and imf2_open_flag!=2: # if (imf2_flag==1 and self.open_flag==1) or (imf2_flag==1 and imf2_macd_flag==1) or (imf2_macd_flag==1 and self.close_flag==1) or (imf2_macd_flag==1 and imf2_flag==1):#and imf2_macd_flag==1:#and power_de==1:#and imf3_flag==1: decision = 1 #if (imf2_flag==2 or imf2_macd_flag==2) and imf2_or_flag==2 :#and imf2_open_flag!=2: # if imf2_flag==2 and imf2_open_flag==2 : #if (imf2_flag==2 and self.open_flag==2) or (imf2_flag==2 and imf2_open_flag==2) or (imf2_open_flag==2 and self.close_flag==2) : if imf2_flag == 2: #imf2_flag==2 and imf2_open_flag==2: decision = 2 if imf2_flag == 1: self.close_flag = 1 else: self.close_flag = 0 if imf2_macd_flag == 1: self.open_flag = 1 else: self.open_flag = 0 if power_de == 1: self.power_flag = 1 else: self.power_flag = 0 return decision