Ejemplo n.º 1
0
    def run_predict(self,imf_list,current_price_index,date,datafile,train_data_dir,train_number,hidden_number,result_number):

        decision = 0
        power_decision = 0
        imf_power_decision = 0
        period_decision = 0
        extrem_decision = 0
        current_rise_flag = 0


        data = np.array(list(imf_list[2]))
        imf_max_index = argrelextrema(data,np.greater)[0]
        imf_min_index = argrelextrema(data,np.less)[0]


        price_list = self.close_price[current_price_index-499:current_price_index+1]
        price_data = np.array(price_list)
        price_max_index = argrelextrema(price_data,np.greater)[0]
        print "price_max_index %s"%price_max_index
        price_min_index = argrelextrema(price_data,np.less)[0]
        print "price_min_index %s"%price_min_index

        imf2 = list(imf_list[2])

        imf_extrem_decision = 0
        if len(list(price_max_index)) > 0 and len(list(price_min_index)) > 0 :
            index_list = list(price_max_index)+(list(price_min_index))
            print "index list%s"%index_list
            index_list.sort() 
            last_extrem_position = index_list[-2] 
            distance =  500 - last_extrem_position
            true_index = 500-distance
            print "distance %s"%distance
            print "true index %s"%true_index
            data = np.array(list(imf_list[2]))
            imf_max_index = argrelextrema(data,np.greater)[0]
            print "imf max index %s"%imf_max_index
            imf_min_index = argrelextrema(data,np.less)[0]
            print "imf min index %s"%imf_min_index
            
            imf_max_true_index = list(filter(lambda n:n<true_index,imf_max_index))
            imf_min_true_index = list(filter(lambda n:n<true_index,imf_min_index))
            print "imf max true index %s"%imf_max_true_index
            print "imf min true index %s"%imf_min_true_index
            if  list(imf_max_true_index)[-1]>list(imf_min_true_index)[-1]:
                buyhigh = imf2[imf_max_true_index[-1]]-imf2[imf_min_true_index[-1]]
                buyhigh2 = imf2[imf_max_true_index[-1]]-imf2[-1]
                print "buy high %s"%buyhigh
                print "buy high2 %s"%buyhigh2
                sellhigh = imf2[imf_max_true_index[-1]]-imf2[imf_min_true_index[-1]]
                sellhigh2 = imf2[-1]-imf2[imf_min_true_index[-1]]
                print "sell high %s"%sellhigh
                print "sell high2 %s"%sellhigh2
                distance_last_imf_max = 500-list(imf_max_true_index)[-1]
                last_min_max = list(imf_max_true_index)[-1] - list(imf_min_true_index)[-1]
                cha = distance_last_imf_max- last_min_max
                print "cha %s"%(distance_last_imf_max- last_min_max)
            #    if distance_last_imf_max<10 and distance_last_imf_max>5:
                if (buyhigh/buyhigh2)>0.9 and (buyhigh/buyhigh2)<1.1:
                    imf_extrem_decision = -1
            elif  list(imf_max_true_index)[-1]<list(imf_min_true_index)[-1]:
                buyhigh = imf2[imf_max_true_index[-1]]-imf2[imf_min_true_index[-1]]
                buyhigh2 = imf2[imf_max_true_index[-1]]-imf2[-1]
                print "buy high %s"%buyhigh
                print "buy high2 %s"%buyhigh2
                sellhigh = imf2[imf_max_true_index[-1]]-imf2[imf_min_true_index[-1]]
                sellhigh2 = imf2[-1]-imf2[imf_min_true_index[-1]]
                print "sell high %s"%sellhigh
                print "sell high2 %s"%sellhigh2
                distance_last_imf_max = 500-list(imf_max_true_index)[-1]
                last_min_max = list(imf_max_true_index)[-1] - list(imf_min_true_index)[-1]
                cha = distance_last_imf_max- last_min_max
                print "cha %s"%(distance_last_imf_max- last_min_max)
            #    if distance_last_imf_max<10 and distance_last_imf_max>5:
                if (sellhigh/sellhigh2)>0.8 and (sellhigh/sellhigh2)<1.2:
                    imf_extrem_decision = 1


        now_position_last_min_delta = len(imf_list[2])-1-imf_min_index[-1]
        print "now min delta%s"%now_position_last_min_delta
        now_position_last_max_delta = len(imf_list[2])-1-imf_max_index[-1]
        print "now max delta%s"%now_position_last_max_delta
         
        

        print "date %s"%self.date[current_price_index]
        print "date %s"%date
        print "current price %s"%self.close_price[current_price_index]
        print "trade price %s"%self.open_price[current_price_index+1]
        print "buy price %s"%self.buy_price
