{ "cells": [ { "cell_type": "code", "execution_count": 1, "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 AdvancedSpreadStrategy_dualthrust2 import AdvancedSpreadStrategy" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "设置回测的参数。参数及其含义如下 1. vt_symbol ==> 产品名称 2. interval ==> 周期 3. start ==> 开始时间 4. rate ==> 手续费 5. slippage ==> 滑点 6. size ==> 合约乘数 7. pricetick ==> 价格跳动 8. capital ==> 回测资本 9. end ==> 截止时间 10. mode ==> 回测的模式,一共有两种:BacktestingMode.BAR和BacktestingMode.TICK 11. inverse ==> 周期" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "# 创建回测引擎\n", "engine = BacktestingEngine()\n", "\n", "engine.set_parameters(\n", " interval=Interval.MINUTE,\n", " start=datetime(2024, 7, 1),\n", " end=datetime(2024, 8, 30),\n", " rate=0,\n", " slippage=0.6 + (16 / 100),\n", " capital= 1000000\n", ")\n", "\n", "\n", "setting = {\n", " # \"aroon_window\": 5,\n", " \"k1\":0.3,\n", " \"k2\": 0.9,\n", " \"percent_add\": 0,\n", " \"otm_level\": 0\n", "}\n", "engine.add_strategy(AdvancedSpreadStrategy, setting)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ " 72%|███████▏ | 44/61 [02:21<00:55, 3.28s/it]" ] } ], "source": [ "# 历史数据回放\n", "engine.run_backtesting()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "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": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "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 }