# Path: kquant_data/option/info.py
# def get_opt_info_filtered_by_date(df_info, date):
#     # date = '2018-03-04'  # 指定日期
#     # call_or_put = 'C'  # 指定类别
#     # limit_month = 1806  # 指定月份
#     # limit_month_m = 'A'  # 指定月份2
#     df_filtered = df_info[(df_info['listed_date'] <= date)
#                           & (df_info['expire_date'] >= date)]
#     return df_filtered
# 
# def get_opt_info_filtered_by_month(df_info, limit_month):
#     df_filtered = df_info[
#         (df_info['limit_month'] == limit_month)]
#     return df_filtered
# 
# def get_opt_info_filtered_by_call_or_put(df_info, call_or_put):
#     df_filtered = df_info[
#         (df_info['call_or_put'] == call_or_put)]
#     return df_filtered


import numpy as np
from .info import get_opt_info_filtered_by_date, get_opt_info_filtered_by_month, \
    get_opt_info_filtered_by_call_or_put
    :param df_info: 期权合约信息表，将通过etf行情和执行价选出实虚合约
    :param month_index: 选出当月、次月、当季、次季
    :param atm_index: 选出虚几
    :return:
    """
    signal = row[field]
    if np.isnan(signal):
        return row
    if signal == 0:
        row[field] = np.nan
        return row
    # 取指定日期的数据
    date = row['index_datetime'].strftime("%Y-%m-%d")  # 指定日期

    # 2015-05-29号，5月份的合约已经结束，应当切换成6月才行
    df_filtered = get_opt_info_filtered_by_date(df_info, date)
    df_filtered1 = df_filtered.reset_index()
    # 除权时，选合约会有问题，如果改成除权后，有新开仓才去新合约即可
    df_filtered1.set_index(['limit_month', 'limit_month_m'], inplace=True)
    list_limit_month = list(set(df_filtered1.index.get_level_values(0)))
    list_limit_month.sort()

    # 设定是做哪个类型
    # 看多，买入call
    # 看空，买入put
    call_or_put = 'C' if signal > 0 else 'P'
    df_filtered2 = get_opt_info_filtered_by_call_or_put(df_filtered, call_or_put)

    # 选择当月，次月，当季，下季
    # 有可能选出来的合约没有虚值，或没有实值，因为当天新挂合约并知道

# Path: kquant_data/option/info.py
# def get_opt_info_filtered_by_date(df_info, date):
#     # date = '2018-03-04'  # 指定日期
#     # call_or_put = 'C'  # 指定类别
#     # limit_month = 1806  # 指定月份
#     # limit_month_m = 'A'  # 指定月份2
#     df_filtered = df_info[(df_info['listed_date'] <= date)
#                           & (df_info['expire_date'] >= date)]
#     return df_filtered
# 
# def get_opt_info_filtered_by_month(df_info, limit_month):
#     df_filtered = df_info[
#         (df_info['limit_month'] == limit_month)]
#     return df_filtered
# 
# def get_opt_info_filtered_by_call_or_put(df_info, call_or_put):
#     df_filtered = df_info[
#         (df_info['call_or_put'] == call_or_put)]
#     return df_filtered


# Path: kquant_data/option/strategy.py
import numpy as np
from .info import get_opt_info_filtered_by_date, get_opt_info_filtered_by_month, \
    get_opt_info_filtered_by_call_or_put

    :param df_info: 期权合约信息表，将通过etf行情和执行价选出实虚合约
    :param month_index: 选出当月、次月、当季、次季
    :param atm_index: 选出虚几
    :return:
    """
    signal = row[field]
    if np.isnan(signal):
        return row
    if signal == 0:
        row[field] = np.nan
        return row
    # 取指定日期的数据
    date = row['index_datetime'].strftime("%Y-%m-%d")  # 指定日期

    # 2015-05-29号，5月份的合约已经结束，应当切换成6月才行
    df_filtered = get_opt_info_filtered_by_date(df_info, date)
    df_filtered1 = df_filtered.reset_index()
    # 除权时，选合约会有问题，如果改成除权后，有新开仓才去新合约即可
    df_filtered1.set_index(['limit_month', 'limit_month_m'], inplace=True)
    list_limit_month = list(set(df_filtered1.index.get_level_values(0)))
    list_limit_month.sort()

    # 设定是做哪个类型
    # 看多，买入call
    # 看空，买入put
    call_or_put = 'C' if signal > 0 else 'P'
    df_filtered2 = get_opt_info_filtered_by_call_or_put(df_filtered, call_or_put)

    # 选择当月，次月，当季，下季
    # 有可能选出来的合约没有虚值，或没有实值，因为当天新挂合约并知道

    df_filtered3 = get_opt_info_filtered_by_month(df_filtered2, list_limit_month[month_index])