{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2d85dda4",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import pandas as pd\n",
    "from merged_tickdata_20240724 import merged_new_tickdata, reinstatement_tickdata"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "413eb7eb",
   "metadata": {},
   "outputs": [],
   "source": [
    "for k in ['2021']:#, '2023'\n",
    "    for v in ['IH', 'IF', 'IC', 'IM', 'T', 'TF', 'TL', 'TS']:\n",
    "        new_up_file = 'E:/%s888/%s888_up_%s.csv'%(v,v,k)\n",
    "        new_df_file = 'E:/%s888/%s888_%s.csv'%(v,v,k)\n",
    "        new_rs_df_file= 'E:/%s888/%s888_rs_%s.csv'%(v,v,k)\n",
    "        sp_new_chars = '_%s'%(k)\n",
    "        alpha_chars = '%s'%(v)\n",
    "        print(new_up_file,new_df_file,new_rs_df_file,sp_new_chars,alpha_chars)\n",
    "        # 生成按年份处理后的CSV文件\n",
    "        try:\n",
    "            new_up_df = pd.read_csv(str(new_up_file), encoding='utf-8')\n",
    "            print('品种%s在%s年数据读取成功!'%(v,k))\n",
    "        except FileNotFoundError:\n",
    "            print('品种%s在%s年无数据!'%(v,k))\n",
    "            continue\n",
    "        new_df = merged_new_tickdata(new_up_df, alpha_chars)\n",
    "        del new_up_df\n",
    "        new_df.to_csv(new_df_file, index=False)\n",
    "        print(\"按年份处理的CSV文件合并成功!\")\n",
    "        \n",
    "        # 生成按年份处理后的CSV文件按照等差复权处理\n",
    "        new_rs_df = reinstatement_tickdata(new_df)\n",
    "        del new_df\n",
    "        new_rs_df.to_csv(new_rs_df_file, index=False)\n",
    "        print(\"按年份处理且进行等差复权的文件合并成功!\")\n",
    "        del new_rs_df\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "39018dfe",
   "metadata": {},
   "outputs": [],
   "source": [
    "new_up_df = pd.read_csv(r\"E:\\TS888\\TS888_up_2022.csv\", encoding='utf-8')\n",
    "new_df_file = r\"E:\\TS888\\TS888_2022.csv\"\n",
    "new_rs_df_file=r\"E:\\TS888\\TS888_rs_2022.csv\"\n",
    "sp_new_chars = '_2022'\n",
    "alpha_chars = 'TS'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b391b81f",
   "metadata": {},
   "outputs": [],
   "source": [
    "new_up_df.tail()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b80fde40",
   "metadata": {},
   "outputs": [],
   "source": [
    "new_df = merged_new_tickdata(new_up_df, alpha_chars)\n",
    "del new_up_df\n",
    "new_df.to_csv(new_df_file, index=False)\n",
    "print(\"按年份处理的CSV文件合并成功!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fbe64270",
   "metadata": {},
   "outputs": [],
   "source": [
    "new_df.tail()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f076b084",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 生成按年份处理后的CSV文件按照等差复权处理\n",
    "new_rs_df = reinstatement_tickdata(new_df)\n",
    "del new_df\n",
    "new_rs_df.to_csv(new_rs_df_file, index=False)\n",
    "print(\"按年份处理且进行等差复权的文件合并成功!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8bb177b0",
   "metadata": {},
   "outputs": [],
   "source": [
    "new_rs_df.tail()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b2202624",
   "metadata": {},
   "outputs": [],
   "source": [
    "del new_rs_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "0bf526ef",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "da125360",
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.read_csv(r\"D:\\BaiduNetdiskDownload\\主力连续\\tick生成的OF数据(5M)\\data_rs_merged\\中金所\\IM888\\IM888_rs_2022_5T_back_ofdata_dj.csv\", encoding='utf-8')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "a574a928",
   "metadata": {},
   "outputs": [],
   "source": [
    "df_new = df.drop(df[df['delta'] == 0].index)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "ebdd32fd",
   "metadata": {},
   "outputs": [],
   "source": [
    "df_new.to_csv(r\"D:\\BaiduNetdiskDownload\\主力连续\\tick生成的OF数据(5M)\\data_rs_merged\\中金所\\IM888\\IM888_rs_2022_5T_back_ofdata_dj_new.csv\", index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "55e799b0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 1114444 characters]",
      "text/plain": [
       "<Figure size 4800x2400 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "# 假设你的 DataFrame 已经加载为 df，并且 df['a'] 是要计算布林带的数值列\n",
    "df = pd.read_csv(r\"D:\\BaiduNetdiskDownload\\主力连续\\tick生成的OF数据(5M)\\data_rs_merged\\中金所\\IM888\\IM888_rs_2022_5T_back_ofdata_dj_new.csv\")  # 例如从CSV加载\n",
    "\n",
    "# 计算中轨线 (通常是20日简单移动平均线)\n",
    "window = 40  # 窗口大小，可以根据你的需求调整\n",
    "df['Middle Band'] = df['delta'].rolling(window=window).mean()\n",
    "\n",
    "# 计算标准差\n",
    "df['Std Dev'] = df['delta'].rolling(window=window).std()\n",
    "\n",
    "# 计算上轨线和下轨线\n",
    "df['Upper Band'] = df['Middle Band'] + (2 * df['Std Dev'])\n",
    "df['Lower Band'] = df['Middle Band'] - (2 * df['Std Dev'])\n",
    "\n",
    "# 绘制图形\n",
    "plt.figure(figsize=(48,24))\n",
    "plt.plot(df['delta'], label='Price', color='blue')\n",
    "plt.plot(df['Middle Band'], label='Middle Band', color='black', linestyle='--')\n",
    "plt.plot(df['Upper Band'], label='Upper Band', color='red', linestyle='--')\n",
    "plt.plot(df['Lower Band'], label='Lower Band', color='green', linestyle='--')\n",
    "plt.fill_between(df.index, df['Upper Band'], df['Lower Band'], color='gray', alpha=0.3)\n",
    "\n",
    "plt.title('Bollinger Bands')\n",
    "plt.legend(loc='best')\n",
    "plt.show()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "75652a31",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 83048 characters]",
      "text/plain": [
       "<Figure size 1200x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "# 假设你的 DataFrame 已经加载为 df，并且 df['a'] 是要计算CMO的数值列\n",
    "df = pd.read_csv(r\"D:\\BaiduNetdiskDownload\\主力连续\\tick生成的OF数据(5M)\\data_rs_merged\\中金所\\IM888\\IM888_rs_2022_5T_back_ofdata_dj_new.csv\")  # 例如从CSV加载\n",
    "\n",
    "# 定义时间窗口，通常使用14天\n",
    "window = 14\n",
    "\n",
    "# 计算价格的变化\n",
    "df['Change'] = df['delta'].diff()\n",
    "\n",
    "# 计算上升动量 U 和下降动量 D\n",
    "df['U'] = np.where(df['Change'] > 0, df['Change'], 0)\n",
    "df['D'] = np.where(df['Change'] < 0, -df['Change'], 0)\n",
    "\n",
    "# 计算 U 和 D 的移动平均线\n",
    "df['SMA_U'] = df['U'].rolling(window=window).mean()\n",
    "df['SMA_D'] = df['D'].rolling(window=window).mean()\n",
    "\n",
    "# 计算CMO\n",
    "df['CMO'] = 100 * (df['SMA_U'] - df['SMA_D']) / (df['SMA_U'] + df['SMA_D'])\n",
    "\n",
    "# 绘制CMO图形\n",
    "plt.figure(figsize=(12,6))\n",
    "plt.plot(df['CMO'], label='CMO', color='purple')\n",
    "\n",
    "plt.title('Chande Momentum Oscillator (CMO)')\n",
    "plt.axhline(y=50, color='r', linestyle='--', label='Overbought')  # 超买线\n",
    "plt.axhline(y=-50, color='g', linestyle='--', label='Oversold')   # 超卖线\n",
    "plt.axhline(y=0, color='black', linestyle='-')  # 中线\n",
    "\n",
    "plt.legend(loc='best')\n",
    "plt.show()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "8e7b22c6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 78224 characters]",
      "text/plain": [
       "<Figure size 1200x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import talib as tb\n",
    "\n",
    "# 假设你的 DataFrame 已经加载为 df，并且 df['a'] 是要计算CMO的数值列\n",
    "df = pd.read_csv(r\"E:\\of_data\\主力连续\\tick生成的OF数据(5M)\\data_rs_merged\\中金所\\IM888\\IM888_rs_2023_5T_back_ofdata_dj.csv\")  # 例如从CSV加载\n",
    "\n",
    "# 定义时间窗口，通常使用14天\n",
    "window = 14\n",
    "\n",
    "# 计算HT_TRENDLINE\n",
    "df['HT_TRENDLINE'] = tb.HT_TRENDLINE(df['close'])\n",
    "\n",
    "# 绘制CMO图形\n",
    "plt.figure(figsize=(12,6))\n",
    "plt.plot(df['HT_TRENDLINE'], label='HT_TRENDLINE', color='purple')\n",
    "plt.plot(df['close'], label='close', color='blue')\n",
    "\n",
    "plt.title('Chande Momentum Oscillator (HT_TRENDLINE)')\n",
    "# plt.axhline(y=50, color='r', linestyle='--', label='Overbought')  # 超买线\n",
    "# plt.axhline(y=-50, color='g', linestyle='--', label='Oversold')   # 超卖线\n",
    "# plt.axhline(y=0, color='black', linestyle='-')  # 中线\n",
    "\n",
    "plt.legend(loc='best')\n",
    "plt.show()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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": 5
}
