{"id":5720,"date":"2025-06-03T16:11:41","date_gmt":"2025-06-03T08:11:41","guid":{"rendered":"http:\/\/192.168.1.29\/?p=5720"},"modified":"2025-06-03T16:11:42","modified_gmt":"2025-06-03T08:11:42","slug":"python%e8%ae%a1%e7%ae%97%e8%82%a1%e7%a5%a8%e5%b9%b4%e5%8c%96%e6%94%b6%e7%9b%8a%e7%8e%87%e3%80%81%e5%b9%b4%e5%8c%96%e6%b3%a2%e5%8a%a8%e7%8e%87%e3%80%81%e5%a4%8f%e6%99%ae%e6%af%94%e7%8e%87%e7%b3%bb","status":"publish","type":"post","link":"http:\/\/cnliutz.wicp.vip\/?p=5720","title":{"rendered":"python\u8ba1\u7b97\u80a1\u7968\u5e74\u5316\u6536\u76ca\u7387\u3001\u5e74\u5316\u6ce2\u52a8\u7387\u3001\u590f\u666e\u6bd4\u7387\u7cfb\u6570(>1,good)\u5e76\u7ed8\u56fe"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">python\u4ee3\u7801\uff0c\u7531vscode trae\u63d2\u4ef6\u751f\u6210&#8211;\u65e0\u9700\u4efb\u4f55\u8c03\u6574\uff0c\u5373\u53ef\u8fd0\u884c<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>import akshare as ak\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\n\ndef get_stock_data(stock_code, start_date, end_date):\n    \"\"\"\n    \u83b7\u53d6\u6307\u5b9a\u80a1\u7968\u7684\u5386\u53f2\u6570\u636e\n\n    :param stock_code: \u80a1\u7968\u4ee3\u7801\uff0c\u4f8b\u5982 'sh600519'\n    :param start_date: \u5f00\u59cb\u65e5\u671f\uff0c\u683c\u5f0f 'YYYYMMDD'\n    :param end_date: \u7ed3\u675f\u65e5\u671f\uff0c\u683c\u5f0f 'YYYYMMDD'\n    :return: \u5305\u542b\u5386\u53f2\u6570\u636e\u7684 DataFrame\n    \"\"\"\n    stock_df = ak.stock_zh_a_hist(symbol=stock_code&#91;2:], period=\"daily\", start_date=start_date, end_date=end_date)\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\ndef calculate_risk_metrics(stock_df):\n    \"\"\"\n    \u8ba1\u7b97\u80a1\u7968\u98ce\u9669\u6307\u6807\n\n    :param stock_df: \u5305\u542b\u5386\u53f2\u6570\u636e\u7684 DataFrame\n    :return: \u5305\u542b\u98ce\u9669\u6307\u6807\u7684\u5b57\u5178\u548c\u5305\u542b\u65e5\u6536\u76ca\u7387\u7684 DataFrame\n    \"\"\"\n    # \u8ba1\u7b97\u65e5\u6536\u76ca\u7387\n    stock_df&#91;'\u65e5\u6536\u76ca\u7387'] = stock_df&#91;'\u6536\u76d8'].pct_change()\n    # \u8ba1\u7b97\u5e74\u5316\u6536\u76ca\u7387\n    annual_return = stock_df&#91;'\u65e5\u6536\u76ca\u7387'].mean() * 252\n    # \u8ba1\u7b97\u5e74\u5316\u6ce2\u52a8\u7387\uff08\u6536\u76ca\u7387\u6807\u51c6\u5dee\uff09\n    annual_volatility = stock_df&#91;'\u65e5\u6536\u76ca\u7387'].std() * np.sqrt(252)\n    # \u5047\u8bbe\u65e0\u98ce\u9669\u5229\u7387\u4e3a 2%\n    risk_free_rate = 0.02\n    # \u8ba1\u7b97\u590f\u666e\u6bd4\u7387\n    sharpe_ratio = (annual_return - risk_free_rate) \/ annual_volatility\n\n    return {\n        '\u5e74\u5316\u6536\u76ca\u7387': annual_return,\n        '\u5e74\u5316\u6ce2\u52a8\u7387': annual_volatility,\n        '\u590f\u666e\u6bd4\u7387': sharpe_ratio\n    }, stock_df\n\ndef plot_stock_data(stock_df, stock_code):\n    \"\"\"\n    \u7ed8\u5236\u80a1\u4ef7\u53d8\u5316\u548c\u65e5\u6536\u76ca\u7387\u53d8\u5316\u56fe\n\n    :param stock_df: \u5305\u542b\u5386\u53f2\u6570\u636e\u548c\u65e5\u6536\u76ca\u7387\u7684 DataFrame\n    :param stock_code: \u80a1\u7968\u4ee3\u7801\n    \"\"\"\n    # \u8bbe\u7f6e\u56fe\u7247\u6e05\u6670\u5ea6\n    plt.rcParams&#91;'figure.dpi'] = 300\n    # \u8bbe\u7f6e\u4e2d\u6587\u5b57\u4f53\n    plt.rcParams&#91;'font.sans-serif'] = &#91;'SimHei']\n    plt.rcParams&#91;'axes.unicode_minus'] = False\n\n    # \u521b\u5efa\u4e00\u4e2a\u5305\u542b\u4e24\u4e2a\u5b50\u56fe\u7684\u753b\u5e03\n    fig, axes = plt.subplots(2, 1, figsize=(12, 8))\n\n    # \u7ed8\u5236\u80a1\u4ef7\u53d8\u5316\u56fe\n    axes&#91;0].plot(stock_df&#91;'\u6536\u76d8'], label='\u6536\u76d8\u4ef7')\n    axes&#91;0].set_title(f'{stock_code} \u80a1\u4ef7\u53d8\u5316')\n    axes&#91;0].set_ylabel('\u4ef7\u683c')\n    axes&#91;0].legend()\n\n    # \u7ed8\u5236\u65e5\u6536\u76ca\u7387\u53d8\u5316\u56fe\n    axes&#91;1].plot(stock_df&#91;'\u65e5\u6536\u76ca\u7387'], label='\u65e5\u6536\u76ca\u7387', color='orange')\n    axes&#91;1].set_title(f'{stock_code} \u65e5\u6536\u76ca\u7387\u53d8\u5316')\n    axes&#91;1].set_xlabel('\u65e5\u671f')\n    axes&#91;1].set_ylabel('\u6536\u76ca\u7387')\n    axes&#91;1].legend()\n\n    # \u624b\u52a8\u8c03\u6574\u5b50\u56fe\u4e4b\u95f4\u7684\u5782\u76f4\u95f4\u8ddd\n    plt.subplots_adjust(hspace=0.5)\n    plt.show()\n\nif __name__ == \"__main__\":\n    stock_code = 'sh600938'  # \u519c\u4e1a\u94f6\u884c\n    start_date = '20240101'\n    end_date = '20250531'\n\n    # \u83b7\u53d6\u80a1\u7968\u6570\u636e\n    stock_data = get_stock_data(stock_code, start_date, end_date)\n\n    # \u8ba1\u7b97\u98ce\u9669\u6307\u6807\n    risk_metrics, stock_data = calculate_risk_metrics(stock_data)\n\n    print(f\"\u80a1\u7968\u4ee3\u7801: {stock_code}\")\n    print(f\"\u5f00\u59cb\u65e5\u671f: {start_date}\")\n    print(f\"\u7ed3\u675f\u65e5\u671f: {end_date}\")\n    print(\"\u98ce\u9669\u6307\u6807:\")\n    for key, value in risk_metrics.items():\n        print(f\"{key}: {value:.4f}\")\n\n    # \u7ed8\u5236\u56fe\u8868\n    plot_stock_data(stock_data, stock_code)<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u8ba1\u7b97\u7ed3\u679c\uff1a\u519c\u884c\u3001\u4e2d\u56fd\u6d77\u6cb9 <\/h2>\n\n\n\n<p>\u80a1\u7968\u4ee3\u7801: sh600938 \u4e2d\u56fd\u6d77\u6cb9<br>\u5f00\u59cb\u65e5\u671f: 20240101<br>\u7ed3\u675f\u65e5\u671f: 20250531<br>\u98ce\u9669\u6307\u6807:<br>\u5e74\u5316\u6536\u76ca\u7387: 0.2109<br>\u5e74\u5316\u6ce2\u52a8\u7387: 0.3317 <strong>  \u6ce2\u52a8\u5927\u3001\u5f71\u54cd\u6536\u76ca<\/strong><br>\u590f\u666e\u6bd4\u7387: 0.5756<\/p>\n\n\n\n<p>\u80a1\u7968\u4ee3\u7801: sh601288 \u519c\u4e1a\u94f6\u884c<br>\u5f00\u59cb\u65e5\u671f: 20240101<br>\u7ed3\u675f\u65e5\u671f: 20250531<br>\u98ce\u9669\u6307\u6807:<br>\u5e74\u5316\u6536\u76ca\u7387: 0.3293<br>\u5e74\u5316\u6ce2\u52a8\u7387: 0.2067   <strong> \u6ce2\u52a8\u5c0f\u6536\u76ca\u9ad8<\/strong><br><strong>\u590f\u666e\u6bd4\u7387: 1.4961<\/strong>    \u6536\u76ca\u9ad8<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>python\u4ee3\u7801\uff0c\u7531vscode trae\u63d2\u4ef6\u751f\u6210&#8211;\u65e0\u9700\u4efb\u4f55\u8c03\u6574\uff0c\u5373 <span class=\"readmore\"><a href=\"http:\/\/cnliutz.wicp.vip\/?p=5720\">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":[2,24],"tags":[],"class_list":["post-5720","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\/5720","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=5720"}],"version-history":[{"count":1,"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=\/wp\/v2\/posts\/5720\/revisions"}],"predecessor-version":[{"id":5721,"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=\/wp\/v2\/posts\/5720\/revisions\/5721"}],"wp:attachment":[{"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5720"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5720"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5720"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}