{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import akshare as ak\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import os\n",
    "import time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "# option_df = ak.option_finance_board(symbol=\"中证1000股指期权\", end_month=\"2503\")\n",
    "# option_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# option_cffex_df = ak.option_cffex_zz1000_spot_sina(symbol='ag2504')\n",
    "option_commodity_contract_table_sina_df = ak.option_commodity_contract_table_sina(symbol=\"白银期权\", contract=\"ag2504\")\n",
    "option_commodity_contract_table_sina_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "#根据中证l000指数获取点位\n",
    "\n",
    "def get_format_option_gap(value:float, deviation:int=0):\n",
    "    \"\"\"\n",
    "    根据标准的行权价，生成不同档位的期权列表，适合中金所\n",
    "    :param value:当前行权价\n",
    "    :param deviation:相较于行权价向哪个方向偏移，>0表示较行权价向上调整，<-表示较行权价向下调整\n",
    "    return:\n",
    "    \"\"\"\n",
    "    def _gap_value(_mark_value):\n",
    "        if _mark_value < 500:\n",
    "            return 25\n",
    "        elif _mark_value < 5000:\n",
    "            return 50\n",
    "        elif _mark_value < 10000:\n",
    "            return 100\n",
    "        elif _mark_value >10000:\n",
    "            return 200\n",
    "        \n",
    "    for i in range(abs(deviation)):\n",
    "        _gap =_gap_value(value)\n",
    "        #option a ex price=int(value/gap)*gap#买入看跌期权\n",
    "        if deviation >=0:\n",
    "            value +=_gap\n",
    "        elif deviation<0:\n",
    "            value -=_gap\n",
    "    return value\n",
    "  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "def future_to_option_code(symbol):\n",
    "    \"\"\"\n",
    "    转换期权代码格式\n",
    "    \n",
    "    Args:\n",
    "        symbol (str): 原始期权代码\n",
    "        \n",
    "    Returns:\n",
    "        str: 转换后的期权代码\n",
    "    \"\"\"\n",
    "    symbol_upper = symbol.upper()\n",
    "    \n",
    "    if 'IM' in symbol_upper:\n",
    "        # 将IM替换为MO\n",
    "        return symbol_upper.replace('IM', 'mo')\n",
    "    elif 'IF' in symbol_upper:\n",
    "        # 将IF替换为IO\n",
    "        return symbol_upper.replace('IF', 'io')\n",
    "    elif 'IH' in symbol_upper:\n",
    "        # 将IH替换为HO\n",
    "        return symbol_upper.replace('IH', 'ho')\n",
    "    else:\n",
    "        raise ValueError(f\"不支持的期权代码格式: {symbol}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "future_to_option_code('im2503')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "def get_atm_option_code(symbol:str, current_price:float, call_level:int, put_level:int):\n",
    "    symbol_upper = symbol.upper()\n",
    "\n",
    "    # if 'IM' in symbol_upper:\n",
    "    #     # 将IM替换为MO\n",
    "    #     return symbol_upper.replace('IM', 'mo')\n",
    "    #     # print(symbol_upper)\n",
    "    # elif 'IF' in symbol_upper:\n",
    "    #     # 将IF替换为IO\n",
    "    #     return symbol_upper.replace('IF', 'io')\n",
    "    # elif 'IH' in symbol_upper:\n",
    "    #     # 将IH替换为HO\n",
    "    #     return symbol_upper.replace('IH', 'ho')\n",
    "    # else:\n",
    "    #     raise ValueError(f\"不支持的期权代码格式: {symbol}\")\n",
    "\n",
    "    # # symbol_upper = symbol_upper.lower()\n",
    "    # print(symbol_upper)\n",
    "    if 'MO' in symbol_upper:\n",
    "        option_cffex_df = ak.option_cffex_zz1000_spot_sina(symbol=symbol)\n",
    "    elif 'IO' in symbol_upper:\n",
    "        option_cffex_df = ak.option_cffex_hs300_spot_sina(symbol=symbol)\n",
    "    elif 'HO' in symbol_upper:\n",
    "        option_cffex_df = ak.option_cffex_sz50_spot_sina(symbol=symbol)\n",
    "    else:\n",
    "        raise ValueError(f\"不支持的期权代码格式: {symbol_upper}\")\n",
    "    # 提取所有行权价并转换为数值\n",
    "    strike_prices = option_cffex_df['行权价'].astype(float).unique()\n",
    "    find_call_prices = get_format_option_gap(current_price,call_level)\n",
    "    find_put_prices = get_format_option_gap(current_price,-put_level)\n",
    "    closest_call_strike = min(strike_prices, key=lambda x: abs(x - find_call_prices))\n",
    "    closest_put_strike = min(strike_prices, key=lambda x: abs(x - find_put_prices))\n",
    "    # 生成平值期权代码（如MO2503-C-6250）\n",
    "    call_atm_option_code = f\"{symbol_upper}-C-{int(closest_call_strike)}\"\n",
    "    put_atm_option_code = f\"{symbol_upper}-P-{int(closest_put_strike)}\"\n",
    "    # print(f\"平值看涨期权：{call_atm_option_code}\")\n",
    "    # print(f\"平值看跌期权：{put_atm_option_code}\")\n",
    "    return call_atm_option_code, put_atm_option_code\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "get_atm_option_code(future_to_option_code('im2503'),6273,0,0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "get_atm_option_code(future_to_option_code('ih2503'),2628,0,0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "get_atm_option_code(future_to_option_code('if2503'),3386,0,0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "option_cffex_df = ak.option_cffex_zz1000_spot_sina(symbol='mo2503')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "# stock_individual_spot_xq_df = ak.stock_individual_spot_xq(symbol='000852')\n",
    "# print(stock_individual_spot_xq_df.dtypes)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
