{"id":1533,"date":"2024-10-21T07:58:00","date_gmt":"2024-10-20T23:58:00","guid":{"rendered":"https:\/\/blog.laoyulaoyu.top\/?p=1533"},"modified":"2024-09-24T16:00:55","modified_gmt":"2024-09-24T08:00:55","slug":"%e7%9c%9f%e6%ad%a3%e6%89%8b%e6%8a%8a%e6%89%8b%e6%95%99%e4%bc%9a%e4%bd%a0%ef%bc%9a%e7%94%a8-python-%e6%9e%84%e5%bb%ba%e7%bd%91%e6%a0%bc%e4%ba%a4%e6%98%93%e7%ad%96%e7%95%a5","status":"publish","type":"post","link":"https:\/\/laoyulaoyu.com\/index.php\/2024\/10\/21\/%e7%9c%9f%e6%ad%a3%e6%89%8b%e6%8a%8a%e6%89%8b%e6%95%99%e4%bc%9a%e4%bd%a0%ef%bc%9a%e7%94%a8-python-%e6%9e%84%e5%bb%ba%e7%bd%91%e6%a0%bc%e4%ba%a4%e6%98%93%e7%ad%96%e7%95%a5\/","title":{"rendered":"\u771f\u6b63\u624b\u628a\u624b\u6559\u4f1a\u4f60\uff1a\u7528 Python \u6784\u5efa\u7f51\u683c\u4ea4\u6613\u7b56\u7565"},"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\/2024\/09\/image-222.png\" alt=\"\" class=\"wp-image-2225\"\/><\/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>\u7f51\u7edc\u4e0a\u5145\u65a5\u7740\u5927\u91cf\u6709\u5173\u4f7f\u7528 Python \u6784\u5efa\u7f51\u683c\u4ea4\u6613\u7b56\u7565\u7684\u6587\u7ae0\uff0c\u6211\u501f\u52a9 AI \u5de5\u5177\u8fc5\u901f\u6d4f\u89c8\u4e86\u4e00\u756a\uff0c\u7ed3\u679c\u53d1\u73b0\u8981\u4e48\u662f\u628a\u7b80\u5355\u7684\u4e8b\u60c5\u590d\u6742\u5316\u7684\u6280\u672f\u6587\uff0c\u8981\u4e48\u662f\u90a3\u79cd\u8bf4\u4e00\u534a\u7559\u4e00\u534a\u7684 \u201c\u94a9\u5b50\u201d \u6587\uff0c\u800c\u6700\u591a\u7684\u8fd8\u662f\u4f5c\u8005\u81ea\u5df1\u4e5f\u4e0d\u592a\u660e\u767d\u5c31\u642c\u8fd0\u8fc7\u6765\u7684\u586b\u5751\u6587\u3002\u56e0\u6b64\uff0c\u6211\u6253\u7b97\u64b0\u5199\u4e00\u7bc7\u80fd\u591f\u8ba9\u5927\u5bb6\u8ddf\u7740\u64cd\u4f5c\u5c31\u80fd\u5b8c\u6210\u7684\u624b\u628a\u624b\u6307\u5bfc\u6587\u3002\u6211\u5c06\u6309\u7167\u73af\u5883\u914d\u7f6e\u3001\u521b\u5efa\u7f51\u683c\u3001\u521b\u5efa\u4ea4\u6613\u4fe1\u53f7\u3001\u51c6\u5907\u56de\u6eaf\u6d4b\u8bd5\u3001\u8fdb\u884c\u56de\u6eaf\u6d4b\u8bd5\u4ee5\u53ca\u7ed3\u679c\u5206\u6790\u8fd9 6 \u4e2a\u5fc5\u8981\u7684\u987a\u5e8f\u73af\u8282\uff0c\u6307\u5bfc\u5927\u5bb6<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">\u8fd0\u7528 Python \u6784\u5efa\u5e76\u56de\u6eaf\u6d4b\u8bd5\u4e00\u4e2a\u7b80\u5355\u7684\u7f51\u683c\u4ea4\u6613\u7b56\u7565\uff0c\u800c\u4e14\u8be5\u7b56\u7565\u80fd\u591f\u5448\u73b0\u51fa\u8d44\u4ea7\u51c0\u503c\u7684\u7a33\u5b9a\u589e\u957f<\/mark>\uff08<em>\u590f\u666e\u6bd4\u7387\u63a5\u8fd1 5.7<\/em>\uff09\u3002\u800c\u5238\u5546\u548c\u673a\u6784\u4e0d\u4f1a\u544a\u8bc9\u4f60\u7684\u662f\uff0c\u4ed6\u4eec\u5c31\u662f<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">\u62ff\u7f51\u683c\u4ea4\u6613\u7b56\u7565\u53bb\u505a\u6301\u4ed3\u6df1\u5957\u7684\u5e95\u5c42\u89e3\u51b3\u903b\u8f91<\/mark>\u3002<\/pre>\n<\/blockquote>\n\n\n\n<p>\u7f51\u683c\u4ea4\u6613\u7b56\u7565 \u2014\u2014 \u4e00\u79cd\u65e0\u9700\u8fd0\u7528\u6280\u672f\u6307\u6807\u6765\u68c0\u6d4b\u8d8b\u52bf\u7684\u7b80\u4fbf\u4e14\u9ad8\u6548\u7684\u6280\u672f\u3002\u8be5\u65b9\u6cd5\u662f\u7b97\u6cd5\u4ea4\u6613\u7684\u4e0a\u4f73\u4e4b\u9009\uff0c\u6211\u4eec\u7684 Python \u56de\u6eaf\u6d4b\u8bd5\u8868\u660e\uff0c\u5f53\u4e24\u4e2a\u6708\u7684\u6570\u636e\u663e\u793a\u5728 5 \u5206\u949f\u56fe\u8868\u4e0a\u65f6\uff0c\u8d44\u4ea7\u51c0\u503c\u7a33\u6b65\u589e\u957f\uff0c\u590f\u666e\u6bd4\u7387\uff08Sharpe Ratio\uff09\u63a5\u8fd1 5.7\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/www.laoyulaoyu.com\/wp-content\/uploads\/2024\/09\/image-223.png\" alt=\"\" class=\"wp-image-2226\"\/><\/figure>\n\n\n\n<p><strong>\u590f\u666e\u6bd4\u7387\uff08Sharpe Ratio\uff09\uff1a<\/strong>\u8fd9\u662f\u4e00\u4e2a\u5728\u91d1\u878d\u548c\u6295\u8d44\u9886\u57df\u5e7f\u6cdb\u4f7f\u7528\u7684\u6307\u6807\uff0c\u7528\u4e8e\u8861\u91cf\u6295\u8d44\u7ec4\u5408\u7684\u98ce\u9669\u8c03\u6574\u540e\u8868\u73b0\u3002\u8fd9\u4e00\u6307\u6807\u7531\u8bfa\u8d1d\u5c14\u7ecf\u6d4e\u5b66\u5956\u83b7\u5f97\u8005\u5a01\u5ec9\u00b7\u590f\u666e\uff08William Sharpe\uff09\u63d0\u51fa\uff0c\u5176\u6838\u5fc3\u601d\u60f3\u5728\u4e8e\u5e2e\u52a9\u6295\u8d44\u8005\u5728\u627f\u62c5\u76f8\u540c\u98ce\u9669\u6c34\u5e73\u7684\u60c5\u51b5\u4e0b\uff0c\u6bd4\u8f83\u5e76\u9009\u62e9\u5177\u6709\u66f4\u9ad8\u56de\u62a5\u6f5c\u529b\u7684\u6295\u8d44\u54c1\u79cd\u3002\u590f\u666e\u6bd4\u7387\u662f\u6295\u8d44\u7ec4\u5408\u7684\u8d85\u989d\u6536\u76ca\uff08\u5373\u6295\u8d44\u7ec4\u5408\u7684\u5e73\u5747\u6536\u76ca\u7387\u51cf\u53bb\u65e0\u98ce\u9669\u5229\u7387\uff09\u4e0e\u6295\u8d44\u7ec4\u5408\u6ce2\u52a8\u7387\uff08\u5373\u6807\u51c6\u5dee\uff09\u7684\u6bd4\u503c\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step 0:&nbsp;\u7f51\u683c\u4ea4\u6613\u5165\u95e8<\/strong><\/h3>\n\n\n\n<p>\u7f51\u683c\u4ea4\u6613\u662f\u6307\u5728\u4ef7\u683c\u56fe\u8868\u4e0a\u8bbe\u7f6e\u6570\u503c\u7f51\u683c\u3002\u6bcf\u5f53\u4ef7\u683c\u8d8a\u8fc7\u4e00\u6761\u7f51\u683c\u7ebf\uff0c\u6211\u4eec\u5c31\u540c\u65f6\u5efa\u7acb\u4e00\u4e2a\u591a\u4ed3\u548c\u4e00\u4e2a\u7a7a\u4ed3\u3002\u6bcf\u6b21\u5f00\u4ed3\u7684\u6b62\u76c8\u90fd\u653e\u5728\u968f\u540e\u7684\u7f51\u683c\u7ebf\u4e0a\u3002<strong>\u8fd9\u79cd\u7b56\u7565\u64c5\u957f\u4ef7\u683c\u6ce2\u52a8\u9891\u7e41\u7684\u5e02\u573a<\/strong>\uff0c<strong>\u9002\u5408\u7528\u5728\u8f83\u77ed\u7684\u65f6\u95f4\u6846\u67b6<\/strong>\uff0c\u5982\u679c\u7f51\u683c\u4ea4\u6613\u6cd5\u4e0d\u8003\u8651\u65f6\u95f4\u6210\u672c\u53ef\u80fd\u5bfc\u81f4\u8d44\u91d1\u88ab\u957f\u65f6\u95f4\u5360\u7528\u65e0\u6cd5\u83b7\u5f97\u66f4\u9ad8\u56de\u62a5\u3002<br><br><strong>1. \u793a\u4f8b<\/strong><\/p>\n\n\n\n<p>\u5047\u8bbe\u4f60\u9009\u62e9\u4e86\u4e00\u53ea\u80a1\u7968\uff0c\u521d\u59cb\u4ef7\u683c\u4e3a100\u5143\u3002\u4f60\u8bbe\u7f6e\u4e86\u4e00\u4e2a10%\u7684\u7f51\u683c\u95f4\u8ddd\uff0c\u5171\u67096\u4e2a\u683c\u5b50\uff0c\u6bcf\u683c10%\u3002\u4ea4\u6613\u89c4\u5219\u662f: \u5f53\u80a1\u4ef7\u4e0b\u8dcc\u523090\u5143\u65f6\uff0c\u4e70\u51651000\u80a1\u5f53\u80a1\u4ef7\u4e0a\u6da8\u5230110\u5143\u65f6\uff0c\u5356\u51fa1000\u80a1\u5f53\u80a1\u4ef7\u4e0b\u8dcc\u523090\u5143\u65f6\uff0c\u518d\u6b21\u4e70\u51651000\u80a1\uff0c\u4ee5\u6b64\u7c7b\u63a8\u3002<strong>\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u5728\u4ef7\u683c\u6ce2\u52a8\u65f6\u5b9e\u73b0\u4e70\u4f4e\u5356\u9ad8\uff0c\u4f46\u5728\u5355\u8fb9\u4e0a\u5347\u6216\u4e0b\u8dcc\u8d8b\u52bf\u4e2d\u53ef\u80fd\u8868\u73b0\u4e0d\u4f73\u3002<\/strong><br><br><strong>2. \u4f18\u70b9<\/strong><\/p>\n\n\n\n<p>\u5728\u9707\u8361\u884c\u60c5\u4e2d\uff0c\u5982\u679c<strong>\u7f51\u683c\u8bbe\u7f6e\u5408\u7406\uff0c\u53ef\u4ee5\u6765\u56de\u4e0d\u65ad\u5730\u8d5a\u53d6\u5229\u6da6<\/strong>\u3002\u5728\u9707\u8361\u884c\u60c5\u4e2d\u8868\u73b0\u5f97\u597d\uff0c\u662f\u7f51\u683c\u6700\u91cd\u8981\u7684\u4f5c\u7528\u3002\u8fd9\u79cd\u7b56\u7565\u7279\u522b\u9002\u5408\u90a3\u4e9b\u6ca1\u6709\u660e\u663e\u8d8b\u52bf\u7684\u5e02\u573a\uff08\u5927A\uff0c\u4f60\u5c31\u76f4\u63a5\u62a5\u6211\u540d\u5b57\u4e0d\u597d\u5417\uff1f\uff09\uff0c\u56e0\u4e3a\u5b83\u80fd\u591f\u5728\u4ef7\u683c\u4e0a\u6da8\u548c\u4e0b\u8dcc\u65f6\u90fd\u5b9e\u73b0\u6536\u76ca\u3002<\/p>\n\n\n\n<p>\u6301\u6709\u4f53\u9a8c\u4f1a\u5f88\u4e0d\u9519\uff0c\u56e0\u4e3a\u5b83\u6bcf\u4e00\u7b14\u4ea4\u6613\u90fd\u65e8\u5728\u201c\u9ad8\u629b\u4f4e\u5438\u201d\uff0c\u6bcf\u4e00\u6b21\u4e70\u5165\u90fd\u65e8\u5728\u76c8\u5229\u9000\u51fa\u3002<strong>\u7406\u8bba\u4e0a\u6765\u8bf4\uff0c\u7f51\u683c\u4ea4\u6613\u7b56\u7565\u7684\u4ea4\u6613\u80dc\u7387\u662f100%\uff0c<\/strong>\u8fd9\u5f88\u5bb9\u6613\u8ba9\u6295\u8d44\u8005\u5728\u6295\u8d44\u7684\u8fc7\u7a0b\u4e2d\u5fc3\u7406\u6109\u60a6\uff0c\u83b7\u5f97\u6b63\u5411\u53cd\u9988\u3002<\/p>\n\n\n\n<p><br><strong>3. \u7f3a\u70b9<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u5ffd\u89c6\u65f6\u95f4\u6210\u672c<\/strong>\uff1a\u7f51\u683c\u4ea4\u6613\u6cd5\u4e0d\u8003\u8651\u65f6\u95f4\u6210\u672c\u53ef\u80fd\u5bfc\u81f4\u8d44\u91d1\u88ab\u957f\u65f6\u95f4\u5360\u7528\u65e0\u6cd5\u83b7\u5f97\u66f4\u9ad8\u56de\u62a5\u3002<\/li>\n\n\n\n<li><strong>\u5355\u6b21\u83b7\u5229\u6709\u9650<\/strong>\uff1a\u8be5\u7b56\u7565\u7684\u76c8\u5229\u6f5c\u529b\u53d7\u5230\u7f51\u683c\u95f4\u8ddd\u7684\u9650\u5236\uff0c\u53ea\u80fd\u83b7\u5f97\u8bbe\u5b9a\u7684\u4e70\u5165\u5356\u51fa\u5e45\u5ea6\u5185\u7684\u5229\u6da6\u3002<\/li>\n\n\n\n<li><strong>\u4e0d\u9002\u7528\u4e8e\u9ad8\u589e\u957f\u80a1\u7968<\/strong>\uff1a\u7f51\u683c\u4ea4\u6613\u6700\u9002\u5408\u4e8e\u4ef7\u683c\u6ce2\u52a8\u8f83\u5927\u4f46\u6ca1\u6709\u660e\u663e\u589e\u957f\u8d8b\u52bf\u7684\u8d44\u4ea7\uff0c \u4e0d\u9002\u5408\u90a3\u4e9b\u6bcf\u5e74\u5229\u6da6\u7a33\u5b9a\u589e\u957f\u7684\u516c\u53f8\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u603b\u4e4b\uff0c\u7f51\u683c\u4ea4\u6613\u6cd5\u662f\u4e00\u79cd\u9002\u7528\u4e8e\u9707\u8361\u5e02\u573a\u7684\u4ea4\u6613\u7b56\u7565\uff0c\u5b83\u5ffd\u7565\u4e86\u65f6\u95f4\u6210\u672c\uff0c\u76c8\u5229\u6709\u9650\uff0c\u9002\u5408\u7528\u4e8e\u6ce2\u52a8\u9891\u7e41\u7684\u6807\u7684\u8d44\u4ea7\u3002\u800c\u5927\u5bb6\u5173\u5fc3\u7684\u7f51\u683c\u4ea4\u6613\u7b56\u7565\u5982\u4f55\u80fd\u4f5c\u4e3a\u4e00\u79cd\u89e3\u51b3\u6301\u4ed3\u6df1\u5957\u95ee\u9898\u7684\u65b9\u6cd5\uff0c\u5176\u6838\u5fc3\u903b\u8f91\u5c31\u662f\u5728\u4e8e<strong>\u901a\u8fc7\u673a\u68b0\u5316\u7684\u81ea\u52a8\u6267\u884c\u9ad8\u629b\u4f4e\u5438\u64cd\u4f5c\uff0c\u5728\u5e02\u573a\u4ef7\u683c\u6ce2\u52a8\u4e2d\u5bfb\u6c42\u76c8\u5229\u673a\u4f1a\uff0c\u4ece\u800c\u9010\u6b65\u964d\u4f4e\u6301\u4ed3\u6210\u672c\u5e76\u6700\u7ec8\u5b9e\u73b0\u89e3\u5957<\/strong>\u3002\u4f46\u6b8b\u9177\u7684\u73b0\u5b9e\u662f\uff0c\u5982\u679c\u4f60\u4e0d\u5177\u5907\u5238\u5546\u6216\u673a\u6784\u90a3\u6837\u7684\u8f6f\u786c\u4ef6\u73af\u5883\uff0c\u4e5f\u6ca1\u6709\u53cd\u6b63\u64cd\u4f5c\u7684\u90fd\u662f\u522b\u4eba\u94b1\u7684\u5fc3\u6001\u65f6\uff0c\u5f53\u4ed6\u4eec\u6765\u7535\u8bdd\u8be2\u95ee\u4f60\u662f\u5426\u9700\u8981\u6301\u4ed3\u6df1\u5957\u89e3\u5957\u670d\u52a1\u65f6\uff0c\u4f60\u8fd8\u662f\u8001\u8001\u5b9e\u5b9e\u7684\u56de\u7b54<strong> yes<\/strong> \u5427!<\/p>\n\n\n\n<p>\u524d\u9762\u5570\u5570\u55e6\u55e6\u8fd9\u4e48\u591a\uff0c\u5c31\u662f\u544a\u8bc9\u6211\u7684\u8bfb\u8005\uff0c\u5728\u4f7f\u7528\u8be5\u7b56\u7565\u65f6\u9700\u8981\u8c28\u614e\uff0c<strong>\u5fc5\u987b\u5148\u4e86\u89e3\u5176\u5c40\u9650\u6027\u548c\u9002\u7528\u6027\u3002<\/strong>\u73b0\u5728\uff0c\u6211\u4eec\u5c06\u7528 Python \u5b9e\u73b0\u7f51\u683c\u7b56\u7565\uff0c\u5e76\u5728\u5386\u53f2\u6570\u636e\u4e0a\u8fdb\u884c\u56de\u6eaf\u6d4b\u8bd5\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step 1:&nbsp;\u914d\u7f6e\u73af\u5883<\/strong><\/h3>\n\n\n\n<p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u914d\u7f6e Python \u73af\u5883\uff0c\u5bfc\u5165\u6240\u9700\u7684\u5e93\u5e76\u4e0b\u8f7d\u6570\u636e\u96c6\u3002\u6211\u4eec\u5c06\u4f7f\u7528 yfinance \u83b7\u53d6\u6570\u636e\uff0c\u4f7f\u7528 pandas \u8fdb\u884c\u6570\u636e\u64cd\u4f5c\uff0c\u4f7f\u7528 numpy \u8fdb\u884c\u6570\u503c\u8fd0\u7b97\uff0c\u4f7f\u7528 pandas_ta \u8fdb\u884c\u6280\u672f\u5206\u6790\u6307\u6807\u3002\uff08\u7279\u522b\u662f numpy\u548cpandas_ta \uff0c\u56fd\u5185\u5927\u90e8\u5206\u6559\u7a0b\u90fd\u4e0d\u4f1a\u6559\u8fd9\u4e2a\uff09<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>import yfinance as yf\nimport pandas as pd\nimport numpy as np\nimport pandas_ta as ta\n\n# Download the EUR\/USD data for the last 59 days with a 5-minute interval\ndataF = yf.download(\"EURUSD=X\", start=pd.Timestamp.today() - pd.DateOffset(days=59), end=pd.Timestamp.today(), interval='5m')\n\n# yfinance (yf): A Python library to download historical market data from Yahoo Finance.\n# pandas (pd): A powerful data manipulation and analysis library.\n# numpy (np): A library for numerical operations.\n# pandas_ta (ta): A technical analysis library for financial datasets.<\/code><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step 2:&nbsp;\u521b\u5efa\u7f51\u683c<\/strong><\/h3>\n\n\n\n<p>\u5728\u8fd9\u4e00\u6b65\u4e2d\uff0c\u6211\u4eec\u8981\u5efa\u7acb\u4e00\u4e2a\u6570\u503c\u7f51\u683c\uff0c\u51b3\u5b9a\u6211\u4eec\u7684\u8fdb\u573a\u548c\u51fa\u573a\u70b9\u3002\u7f51\u683c\u7531\u4ef7\u683c\u56fe\u8868\u4e0a\u5747\u5300\u5206\u5e03\u7684\u4e00\u7cfb\u5217\u6c34\u5e73\u7ebf\u7ec4\u6210\u3002\u6bcf\u5f53\u4ef7\u683c\u7a7f\u8fc7\u5176\u4e2d\u4e00\u6761\u7f51\u683c\u7ebf\uff0c\u6211\u4eec\u5c31\u4f1a\u5efa\u7acb\u4e00\u4e2a\u65b0\u4ed3\u4f4d\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>grid_distance = 0.005\nmidprice = 1.065\n\ndef generate_grid(midprice, grid_distance, grid_range):\n    return np.arange(midprice - grid_range, midprice + grid_range, grid_distance)\ngrid = generate_grid(midprice=midprice, grid_distance=grid_distance, grid_range=0.1)<\/code><\/code><\/pre>\n\n\n\n<p><strong>2.1 \u8bbe\u7f6e\u53c2\u6570<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>grid_distance = 0.005\uff1a<\/strong>\u8bbe\u7f6e\u6bcf\u6761\u7f51\u683c\u7ebf\u4e4b\u95f4\u7684\u95f4\u8ddd\uff0c\u4ee3\u8868\u5f00\u65b0\u4ed3\u7684\u4ef7\u683c\u589e\u91cf\u3002<\/li>\n\n\n\n<li><strong>midprice = 1.065:&nbsp;<\/strong>\u8fd9\u662f\u5efa\u7acb\u7f51\u683c\u7684\u4e2d\u5fc3\u53c2\u8003\u4ef7\u3002\u53ef\u4ee5\u8bbe\u7f6e\u4e3a\u5f53\u524d\u5e02\u573a\u4ef7\u683c\u6216\u5176\u4ed6\u53c2\u8003\u4ef7\u683c\u3002<\/li>\n<\/ol>\n\n\n\n<p><strong>2.2 \u91cd\u8981\u63d0\u793a<\/strong><\/p>\n\n\n\n<p>\u8fd9\u4e9b\u503c\u5fc5\u987b\u6839\u636e\u5e02\u573a\u3001\u4ea4\u6613\u8d44\u4ea7\u548c\u5f53\u524d\u5206\u6790\u8fdb\u884c\u8c03\u6574\u3002\u56e0\u6b64\uff0c\u6bcf\u6b21\u4ece Yfinance \u67e5\u8be2\u6570\u636e\u65f6\uff0c\u6570\u636e\u90fd\u4f1a\u66f4\u65b0\uff0c\u8fd9\u4e9b\u53c2\u6570\u4ee5\u53ca\u672c\u7b56\u7565\u4e2d\u7684\u5176\u4ed6\u53c2\u6570\u4e5f\u9700\u8981\u76f8\u5e94\u66f4\u65b0\u3002<\/p>\n\n\n\n<p><strong>2.3 \u751f\u6210\u7f51\u683c<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u51fd\u6570 generate_grid \u4f1a\u521b\u5efa\u4e00\u7cfb\u5217\u6570\u503c\uff0c\u8303\u56f4\u4ece\u4e2d\u95f4\u4ef7 &#8211; grid_range \u5230\u4e2d\u95f4\u4ef7 + grid_range\uff0c\u4e2d\u95f4\u95f4\u9694\u4e3a grid_distance\u3002<\/li>\n\n\n\n<li><strong>grid_range = 0.1:<\/strong>&nbsp;\u5b9a\u4e49\u4e2d\u95f4\u4ef7\u4e0a\u4e0b\u7684\u7f51\u683c\u603b\u8303\u56f4\u3002<\/li>\n<\/ol>\n\n\n\n<p><strong>2.4 \u6784\u5efa\u7f51\u683c<\/strong><\/p>\n\n\n\n<p>\u7f51\u683c\u9635\u5217\u5305\u62ec\u6211\u4eec\u5c06\u8003\u8651\u5efa\u4ed3\u7684\u6240\u6709\u4ef7\u4f4d\u3002\u8fd9\u662f\u6211\u4eec\u7f51\u683c\u4ea4\u6613\u7b56\u7565\u7684\u6838\u5fc3\uff0c\u4f7f\u6211\u4eec\u80fd\u591f\u968f\u7740\u4ef7\u683c\u6ce2\u52a8\u7cfb\u7edf\u5730\u8fdb\u5165\u548c\u9000\u51fa\u4ea4\u6613\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step 3:&nbsp;\u521b\u5efa\u4ea4\u6613\u4fe1\u53f7<\/strong><\/h3>\n\n\n\n<p>\u5728\u8fd9\u4e00\u6b65\u4e2d\uff0c\u6211\u4eec\u6839\u636e\u5efa\u7acb\u7684\u7f51\u683c\u7ebf\u751f\u6210\u4ea4\u6613\u4fe1\u53f7\u3002\u53ea\u8981\u4ef7\u683c\u4e0e\u7f51\u683c\u7ebf\u4ea4\u53c9\uff0c\u5c31\u4f1a\u4ea7\u751f\u4ea4\u6613\u4fe1\u53f7\u3002\u8fd9\u9700\u8981\u904d\u5386\u6570\u636e\u96c6\u4e2d\u7684\u6bcf\u4e00\u884c\uff0c\u68c0\u67e5\u8be5\u533a\u95f4\u7684\u6700\u9ad8\u4ef7\u6216\u6700\u4f4e\u4ef7\u662f\u5426\u4e0e\u4efb\u4f55\u7f51\u683c\u7ebf\u4ea4\u53c9\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>signal = &#91;0] * len(dataF)\ni = 0\nfor index, row in dataF.iterrows():\n    for p in grid:\n        if min(row.Low, row.High) &lt; p and max(row.Low, row.High) &gt; p:\n            signal&#91;i] = 1\n    i += 1\ndataF&#91;\"signal\"] = signal\ndataF&#91;dataF&#91;\"signal\"] == 1]<\/code><\/code><\/pre>\n\n\n\n<p><strong>3.1 \u521d\u59cb\u5316\u4fe1\u53f7<\/strong><\/p>\n\n\n\n<p><strong>signal = [0] * len(dataF)\uff1a<\/strong>\u6211\u4eec\u521b\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e0e\u6570\u636e\u96c6\u76f8\u540c\u7684\u96f6\u5217\u8868\u3002\u8be5\u5217\u8868\u5c06\u5b58\u50a8\u6211\u4eec\u7684\u4ea4\u6613\u4fe1\u53f7\u3002<\/p>\n\n\n\n<p><strong>3.2 \u6570\u636e\u8fed\u4ee3<\/strong><\/p>\n\n\n\n<p>\u6211\u4eec\u4f7f\u7528 dataF.iterrows() \u5faa\u73af\u67e5\u770b\u6570\u636e\u96c6\u4e2d\u7684\u6bcf\u4e00\u884c\u3002<\/p>\n\n\n\n<p>\u5bf9\u4e8e\u6bcf\u4e00\u884c\uff0c\u6211\u4eec\u901a\u8fc7\u6bd4\u8f83\u8be5\u884c\u6700\u9ad8\u4ef7\u548c\u6700\u4f4e\u4ef7\u7684\u6700\u5c0f\u503c\u548c\u6700\u5927\u503c\u4e0e\u6bcf\u6761\u7f51\u683c\u7ebf\uff0c\u6765\u68c0\u67e5\u4ef7\u683c\u662f\u5426\u4e0e\u4efb\u4f55\u7f51\u683c\u7ebf\u4ea4\u53c9\u3002<\/p>\n\n\n\n<p><strong>3.3 \u751f\u6210\u4fe1\u53f7<\/strong><\/p>\n\n\n\n<p>\u5982\u679c\u7f51\u683c\u7ebf\u88ab\u4ea4\u53c9\uff08\u5373\u6700\u4f4e\u4ef7\u4f4e\u4e8e\u7f51\u683c\u7ebf\uff0c\u6700\u9ad8\u4ef7\u9ad8\u4e8e\u7f51\u683c\u7ebf\uff09\uff0c\u6211\u4eec\u5c31\u4f1a\u5c06\u4fe1\u53f7\u5217\u8868\u4e2d\u7684\u76f8\u5e94\u6761\u76ee\u8bbe\u4e3a 1\u3002<\/p>\n\n\n\n<p><strong>3.4 \u4e3a DataFrame \u6dfb\u52a0\u4fe1\u53f7<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>dataF[&#8220;signal&#8221;] = signal\uff1a<\/strong>\u6211\u4eec\u5c06\u4fe1\u53f7\u5217\u8868\u4f5c\u4e3a\u65b0\u5217\u6dfb\u52a0\u5230 DataFrame \u4e2d\u3002<\/li>\n\n\n\n<li><strong>dataF[dataF[&#8220;\u4fe1\u53f7&#8221;] == 1]\uff1a<\/strong>\u8fd9\u5c06\u8fc7\u6ee4 DataFrame\uff0c\u53ea\u663e\u793a\u4ea7\u751f\u4ea4\u6613\u4fe1\u53f7\u7684\u884c\u3002<\/li>\n<\/ol>\n\n\n\n<p>\u901a\u8fc7\u521b\u5efa\u8fd9\u4e9b\u4ea4\u6613\u4fe1\u53f7\uff0c\u6211\u4eec\u53ef\u4ee5\u786e\u5b9a\u4ef7\u683c\u4e0e\u7f51\u683c\u7ebf\u4ea4\u53c9\u7684\u786e\u5207\u65f6\u95f4\u70b9\uff0c\u4ece\u800c\u6307\u793a\u6f5c\u5728\u7684\u4ea4\u6613\u5165\u53e3\u3002\u8fd9\u79cd\u7cfb\u7edf\u5316\u7684\u65b9\u6cd5\u53ef\u786e\u4fdd\u6211\u4eec\u6355\u6349\u5230\u6240\u5b9a\u4e49\u7f51\u683c\u5185\u7684\u6240\u6709\u76f8\u5173\u4ef7\u683c\u8d70\u52bf\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step 4:&nbsp;\u51c6\u5907\u56de\u6eaf\u6d4b\u8bd5<\/strong><\/h3>\n\n\n\n<p>\u5728\u8fd9\u4e00\u6b65\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7\u8ba1\u7b97\u5e73\u5747\u771f\u5b9e\u8303\u56f4 (ATR) \u548c\u5b9a\u4e49\u4fe1\u53f7\u51fd\u6570\uff0c\u4e3a\u56de\u6d4b\u6570\u636e\u96c6\u505a\u597d\u51c6\u5907\u3002ATR \u662f\u4e00\u4e2a\u6ce2\u52a8\u7387\u6307\u6807\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u8bbe\u7f6e\u52a8\u6001\u6b62\u635f\u548c\u6b62\u76c8\u6c34\u5e73\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>dfpl = dataF&#91;:].copy()\n\ndef SIGNAL():\n    return dfpl.signal\ndfpl&#91;'ATR'] = ta.atr(high=dfpl.High, low=dfpl.Low, close=dfpl.Close, length=16)\ndfpl.dropna(inplace=True)<\/code><\/code><\/pre>\n\n\n\n<p><strong>4.1 \u590d\u5236 DF<\/strong><\/p>\n\n\n\n<p><strong>dfpl = dataF[:].copy()\uff1a<\/strong>\u6211\u4eec\u521b\u5efa\u539f\u59cb\u6570\u636e\u96c6\u7684\u526f\u672c\uff0c\u4ee5\u907f\u514d\u5728\u56de\u6eaf\u6d4b\u8bd5\u8fc7\u7a0b\u4e2d\u76f4\u63a5\u66f4\u6539\u6570\u636e\u96c6\u3002\u8fd9\u5c06\u786e\u4fdd\u6211\u4eec\u7684\u539f\u59cb\u6570\u636e\u4fdd\u6301\u4e0d\u53d8\u3002<\/p>\n\n\n\n<p><strong>4.2 \u5b9a\u4e49\u4fe1\u53f7\u529f\u80fd<\/strong><\/p>\n\n\n\n<p><strong>def SIGNAL()\uff1a<\/strong>\u6b64\u51fd\u6570\u8fd4\u56de DataFrame \u4e2d\u7684\u4fe1\u53f7\u5217\u3002\u5b83\u5c06\u5728\u56de\u6eaf\u6d4b\u8bd5\u4e2d\u7528\u4e8e\u8bbf\u95ee\u751f\u6210\u7684\u4ea4\u6613\u4fe1\u53f7\u3002<\/p>\n\n\n\n<p><strong>4.3 \u8ba1\u7b97\u5e73\u5747\u771f\u5b9e\u8303\u56f4 (ATR)<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>dfpl[&#8216;ATR&#8217;] = ta.atr(high=dfpl.High, low=dfpl.Low, close=dfpl.Close, length=16)\uff1a<\/strong>\u6211\u4eec\u4f7f\u7528 16 \u4e2a\u533a\u95f4\u8ba1\u7b97 ATR\u3002ATR \u662f\u8861\u91cf\u5e02\u573a\u6ce2\u52a8\u6027\u7684\u6307\u6807\uff0c\u6709\u52a9\u4e8e\u8bbe\u7f6e\u5408\u9002\u7684\u6b62\u635f\u548c\u6b62\u76c8\u6c34\u5e73\u3002<\/li>\n\n\n\n<li><strong>high=dfpl.High\uff0clow=dfpl.Low\uff0cclose=dfpl.Close\uff1a<\/strong>\u8fd9\u4e9b\u53c2\u6570\u6307\u5b9a\u7528\u4e8e\u8ba1\u7b97 ATR \u7684\u6700\u9ad8\u4ef7\u3001\u6700\u4f4e\u4ef7\u548c\u6536\u76d8\u4ef7\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>4.4 \u6e05\u6d17<\/strong><\/p>\n\n\n\n<p><strong>dfpl.dropna(inplace=True)\uff1a<\/strong>\u6211\u4eec\u5254\u9664\u6240\u6709\u7f3a\u5931\u503c\u7684\u884c\uff0c\u4ee5\u4fdd\u6301\u56de\u6eaf\u6d4b\u8bd5\u8fc7\u7a0b\u7684\u5b8c\u6574\u6027\u3002\u8fd9\u4e00\u6b65\u5bf9\u4e8e\u83b7\u5f97\u51c6\u786e\u53ef\u9760\u7684\u56de\u6eaf\u6d4b\u8bd5\u7ed3\u679c\u81f3\u5173\u91cd\u8981\u3002<\/p>\n\n\n\n<p>ATR \u662f\u5bf9\u6ce2\u52a8\u7387\u7684\u52a8\u6001\u8861\u91cf\u3002\u901a\u8fc7\u8ba1\u7b97 ATR \u548c\u5b9a\u4e49\u4fe1\u53f7\u51fd\u6570\uff0c\u6211\u4eec\u7684\u56de\u6eaf\u6d4b\u8bd5\u6846\u67b6\u5c31\u5177\u5907\u4e86\u8bc4\u4f30\u7f51\u683c\u4ea4\u6613\u7b56\u7565\u6027\u80fd\u7684\u5fc5\u8981\u5de5\u5177\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step 5:&nbsp;\u56de\u6eaf\u6d4b\u8bd5<\/strong><\/h3>\n\n\n\n<p>\u5728\u8fd9\u4e00\u6b65\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u56de\u6eaf\u6d4b\u8bd5\u5e93\u5b9e\u65bd\u6211\u4eec\u7684\u4ea4\u6613\u7b56\u7565\uff0c\u5e76\u6267\u884c\u56de\u6eaf\u6d4b\u8bd5\u4ee5\u8bc4\u4f30\u5176\u6027\u80fd\u3002\u6211\u4eec\u5b9a\u4e49\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7b56\u7565\u7c7b\u5e76\u914d\u7f6e\u56de\u6eaf\u6d4b\u8bd5\u53c2\u6570\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>from backtesting import Strategy\nfrom backtesting import Backtest\nimport backtesting\n\nclass MyStrat(Strategy):\n    mysize = 50\n    \n    def init(self):\n        super().init()\n        self.signal1 = self.I(SIGNAL)\n    def next(self):\n        super().next()\n        slatr = 1.5 * grid_distance  # Stop loss distance\n        TPSLRatio = 0.5  # Take profit to stop loss ratio\n        if self.signal1 == 1 and len(self.trades) &lt;= 10000:\n            # Sell position\n            sl1 = self.data.Close&#91;-1] + slatr\n            tp1 = self.data.Close&#91;-1] - slatr * TPSLRatio\n            self.sell(sl=sl1, tp=tp1, size=self.mysize)\n            # Buy position\n            sl1 = self.data.Close&#91;-1] - slatr\n            tp1 = self.data.Close&#91;-1] + slatr * TPSLRatio\n            self.buy(sl=sl1, tp=tp1, size=self.mysize)\n# Running the backtest\nbt = Backtest(dfpl, MyStrat, cash=50, margin=1\/100, hedging=True, exclusive_orders=False)\nstat = bt.run()\nstat<\/code><\/code><\/pre>\n\n\n\n<p><strong>5.1 \u5bfc\u5165\u5e93<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>from backtesting import Strategy, Backtest\uff1a<\/strong>\u4ece\u56de\u6eaf\u6d4b\u8bd5\u5e93\u4e2d\u5bfc\u5165\u57fa\u672c\u7c7b\uff0c\u4ee5\u521b\u5efa\u5e76\u6267\u884c\u6211\u4eec\u7684\u4ea4\u6613\u7b56\u7565\u3002<\/li>\n\n\n\n<li><strong>import backtesting:<\/strong>&nbsp;\u5bfc\u5165\u53cd\u5411\u6d4b\u8bd5\u5e93<\/li>\n<\/ol>\n\n\n\n<p><strong>5.2 \u521b\u5efa\u7b56\u7565\u7c7b<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>class MyStrat(Strategy):<\/strong>&nbsp;\u6211\u4eec\u901a\u8fc7\u6269\u5c55\u7b56\u7565\u7c7b\u6765\u5b9a\u4e49\u81ea\u5b9a\u4e49\u7b56\u7565\u7c7b\u3002<\/li>\n\n\n\n<li><strong>mysize = 50:<\/strong>&nbsp;\u8bbe\u7f6e\u6bcf\u6b21\u4ea4\u6613\u7684\u4ed3\u4f4d\u5927\u5c0f\u3002<\/li>\n<\/ol>\n\n\n\n<p><strong>5.3 \u521d\u59cb\u5316<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>def init(self):<\/strong>\u521d\u59cb\u5316\u7b56\u7565\u3002<\/li>\n\n\n\n<li><strong>self.signal1 = self.I(SIGNAL):<\/strong>&nbsp; \u4f7f\u7528\u4e4b\u524d\u5b9a\u4e49\u7684 SIGNAL \u51fd\u6570\u8bbf\u95ee\u4ea4\u6613\u4fe1\u53f7\u3002<\/li>\n<\/ol>\n\n\n\n<p><strong>5.4 \u786e\u5b9a\u4ea4\u6613\u903b\u8f91<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>def next(self):<\/strong>&nbsp;\u6982\u8ff0\u6267\u884c\u4ea4\u6613\u7684\u89c4\u5219\u3002<\/li>\n\n\n\n<li><strong>slatr = 1.5 * grid_distance:<\/strong>&nbsp;\u6839\u636e\u7f51\u683c\u95f4\u8ddd\u786e\u5b9a\u6b62\u635f\u8ddd\u79bb\u3002<\/li>\n\n\n\n<li><strong>TPSLRatio = 0.5:<\/strong>&nbsp; \u5efa\u7acb\u6b62\u76c8\u4e0e\u6b62\u635f\u6bd4\u7387\u3002<\/li>\n<\/ol>\n\n\n\n<p><strong>5.5 \u6267\u884c\u4ea4\u6613<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u5982\u679c\u89e6\u53d1\u4e86\u4fe1\u53f7\uff08self.signal1 == 1\uff09\uff0c\u4e14\u4ea4\u6613\u6b21\u6570\u5c0f\u4e8e\u6216\u7b49\u4e8e 10,000\uff1a<\/li>\n\n\n\n<li>\u5356\u51fa\u5934\u5bf8\uff1a &#8211; sl1 = self.data.Close[-1] + slatr\uff1a\u8bbe\u7f6e\u9ad8\u4e8e\u5f53\u524d\u6536\u76d8\u4ef7\u7684\u6b62\u635f\u3002 &#8211; tp1 = self.data.Close[-1] &#8211; slatr * TPSLRatio\uff1a\u8bbe\u7f6e\u4f4e\u4e8e\u5f53\u524d\u6536\u76d8\u4ef7\u7684\u6b62\u76c8\u3002 &#8211; self.sell(sl=sl1, tp=tp1, size=self.mysize)\uff1a\u4e70\u5165\u5934\u5bf8\uff1a &#8211; sl1 = self.data.Close[-1] &#8211; slatr\uff1a\u8bbe\u7f6e\u4f4e\u4e8e\u5f53\u524d\u6536\u76d8\u4ef7\u7684\u6b62\u635f\u3002 &#8211; tp1 = self.data.Close[-1] + slatr * TPSLRatio\uff1a\u8bbe\u7f6e\u9ad8\u4e8e\u5f53\u524d\u6536\u76d8\u4ef7\u7684\u6b62\u76c8\u3002 &#8211; self.buy(sl=sl1, tp=tp1, size=self.mysize)\uff1a\u4e0b\u8fbe\u4e70\u5165\u6307\u4ee4\u3002<\/li>\n<\/ol>\n\n\n\n<p><strong>5.6 \u8fd0\u884c\u56de\u6eaf\u6d4b\u8bd5<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>bt = Backtest(dfpl, MyStrat, cash=50, margin=1\/100, hedging=True, exclusive_orders=False):<\/strong>&nbsp;\u8bbe\u7f6e\u56de\u6eaf\u6d4b\u8bd5\u53c2\u6570\u3002<\/li>\n\n\n\n<li><strong>cash=50:<\/strong>&nbsp;\u521d\u59cb\u53ef\u7528\u73b0\u91d1\u989d\u3002<\/li>\n\n\n\n<li><strong>margin=1\/100:<\/strong>&nbsp;\u6760\u6746\u6bd4\u7387\u3002<\/li>\n\n\n\n<li><strong>hedging=True:<\/strong>&nbsp;\u5141\u8bb8\u5bf9\u51b2\u3002<\/li>\n\n\n\n<li><strong>exclusive_orders=False:<\/strong>&nbsp;\u5141\u8bb8\u91cd\u53e0\u8ba2\u5355\u3002<\/li>\n\n\n\n<li><strong>stat = bt.run():<\/strong>&nbsp;\u6267\u884c\u56de\u6eaf\u6d4b\u8bd5\u5e76\u4fdd\u5b58\u7ed3\u679c\u3002<\/li>\n<\/ol>\n\n\n\n<p id=\"8cb4\">\u901a\u8fc7\u8be5\u56de\u6eaf\u6d4b\u8bd5\uff0c\u6211\u4eec\u53ef\u4ee5\u8bc4\u4f30\u7f51\u683c\u4ea4\u6613\u7b56\u7565\u7684\u6027\u80fd\u3002<strong>`stat` <\/strong>\u5bf9\u8c61\u63d0\u4f9b\u4e86\u8be6\u7ec6\u7684\u6307\u6807\uff0c\u7528\u4e8e\u8bc4\u4f30\u7b56\u7565\u7684\u6709\u6548\u6027\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step 6:&nbsp;\u7ed3\u679c<\/strong><\/h3>\n\n\n\n<p>\u4e0b\u56fe\u5c55\u793a\u4e86\u56de\u6eaf\u6d4b\u8bd5\u7684\u7ed3\u679c\uff0c\u5305\u62ec\u7b56\u7565\u7684\u6301\u7eed\u65f6\u95f4\u3001\u4ea4\u6613\u65f6\u95f4\u6bd4\u4f8b\u3001\u7ee9\u6548\u6307\u6807\uff08\u6700\u7ec8\u6743\u76ca\u3001\u6536\u76ca\u5cf0\u503c\u3001\u6536\u76ca\u7387\u7b49\uff09\u3001\u98ce\u9669\u8c03\u6574\u6307\u6807\uff08\u590f\u666e\u6bd4\u7387\u3001\u7d22\u63d0\u8bfa\u6bd4\u7387\u3001\u5361\u5c14\u739b\u6bd4\u7387\u7b49\uff09\u3001\u8dcc\u7a7f\u7387\u3001\u4ea4\u6613\u6d3b\u52a8\u7b49\u3002\u540e\u9762\u8fd8\u6709\u7a0b\u5e8f\u8f93\u51fa\u7684\u7ed3\u679c\u6570\u636e\uff0c\u8bf7\u5927\u5bb6\u67e5\u9605\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/www.laoyulaoyu.com\/wp-content\/uploads\/2024\/09\/0_YaEZrywNwUrxik50.png\" alt=\"\" class=\"wp-image-2233\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code><code><strong>### Summary:<\/strong>\n- <strong>**Duration:**<\/strong> The strategy was evaluated over a period of 57 days and 21 hours, offering a thorough assessment timeframe.\n- <strong>**Exposure Time:**<\/strong> 99.14%, meaning the strategy was nearly always active in the market.\n<strong>### Performance Metrics:<\/strong>\n- <strong>**Final Equity:**<\/strong> $136.02, up from the initial $50.\n- <strong>**Equity Peak:**<\/strong> $156.59, indicating potential for greater gains.\n- <strong>**Return:**<\/strong> 172.04%, showing substantial profitability.\n- <strong>**Buy &amp; Hold Return:**<\/strong> 4.23%, highlighting the strategy's superior performance over a passive approach.\n- <strong>**Annualized Return:**<\/strong> 37364.62%, extremely high due to the brief backtesting period.\n- <strong>**Annualized Volatility:**<\/strong> 46309.60%, suggesting significant risk and frequent trading activity.\n<strong>### Risk-Adjusted Metrics:<\/strong>\n- <strong>**Sharpe Ratio:**<\/strong> 0.81, indicating moderate returns adjusted for risk.\n- <strong>**Sortino Ratio:**<\/strong> 1089.28, reflecting excellent management of downside risk.\n- <strong>**Calmar Ratio:**<\/strong> 2194.58, demonstrating high returns relative to the maximum drawdown.\n<strong>### Drawdown:<\/strong>\n- <strong>**Max Drawdown:**<\/strong> -17.03%, a notable but manageable drop.\n- <strong>**Avg Drawdown:**<\/strong> -1.16%, relatively modest.\n- <strong>**Max Drawdown Duration:**<\/strong> Almost 10 days, representing the longest recovery time.\n- <strong>**Avg Drawdown Duration:**<\/strong> Under 7 hours, suggesting quick recoveries.\n<strong>### Trading Activity:<\/strong>\n- <strong>**Number of Trades:**<\/strong> 1698, indicating high trading frequency due to the grid strategy.\n- <strong>**Win Rate:**<\/strong> 73.03%, a very high success rate.\n- <strong>**Best Trade:**<\/strong> 0.87%, reflecting consistent small gains.\n- <strong>**Worst Trade:**<\/strong> -0.85%, indicating controlled losses.\n- <strong>**Avg Trade:**<\/strong> 0.10%, showing the cumulative effect of many small trades.<\/code><\/code><\/pre>\n\n\n\n<p><strong>\u7f51\u683c\u4ea4\u6613\u7b56\u7565\u7684\u6709\u6548\u6027<\/strong>\uff1a\u6211\u4eec\u901a\u8fc7\u5b9e\u9645\u6570\u636e\u7684\u56de\u6eaf\u6d4b\u8bd5\uff0c\u5c55\u793a\u4e86\u7f51\u683c\u4ea4\u6613\u7b56\u7565\u5728\u67d0\u4e9b\u5e02\u573a\u6761\u4ef6\u4e0b\u7684\u6709\u6548\u6027\uff0c\u5c24\u5176\u662f\u5728\u6ce2\u52a8\u6027\u8f83\u9ad8\u7684\u5e02\u573a\u4e2d\uff0c\u8be5\u7b56\u7565\u786e\u5b9e\u80fd\u591f\u5b9e\u73b0\u7a33\u5b9a\u7684\u6536\u76ca\u589e\u957f\u3002<\/p>\n\n\n\n<p><strong>\u98ce\u9669\u548c\u6536\u76ca\u7684\u5e73\u8861<\/strong>\uff1a\u6587\u7ae0\u4e2d\u7684\u56de\u6eaf\u6d4b\u8bd5\u7ed3\u679c\u663e\u793a\uff0c\u7f51\u683c\u4ea4\u6613\u7b56\u7565\u5728\u77ed\u65f6\u95f4\u5185\u5b9e\u73b0\u4e86\u9ad8\u6536\u76ca\uff0c\u4f46\u540c\u65f6\u4f34\u968f\u7740\u8f83\u9ad8\u7684\u6ce2\u52a8\u6027\u548c\u98ce\u9669\uff0c\u8fd9\u8981\u6c42\u4ea4\u6613\u8005\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u5bf9\u7b56\u7565\u8fdb\u884c\u9002\u5f53\u7684\u8c03\u6574\u548c\u4f18\u5316\u3002<\/p>\n\n\n\n<p><strong>\u7b56\u7565\u7684\u7cfb\u7edf\u5316\u548c\u81ea\u52a8\u5316<\/strong>\uff1a\u901a\u8fc7 Python \u7f16\u7a0b\u5b9e\u73b0\u7f51\u683c\u4ea4\u6613\u7b56\u7565\uff0c\u53ef\u4ee5\u7cfb\u7edf\u5316\u548c\u81ea\u52a8\u5316\u4ea4\u6613\u51b3\u7b56\u8fc7\u7a0b\uff0c\u51cf\u5c11\u4e2a\u4eba\u60c5\u7eea\u6ce2\u52a8\u5bf9\u4ea4\u6613\u7684\u5f71\u54cd\u3002<\/p>\n\n\n\n<p><strong>\u56de\u6eaf\u6d4b\u8bd5\u7684\u91cd\u8981\u6027<\/strong>\uff1a\u6211\u9700\u8981\u518d\u6b21\u5f3a\u8c03\u56de\u6eaf\u6d4b\u8bd5\u5728\u8bc4\u4f30\u548c\u4f18\u5316\u4ea4\u6613\u7b56\u7565\u4e2d\u7684\u91cd\u8981\u6027\uff0c\u901a\u8fc7\u56de\u6eaf\u6d4b\u8bd5\u53ef\u4ee5\u5728\u5b9e\u9645\u6295\u5165\u8d44\u91d1\u4e4b\u524d\u8bc4\u4f30\u7b56\u7565\u7684\u7a33\u5065\u6027\u548c\u6f5c\u5728\u6027\u80fd\u3002<\/p>\n\n\n\n<p><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p class=\"has-text-align-center\"><strong><mark style=\"background-color:#ffffff\" class=\"has-inline-color has-cyan-bluish-gray-color\">\u672c<\/mark><\/strong><strong><mark style=\"background-color:#ffffff\" class=\"has-inline-color has-cyan-bluish-gray-color\">\u6587\u5185\u5bb9\u4ec5\u4ec5\u662f\u6280\u672f\u63a2\u8ba8\u548c\u5b66\u4e60\uff0c\u5e76\u4e0d\u6784\u6210\u4efb\u4f55\u6295\u8d44\u5efa\u8bae\u3002<\/mark><\/strong><\/p>\n\n\n\n<p class=\"has-text-align-center\"><strong><mark style=\"background-color:#ffffff\" class=\"has-inline-color has-cyan-bluish-gray-color\">\u8f6c\u53d1\u8bf7\u6ce8\u660e\u539f\u4f5c\u8005\u548c\u51fa\u5904\u3002<\/mark><\/strong><\/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\/2024\/10\/21\/%e7%9c%9f%e6%ad%a3%e6%89%8b%e6%8a%8a%e6%89%8b%e6%95%99%e4%bc%9a%e4%bd%a0%ef%bc%9a%e7%94%a8-python-%e6%9e%84%e5%bb%ba%e7%bd%91%e6%a0%bc%e4%ba%a4%e6%98%93%e7%ad%96%e7%95%a5\/\">Continue reading<span class=\"screen-reader-text\">\u771f\u6b63\u624b\u628a\u624b\u6559\u4f1a\u4f60\uff1a\u7528 Python \u6784\u5efa\u7f51\u683c\u4ea4\u6613\u7b56\u7565<\/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":[3,4,5],"class_list":{"0":"post-1533","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"hentry","6":"category-aiinvest","7":"tag-ai","10":"entry"},"_links":{"self":[{"href":"https:\/\/laoyulaoyu.com\/index.php\/wp-json\/wp\/v2\/posts\/1533","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=1533"}],"version-history":[{"count":1,"href":"https:\/\/laoyulaoyu.com\/index.php\/wp-json\/wp\/v2\/posts\/1533\/revisions"}],"predecessor-version":[{"id":1534,"href":"https:\/\/laoyulaoyu.com\/index.php\/wp-json\/wp\/v2\/posts\/1533\/revisions\/1534"}],"wp:attachment":[{"href":"https:\/\/laoyulaoyu.com\/index.php\/wp-json\/wp\/v2\/media?parent=1533"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/laoyulaoyu.com\/index.php\/wp-json\/wp\/v2\/categories?post=1533"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/laoyulaoyu.com\/index.php\/wp-json\/wp\/v2\/tags?post=1533"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}