# Path: kquant_data/config.py
# __CONFIG_DZH_PWR_FILE__ = r"D:\dzh2\Download\PWR\full.PWR"
# 
# __CONFIG_H5_STK_DIVIDEND_DIR__ = os.path.join(__CONFIG_H5_STK_DIR__, 'dividend')
# 
# __CONFIG_TDX_STK_DIR__ = r'D:\new_hbzq'
# 
# __CONFIG_H5_STK_DIR__ = r'D:\DATA_STK'
# 
# Path: kquant_data/stock/stock.py
# def export_dividend_daily_gbbq(gbbq_input, tdx_root, dividend_output, daily_output):
#     """
#     导出除权数据，并同时生成对应的日线数据
#     :param tdx_input:
#     :param dzh_input:
#     :param dzh_output:
#     :param daily_output:
#     :return:
#     """
#     df = pd.read_csv(gbbq_input, index_col=0, dtype={'code': str})
#     # 只取除权信息
#     df = df[df['category'] == 1]
#     df['exchange'] = df['market'].replace(0, "sz").replace(1, 'sh')
#     df['symbol'] = df['exchange'] + df['code']
#     div_list = [(name, group) for name, group in df.groupby(by=['symbol'])]
# 
#     tic()
# 
#     multi = True
#     if multi:
#         # 多进程并行计算
#         pool_size = multiprocessing.cpu_count()
#         if pool_size > 2:
#             pool_size -= 1
#         pool = multiprocessing.Pool(processes=pool_size)
#         func = partial(_export_dividend_from_data, tdx_root, dividend_output, daily_output)
#         pool_outputs = pool.map(func, div_list)
#         print('Pool:', pool_outputs)
#     else:
#         # 单线程
#         for d in div_list:
#             _export_dividend_from_data(tdx_root, dividend_output, daily_output, d)
# 
#     toc()
# 
# Path: kquant_data/stock/symbol.py
# def get_symbols_from_path_only_stock(path, exchange):
#     """
#     指定目录，将目录转成合约列表
#     :param path:
#     :param exchange:
#     :return:
#     """
#     list1 = []
#     list2 = []
#     list3 = []
#     list4 = []
#     list5 = []
#     for dirpath, dirnames, filenames in os.walk(path):
#         for filename in filenames:
#             if is_stock(filename):
#                 list1.append(filename[:8])
#                 list2.append(filename[:2])
#                 list3.append(filename[2:8])
#                 list4.append("%s.%s" % (filename[2:8], exchange))
#                 list5.append("%s.%s" % (filename[2:8], filename[:2].upper()))
# 
#     df = pd.DataFrame({"local_symbol": list1, "market": list2, "code": list3, "symbol": list4, "wind_code": list5})
# 
#     return df


import os
import pandas as pd
from kquant_data.config import __CONFIG_DZH_PWR_FILE__, __CONFIG_H5_STK_DIVIDEND_DIR__, __CONFIG_TDX_STK_DIR__, \
    __CONFIG_H5_STK_DIR__
from kquant_data.stock.stock import export_dividend_daily_gbbq
from kquant_data.stock.symbol import get_symbols_from_path_only_stock


def export_daily():
    """
    只导出有除权信息的股票，没有导出的数据，以后直接读通达信
    :return:
    """
    # 复权因子的导出
    dividend_input = os.path.join(__CONFIG_H5_STK_DIR__, 'gbbq.csv')
    dividend_output = __CONFIG_H5_STK_DIVIDEND_DIR__
    daily_input = os.path.join(__CONFIG_TDX_STK_DIR__, "vipdoc")
    daily_output = os.path.join(__CONFIG_H5_STK_DIR__, "1day")
    # export_dividend_daily(dividend_input, daily_input, dividend_output, daily_output)
    export_dividend_daily_gbbq(dividend_input, daily_input, dividend_output, daily_output)


def export_symbols():
    path = os.path.join(__CONFIG_TDX_STK_DIR__, "vipdoc", 'sh', 'lday')
    df_sh = get_symbols_from_path_only_stock(path, "SSE")
    path = os.path.join(__CONFIG_TDX_STK_DIR__, "vipdoc", 'sz', 'lday')
    df_sz = get_symbols_from_path_only_stock(path, "SZSE")
    df = pd.concat([df_sh, df_sz])

    return df


if __name__ == '__main__':
    # 导出合约信息，要有合约的名字
    list = export_symbols()
    # 这个保存相当重要，因为日线数据没有除权信息的就不转换了

