{ "cells": [ { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "import pandas as pd" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "file_path_888 = r\"E:\\data\\data_rs_merged\\中金所\\IM888\\IM888_rs_2023.csv\"\n", "df_888 = pd.read_csv(file_path_888, encoding='utf-8')" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
main_contractsymboldatetimelastpricevolumebid_pask_pbid_vask_v
0IM888IM23012023-01-03 09:30:00.2006280.0466276.06277.013
1IM888IM23012023-01-03 09:30:00.7006277.0616278.06278.8116
2IM888IM23012023-01-03 09:30:01.2006277.2816277.26278.815
3IM888IM23012023-01-03 09:30:01.7006277.8906277.86278.634
4IM888IM23012023-01-03 09:30:02.2006278.81126278.86280.017
\n", "
" ], "text/plain": [ " main_contract symbol datetime lastprice volume bid_p \\\n", "0 IM888 IM2301 2023-01-03 09:30:00.200 6280.0 46 6276.0 \n", "1 IM888 IM2301 2023-01-03 09:30:00.700 6277.0 61 6278.0 \n", "2 IM888 IM2301 2023-01-03 09:30:01.200 6277.2 81 6277.2 \n", "3 IM888 IM2301 2023-01-03 09:30:01.700 6277.8 90 6277.8 \n", "4 IM888 IM2301 2023-01-03 09:30:02.200 6278.8 112 6278.8 \n", "\n", " ask_p bid_v ask_v \n", "0 6277.0 1 3 \n", "1 6278.8 1 16 \n", "2 6278.8 1 5 \n", "3 6278.6 3 4 \n", "4 6280.0 1 7 " ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df_888.head()" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "# 重命名列以便处理\n", "# df_888.rename(columns={'时间': 'datetime', '最新': 'price', '成交量': 'volume'}, inplace=True)\n", "df_888.rename(columns={'datetime': 'datetime', 'lastprice': 'price', 'volume': 'volume'}, inplace=True)\n", "\n", "# 确保datetime列是datetime类型\n", "df_888['datetime'] = pd.to_datetime(df_888['datetime'])\n", "\n", "# 设置datetime列为索引\n", "df_888.set_index('datetime', inplace=True)\n", "\n", "# 使用resample方法将数据重新采样为1分钟数据\n", "df_resampled = df_888.resample('1T').agg({\n", " 'price': ['first', 'max', 'min', 'last'],\n", " 'volume': 'sum'\n", "})" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
pricevolume
firstmaxminlastsum
datetime
2023-01-03 09:30:006280.06306.46277.06302.066894
2023-01-03 09:31:006302.06320.06302.06318.8172512
2023-01-03 09:32:006319.86328.06314.86314.8238716
2023-01-03 09:33:006313.06325.06310.46312.4297675
2023-01-03 09:34:006311.06323.26311.06319.4352184
\n", "
" ], "text/plain": [ " price volume\n", " first max min last sum\n", "datetime \n", "2023-01-03 09:30:00 6280.0 6306.4 6277.0 6302.0 66894\n", "2023-01-03 09:31:00 6302.0 6320.0 6302.0 6318.8 172512\n", "2023-01-03 09:32:00 6319.8 6328.0 6314.8 6314.8 238716\n", "2023-01-03 09:33:00 6313.0 6325.0 6310.4 6312.4 297675\n", "2023-01-03 09:34:00 6311.0 6323.2 6311.0 6319.4 352184" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df_resampled.head()" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'IM888'" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df_888['main_contract'][1]" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [], "source": [ "# df_resampled['symbol'] = df_888['main_contract'][1]\n", "df_resampled.insert(0, 'symbol', df_888['main_contract'][1])" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
symbolpricevolume
firstmaxminlastsum
datetime
2023-01-03 09:30:00IM8886280.06306.46277.06302.066894
2023-01-03 09:31:00IM8886302.06320.06302.06318.8172512
2023-01-03 09:32:00IM8886319.86328.06314.86314.8238716
2023-01-03 09:33:00IM8886313.06325.06310.46312.4297675
2023-01-03 09:34:00IM8886311.06323.26311.06319.4352184
\n", "
" ], "text/plain": [ " symbol price volume\n", " first max min last sum\n", "datetime \n", "2023-01-03 09:30:00 IM888 6280.0 6306.4 6277.0 6302.0 66894\n", "2023-01-03 09:31:00 IM888 6302.0 6320.0 6302.0 6318.8 172512\n", "2023-01-03 09:32:00 IM888 6319.8 6328.0 6314.8 6314.8 238716\n", "2023-01-03 09:33:00 IM888 6313.0 6325.0 6310.4 6312.4 297675\n", "2023-01-03 09:34:00 IM888 6311.0 6323.2 6311.0 6319.4 352184" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df_resampled.head()" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [], "source": [ "# 重命名列名以符合K线数据的标准命名\n", "df_resampled.columns = ['open', 'high', 'low', 'close', 'volume', 'symbol']" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
openhighlowclosevolumesymbol
datetime
2023-01-03 09:30:00IM8886280.06306.46277.06302.066894
2023-01-03 09:31:00IM8886302.06320.06302.06318.8172512
2023-01-03 09:32:00IM8886319.86328.06314.86314.8238716
2023-01-03 09:33:00IM8886313.06325.06310.46312.4297675
2023-01-03 09:34:00IM8886311.06323.26311.06319.4352184
\n", "
" ], "text/plain": [ " open high low close volume symbol\n", "datetime \n", "2023-01-03 09:30:00 IM888 6280.0 6306.4 6277.0 6302.0 66894\n", "2023-01-03 09:31:00 IM888 6302.0 6320.0 6302.0 6318.8 172512\n", "2023-01-03 09:32:00 IM888 6319.8 6328.0 6314.8 6314.8 238716\n", "2023-01-03 09:33:00 IM888 6313.0 6325.0 6310.4 6312.4 297675\n", "2023-01-03 09:34:00 IM888 6311.0 6323.2 6311.0 6319.4 352184" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df_resampled.head()" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1分钟历史数据已保存至E:\\data\\data_rs_merged\\中金所\\IM888\\IM888_rs_2023_1min.csv\n" ] } ], "source": [ "# 删除存在NA值的行(如果有的时间段没有交易数据)\n", "df_resampled.dropna(inplace=True)\n", "# df_resampled['symbol'] = df_888['统一代码']\n", "# df_resampled.insert(loc=0, column='main_contract', value=df_888['main_contract'])\n", "# df_resampled['symbol'] = df_888['main_contract']\n", "# 将重新采样的数据写入新的CSV文件\n", "output_file = r\"E:\\data\\data_rs_merged\\中金所\\IM888\\IM888_rs_2023_1min.csv\"\n", "df_resampled.to_csv(output_file)\n", "\n", "print(f'1分钟历史数据已保存至{output_file}')" ] } ], "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 }