# Path: kquant_data/config.py
# __CONFIG_H5_OPT_DIR__ = r'D:\DATA_OPT'
# 
# __CONFIG_H5_STK_DIR__ = r'D:\DATA_STK'
# 
# 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/utils/xdatetime.py
# def yyyyMMddHHmm_2_datetime(dt):
#     """
#     输入一个长整型yyyyMMddhmm，返回对应的时间
#     :param dt:
#     :return:
#     """
#     dt = int(dt)  # FIXME：在python2下会有问题吗？
#     (yyyyMMdd, hh) = divmod(dt, 10000)
#     (yyyy, MMdd) = divmod(yyyyMMdd, 10000)
#     (MM, dd) = divmod(MMdd, 100)
#     (hh, mm) = divmod(hh, 100)
# 
#     return datetime(yyyy, MM, dd, hh, mm)
# 
# Path: kquant_data/xio/csv.py
# def write_data_dataframe(path, df, date_format='%Y-%m-%d'):
#     """
#     写入数据
#     :param path:
#     :param df:
#     :return:
#     """
#     df.to_csv(path, date_format=date_format, encoding='utf-8-sig')
# 
# Path: kquant_data/wind/wset.py
# def download_optionchain(w, date='2017-11-28', us_code='510050.SH',
#                          field='option_code,option_name,strike_price,multiplier'):
#     """
#     下载指定日期期权数据
# 
# w_wset_data = vba_wset("optionchain","date=2017-11-28;us_code=510050.SH;option_var=全部;call_put=全部;field=option_code,option_name,strike_price,multiplier",)
#     :param w:
#     :param windcode:
#     :param date:
#     :return:
#     """
#     param = 'date=%s' % date
#     param += ';us_code=%s' % us_code
#     if field:
#         param += ';field=%s' % field
# 
#     w.asDateTime = asDateTime
#     w_wset_data = w.wset("optionchain", param)
#     df = pd.DataFrame(w_wset_data.Data)
#     df = df.T
#     df.columns = w_wset_data.Fields
#     return df


from WindPy import w
from kquant_data.config import __CONFIG_H5_OPT_DIR__, __CONFIG_H5_STK_DIR__
from kquant_data.option.info import get_opt_info
from kquant_data.utils.xdatetime import yyyyMMddHHmm_2_datetime
from kquant_data.xio.csv import write_data_dataframe
from kquant_data.wind.wset import download_optionchain
import os
import pandas as pd
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
下载合约乘数和执行价
"""



def get_sector_at(df_info, date):
    idx = (df_info['listed_date'] <= date) & (df_info['expire_date'] >= date)
    df_info2 = df_info[idx]
    if df_info2.empty:
        return None
    # print(df_info2)
    return df_info2


def download_exe_price(w, sector, date):
    if sector is None:
        return

    path = os.path.join(__CONFIG_H5_OPT_DIR__, 'optionchain', '510050.SH', '%s.csv' % date)
    if os.path.exists(path):
        return
    print('准备下载数据')
    df = download_optionchain(w, date, '510050.SH')

# Path: kquant_data/config.py
# __CONFIG_H5_OPT_DIR__ = r'D:\DATA_OPT'
# 
# __CONFIG_H5_STK_DIR__ = r'D:\DATA_STK'
# 
# 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/utils/xdatetime.py
# def yyyyMMddHHmm_2_datetime(dt):
#     """
#     输入一个长整型yyyyMMddhmm，返回对应的时间
#     :param dt:
#     :return:
#     """
#     dt = int(dt)  # FIXME：在python2下会有问题吗？
#     (yyyyMMdd, hh) = divmod(dt, 10000)
#     (yyyy, MMdd) = divmod(yyyyMMdd, 10000)
#     (MM, dd) = divmod(MMdd, 100)
#     (hh, mm) = divmod(hh, 100)
# 
#     return datetime(yyyy, MM, dd, hh, mm)
# 
# Path: kquant_data/xio/csv.py
# def write_data_dataframe(path, df, date_format='%Y-%m-%d'):
#     """
#     写入数据
#     :param path:
#     :param df:
#     :return:
#     """
#     df.to_csv(path, date_format=date_format, encoding='utf-8-sig')
# 
# Path: kquant_data/wind/wset.py
# def download_optionchain(w, date='2017-11-28', us_code='510050.SH',
#                          field='option_code,option_name,strike_price,multiplier'):
#     """
#     下载指定日期期权数据
# 
# w_wset_data = vba_wset("optionchain","date=2017-11-28;us_code=510050.SH;option_var=全部;call_put=全部;field=option_code,option_name,strike_price,multiplier",)
#     :param w:
#     :param windcode:
#     :param date:
#     :return:
#     """
#     param = 'date=%s' % date
#     param += ';us_code=%s' % us_code
#     if field:
#         param += ';field=%s' % field
# 
#     w.asDateTime = asDateTime
#     w_wset_data = w.wset("optionchain", param)
#     df = pd.DataFrame(w_wset_data.Data)
#     df = df.T
#     df.columns = w_wset_data.Fields
#     return df


# Path: demo_option/A02_download_multiplier.py
from WindPy import w
from kquant_data.config import __CONFIG_H5_OPT_DIR__, __CONFIG_H5_STK_DIR__
from kquant_data.option.info import get_opt_info
from kquant_data.utils.xdatetime import yyyyMMddHHmm_2_datetime
from kquant_data.xio.csv import write_data_dataframe
from kquant_data.wind.wset import download_optionchain
import os
import pandas as pd

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
下载合约乘数和执行价
"""



def get_sector_at(df_info, date):
    idx = (df_info['listed_date'] <= date) & (df_info['expire_date'] >= date)
    df_info2 = df_info[idx]
    if df_info2.empty:
        return None
    # print(df_info2)
    return df_info2


def download_exe_price(w, sector, date):
    if sector is None:
        return

    path = os.path.join(__CONFIG_H5_OPT_DIR__, 'optionchain', '510050.SH', '%s.csv' % date)
    if os.path.exists(path):
        return
    print('准备下载数据')
    df = download_optionchain(w, date, '510050.SH')

    write_data_dataframe(path, df)