# -*- encoding:utf-8 -*-
import numpy as np
from keras.models import load_model
import joblib


def read_data(path):
    lines = []
    with open(path) as f:
        for line in f.readlines()[:]:
            line = eval(line.strip())
            if line[-2][0].startswith('0') or line[-2][0].startswith('3'):
                lines.append(line)

    size = len(lines[0])
    train_x=[s[:size - 2] for s in lines]
    train_y=[s[size-1] for s in lines]
    return np.array(train_x),np.array(train_y),lines

def _score(fact, line):
    with open('dnn_predict_dmi_18d.txt', 'a') as f:
        f.write(str([line[-2], line[-1]]) + "\n")

    up_right = 0
    up_error = 0

    if fact[0] == 1:
        up_right = up_right + 1.12
    elif fact[1] == 1:
        up_right = up_right + 1.06
    elif fact[2] == 1:
        up_right = up_right + 1
    elif fact[3] == 1:
        up_right = up_right + 0.94
    else:
        up_error = up_error + 1
        up_right = up_right + 0.88
    return up_right,up_error


def predict(file_path='', model_path='15min_dnn_seq.h5', idx=-1):
    test_x,test_y,lines=read_data(file_path)

    model=load_model(model_path)
    score = model.evaluate(test_x, test_y)
    print('DNN', score)

    up_num = 0
    up_error = 0
    up_right = 0
    down_num = 0
    down_error = 0
    down_right = 0
    i = 0
    result=model.predict(test_x)
    win_dnn = []
    for r in result:
        fact = test_y[i]

        if idx in [-2]:
            if r[0] > 0.5 or r[1] > 0.5:
                pass
                # if fact[0] == 1:
                #     up_right = up_right + 1.12
                # elif fact[1] == 1:
                #     up_right = up_right + 1.06
                # elif fact[2] == 1:
                #     up_right = up_right + 1
                # elif fact[3] == 1:
                #     up_right = up_right + 0.94
                # else:
                #     up_error = up_error + 1
                #     up_right = up_right + 0.88
                # up_num = up_num + 1
        else:
            if r[0] > 0.6 or r[1] > 0.6:
                tmp_right,tmp_error = _score(fact, lines[i])
                up_right = tmp_right + up_right
                up_error = tmp_error + up_error
                up_num = up_num + 1
            elif r[3] > 0.5 or r[4] > 0.5:
                if fact[0] == 1:
                    down_error = down_error + 1
                    down_right = down_right + 1.12
                elif fact[1] == 1:
                    down_right = down_right + 1.06
                elif fact[2] == 1:
                    down_right = down_right + 1
                elif fact[3] == 1:
                    down_right = down_right + 0.94
                else:
                    down_right = down_right + 0.88
                down_num = down_num + 1

        i = i + 1
    if up_num == 0:
        up_num = 1
    if down_num == 0:
        down_num = 1
    print('DNN', up_right, up_num, up_right/up_num, up_error/up_num, down_right/down_num, down_error/down_num)
    return win_dnn,up_right/up_num,down_right/down_num


def multi_predict():
    r = 0;
    p = 0
    for x in range(0, 12): # 0,2,3,4,6,8,9,10,11
    # for x in [5,9,11,0,3,4,8]: #10_18,0没数据需要重新计算 [0,2,3,4,5,9,10,11]
    # for x in [0,1,10]:
    # for x in [2,4,7,10]: # 2表现最好 优秀的 0,8正确的反向指标,(9错误的反向指标 样本量太少)
        print(x)
    # for x in [0,2,5,6,7]: # 5表现最好
        win_dnn, up_ratio,down_ratio = predict(file_path='D:\\data\\quantization\\kmeans\\stock13_18d_test_' + str(x) + '.log',
                                               model_path='13_18d_dnn_seq_' + str(x) + '.h5', idx=x)
        r = r + up_ratio
        p = p + down_ratio
    print(r, p)

import pymongo
from util.mongodb import get_mongo_table_instance
code_table = get_mongo_table_instance('tushare_code')
k_table = get_mongo_table_instance('stock_day_k')

industry = ['家用电器', '元器件', 'IT设备', '汽车服务',
            '汽车配件', '软件服务',
            '互联网', '纺织',
            '塑料', '半导体',]


def predict_today(day, model='10_18d'):
    lines = []
    with open('D:\\data\\quantization\\stock' + model + '_' +  str(day) +'.log') as f:
        for line in f.readlines()[:]:
            line = eval(line.strip())
            # if line[-1][0].startswith('0') or line[-1][0].startswith('3'):
            lines.append(line)

    size = len(lines[0])
    train_x=[s[:size - 1] for s in lines]
    np.array(train_x)

    estimator = joblib.load('km_dmi_18.pkl')

    models = []
    for x in range(0, 12):
        models.append(load_model(model + '_dnn_seq_' + str(x) + '.h5'))

    x = 24 # 每条数据项数
    k = 18 # 周期
    for line in lines:
        v = line[1:x*k + 1]
        v = np.array(v)
        v = v.reshape(k, x)
        v = v[:,4:8]
        v = v.reshape(1, 4*k)
        # print(v)
        r = estimator.predict(v)

        # if r[0] in [1,6,10]:
        #     train_x = np.array([line[:size - 1]])
        #
        #     result = models[r[0]].predict(train_x)
        #     if result[0][3] > 0.5 or result[0][4] > 0.5:
        #         stock = code_table.find_one({'ts_code':line[-1][0]})
        #         if stock['name'].startswith('ST') or stock['name'].startswith('N') or stock['name'].startswith('*'):
        #             continue
        #         if line[0] > 80:
        #             continue
        #         if stock['industry'] in industry:
        #             pass
        #             # print(line[-1], stock['name'], stock['industry'], 'sell')

        if r[0] in [2,5,9,10,11]:
            train_x = np.array([line[:size - 1]])

            result = models[r[0]].predict(train_x)
            # print(result, line[-1])
            if result[0][0] > 0.6 or result[0][1] > 0.6:
                if line[-1][0].startswith('688'):
                    continue
                # 去掉ST
                stock = code_table.find_one({'ts_code':line[-1][0]})
                if stock['name'].startswith('ST') or stock['name'].startswith('N') or stock['name'].startswith('*'):
                    continue

                # 跌的
                k_table_list = list(k_table.find({'code':line[-1][0], 'tradeDate':{'$lte':day}}).sort("tradeDate", pymongo.DESCENDING).limit(5))
                if k_table_list[0]['close'] > k_table_list[-1]['close']*1.20:
                    continue
                if k_table_list[0]['close'] < k_table_list[-1]['close']*0.90:
                    continue
                if k_table_list[-1]['close'] > 80:
                    continue

                # 指定某几个行业
                # if stock['industry'] in industry:
                print(line[-1], stock['name'], stock['industry'], 'buy')


if __name__ == '__main__':
    # predict(file_path='D:\\data\\quantization\\stock6_5_test.log', model_path='5d_dnn_seq.h5')
    # predict(file_path='D:\\data\\quantization\\stock6_test.log', model_path='15m_dnn_seq.h5')
    multi_predict()
    # predict_today(20200229, model='11_18d')