def analy_deal_type(self, stock_name, start_date, end_date, plt, color):
     delta_days = (end_date - start_date).days
     mysql = MySQL('trans')
     mysql.connect()
     for day in xrange(0, delta_days, 1):
         today = start_date + timedelta(days=day)
         next_day = start_date + timedelta(days=day+1)
         state_where_buy = "DEAL_DATE > '%s' and DEAL_DATE < '%s' and DEAL_TYPE = 1" % (today, next_day)
         buy_data = mysql.query_where(stock_name, state_where_buy)
         cnt_buy_data = len(buy_data)
         state_where_sell = "DEAL_DATE > '%s' and DEAL_DATE < '%s' and DEAL_TYPE = -1" % (today, next_day)
         sell_data = mysql.query_where(stock_name, state_where_sell)
         cnt_sell_data = len(sell_data)
         scale = 5
         if cnt_sell_data is not 0:
             if cnt_buy_data is not 0:
                 buy_total = self.sum_long(buy_data)
             else:
                 buy_total = 0
             sell_total = self.sum_long(sell_data)
             perc = (float(buy_total) / sell_total) * scale
             # perc = (float(cnt_buy_data) / cnt_sell_data) * scale
         else:
             perc = 0
         plt.scatter(today, perc, c=color)
     plt.axhline(y=scale, xmin=0, xmax=1)
     try:
         plt.savefig(".\\results\\"+self.stock_name+".png", dpi=200)
     except Exception:
         print "save png file error!"
     mysql.close_connect()
Beispiel #2
0
def daily2DB(src, db_name):
    tables = []
    for root, dirs, files in os.walk(src):
        for file_name in files:
            if file_name.find('.txt') == -1:
                continue
            else:
                print "Processing daily to DB File: %s" % file_name
                table_name = file_name.split('.')[0]
                tables.append(table_name)
                full_file_name = join(root, file_name)
                txt_file = open(full_file_name)
                stock_name = txt_file.readline().decode('gbk').encode('utf-8')
                #for str in stock_name.split(" ", 3):
                #    print str.lstrip().rstrip()
                title = txt_file.readline().decode('gbk').encode('utf-8')
                #for str in title.lstrip().split("\t", 7):
                #    print str.lstrip()
                mysql = MySQL(db_name)
                mysql.connect()
                col_type = list()
                col_type.append("`ID` INT NOT NULL AUTO_INCREMENT")
                col_type.append("`DATE` DATETIME NULL")
                col_type.append("`START_PRICE` FLOAT NULL")
                col_type.append("`HIGH_PRICE` FLOAT NULL")
                col_type.append("`LOW_PRICE` FLOAT NULL")
                col_type.append("`CLOSE_PRICE` FLOAT NULL")
                col_type.append("`DEAL_AMOUNT` INT NULL")
                col_type.append("`DEAL_PRICE` FLOAT NULL")
                mysql.create_table_with_delete(table_name, "ID", col_type)
                content = txt_file.readline()
                data = list()
                while content:
                    content = content.replace('\n', '')
                    contents = content.split(';', 7)
                    content = txt_file.readline()
                    if len(contents) < 7:
                        continue
                    else:
                        contents[0] = datetime.strptime(contents[0], "%m/%d/%Y").strftime("%Y-%m-%d %H:%M:%S")
                        data.append(contents)
                mysql.insert_many(table_name, "`DATE`, `START_PRICE`, `HIGH_PRICE`, `LOW_PRICE`, \
                `CLOSE_PRICE`, `DEAL_AMOUNT`, `DEAL_PRICE`", data)
                mysql.close_connect()
 def analysis_deal_price(self, stock_name, start_date, end_date, plt, color):
     one_day = timedelta(days=1)
     delta_days = (end_date - start_date).days
     mysql = MySQL('trans')
     mysql.connect()
     for day in xrange(0, delta_days, 1):
         query_day = start_date + timedelta(days=day)
         state_where = "DEAL_DATE > '%s' and DEAL_DATE < '%s'" % (query_day, query_day + one_day)
         data = mysql.query_where(stock_name, state_where)
         ret = self.max_data(data)
         # ax.scatter(query_day, ret, c=color)
         plt.plot(query_day, ret, 'o-', c=color)
     mysql.close_connect()
 def analysis_close_price(self, stock_name, start_date, end_date, plt, color):
     one_day = timedelta(days=1)
     delta_days = (end_date - start_date).days
     mysql = MySQL('daily')
     mysql.connect()
     for day in xrange(0, delta_days, 1):
         query_day = start_date + timedelta(days=day)
         state_where = "DATE = '%s'" % query_day
         data = mysql.query_where(stock_name, state_where)
         if len(data) > 0:
             plt.scatter(query_day, data[0][5], c=color)
         else:
             plt.scatter(query_day, 0, c=color)
     mysql.close_connect()
 def analy_big_trans(self, stock_name, gate_money, start_date, end_date, plt, color):
     delta_days = (end_date - start_date).days
     mysql = MySQL('trans')
     mysql.connect()
     for day in xrange(0, delta_days, 1):
         today = start_date + timedelta(days=day)
         next_day = start_date + timedelta(days=day+1)
         state_where = "DEAL_DATE > '%s' and DEAL_DATE < '%s' and TOTAL_AMOUNT > %d" % (today, next_day, gate_money)
         big_data = mysql.query_where(stock_name, state_where)
         state_where = "DEAL_DATE > '%s' and DEAL_DATE < '%s'" % (today, next_day)
         all_data = mysql.query_where(stock_name, state_where)
         cnt_big_data = len(big_data)
         cnt_all_data = len(all_data)
         if cnt_all_data is not 0:
             perc = (float(cnt_big_data) / cnt_all_data) * 100
         else:
             perc = 0
         plt.scatter(today, perc, c=color)
     mysql.close_connect()
