{"id":6807,"date":"2025-12-28T13:20:26","date_gmt":"2025-12-28T05:20:26","guid":{"rendered":"http:\/\/cnliutz.ipyingshe.net\/?p=6807"},"modified":"2025-12-28T13:30:56","modified_gmt":"2025-12-28T05:30:56","slug":"%e8%82%a1%e7%a5%a8%e5%88%86%e6%9e%90akshre%e8%8e%b7%e5%8f%96%e8%82%a1%e7%a5%a8%e6%95%b0%e6%8d%ae%e7%bb%98%e5%9b%be%e5%b8%b8%e7%94%a8%e6%8c%87%e6%a0%87python","status":"publish","type":"post","link":"http:\/\/xc.ipyingshe.net:5347\/?p=6807","title":{"rendered":"\u80a1\u7968\u5206\u6790akshare\u83b7\u53d6\u80a1\u7968\u6570\u636e\u7ed8\u56fe\u5e38\u7528\u6307\u6807Python"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport akshare as ak\nfrom datetime import datetime\n\ndef get_stock_data(stock_code, start_date, end_date):\n    \"\"\"\n    \u4f7f\u7528akshare\u83b7\u53d6\u80a1\u7968\u5386\u53f2\u6570\u636e\n    \n    \u53c2\u6570:\n    stock_code: \u80a1\u7968\u4ee3\u7801\uff0c\u4f8b\u5982 'SH600519'\uff08\u8d35\u5dde\u8305\u53f0\uff09\u6216 'sz000001'\uff08\u5e73\u5b89\u94f6\u884c\uff09\n    start_date: \u5f00\u59cb\u65e5\u671f\uff0c\u683c\u5f0f 'YYYY-MM-DD'\n    end_date: \u7ed3\u675f\u65e5\u671f\uff0c\u683c\u5f0f 'YYYY-MM-DD'\n    \n    \u8fd4\u56de:\n    \u5305\u542b\u80a1\u7968\u5386\u53f2\u6570\u636e\u7684DataFrame\n    \"\"\"\n    try:\n        # \u8f6c\u6362\u65e5\u671f\u683c\u5f0f\u4e3aakshare\u8981\u6c42\u7684\u683c\u5f0f\n        start_date_ak = start_date.replace('-', '')\n        end_date_ak = end_date.replace('-', '')\n        \n        # \u8f6c\u6362\u80a1\u7968\u4ee3\u7801\u683c\u5f0f\u4e3aakshare\u8981\u6c42\u7684\u683c\u5f0f\n        if stock_code.startswith('SH'):\n            stock_code_ak = f'sh{stock_code&#91;2:]}'\n        elif stock_code.startswith('SZ'):\n            stock_code_ak = f'sz{stock_code&#91;2:]}'\n        else:\n            # \u5047\u8bbe\u5df2\u7ecf\u662f\u6b63\u786e\u683c\u5f0f\n            stock_code_ak = stock_code\n        \n        # \u4f7f\u7528akshare\u83b7\u53d6\u80a1\u7968\u6570\u636e\n        stock_df = ak.stock_zh_a_hist(symbol=stock_code_ak&#91;2:], period=\"daily\", \n                                     start_date=start_date_ak, end_date=end_date_ak)\n        \n        stock_df&#91;'\u65e5\u671f'] = pd.to_datetime(stock_df&#91;'\u65e5\u671f'])\n        stock_df.set_index('\u65e5\u671f', inplace=True)\n        return stock_df\n    except Exception as e:\n        print(f\"\u83b7\u53d6\u6570\u636e\u5931\u8d25: {e}\")\n        return None\n\ndef calculate_technical_indicators(df):\n    \"\"\"\n    \u8ba1\u7b97\u5e38\u7528\u6280\u672f\u6307\u6807\n    \n    \u53c2\u6570:\n    df: \u5305\u542b\u80a1\u7968\u6570\u636e\u7684DataFrame\n    \n    \u8fd4\u56de:\n    \u5305\u542b\u8ba1\u7b97\u540e\u6307\u6807\u7684DataFrame\n    \"\"\"\n    # \u8ba1\u7b97\u5747\u7ebf\n    df&#91;'MA5'] = df&#91;'\u6536\u76d8'].rolling(window=5).mean()  # 5\u65e5\u5747\u7ebf\n    df&#91;'MA10'] = df&#91;'\u6536\u76d8'].rolling(window=10).mean()  # 10\u65e5\u5747\u7ebf\n    df&#91;'MA20'] = df&#91;'\u6536\u76d8'].rolling(window=20).mean()  # 20\u65e5\u5747\u7ebf\n    df&#91;'MA60'] = df&#91;'\u6536\u76d8'].rolling(window=60).mean()  # 60\u65e5\u5747\u7ebf\n    \n    # \u8ba1\u7b97RSI\u6307\u6807\n    def calculate_rsi(data, period=14):\n        delta = data&#91;'\u6536\u76d8'].diff(1)\n        gain = delta.where(delta > 0, 0)\n        loss = -delta.where(delta &lt; 0, 0)\n        \n        avg_gain = gain.rolling(window=period).mean()\n        avg_loss = loss.rolling(window=period).mean()\n        \n        rs = avg_gain \/ avg_loss\n        rsi = 100 - (100 \/ (1 + rs))\n        return rsi\n    \n    df&#91;'RSI14'] = calculate_rsi(df, period=14)\n    \n    # \u8ba1\u7b97\u80a1\u4ef7\u53d8\u5316\u6807\u51c6\u5dee\uff0820\u65e5\u7a97\u53e3\uff09\n    df&#91;'\u6536\u76d8\u4ef7\u6807\u51c6\u5dee'] = df&#91;'\u6536\u76d8'].rolling(window=20).std()\n    \n    # \u8ba1\u7b97\u65e5\u6536\u76ca\u7387\n    df&#91;'\u65e5\u6536\u76ca\u7387'] = df&#91;'\u6536\u76d8'].pct_change()\n    \n    return df\n\ndef plot_stock_analysis(df, stock_code):\n    \"\"\"\n    \u7ed8\u5236\u80a1\u7968\u5206\u6790\u56fe\u8868\n    \n    \u53c2\u6570:\n    df: \u5305\u542b\u80a1\u7968\u6570\u636e\u548c\u6307\u6807\u7684DataFrame\n    stock_code: \u80a1\u7968\u4ee3\u7801\n    \"\"\"\n    # \u8bbe\u7f6e\u4e2d\u6587\u663e\u793a\n    plt.rcParams&#91;'font.family'] = &#91;'SimHei', 'Microsoft YaHei']\n    plt.rcParams&#91;'axes.unicode_minus'] = False\n    \n    # \u521b\u5efa\u4e00\u4e2a\u5305\u542b4\u4e2a\u5b50\u56fe\u7684\u753b\u5e03\n    fig, (ax1, ax2, ax3, ax4) = plt.subplots(4, 1, figsize=(14, 16), sharex=True)\n    \n    # 1. \u7ed8\u5236\u80a1\u4ef7\u548c\u5747\u7ebf\n    ax1.plot(df.index, df&#91;'\u6536\u76d8'], label='\u6536\u76d8\u4ef7', linewidth=1.5, color='black')\n    ax1.plot(df.index, df&#91;'MA5'], label='MA5', linewidth=1.2, color='blue')\n    ax1.plot(df.index, df&#91;'MA10'], label='MA10', linewidth=1.2, color='green')\n    ax1.plot(df.index, df&#91;'MA20'], label='MA20', linewidth=1.2, color='orange')\n    ax1.plot(df.index, df&#91;'MA60'], label='MA60', linewidth=1.2, color='red')\n    ax1.set_title(f'{stock_code} \u80a1\u4ef7\u4e0e\u5747\u7ebf\u8d70\u52bf')\n    ax1.set_ylabel('\u4ef7\u683c (\u5143)')\n    ax1.legend()\n    ax1.grid(True, linestyle='--', alpha=0.7)\n    \n    # 2. \u7ed8\u5236\u6210\u4ea4\u91cf\n    ax2.bar(df.index, df&#91;'\u6210\u4ea4\u91cf'], label='\u6210\u4ea4\u91cf', color='purple', alpha=0.6)\n    ax2.set_title('\u6210\u4ea4\u91cf')\n    ax2.set_ylabel('\u6210\u4ea4\u91cf')\n    ax2.grid(True, linestyle='--', alpha=0.7)\n    \n    # 3. \u7ed8\u5236RSI\n    ax3.plot(df.index, df&#91;'RSI14'], label='RSI14', linewidth=1.2, color='brown')\n    ax3.axhline(y=70, color='red', linestyle='--', alpha=0.7, label='\u8d85\u4e70\u7ebf')\n    ax3.axhline(y=30, color='green', linestyle='--', alpha=0.7, label='\u8d85\u5356\u7ebf')\n    ax3.set_title('RSI\u6307\u6807')\n    ax3.set_ylabel('RSI\u503c')\n    ax3.legend()\n    ax3.grid(True, linestyle='--', alpha=0.7)\n    \n    # 4. \u7ed8\u5236\u6536\u76d8\u4ef7\u6807\u51c6\u5dee\n    ax4.plot(df.index, df&#91;'\u6536\u76d8\u4ef7\u6807\u51c6\u5dee'], label='\u6536\u76d8\u4ef7\u6807\u51c6\u5dee', linewidth=1.2, color='teal')\n    ax4.set_title('\u80a1\u4ef7\u53d8\u5316\u6807\u51c6\u5dee')\n    ax4.set_ylabel('\u6807\u51c6\u5dee')\n    ax4.set_xlabel('\u65e5\u671f')\n    ax4.legend()\n    ax4.grid(True, linestyle='--', alpha=0.7)\n    \n    # \u8c03\u6574\u5e03\u5c40\n    plt.tight_layout()\n    plt.subplots_adjust(hspace=0.4)\n    \n    # \u663e\u793a\u56fe\u8868\n    plt.show()\n\nif __name__ == \"__main__\":\n    # \u8bbe\u7f6e\u53c2\u6570\n    stock_code = \"SH600938\"  # \u4e2d\u56fd\u5de5\u5546\u94f6\u884c\n    start_date = \"2025-01-01\"\n    end_date = datetime.now().strftime(\"%Y-%m-%d\")\n    \n    print(f\"\u6b63\u5728\u83b7\u53d6 {stock_code} \u4ece {start_date} \u5230 {end_date} \u7684\u6570\u636e...\")\n    \n    # \u83b7\u53d6\u6570\u636e\n    stock_data = get_stock_data(stock_code, start_date, end_date)\n    \n    if stock_data is not None and not stock_data.empty:\n        print(f\"\u6210\u529f\u83b7\u53d6 {len(stock_data)} \u6761\u6570\u636e\")\n        \n        # \u8ba1\u7b97\u6307\u6807\n        print(\"\u6b63\u5728\u8ba1\u7b97\u6280\u672f\u6307\u6807...\")\n        stock_data_with_indicators = calculate_technical_indicators(stock_data)\n        \n        # \u663e\u793a\u6570\u636e\u9884\u89c8\n        print(\"\\n\u6570\u636e\u9884\u89c8:\")\n        print(stock_data_with_indicators.tail())\n        \n        # \u7ed8\u56fe\n        print(\"\\n\u6b63\u5728\u7ed8\u5236\u56fe\u8868...\")\n        plot_stock_analysis(stock_data_with_indicators, stock_code)\n    else:\n        print(\"\u83b7\u53d6\u6570\u636e\u5931\u8d25\uff0c\u8bf7\u68c0\u67e5\u80a1\u7968\u4ee3\u7801\u548c\u7f51\u7edc\u8fde\u63a5\")<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">\u80a1\u7968\u6570\u636e\u5206\u6790\u7a0b\u5e8f\u603b\u7ed3<\/h1>\n\n\n\n<h2 class=\"wp-block-heading\">\u7a0b\u5e8f\u529f\u80fd<\/h2>\n\n\n\n<p>\u672c\u7a0b\u5e8f\u5b9e\u73b0\u4e86\u80a1\u7968\u6570\u636e\u7684\u83b7\u53d6\u3001\u6280\u672f\u6307\u6807\u8ba1\u7b97\u548c\u53ef\u89c6\u5316\u5206\u6790\u529f\u80fd\uff0c\u4e3b\u8981\u5305\u62ec\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u6570\u636e\u83b7\u53d6<\/strong>\uff1a\u4f7f\u7528akshare\u5e93\u83b7\u53d6\u6307\u5b9a\u80a1\u7968\u7684\u5386\u53f2\u65e5\u7ebf\u6570\u636e<\/li>\n\n\n\n<li><strong>\u6307\u6807\u8ba1\u7b97<\/strong>\uff1a\u4f7f\u7528pandas\u8ba1\u7b97\u591a\u79cd\u5e38\u7528\u6280\u672f\u6307\u6807<\/li>\n\n\n\n<li><strong>\u53ef\u89c6\u5316\u5206\u6790<\/strong>\uff1a\u901a\u8fc7matplotlib\u7ed8\u5236\u76f4\u89c2\u7684\u5206\u6790\u56fe\u8868<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">\u6280\u672f\u5b9e\u73b0<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. \u6570\u636e\u83b7\u53d6 (<code>get_stock_data<\/code>\u51fd\u6570)<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4f7f\u7528akshare\u5e93\u83b7\u53d6A\u80a1\u80a1\u7968\u5386\u53f2\u6570\u636e<\/li>\n\n\n\n<li>\u652f\u6301\u81ea\u52a8\u8f6c\u6362\u80a1\u7968\u4ee3\u7801\u683c\u5f0f\uff08\u5982&#8217;SH600938&#8217;\u8f6c\u6362\u4e3a&#8217;sh600938&#8217;\uff09<\/li>\n\n\n\n<li>\u652f\u6301\u81ea\u52a8\u8f6c\u6362\u65e5\u671f\u683c\u5f0f\uff08\u5982&#8217;2024-01-01&#8217;\u8f6c\u6362\u4e3a&#8217;20240101&#8217;\uff09<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">2. \u6307\u6807\u8ba1\u7b97 (<code>calculate_technical_indicators<\/code>\u51fd\u6570)<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u5747\u7ebf\u6307\u6807<\/strong>\uff1a\u8ba1\u7b975\u65e5\u300110\u65e5\u300120\u65e5\u548c60\u65e5\u5747\u7ebf<\/li>\n\n\n\n<li><strong>RSI\u6307\u6807<\/strong>\uff1a\u8ba1\u7b9714\u65e5\u76f8\u5bf9\u5f3a\u5f31\u6307\u6807<\/li>\n\n\n\n<li><strong>\u6210\u4ea4\u91cf<\/strong>\uff1a\u76f4\u63a5\u4ece\u539f\u59cb\u6570\u636e\u83b7\u53d6<\/li>\n\n\n\n<li><strong>\u80a1\u4ef7\u53d8\u5316\u6807\u51c6\u5dee<\/strong>\uff1a\u8ba1\u7b9720\u65e5\u7a97\u53e3\u7684\u6536\u76d8\u4ef7\u6807\u51c6\u5dee<\/li>\n\n\n\n<li><strong>\u65e5\u6536\u76ca\u7387<\/strong>\uff1a\u7528\u4e8e\u98ce\u9669\u5206\u6790\u53c2\u8003<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">3. \u53ef\u89c6\u5316\u5206\u6790 (<code>plot_stock_analysis<\/code>\u51fd\u6570)<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u7ed8\u52364\u4e2a\u5b50\u56fe\u7684\u7efc\u5408\u5206\u6790\u56fe\u8868\uff1a<\/li>\n\n\n\n<li>\u80a1\u4ef7\u4e0e\u591a\u5468\u671f\u5747\u7ebf\u5bf9\u6bd4\u56fe<\/li>\n\n\n\n<li>\u6210\u4ea4\u91cf\u67f1\u72b6\u56fe<\/li>\n\n\n\n<li>RSI\u6307\u6807\u56fe\uff08\u542b\u8d85\u4e70\u8d85\u5356\u7ebf\uff09<\/li>\n\n\n\n<li>\u80a1\u4ef7\u53d8\u5316\u6807\u51c6\u5dee\u56fe<\/li>\n\n\n\n<li>\u652f\u6301\u4e2d\u6587\u663e\u793a\uff0c\u786e\u4fdd\u56fe\u8868\u53ef\u8bfb\u6027<\/li>\n\n\n\n<li>\u63d0\u4f9b\u7f51\u683c\u7ebf\u3001\u56fe\u4f8b\u7b49\u8f85\u52a9\u5143\u7d20<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u4f7f\u7528\u65b9\u6cd5<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u786e\u4fdd\u5b89\u88c5\u4e86\u5fc5\u8981\u7684\u5e93\uff1a<code>pandas<\/code>\u3001<code>numpy<\/code>\u3001<code>matplotlib<\/code>\u3001<code>akshare<\/code><\/li>\n\n\n\n<li>\u4fee\u6539<code>stock_code<\/code>\u3001<code>start_date<\/code>\u53c2\u6570\u53ef\u67e5\u770b\u4e0d\u540c\u80a1\u7968\u548c\u65f6\u95f4\u6bb5\u7684\u6570\u636e<\/li>\n\n\n\n<li>\u8fd0\u884c\u7a0b\u5e8f\u540e\u4f1a\u81ea\u52a8\u6267\u884c\u6570\u636e\u83b7\u53d6\u3001\u6307\u6807\u8ba1\u7b97\u548c\u56fe\u8868\u7ed8\u5236<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">\u4f18\u52bf<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u65e0\u9700\u6ce8\u518c\u8d26\u6237\u5373\u53ef\u4f7f\u7528\uff08\u907f\u514d\u4e86tqcenter\u9700\u8981\u767b\u5f55\u7684\u95ee\u9898\uff09<\/li>\n\n\n\n<li>\u4ee3\u7801\u7b80\u6d01\u6613\u61c2\uff0c\u4fbf\u4e8e\u4fee\u6539\u548c\u6269\u5c55<\/li>\n\n\n\n<li>\u652f\u6301\u591a\u79cd\u5e38\u7528\u6280\u672f\u6307\u6807\uff0c\u6ee1\u8db3\u57fa\u672c\u5206\u6790\u9700\u6c42<\/li>\n\n\n\n<li>\u56fe\u8868\u76f4\u89c2\u6e05\u6670\uff0c\u4fbf\u4e8e\u7406\u89e3\u80a1\u7968\u8d70\u52bf\u548c\u6307\u6807\u53d8\u5316<\/li>\n<\/ul>\n\n\n\n<p>\u8be5\u7a0b\u5e8f\u9002\u5408\u80a1\u7968\u6295\u8d44\u8005\u548c\u5206\u6790\u5e08\u8fdb\u884c\u57fa\u672c\u7684\u6280\u672f\u5206\u6790\uff0c\u4e5f\u53ef\u4f5c\u4e3a\u5b66\u4e60\u91d1\u878d\u6570\u636e\u5904\u7406\u548c\u53ef\u89c6\u5316\u7684\u53c2\u8003\u793a\u4f8b\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"534\" src=\"http:\/\/cnliutz.ipyingshe.net\/wp-content\/uploads\/2025\/12\/Figure_1_600938_2025-1024x534.png\" alt=\"\" class=\"wp-image-6808\" srcset=\"http:\/\/xc.ipyingshe.net:5347\/wp-content\/uploads\/2025\/12\/Figure_1_600938_2025-1024x534.png 1024w, http:\/\/xc.ipyingshe.net:5347\/wp-content\/uploads\/2025\/12\/Figure_1_600938_2025-300x157.png 300w, http:\/\/xc.ipyingshe.net:5347\/wp-content\/uploads\/2025\/12\/Figure_1_600938_2025-768x401.png 768w, http:\/\/xc.ipyingshe.net:5347\/wp-content\/uploads\/2025\/12\/Figure_1_600938_2025-1536x802.png 1536w, http:\/\/xc.ipyingshe.net:5347\/wp-content\/uploads\/2025\/12\/Figure_1_600938_2025-2048x1069.png 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>\u80a1\u7968\u6570\u636e\u5206\u6790\u7a0b\u5e8f\u603b\u7ed3 \u7a0b\u5e8f\u529f\u80fd \u672c\u7a0b\u5e8f\u5b9e\u73b0\u4e86\u80a1\u7968\u6570\u636e\u7684\u83b7\u53d6\u3001\u6280\u672f\u6307\u6807\u8ba1\u7b97\u548c\u53ef\u89c6\u5316 <span class=\"readmore\"><a href=\"http:\/\/xc.ipyingshe.net:5347\/?p=6807\">Continue Reading<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10,24],"tags":[],"class_list":["post-6807","post","type-post","status-publish","format-standard","hentry","category-python","category-24"],"_links":{"self":[{"href":"http:\/\/xc.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts\/6807","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/xc.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/xc.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/xc.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/xc.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6807"}],"version-history":[{"count":2,"href":"http:\/\/xc.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts\/6807\/revisions"}],"predecessor-version":[{"id":6810,"href":"http:\/\/xc.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts\/6807\/revisions\/6810"}],"wp:attachment":[{"href":"http:\/\/xc.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6807"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/xc.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6807"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/xc.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6807"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}