###########################################################################################
   
        recent_price_list = self.close_price[current_price_index-4:current_price_index+1]

        recent_price_format = self.format_data(recent_price_list)
        recent_price_format2 = recent_price_format[-5:] 
  


        print "recent price format %s"%recent_price_format2


        


############################################################################################


#        f = os.popen("tail %s/data_390 -n 2|head -n 1"%train_data_dir)
        f = os.popen("cat imf2")
        lines = f.readlines()
        f.close()
#        test_data = line.split(" ")
#        test_data.pop()
        test_data_float1 = [float(each) for each in lines]

        print "raw test data length %s"%len(test_data_float1)
        tmp = self.format_data(test_data_float1)
        test_data_float = tmp[-60:-10]
        print "test data %s"%test_data_float
        print "test data length %s"%len(test_data_float)
   
        result = []
        fin_result = []
        sum = 0
        for i in range(1):
            neuro_prediction = neuroprediction("%s/data_380"%train_data_dir,"net_file",train_number,hidden_number,0,result_number)
            neuro_prediction.train_nn()
            result.append(neuro_prediction.test_each(test_data_float))
            print result[-1]
        for i in range(10):
            for j in range(1):
                sum += result[j][i]
            fin_result.append(sum)
            sum = 0
        fin_result1 = [fin_result[i]/1 for i in range(10)]
  
  
  
        print "recent price format %s"%recent_price_format2
        print "predict result %s"%fin_result1
  
  ##    ######################################################################################
        recent_price_format3 = []
        for i in recent_price_format2:
            if i<0:
                beishu1 = (-1)/min(test_data_float)
                recent_price_format3.append(i/beishu1)
            elif i>0:
                beishu2 = 1/max(test_data_float)
                recent_price_format3.append(i/beishu2)
            else:
                recent_price_format3.append(i)
        print "recent price format 3 %s"%recent_price_format3

        fin_result2 = [(recent_price_format2[i]+fin_result1[i+5])/2 for i in range(len(recent_price_format3))]
        print "after add recent price %s"%fin_result2
  
  ##    #######################################################################################
  
  
  
  
  
  ##    #######################################################################################
        imf_2 = list(imf_list[2][:-10])
        imf = imf_2+fin_result2
        print "len imf %s"%len(imf)
        print "imf %s"%list(imf_list[2])[-10:]

        imf = fin_result1
        imf = list(imf_list[2])
  
  
        imf_ma5_before = (imf[-6]+imf[-5]+imf[-4]+imf[-3]+imf[-2])/5.0
        imf_ma3_before = (imf[-4]+imf[-3]+imf[-2])/3.0
        imf_ma5_current = (imf[-5]+imf[-4]+imf[-3]+imf[-2]+imf[-1])/5.0
        imf_ma3_current = (imf[-3]+imf[-2]+imf[-1])/3.0
        imf_power_before = imf[-2] - imf_ma5_before
        imf_3_power_before = imf[-2] - imf_ma3_before
        imf_power_current = imf[-1] - imf_ma5_current
        imf_3_power_current = imf[-1] - imf_ma3_current
        print "imf power before %s"%imf_power_before
        print "imf power current %s"%imf_power_current
        print "last imf power current %s"%self.last_imf_power
        print "imf 3 power before %s"%imf_3_power_before
        print "imf 3 power current %s"%imf_3_power_current
        print "last imf 3 power current %s"%self.last_imf_3_power
 
        last_5 = self.close_price[current_price_index-5:current_price_index]
        last_5_mean = np.mean(self.close_price[current_price_index-5:current_price_index])
        last_5_std = np.std([last_5[i]-last_5_mean for i in range(5)])
        print "std 5 %s"%last_5_std
        self.imf_power.append(imf_power_current)


        last_10 = self.close_price[current_price_index-6:current_price_index]


