Files
Quant_Code/2.数据下载与处理/tick数据转分钟数据脚本/tick_to_min.py
Win_home f925dff46b 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
2025-03-15 22:45:08 +08:00

34 lines
1.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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}')