# Path: kquant_data/stock/tdx.py
# def read_file(path, instrument_type='stock'):
#     """
#     http://www.tdx.com.cn/list_66_68.html
#     通达信本地目录有day/lc1/lc5三种后缀名，两种格式
#     从通达信官网下载的5分钟后缀只有5这种格式，为了处理方便，时间精度都只到分钟
#     :param path:
#     :return:
#     """
#     columns = ['DateTime', 'Open', 'High', 'Low', 'Close', 'Amount', 'Volume', 'na']
# 
#     file_ext = os.path.splitext(path)[1][1:]
#     if instrument_type == 'stock':
#         ohlc_type = {'day': 'i4', '5': 'i4', 'lc1': 'f4', 'lc5': 'f4'}[file_ext]
#         formats = ['i4'] + [ohlc_type] * 4 + ['f4'] + ['i4'] * 2
#     elif instrument_type == 'option':
#         ohlc_type = {'day': 'f4', '5': 'i4', 'lc1': 'f4', 'lc5': 'f4'}[file_ext]
#         formats = ['i4'] + [ohlc_type] * 4 + ['i4'] + ['i4'] * 2
#     date_parser = {'day': day_datetime_long,
#                    '5': min_datetime_long,
#                    'lc1': min_datetime_long,
#                    'lc5': min_datetime_long,
#                    }[file_ext]
# 
#     dtype = np.dtype({'names': columns, 'formats': formats})
#     data = np.fromfile(path, dtype=dtype)
#     df = pd.DataFrame(data)
#     # 为了处理的方便，存一套long类型的时间
#     df['DateTime'] = df['DateTime'].apply(date_parser)
#     df['datetime'] = df['DateTime'].apply(yyyyMMddHHmm_2_datetime)
#     df = df.set_index('datetime')
#     df = df.drop('na', 1)
# 
#     # 有两种格式的数据的价格需要调整
#     if instrument_type == 'stock':
#         if file_ext == 'day' or file_ext == '5':
#             tmp = df.tail(10)
#             r = tmp.Amount / tmp.Volume / tmp.Close
#             # 为了解决价格扩大了多少倍的问题
#             type_unit = np.power(10, np.round(np.log10(r))).median()
#             # 这个地方要考虑到实际情况，不要漏价格，也不要把时间做了除法
#             df.ix[:, 1:5] = df.ix[:, 1:5] * type_unit
# 
#     # 转换格式，占用内存更少
#     df['DateTime'] = df['DateTime'].astype(np.uint64)
#     df['Open'] = df['Open'].astype(np.float32)
#     df['High'] = df['High'].astype(np.float32)
#     df['Low'] = df['Low'].astype(np.float32)
#     df['Close'] = df['Close'].astype(np.float32)
#     df['Amount'] = df['Amount'].astype(np.float32)
#     df['Volume'] = df['Volume'].astype(np.uint32)
# 
#     # print(df.dtypes)
# 
#     return df


import os
import numpy as np
import pandas as pd
import struct
from ctypes import create_string_buffer
from kquant_data.stock.tdx import read_file
    #
    columns = ['DateTime', 'Open', 'High', 'Low', 'Close', 'Amount', 'Volume', 'na']
    tmp = pd.DataFrame(data[-10:], columns=columns)
    # 要是没有成交额就惨了
    r = tmp.Amount / tmp.Volume / tmp.Close
    # 为了解决价格扩大了多少倍的问题
    type_unit = np.power(10, np.round(np.log10(r))).median()

    data = list(map(int_to_float, data, [type_unit] * len(data)))

    with open(output_file, 'wb') as f:
        pack_records(formats_lc5, data, f)


if __name__ == '__main__':
    # 先将数据转换格式，然后手工复制即可
    # 以下代码可以复制两次，sh与sz分别处理即可
    input_path = r'D:\test\\5'
    output_path = r'D:\test\\lc5'
    for dirpath, dirnames, filenames in os.walk(input_path, topdown=True):
        for filename in filenames:
            shotname, extension = os.path.splitext(filename)
            if extension != '.5':
                continue
            input_filname = os.path.join(input_path, filename)
            ouput_filname = os.path.join(output_path, '%s.lc5' % shotname)
            min_5_to_lc5(input_filname, ouput_filname)
            print(ouput_filname)
            if True:
                df_5 = read_file(input_filname)