####################################################################################################################


        imf_decision = 0 
        data = np.array(last_5)
        last_5_max_index = argrelextrema(data,np.greater)[0]
        last_5_min_index = argrelextrema(data,np.less)[0]

        data = np.array(last_10)
        last_10_max_index = argrelextrema(data,np.greater)[0]
        last_10_min_index = argrelextrema(data,np.less)[0]


        last_10_imf = list(imf_list[2])[-10:]
        print "last 10 price %s"%last_10

        if len(last_5_max_index)>0 and len(last_5_min_index)>0: 
            last_10_imf_array = np.array(last_10_imf)
            last_10_imf_max_index = argrelextrema(last_10_imf_array,np.greater)[0]
            last_10_imf_min_index = argrelextrema(last_10_imf_array,np.less)[0]
            if len(last_10_imf_max_index)>0 and last_10_imf_max_index[-1]<4:
                imf_decision = -1
            if len(last_10_imf_min_index)>0 and last_10_imf_min_index[-1]<4:
                imf_decision = 1

            
#####################################################################################################################

   
        #if (imf_power_before>0 and imf_power_current<0): #or float(imf_power_before)/imf_power_current > 2.5:
        #if (imf_3_power_before>0 and imf_3_power_current<0): #or float(imf_power_before)/imf_power_current > 2.5:
        #if (self.last_imf_power>0 and imf_power_current<0): #or float(imf_power_before)/imf_power_current > 2.5:
        if (imf[-1]<imf[-2]) and ((recent_price_format2[-1]<recent_price_format2[-2])):# and ((recent_price_format2[-1]==1) or (recent_price_format2[-2]==1 and recent_price_format2[-1]!=-1)):# and (recent_price_format2[-1]==1):# or recent_price_format2[-1]==-1): #or float(imf_power_before)/imf_power_current > 2.5:
        #if (self.last_imf_3_power>0 and imf_3_power_current<0): #or float(imf_power_before)/imf_power_current > 2.5:
        #if (self.last_imf_3_power>imf_3_power_current): #or float(imf_power_before)/imf_power_current > 2.5:
            imf_power_decision = 1
        #if (imf_power_before<0 and imf_power_current>0):# or float(imf_power_current)/imf_power_before > 2.5:
        #if (imf_3_power_before<0 and imf_3_power_current>0):# or float(imf_power_current)/imf_power_before > 2.5:
        #if (self.last_imf_3_power<0 and imf_3_power_current>0):# or float(imf_power_current)/imf_power_before > 2.5:
        if (imf[-1]>imf[-2]) and ((recent_price_format2[-1]>recent_price_format2[-2]) and (recent_price_format2[-3]>recent_price_format2[-2])):# or (recent_price_format2[-2]==-1 and recent_price_format2[-1]!=1)) :#or ((imf[-1]<imf[-2] and imf[-2]<imf[-3]) and (recent_price_format2[-1]==-1)):# or recent_price_format2[-1]==1) : #or float(imf_power_before)/imf_power_current > 2.5:
        #if (self.last_imf_power<0 and imf_power_current>0):# or float(imf_power_current)/imf_power_before > 2.5:
            imf_power_decision = -1

        print "imf power decision %s"%imf_power_decision
        trade_price = self.open_price[current_price_index+1]
        current_price = self.close_price[current_price_index]

        if self.buy_price!=0 and (imf_extrem_decision == 1) and self.share > 0 :
            print "sell 1"
            self.money = self.sell(self.share,trade_price)
            self.share = 0
            self.buy_price = 0
            self.total_asset = self.money - self.sell_fee(self.money)
            self.last_buy_flag = 0
            self.money = self.total_asset
            self.sell_x_index.append(current_price_index)
            self.sell_y_index.append(current_price)
            tomorrow_will = 1
            self.down_macd_counter = 0
            self.buy_macd = 0
            self.holdtime = 0
            once_flag = 1
  #      elif self.buy_price!=0 and ((trade_price-self.buy_price)/self.buy_price) >0.02 and self.share > 0 :
  #          print "sell 2"
  #          self.money = self.sell(self.share,trade_price)
  #          self.share = 0
  #          self.buy_price = 0
  #          self.total_asset = self.money - self.sell_fee(self.money)
  #          self.last_buy_flag = 0
  #          self.money = self.total_asset
  #          self.sell_x_index.append(current_price_index)
  #          self.sell_y_index.append(current_price)
  #          tomorrow_will = 1
  #          self.down_macd_counter = 0
  #          self.buy_macd = 0
  #          self.holdtime = 0
  #          once_flag = 1
        elif self.money>0 and imf_extrem_decision == -1:
            print "buy buy 1"
            self.share = self.buy(self.money,trade_price)
            self.money = 0
            self.buy_price = trade_price
            self.total_asset = self.share*trade_price
            self.buy_x_index.append(current_price_index)
            self.buy_y_index.append(current_price)
            self.last_buy_flag = 1
            self.buy_macd = self.macd[current_price_index]
            tomorrow_will = -1
            self.holdtime = 1
        else:
            self.last_buy_flag = 0
  
        self.last_imf_power = imf_power_current
        self.last_imf_3_power = imf_3_power_current
  
        self.total_asset_list.append(self.total_asset)
        print "total asset is %s"%(self.total_asset)
        print "money is %s"%(self.money)
        print "share is %s"%(self.share)
