Enhance trading workflow with new order flow management
- Added dingdanliu_nb_mflow for improved order processing - Updated related scripts and configurations to support new functionality
This commit is contained in:
610
2.数据下载与处理/tick数据转分钟数据脚本/tick_to_min.ipynb
Normal file
610
2.数据下载与处理/tick数据转分钟数据脚本/tick_to_min.ipynb
Normal file
@@ -0,0 +1,610 @@
|
||||
{
|
||||
"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": [
|
||||
"<div>\n",
|
||||
"<style scoped>\n",
|
||||
" .dataframe tbody tr th:only-of-type {\n",
|
||||
" vertical-align: middle;\n",
|
||||
" }\n",
|
||||
"\n",
|
||||
" .dataframe tbody tr th {\n",
|
||||
" vertical-align: top;\n",
|
||||
" }\n",
|
||||
"\n",
|
||||
" .dataframe thead th {\n",
|
||||
" text-align: right;\n",
|
||||
" }\n",
|
||||
"</style>\n",
|
||||
"<table border=\"1\" class=\"dataframe\">\n",
|
||||
" <thead>\n",
|
||||
" <tr style=\"text-align: right;\">\n",
|
||||
" <th></th>\n",
|
||||
" <th>main_contract</th>\n",
|
||||
" <th>symbol</th>\n",
|
||||
" <th>datetime</th>\n",
|
||||
" <th>lastprice</th>\n",
|
||||
" <th>volume</th>\n",
|
||||
" <th>bid_p</th>\n",
|
||||
" <th>ask_p</th>\n",
|
||||
" <th>bid_v</th>\n",
|
||||
" <th>ask_v</th>\n",
|
||||
" </tr>\n",
|
||||
" </thead>\n",
|
||||
" <tbody>\n",
|
||||
" <tr>\n",
|
||||
" <th>0</th>\n",
|
||||
" <td>IM888</td>\n",
|
||||
" <td>IM2301</td>\n",
|
||||
" <td>2023-01-03 09:30:00.200</td>\n",
|
||||
" <td>6280.0</td>\n",
|
||||
" <td>46</td>\n",
|
||||
" <td>6276.0</td>\n",
|
||||
" <td>6277.0</td>\n",
|
||||
" <td>1</td>\n",
|
||||
" <td>3</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>1</th>\n",
|
||||
" <td>IM888</td>\n",
|
||||
" <td>IM2301</td>\n",
|
||||
" <td>2023-01-03 09:30:00.700</td>\n",
|
||||
" <td>6277.0</td>\n",
|
||||
" <td>61</td>\n",
|
||||
" <td>6278.0</td>\n",
|
||||
" <td>6278.8</td>\n",
|
||||
" <td>1</td>\n",
|
||||
" <td>16</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>2</th>\n",
|
||||
" <td>IM888</td>\n",
|
||||
" <td>IM2301</td>\n",
|
||||
" <td>2023-01-03 09:30:01.200</td>\n",
|
||||
" <td>6277.2</td>\n",
|
||||
" <td>81</td>\n",
|
||||
" <td>6277.2</td>\n",
|
||||
" <td>6278.8</td>\n",
|
||||
" <td>1</td>\n",
|
||||
" <td>5</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>3</th>\n",
|
||||
" <td>IM888</td>\n",
|
||||
" <td>IM2301</td>\n",
|
||||
" <td>2023-01-03 09:30:01.700</td>\n",
|
||||
" <td>6277.8</td>\n",
|
||||
" <td>90</td>\n",
|
||||
" <td>6277.8</td>\n",
|
||||
" <td>6278.6</td>\n",
|
||||
" <td>3</td>\n",
|
||||
" <td>4</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>4</th>\n",
|
||||
" <td>IM888</td>\n",
|
||||
" <td>IM2301</td>\n",
|
||||
" <td>2023-01-03 09:30:02.200</td>\n",
|
||||
" <td>6278.8</td>\n",
|
||||
" <td>112</td>\n",
|
||||
" <td>6278.8</td>\n",
|
||||
" <td>6280.0</td>\n",
|
||||
" <td>1</td>\n",
|
||||
" <td>7</td>\n",
|
||||
" </tr>\n",
|
||||
" </tbody>\n",
|
||||
"</table>\n",
|
||||
"</div>"
|
||||
],
|
||||
"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": [
|
||||
"<div>\n",
|
||||
"<style scoped>\n",
|
||||
" .dataframe tbody tr th:only-of-type {\n",
|
||||
" vertical-align: middle;\n",
|
||||
" }\n",
|
||||
"\n",
|
||||
" .dataframe tbody tr th {\n",
|
||||
" vertical-align: top;\n",
|
||||
" }\n",
|
||||
"\n",
|
||||
" .dataframe thead tr th {\n",
|
||||
" text-align: left;\n",
|
||||
" }\n",
|
||||
"\n",
|
||||
" .dataframe thead tr:last-of-type th {\n",
|
||||
" text-align: right;\n",
|
||||
" }\n",
|
||||
"</style>\n",
|
||||
"<table border=\"1\" class=\"dataframe\">\n",
|
||||
" <thead>\n",
|
||||
" <tr>\n",
|
||||
" <th></th>\n",
|
||||
" <th colspan=\"4\" halign=\"left\">price</th>\n",
|
||||
" <th>volume</th>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th></th>\n",
|
||||
" <th>first</th>\n",
|
||||
" <th>max</th>\n",
|
||||
" <th>min</th>\n",
|
||||
" <th>last</th>\n",
|
||||
" <th>sum</th>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>datetime</th>\n",
|
||||
" <th></th>\n",
|
||||
" <th></th>\n",
|
||||
" <th></th>\n",
|
||||
" <th></th>\n",
|
||||
" <th></th>\n",
|
||||
" </tr>\n",
|
||||
" </thead>\n",
|
||||
" <tbody>\n",
|
||||
" <tr>\n",
|
||||
" <th>2023-01-03 09:30:00</th>\n",
|
||||
" <td>6280.0</td>\n",
|
||||
" <td>6306.4</td>\n",
|
||||
" <td>6277.0</td>\n",
|
||||
" <td>6302.0</td>\n",
|
||||
" <td>66894</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>2023-01-03 09:31:00</th>\n",
|
||||
" <td>6302.0</td>\n",
|
||||
" <td>6320.0</td>\n",
|
||||
" <td>6302.0</td>\n",
|
||||
" <td>6318.8</td>\n",
|
||||
" <td>172512</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>2023-01-03 09:32:00</th>\n",
|
||||
" <td>6319.8</td>\n",
|
||||
" <td>6328.0</td>\n",
|
||||
" <td>6314.8</td>\n",
|
||||
" <td>6314.8</td>\n",
|
||||
" <td>238716</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>2023-01-03 09:33:00</th>\n",
|
||||
" <td>6313.0</td>\n",
|
||||
" <td>6325.0</td>\n",
|
||||
" <td>6310.4</td>\n",
|
||||
" <td>6312.4</td>\n",
|
||||
" <td>297675</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>2023-01-03 09:34:00</th>\n",
|
||||
" <td>6311.0</td>\n",
|
||||
" <td>6323.2</td>\n",
|
||||
" <td>6311.0</td>\n",
|
||||
" <td>6319.4</td>\n",
|
||||
" <td>352184</td>\n",
|
||||
" </tr>\n",
|
||||
" </tbody>\n",
|
||||
"</table>\n",
|
||||
"</div>"
|
||||
],
|
||||
"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": [
|
||||
"<div>\n",
|
||||
"<style scoped>\n",
|
||||
" .dataframe tbody tr th:only-of-type {\n",
|
||||
" vertical-align: middle;\n",
|
||||
" }\n",
|
||||
"\n",
|
||||
" .dataframe tbody tr th {\n",
|
||||
" vertical-align: top;\n",
|
||||
" }\n",
|
||||
"\n",
|
||||
" .dataframe thead tr th {\n",
|
||||
" text-align: left;\n",
|
||||
" }\n",
|
||||
"\n",
|
||||
" .dataframe thead tr:last-of-type th {\n",
|
||||
" text-align: right;\n",
|
||||
" }\n",
|
||||
"</style>\n",
|
||||
"<table border=\"1\" class=\"dataframe\">\n",
|
||||
" <thead>\n",
|
||||
" <tr>\n",
|
||||
" <th></th>\n",
|
||||
" <th>symbol</th>\n",
|
||||
" <th colspan=\"4\" halign=\"left\">price</th>\n",
|
||||
" <th>volume</th>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th></th>\n",
|
||||
" <th></th>\n",
|
||||
" <th>first</th>\n",
|
||||
" <th>max</th>\n",
|
||||
" <th>min</th>\n",
|
||||
" <th>last</th>\n",
|
||||
" <th>sum</th>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>datetime</th>\n",
|
||||
" <th></th>\n",
|
||||
" <th></th>\n",
|
||||
" <th></th>\n",
|
||||
" <th></th>\n",
|
||||
" <th></th>\n",
|
||||
" <th></th>\n",
|
||||
" </tr>\n",
|
||||
" </thead>\n",
|
||||
" <tbody>\n",
|
||||
" <tr>\n",
|
||||
" <th>2023-01-03 09:30:00</th>\n",
|
||||
" <td>IM888</td>\n",
|
||||
" <td>6280.0</td>\n",
|
||||
" <td>6306.4</td>\n",
|
||||
" <td>6277.0</td>\n",
|
||||
" <td>6302.0</td>\n",
|
||||
" <td>66894</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>2023-01-03 09:31:00</th>\n",
|
||||
" <td>IM888</td>\n",
|
||||
" <td>6302.0</td>\n",
|
||||
" <td>6320.0</td>\n",
|
||||
" <td>6302.0</td>\n",
|
||||
" <td>6318.8</td>\n",
|
||||
" <td>172512</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>2023-01-03 09:32:00</th>\n",
|
||||
" <td>IM888</td>\n",
|
||||
" <td>6319.8</td>\n",
|
||||
" <td>6328.0</td>\n",
|
||||
" <td>6314.8</td>\n",
|
||||
" <td>6314.8</td>\n",
|
||||
" <td>238716</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>2023-01-03 09:33:00</th>\n",
|
||||
" <td>IM888</td>\n",
|
||||
" <td>6313.0</td>\n",
|
||||
" <td>6325.0</td>\n",
|
||||
" <td>6310.4</td>\n",
|
||||
" <td>6312.4</td>\n",
|
||||
" <td>297675</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>2023-01-03 09:34:00</th>\n",
|
||||
" <td>IM888</td>\n",
|
||||
" <td>6311.0</td>\n",
|
||||
" <td>6323.2</td>\n",
|
||||
" <td>6311.0</td>\n",
|
||||
" <td>6319.4</td>\n",
|
||||
" <td>352184</td>\n",
|
||||
" </tr>\n",
|
||||
" </tbody>\n",
|
||||
"</table>\n",
|
||||
"</div>"
|
||||
],
|
||||
"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": [
|
||||
"<div>\n",
|
||||
"<style scoped>\n",
|
||||
" .dataframe tbody tr th:only-of-type {\n",
|
||||
" vertical-align: middle;\n",
|
||||
" }\n",
|
||||
"\n",
|
||||
" .dataframe tbody tr th {\n",
|
||||
" vertical-align: top;\n",
|
||||
" }\n",
|
||||
"\n",
|
||||
" .dataframe thead th {\n",
|
||||
" text-align: right;\n",
|
||||
" }\n",
|
||||
"</style>\n",
|
||||
"<table border=\"1\" class=\"dataframe\">\n",
|
||||
" <thead>\n",
|
||||
" <tr style=\"text-align: right;\">\n",
|
||||
" <th></th>\n",
|
||||
" <th>open</th>\n",
|
||||
" <th>high</th>\n",
|
||||
" <th>low</th>\n",
|
||||
" <th>close</th>\n",
|
||||
" <th>volume</th>\n",
|
||||
" <th>symbol</th>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>datetime</th>\n",
|
||||
" <th></th>\n",
|
||||
" <th></th>\n",
|
||||
" <th></th>\n",
|
||||
" <th></th>\n",
|
||||
" <th></th>\n",
|
||||
" <th></th>\n",
|
||||
" </tr>\n",
|
||||
" </thead>\n",
|
||||
" <tbody>\n",
|
||||
" <tr>\n",
|
||||
" <th>2023-01-03 09:30:00</th>\n",
|
||||
" <td>IM888</td>\n",
|
||||
" <td>6280.0</td>\n",
|
||||
" <td>6306.4</td>\n",
|
||||
" <td>6277.0</td>\n",
|
||||
" <td>6302.0</td>\n",
|
||||
" <td>66894</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>2023-01-03 09:31:00</th>\n",
|
||||
" <td>IM888</td>\n",
|
||||
" <td>6302.0</td>\n",
|
||||
" <td>6320.0</td>\n",
|
||||
" <td>6302.0</td>\n",
|
||||
" <td>6318.8</td>\n",
|
||||
" <td>172512</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>2023-01-03 09:32:00</th>\n",
|
||||
" <td>IM888</td>\n",
|
||||
" <td>6319.8</td>\n",
|
||||
" <td>6328.0</td>\n",
|
||||
" <td>6314.8</td>\n",
|
||||
" <td>6314.8</td>\n",
|
||||
" <td>238716</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>2023-01-03 09:33:00</th>\n",
|
||||
" <td>IM888</td>\n",
|
||||
" <td>6313.0</td>\n",
|
||||
" <td>6325.0</td>\n",
|
||||
" <td>6310.4</td>\n",
|
||||
" <td>6312.4</td>\n",
|
||||
" <td>297675</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>2023-01-03 09:34:00</th>\n",
|
||||
" <td>IM888</td>\n",
|
||||
" <td>6311.0</td>\n",
|
||||
" <td>6323.2</td>\n",
|
||||
" <td>6311.0</td>\n",
|
||||
" <td>6319.4</td>\n",
|
||||
" <td>352184</td>\n",
|
||||
" </tr>\n",
|
||||
" </tbody>\n",
|
||||
"</table>\n",
|
||||
"</div>"
|
||||
],
|
||||
"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
|
||||
}
|
||||
33
2.数据下载与处理/tick数据转分钟数据脚本/tick_to_min.py
Normal file
33
2.数据下载与处理/tick数据转分钟数据脚本/tick_to_min.py
Normal file
@@ -0,0 +1,33 @@
|
||||
import pandas as pd
|
||||
|
||||
# 读取上传的CSV文件
|
||||
file_path = 'C:/Users/zhouj/Desktop/a次主力连续_20190103.csv'
|
||||
df = pd.read_csv(file_path, encoding='gbk')
|
||||
|
||||
# 重命名列以便处理
|
||||
df.rename(columns={'时间': 'datetime', '最新': 'price', '成交量': 'volume'}, inplace=True)
|
||||
|
||||
# 确保datetime列是datetime类型
|
||||
df['datetime'] = pd.to_datetime(df['datetime'])
|
||||
|
||||
# 设置datetime列为索引
|
||||
df.set_index('datetime', inplace=True)
|
||||
|
||||
# 使用resample方法将数据重新采样为1分钟数据
|
||||
df_resampled = df.resample('1T').agg({
|
||||
'price': ['first', 'max', 'min', 'last'],
|
||||
'volume': 'sum'
|
||||
})
|
||||
|
||||
# 重命名列名以符合K线数据的标准命名
|
||||
df_resampled.columns = ['open', 'high', 'low', 'close', 'volume']
|
||||
|
||||
# 删除存在NA值的行(如果有的时间段没有交易数据)
|
||||
df_resampled.dropna(inplace=True)
|
||||
|
||||
# 将重新采样的数据写入新的CSV文件
|
||||
output_file = 'C:/Users/zhouj/Desktop/tic_data_1min.csv'
|
||||
df_resampled.to_csv(output_file)
|
||||
|
||||
print(f'1分钟历史数据已保存至{output_file}')
|
||||
|
||||
Reference in New Issue
Block a user