Beispiel #6
0
def trans2db(db_name, trans_path):
    mysql = MySQL(db_name)
    mysql.connect()
    col_type = list()
    col_type.append("`ID` INT NOT NULL AUTO_INCREMENT")
    col_type.append("`DEAL_DATE` DATETIME NULL")
    col_type.append("`DEAL_PRICE` FLOAT NULL")
    col_type.append("`DEAL_GAP` FLOAT NULL")
    col_type.append("`TOTAL_LOT` INT NULL")
    col_type.append("`TOTAL_AMOUNT` INT NULL")
    col_type.append("`DEAL_TYPE` INT NULL")
    col_type.append("`OTHER` FLOAT NULL")
    try:
        for root1, dirs1, files1 in os.walk(trans_path):
            for dir1 in dirs1:
                mysql.create_table(dir1, "ID", col_type)
                next_dir = root1 + "\\" + dir1
                for root2, dirs2, files2 in os.walk(next_dir):
                    for file2 in files2:
                        if file2.find('.txt') != -1:
                            data = list()
                            file_path = join(next_dir, file2)
                            file_name = file2.split(".")[0]
                            with open(file_path, 'r') as fp:
                                text_list = fp.readlines()
                                print "Processing file to DB.... " + file_path
                                for i in range(1, len(text_list)):
                                    text = text_list[i].decode('gb2312').encode('utf-8')
                                    text_elem = text.split("\t")
                                    deal_time_str = file_name + ';' + text_elem[0].strip()
                                    check_ret = check_data(text_elem)
                                    if check_ret != 0:
                                        print "###########" + str(check_ret)
                                        if check_ret != 7:
                                            print text_elem[check_ret - 1]
                                        continue
                                    deal_time = datetime.strptime('1900-01-01;00:00:00', "%Y-%m-%d;%H:%M:%S")
                                    try:
                                        deal_time = datetime.strptime(deal_time_str, "%Y-%m-%d;%H:%M:%S")
                                    except Exception:
                                        print deal_time_str
                                    #deal nums
                                    deal_price = 0
                                    deal_gap = 0
                                    deal_lot = 0
                                    deal_amount = 0
                                    try:
                                        deal_price_str = text_elem[1].strip()
                                        deal_gap_str = text_elem[2].strip()
                                        deal_lot_str = text_elem[3].strip()
                                        deal_amount_str = text_elem[4].strip()

                                        deal_price = float(deal_price_str)
                                        if deal_gap_str.find('-') == -1:
                                            deal_gap = float(deal_gap_str)
                                        else:
                                            deal_gap = 0
                                        deal_lot = float(deal_lot_str)
                                        deal_amount = float(deal_amount_str)
                                    except Exception, e:
                                        print e
                                        print deal_price_str
                                        print deal_gap_str
                                        print deal_lot
                                        print deal_amount

                                    deal_type_str = text_elem[5].strip()
                                    deal_type = 0
                                    if deal_type_str == "买盘":
                                        deal_type = 1
                                    elif deal_type_str == "卖盘":
                                        deal_type = -1
                                    else:
                                        deal_type = 0

                                    if i == 1:
                                        ret = mysql.query(dir1, "DEAL_DATE", deal_time)
                                        if len(ret) > 0:
                                            print "pass this file!"
                                            break
                                        else:
                                            pass
                                    else:
                                        pass
                                    data.append([deal_time, deal_price, deal_gap, deal_lot, deal_amount, deal_type])
                            if len(data) > 0:
                                mysql.insert_many(dir1, "`DEAL_DATE`, `DEAL_PRICE`, `DEAL_GAP`, \
                                    `TOTAL_LOT`, `TOTAL_AMOUNT`, `DEAL_TYPE`", data)
                            else:
                                pass
                            fp.close()
                            os.remove(file_path)
                        else:
                            continue
                os.rmdir(next_dir)
    except IOError, e:
        print "ERROR: " + dir1 + " FILE: " + file2
        mysql.close_connect()