Ejemplo n.º 2
0
    def run_predict(self,imf_list,current_price_index,date,datafile,train_data_dir,train_number,hidden_number,result_number):

        decision = 0
        power_decision = 0
        imf_power_decision = 0
        period_decision = 0
        extrem_decision = 0
        current_rise_flag = 0


        data = np.array(imf_list[2])
        imf_max_index = argrelextrema(data,np.greater)[0]
        imf_min_index = argrelextrema(data,np.less)[0]


        now_position_last_min_delta = len(imf_list[2])-1-imf_min_index[-1]
        print "now min delta%s"%now_position_last_min_delta
        now_position_last_max_delta = len(imf_list[2])-1-imf_max_index[-1]
        print "now max delta%s"%now_position_last_max_delta
         
        

        print "date %s"%self.date[current_price_index]
        print "date %s"%date
        print "current price %s"%self.close_price[current_price_index]
        print "trade price %s"%self.open_price[current_price_index+1]
        print "buy price %s"%self.buy_price
###########################################################################################
   
        recent_price_list = self.close_price[current_price_index-4:current_price_index+1]

        recent_price_format = self.format_data(recent_price_list)
        recent_price_format2 = recent_price_format[-5:] 
  


        print "recent price format %s"%recent_price_format2


        


############################################################################################


#        f = os.popen("tail %s/data_390 -n 2|head -n 1"%train_data_dir)
        f = os.popen("cat imf2")
        lines = f.readlines()
        f.close()
