{"id":5653,"date":"2025-05-25T17:44:53","date_gmt":"2025-05-25T09:44:53","guid":{"rendered":"http:\/\/192.168.1.29\/?p=5653"},"modified":"2025-05-25T17:45:51","modified_gmt":"2025-05-25T09:45:51","slug":"python%e8%82%a1%e7%a5%a8%e5%9d%87%e7%ba%bf%e7%b3%bb%e7%bb%9f%e5%ba%94%e7%94%a8%e3%80%81%e8%82%a1%e4%bb%b7%e6%ad%a3%e6%80%81%e5%88%86%e5%b8%83%e6%a3%80%e9%aa%8c%ef%bc%8c%e7%bb%98%e5%88%b6%e7%9b%b4","status":"publish","type":"post","link":"http:\/\/cnliutz.wicp.vip\/?p=5653","title":{"rendered":"python\u80a1\u7968\u80a1\u4ef7\u3001\u6b63\u6001\u5206\u5e03\u68c0\u9a8c\uff0c\u7ed8\u5236\u76f4\u65b9\u56fe\u548c\u6b63\u6001\u5206\u5e03\u66f2\u7ebf"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>#\u80a1\u4ef7\u79fb\u52a8\u5e73\u5747\u7ebf\u7cfb\u7edf\uff0c\u80a1\u4ef7\u6b63\u6001\u5206\u5e03\u68c0\u9a8c\uff0c\u7ed8\u5236\u76f4\u65b9\u56fe\u548c\u6b63\u6001\u5206\u5e03\u66f2\u7ebf\nimport akshare as ak\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport matplotlib.font_manager as fm\nfrom scipy import stats\n\n# \u8bbe\u7f6e\u5b57\u4f53\nfont_path = 'C:\/Windows\/Fonts\/simhei.ttf'  # \u9ed1\u4f53\u5b57\u4f53\u8def\u5f84\uff0c\u6839\u636e\u5b9e\u9645\u60c5\u51b5\u4fee\u6539\nmy_font = fm.FontProperties(fname=font_path)\nplt.rcParams&#91;'font.family'] = my_font.get_name()\n# \u89e3\u51b3\u8d1f\u53f7\u663e\u793a\u95ee\u9898\nplt.rcParams&#91;'axes.unicode_minus'] = False\n\n# Get Kweichow Moutai data\nstock_code = \"600938\"  # \u8d35\u5dde\u8305\u53f0\nstart_date = \"20241201\"\nend_date = \"20250523\"\n# ma_periods=&#91;5, 10, 20, 30]\ndf = ak.stock_zh_a_hist(symbol=stock_code, period=\"daily\", start_date=start_date, end_date=end_date, adjust=\"qfq\")\ndf&#91;'\u65e5\u671f'] = pd.to_datetime(df&#91;'\u65e5\u671f'])\ndf.set_index('\u65e5\u671f', inplace=True)\n# Check if data retrieval failed or the DataFrame is empty\nif df is None or df.empty:\n    print(\"Failed to retrieve data or data is empty\")\n    # Exit the program early if data retrieval fails\n    quit()\n\nprint(df.tail())\nprint(df.columns)\nprint(df&#91;'\u6536\u76d8'].describe())\n\n# Calculate moving averages\nwindows = &#91;5, 10, 20, 60, 120]\nfor w in windows:\n    df&#91;f'MA{w}'] = df&#91;'\u6536\u76d8'].rolling(window=w).mean()\n\ndf.fillna(method='ffill', inplace=True)\n\n# Create a canvas\nplt.figure(figsize=(12, 6))\n\n# Plot the price and moving averages\nplt.plot(df.index, df&#91;'\u6536\u76d8'], label='\u6536\u76d8\u4ef7', alpha=0.5)\nplt.plot(df.index, df&#91;'MA5'], label='5\u65e5\u5747\u7ebf', linestyle='--', linewidth=1)\nplt.plot(df.index, df&#91;'MA10'], label='10\u65e5\u5747\u7ebf', linestyle=':', linewidth=1.5)\nplt.plot(df.index, df&#91;'MA20'], label='20\u65e5\u5747\u7ebf', color='purple')\nplt.plot(df.index, df&#91;'MA60'], label='60\u65e5\u5747\u7ebf', color='orange')\nplt.plot(df.index, df&#91;'MA120'], label='120\u65e5\u5747\u7ebf', color='red')\n\n# Add chart elements\nplt.title(f'{stock_code} \u80a1\u7968\u4ef7\u683c\u79fb\u52a8\u5e73\u5747\u7ebf\u7cfb\u7edf')\nplt.xlabel('\u65e5\u671f')\nplt.ylabel('\u4ef7\u683c')\nplt.legend(loc='upper left')\nplt.grid(True, alpha=0.3)\n\n# Automatically rotate date labels\nplt.gcf().autofmt_xdate()\n\n# Display the chart\nplt.show()\n\n# \u68c0\u9a8c\u6536\u76d8\u4ef7\u662f\u5426\u6b63\u6001\u5206\u5e03\n_, p_value = stats.normaltest(df&#91;'\u6536\u76d8'])\nalpha = 0.05\nif p_value &lt; alpha:\n    print(\"\u6536\u76d8\u4ef7\u4e0d\u670d\u4ece\u6b63\u6001\u5206\u5e03\uff0c\u8fdb\u884c\u5bf9\u6570\u53d8\u6362\")\n    data = np.log(df&#91;'\u6536\u76d8'])\nelse:\n    print(\"\u6536\u76d8\u4ef7\u670d\u4ece\u6b63\u6001\u5206\u5e03\")\n    data = df&#91;'\u6536\u76d8']\n\n# \u7ed8\u5236\u76f4\u65b9\u56fe\u53e0\u52a0\u6b63\u6001\u5206\u5e03\u66f2\u7ebf\nplt.figure(figsize=(12, 6))\nn, bins, patches = plt.hist(data, bins=30, density=True, alpha=0.5, label='\u76f4\u65b9\u56fe')\n\n# \u62df\u5408\u6b63\u6001\u5206\u5e03\nmu, std = stats.norm.fit(data)\nxmin, xmax = plt.xlim()\nx = np.linspace(xmin, xmax, 100)\np = stats.norm.pdf(x, mu, std)\nplt.plot(x, p, 'k', linewidth=2, label='\u6b63\u6001\u5206\u5e03\u66f2\u7ebf')\n\n# Add chart elements\nplt.title(f'{stock_code} \u80a1\u7968\u4ef7\u683c{\"\u5bf9\u6570\" if p_value &lt; alpha else \"\"}\u6536\u76d8\u4ef7\u5206\u5e03')\nplt.xlabel(f'{\"\u5bf9\u6570\" if p_value &lt; alpha else \"\"}\u80a1\u4ef7')\nplt.ylabel('\u6982\u7387\u5bc6\u5ea6')\nplt.legend(loc='upper left')\nplt.grid(True, alpha=0.1)\n\n# Automatically rotate date labels\nplt.gcf().autofmt_xdate()\n\n# Display the chart\nplt.show()\n<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-gallery has-nested-images columns-default is-cropped wp-block-gallery-1 is-layout-flex wp-block-gallery-is-layout-flex\">\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1000\" height=\"400\" data-id=\"5654\" src=\"http:\/\/192.168.1.29\/wp-content\/uploads\/2025\/05\/Figure_1-1.png\" alt=\"\" class=\"wp-image-5654\" srcset=\"http:\/\/cnliutz.wicp.vip\/wp-content\/uploads\/2025\/05\/Figure_1-1.png 1000w, http:\/\/cnliutz.wicp.vip\/wp-content\/uploads\/2025\/05\/Figure_1-1-300x120.png 300w, http:\/\/cnliutz.wicp.vip\/wp-content\/uploads\/2025\/05\/Figure_1-1-768x307.png 768w\" sizes=\"auto, (max-width: 1000px) 100vw, 1000px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1000\" height=\"600\" data-id=\"5655\" src=\"http:\/\/192.168.1.29\/wp-content\/uploads\/2025\/05\/Figure_2-1.png\" alt=\"\" class=\"wp-image-5655\" srcset=\"http:\/\/cnliutz.wicp.vip\/wp-content\/uploads\/2025\/05\/Figure_2-1.png 1000w, http:\/\/cnliutz.wicp.vip\/wp-content\/uploads\/2025\/05\/Figure_2-1-300x180.png 300w, http:\/\/cnliutz.wicp.vip\/wp-content\/uploads\/2025\/05\/Figure_2-1-768x461.png 768w\" sizes=\"auto, (max-width: 1000px) 100vw, 1000px\" \/><\/figure>\n<\/figure>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,24],"tags":[],"class_list":["post-5653","post","type-post","status-publish","format-standard","hentry","category-2","category-24"],"_links":{"self":[{"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=\/wp\/v2\/posts\/5653","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5653"}],"version-history":[{"count":2,"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=\/wp\/v2\/posts\/5653\/revisions"}],"predecessor-version":[{"id":5657,"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=\/wp\/v2\/posts\/5653\/revisions\/5657"}],"wp:attachment":[{"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5653"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5653"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5653"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}