#!/usr/bin/python2.7
# coding=utf-8

import os
import sys
import re
import json
import time
import traceback
import logging
import logging.handlers

import common
import tools

IS_TEST = False

RECENT_WEEK = 7
RECENT_MONTH = 30

logger = common.get_file_logger('v', 'volume.log')

def getFundBList():
    fundb_info = tools.httpGet('https://www.jisilu.cn/data/sfnew/fundb_list/?___t=1461335375159')
    fundbj = json.loads(fundb_info)
    for row in fundbj['rows']:
        print row['id']


"""
analyze('sh', 'sh_name_list.txt')
analyze('sz', 'sz_name_list.txt')

"""
def analyze(market, code_list_file):
    data = list()

    sh_f = open(code_list_file, 'r')
    tc = 0
    for line in sh_f:
        try:
            tc = tc + 1
            if IS_TEST and tc >= 6: break;

            code, name = line.split(' ')
            print 'analyzing '+name.strip()+'(%s)#%d...'%(code,tc)
            logger.debug('analyzing '+name.strip()+'(%s)#%d...'%(code,tc))
            stock_detail = common.grab_detail(code)
            if not stock_detail:
                print 'get detail fail:'+'(%s)!'%(code)
                continue

            d = []
            d.append(code) #0
            d.append(name.strip()) #1
            d.append(float(stock_detail['realPrice'])) #2,价格
            d.append(stock_detail['changeRate']) #3,价格增幅
            d.append(stock_detail['itab']) #4,成交金额
            d.append(float(stock_detail['itca'].replace('%',''))) #5,换手率
            d.append(0)  #6,成交量增幅
            d.append(0)  #7,最近几个交易日均价（不包含当天）
            d.append(0)  #8,最近几个交易日平均换手率（不包含当天）
            d.append(0)  #9,最近几个交易日成交量平均增幅
            d.append(0)  #10,最近几个交易日价格平均增幅
            d.append(0)  #11,从当天开始倒推，价格连续上涨的天数
            d.append(0)  #12,从当天开始倒推，成交量连续上涨的天数
            d.append(0)  #13,底部特征指标
            d.append('')  #14,7天底部特征描述。min_price~max_price~avg_price~zhenfu%|min_volume~max_volume~avg_volume~zhenfu%
            d.append('')  #15,1个月底部特征描述。min_price~max_price~avg_price~zhenfu%|min_volume~max_volume~avg_volume~zhenfu%
            d.append('')  #16,2个月底部特征描述。min_price~max_price~avg_price~zhenfu%|min_volume~max_volume~avg_volume~zhenfu%

            data.append(d)
        except Exception as e:
            logger.error(str(e) + '\n' + traceback.format_exc())
            print >> sys.stderr, str(e) + '\n' + traceback.format_exc()


    daylist = []
    valid_day_num = 2 * RECENT_MONTH
    i = 1
    while valid_day_num > 0:
        daystr = time.strftime('%Y%m%d',time.gmtime(time.time()-3600*24*i))
        i = i + 1
        if (i > 3 * RECENT_MONTH): break;
        #print daystr
        filepath = 'data/%s_%s'%(market,daystr)
        if IS_TEST:
            filepath = 'data/%s_%s_test'%(market,daystr)

        if not os.path.exists(filepath):
            print filepath, 'not exists'
            continue

        daydict = {}
        with open(filepath, 'r') as f:
            for line in f:
                if len(line) == 0:
                    continue
                fields = line.split(',')

                #while len(fields) < 11:
                #    fields.append(0)

                daydict[fields[0]] = fields
                #print daydict[fields[0]]

        daylist.append(daydict)
        valid_day_num = valid_day_num - 1


    all_the_same = True
    if len(daylist) == 0:
        all_the_same = False
    else:
        for todayline in data:
            if not daylist[0].has_key(todayline[0]):
                all_the_same = False
                break
            #print float(todayline[2]) == float(daylist[0][todayline[0]][2]) True
            #print todayline[2] == daylist[0][todayline[0]][2]  False  WHY?
            if float(todayline[2]) != float(daylist[0][todayline[0]][2]) or float(todayline[4]) != float(daylist[0][todayline[0]][4]):
                all_the_same = False
                break

    todaystr = time.strftime('%Y%m%d')
    if not all_the_same:
        print 'not all the same, save new file'
        todayfile = open('data/%s_%s'%(market,todaystr), 'w')
        for d in data:
            todayfile.write(d[0]+','+str(d[1])+','+str(d[2])+','+str(d[3])+','+str(d[4])+','+str(d[5])+'\n')
        todayfile.close()
    else:
        print 'all the same'
        if not IS_TEST:
            return False

    if len(daylist) == 0:
        return
    
    print "Computing all stocks' exchange information..."
    for todayline in data:
        code = todayline[0]
        huanshoulv_zengfu = 0.0
        volume_zengfu = 0.0
        total_huanshou = 0.0
        total_price = 0.0
        price_up_days = 0
        volume_up_days = 0
        price_down = 0
        volume_down = 0
        total_volume_change = 0.0
        total_price_change = float(todayline[3].replace('%',''))
        volume1 = float(todayline[4])
        volume2 = 0
        daynum = 0
        today_price_change = float(todayline[3].replace('%',''))
        #需要加上涨幅范围的限制？例如跌3个点以上的不计入连续性 
        if today_price_change < 0:
            price_down = price_down + 1
        elif today_price_change >= 0:
            price_up_days = price_up_days + 1

        #以下是底部特征描述参数
        week_min_price = sys.float_info.max
        week_max_price = 0.0
        week_total_price = 0.0
        week_avg_price = 0.0
        week_price_zhenfu = 0.0
        week_min_volume = sys.float_info.max
        week_max_volume = 0.0
        week_total_volume = 0.0
        week_avg_volume = 0.0
        week_volume_zhenfu = 0.0
        week_n = 0

        month_min_price = sys.float_info.max
        month_max_price = 0.0
        month_total_price = 0.0
        month_avg_price = 0.0
        month_price_zhenfu = 0.0
        month_min_volume = sys.float_info.max
        month_max_volume = 0.0
        month_total_volume = 0.0
        month_avg_volume = 0.0
        month_volume_zhenfu = 0.0
        month_n = 0

        month2_min_price = sys.float_info.max
        month2_max_price = 0.0
        month2_total_price = 0.0
        month2_avg_price = 0.0
        month2_price_zhenfu = 0.0
        month2_min_volume = sys.float_info.max
        month2_max_volume = 0.0
        month2_total_volume = 0.0
        month2_avg_volume = 0.0
        month2_volume_zhenfu = 0.0
        month2_n = 0

        vn = 0
        for i in range(len(daylist)):
            day = daylist[i]
            if day.has_key(code):
                day_price = float(day[code][2])
                day_volume = float(day[code][4])
                day_huanshou = float(day[code][5])
                #停牌日略过去不计算
                if day_price <= 0 or day_volume <= 0 or day_huanshou <= 0:
                    continue

                if i == 0:
                    huanshoulv_zengfu = float(todayline[5])/float(day[code][5])
                    #print 'huanshoulv zengfu:',huanshoulv_zengfu
                    volume_zengfu = (float(todayline[4]) - day_volume)/day_volume
                    todayline[6] = float('%0.4f'%volume_zengfu)
                    #todayline[6] = float('%0.2f'%huanshoulv_zengfu)

                if vn < RECENT_WEEK:
                    total_price = total_price + day_price
                    total_huanshou = total_huanshou + float(day[code][5])

                    price_change = float(day[code][3].replace('%',''))
                    if price_change < 0:
                        price_down = price_down + 1

                    #允许一次跌幅
                    if price_down < 2 and price_change >= 0:
                        price_up_days = price_up_days + 1

                    volume2 = float(day[code][4])
                    if volume1 < volume2:
                        volume_down = volume_down + 1

                    #允许一次量缩
                    if volume_down < 2 and volume1 >= volume2:
                        volume_up_days = volume_up_days + 1

                    total_volume_change = total_volume_change + (volume1-volume2)/volume2
                    total_price_change = total_price_change + float(day[code][3].replace('%',''))

                    volume1 = volume2
                    daynum = daynum + 1

                    #以下是一周底部参数计算
                    if day_price < week_min_price:
                        week_min_price = day_price
                    if day_price > week_max_price:
                        week_max_price = day_price
                    week_total_price = week_total_price + day_price

                    if day_volume < week_min_volume:
                        week_min_volume = day_volume
                    if day_volume > week_max_volume:
                        week_max_volume = day_volume
                    week_total_volume = week_total_volume + day_volume
                    week_n = week_n + 1

                    #以下是一个月底部参数计算
                    if day_price < month_min_price:
                        month_min_price = day_price
                    if day_price > month_max_price:
                        month_max_price = day_price
                    month_total_price = month_total_price + day_price

                    if day_volume < month_min_volume:
                        month_min_volume = day_volume
                    if day_volume > month_max_volume:
                        month_max_volume = day_volume
                    month_total_volume = month_total_volume + day_volume
                    month_n = month_n + 1

                    #以下是两个月底部参数计算
                    if day_price < month2_min_price:
                        month2_min_price = day_price
                    if day_price > month2_max_price:
                        month2_max_price = day_price
                    month2_total_price = month2_total_price + day_price

                    if day_volume < month2_min_volume:
                        month2_min_volume = day_volume
                    if day_volume > month2_max_volume:
                        month2_max_volume = day_volume
                    month2_total_volume = month2_total_volume + day_volume
                    month2_n = month2_n + 1

                elif vn < RECENT_MONTH:
                    #以下是一个月底部参数计算
                    if day_price < month_min_price:
                        month_min_price = day_price
                    if day_price > month_max_price:
                        month_max_price = day_price
                    month_total_price = month_total_price + day_price

                    if day_volume < month_min_volume:
                        month_min_volume = day_volume
                    if day_volume > month_max_volume:
                        month_max_volume = day_volume
                    month_total_volume = month_total_volume + day_volume
                    month_n = month_n + 1

                    #以下是两个月底部参数计算
                    if day_price < month2_min_price:
                        month2_min_price = day_price
                    if day_price > month2_max_price:
                        month2_max_price = day_price
                    month2_total_price = month2_total_price + day_price

                    if day_volume < month2_min_volume:
                        month2_min_volume = day_volume
                    if day_volume > month2_max_volume:
                        month2_max_volume = day_volume
                    month2_total_volume = month2_total_volume + day_volume
                    month2_n = month2_n + 1

                elif vn < RECENT_MONTH * 2:
                    #以下是两个月底部参数计算
                    if day_price < month2_min_price:
                        month2_min_price = day_price
                    if day_price > month2_max_price:
                        month2_max_price = day_price
                    month2_total_price = month2_total_price + day_price

                    if day_volume < month2_min_volume:
                        month2_min_volume = day_volume
                    if day_volume > month2_max_volume:
                        month2_max_volume = day_volume
                    month2_total_volume = month2_total_volume + day_volume
                    month2_n = month2_n + 1

                else:
                    break

                vn = vn + 1

        if daynum > 0:
            avg_price = total_price / daynum
            avg_huanshou = total_huanshou / daynum
            avg_price_change = total_price_change / (daynum+1)
            avg_volume_change = total_volume_change / daynum
            todayline[7] = float('%0.2f'%avg_price)
            todayline[8] = float('%0.2f'%avg_huanshou)
            todayline[9] = float('%0.4f'%avg_volume_change)
            todayline[10] = float('%0.2f'%avg_price_change)
            todayline[11] = price_up_days
            todayline[12] = volume_up_days

        week_prop = {}
        if week_n > 0:
            week_avg_price = week_total_price / week_n;
            week_price_zhenfu = (week_max_price - week_min_price) / week_min_price;
            week_avg_volume = week_total_volume / week_n;
            week_volume_zhenfu = (week_max_volume - week_min_volume) / week_min_volume;
            week_avg_price = float('%0.2f'%week_avg_price)
            week_price_zhenfu = float('%0.2f'%week_price_zhenfu)
            week_avg_volume = float('%0.2f'%week_avg_volume)
            week_volume_zhenfu = float('%0.2f'%week_volume_zhenfu)
            week_prop['week_max_price'] = week_max_price
            week_prop['week_min_price'] = week_min_price
            week_prop['week_avg_price'] = week_avg_price
            week_prop['week_price_zhenfu'] = week_price_zhenfu
            week_prop['week_max_volume'] = week_max_volume
            week_prop['week_min_volume'] = week_min_volume
            week_prop['week_avg_volume'] = week_avg_volume
            week_prop['week_volume_zhenfu'] = week_volume_zhenfu

            #min_price~max_price~avg_price~zhenfu%|min_volume~max_volume~avg_volume~zhenfu%
            week_str = '%s~%s~%s~%s|%s~%s~%s~%s'%\
                    (str(week_min_price),str(week_max_price),str(week_avg_price),str(week_price_zhenfu),\
                    str(week_min_volume),str(week_max_volume),str(week_avg_volume),str(week_volume_zhenfu))
            todayline[14] = week_str


        month_prop = {}
        if month_n > 0:
            month_avg_price = month_total_price / month_n;
            month_price_zhenfu = (month_max_price - month_min_price) / month_min_price;
            month_avg_volume = month_total_volume / month_n;
            month_volume_zhenfu = (month_max_volume - month_min_volume) / month_min_volume;
            month_avg_price = float('%0.2f'%month_avg_price)
            month_price_zhenfu = float('%0.2f'%month_price_zhenfu)
            month_avg_volume = float('%0.2f'%month_avg_volume)
            month_volume_zhenfu = float('%0.2f'%month_volume_zhenfu)
            month_prop['month_max_price'] = month_max_price
            month_prop['month_min_price'] = month_min_price
            month_prop['month_avg_price'] = month_avg_price
            month_prop['month_price_zhenfu'] = month_price_zhenfu
            month_prop['month_max_volume'] = month_max_volume
            month_prop['month_min_volume'] = month_min_volume
            month_prop['month_avg_volume'] = month_avg_volume
            month_prop['month_volume_zhenfu'] = month_volume_zhenfu


            #min_price~max_price~avg_price~zhenfu%|min_volume~max_volume~avg_volume~zhenfu%
            month_str = '%s~%s~%s~%s|%s~%s~%s~%s'%\
                    (str(month_min_price),str(month_max_price),str(month_avg_price),str(month_price_zhenfu),\
                    str(month_min_volume),str(month_max_volume),str(month_avg_volume),str(month_volume_zhenfu))
            todayline[15] = month_str

        month2_prop = {}
        if month2_n > 0:
            month2_avg_price = month2_total_price / month2_n;
            month2_price_zhenfu = (month2_max_price - month2_min_price) / month2_min_price;
            month2_avg_volume = month2_total_volume / month2_n;
            month2_volume_zhenfu = (month2_max_volume - month2_min_volume) / month2_min_volume;
            month2_avg_price = float('%0.2f'%month2_avg_price)
            month2_price_zhenfu = float('%0.2f'%month2_price_zhenfu)
            month2_avg_volume = float('%0.2f'%month2_avg_volume)
            month2_volume_zhenfu = float('%0.2f'%month2_volume_zhenfu)
            month2_prop['month2_max_price'] = month2_max_price
            month2_prop['month2_min_price'] = month2_min_price
            month2_prop['month2_avg_price'] = month2_avg_price
            month2_prop['month2_price_zhenfu'] = month2_price_zhenfu
            month2_prop['month2_max_volume'] = month2_max_volume
            month2_prop['month2_min_volume'] = month2_min_volume
            month2_prop['month2_avg_volume'] = month2_avg_volume
            month2_prop['month2_volume_zhenfu'] = month2_volume_zhenfu

            month2_str = '%s~%s~%s~%s|%s~%s~%s~%s'%\
                    (str(month2_min_price),str(month2_max_price),str(month2_avg_price),str(month2_price_zhenfu),\
                    str(month2_min_volume),str(month2_max_volume),str(month2_avg_volume),str(month2_volume_zhenfu))

            todayline[16] = month2_str


        todayline[13] = computeBottomIndex(todayline, week_prop, month_prop, month2_prop)


        """
        print code,week_n, week_min_price, week_max_price, week_avg_price, week_price_zhenfu, week_min_volume, week_max_volume, week_avg_volume, week_volume_zhenfu
        print '----'
        print code,month_n, month_min_price, month_max_price, month_avg_price, month_price_zhenfu, month_min_volume, month_max_volume, month_avg_volume, month_volume_zhenfu
        print '----'
        print code,month2_n, month2_min_price, month2_max_price, month2_avg_price, month2_price_zhenfu, month2_min_volume, month2_max_volume, month2_avg_volume, month2_volume_zhenfu
        print '===='
        """

    print "Compute done, sort and save file..."
    ndata = sorted(data, cmp=sort_stock)
    compute_file_path = 'data/%s_%s_volume.txt'%(market,todaystr)
    if IS_TEST:
        compute_file_path = 'data/%s_%s_test_volume.txt'%(market,todaystr)
    compute_file = open(compute_file_path, 'w')
    for d in ndata:

        compute_file.write(d[0]+','+str(d[1])+','+str(d[2])+','+str(d[3])+','+str(d[4])+','+str(d[5])+\
            ',*'+str(d[6])+'*,'+str(d[13])+','+d[14]+','+d[15]+','+d[16]+'\n')
    compute_file.close()

    """
    ndata = sorted(data, cmp=sort_stock4)
    compute_file_path = 'data/%s_%s_volume2.txt'%(market,todaystr)
    if IS_TEST:
        compute_file_path = 'data/%s_%s_test_volume2.txt'%(market,todaystr)
    compute_file = open(compute_file_path, 'w')
    for d in ndata:

        compute_file.write(d[0]+','+str(d[1])+','+str(d[2])+','+str(d[3])+','+str(d[4])+','+str(d[5])+\
            ',*'+str(d[6])+'*,'+str(d[13])+','+d[14]+','+d[15]+','+d[16]+'\n')
    compute_file.close()

    ndata = sorted(data, cmp=sort_stock2)
    compute_file_path = 'data/%s_%s_trend.txt'%(market,todaystr)
    if IS_TEST:
        compute_file_path = 'data/%s_%s_test_trend.txt'%(market,todaystr)
    compute_file = open(compute_file_path, 'w')
    for d in ndata:
        compute_file.write(d[0]+','+str(d[1])+','+str(d[2])+','+str(d[3])+','+str(d[4])+','+str(d[5])+\
            ',*'+str(d[6])+'*,'+str(d[7])+','+str(d[8])+',*'+str(d[9])+'*,*'+str(d[10])+'*,'+str(d[11])+','+str(d[12])+'\n')
    compute_file.close()
    """

    ndata = sorted(data, cmp=sort_stock3)
    compute_file_path = 'data/%s_%s_vp.txt'%(market,todaystr)
    if IS_TEST:
        compute_file_path = 'data/%s_%s_test_vp.txt'%(market,todaystr)
    compute_file = open(compute_file_path, 'w')
    for d in ndata:
        compute_file.write(d[0]+','+str(d[1])+','+str(d[2])+','+str(d[3])+','+str(d[4])+','+str(d[5])+\
            ',*'+str(d[6])+'*,'+str(d[7])+','+str(d[8])+',*'+str(d[9])+'*,*'+str(d[10])+'*,*'+str(d[11])+'*,*'+str(d[12])+'*\n')
    compute_file.close()