#        test_data = line.split(" ")
#        test_data.pop()
        test_data_float1 = [float(each) for each in lines]

        print "raw test data length %s"%len(test_data_float1)
        tmp = self.format_data(test_data_float1)
        test_data_float = tmp[-60:-10]
        print "test data %s"%test_data_float
        print "test data length %s"%len(test_data_float)
   
        result = []
        fin_result = []
        sum = 0
        for i in range(20):
            neuro_prediction = neuroprediction("%s/data_380"%train_data_dir,"net_file",train_number,hidden_number,0,result_number)
            neuro_prediction.train_nn()
            result.append(neuro_prediction.test_each(test_data_float))
            print result[-1]
        for i in range(10):
            for j in range(20):
                sum += result[j][i]
            fin_result.append(sum)
            sum = 0
        fin_result1 = [fin_result[i]/20 for i in range(10)]
  
  
  
        print "recent price format %s"%recent_price_format2
        print "predict result %s"%fin_result1
  
  ##    ######################################################################################
        recent_price_format3 = []
        for i in recent_price_format2:
            if i<0:
                beishu1 = (-1)/min(test_data_float)
                recent_price_format3.append(i/beishu1)
            elif i>0:
                beishu2 = 1/max(test_data_float)
                recent_price_format3.append(i/beishu2)
            else:
                recent_price_format3.append(i)
        print "recent price format 3 %s"%recent_price_format3

        fin_result2 = [(recent_price_format3[i]+fin_result1[i+5])/2 for i in range(len(recent_price_format3))]
        print "after add recent price %s"%fin_result2
  
  ##    #######################################################################################
  
  
  
  
  
  ##    #######################################################################################
        imf_2 = list(imf_list[2][:-10])
        imf = imf_2+fin_result2
        print "len imf %s"%len(imf)
  
  
        imf_ma5_before = (imf[-6]+imf[-5]+imf[-4]+imf[-3]+imf[-2])/5.0
        imf_ma3_before = (imf[-4]+imf[-3]+imf[-2])/3.0
        imf_ma5_current = (imf[-5]+imf[-4]+imf[-3]+imf[-2]+imf[-1])/5.0
        imf_ma3_current = (imf[-3]+imf[-2]+imf[-1])/3.0
        imf_power_before = imf[-2] - imf_ma5_before
        imf_3_power_before = imf[-2] - imf_ma3_before
        imf_power_current = imf[-1] - imf_ma5_current
        imf_3_power_current = imf[-1] - imf_ma3_current
        print "imf power before %s"%imf_power_before
        print "imf power current %s"%imf_power_current
        print "last imf power current %s"%self.last_imf_power
        print "imf 3 power before %s"%imf_3_power_before
        print "imf 3 power current %s"%imf_3_power_current
        print "last imf 3 power current %s"%self.last_imf_3_power
 
        last_5 = self.close_price[current_price_index-5:current_price_index]
        last_5_mean = np.mean(self.close_price[current_price_index-5:current_price_index])
        last_5_std = np.std([last_5[i]-last_5_mean for i in range(5)])
        print "std 5 %s"%last_5_std
        self.imf_power.append(imf_power_current)
   
        #if (imf_power_before>0 and imf_power_current<0): #or float(imf_power_before)/imf_power_current > 2.5:
        #if (imf_3_power_before>0 and imf_3_power_current<0): #or float(imf_power_before)/imf_power_current > 2.5:
        #if (self.last_imf_power>0 and imf_power_current<0): #or float(imf_power_before)/imf_power_current > 2.5:
        if (imf[-1]<imf[-2] and imf[-2]>imf[-3]):# and (recent_price_format2[-1]==1):# or recent_price_format2[-1]==-1): #or float(imf_power_before)/imf_power_current > 2.5:
        #if (self.last_imf_3_power>0 and imf_3_power_current<0): #or float(imf_power_before)/imf_power_current > 2.5:
        #if (self.last_imf_3_power>imf_3_power_current): #or float(imf_power_before)/imf_power_current > 2.5:
            imf_power_decision = 1
        #if (imf_power_before<0 and imf_power_current>0):# or float(imf_power_current)/imf_power_before > 2.5:
        #if (imf_3_power_before<0 and imf_3_power_current>0):# or float(imf_power_current)/imf_power_before > 2.5:
        #if (self.last_imf_3_power<0 and imf_3_power_current>0):# or float(imf_power_current)/imf_power_before > 2.5:
        if (imf[-1]>imf[-2] and imf[-2]<imf[-3]) and (recent_price_format2[-1]==-1):# or recent_price_format2[-1]==1) : #or float(imf_power_before)/imf_power_current > 2.5:
        #if (self.last_imf_power<0 and imf_power_current>0):# or float(imf_power_current)/imf_power_before > 2.5:
            imf_power_decision = -1

        print "imf power decision %s"%imf_power_decision
        trade_price = self.open_price[current_price_index+1]
        current_price = self.close_price[current_price_index]

        if self.buy_price!=0 and (imf_power_decision == 1) and self.share > 0 :
            print "sell 1"
            self.money = self.sell(self.share,trade_price)
            self.share = 0
            self.buy_price = 0
            self.total_asset = self.money - self.sell_fee(self.money)
            self.last_buy_flag = 0
            self.money = self.total_asset
            self.sell_x_index.append(current_price_index)
            self.sell_y_index.append(current_price)
            tomorrow_will = 1
            self.down_macd_counter = 0
            self.buy_macd = 0
            self.holdtime = 0
            once_flag = 1
        elif self.money>0 and imf_power_decision == -1:
            print "buy buy 1"
            self.share = self.buy(self.money,trade_price)
            self.money = 0
            self.buy_price = trade_price
            self.total_asset = self.share*trade_price
            self.buy_x_index.append(current_price_index)
            self.buy_y_index.append(current_price)
            self.last_buy_flag = 1
            self.buy_macd = self.macd[current_price_index]
            tomorrow_will = -1
            self.holdtime = 1
        else:
            self.last_buy_flag = 0
  
        self.last_imf_power = imf_power_current
        self.last_imf_3_power = imf_3_power_current
  
        self.total_asset_list.append(self.total_asset)
        print "total asset is %s"%(self.total_asset)
        print "money is %s"%(self.money)
        print "share is %s"%(self.share)