# Path: kquant_data/config.py
# __CONFIG_DZH_PWR_FILE__ = r"D:\dzh2\Download\PWR\full.PWR"
# 
# __CONFIG_H5_STK_DIVIDEND_DIR__ = os.path.join(__CONFIG_H5_STK_DIR__, 'dividend')
# 
# __CONFIG_TDX_STK_DIR__ = r'D:\new_hbzq'
# 
# __CONFIG_H5_STK_DIR__ = r'D:\DATA_STK'
# 
# Path: kquant_data/stock/stock.py
# def export_dividend_daily_gbbq(gbbq_input, tdx_root, dividend_output, daily_output):
#     """
#     导出除权数据，并同时生成对应的日线数据
#     :param tdx_input:
#     :param dzh_input:
#     :param dzh_output:
#     :param daily_output:
#     :return:
#     """
#     df = pd.read_csv(gbbq_input, index_col=0, dtype={'code': str})
#     # 只取除权信息
#     df = df[df['category'] == 1]
#     df['exchange'] = df['market'].replace(0, "sz").replace(1, 'sh')
#     df['symbol'] = df['exchange'] + df['code']
#     div_list = [(name, group) for name, group in df.groupby(by=['symbol'])]
# 
#     tic()
# 
#     multi = True
#     if multi:
#         # 多进程并行计算
#         pool_size = multiprocessing.cpu_count()
#         if pool_size > 2:
#             pool_size -= 1
#         pool = multiprocessing.Pool(processes=pool_size)
#         func = partial(_export_dividend_from_data, tdx_root, dividend_output, daily_output)
#         pool_outputs = pool.map(func, div_list)
#         print('Pool:', pool_outputs)
#     else:
#         # 单线程
#         for d in div_list:
#             _export_dividend_from_data(tdx_root, dividend_output, daily_output, d)
# 
#     toc()
# 
# Path: kquant_data/stock/symbol.py
# def get_symbols_from_path_only_stock(path, exchange):
#     """
#     指定目录，将目录转成合约列表
#     :param path:
#     :param exchange:
#     :return:
#     """
#     list1 = []
#     list2 = []
#     list3 = []
#     list4 = []
#     list5 = []
#     for dirpath, dirnames, filenames in os.walk(path):
#         for filename in filenames:
#             if is_stock(filename):
#                 list1.append(filename[:8])
#                 list2.append(filename[:2])
#                 list3.append(filename[2:8])
#                 list4.append("%s.%s" % (filename[2:8], exchange))
#                 list5.append("%s.%s" % (filename[2:8], filename[:2].upper()))
# 
#     df = pd.DataFrame({"local_symbol": list1, "market": list2, "code": list3, "symbol": list4, "wind_code": list5})
# 
#     return df


# Path: demo_stock/A_1day/B02_export.py
import os
import pandas as pd
from kquant_data.config import __CONFIG_DZH_PWR_FILE__, __CONFIG_H5_STK_DIVIDEND_DIR__, __CONFIG_TDX_STK_DIR__, \
    __CONFIG_H5_STK_DIR__
from kquant_data.stock.stock import export_dividend_daily_gbbq
from kquant_data.stock.symbol import get_symbols_from_path_only_stock



def export_daily():
    """
    只导出有除权信息的股票，没有导出的数据，以后直接读通达信
    :return:
    """
    # 复权因子的导出
    dividend_input = os.path.join(__CONFIG_H5_STK_DIR__, 'gbbq.csv')
    dividend_output = __CONFIG_H5_STK_DIVIDEND_DIR__
    daily_input = os.path.join(__CONFIG_TDX_STK_DIR__, "vipdoc")
    daily_output = os.path.join(__CONFIG_H5_STK_DIR__, "1day")
    # export_dividend_daily(dividend_input, daily_input, dividend_output, daily_output)
    export_dividend_daily_gbbq(dividend_input, daily_input, dividend_output, daily_output)


def export_symbols():
    path = os.path.join(__CONFIG_TDX_STK_DIR__, "vipdoc", 'sh', 'lday')
    df_sh = get_symbols_from_path_only_stock(path, "SSE")
    path = os.path.join(__CONFIG_TDX_STK_DIR__, "vipdoc", 'sz', 'lday')
    df_sz = get_symbols_from_path_only_stock(path, "SZSE")
    df = pd.concat([df_sh, df_sz])

    return df


if __name__ == '__main__':
    # 导出合约信息，要有合约的名字
    list = export_symbols()
    # 这个保存相当重要，因为日线数据没有除权信息的就不转换了

    path = os.path.join(__CONFIG_H5_STK_DIR__, "1day", 'Symbol.csv')