# Path: kquant_data/api.py
# def get_price(symbols, instrument_type, start_date=None, end_date=None, bar_size=86400, fields=None, path=None,
#               new_symbols=None):
#     """
#     从通达信中取数据，没有除权的可以直接读，但除权的需要拿到除权因子才能用
#     这样的话相当于日线需要转一次，所以可以在本地为日线存一次HDF5
#     通达信中目前只有日线、5分钟、1分钟
#     :param symbols:
#     :param instrument_type:
#     :param start_date:
#     :param end_date:
#     :param bar_size:
#     :param fields:
#     :param path:
#     :param new_symbols: 50ETF期权需要将数字转成符号，方便使用
#     :return:
#     """
#     # 将str转成list
#     if isinstance(symbols, str):
#         symbols = [symbols]
#     if isinstance(fields, str):
#         fields = [fields]
#     if new_symbols is None:
#         new_symbols = symbols
#     if isinstance(new_symbols, str):
#         new_symbols = [new_symbols]
# 
#     _dict = collections.OrderedDict()
#     _fields = None
#     for idx, symbol in enumerate(symbols):
#         code_market = split_by_dot(symbol)
#         if len(code_market) == 2:
#             code, market = code_market
#         else:
#             code, market = code_market[0], ''
# 
#         df = _get_date_from_file(symbol, market, code, bar_size, start_date, end_date, fields, path, instrument_type)
#         _fields = df.columns
#         _dict[new_symbols[idx]] = df
# 
#     # 只有一个合约，直接输出单个合约的表
#     if len(_dict) == 1:
#         return list(_dict.values())[0]
# 
#     # 只有一个字段时，输出每个合约的列表
#     if len(_fields) == 1:
#         dfs = None
#         for (k, v) in _dict.items():
#             if dfs is None:
#                 dfs = v
#             else:
#                 dfs = pd.merge(dfs, v, left_index=True, right_index=True, how='outer')
#         dfs.columns = list(_dict.keys())
#         # 需要将不同合约的进行合并
#         return dfs
# 
#     # 三维的，需要转换成Panel
#     # 是否可以使用 pan = pan_.transpose(2,1,0)，发现不能用，因为没有对齐
#     _dict2 = collections.OrderedDict()
#     for (k, v) in _dict.items():
#         for c in v.columns:
#             _df = pd.DataFrame(v[c])
#             if c in _dict2:
#                 _dict2[c] = pd.merge(_dict2[c], _df, left_index=True, right_index=True, how='outer')
#             else:
#                 _dict2[c] = _df
# 
#     for (k, v) in _dict2.items():
#         v.columns = new_symbols
#         # v.columns = list(_dict.keys())
# 
#     return pd.Panel(_dict2)
# 
# Path: kquant_data/option/info.py
# def get_opt_info(filename):
#     root_path = os.path.join(__CONFIG_H5_OPT_DIR__, 'optioncontractbasicinfo', filename)
#     df_info = read_optioncontractbasicinfo(root_path)
#     # 排序一下，方便显示，先按月份，然再换名后的月份
#     df_info = df_info.sort_values(by=['limit_month', 'limit_month_m', 'call_or_put', 'exercise_price'])
#     return df_info
# 
# Path: kquant_data/config.py
# __CONFIG_H5_OPT_DIR__ = r'D:\DATA_OPT'
# 
# __CONFIG_TDX_STK_DIR__ = r'D:\new_hbzq'


import os
import pandas as pd
from kquant_data.api import get_price
from kquant_data.option.info import get_opt_info
from kquant_data.config import __CONFIG_H5_OPT_DIR__, __CONFIG_TDX_STK_DIR__
    df_volume = None
    for wind_code in wind_codes:
        print(wind_code)
        try:
            df_volume_ = get_price(wind_code, path=path, instrument_type='option',
                                   fields=['Volume'])
        except:
            continue
        if df_volume is None:
            df_volume = df_volume_
        else:
            # 将所有数据合并成一个DataFrame太占用内存，读一个计算一个更快
            df_volume = df_volume.add(df_volume_, fill_value=0)
    path_csv = os.path.join(__CONFIG_H5_OPT_DIR__, 'data', 'call.csv')
    df_volume.to_csv(path_csv)

    wind_codes = list(set(df_info_put['wind_code']))
    wind_codes.sort()
    df_volume = None
    for wind_code in wind_codes:
        print(wind_code)
        try:
            df_volume_ = get_price(wind_code, path=path, instrument_type='option',
                                   fields=['Volume'])
        except:
            continue
        if df_volume is None:
            df_volume = df_volume_
        else:
            df_volume = df_volume.add(df_volume_, fill_value=0)