Ejemplo n.º 3
0
    #print array_after_format
    label = list(data_mat[:,-1])
    fp=open(train_file,'w')

    fp.write(str(row_number))
    fp.write(" ")
    fp.write(str(col_number-1))
    fp.write(" ")
    fp.write("1")
    fp.write("\n")


    for i in range(row_number):
        for j in range(col_number-1):
            fp.write(str(array_after_format[i][j]))
            fp.write(" ")
        fp.write("\n")
        fp.write(str(label[i]))
        fp.write("\n")
    fp.close()


    my_nn = neuroprediction(train_file,"net_file",4,10,0,1)
    my_nn.train_nn()
    
    

        
        
    
Ejemplo n.º 4
0
    col_number = data_mat.shape[1]
    col_after_format = []
    for i in range(col_number - 1):
        col_list = list(data_mat[:, i])
        #col_after_format.append(format_data(col_list))
        col_after_format.append((col_list))
    new_array = np.array(col_after_format)
    array_after_format = new_array.transpose()
    #print array_after_format
    label = list(data_mat[:, -1])
    fp = open(train_file, 'w')

    fp.write(str(row_number))
    fp.write(" ")
    fp.write(str(col_number - 1))
    fp.write(" ")
    fp.write("1")
    fp.write("\n")

    for i in range(row_number):
        for j in range(col_number - 1):
            fp.write(str(array_after_format[i][j]))
            fp.write(" ")
        fp.write("\n")
        fp.write(str(label[i]))
        fp.write("\n")
    fp.close()

    my_nn = neuroprediction(train_file, "net_file", 4, 10, 0, 1)
    my_nn.train_nn()