增加交易策略、交易指标、量化库代码等文件夹
This commit is contained in:
199
5.课程代码/2.Option_spread_strategy/使用文档/16/16_demo.ipynb
Normal file
199
5.课程代码/2.Option_spread_strategy/使用文档/16/16_demo.ipynb
Normal file
@@ -0,0 +1,199 @@
|
||||
{
|
||||
"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\"] = \"4aff6f3b0dcfc990ec9476213ba784e17c34e757\""
|
||||
]
|
||||
},
|
||||
{
|
||||
"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"
|
||||
]
|
||||
},
|
||||
{
|
||||
"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": 23,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"数据长度 51143\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# 查询期权历史数据\n",
|
||||
"req = HistoryRequest(\n",
|
||||
" symbol=\"m2205-C-3000\",\n",
|
||||
" exchange=Exchange.DCE,#SZSE\n",
|
||||
" start=datetime(2017, 1, 1),\n",
|
||||
" end=datetime.now(),\n",
|
||||
" interval=Interval.MINUTE\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"bars = datafeed.query_bar_history(req)\n",
|
||||
"print(\"数据长度\", len(bars))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 9,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"ename": "IndexError",
|
||||
"evalue": "list index out of range",
|
||||
"output_type": "error",
|
||||
"traceback": [
|
||||
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
|
||||
"\u001b[1;31mIndexError\u001b[0m Traceback (most recent call last)",
|
||||
"Cell \u001b[1;32mIn[9], line 2\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[38;5;66;03m# 查看K线数据内容\u001b[39;00m\n\u001b[1;32m----> 2\u001b[0m \u001b[43mbars\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m]\u001b[49m\n",
|
||||
"\u001b[1;31mIndexError\u001b[0m: list index out of range"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# 查看K线数据内容\n",
|
||||
"bars[0]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# 创建SQLite数据库实例并写入数据\n",
|
||||
"db1 = SqliteDatabase()\n",
|
||||
"db1.save_bar_data(bars)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# 测试SQLite数据查询性能\n",
|
||||
"%timeit bars = db1.load_bar_data(req.symbol, req.exchange, req.interval, req.start, req.end)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# 重新查询一次数据\n",
|
||||
"bars = datafeed.query_bar_history(req)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"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": [
|
||||
"# 测试Elite数据查询性能\n",
|
||||
"%timeit bars = db2.load_bar_data(req.symbol, req.exchange, req.interval, req.start, req.end)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"153/6.25"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"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
|
||||
}
|
||||
Reference in New Issue
Block a user