{
 "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 ast\n",
    "import time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "symbol_name = 'rb2505'\n",
    "# 获取当前工作目录\n",
    "current_directory = os.getcwd()\n",
    "print(\"当前工作目录：\", current_directory)\n",
    "# 设置新的工作目录\n",
    "new_directory = \"C:/Users/zhouj/Desktop\"\n",
    "os.chdir(new_directory) \n",
    "# 验证新的工作目录\n",
    "updated_directory = os.getcwd()\n",
    "print(\"已更改为新的工作目录：\", updated_directory)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "futures_zh_minute_sina_df = ak.futures_zh_minute_sina(symbol=symbol_name, period=\"5\")\n",
    "print(futures_zh_minute_sina_df)\n",
    "\n",
    "df = pd.read_csv(f\"{symbol_name}_ofdata.csv\", usecols=range(12))\n",
    "df.tail(1)\n",
    "\n",
    "def ultimate_smoother(price, period):\n",
    "    # 初始化变量（修正角度单位为弧度）\n",
    "    a1 = np.exp(-1.414 * np.pi / period)\n",
    "    b1 = 2 * a1 * np.cos(1.414 * np.pi / period)  # 将180改为np.pi\n",
    "    c2 = b1\n",
    "    c3 = -a1**2\n",
    "    c1 = (1 + c2 - c3) / 4\n",
    "\n",
    "    # 准备输出序列\n",
    "    us = np.zeros(len(price))\n",
    "    us_new = np.zeros(len(price))\n",
    "    trend = [None] * (len(price))\n",
    "    ma_close = np.zeros(len(price))\n",
    "\n",
    "    # 前4个点用原始价格初始化\n",
    "    for i in range(len(price)):\n",
    "        if i < 4:\n",
    "            us[i] = price[i]\n",
    "        else:\n",
    "            # 应用递归公式\n",
    "            us[i] = (1 - c1) * price[i] + (2 * c1 - c2) * price[i-1] \\\n",
    "                    - (c1 + c3) * price[i-2] + c2 * us[i-1] + c3 * us[i-2]\n",
    "\n",
    "        us_new = np.around(us, decimals=2)\n",
    "        ma_close = price.rolling(window=5 * period).mean()\n",
    "\n",
    "        if us_new[i] > price[i] and ma_close[i] > price[i]:\n",
    "            trend[i] = '空头趋势'\n",
    "        elif us_new[i] < price[i] and ma_close[i] < price[i]:\n",
    "            trend[i] = '多头趋势'\n",
    "        else:\n",
    "            trend[i] = '无趋势'\n",
    "\n",
    "    return us_new, trend"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "futures_zh_minute_sina_df['datetime'] = pd.to_datetime(futures_zh_minute_sina_df['datetime'])\n",
    "futures_zh_minute_sina_df['终极平滑值'], futures_zh_minute_sina_df[\n",
    "    '趋势方向'] = ultimate_smoother(futures_zh_minute_sina_df[\"close\"], 48)\n",
    "\n",
    "# 设置索引\n",
    "df['datetime'] = pd.to_datetime(df['datetime'])\n",
    "df = df.set_index('datetime')\n",
    "futures_zh_minute_sina_df = futures_zh_minute_sina_df.set_index('datetime')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_new = futures_zh_minute_sina_df.join(df[['price', 'Ask', 'Bid', 'delta','dj']], how='left')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_new['symbol'] = symbol_name\n",
    "df_new = df_new.reset_index()\n",
    "cols = [\n",
    "    'price', 'Ask', 'Bid', 'symbol', 'datetime', 'delta', 'close', 'open',\n",
    "    'high', 'low', 'volume', 'hold', 'dj', '终极平滑值', '趋势方向'\n",
    "]\n",
    "df_new = df_new[cols]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_new.head(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_new.tail(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "del df_new['hold'],df_new['终极平滑值'],df_new['趋势方向']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_new.to_csv(f\"{symbol_name}_ofdata_new.csv\", index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import akshare as ak\n",
    "ak.futures_fees_info() "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "url = \"http://openctp.cn/fees.html\"\n",
    "temp_df = pd.read_html(url, encoding='gbk')[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "temp_df"
   ]
  }
 ],
 "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
}
