{"id":1865,"date":"2025-01-29T07:37:00","date_gmt":"2025-01-28T23:37:00","guid":{"rendered":"https:\/\/blog.laoyulaoyu.top\/?p=1865"},"modified":"2025-01-04T12:54:58","modified_gmt":"2025-01-04T04:54:58","slug":"python%e5%8a%a8%e9%87%8f%e7%ad%96%e7%95%a5%e5%ae%9e%e6%88%98%ef%bc%9a%e5%a4%a7%e5%b9%85%e8%b7%91%e8%b5%a2%e5%b8%82%e5%9c%ba%e7%9a%84%e5%ba%95%e5%b1%82%e9%80%bb%e8%be%91","status":"publish","type":"post","link":"https:\/\/www.laoyulaoyu.com\/index.php\/2025\/01\/29\/python%e5%8a%a8%e9%87%8f%e7%ad%96%e7%95%a5%e5%ae%9e%e6%88%98%ef%bc%9a%e5%a4%a7%e5%b9%85%e8%b7%91%e8%b5%a2%e5%b8%82%e5%9c%ba%e7%9a%84%e5%ba%95%e5%b1%82%e9%80%bb%e8%be%91\/","title":{"rendered":"Python\u52a8\u91cf\u7b56\u7565\u5b9e\u6218\uff1a\u5927\u5e45\u8dd1\u8d62\u5e02\u573a\u7684\u5e95\u5c42\u903b\u8f91"},"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\/01\/112601.png\" alt=\"\" class=\"wp-image-3736\"\/><\/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\u6211\u6df1\u5165\u7814\u7a76\u4e86<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">\u4e00\u79cd\u57fa\u4e8e\u52a8\u91cf\u7b56\u7565\u7684\u91cf\u5316\u6295\u8d44\u65b9\u6cd5\uff0c\u5e76\u7528Python\u5b9e\u73b0\u4e86\u5b83<\/mark>\u3002\u52a8\u91cf\u7b56\u7565\u7684\u6838\u5fc3\u662f\u201c\u8ffd\u6da8\u6740\u8dcc\u201d\uff0c\u901a\u8fc7\u5386\u53f2\u6570\u636e\u9884\u6d4b\u672a\u6765\u8d8b\u52bf\u3002\u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u5206\u4eab\u4e86\u5982\u4f55\u7528Python\u4ece\u6570\u636e\u83b7\u53d6\u3001\u7b56\u7565\u6784\u5efa\u5230\u56de\u6d4b\u7684\u5168\u8fc7\u7a0b\u3002\u5982\u679c\u4f60\u5bf9\u91cf\u5316\u6295\u8d44\u611f\u5174\u8da3\uff0c\u8fd9\u7bc7\u6587\u7ae0\u4f1a\u662f\u4e00\u4e2a\u4e0d\u9519\u7684\u5165\u95e8\u6307\u5357\u3002<\/pre>\n<\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u4e00\u3001\u4ec0\u4e48\u662f\u52a8\u91cf\u6295\u8d44\u7b56\u7565\uff1f<\/strong><\/h2>\n\n\n\n<p><strong>\u52a8\u91cf\u6295\u8d44<\/strong>\uff08Momentum Investing\uff09 \u662f\u4e00\u79cd\u57fa\u4e8e\u4ef7\u683c\u8d8b\u52bf\u548c\u5e02\u573a\u60c5\u7eea\u7684\u6295\u8d44\u7b56\u7565\uff0c\u65e8\u5728\u901a\u8fc7\u6355\u6349\u4ef7\u683c\u52a8\u91cf\u6765\u5b9e\u73b0\u76c8\u5229\u3002<\/p>\n\n\n\n<p>\u52a8\u91cf\u6295\u8d44\u7684\u6838\u5fc3\u5728\u4e8e<strong>\u201c\u4e70\u5165\u5f3a\u52bf\u80a1\uff0c\u5356\u51fa\u5f31\u52bf\u80a1\u201d<\/strong>\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\/01\/image-22.png\" alt=\"\" class=\"wp-image-3738\"\/><\/figure>\n\n\n\n<p>\u8fd9\u79cd\u6295\u8d44\u7b56\u7565\u7684\u6210\u529f\u5f80\u5f80\u4f9d\u8d56\u4e8e\u5e02\u573a\u7684\u5fc3\u7406\u56e0\u7d20\uff0c\u56e0\u4e3a\u6295\u8d44\u8005\u7684\u60c5\u7eea\u548c\u884c\u4e3a\u4f1a\u5f71\u54cd\u8d44\u4ea7\u4ef7\u683c\u7684\u6ce2\u52a8\u3002\u52a8\u91cf\u6295\u8d44\u9002\u5408\u90a3\u4e9b\u613f\u610f\u627f\u62c5\u4e00\u5b9a\u98ce\u9669\u5e76\u80fd\u591f\u5feb\u901f\u53cd\u5e94\u5e02\u573a\u53d8\u5316\u7684\u6295\u8d44\u8005\u3002<\/p>\n\n\n\n<p>\u52a8\u91cf\u6295\u8d44\u7b56\u7565\u901a\u5e38\u4ee5\u4e0b\u51e0\u4e2a\u6b65\u9aa4\u8fdb\u884c\u5b9e\u65bd\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/www.laoyulaoyu.com\/wp-content\/uploads\/2025\/01\/image-23.png\" alt=\"\" class=\"wp-image-3739\"\/><\/figure>\n\n\n\n<ul id=\"bh-OKbRc865ysPnP_-XlOLVX\" class=\"wp-block-list\">\n<li><strong>\u7b5b\u9009\u80a1\u7968<\/strong>\uff1a\u6295\u8d44\u8005\u4f1a\u6839\u636e\u8fc7\u53bb\u51e0\u4e2a\u6708\u6216\u51e0\u5e74\u7684\u8868\u73b0\u6765\u7b5b\u9009\u51fa\u8868\u73b0\u4f18\u5f02\u7684\u80a1\u7968\uff08\u5373\u52a8\u91cf\u80a1\u7968\uff09\u548c\u8868\u73b0\u4e0d\u4f73\u7684\u80a1\u7968\uff08\u5373\u53cd\u52a8\u91cf\u80a1\u7968\uff09\u3002<\/li>\n\n\n\n<li><strong>\u5efa\u7acb\u6295\u8d44\u7ec4\u5408<\/strong>\uff1a\u6295\u8d44\u8005\u4f1a\u5c06\u9009\u4e2d\u7684\u52a8\u91cf\u80a1\u7968\u7eb3\u5165\u6295\u8d44\u7ec4\u5408\uff0c\u540c\u65f6\u53ef\u80fd\u4f1a\u5356\u7a7a\u53cd\u52a8\u91cf\u80a1\u7968\uff0c\u4ee5\u671f\u5728\u4ef7\u683c\u56de\u8c03\u65f6\u83b7\u5229\u3002<\/li>\n\n\n\n<li><strong>\u5b9a\u671f\u8c03\u6574<\/strong>\uff1a\u52a8\u91cf\u6295\u8d44\u8005\u901a\u5e38\u4f1a\u5b9a\u671f\u91cd\u65b0\u8bc4\u4f30\u548c\u8c03\u6574\u6295\u8d44\u7ec4\u5408\uff0c\u4ee5\u786e\u4fdd\u6301\u7eed\u6301\u6709\u8868\u73b0\u826f\u597d\u7684\u80a1\u7968\uff0c\u5e76\u5254\u9664\u8868\u73b0\u4e0d\u4f73\u7684\u80a1\u7968\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u52a8\u91cf\u6295\u8d44\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u7b56\u7565\uff0c\u65e8\u5728\u5229\u7528\u5e02\u573a\u73b0\u6709\u8d8b\u52bf\u7684\u6301\u7eed\u6027\u3002\u901a\u8fc7\u5173\u6ce8\u8fc7\u53bb\u8868\u73b0\u826f\u597d\u7684\u80a1\u7968\uff0c\u6295\u8d44\u8005\u6709\u53ef\u80fd\u4e58\u52bf\u53d6\u5f97\u53ef\u89c2\u7684\u56de\u62a5\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u4e8c\u3001\u52a8\u91cf\u6295\u8d44\u7b56\u7565\u8bbe\u5b9a<\/strong><\/h2>\n\n\n\n<p>\u6211\u7684\u52a8\u91cf\u6295\u8d44\u6218\u7565\u4e3b\u8981\u7531\u8fd9\u4e24\u4e2a\u90e8\u5206\u7ec4\u6210\uff1a<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2.1 \u9009\u62e9\u80a1\u7968<\/strong><\/h3>\n\n\n\n<p>\u806a\u660e\u7684\u6295\u8d44\u8005\u90fd\u660e\u767d\u201c\u9009\u62e9\u201d\u8fdc\u5927\u4e8e\u201c\u52aa\u529b\u201d\uff0c\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u63a2\u8ba8\u4e3a Nifty 50 \u6307\u6570\uff08\u65b0\u5174\u7684\u5370\u5ea6\u5e02\u573a\u4e2d\u5e02\u503c\u6700\u5927\u768450\u5bb6\u516c\u53f8\uff09\u4e2d\u7684\u80a1\u7968\u91cf\u8eab\u5b9a\u5236\u7684\u52a8\u91cf\u7b56\u7565\u3002\u4f60\u6ca1\u770b\u9519\uff0c\u4e0d\u662f\u7f8e\u80a1\uff0c\u66f4\u4e0d\u662fMianA\uff0c\u6211\u8ba4\u4e3a\u76ee\u524d\u5168\u7403\u6700\u9002\u5408\u52a8\u91cf\u7b56\u7565\u5b9e\u65bd\u7684\u5730\u65b9\u5c31\u662f\u5370\u5ea6\u80a1\u5e02\uff0c\u6ca1\u6709\u4e4b\u4e00\uff01\u539f\u56e0\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/www.laoyulaoyu.com\/wp-content\/uploads\/2025\/01\/image-24.png\" alt=\"\" class=\"wp-image-3740\"\/><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"bh-94RU78m-UjHMZHMLisg0-\">1. <strong>\u7ecf\u6d4e\u589e\u957f\u6f5c\u529b\u5de8\u5927<\/strong><\/h4>\n\n\n\n<p id=\"bh-AKUgX8ah-yP-WJAFMHhhx\">\u5370\u5ea6\u662f\u5168\u7403\u589e\u957f\u6700\u5feb\u7684\u4e3b\u8981\u7ecf\u6d4e\u4f53\u4e4b\u4e00,2023\u5e74GDP\u589e\u901f\u8fbe\u52307.6%,\u4f4d\u5c45\u5168\u7403\u5341\u5927\u7ecf\u6d4e\u4f53\u4e4b\u9996\u3002\u9ad8\u76db\u9884\u6d4b,\u52302050\u5e74,\u5370\u5ea6\u5728\u5168\u7403\u5e02\u573a\u4e2d\u7684\u4efd\u989d\u5c06\u4ece3%\u63d0\u5347\u81f38%,\u5e76\u8fdb\u4e00\u6b65\u589e\u957f\u81f32075\u5e74\u768412%\u3002\u5370\u5ea6\u7684\u4eba\u53e3\u7ea2\u5229\u662f\u5176\u7ecf\u6d4e\u589e\u957f\u7684\u91cd\u8981\u9a71\u52a8\u529b,14.28\u4ebf\u4eba\u53e3\u4e2d,\u5e74\u9f84\u4e2d\u4f4d\u6570\u4ec5\u4e3a28.4\u5c81,\u5e74\u8f7b\u5316\u7684\u4eba\u53e3\u7ed3\u6784\u4e3a\u6d88\u8d39\u548c\u7ecf\u6d4e\u589e\u957f\u63d0\u4f9b\u4e86\u5f3a\u52b2\u52a8\u529b\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"bh-vj2ddcMm8geLlUQrCUVdM\">2. <strong>\u80a1\u5e02\u957f\u671f\u8868\u73b0\u4f18\u5f02<\/strong><\/h4>\n\n\n\n<p id=\"bh-btSvzj2eqk51b_dGJ9HOr\">\u5370\u5ea6\u80a1\u5e02\u4ee5\u201c\u957f\u725b\u201d\u8457\u79f0,\u81ea2000\u5e74\u4ee5\u6765,\u5370\u5ea6SENSEX30\u6307\u6570\u6da8\u5e45\u8fbe1507%,\u8fdc\u8d85\u540c\u671f\u6807\u666e500\u7684304%\u548c\u7eb3\u65af\u8fbe\u514b\u7684367%\u3002\u8fc7\u53bb5\u5e74,SENSEX30\u6307\u6570\u4e0a\u6da8198%,\u8fdc\u8d85\u7eb3\u65af\u8fbe\u514b\u7684160%\u548c\u65e5\u7ecf225\u6307\u6570\u7684118%\u3002\u8fd9\u79cd\u957f\u671f\u4f18\u5f02\u8868\u73b0\u5f97\u76ca\u4e8e\u5370\u5ea6\u7ecf\u6d4e\u7684\u5f3a\u52b2\u589e\u957f\u548c\u80a1\u5e02\u5236\u5ea6\u7684\u5b8c\u5584\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"bh-liyRfO2CIWZgFn3GPOj-z\">3. <strong>\u5b8c\u5584\u7684\u6295\u8d44\u8005\u4fdd\u62a4\u5236\u5ea6<\/strong><\/h4>\n\n\n\n<p id=\"bh-nQlkxkNuQJdD7qzKb4Bv5\">\u5370\u5ea6\u80a1\u5e02\u5b9e\u884c\u4e25\u683c\u7684\u6ce8\u518c\u5236\u548c\u9000\u5e02\u5236\u5ea6,\u786e\u4fdd\u5e02\u573a\u900f\u660e\u5ea6\u548c\u6295\u8d44\u8005\u6743\u76ca\u3002\u4e0a\u5e02\u516c\u53f8\u4e00\u65e6\u88ab\u5f3a\u5236\u9000\u5e02,\u5fc5\u987b\u6309\u4ea4\u6613\u6240\u8bc4\u4f30\u7684\u516c\u5141\u4ef7\u683c\u56de\u8d2d\u516c\u4f17\u6295\u8d44\u8005\u80a1\u4efd,\u5426\u5219\u5c06\u9762\u4e34\u4e25\u5389\u60e9\u7f5a\u3002\u6b64\u5916,\u5370\u5ea6\u8fd8\u5b9e\u884cT+0\u4ea4\u6613\u5236\u5ea6,\u7ed3\u7b97\u5468\u671f\u77ed,\u5e02\u573a\u6d41\u52a8\u6027\u5f3a\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"bh-_EPc09m-elxyJC2EDHmKp\">4. <strong>\u5916\u8d44\u6301\u7eed\u6d41\u5165<\/strong><\/h4>\n\n\n\n<p id=\"bh-pbcQ1zfFRPwTENl4SZ__u\">\u5370\u5ea6\u80a1\u5e02\u7684\u5916\u8d44\u6301\u80a1\u6bd4\u4f8b\u9ad8\u8fbe38%,\u5916\u8d44\u7684\u6301\u7eed\u6d41\u5165\u4e3a\u5e02\u573a\u63d0\u4f9b\u4e86\u5145\u8db3\u7684\u6d41\u52a8\u6027\u548c\u589e\u957f\u52a8\u529b\u3002\u9ad8\u76db\u3001\u5df4\u514b\u83b1\u7b49\u56fd\u9645\u673a\u6784\u5747\u770b\u597d\u5370\u5ea6\u80a1\u5e02,\u8ba4\u4e3a\u5176\u5177\u6709\u201c\u8be5\u5730\u533a\u6700\u597d\u7684\u7ed3\u6784\u6027\u589e\u957f\u524d\u666f\u201d\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"bh-xo34HexW3cvmssVuQgkf5\">5. <strong>\u591a\u6837\u5316\u7684\u6295\u8d44\u673a\u4f1a<\/strong><\/h4>\n\n\n\n<p id=\"bh-EAjP7SfV40VqiFfi7llge\">\u5370\u5ea6\u80a1\u5e02\u6db5\u76d6\u91d1\u878d\u3001\u4fe1\u606f\u6280\u672f\u3001\u80fd\u6e90\u3001\u6d88\u8d39\u54c1\u7b49\u591a\u4e2a\u884c\u4e1a,\u4e3a\u6295\u8d44\u8005\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u9009\u62e9\u3002Nifty50\u6307\u6570\u6210\u5206\u80a1\u4e2d,\u91d1\u878d\u884c\u4e1a\u5360\u6bd4\u6700\u9ad8\uff0832.76%\uff09,\u5176\u6b21\u662f\u4fe1\u606f\u6280\u672f\uff0813.76%\uff09\u548c\u80fd\u6e90\uff0812.12%\uff09\u3002\u6b64\u5916,\u5370\u5ea6\u8fd8\u5141\u8bb8\u5883\u5916\u4f01\u4e1aIPO,\u8fdb\u4e00\u6b65\u63d0\u5347\u4e86\u5e02\u573a\u7684\u56fd\u9645\u5316\u7a0b\u5ea6\u3002<\/p>\n\n\n\n<p>\u5370\u5ea6\u80a1\u5e02\u51ed\u501f\u5176\u5f3a\u52b2\u7684\u7ecf\u6d4e\u589e\u957f\u3001\u957f\u671f\u4f18\u5f02\u8868\u73b0\u3001\u5b8c\u5584\u7684\u6295\u8d44\u8005\u4fdd\u62a4\u5236\u5ea6\u3001\u5916\u8d44\u6301\u7eed\u6d41\u5165\u4ee5\u53ca\u591a\u6837\u5316\u7684\u6295\u8d44\u673a\u4f1a\uff0c\u6210\u4e3a\u5168\u7403\u6295\u8d44\u8005\u7684\u70ed\u95e8\u9009\u62e9\uff0c\u6240\u4ee5\u8fd9\u4e2a\u5e02\u573a\u592a\u9002\u5408\u201c\u8ffd\u6da8\u6740\u8dcc\u201d\u4e86\uff01<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2.2 \u5236\u5b9a\u7b56\u7565<\/strong><\/h3>\n\n\n\n<p>\u52a8\u91cf\u6295\u8d44\u7684\u524d\u63d0\u662f\uff1a\u8fc7\u53bb\u8868\u73b0\u826f\u597d\u7684\u80a1\u7968\u5728\u4e0d\u4e45\u7684\u5c06\u6765\u4f1a\u7ee7\u7eed\u8868\u73b0\u826f\u597d\u3002\u6211\u4eec\u7b56\u7565\u4e00\u53e5\u8bdd\u8bf4\u660e\uff1a\u4e70\u5165\u5728\u7279\u5b9a\u65f6\u671f\uff08\u5982\u8fc7\u53bb 1 \u5e74\uff09\u8868\u73b0\u5f3a\u52b2\u7684\u80a1\u7968\uff0c\u5e76\u5728\u91cd\u65b0\u8bc4\u4f30\u6295\u8d44\u7ec4\u5408\u4e4b\u524d\u6301\u6709\u4e00\u5b9a\u65f6\u95f4\uff08\u5982 1 \u4e2a\u6708\uff09\u3002\u5177\u4f53\u5206\u89e3\u5c31\u662f\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u8ba1\u7b97\u8fc7\u5f80\u56de\u62a5\uff1a\u8ba1\u7b97\u6bcf\u53ea\u80a1\u7968 12 \u4e2a\u6708\u7684\u56de\u62a5\u7387\u3002<\/li>\n\n\n\n<li>\u8fdb\u884c\u80a1\u7968\u6392\u540d\uff1a\u6839\u636e 12 \u4e2a\u6708\u7684\u56de\u62a5\u7387\u5bf9\u80a1\u7968\u8fdb\u884c\u6392\u540d\u3002<\/li>\n\n\n\n<li>\u9009\u62e9\u70ed\u95e8\u80a1\u7968\uff1a\u6311\u9009\u6536\u76ca\u6700\u9ad8\u7684\u524d 10 \u79cd\u80a1\u7968\u3002<\/li>\n\n\n\n<li>\u6bcf\u6708\u91cd\u65b0\u5e73\u8861\uff1a\u6bcf\u6708\u91cd\u65b0\u8bc4\u4f30\u548c\u5e73\u8861\u6295\u8d44\u7ec4\u5408\u3002<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u4e09\u3001\u7b56\u7565\u6267\u884c<\/strong><\/h2>\n\n\n\n<p>\u63a5\u4e0b\u6765\u6211\u4eec\u4f1a\u7528Python\u5e2e\u52a9\u60a8\u5b9e\u65bd\u8be5\u7b56\u7565\uff0c\u5e76\u5bf9\u8be5\u7b56\u7565\u8fdb\u884c\u4e3a\u671f3\u5e74\u7684\u56de\u6d4b\uff0c\u518d\u5c06\u5176\u7ed3\u679c\u4e0eNifty50\u6307\u6570\u7684\u4e70\u5165\u548c\u6301\u6709\u7b56\u7565\u8fdb\u884c\u6536\u76ca\u6bd4\u8f83\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u6b65\u9aa4 1\uff1a\u6570\u636e\u51c6\u5907<\/strong><\/h3>\n\n\n\n<p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u6536\u96c6\u8fc7\u53bb\u4e09\u5e74 Nifty 50 \u80a1\u7968\u7684\u5386\u53f2\u4ef7\u683c\u6570\u636e\u3002\u5728\u8fd9\u91cc\uff0c\u6211\u4f7f\u7528\u96c5\u864e\u8d22\u7ecf api\u83b7\u53d6\u4e86\u8fc7\u53bb\u4e09\u5e74\u7684\u5386\u53f2\u6570\u636e\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import yfinance as yf\nimport pandas as pd\nimport numpy as np\nfrom datetime import datetime, timedelta\n\n# List of Nifty 50 stock symbols\nnifty50_symbols = &#91;\"RELIANCE.NS\", \"HDFCBANK.NS\", \"INFY.NS\", \"ICICIBANK.NS\", \"TCS.NS\", \"KOTAKBANK.NS\", \n                   \"HINDUNILVR.NS\", \"SBIN.NS\", \"BHARTIARTL.NS\", \"HDFC.NS\", \"ITC.NS\", \"BAJFINANCE.NS\", \n                   \"ASIANPAINT.NS\", \"HCLTECH.NS\", \"LT.NS\", \"MARUTI.NS\", \"AXISBANK.NS\", \"ULTRACEMCO.NS\", \n                   \"WIPRO.NS\", \"NESTLEIND.NS\", \"ONGC.NS\", \"TITAN.NS\", \"SUNPHARMA.NS\", \"M&amp;M.NS\", \n                   \"POWERGRID.NS\", \"JSWSTEEL.NS\", \"TATASTEEL.NS\", \"TECHM.NS\", \"HDFCLIFE.NS\", \"COALINDIA.NS\", \n                   \"BPCL.NS\", \"INDUSINDBK.NS\", \"BAJAJ-AUTO.NS\", \"IOC.NS\", \"BRITANNIA.NS\", \"HEROMOTOCO.NS\", \n                   \"ADANIPORTS.NS\", \"DRREDDY.NS\", \"GRASIM.NS\", \"CIPLA.NS\", \"DIVISLAB.NS\", \"EICHERMOT.NS\", \n                   \"BAJAJFINSV.NS\", \"SHREECEM.NS\", \"TATAMOTORS.NS\", \"SBILIFE.NS\", \"ADANIENT.NS\", \n                   \"DABUR.NS\", \"VEDL.NS\", \"APOLLOHOSP.NS\"]\n\n# Define the time period\nend_date = datetime.today()\nstart_date = end_date - timedelta(days=365*3)  # Last 3 years\n\n# Fetch data\ndata = yf.download(nifty50_symbols, start=start_date, end=end_date)&#91;'Adj Close']\n\n# Fill missing values\ndata = data.fillna(method='ffill').dropna()\n\n# Display the first few rows of the data\nprint(data.head())<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u6b65\u9aa4 2\uff1a\u5b9e\u65bd\u7b56\u7565\u5e76\u8ba1\u7b97\u6536\u76ca<\/strong><\/h3>\n\n\n\n<p>\u6211\u4eec\u7528\u4e0b\u9762\u7684\u4ee3\u7801\u6765\u5b9e\u65bd\u52a8\u91cf\u7b56\u7565\uff0c\u5e76\u8ba1\u7b97\u8fc7\u53bb\u4e09\u5e74\u7684\u6295\u8d44\u7ec4\u5408\u56de\u62a5\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">def calculate_portfolio_returns(data, top_n=10):\n    # Calculate monthly returns\n    monthly_returns = data.resample('ME').ffill().pct_change()\n    \n    # Calculate 12-month returns\n    twelve_month_returns = monthly_returns.rolling(window=12).apply(lambda x: np.prod(1 + x) - 1, raw=True)\n    \n    # Initialize an empty list to store monthly portfolio values\n    portfolio_values = []\n    \n    # Start with an initial capital\n    initial_capital = 100000  # 1 lakh\n    capital = initial_capital\n    \n    # Loop through each month starting from the 13th month\n    for i in range(12, len(twelve_month_returns)):\n        # Get the 12-month returns for the current month\n        current_returns = twelve_month_returns.iloc[i]\n        \n        # Rank the stocks by their 12-month returns\n        ranked_stocks = current_returns.sort_values(ascending=False)\n        \n        # Select the top N stocks\n        top_stocks = ranked_stocks.head(top_n).index\n        \n        # Calculate the equal weight for each stock\n        weight = 1 \/ top_n\n        \n        # Calculate the portfolio return for the current month\n        portfolio_return = (monthly_returns.iloc[i][top_stocks] * weight).sum()\n        \n        # Update the capital\n        capital = capital * (1 + portfolio_return)\n        \n        # Append the current capital to the portfolio values list\n        portfolio_values.append(capital)\n    \n    # Convert the portfolio values list to a pandas Series\n    portfolio_values = pd.Series(portfolio_values, index=twelve_month_returns.index[12:])\n    \n    return portfolio_values\n\n# Calculate the portfolio returns\nmomentum_portfolio_returns = calculate_portfolio_returns(data)\n\n# Display the portfolio returns\nprint(momentum_portfolio_returns)<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u6b65\u9aa4 3\uff1a\u4e0e Nifty 50 \u6307\u6570\u6bd4\u8f83<\/strong><\/h3>\n\n\n\n<p>\u4e3a\u4e86\u8bc4\u4f30\u52a8\u91cf\u7b56\u7565\u7684\u8868\u73b0\uff0c\u6211\u4eec\u5c06\u5176\u4e0e\u540c\u671f Nifty 50 \u6307\u6570\u7684\u6536\u76ca\u7387\u8fdb\u884c\u4e86\u6bd4\u8f83\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Fetch Nifty 50 index data\nnifty50_index = yf.download(\"^NSEI\", start=start_date, end=end_date)&#91;'Adj Close']\n\n# Calculate Nifty 50 monthly returns\nnifty50_monthly_returns = nifty50_index.resample('ME').ffill().pct_change()\n\n# Calculate Nifty 50 cumulative returns\nnifty50_cumulative_returns = (1 + nifty50_monthly_returns).cumprod()\n\n# Calculate momentum portfolio cumulative returns\nmomentum_cumulative_returns = (1 + momentum_portfolio_returns.pct_change()).cumprod()\n\n# Plot the results\nimport matplotlib.pyplot as plt\n\nplt.figure(figsize=(12, 6))\nplt.plot(momentum_cumulative_returns, label='Momentum Portfolio')\nplt.plot(nifty50_cumulative_returns, label='Nifty 50 Index', linestyle='--')\nplt.title('Momentum Portfolio vs Nifty 50 Index')\nplt.xlabel('Date')\nplt.ylabel('Cumulative Returns')\nplt.legend()\nplt.grid(True)\nplt.show()<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u56db\u3001\u7ed3\u679c\u4e0e\u5206\u6790<\/strong><\/h2>\n\n\n\n<p>\u4e0b\u56fe\u663e\u793a\u4e86\u52a8\u91cf\u6295\u8d44\u7ec4\u5408\u4e0e Nifty 50 \u6307\u6570\u5728\u8fc7\u53bb\u4e09\u5e74\u4e2d\u7684\u7d2f\u8ba1\u56de\u62a5\u7387\u5bf9\u6bd4\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u4e3b\u8981\u89c2\u5bdf\u7ed3\u679c\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/www.laoyulaoyu.com\/wp-content\/uploads\/2025\/01\/image-25.png\" alt=\"\" class=\"wp-image-3741\"\/><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5728\u6b64\u671f\u95f4\uff0c\u52a8\u91cf\u6295\u8d44\u7ec4\u5408\u7684\u8868\u73b0\u666e\u904d\u4f18\u4e8e Nifty 50 \u6307\u6570\uff0c\u663e\u793a\u4e86\u52a8\u91cf\u7b56\u7565\u7684\u6709\u6548\u6027\u3002<\/li>\n\n\n\n<li>\u5728\u4e00\u4e9b\u6ce2\u52a8\u65f6\u671f\uff0c\u52a8\u91cf\u6295\u8d44\u7ec4\u5408\u7ecf\u5386\u4e86\u5927\u5e45\u6ce2\u52a8\uff0c\u8fd9\u662f\u52a8\u91cf\u578b\u7b56\u7565\u7684\u5178\u578b\u7279\u5f81\u3002<\/li>\n\n\n\n<li>\u603b\u4f53\u800c\u8a00\uff0c\u4e0e\u5355\u7eaf\u6301\u6709 Nifty 50 \u6307\u6570\u76f8\u6bd4\uff0c\u52a8\u91cf\u7b56\u7565\u7684\u6295\u8d44\u56de\u62a5\u7387\u66f4\u9ad8\u3002<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u4e94\u3001\u89c2\u70b9\u603b\u7ed3<\/strong><\/h2>\n\n\n\n<p>\u8fc7\u53bb\u4e09\u5e74\uff0c\u57fa\u4e8eNifty 50\u80a1\u7968\u7684\u52a8\u91cf\u6295\u8d44\u7b56\u7565\u8868\u73b0\u4eae\u773c\u3002\u901a\u8fc7\u7b5b\u900912\u4e2a\u6708\u56de\u62a5\u7387\u6700\u9ad8\u7684\u524d10\u53ea\u80a1\u7968\uff0c\u5e76\u6bcf\u6708\u8fdb\u884c\u6295\u8d44\u7ec4\u5408\u518d\u5e73\u8861\uff0c\u8be5\u7b56\u7565\u6210\u529f\u8dd1\u8d62\u4e86Nifty 50\u6307\u6570\u3002\u7136\u800c\uff0c\u5386\u53f2\u8868\u73b0\u5e76\u4e0d\u80fd\u4fdd\u8bc1\u672a\u6765\u6536\u76ca\uff0c\u6295\u8d44\u8005\u5728\u91c7\u7528\u8fd9\u4e00\u7b56\u7565\u524d\uff0c\u52a1\u5fc5\u8bc4\u4f30\u81ea\u8eab\u98ce\u9669\u627f\u53d7\u80fd\u529b\u548c\u6295\u8d44\u76ee\u6807\uff0c\u8c28\u614e\u51b3\u7b56\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u52a8\u91cf\u6295\u8d44\u7b56\u7565<\/strong>\uff1a\u4f9d\u8d56\u4e8e\u8fc7\u53bb\u8868\u73b0\u826f\u597d\u7684\u80a1\u7968\u5728\u672a\u6765\u53ef\u80fd\u7ee7\u7eed\u8868\u73b0\u826f\u597d\u7684\u7406\u8bba\uff0c\u901a\u8fc7\u9009\u62e9\u548c\u6301\u6709\u8868\u73b0\u6700\u4f73\u7684\u80a1\u7968\u6765\u5b9e\u73b0\u8d85\u989d\u56de\u62a5\u3002<\/li>\n\n\n\n<li><strong>\u7b56\u7565\u5b9e\u65bd<\/strong>\uff1a\u901a\u8fc7\u9009\u62e9 Nifty 50 \u6307\u6570\u80a1\u7968\uff0c\u8ba1\u7b97\u548c\u6392\u540d\u80a1\u7968\u7684\u5386\u53f2\u56de\u62a5\u7387\uff0c\u9009\u62e9\u8868\u73b0\u6700\u4f73\u7684\u80a1\u7968\uff0c\u4ee5\u53ca\u5b9a\u671f\u91cd\u65b0\u5e73\u8861\u6295\u8d44\u7ec4\u5408\u6765\u6267\u884c\u7b56\u7565\u3002<\/li>\n\n\n\n<li><strong>\u56de\u6eaf\u6d4b\u8bd5<\/strong>\uff1a\u4f7f\u7528 Python \u5bf9\u52a8\u91cf\u7b56\u7565\u8fdb\u884c\u4e86\u5386\u53f2\u6570\u636e\u7684\u56de\u6eaf\u6d4b\u8bd5\uff0c\u8bc1\u660e\u4e86\u8be5\u7b56\u7565\u5728\u8fc7\u53bb\u4e09\u5e74\u4e2d\u7684\u6709\u6548\u6027\u548c\u4f18\u4e8e Nifty 50 \u6307\u6570\u7684\u8868\u73b0\u3002<\/li>\n\n\n\n<li><strong>\u98ce\u9669\u63d0\u793a<\/strong>\uff1a\u867d\u7136\u52a8\u91cf\u7b56\u7565\u5728\u5386\u53f2\u4e0a\u8868\u73b0\u826f\u597d\uff0c\u4f46\u8fc7\u53bb\u7684\u8868\u73b0\u5e76\u4e0d\u4fdd\u8bc1\u672a\u6765\u7684\u7ed3\u679c\uff0c\u6295\u8d44\u8005\u5e94\u8c28\u614e\u8003\u8651\u4e2a\u4eba\u7684\u98ce\u9669\u627f\u53d7\u80fd\u529b\u548c\u6295\u8d44\u76ee\u6807\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u611f\u8c22\u60a8\u9605\u8bfb\u5230\u6700\u540e\uff0c\u5e0c\u671b\u672c\u6587\u80fd\u7ed9\u60a8\u5e26\u6765\u65b0\u7684\u6536\u83b7\u3002\u795d\u60a8\u6295\u8d44\u987a\u5229\uff01\u5982\u679c\u5bf9\u6587\u4e2d\u7684\u5185\u5bb9\u6709\u4efb\u4f55\u7591\u95ee\uff0c\u8bf7\u7ed9\u6211\u7559\u8a00\uff0c\u5fc5\u590d\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p class=\"has-text-align-center\" id=\"a1c6\">\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:\/\/www.laoyulaoyu.com\/index.php\/2025\/01\/29\/python%e5%8a%a8%e9%87%8f%e7%ad%96%e7%95%a5%e5%ae%9e%e6%88%98%ef%bc%9a%e5%a4%a7%e5%b9%85%e8%b7%91%e8%b5%a2%e5%b8%82%e5%9c%ba%e7%9a%84%e5%ba%95%e5%b1%82%e9%80%bb%e8%be%91\/\">Continue reading<span class=\"screen-reader-text\">Python\u52a8\u91cf\u7b56\u7565\u5b9e\u6218\uff1a\u5927\u5e45\u8dd1\u8d62\u5e02\u573a\u7684\u5e95\u5c42\u903b\u8f91<\/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],"class_list":{"0":"post-1865","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"hentry","6":"category-aiinvest","7":"tag-ai","9":"entry"},"_links":{"self":[{"href":"https:\/\/www.laoyulaoyu.com\/index.php\/wp-json\/wp\/v2\/posts\/1865","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laoyulaoyu.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laoyulaoyu.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laoyulaoyu.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laoyulaoyu.com\/index.php\/wp-json\/wp\/v2\/comments?post=1865"}],"version-history":[{"count":1,"href":"https:\/\/www.laoyulaoyu.com\/index.php\/wp-json\/wp\/v2\/posts\/1865\/revisions"}],"predecessor-version":[{"id":1866,"href":"https:\/\/www.laoyulaoyu.com\/index.php\/wp-json\/wp\/v2\/posts\/1865\/revisions\/1866"}],"wp:attachment":[{"href":"https:\/\/www.laoyulaoyu.com\/index.php\/wp-json\/wp\/v2\/media?parent=1865"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laoyulaoyu.com\/index.php\/wp-json\/wp\/v2\/categories?post=1865"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laoyulaoyu.com\/index.php\/wp-json\/wp\/v2\/tags?post=1865"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}