{"id":3444,"date":"2026-03-08T23:40:15","date_gmt":"2026-03-08T15:40:15","guid":{"rendered":"http:\/\/xuebuwan.com\/wp\/?p=3444"},"modified":"2026-03-08T23:40:15","modified_gmt":"2026-03-08T15:40:15","slug":"python%e9%87%8f%e5%8c%96%e6%8a%95%e8%b5%84%e5%85%a5%e9%97%a8%ef%bc%9a%e6%89%8b%e6%8a%8a%e6%89%8b%e6%95%99%e4%bd%a0%e5%86%99%e5%87%ba%e7%ac%ac%e4%b8%80%e4%b8%aa%e9%80%89%e8%82%a1%e7%ad%96%e7%95%a5","status":"publish","type":"post","link":"http:\/\/xuebuwan.com\/wp\/?p=3444","title":{"rendered":"Python\u91cf\u5316\u6295\u8d44\u5165\u95e8\uff1a\u624b\u628a\u624b\u6559\u4f60\u5199\u51fa\u7b2c\u4e00\u4e2a\u9009\u80a1\u7b56\u7565"},"content":{"rendered":"<h2>\u524d\u8a00<\/h2>\n<p>\u5f88\u591a\u6563\u6237\u60f3\u5b66\u91cf\u5316\uff0c\u4f46\u89c9\u5f97\u95e8\u69db\u592a\u9ad8\u3002\u5176\u5b9e\u7528Python\u5b9e\u73b0\u4e00\u4e2a\u7b80\u5355\u7684\u9009\u80a1\u7b56\u7565\uff0c\u5e76\u6ca1\u6709\u90a3\u4e48\u96be\u3002\u672c\u6587\u5c06\u624b\u628a\u624b\u6559\u4f60\u7528Python\u5199\u51fa\u7b2c\u4e00\u4e2a\u53ef\u4ee5\u8dd1\u901a\u7684\u9009\u80a1\u7b56\u7565\u3002<\/p>\n<h2>\u4e00\u3001\u4ec0\u4e48\u662f\u91cf\u5316\u6295\u8d44\uff1f<\/h2>\n<p>\u91cf\u5316\u6295\u8d44\u5c31\u662f\u7528<strong>\u6570\u5b66\u6a21\u578b<\/strong>\u548c<strong>\u8ba1\u7b97\u673a\u7a0b\u5e8f<\/strong>\u6765\u6307\u5bfc\u6295\u8d44\u51b3\u7b56\uff0c\u7279\u70b9\u662f\uff1a<\/p>\n<ul><li><strong>\u7eaa\u5f8b\u6027\u5f3a<\/strong>\uff1a\u4e25\u683c\u6309\u7167\u7b56\u7565\u6267\u884c\uff0c\u4e0d\u53d7\u60c5\u7eea\u5f71\u54cd<\/li>\n<li><strong>\u53cd\u5e94\u5feb<\/strong>\uff1a\u7a0b\u5e8f\u53ef\u4ee5\u540c\u65f6\u5904\u7406\u6210\u5343\u4e0a\u4e07\u53ea\u80a1\u7968<\/li>\n<li><strong>\u8986\u76d6\u9762\u5e7f<\/strong>\uff1a\u53ef\u4ee5\u56de\u6d4b\u5386\u53f2\u6570\u636e\uff0c\u9a8c\u8bc1\u7b56\u7565\u6709\u6548\u6027<\/li><\/ul>\n<h2>\u4e8c\u3001\u73af\u5883\u51c6\u5907<\/h2>\n<p>\u4f60\u9700\u8981\u5b89\u88c5Python\uff0c\u7136\u540e\u5b89\u88c5\u4ee5\u4e0b\u5e93\uff1a<\/p>\n<pre><code>pip install akshare pandas numpy<\/code><\/pre>\n<p><strong>akshare<\/strong> \u662f\u4e00\u4e2a\u514d\u8d39\u7684A\u80a1\u6570\u636e\u63a5\u53e3\uff0c\u975e\u5e38\u9002\u5408\u65b0\u624b\u4f7f\u7528<\/p>\n<h2>\u4e09\u3001\u7b2c\u4e00\u4e2a\u9009\u80a1\u7b56\u7565<\/h2>\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684<strong>MACD\u91d1\u53c9\u9009\u80a1\u7b56\u7565<\/strong>\uff1a<\/p>\n<pre><code>import akshare as ak\nimport pandas as pd\nimport numpy as np\n\n# \u83b7\u53d6\u80a1\u7968\u6570\u636e\uff08\u4ee5\u5e73\u5b89\u94f6\u884c000001\u4e3a\u4f8b\uff09\nstock_df = ak.stock_zh_a_hist(\n    symbol=&quot;000001&quot;, \n    period=&quot;daily&quot;, \n    start_date=&quot;20230101&quot;, \n    end_date=&quot;20231231&quot;\n)\n\n# \u8ba1\u7b97MACD\u6307\u6807\nstock_df[EMA12] = stock_df[\u6536\u76d8].ewm(span=12).mean()\nstock_df[EMA26] = stock_df[\u6536\u76d8].ewm(span=26).mean()\nstock_df[DIF] = stock_df[EMA12] - stock_df[EMA26]\nstock_df[DEA] = stock_df[DIF].ewm(span=9).mean()\nstock_df[MACD] = 2 * (stock_df[DIF] - stock_df[DEA])\n\n# \u91d1\u53c9\u4fe1\u53f7\uff1aDIF\u4ece\u4e0b\u65b9\u7a7f\u8d8aDEA\ndef get_signal(row, df, idx):\n    if idx == 0:\n        return 0\n    prev = df.iloc[idx-1]\n    if row[DIF] &gt; row[DEA] and prev[DIF] = prev[DEA]:\n        return -1  # \u6b7b\u53c9\n    return 0\n\nstock_df[signal] = [get_signal(row, stock_df, i) for i, row in stock_df.iterrows()]\n\n# \u9009\u51fa\u91d1\u53c9\u7684\u65e5\u671f\ngolden_cross = stock_df[stock_df[signal] == 1]\nprint(f&quot;MACD\u91d1\u53c9\u6b21\u6570: {len(golden_cross)}&quot;)\nprint(golden_cross[[\u65e5\u671f, \u6536\u76d8, DIF, DEA]])<\/code><\/pre>\n<h2>\u56db\u3001\u56de\u6d4b\u4e00\u4e0b<\/h2>\n<p>\u5149\u6709\u9009\u80a1\u4fe1\u53f7\u8fd8\u4e0d\u591f\uff0c\u6211\u4eec\u9700\u8981\u77e5\u9053\u8fd9\u4e2a\u7b56\u7565\u5728\u8fc7\u53bb\u80fd\u4e0d\u80fd\u8d5a\u94b1\uff1a<\/p>\n<pre><code># \u7b80\u5355\u56de\u6d4b\u6846\u67b6\ndef backtest(df, initial_money=100000):\n    money = initial_money\n    position = 0  # \u6301\u4ed3\u80a1\u6570\n    buy_price = 0\n\n    for i, row in df.iterrows():\n        if row[signal] == 1 and position == 0:  # \u91d1\u53c9\u4e70\u5165\n            position = money \/\/ row[\u6536\u76d8]\n            buy_price = row[\u6536\u76d8]\n            money = money - position * buy_price\n            print(f&quot;\u4e70\u5165: {row[\u65e5\u671f]}, \u4ef7\u683c: {buy_price}&quot;)\n\n        elif row[signal] == -1 and position &gt; 0:  # \u6b7b\u53c9\u5356\u51fa\n            money = position * row[\u6536\u76d8]\n            print(f&quot;\u5356\u51fa: {row[\u65e5\u671f]}, \u4ef7\u683c: {row[\u6536\u76d8]}&quot;)\n            position = 0\n\n    # \u6700\u7ec8\u6536\u76ca\n    final_value = money + position * df.iloc[-1][\u6536\u76d8]\n    return_final_value = (final_value - initial_money) \/ initial_money * 100\n\n    print(f&quot;\u6700\u7ec8\u8d44\u91d1: {final_value:.2f}&quot;)\n    print(f&quot;\u6536\u76ca\u7387: {return_final_value:.2f}%&quot;)\n\n# \u8fd0\u884c\u56de\u6d4b\nbacktest(stock_df)<\/code><\/pre>\n<h2>\u4e94\u3001\u7b56\u7565\u4f18\u5316\u65b9\u5411<\/h2>\n<p>\u8fd9\u53ea\u662f\u4e00\u4e2a\u6700\u7b80\u5355\u7684\u793a\u4f8b\u3002\u771f\u6b63\u7684\u91cf\u5316\u6295\u8d44\u9700\u8981\u8003\u8651\uff1a<\/p>\n<ul><li><strong>\u624b\u7eed\u8d39\u548c\u6ed1\u70b9<\/strong>\uff1a\u5b9e\u76d8\u4ea4\u6613\u6210\u672c<\/li>\n<li><strong>\u4ed3\u4f4d\u7ba1\u7406<\/strong>\uff1a\u6bcf\u6b21\u4e70\u591a\u5c11<\/li>\n<li><strong>\u98ce\u9669\u63a7\u5236<\/strong>\uff1a\u6b62\u635f\u6b62\u76c8<\/li>\n<li><strong>\u591a\u56e0\u5b50\u7ec4\u5408<\/strong>\uff1a\u591a\u4e2a\u6307\u6807\u7ed3\u5408<\/li><\/ul>\n<h2>\u516d\u3001MACD\u7b56\u7565\u7684\u4f18\u7f3a\u70b9<\/h2>\n<p><strong>\u4f18\u70b9<\/strong>\uff1a<\/p>\n<ul><li>\u6307\u6807\u8ba1\u7b97\u7b80\u5355\uff0c\u5bb9\u6613\u7406\u89e3<\/li>\n<li>\u5728\u8d8b\u52bf\u660e\u663e\u7684\u884c\u60c5\u4e2d\u8868\u73b0\u8f83\u597d<\/li><\/ul>\n<p><strong>\u7f3a\u70b9<\/strong>\uff1a<\/p>\n<ul><li>\u9707\u8361\u884c\u60c5\u4e2d\u5bb9\u6613\u53cd\u590d\u4e8f\u635f<\/li>\n<li>\u6ede\u540e\u6027\u8f83\u5f3a<\/li>\n<li>\u9700\u8981\u7ed3\u5408\u5176\u4ed6\u6307\u6807\u8fc7\u6ee4\u5047\u4fe1\u53f7<\/li><\/ul>\n<h2>\u603b\u7ed3<\/h2>\n<p>\u672c\u6587\u6559\u5927\u5bb6\u7528Python\u5199\u4e86\u4e00\u4e2a\u6700\u7b80\u5355\u7684MACD\u9009\u80a1\u7b56\u7565\u3002\u4f46<strong>\u80a1\u5e02\u6709\u98ce\u9669\uff0c\u5165\u5e02\u9700\u8c28\u614e<\/strong>\u3002\u8fd9\u4e2a\u7b56\u7565\u53ea\u662fdemo\u7ea7\u522b\uff0c\u5b9e\u76d8\u9700\u8981\uff1a<\/p>\n<ol><li>\u66f4\u591a\u56e0\u5b50\u9a8c\u8bc1<\/li>\n<li>\u4e25\u683c\u7684\u98ce\u63a7\u4f53\u7cfb<\/li>\n<li>\u8db3\u591f\u7684\u56de\u6d4b\u6570\u636e<\/li>\n<li>\u6a21\u62df\u76d8\u6d4b\u8bd5<\/li><\/ol>\n<p>\u5982\u679c\u4f60\u60f3\u7ee7\u7eed\u5b66\u4e60\u91cf\u5316\uff0c\u53ef\u4ee5\u5173\u6ce8\u6211\u4eec\u7684\u540e\u7eed\u6559\u7a0b\u3002\u4e0b\u671f\u9884\u544a\uff1a<strong>\u5982\u4f55\u7528Python\u642d\u5efa\u5b8c\u6574\u7684\u56de\u6d4b\u7cfb\u7edf<\/strong>\u3002<\/p>\n<blockquote><p><strong>\u58f0\u660e<\/strong>\uff1a\u672c\u6587\u4ec5\u4f9b\u53c2\u8003\uff0c\u4e0d\u6784\u6210\u6295\u8d44\u5efa\u8bae\u3002\u6295\u8d44\u6709\u98ce\u9669\uff0c\u5165\u5e02\u9700\u8c28\u614e\u3002<\/p><\/blockquote>","protected":false},"excerpt":{"rendered":"<p>\u524d\u8a00 \u5f88\u591a\u6563\u6237\u60f3\u5b66\u91cf\u5316\uff0c\u4f46\u89c9\u5f97\u95e8\u69db\u592a\u9ad8\u3002\u5176\u5b9e\u7528Python\u5b9e\u73b0\u4e00\u4e2a\u7b80\u5355\u7684\u9009\u80a1\u7b56\u7565\uff0c\u5e76\u6ca1\u6709\u90a3\u4e48\u96be\u3002\u672c\u6587\u5c06\u624b\u628a\u624b\u6559 &#8230; <a title=\"Python\u91cf\u5316\u6295\u8d44\u5165\u95e8\uff1a\u624b\u628a\u624b\u6559\u4f60\u5199\u51fa\u7b2c\u4e00\u4e2a\u9009\u80a1\u7b56\u7565\" class=\"read-more\" href=\"http:\/\/xuebuwan.com\/wp\/?p=3444\" aria-label=\"\u9605\u8bfb Python\u91cf\u5316\u6295\u8d44\u5165\u95e8\uff1a\u624b\u628a\u624b\u6559\u4f60\u5199\u51fa\u7b2c\u4e00\u4e2a\u9009\u80a1\u7b56\u7565\">\u9605\u8bfb\u66f4\u591a<\/a><\/p>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-3444","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"http:\/\/xuebuwan.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/3444","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/xuebuwan.com\/wp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/xuebuwan.com\/wp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/xuebuwan.com\/wp\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/xuebuwan.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3444"}],"version-history":[{"count":1,"href":"http:\/\/xuebuwan.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/3444\/revisions"}],"predecessor-version":[{"id":3445,"href":"http:\/\/xuebuwan.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/3444\/revisions\/3445"}],"wp:attachment":[{"href":"http:\/\/xuebuwan.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3444"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/xuebuwan.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3444"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/xuebuwan.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3444"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}