# Path: kquant_data/api.py
# def get_price(symbols, instrument_type, start_date=None, end_date=None, bar_size=86400, fields=None, path=None,
#               new_symbols=None):
#     """
#     从通达信中取数据，没有除权的可以直接读，但除权的需要拿到除权因子才能用
#     这样的话相当于日线需要转一次，所以可以在本地为日线存一次HDF5
#     通达信中目前只有日线、5分钟、1分钟
#     :param symbols:
#     :param instrument_type:
#     :param start_date:
#     :param end_date:
#     :param bar_size:
#     :param fields:
#     :param path:
#     :param new_symbols: 50ETF期权需要将数字转成符号，方便使用
#     :return:
#     """
#     # 将str转成list
#     if isinstance(symbols, str):
#         symbols = [symbols]
#     if isinstance(fields, str):
#         fields = [fields]
#     if new_symbols is None:
#         new_symbols = symbols
#     if isinstance(new_symbols, str):
#         new_symbols = [new_symbols]
# 
#     _dict = collections.OrderedDict()
#     _fields = None
#     for idx, symbol in enumerate(symbols):
#         code_market = split_by_dot(symbol)
#         if len(code_market) == 2:
#             code, market = code_market
#         else:
#             code, market = code_market[0], ''
# 
#         df = _get_date_from_file(symbol, market, code, bar_size, start_date, end_date, fields, path, instrument_type)
#         _fields = df.columns
#         _dict[new_symbols[idx]] = df
# 
#     # 只有一个合约，直接输出单个合约的表
#     if len(_dict) == 1:
#         return list(_dict.values())[0]
# 
#     # 只有一个字段时，输出每个合约的列表
#     if len(_fields) == 1:
#         dfs = None
#         for (k, v) in _dict.items():
#             if dfs is None:
#                 dfs = v
#             else:
#                 dfs = pd.merge(dfs, v, left_index=True, right_index=True, how='outer')
#         dfs.columns = list(_dict.keys())
#         # 需要将不同合约的进行合并
#         return dfs
# 
#     # 三维的，需要转换成Panel
#     # 是否可以使用 pan = pan_.transpose(2,1,0)，发现不能用，因为没有对齐
#     _dict2 = collections.OrderedDict()
#     for (k, v) in _dict.items():
#         for c in v.columns:
#             _df = pd.DataFrame(v[c])
#             if c in _dict2:
#                 _dict2[c] = pd.merge(_dict2[c], _df, left_index=True, right_index=True, how='outer')
#             else:
#                 _dict2[c] = _df
# 
#     for (k, v) in _dict2.items():
#         v.columns = new_symbols
#         # v.columns = list(_dict.keys())
# 
#     return pd.Panel(_dict2)
# 
# Path: kquant_data/option/info.py
# def get_opt_info(filename):
#     root_path = os.path.join(__CONFIG_H5_OPT_DIR__, 'optioncontractbasicinfo', filename)
#     df_info = read_optioncontractbasicinfo(root_path)
#     # 排序一下，方便显示，先按月份，然再换名后的月份
#     df_info = df_info.sort_values(by=['limit_month', 'limit_month_m', 'call_or_put', 'exercise_price'])
#     return df_info
# 
# Path: kquant_data/config.py
# __CONFIG_H5_OPT_DIR__ = r'D:\DATA_OPT'
# 
# __CONFIG_TDX_STK_DIR__ = r'D:\new_hbzq'


# Path: demo_option/B01_50etf_put_call_ratio.py
import os
import pandas as pd
from kquant_data.api import get_price
from kquant_data.option.info import get_opt_info
from kquant_data.config import __CONFIG_H5_OPT_DIR__, __CONFIG_TDX_STK_DIR__

    df_volume = None
    for wind_code in wind_codes:
        print(wind_code)
        try:
            df_volume_ = get_price(wind_code, path=path, instrument_type='option',
                                   fields=['Volume'])
        except:
            continue
        if df_volume is None:
            df_volume = df_volume_
        else:
            # 将所有数据合并成一个DataFrame太占用内存，读一个计算一个更快
            df_volume = df_volume.add(df_volume_, fill_value=0)
    path_csv = os.path.join(__CONFIG_H5_OPT_DIR__, 'data', 'call.csv')
    df_volume.to_csv(path_csv)

    wind_codes = list(set(df_info_put['wind_code']))
    wind_codes.sort()
    df_volume = None
    for wind_code in wind_codes:
        print(wind_code)
        try:
            df_volume_ = get_price(wind_code, path=path, instrument_type='option',
                                   fields=['Volume'])
        except:
            continue
        if df_volume is None:
            df_volume = df_volume_
        else:
            df_volume = df_volume.add(df_volume_, fill_value=0)

    path_csv = os.path.join(__CONFIG_H5_OPT_DIR__, 'data', 'put.csv')