{ "cells": [ { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "# 加载功能模块\n", "from datetime import datetime\n", "\n", "from vnpy.trader.constant import Interval\n", "\n", "from elite_optionstrategy import BacktestingEngine\n", "\n", "from buy_option_strategy import BuyOptionStrategy" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "# 创建回测引擎\n", "engine = BacktestingEngine()\n", "\n", "engine.set_parameters(\n", " interval=Interval.MINUTE,\n", " start=datetime(2023, 1, 1),\n", " end=datetime(2023, 6, 30),\n", " rate=0,\n", " slippage=0.6 + (16 / 100),\n", ")\n", "\n", "engine.add_strategy(BuyOptionStrategy, {})" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "tags": [] }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ " 1%| | 2/180 [00:00<00:16, 10.53it/s]" ] }, { "name": "stdout", "output_type": "stream", "text": [ "2024-03-09 09:42:27.361177\t触发异常,回测终止\n", "2024-03-09 09:42:27.362176\tTraceback (most recent call last):\n", " File \"C:\\veighna_elite_simulation\\lib\\site-packages\\elite_optionstrategy\\backtesting.py\", line 114, in run_backtesting\n", " File \"C:\\veighna_elite_simulation\\lib\\site-packages\\elite_optionstrategy\\backtesting.py\", line 397, in _run_intraday\n", " File \"C:\\veighna_elite_simulation\\lib\\site-packages\\elite_optionstrategy\\backtesting.py\", line 442, in _new_bars\n", " File \"D:\\Gitee_Code\\trading_strategy\\VNPY_Code\\Option_spread_strategy\\使用文档\\19\\buy_option_strategy.py\", line 66, in on_bars\n", " portfolio: PortfolioData = self.get_portfolio(self.option_portfolio)\n", " File \"C:\\veighna_elite_simulation\\lib\\site-packages\\elite_optionstrategy\\template.py\", line 247, in get_portfolio\n", "KeyError: 'IO'\n", "\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "# 历史数据回放\n", "engine.run_backtesting()" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2024-03-09 09:28:32.115670\t开始计算逐日盯市盈亏\n", "2024-03-09 09:28:32.115670\t逐日盯市盈亏计算完成\n" ] } ], "source": [ "# 计算每日盈亏\n", "engine.calculate_result()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# 统计绩效结果\n", "result = engine.calculate_statistics()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# 显示资金图表\n", "engine.show_chart()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# 打印逐笔成交\n", "for trade in engine.all_trades.values():\n", " print(trade.datetime, trade.vt_symbol, trade.direction.value, trade.offset.value, trade.volume, \"@\", trade.price)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for result in engine.daily_results.values():\n", " print(result.today_close_data[\"IO2201-C-4900.CFFEX\"])" ] }, { "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 }