{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "# 配置迅投研数据服务\n", "from vnpy.trader.setting import SETTINGS\n", "\n", "SETTINGS[\"datafeed.name\"] = \"xt\"\n", "SETTINGS[\"datafeed.username\"] = \"token\"\n", "SETTINGS[\"datafeed.password\"] = \"ef326f853a744c58572f0158d470912c38a09552\"" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "# 加载功能模块\n", "from datetime import datetime\n", "\n", "from vnpy.trader.datafeed import get_datafeed\n", "from vnpy.trader.object import HistoryRequest, Exchange, Interval\n", "\n", "from vnpy_sqlite import Database as SqliteDatabase\n", "#from elite_database import Database as EliteDatabase\n", "\n", "#增加\n", "import pandas as pd" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 初始化数据服务\n", "datafeed = get_datafeed()\n", "datafeed.init()" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "# 交易所映射关系\n", "EXCHANGE_XT2VT = {\n", " \"SH\": Exchange.SSE,\n", " \"SZ\": Exchange.SZSE,\n", " \"BJ\": Exchange.BSE,\n", " \"SF\": Exchange.SHFE,\n", " \"IF\": Exchange.CFFEX,\n", " \"INE\": Exchange.INE,\n", " \"DF\": Exchange.DCE,\n", " \"ZF\": Exchange.CZCE,\n", " \"GF\": Exchange.GFEX\n", "}" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "数据长度 41336\n" ] } ], "source": [ "# 查询期货历史数据\n", "req = HistoryRequest(\n", " symbol=\"rb00\", # 加权指数 \n", " # symbol=\"IF00\", # 主力连续(未平滑)\n", " # exchange=Exchange.CFFEX,\n", " exchange = EXCHANGE_XT2VT[\"SF\"],\n", " start=datetime(2023, 1, 1),\n", " end=datetime(2023, 11, 24),#end=datetime.now(),\n", " interval=Interval.TICK\n", ")\n", "\n", "ticks = datafeed.query_tick_history(req)\n", "print(\"数据长度\", len(ticks))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# 创建Elite数据库实例并写入数据\n", "#db2 = EliteDatabase()\n", "#db2.save_bar_data(bars)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df = pd.DataFrame(ticks)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# 创建CSV文件并写入数据\n", "filepath = \"rb00_11.csv\" # CSV文件保存路径及文件名\n", "df.to_csv(filepath, index=False) # index参数设置为False表示不包含索引列\n", "#df.to_csv(filepath, mode='a', index=False, header=False) # index参数设置为False表示不包含索引列" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# 读取CSV文件\n", "data = pd.read_csv(\"IC0.csv\")\n", "# 对数据进行排序\n", "sorted_data = data.sort_values(by='datetime')\n", "# 将排序结果写入CSV文件\n", "sorted_data.to_csv('sort_IC00.csv', index=False)" ] } ], "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" }, "vscode": { "interpreter": { "hash": "1b43cb0bd93d5abbadd54afed8252f711d4681fe6223ad6b67ffaee289648f85" } } }, "nbformat": 4, "nbformat_minor": 4 }