# Path: kquant_data/stock/tdx.py
# def read_file(path, instrument_type='stock'):
#     """
#     http://www.tdx.com.cn/list_66_68.html
#     通达信本地目录有day/lc1/lc5三种后缀名，两种格式
#     从通达信官网下载的5分钟后缀只有5这种格式，为了处理方便，时间精度都只到分钟
#     :param path:
#     :return:
#     """
#     columns = ['DateTime', 'Open', 'High', 'Low', 'Close', 'Amount', 'Volume', 'na']
# 
#     file_ext = os.path.splitext(path)[1][1:]
#     if instrument_type == 'stock':
#         ohlc_type = {'day': 'i4', '5': 'i4', 'lc1': 'f4', 'lc5': 'f4'}[file_ext]
#         formats = ['i4'] + [ohlc_type] * 4 + ['f4'] + ['i4'] * 2
#     elif instrument_type == 'option':
#         ohlc_type = {'day': 'f4', '5': 'i4', 'lc1': 'f4', 'lc5': 'f4'}[file_ext]
#         formats = ['i4'] + [ohlc_type] * 4 + ['i4'] + ['i4'] * 2
#     date_parser = {'day': day_datetime_long,
#                    '5': min_datetime_long,
#                    'lc1': min_datetime_long,
#                    'lc5': min_datetime_long,
#                    }[file_ext]
# 
#     dtype = np.dtype({'names': columns, 'formats': formats})
#     data = np.fromfile(path, dtype=dtype)
#     df = pd.DataFrame(data)
#     # 为了处理的方便，存一套long类型的时间
#     df['DateTime'] = df['DateTime'].apply(date_parser)
#     df['datetime'] = df['DateTime'].apply(yyyyMMddHHmm_2_datetime)
#     df = df.set_index('datetime')
#     df = df.drop('na', 1)
# 
#     # 有两种格式的数据的价格需要调整
#     if instrument_type == 'stock':
#         if file_ext == 'day' or file_ext == '5':
#             tmp = df.tail(10)
#             r = tmp.Amount / tmp.Volume / tmp.Close
#             # 为了解决价格扩大了多少倍的问题
#             type_unit = np.power(10, np.round(np.log10(r))).median()
#             # 这个地方要考虑到实际情况，不要漏价格，也不要把时间做了除法
#             df.ix[:, 1:5] = df.ix[:, 1:5] * type_unit
# 
#     # 转换格式，占用内存更少
#     df['DateTime'] = df['DateTime'].astype(np.uint64)
#     df['Open'] = df['Open'].astype(np.float32)
#     df['High'] = df['High'].astype(np.float32)
#     df['Low'] = df['Low'].astype(np.float32)
#     df['Close'] = df['Close'].astype(np.float32)
#     df['Amount'] = df['Amount'].astype(np.float32)
#     df['Volume'] = df['Volume'].astype(np.uint32)
# 
#     # print(df.dtypes)
# 
#     return df


# Path: demo_stock/B_5min/C01_conver_5_to_lc5.py
import os
import numpy as np
import pandas as pd
import struct
from ctypes import create_string_buffer
from kquant_data.stock.tdx import read_file

    #
    columns = ['DateTime', 'Open', 'High', 'Low', 'Close', 'Amount', 'Volume', 'na']
    tmp = pd.DataFrame(data[-10:], columns=columns)
    # 要是没有成交额就惨了
    r = tmp.Amount / tmp.Volume / tmp.Close
    # 为了解决价格扩大了多少倍的问题
    type_unit = np.power(10, np.round(np.log10(r))).median()

    data = list(map(int_to_float, data, [type_unit] * len(data)))

    with open(output_file, 'wb') as f:
        pack_records(formats_lc5, data, f)


if __name__ == '__main__':
    # 先将数据转换格式，然后手工复制即可
    # 以下代码可以复制两次，sh与sz分别处理即可
    input_path = r'D:\test\\5'
    output_path = r'D:\test\\lc5'
    for dirpath, dirnames, filenames in os.walk(input_path, topdown=True):
        for filename in filenames:
            shotname, extension = os.path.splitext(filename)
            if extension != '.5':
                continue
            input_filname = os.path.join(input_path, filename)
            ouput_filname = os.path.join(output_path, '%s.lc5' % shotname)
            min_5_to_lc5(input_filname, ouput_filname)
            print(ouput_filname)
            if True:
                df_5 = read_file(input_filname)

                df_lc5 = read_file(ouput_filname)