{"id":6763,"date":"2025-12-18T10:55:25","date_gmt":"2025-12-18T02:55:25","guid":{"rendered":"http:\/\/cnliutz.ipyingshe.net\/?p=6763"},"modified":"2025-12-19T23:15:44","modified_gmt":"2025-12-19T15:15:44","slug":"stock-analysis","status":"publish","type":"post","link":"http:\/\/xc.ipyingshe.net:5347\/?p=6763","title":{"rendered":"\u67d0\u53ea\u80a1\u7968\u6ce2\u52a8\u53caema\u6307\u6807\u5206\u6790Stock analysis"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\n# \u8bbe\u7f6e\u4e2d\u6587\u663e\u793a\nplt.rcParams&#91;\"font.family\"] = &#91;\"SimHei\", \"Microsoft YaHei\", \"SimSun\", \"KaiTi\", \"FangSong\"]\nplt.rcParams&#91;\"axes.unicode_minus\"] = False\nimport akshare as ak\nfrom datetime import datetime, timedelta\n\n\ndef fetch_stock_data(symbol, period='1y'):\n    \"\"\"\u83b7\u53d6\u80a1\u7968\u6570\u636e\"\"\"\n    end_date = datetime.now().strftime('%Y%m%d')\n    start_date = (datetime.now() - timedelta(days=365)).strftime('%Y%m%d')\n    \n    # \u5904\u7406\u80a1\u7968\u4ee3\u7801\u683c\u5f0f\uff0c\u786e\u4fdd\u7b26\u5408akshare\u8981\u6c42\n    if not symbol.startswith(('sh', 'sz')):\n        if symbol.startswith(('6', '9')):\n            symbol = f\"sh{symbol}\"\n        else:\n            symbol = f\"sz{symbol}\"\n    \n    # \u4f7f\u7528\u66f4\u7a33\u5b9a\u7684stock_zh_a_hist\u51fd\u6570\u83b7\u53d6\u80a1\u7968\u65e5\u7ebf\u6570\u636e\n    data = ak.stock_zh_a_hist(symbol=symbol&#91;2:],  # \u53ea\u4f20\u80a1\u7968\u4ee3\u7801\u90e8\u5206\uff0c\u4e0d\u542bsh\/sz\n                             period=\"daily\",\n                             start_date=start_date,\n                             end_date=end_date,\n                             adjust=\"qfq\")  # \u4f7f\u7528\u524d\u590d\u6743\u4ef7\u683c\n    \n    # \u68c0\u67e5\u6570\u636e\u662f\u5426\u83b7\u53d6\u6210\u529f\n    if data.empty:\n        print(f\"\u83b7\u53d6\u80a1\u7968{symbol}\u6570\u636e\u5931\u8d25\uff0c\u8bf7\u68c0\u67e5\u80a1\u7968\u4ee3\u7801\u548c\u7f51\u7edc\u8fde\u63a5\")\n        return None\n    \n    # \u5148\u8f6c\u6362\u65e5\u671f\u683c\u5f0f\n    if '\u65e5\u671f' in data.columns:\n        data&#91;'\u65e5\u671f'] = pd.to_datetime(data&#91;'\u65e5\u671f'])\n        # \u8bbe\u7f6e\u65e5\u671f\u4e3a\u7d22\u5f15\n        data.set_index('\u65e5\u671f', inplace=True)\n        # \u91cd\u547d\u540d\u6536\u76d8\u4ef7\u5217\n        if '\u6536\u76d8' in data.columns:\n            data = data.rename(columns={'\u6536\u76d8': 'Close'})\n    else:\n        print(\"\u6570\u636e\u683c\u5f0f\u5f02\u5e38\uff0c\u65e0\u6cd5\u5904\u7406\")\n        return None\n    \n    return data\n\n\ndef calculate_ema(data, window=20):\n    \"\"\"\u8ba1\u7b97\u6307\u6570\u79fb\u52a8\u5e73\u5747\u7ebf EMA\"\"\"\n    ema = data&#91;'Close'].ewm(span=window, adjust=False).mean()\n    return ema\n\n\ndef calculate_std(data, window=20):\n    \"\"\"\u8ba1\u7b97\u6807\u51c6\u5dee STD\"\"\"\n    std = data&#91;'Close'].rolling(window=window).std()\n    return std\n\n\ndef plot_analysis(data, ema, std, symbol):\n    \"\"\"\u7ed8\u5236\u56fe\u8868\u5206\u6790\u7ed3\u679c\"\"\"\n    plt.figure(figsize=(14, 8))\n\n    plt.subplot(2, 1, 1)\n    plt.plot(data&#91;'Close'], label='\u6536\u76d8\u4ef7', color='blue')\n    plt.plot(ema, label='EMA(20)', color='orange')\n    plt.title(f'{symbol} \u80a1\u7968\u4ef7\u683c\u4e0eEMA\u5206\u6790')\n    plt.legend()\n\n    plt.subplot(2, 1, 2)\n    plt.plot(std, label='STD(20)', color='red')\n    plt.title(f'{symbol} \u80a1\u7968\u4ef7\u683c\u6ce2\u52a8\u6027(STD)\u5206\u6790')\n    plt.legend()\n\n    plt.tight_layout()\n    plt.show()\n\n\ndef main():\n    symbol = input(\"\u8bf7\u8f93\u5165\u80a1\u7968\u4ee3\u7801\uff08\u5982 000001 \u6216 600000\uff09\uff1a\")\n    data = fetch_stock_data(symbol)\n    \n    # \u68c0\u67e5\u6570\u636e\u83b7\u53d6\u662f\u5426\u6210\u529f\n    if data is not None:\n        ema = calculate_ema(data)\n        std = calculate_std(data)\n        plot_analysis(data, ema, std, symbol)\n\n\nif __name__ == '__main__':\n    main()\n<\/code><\/pre>\n\n\n\n<p>\u6211\u6765\u4e3a\u60a8\u8be6\u7ec6\u89e3\u91ca\u8fd9\u4e2a\u80a1\u7968\u5206\u6790\u7a0b\u5e8f\u8f93\u51fa\u7684\u56fe\u8868\u548cEMA\u6307\u6807\u7684\u7528\u6cd5\uff1a<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u7a0b\u5e8f\u8f93\u51fa\u7684\u56fe\u8868\u8bf4\u660e<\/h2>\n\n\n\n<p>\u7a0b\u5e8f\u8fd0\u884c\u540e\u4f1a\u751f\u6210\u4e00\u4e2a\u5305\u542b\u4e24\u4e2a\u5b50\u56fe\u7684\u56fe\u8868\uff1a<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. \u80a1\u7968\u4ef7\u683c\u4e0eEMA\u5206\u6790\uff08\u4e0a\u56fe\uff09<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u84dd\u8272\u66f2\u7ebf<\/strong>\uff1a\u80a1\u7968\u7684\u6bcf\u65e5\u6536\u76d8\u4ef7\u8d70\u52bf<\/li>\n\n\n\n<li><strong>\u6a59\u8272\u66f2\u7ebf<\/strong>\uff1a20\u65e5\u6307\u6570\u79fb\u52a8\u5e73\u5747\u7ebf(EMA(20))<\/li>\n\n\n\n<li><strong>\u6807\u9898<\/strong>\uff1a\u663e\u793a\u5f53\u524d\u5206\u6790\u7684\u80a1\u7968\u4ee3\u7801\u548c\u5206\u6790\u7c7b\u578b<\/li>\n\n\n\n<li><strong>\u56fe\u4f8b<\/strong>\uff1a\u6807\u8bc6\u4e0d\u540c\u66f2\u7ebf\u7684\u542b\u4e49<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">2. \u80a1\u7968\u4ef7\u683c\u6ce2\u52a8\u6027(STD)\u5206\u6790\uff08\u4e0b\u56fe\uff09<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u7ea2\u8272\u66f2\u7ebf<\/strong>\uff1a20\u65e5\u6536\u76d8\u4ef7\u7684\u6807\u51c6\u5dee(STD(20))<\/li>\n\n\n\n<li><strong>\u6807\u9898<\/strong>\uff1a\u663e\u793a\u5f53\u524d\u5206\u6790\u7684\u80a1\u7968\u4ee3\u7801\u548c\u6ce2\u52a8\u6027\u5206\u6790\u7c7b\u578b<\/li>\n\n\n\n<li><strong>\u56fe\u4f8b<\/strong>\uff1a\u6807\u8bc6\u6807\u51c6\u5dee\u66f2\u7ebf<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">EMA\u6307\u6807\u8be6\u89e3<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u4ec0\u4e48\u662fEMA\uff1f<\/h3>\n\n\n\n<p>EMA\uff08Exponential Moving Average\uff09\u5373<strong>\u6307\u6570\u79fb\u52a8\u5e73\u5747\u7ebf<\/strong>\uff0c\u662f\u4e00\u79cd\u52a0\u6743\u79fb\u52a8\u5e73\u5747\u7ebf\uff0c\u5bf9\u6700\u8fd1\u7684\u4ef7\u683c\u7ed9\u4e88\u66f4\u591a\u6743\u91cd\uff0c\u53cd\u5e94\u66f4\u7075\u654f\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">EMA\u7684\u8ba1\u7b97\u65b9\u6cd5<\/h3>\n\n\n\n<p>\u7a0b\u5e8f\u4e2d\u4f7f\u7528\u7684\u8ba1\u7b97\u516c\u5f0f\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ema = data&#91;'Close'].ewm(span=window, adjust=False).mean()<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>span=window<\/code>\uff1a\u8bbe\u7f6e\u8ba1\u7b97\u7a97\u53e3\u5927\u5c0f\uff08\u8fd9\u91cc\u662f20\u5929\uff09<\/li>\n\n\n\n<li><code>adjust=False<\/code>\uff1a\u4f7f\u7528\u6807\u51c6\u7684\u6307\u6570\u79fb\u52a8\u5e73\u5747\u8ba1\u7b97\u516c\u5f0f\uff0c\u4e0d\u8fdb\u884c\u8c03\u6574<\/li>\n<\/ul>\n\n\n\n<p>\u6570\u5b66\u516c\u5f0f\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>EMA(t) = Price(t) \u00d7 k + EMA(t-1) \u00d7 (1 - k)\n\u5176\u4e2d\uff1ak = 2 \/ (n + 1)\uff0cn\u662f\u7a97\u53e3\u5927\u5c0f<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">EMA\u4e0eSMA\u7684\u533a\u522b<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>SMA\uff08\u7b80\u5355\u79fb\u52a8\u5e73\u5747\uff09<\/strong>\uff1a\u5bf9\u6240\u6709\u5468\u671f\u4ef7\u683c\u7ed9\u4e88\u76f8\u540c\u6743\u91cd\uff0c\u53cd\u5e94\u8f83\u8fdf\u7f13<\/li>\n\n\n\n<li><strong>EMA\uff08\u6307\u6570\u79fb\u52a8\u5e73\u5747\uff09<\/strong>\uff1a\u5bf9\u8fd1\u671f\u4ef7\u683c\u7ed9\u4e88\u66f4\u9ad8\u6743\u91cd\uff0c\u80fd\u66f4\u5feb\u53cd\u6620\u4ef7\u683c\u53d8\u5316<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">20\u65e5EMA\u7684\u5177\u4f53\u5e94\u7528<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">1. \u5224\u65ad\u8d8b\u52bf\u65b9\u5411<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u5f53\u4ef7\u683c\u5728EMA\u4e0a\u65b9<\/strong>\uff1a\u901a\u5e38\u8868\u793a\u5904\u4e8e\u4e0a\u5347\u8d8b\u52bf<\/li>\n\n\n\n<li><strong>\u5f53\u4ef7\u683c\u5728EMA\u4e0b\u65b9<\/strong>\uff1a\u901a\u5e38\u8868\u793a\u5904\u4e8e\u4e0b\u964d\u8d8b\u52bf<\/li>\n\n\n\n<li><strong>EMA\u659c\u7387\u5411\u4e0a<\/strong>\uff1a\u8d8b\u52bf\u53ef\u80fd\u7ee7\u7eed\u4e0a\u5347<\/li>\n\n\n\n<li><strong>EMA\u659c\u7387\u5411\u4e0b<\/strong>\uff1a\u8d8b\u52bf\u53ef\u80fd\u7ee7\u7eed\u4e0b\u964d<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">2. \u652f\u6491\u4f4d\u4e0e\u963b\u529b\u4f4d<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u652f\u6491\u4f4d<\/strong>\uff1a\u4ef7\u683c\u4e0b\u8dcc\u5230EMA\u9644\u8fd1\u65f6\u53ef\u80fd\u83b7\u5f97\u652f\u6491\u53cd\u5f39<\/li>\n\n\n\n<li><strong>\u963b\u529b\u4f4d<\/strong>\uff1a\u4ef7\u683c\u4e0a\u6da8\u5230EMA\u9644\u8fd1\u65f6\u53ef\u80fd\u9047\u5230\u963b\u529b\u56de\u8c03<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">3. \u4ea4\u6613\u4fe1\u53f7<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u91d1\u53c9<\/strong>\uff1a\u77ed\u671fEMA\uff08\u59825\u65e5\uff09\u4e0a\u7a7f\u957f\u671fEMA\uff08\u598220\u65e5\uff09\uff0c\u53ef\u80fd\u53d1\u51fa\u4e70\u5165\u4fe1\u53f7<\/li>\n\n\n\n<li><strong>\u6b7b\u53c9<\/strong>\uff1a\u77ed\u671fEMA\u4e0b\u7a7f\u957f\u671fEMA\uff0c\u53ef\u80fd\u53d1\u51fa\u5356\u51fa\u4fe1\u53f7<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">4. \u4ef7\u683c\u4e0eEMA\u7684\u504f\u79bb\u7a0b\u5ea6<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4ef7\u683c\u4e0eEMA\u504f\u79bb\u8fc7\u5927\u65f6\uff0c\u901a\u5e38\u4f1a\u56de\u5f52\uff0c\u8fd9\u662f\u4e00\u79cd\u8d85\u4e70\u8d85\u5356\u7684\u5224\u65ad\u4f9d\u636e<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u7ed3\u5408\u6807\u51c6\u5dee(STD)\u7684\u5206\u6790<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u6807\u51c6\u5dee<\/strong>\uff1a\u8861\u91cf\u4ef7\u683c\u7684\u6ce2\u52a8\u6027\u6216\u98ce\u9669\u7a0b\u5ea6<\/li>\n\n\n\n<li><strong>\u9ad8\u6807\u51c6\u5dee<\/strong>\uff1a\u4ef7\u683c\u6ce2\u52a8\u5927\uff0c\u98ce\u9669\u9ad8\uff0c\u53ef\u80fd\u5904\u4e8e\u8d8b\u52bf\u53cd\u8f6c\u6216\u5267\u70c8\u6ce2\u52a8\u671f<\/li>\n\n\n\n<li><strong>\u4f4e\u6807\u51c6\u5dee<\/strong>\uff1a\u4ef7\u683c\u6ce2\u52a8\u5c0f\uff0c\u98ce\u9669\u4f4e\uff0c\u53ef\u80fd\u5904\u4e8e\u6a2a\u76d8\u6574\u7406\u671f<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u5b9e\u9645\u5e94\u7528\u793a\u4f8b<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u8d8b\u52bf\u786e\u8ba4<\/strong>\uff1a\u5982\u679c\u4ef7\u683c\u6301\u7eed\u572820\u65e5EMA\u4e0a\u65b9\u8fd0\u884c\uff0c\u4e14EMA\u659c\u7387\u5411\u4e0a\uff0c\u8bf4\u660e\u4e0a\u5347\u8d8b\u52bf\u5f3a\u52b2<\/li>\n\n\n\n<li><strong>\u5165\u573a\u65f6\u673a<\/strong>\uff1a\u5f53\u4ef7\u683c\u56de\u8c03\u523020\u65e5EMA\u9644\u8fd1\u83b7\u5f97\u652f\u6491\uff0c\u5e76\u4f34\u968f\u6210\u4ea4\u91cf\u653e\u5927\u65f6\uff0c\u53ef\u80fd\u662f\u8f83\u597d\u7684\u4e70\u5165\u65f6\u673a<\/li>\n\n\n\n<li><strong>\u6b62\u635f\u8bbe\u7f6e<\/strong>\uff1a\u53ef\u4ee5\u5c06\u6b62\u635f\u4f4d\u8bbe\u7f6e\u572820\u65e5EMA\u4e0b\u65b9\u4e00\u5b9a\u6bd4\u4f8b\u5904\uff0c\u4fdd\u62a4\u5229\u6da6<\/li>\n\n\n\n<li><strong>\u98ce\u9669\u8bc4\u4f30<\/strong>\uff1a\u7ed3\u5408\u6807\u51c6\u5dee\u66f2\u7ebf\uff0c\u5982\u679c\u4ef7\u683c\u4e0a\u5347\u4f46\u6807\u51c6\u5dee\u4e0b\u964d\uff0c\u8bf4\u660e\u8d8b\u52bf\u7a33\u5b9a\uff1b\u5982\u679c\u6807\u51c6\u5dee\u540c\u65f6\u4e0a\u5347\uff0c\u8bf4\u660e\u6ce2\u52a8\u52a0\u5927\uff0c\u98ce\u9669\u589e\u52a0<\/li>\n<\/ol>\n\n\n\n<p>\u8fd9\u4e2a\u7a0b\u5e8f\u63d0\u4f9b\u4e86\u57fa\u7840\u7684\u6280\u672f\u5206\u6790\u5de5\u5177\uff0c\u60a8\u53ef\u4ee5\u6839\u636e\u81ea\u5df1\u7684\u4ea4\u6613\u7b56\u7565\u8fdb\u4e00\u6b65\u4f18\u5316\u53c2\u6570\uff08\u5982\u8c03\u6574EMA\u7684\u7a97\u53e3\u5927\u5c0f\uff09\u6216\u7ed3\u5408\u5176\u4ed6\u6280\u672f\u6307\u6807\uff08\u5982MACD\u3001RSI\u7b49\uff09\u8fdb\u884c\u66f4\u5168\u9762\u7684\u5206\u6790\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6211\u6765\u4e3a\u60a8\u8be6\u7ec6\u89e3\u91ca\u8fd9\u4e2a\u80a1\u7968\u5206\u6790\u7a0b\u5e8f\u8f93\u51fa\u7684\u56fe\u8868\u548cEMA\u6307\u6807\u7684\u7528\u6cd5\uff1a \u7a0b\u5e8f\u8f93\u51fa\u7684\u56fe\u8868\u8bf4 <span class=\"readmore\"><a href=\"http:\/\/xc.ipyingshe.net:5347\/?p=6763\">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-6763","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\/6763","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=6763"}],"version-history":[{"count":3,"href":"http:\/\/xc.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts\/6763\/revisions"}],"predecessor-version":[{"id":6771,"href":"http:\/\/xc.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts\/6763\/revisions\/6771"}],"wp:attachment":[{"href":"http:\/\/xc.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6763"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/xc.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6763"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/xc.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6763"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}