"""
计算底部特征指标
"""
def computeBottomIndex(todayline, week_prop, month_prop, month2_prop):
    today_price = float(todayline[2])
    today_volume = float(todayline[4])

    if today_price == 0 or today_volume == 0:
        return 0.0

    if not week_prop.has_key('week_avg_price') or not week_prop.has_key('week_avg_volume'):
        return 0.0

    if week_prop['week_avg_price'] == 0 or week_prop['week_avg_volume'] == 0:
        return 0.0

    week_price_change = abs(today_price - week_prop['week_avg_price']) / week_prop['week_avg_price']
    week_volume_change = (today_volume - week_prop['week_avg_volume']) / week_prop['week_avg_volume']

    month_price_change = abs(today_price - month_prop['month_avg_price']) / month_prop['month_avg_price']
    month_volume_change = (today_volume - month_prop['month_avg_volume']) / month_prop['month_avg_volume']

    month2_price_change = abs(today_price - month2_prop['month2_avg_price']) / month2_prop['month2_avg_price']
    month2_volume_change = (today_volume - month2_prop['month2_avg_volume']) / month2_prop['month2_avg_volume']

    week_month_price_change = abs(week_prop['week_avg_price'] - month_prop['month_avg_price']) / month_prop['month_avg_price']
    week_month_volume_change = (week_prop['week_avg_volume']- month_prop['month_avg_volume']) / month_prop['month_avg_volume']

    if month_price_change < 0.1:
        month_price_change = 0.1

    month_price_zhenfu = month_prop['month_price_zhenfu']
    if month_price_zhenfu < 0.1:
        month_price_zhenfu = 0.1

    price_delta = 1
    if today_price < month_prop['month_avg_price']:
        price_delta = abs(today_price - month_prop['month_avg_price']) / month_prop['month_avg_price']
        if price_delta < 0.1:
            price_delta = 0.1
    elif today_price > month_prop['month_avg_price']:
        price_delta = 1 + abs(today_price - month_prop['month_avg_price']) / month_prop['month_avg_price']
    else:
        price_delta = 1

    #today_month_index = month_volume_change / month_price_change
    today_month_index = month_volume_change / price_delta

    return float('%0.2f'%today_month_index)


