{"id":2081,"date":"2025-04-08T07:58:00","date_gmt":"2025-04-07T23:58:00","guid":{"rendered":"https:\/\/blog.laoyulaoyu.top\/?p=2081"},"modified":"2025-03-23T17:35:52","modified_gmt":"2025-03-23T09:35:52","slug":"python-%e5%ba%93-vectorbt-%e5%a4%a7%e6%8f%ad%e7%a7%98%ef%bc%9a%e5%a6%82%e4%bd%95%e7%94%a8%e5%ae%83%e5%bc%80%e5%90%af%e9%bb%84%e9%87%91%e4%ba%a4%e6%98%93%e8%b4%a2%e5%af%8c%e5%a4%a7%e9%97%a8%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/laoyulaoyu.com\/index.php\/2025\/04\/08\/python-%e5%ba%93-vectorbt-%e5%a4%a7%e6%8f%ad%e7%a7%98%ef%bc%9a%e5%a6%82%e4%bd%95%e7%94%a8%e5%ae%83%e5%bc%80%e5%90%af%e9%bb%84%e9%87%91%e4%ba%a4%e6%98%93%e8%b4%a2%e5%af%8c%e5%a4%a7%e9%97%a8%ef%bc%9f\/","title":{"rendered":"Python \u5e93 VectorBT \u5927\u63ed\u79d8\uff1a\u5982\u4f55\u7528\u5b83\u5f00\u542f\u9ec4\u91d1\u4ea4\u6613\u8d22\u5bcc\u5927\u95e8\uff1f(\u9644\u6e90\u7801)"},"content":{"rendered":"\n<p>\u4f5c\u8005\uff1a<a href=\"https:\/\/www.laoyulaoyu.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">\u8001\u4f59\u635e\u9c7c<\/a><\/p>\n\n\n\n<p><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-cyan-bluish-gray-color\">\u539f\u521b\u4e0d\u6613\uff0c\u8f6c\u8f7d\u8bf7\u6807\u660e\u51fa\u5904\u53ca\u539f\u4f5c\u8005\u3002<\/mark><\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/www.laoyulaoyu.com\/wp-content\/uploads\/2025\/03\/bbea1aa5d89bb14ed789282c506bdd75d7833a25.webp\" alt=\"\" class=\"wp-image-4592\"\/><\/figure>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<pre class=\"wp-block-verse\"><strong>\u5199\u5728\u524d\u9762\u7684\u8bdd\uff1a<\/strong>\u6700\u8fd1\u5728\u7814\u7a76\u9ec4\u91d1\u4ea4\u6613\u7b56\u7565\u7684\u65f6\u5019\uff0c\u53d1\u73b0\u4e86\u4e00\u4e2a\u770b\u4e0a\u53bb\u5f88\u5389\u5bb3\u7684Python\u5e93\uff0c\u53eb<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\"> VectorBT\uff0c\u5b83\u5c31\u50cf\u662f\u4e00\u4e2a\u7ed9\u4ea4\u6613\u7b56\u7565\u52a0\u901f\u7684\u201c\u795e\u5668\u201d<\/mark>\uff0c\u4eca\u5929\u5c31\u6765\u7ed9\u5927\u5bb6\u597d\u597d\u5206\u4eab\u4e00\u4e0b\uff01<\/pre>\n<\/blockquote>\n\n\n\n<p>\u5148\u7ed9\u5927\u5bb6\u8bb2\u8bb2\u6211\u4e3a\u5565\u5bf9\u8fd9\u4e2a VectorBT \u8fd9\u4e48\u611f\u5174\u8da3\u3002\u60f3\u8c61\u4e00\u4e0b\u8fd9\u6837\u7684\u573a\u666f\uff1a\u6211\u4eec\u5750\u5728\u7535\u8111\u524d\uff0c\u9762\u5bf9\u6d77\u91cf\u7684\u4ea4\u6613\u6570\u636e\uff0c\u8111\u6d77\u4e2d\u4e0d\u65ad\u6d8c\u73b0\u51fa\u5404\u79cd\u4ea4\u6613\u7b56\u7565\u7684\u7075\u611f\uff0c\u4f46\u5982\u4f55\u9a8c\u8bc1\u8fd9\u4e9b\u60f3\u6cd5\u7684\u53ef\u884c\u6027\u5462\uff1f<\/p>\n\n\n\n<p>\u5728\u8fc7\u53bb\uff0c\u6d4b\u8bd5\u8fd9\u4e9b\u4ea4\u6613\u7b56\u7565\u53ef\u8c13\u662f\u4e00\u9879\u8270\u5de8\u7684\u4efb\u52a1\u3002\u6211\u4eec\u9700\u8981\u624b\u52a8\u8ba1\u7b97\u5927\u91cf\u6570\u636e\uff0c\u8003\u8651\u590d\u6742\u7684\u5e02\u573a\u56e0\u7d20\uff0c\u4e0d\u4ec5\u8017\u65f6\u8017\u529b\uff0c\u800c\u4e14\u7ed3\u679c\u5f80\u5f80\u4e0d\u591f\u7cbe\u786e\u3002\u7136\u800c\uff0cVectorBT\u7684\u51fa\u73b0\u5f7b\u5e95\u6539\u53d8\u4e86\u8fd9\u4e00\u5c40\u9762\uff0c\u8ba9\u6574\u4e2a\u8fc7\u7a0b\u53d8\u5f97\u5f02\u5e38\u7b80\u4fbf\uff01<\/p>\n\n\n\n<p>\u63a5\u4e0b\u6765\uff0c\u6211\u5c06\u4e3a\u5927\u5bb6\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5229\u7528VectorBT\u6765\u6784\u5efa\u548c\u6d4b\u8bd5\u9ec4\u91d1\u4ea4\u6613\u7b56\u7565\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u4e00\u3001\u9ec4\u91d1\u4ea4\u6613\u7b56\u7565\u6267\u884c<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><br><strong>\u7b2c\u4e00\u6b65\uff1a\u5de5\u5177\u51c6\u5907<\/strong><\/h3>\n\n\n\n<p>\u8fd9\u4e00\u6b65\u5c31\u662f\u5b89\u88c5\u6211\u4eec\u8981\u7528\u7684\u5e93\u3002\u5728\u4ee3\u7801\u91cc\u8f93\u5165 \u201c!pip install vectorbt numpy pandas matplotlib -q\u201d\uff0c\u8fd9\u91cc\u7684 \u201c-q\u201d \u80fd\u8ba9\u5b89\u88c5\u8fc7\u7a0b\u6084\u6084\u8fdb\u884c\uff0c\u4e0d\u6253\u6270\u54b1\u4eec\u3002<\/p>\n\n\n\n<p>\u5b89\u88c5\u597d\u540e\uff0c\u518d\u628a\u8fd9\u4e9b\u5e93\u90fd\u5bfc\u5165\u5230\u4ee3\u7801\u91cc\uff0c\u5c31\u50cf\u8fd9\u6837\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import vectorbt as vbt\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt<\/code><\/pre>\n\n\n\n<p>\u8fd9\u5c31\u597d\u6bd4\u6211\u4eec\u51c6\u5907\u597d\u4e86\u4e00\u5957\u4e13\u4e1a\u7684\u5de5\u5177\uff0c\u63a5\u4e0b\u6765\u5c31\u80fd\u5927\u5e72\u4e00\u573a\u5566\uff01<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u7b2c\u4e8c\u6b65\uff1a\u7528 VectorBT \u5f00\u542f \u201d\u6dd8\u91d1\u201c<\/strong><\/h3>\n\n\n\n<p>\u63a5\u4e0b\u6765\u54b1\u4eec\u5f97\u53bb \u201c\u627e\u5b9d\u85cf\u201d\uff0c\u4e5f\u5c31\u662f\u83b7\u53d6\u9ec4\u91d1\u7684\u4ef7\u683c\u6570\u636e\u3002VectorBT \u5f88\u5389\u5bb3\uff0c\u7528\u5b83\u7684 \u201cvbt.YFData.download\u201d \u529f\u80fd\uff0c\u4e00\u884c\u4ee3\u7801\u5c31\u80fd\u62ff\u5230\u4e94\u5e74\u7684\u9ec4\u91d1\u671f\u8d27\uff08\u201cGC=F\u201d\uff09\u6bcf\u5929\u7684\u6536\u76d8\u4ef7\u3001\u6700\u9ad8\u4ef7\u3001\u6700\u4f4e\u4ef7\u548c\u6210\u4ea4\u91cf\u6570\u636e\u3002\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ticker = \"GC=F\"\nprice = vbt.YFData.download(ticker, period=\"5y\", interval=\"1d\").get(\"Close\")\nhigh = vbt.YFData.download(ticker, period=\"5y\", interval=\"1d\").get(\"High\")\nlow = vbt.YFData.download(ticker, period=\"5y\", interval=\"1d\").get(\"Low\")\nvolume = vbt.YFData.download(ticker, period=\"5y\", interval=\"1d\").get(\"Volume\")\n<\/code><\/pre>\n\n\n\n<p>\u62ff\u5230\u6570\u636e\u540e\uff0c\u628a\u5b83\u4eec\u6574\u7406\u5230\u4e00\u4e2a pandas \u7684 DataFrame \u91cc\uff0c\u518d\u628a\u90a3\u4e9b\u6ca1\u7528\u7684\u7a7a\u6570\u636e\u53bb\u6389\uff0c\u4ee3\u7801\u662f \u201cdata.dropna (inplace=True)\u201d\uff0c\u8fd9\u6837\u6570\u636e\u5c31\u5e72\u5e72\u51c0\u51c0\uff0c\u80fd\u7528\u6765\u5206\u6790\u5566\uff01<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u7b2c\u4e09\u6b65\uff1a\u6307\u6807\u8ba1\u7b97<\/strong><\/h3>\n\n\n\n<p>\u63a5\u4e0b\u6765\u5c31\u662f\u795e\u5947\u7684\u6307\u6807\u8ba1\u7b97\u73af\u8282\u5566\uff01VectorBT \u81ea\u5e26\u5f88\u591a\u8ba1\u7b97\u6307\u6807\u7684\u5de5\u5177\uff0c\u6bd4\u5982\u8ba1\u7b97\u76f8\u5bf9\u5f3a\u5f31\u6307\u6570\uff08RSI\uff09\uff0c\u7528 \u201cvbt.RSI.run\u201d \u5c31\u80fd\u8f7b\u677e\u641e\u5b9a\uff0c14 \u5929\u7684 RSI \u4e00\u4e0b\u5b50\u5c31\u7b97\u51fa\u6765\u4e86\u3002\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>rsi = vbt.RSI.run(data&#91;'Close'], window=14)\ndata&#91;'RSI'] = rsi.rsi<\/code><\/pre>\n\n\n\n<p>\u8981\u662f\u4f60\u60f3\u8981\u4e00\u4e9b\u66f4\u4e2a\u6027\u5316\u7684\u6307\u6807\uff0c\u4e5f\u6ca1\u95ee\u9898\uff01\u50cf\u6210\u4ea4\u91cf\u53d8\u5316\u7387\uff08VROC\uff09\u548c\u5a01\u5ec9\u6307\u6807\uff08Williams % R\uff09\uff0c\u81ea\u5df1\u5199\u4e2a\u51fd\u6570\u5c31\u80fd\u7b97\u51fa\u6765\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u7b2c\u56db\u6b65\uff1a\u7b56\u7565\u5236\u5b9a<\/strong><\/h3>\n\n\n\n<p>\u6709\u4e86\u6307\u6807\uff0c\u5c31\u8981\u5236\u5b9a\u4ea4\u6613\u7b56\u7565\u5566\uff01\u8fd9\u53ef\u662f\u5173\u952e\u7684\u4e00\u6b65\u3002\u6211\u7528 VectorBT \u5236\u5b9a\u4e86\u56db\u4e2a\u7b56\u7565\uff1a<\/p>\n\n\n\n<p><strong>\u79fb\u52a8\u5e73\u5747\u7ebf\u7b56\u7565<\/strong>\uff1a\u5f53\u4ef7\u683c\u5927\u4e8e 50 \u65e5\u79fb\u52a8\u5e73\u5747\u7ebf\u65f6\uff0c\u5c31\u4e70\u5165\uff1b\u5f53\u4ef7\u683c\u5c0f\u4e8e 50 \u65e5\u79fb\u52a8\u5e73\u5747\u7ebf\u65f6\uff0c\u5c31\u5356\u51fa\u3002<\/p>\n\n\n\n<p><strong>RSI \u7b56\u7565<\/strong>\uff1aRSI \u5c0f\u4e8e 30 \u7684\u65f6\u5019\uff0c\u8bf4\u660e\u5e02\u573a\u8d85\u5356\uff0c\u662f\u4e70\u5165\u4fe1\u53f7\uff1bRSI \u5927\u4e8e 70 \u7684\u65f6\u5019\uff0c\u5e02\u573a\u8d85\u4e70\uff0c\u5c31\u5356\u51fa\u3002<\/p>\n\n\n\n<p><strong>VROC \u7b56\u7565<\/strong>\uff1a\u6210\u4ea4\u91cf\u53d8\u5316\u7387\u5927\u4e8e 0\uff0c\u5c31\u4e70\u5165\uff1b\u5c0f\u4e8e 0\uff0c\u5c31\u5356\u51fa\u3002<\/p>\n\n\n\n<p><strong>\u5a01\u5ec9\u6307\u6807\u7b56\u7565<\/strong>\uff1a\u5a01\u5ec9\u6307\u6807\u5c0f\u4e8e -80\uff0c\u4e70\u5165\uff1b\u5927\u4e8e -20\uff0c\u5356\u51fa\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u7b2c\u4e94\u6b65\uff1a\u56de\u6d4b<\/strong><\/h3>\n\n\n\n<p>\u5236\u5b9a\u597d\u7b56\u7565\u540e\uff0c\u5c31\u53ef\u4ee5\u7528 VectorBT \u8fdb\u884c\u56de\u6d4b\u5566\uff01\u7528 \u201cvbt.Portfolio.from_signals\u201d \u8fd9\u4e2a\u51fd\u6570\uff0c\u628a\u6211\u4eec\u7684\u7b56\u7565\u653e\u8fdb\u53bb\uff0c\u8fd8\u80fd\u8bbe\u7f6e\u4ea4\u6613\u624b\u7eed\u8d39\u3001\u6ed1\u70b9\uff08\u5c31\u662f\u4ea4\u6613\u65f6\u5b9e\u9645\u4ef7\u683c\u548c\u9884\u671f\u4ef7\u683c\u7684\u5dee\u8ddd\uff09\u8fd9\u4e9b\u771f\u5b9e\u5e02\u573a\u4f1a\u9047\u5230\u7684\u56e0\u7d20\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>results = {}\nfor strategy_name, strategy_params in strategies.items():\n    portfolio = vbt.Portfolio.from_signals(\n        close=data&#91;'Close'],\n        entries=strategy_params&#91;\"entries\"],\n        exits=strategy_params&#91;\"exits\"],\n        size=np.inf,\n        fees=0.002,\n        slippage=0.001,\n        freq='D'\n    )\n    results&#91;strategy_name] = portfolio<\/code><\/pre>\n\n\n\n<p>\u56de\u6d4b\u5b8c\uff0c\u5c31\u5230\u4e86\u770b\u7ed3\u679c\u7684\u65f6\u5019\u5566\uff01<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u7b2c\u516d\u6b65\uff1a\u53ef\u89c6\u5316\u4e0e\u7edf\u8ba1<\/strong><\/h3>\n\n\n\n<p>VectorBT \u4e0d\u4ec5\u80fd\u7b97\u51fa\u5404\u79cd\u6570\u636e\uff0c\u8fd8\u80fd\u628a\u7ed3\u679c\u7528\u56fe\u8868\u5c55\u793a\u51fa\u6765\uff0c\u8d85\u7ea7\u76f4\u89c2\u3002\u8ba9\u6211\u4eec\u904d\u5386\u8fd9\u4e9b\u7b56\u7565\uff0c\u770b\u770b\u968f\u7740\u65f6\u95f4\u63a8\u79fb\u6211\u4eec\u7684\u6295\u8d44\u7ec4\u5408\u8868\u73b0\u5982\u4f55\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># \u7b2c\u516d\u6b65\uff1a\u5206\u6790\u548c\u6bd4\u8f83\u7ed3\u679c\nfor strategy_name, portfolio in results.items():\n    print(f\"--- {strategy_name} ---\")\n    print(\"\u6027\u80fd\u6307\u6807:\")\n    print(portfolio.stats())\n    print(\"\\n\")\n    # \u786e\u4fdd\u56fe\u8868\u663e\u793a\n    portfolio.value().plot(title=f\"{strategy_name} - \u6295\u8d44\u7ec4\u5408\u4ef7\u503c\u968f\u65f6\u95f4\u53d8\u5316\")\n    plt.show()\n    portfolio.trades.plot()\n    plt.show()<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u4e8c\u3001\u9ec4\u91d1\u4ea4\u6613\u7b56\u7565\u6bd4\u8f83<\/strong><\/h2>\n\n\n\n<p>\u6211\u56de\u6d4b\u4e86\u4ece 2020 \u5e74 4 \u6708\u5230 2025 \u5e74 3 \u6708\u8fd9\u4e94\u5e74\u7684\u9ec4\u91d1\u4ea4\u6613\u7b56\u7565\uff0c\u53d1\u73b0\u4e0d\u540c\u7b56\u7565\u8868\u73b0\u5dee\u522b\u5f88\u5927\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/www.laoyulaoyu.com\/wp-content\/uploads\/2025\/03\/image-75.png\" alt=\"\" class=\"wp-image-4593\"\/><\/figure>\n\n\n\n<p><strong>\u5a01\u5ec9\u6307\u6807\u7b56\u7565\u8868\u73b0<\/strong>\uff1a\u5728\u6d4b\u8bd5\u671f\u5185\uff0c\u8be5\u7b56\u7565\u5448\u73b0\u51fa\u6301\u7eed\u589e\u957f\u7684\u6001\u52bf\uff0c\u6700\u7ec8\u603b\u56de\u62a5\u7387\u8fbe\u5230 18.14%\u3002\u6ce8\u610f\u5b83\u5728 2023 \u5e74\u5e74\u4e2d\u5e02\u573a\u4e0b\u8dcc\u65f6\u7684\u8868\u73b0\uff0c\u4ee5\u53ca\u5728 2024 &#8211; 2025 \u5e74\u7684\u5f3a\u52b2\u590d\u82cf\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/www.laoyulaoyu.com\/wp-content\/uploads\/2025\/03\/image-76.png\" alt=\"\" class=\"wp-image-4594\"\/><\/figure>\n\n\n\n<p><strong>VROC \u7b56\u7565\u8868\u73b0<\/strong>\uff1a\u6210\u4ea4\u91cf\u53d8\u5316\u7387\u7b56\u7565\u5728\u6d4b\u8bd5\u671f\u5185\u4ef7\u503c\u635f\u5931\u8d85\u8fc7\u4e00\u534a\uff0c\u8fd9\u8868\u660e\u4ec5\u57fa\u4e8e\u6210\u4ea4\u91cf\u7684\u4fe1\u53f7\u53ef\u80fd\u5e76\u4e0d\u53ef\u9760\u3002\u5e73\u7a33\u7684\u4e0b\u964d\u8d8b\u52bf\u8868\u660e\uff0c\u65e0\u8bba\u5e02\u573a\u72b6\u51b5\u5982\u4f55\uff0c\u8be5\u7b56\u7565\u59cb\u7ec8\u8868\u73b0\u4e0d\u4f73\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/www.laoyulaoyu.com\/wp-content\/uploads\/2025\/03\/image-77.png\" alt=\"\" class=\"wp-image-4595\"\/><\/figure>\n\n\n\n<p><strong>RSI \u7b56\u7565\u8868\u73b0<\/strong>\uff1a\u5728\u67d0\u4e9b\u65f6\u671f\u8868\u73b0\u5f3a\u52b2\uff0c\u7279\u522b\u662f\u5728 2022 \u5e74\u548c 2025 \u5e74\u521d\u3002\u6c34\u5e73\u7684\u76f4\u7ebf\u90e8\u5206\u8868\u793a\u8be5\u7b56\u7565\u9000\u51fa\u5e02\u573a\u7684\u65f6\u671f\uff08\u65e2\u672a\u8fbe\u5230\u8d85\u4e70\u4e5f\u672a\u8fbe\u5230\u8d85\u5356\u6761\u4ef6\uff09\u3002\u5b83\u6293\u4f4f\u4e86\u51e0\u4e2a\u663e\u8457\u7684\u4e0a\u5347\u8d8b\u52bf\uff0c\u540c\u65f6\u907f\u5f00\u4e86\u4e00\u4e9b\u4e0b\u8dcc\u8d8b\u52bf\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/www.laoyulaoyu.com\/wp-content\/uploads\/2025\/03\/image-78.png\" alt=\"\" class=\"wp-image-4596\"\/><\/figure>\n\n\n\n<p><strong>\u79fb\u52a8\u5e73\u5747\u7ebf\u7b56\u7565\u8868\u73b0<\/strong>\uff1a\u8be5\u7b56\u7565\u7ecf\u5386\u4e86\u6700\u9ad8\u7684\u6ce2\u52a8\u6027\uff0c\u5728 2021 \u5e74\u521d\u5927\u5e45\u4e0b\u8dcc\u540e\u51fa\u73b0\u4e86\u6025\u5267\u4e0a\u6da8\u30022024 &#8211; 2025 \u5e74\u7684\u590d\u82cf\u8868\u660e\uff0c\u8fd9\u79cd\u7b56\u7565\u5728\u8d8b\u52bf\u660e\u663e\u7684\u5e02\u573a\u4e2d\u8868\u73b0\u826f\u597d\uff0c\u4f46\u5728\u5e02\u573a\u76d8\u6574\u671f\u4f1a\u9047\u5230\u56f0\u96be\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u4e09\u3001\u7528 VectorBT \u62d3\u5c55\u4f60\u7684\u5206\u6790<\/strong><\/h2>\n\n\n\n<p>\u9664\u4e86\u4e0a\u9762\u8fd9\u4e9b\u57fa\u7840\u7684\u7528\u6cd5\uff0cVectorBT \u8fd8\u6709\u5f88\u591a\u9ad8\u7ea7\u73a9\u6cd5\u3002\u6bd4\u5982\uff1a<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3.1 \u53c2\u6570\u4f18\u5316<\/strong><\/h3>\n\n\n\n<p>\u53ef\u4ee5\u8fdb\u4e00\u6b65\u4f18\u5316\u7b56\u7565\u7684\u53c2\u6570\uff0c\u627e\u5230\u6700\u9002\u5408\u7684\u6307\u6807\u6570\u503c.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>strategies = {\n    \"MA\u7b56\u7565\": {\n        # \u4e70\u5165\u4fe1\u53f7\uff1a\u4ef7\u683c\u4e0a\u7a7f\u77ed\u671f\u79fb\u52a8\u5e73\u5747\u7ebf\uff08\u4f8b\u5982\uff0c50\u65e5\u79fb\u52a8\u5e73\u5747\u7ebf\uff09\n        # \u5356\u51fa\u4fe1\u53f7\uff1a\u4ef7\u683c\u4e0b\u7a7f\u77ed\u671f\u79fb\u52a8\u5e73\u5747\u7ebf\n        \"entries\": data&#91;'Close'] &gt; data&#91;'Close'].rolling(window=50).mean(),\n        \"exits\": data&#91;'Close'] &lt; data&#91;'Close'].rolling(window=50).mean()\n    },\n    \"RSI\u7b56\u7565\": {\n        # \u4e70\u5165\u4fe1\u53f7\uff1aRSI &lt; 30\uff08\u8d85\u5356\uff09\n        # \u5356\u51fa\u4fe1\u53f7\uff1aRSI &gt; 70\uff08\u8d85\u4e70\uff09\n        \"entries\": data&#91;'RSI'] &lt; 30,\n        \"exits\": data&#91;'RSI'] &gt; 70\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3.2 \u591a\u6307\u6807\u7ec4\u5408<\/strong><\/h3>\n\n\n\n<p>\u5c06\u591a\u4e2a\u6307\u6807\u7ec4\u5408\u8d77\u6765\uff0c\u5236\u5b9a\u66f4\u590d\u6742\u7684\u7b56\u7565\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># \u793a\u4f8b\uff1a\u7ed3\u5408RSI\u548c\u5a01\u5ec9\u6307\u6807\u786e\u5b9a\u4e70\u5356\u4fe1\u53f7\ncombined_entries = (data&#91;'RSI'] &lt; 30) &amp; (data&#91;'Williams %R'] &lt; -80)  # \u4e24\u8005\u90fd\u8868\u660e\u8d85\u5356\ncombined_exits = (data&#91;'RSI'] &gt; 70) | (data&#91;'Williams %R'] &gt; -20)    # \u4efb\u610f\u4e00\u4e2a\u8868\u660e\u8d85\u4e70\n\n# \u7528\u7ec4\u5408\u7b56\u7565\u8fdb\u884c\u56de\u6d4b\ncombined_portfolio = vbt.Portfolio.from_signals(\n    close=data&#91;'Close'],\n    entries=combined_entries,\n    exits=combined_exits,\n    size=np.inf,\n    fees=0.002,\n    slippage=0.001,\n    freq='D'\n)\nprint(\"\u7ec4\u5408\u7b56\u7565\u6027\u80fd:\")\nprint(combined_portfolio.stats())<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3.3 \u5b9e\u65bd\u6b62\u635f<\/strong><\/h3>\n\n\n\n<p>\u8fd8\u80fd\u8bbe\u7f6e\u6b62\u635f\uff0c\u63a7\u5236\u4ea4\u6613\u98ce\u9669\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># \u793a\u4f8b\uff1a\u5e26\u67095%\u6b62\u635f\u7684\u5a01\u5ec9\u6307\u6807\u7b56\u7565\nportfolio_with_sl = vbt.Portfolio.from_signals(\n    close=data&#91;'Close'],\n    entries=data&#91;'Williams %R'] &lt; -80,\n    exits=data&#91;'Williams %R'] &gt; -20,\n    sl_stop=0.05,  # 5%\u6b62\u635f\n    size=np.inf,\n    fees=0.002,\n    slippage=0.001,\n    freq='D'\n)\nprint(\"\u5e26\u6b62\u635f\u7b56\u7565\u7684\u6027\u80fd:\")\nprint(portfolio_with_sl.stats())<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3.4 \u6e90\u4ee3\u7801\u4e0b\u8f7d<\/strong><\/h3>\n\n\n\n<p>\u4ee5\u4e0a\u7684\u6e90\u4ee3\u7801\u8bf7\u5728\u6211\u7684Google Colab\u4e2d\u7533\u8bf7\u4e0b\u8f7d\uff0c\u5185\u9644\u4e2d\u6587\u6784\u5efa\u8bf4\u660e\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/www.laoyulaoyu.com\/wp-content\/uploads\/2025\/03\/1742271818207.png\" alt=\"\" class=\"wp-image-4598\"\/><\/figure>\n\n\n\n<p>\u5982\u89c9\u5f97\u5bf9\u60a8\u6709\u6240\u5e2e\u52a9\uff0c\u8bf7\u7559\u8a00\u6216\u8005\u79c1\u4fe1\u6211\u7d22\u8981\u5b8c\u6574\u6e90\u4ee3\u7801\uff0c\u540c\u65f6\u8bf7\u6211\u559d\u676f\u5496\u5561\u5c31\u597d\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u56db\u3001\u89c2\u70b9\u603b\u7ed3<\/strong><\/h2>\n\n\n\n<p>VectorBT\u771f\u7684\u662f\u4e00\u4e2a\u975e\u5e38\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u5b83\u80fd\u8ba9\u6211\u4eec\u5728\u4ea4\u6613\u7b56\u7565\u7684\u7814\u7a76\u4e0a\u66f4\u52a0\u79d1\u5b66\u3001\u9ad8\u6548\u3002\u901a\u8fc7\u7528\u5b83\u56de\u6d4b\u9ec4\u91d1\u4ea4\u6613\u7b56\u7565\uff0c\u6211\u4eec\u80fd\u4ece\u5386\u53f2\u6570\u636e\u4e2d\u627e\u5230\u89c4\u5f8b\uff0c\u53d1\u73b0\u66f4\u9002\u5408\u81ea\u5df1\u7684\u4ea4\u6613\u65b9\u6cd5\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u591a\u7b56\u7565\u6d4b\u8bd5<\/strong>\uff1a\u53ef\u4ee5\u540c\u65f6\u6d4b\u8bd5\u591a\u4e2a\u4ea4\u6613\u7b56\u7565\uff0c\u5feb\u901f\u5bf9\u6bd4\u4e0d\u540c\u7b56\u7565\u7684\u4f18\u52a3\u3002<\/li>\n\n\n\n<li><strong>\u81ea\u52a8\u751f\u6210\u6307\u6807<\/strong>\uff1a\u81ea\u52a8\u751f\u6210\u8be6\u7ec6\u7684\u6027\u80fd\u6307\u6807\uff0c\u5e2e\u52a9\u6211\u4eec\u6df1\u5165\u5206\u6790\u7b56\u7565\u8868\u73b0\u3002<\/li>\n\n\n\n<li><strong>\u53ef\u89c6\u5316\u7ed3\u679c<\/strong>\uff1a\u7528\u56fe\u8868\u5c55\u793a\u56de\u6d4b\u7ed3\u679c\uff0c\u8ba9\u6570\u636e\u66f4\u52a0\u76f4\u89c2\u6613\u61c2\u3002<\/li>\n\n\n\n<li><strong>\u9ad8\u7ea7\u73a9\u6cd5\u591a\u6837<\/strong>\uff1a\u652f\u6301\u53c2\u6570\u4f18\u5316\u3001\u6307\u6807\u7ec4\u5408\u548c\u6b62\u635f\u8bbe\u7f6e\u7b49\u9ad8\u7ea7\u529f\u80fd\uff0c\u6ee1\u8db3\u4e0d\u540c\u9700\u6c42\u3002<\/li>\n\n\n\n<li><strong>\u9002\u7528\u8303\u56f4\u5e7f<\/strong>\uff1a\u4e0d\u7ba1\u662f\u9ec4\u91d1\u3001\u80a1\u7968\uff0c\u8fd8\u662f\u6570\u5b57\u8d27\u5e01\u3001\u5916\u6c47\u4ea4\u6613\uff0c\u90fd\u80fd\u4f7f\u7528 VectorBT \u6765\u6d4b\u8bd5\u7b56\u7565\u3002<\/li>\n<\/ul>\n\n\n\n<p><em>\u611f\u8c22\u60a8\u9605\u8bfb\u5230\u6700\u540e\uff0c\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u4e3a\u60a8\u5e26\u6765\u4e86\u65b0\u7684\u542f\u53d1\u548c\u5b9e\u7528\u7684\u77e5\u8bc6\uff01\u5982\u679c\u89c9\u5f97\u6709\u5e2e\u52a9\uff0c\u8bf7\u4e0d\u541d\u70b9\u8d5e\u548c\u5206\u4eab\uff0c\u60a8\u7684\u652f\u6301\u662f\u6211\u6301\u7eed\u521b\u4f5c\u7684\u52a8\u529b\u3002\u795d\u60a8\u6295\u8d44\u987a\u5229\uff0c\u6536\u76ca\u957f\u8679\uff01\u5982\u679c\u5bf9\u6587\u4e2d\u5185\u5bb9\u6709\u4efb\u4f55\u7591\u95ee\uff0c\u6b22\u8fce\u7559\u8a00\uff0c\u6211\u4f1a\u5c3d\u5feb\u56de\u590d\uff01<\/em><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p class=\"has-text-align-center\">\u672c\u6587\u5185\u5bb9\u4ec5\u9650\u6280\u672f\u63a2\u8ba8\u548c\u5b66\u4e60\uff0c\u4e0d\u6784\u6210\u4efb\u4f55\u6295\u8d44\u5efa\u8bae\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f5c\u8005\uff1a\u8001\u4f59\u635e\u9c7c \u539f\u521b\u4e0d\u6613\uff0c\u8f6c\u8f7d\u8bf7\u6807\u660e\u51fa\u5904\u53ca\u539f\u4f5c\u8005\u3002&#8230;<\/p>\n<div class=\"more-link-wrapper\"><a class=\"more-link\" href=\"https:\/\/laoyulaoyu.com\/index.php\/2025\/04\/08\/python-%e5%ba%93-vectorbt-%e5%a4%a7%e6%8f%ad%e7%a7%98%ef%bc%9a%e5%a6%82%e4%bd%95%e7%94%a8%e5%ae%83%e5%bc%80%e5%90%af%e9%bb%84%e9%87%91%e4%ba%a4%e6%98%93%e8%b4%a2%e5%af%8c%e5%a4%a7%e9%97%a8%ef%bc%9f\/\">Continue reading<span class=\"screen-reader-text\">Python \u5e93 VectorBT \u5927\u63ed\u79d8\uff1a\u5982\u4f55\u7528\u5b83\u5f00\u542f\u9ec4\u91d1\u4ea4\u6613\u8d22\u5bcc\u5927\u95e8\uff1f(\u9644\u6e90\u7801)<\/span><\/a><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[4,5,16],"class_list":{"0":"post-2081","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"hentry","6":"category-aiinvest","7":"tag-ai","9":"tag-16","10":"entry"},"_links":{"self":[{"href":"https:\/\/laoyulaoyu.com\/index.php\/wp-json\/wp\/v2\/posts\/2081","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/laoyulaoyu.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/laoyulaoyu.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/laoyulaoyu.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/laoyulaoyu.com\/index.php\/wp-json\/wp\/v2\/comments?post=2081"}],"version-history":[{"count":3,"href":"https:\/\/laoyulaoyu.com\/index.php\/wp-json\/wp\/v2\/posts\/2081\/revisions"}],"predecessor-version":[{"id":2142,"href":"https:\/\/laoyulaoyu.com\/index.php\/wp-json\/wp\/v2\/posts\/2081\/revisions\/2142"}],"wp:attachment":[{"href":"https:\/\/laoyulaoyu.com\/index.php\/wp-json\/wp\/v2\/media?parent=2081"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/laoyulaoyu.com\/index.php\/wp-json\/wp\/v2\/categories?post=2081"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/laoyulaoyu.com\/index.php\/wp-json\/wp\/v2\/tags?post=2081"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}