"""
排序规则，成交量增幅>价格增幅>换手率
"""
def sort_stock(s1, s2):
    try:
        w1 = s1[6]
        w2 = s2[6]

        if w2 > w1:
            return 1
        elif w2 < w1:
            return -1
        else:
            w1 = float(s1[3].replace('%',''))
            w2 = float(s2[3].replace('%',''))
            if w2 > w1:
                return 1
            elif w2 < w1:
                return -1
            else:
                if s2[5] > s1[5]:
                    return 1
                elif s2[5] < s1[5]:
                    return -1
                else:
                    return 0
    except Exception as e:
        return -1

"""
排序规则，成交量平均增幅>价格平均增幅
"""
def sort_stock2(s1, s2):
    try:
        w1 = float(s1[9])
        w2 = float(s2[9])

        if w2 > w1:
            return 1
        elif w2 < w1:
            return -1
        else:
            w1 = float(s1[10])
            w2 = float(s2[10])
            if w2 > w1:
                return 1
            elif w2 < w1:
                return -1
            else:
                return 0
    except Exception as e:
        return -1

"""
排序规则，价格连续上涨天数>成交量上涨天数
"""
def sort_stock3(s1, s2):
    try:
        w1 = float(s1[11])
        w2 = float(s2[11])

        if w2 > w1:
            return 1
        elif w2 < w1:
            return -1
        else:
            w1 = float(s1[12])
            w2 = float(s2[12])
            if w2 > w1:
                return 1
            elif w2 < w1:
                return -1
            else:
                w1 = float(s1[3].replace('%',''))
                w2 = float(s2[3].replace('%',''))
                if w2 > w1:
                    return 1
                elif w2 < w1:
                    return -1
                else:
                    return sort_stock2(s1,s2)
    except Exception as e:
        return -1

"""
排序规则，底部特征指标
"""
def sort_stock4(s1, s2):
    try:
        w1 = float(s1[13])
        w2 = float(s2[13])

        if w2 > w1:
            return 1
        elif w2 < w1:
            return -1
        else:
            return 0
    except Exception as e:
        return -1


if __name__ == '__main__':
    try:
        #analyze('sh', 'sh_name_list.txt')
        #analyze('sz', 'sz_name_list.txt')
        getFundBList()
    except Exception as e:
        logger.error(str(e) + '\n' + traceback.format_exc())
        print >> sys.stderr, str(e) + '\n' + traceback.format_exc()
        sys.exit(1)
