{"id":1129852,"date":"2025-01-08T20:33:14","date_gmt":"2025-01-08T12:33:14","guid":{"rendered":""},"modified":"2025-01-08T20:33:16","modified_gmt":"2025-01-08T12:33:16","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8python%e4%b8%ad%e8%b0%83%e7%94%a8%e4%bc%98%e7%9f%bf%e4%b8%ad%e7%9a%84%e6%95%b0%e6%8d%ae","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1129852.html","title":{"rendered":"\u5982\u4f55\u5728python\u4e2d\u8c03\u7528\u4f18\u77ff\u4e2d\u7684\u6570\u636e"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25100129\/3e42f1b8-bf78-4e23-a889-0c5f6034d4ad.webp\" alt=\"\u5982\u4f55\u5728python\u4e2d\u8c03\u7528\u4f18\u77ff\u4e2d\u7684\u6570\u636e\" \/><\/p>\n<p><p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8c03\u7528\u4f18\u77ff\u4e2d\u7684\u6570\u636e<\/strong><\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\u8c03\u7528\u4f18\u77ff\uff08Uqer\uff09\u4e2d\u7684\u6570\u636e\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff1a<strong>\u5b89\u88c5\u4f18\u77ffSDK\u3001\u767b\u5f55\u4f18\u77ff\u8d26\u6237\u3001\u4f7f\u7528API\u83b7\u53d6\u6570\u636e\u3001\u5904\u7406\u548c\u5206\u6790\u6570\u636e<\/strong>\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u6bcf\u4e00\u4e2a\u6b65\u9aa4\uff0c\u5e2e\u52a9\u4f60\u8f7b\u677e\u5730\u5728Python\u4e2d\u8c03\u7528\u4f18\u77ff\u6570\u636e\uff0c\u5c24\u5176\u662f<strong>\u5b89\u88c5\u4f18\u77ffSDK<\/strong>\u8fd9\u4e00\u5173\u952e\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b89\u88c5\u4f18\u77ffSDK<\/h3>\n<\/p>\n<p><p>\u4f18\u77ff\u63d0\u4f9b\u4e86Python SDK\uff0c\u65b9\u4fbf\u7528\u6237\u76f4\u63a5\u5728Python\u4e2d\u8c03\u7528\u548c\u5904\u7406\u6570\u636e\u3002\u9996\u5148\u9700\u8981\u5b89\u88c5\u4f18\u77ff\u7684SDK\u3002\u4f7f\u7528pip\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install uqer<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u4ee3\u7801\u6d4b\u8bd5\u662f\u5426\u5b89\u88c5\u6210\u529f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import uqer<\/p>\n<p>print(uqer.__version__)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6210\u529f\u5b89\u88c5SDK\u540e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u901a\u8fc7SDK\u8fdb\u884c\u6570\u636e\u7684\u8c03\u7528\u548c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u767b\u5f55\u4f18\u77ff\u8d26\u6237<\/h3>\n<\/p>\n<p><p>\u5728\u4f7f\u7528\u4f18\u77ff\u6570\u636e\u4e4b\u524d\uff0c\u9700\u8981\u5148\u767b\u5f55\u4f18\u77ff\u8d26\u6237\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7SDK\u4e2d\u7684<code>Client<\/code>\u7c7b\u6765\u5b9e\u73b0\u3002\u4ee5\u4e0b\u662f\u5177\u4f53\u7684\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from uqer import Client<\/p>\n<h2><strong>\u8bf7\u8f93\u5165\u60a8\u7684\u4f18\u77ff\u7528\u6237\u540d\u548c\u5bc6\u7801<\/strong><\/h2>\n<p>username = &#39;your_username&#39;<\/p>\n<p>password = &#39;your_password&#39;<\/p>\n<p>client = Client(username, password)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u767b\u5f55\u6210\u529f\u540e\uff0c<code>client<\/code>\u5bf9\u8c61\u5c06\u4fdd\u5b58\u7528\u6237\u7684\u4f1a\u8bdd\u4fe1\u606f\uff0c\u540e\u7eed\u7684\u6240\u6709\u6570\u636e\u8bf7\u6c42\u90fd\u53ef\u4ee5\u901a\u8fc7\u8fd9\u4e2a\u5bf9\u8c61\u8fdb\u884c\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528API\u83b7\u53d6\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u4f18\u77ff\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684API\u63a5\u53e3\uff0c\u65b9\u4fbf\u7528\u6237\u83b7\u53d6\u5404\u79cd\u91d1\u878d\u6570\u636e\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u83b7\u53d6\u80a1\u7968\u7684\u57fa\u672c\u4fe1\u606f\u3001\u884c\u60c5\u6570\u636e\u3001\u8d22\u52a1\u6570\u636e\u7b49\u3002\u4ee5\u4e0b\u662f\u51e0\u4e2a\u5e38\u7528\u7684API\u8c03\u7528\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><h4>1. \u83b7\u53d6\u80a1\u7968\u57fa\u672c\u4fe1\u606f<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">from uqer import DataAPI<\/p>\n<h2><strong>\u83b7\u53d6\u6240\u6709\u80a1\u7968\u7684\u57fa\u672c\u4fe1\u606f<\/strong><\/h2>\n<p>stocks = DataAPI.EquGet(equTypeCD=&#39;A&#39;, field=&#39;secID,ticker,secShortName,exchangeCD&#39;)<\/p>\n<p>print(stocks.head())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u83b7\u53d6\u884c\u60c5\u6570\u636e<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u83b7\u53d6\u67d0\u53ea\u80a1\u7968\u7684\u5386\u53f2\u884c\u60c5\u6570\u636e<\/p>\n<p>ticker = &#39;000001&#39;<\/p>\n<p>start_date = &#39;2022-01-01&#39;<\/p>\n<p>end_date = &#39;2022-12-31&#39;<\/p>\n<p>history_data = DataAPI.MktEqudGet(ticker=ticker, beginDate=start_date, endDate=end_date, field=&#39;tradeDate,ticker,closePrice,turnoverVol&#39;)<\/p>\n<p>print(history_data.head())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u83b7\u53d6\u8d22\u52a1\u6570\u636e<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u83b7\u53d6\u67d0\u53ea\u80a1\u7968\u7684\u8d22\u52a1\u6570\u636e<\/p>\n<p>ticker = &#39;000001&#39;<\/p>\n<p>fiscal_year = 2021<\/p>\n<p>financial_data = DataAPI.FdmtBSGet(ticker=ticker, endDate=fiscal_year, field=&#39;ticker,publishDate,totAssets,totLiab,equity&#39;)<\/p>\n<p>print(financial_data.head())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5904\u7406\u548c\u5206\u6790\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u5728\u83b7\u53d6\u5230\u6240\u9700\u7684\u6570\u636e\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528Pandas\u7b49Python\u5e93\u5bf9\u6570\u636e\u8fdb\u884c\u5904\u7406\u548c\u5206\u6790\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5bf9\u6570\u636e\u8fdb\u884c\u6e05\u6d17\u3001\u53d8\u6362\u3001\u53ef\u89c6\u5316\u7b49\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h4>1. \u6570\u636e\u6e05\u6d17<\/h4>\n<\/p>\n<p><p>\u6570\u636e\u6e05\u6d17\u662f\u6570\u636e\u5206\u6790\u7684\u91cd\u8981\u4e00\u6b65\uff0c\u901a\u5e38\u5305\u62ec\u5904\u7406\u7f3a\u5931\u503c\u3001\u53bb\u91cd\u3001\u683c\u5f0f\u8f6c\u6362\u7b49\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u53bb\u9664\u7f3a\u5931\u503c<\/strong><\/h2>\n<p>cleaned_data = history_data.dropna()<\/p>\n<h2><strong>\u53bb\u9664\u91cd\u590d\u6570\u636e<\/strong><\/h2>\n<p>cleaned_data = cleaned_data.drop_duplicates()<\/p>\n<h2><strong>\u683c\u5f0f\u8f6c\u6362<\/strong><\/h2>\n<p>cleaned_data[&#39;tradeDate&#39;] = pd.to_datetime(cleaned_data[&#39;tradeDate&#39;])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u6570\u636e\u53d8\u6362<\/h4>\n<\/p>\n<p><p>\u6570\u636e\u53d8\u6362\u5305\u62ec\u5bf9\u6570\u636e\u8fdb\u884c\u805a\u5408\u3001\u5206\u7ec4\u3001\u8ba1\u7b97\u884d\u751f\u53d8\u91cf\u7b49\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8ba1\u7b97\u65e5\u6536\u76ca\u7387<\/p>\n<p>cleaned_data[&#39;return&#39;] = cleaned_data[&#39;closePrice&#39;].pct_change()<\/p>\n<h2><strong>\u6309\u6708\u805a\u5408\u6570\u636e<\/strong><\/h2>\n<p>monthly_data = cleaned_data.resample(&#39;M&#39;, on=&#39;tradeDate&#39;).mean()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u6570\u636e\u53ef\u89c6\u5316<\/h4>\n<\/p>\n<p><p>\u6570\u636e\u53ef\u89c6\u5316\u662f\u6570\u636e\u5206\u6790\u7684\u91cd\u8981\u624b\u6bb5\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u76f4\u89c2\u5730\u4e86\u89e3\u6570\u636e\u7684\u5206\u5e03\u548c\u8d8b\u52bf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u7ed8\u5236\u6536\u76d8\u4ef7\u8d70\u52bf<\/strong><\/h2>\n<p>plt.figure(figsize=(10, 6))<\/p>\n<p>plt.plot(cleaned_data[&#39;tradeDate&#39;], cleaned_data[&#39;closePrice&#39;], label=&#39;Close Price&#39;)<\/p>\n<p>plt.xlabel(&#39;Date&#39;)<\/p>\n<p>plt.ylabel(&#39;Close Price&#39;)<\/p>\n<p>plt.title(&#39;Stock Close Price Over Time&#39;)<\/p>\n<p>plt.legend()<\/p>\n<p>plt.show()<\/p>\n<h2><strong>\u7ed8\u5236\u65e5\u6536\u76ca\u7387\u5206\u5e03<\/strong><\/h2>\n<p>plt.figure(figsize=(10, 6))<\/p>\n<p>plt.hist(cleaned_data[&#39;return&#39;].dropna(), bins=50, edgecolor=&#39;k&#39;, alpha=0.7)<\/p>\n<p>plt.xlabel(&#39;D<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>ly Return&#39;)<\/p>\n<p>plt.ylabel(&#39;Frequency&#39;)<\/p>\n<p>plt.title(&#39;Distribution of Daily Returns&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u5982\u4f55\u5728Python\u4e2d\u8c03\u7528\u4f18\u77ff\u4e2d\u7684\u6570\u636e\u3002\u901a\u8fc7\u5b89\u88c5\u4f18\u77ffSDK\u3001\u767b\u5f55\u4f18\u77ff\u8d26\u6237\u3001\u4f7f\u7528API\u83b7\u53d6\u6570\u636e\u3001\u5904\u7406\u548c\u5206\u6790\u6570\u636e\u7b49\u6b65\u9aa4\uff0c\u7528\u6237\u53ef\u4ee5\u65b9\u4fbf\u5730\u83b7\u53d6\u548c\u5904\u7406\u4f18\u77ff\u63d0\u4f9b\u7684\u4e30\u5bcc\u91d1\u878d\u6570\u636e\u3002\u5e0c\u671b\u672c\u6587\u80fd\u591f\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u4f7f\u7528\u4f18\u77ff\u7684\u6570\u636e\u8fdb\u884c\u91d1\u878d\u5206\u6790\u548c\u7814\u7a76\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u83b7\u53d6\u4f18\u77ff\u7684\u6570\u636e\uff1f<\/strong><br \/>\u8981\u5728Python\u4e2d\u83b7\u53d6\u4f18\u77ff\u7684\u6570\u636e\uff0c\u9996\u5148\u9700\u8981\u6ce8\u518c\u5e76\u83b7\u53d6\u4f18\u77ff\u7684API\u5bc6\u94a5\u3002\u5b89\u88c5\u4f18\u77ff\u7684Python SDK\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528\u76f8\u5173\u7684API\u63a5\u53e3\u8fdb\u884c\u6570\u636e\u8c03\u7528\u3002\u901a\u8fc7\u7f16\u5199Python\u811a\u672c\uff0c\u5229\u7528SDK\u63d0\u4f9b\u7684\u51fd\u6570\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u83b7\u53d6\u80a1\u7968\u884c\u60c5\u3001\u8d22\u52a1\u6570\u636e\u7b49\u4fe1\u606f\u3002<\/p>\n<p><strong>\u4f18\u77ffAPI\u7684\u4f7f\u7528\u662f\u5426\u9700\u8981\u4efb\u4f55\u8d39\u7528\uff1f<\/strong><br \/>\u4f18\u77ff\u7684API\u63a5\u53e3\u5927\u90e8\u5206\u529f\u80fd\u662f\u514d\u8d39\u7684\uff0c\u4f46\u4e5f\u63d0\u4f9b\u4e86\u4e00\u4e9b\u9ad8\u7ea7\u529f\u80fd\u548c\u6570\u636e\u670d\u52a1\uff0c\u8fd9\u4e9b\u901a\u5e38\u9700\u8981\u4ed8\u8d39\u8ba2\u9605\u3002\u5efa\u8bae\u5728\u4f7f\u7528\u4e4b\u524d\u67e5\u770b\u4f18\u77ff\u5b98\u7f51\u7684\u5177\u4f53\u8bf4\u660e\uff0c\u4e86\u89e3\u4e0d\u540c\u529f\u80fd\u7684\u8d39\u7528\u7ed3\u6784\uff0c\u4ee5\u4fbf\u9009\u62e9\u6700\u9002\u5408\u81ea\u5df1\u7684\u670d\u52a1\u3002<\/p>\n<p><strong>\u5728\u8c03\u7528\u4f18\u77ff\u6570\u636e\u65f6\uff0c\u5982\u4f55\u5904\u7406\u6570\u636e\u683c\u5f0f\u95ee\u9898\uff1f<\/strong><br \/>\u4f18\u77ff\u8fd4\u56de\u7684\u6570\u636e\u901a\u5e38\u4e3aJSON\u683c\u5f0f\uff0c\u4f7f\u7528Python\u7684\u5185\u7f6e<code>json<\/code>\u5e93\u53ef\u4ee5\u8f7b\u677e\u89e3\u6790\u3002\u786e\u4fdd\u5728\u5904\u7406\u6570\u636e\u65f6\uff0c\u5173\u6ce8\u6570\u636e\u7ed3\u6784\u548c\u5b57\u6bb5\u540d\u79f0\uff0c\u4ee5\u4fbf\u6b63\u786e\u63d0\u53d6\u6240\u9700\u4fe1\u606f\u3002\u53ef\u4ee5\u5c06\u6570\u636e\u8f6c\u6362\u4e3aPandas DataFrame\u683c\u5f0f\uff0c\u65b9\u4fbf\u540e\u7eed\u7684\u6570\u636e\u5206\u6790\u548c\u5904\u7406\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u5728Python\u4e2d\u8fdb\u884c\u5b9e\u65f6\u6570\u636e\u76d1\u63a7\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u4f18\u77ffAPI\u652f\u6301\u5b9e\u65f6\u6570\u636e\u67e5\u8be2\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u5b9a\u65f6\u8bf7\u6c42\u6570\u636e\uff0c\u7ed3\u5408Python\u7684\u65f6\u95f4\u63a7\u5236\u6a21\u5757\uff0c\u5b9e\u65f6\u76d1\u63a7\u7279\u5b9a\u80a1\u7968\u7684\u4ef7\u683c\u53d8\u5316\u548c\u4ea4\u6613\u60c5\u51b5\u3002\u786e\u4fdd\u9075\u5faaAPI\u8c03\u7528\u9891\u7387\u9650\u5236\uff0c\u4ee5\u514d\u9020\u6210\u8d26\u53f7\u88ab\u5c01\u7981\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5982\u4f55\u5728Python\u4e2d\u8c03\u7528\u4f18\u77ff\u4e2d\u7684\u6570\u636e \u5728Python\u4e2d\u8c03\u7528\u4f18\u77ff\uff08Uqer\uff09\u4e2d\u7684\u6570\u636e\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff1a\u5b89 [&hellip;]","protected":false},"author":3,"featured_media":1129857,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1129852"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1129852"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1129852\/revisions"}],"predecessor-version":[{"id":1129861,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1129852\/revisions\/1129861"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1129857"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1129852"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1129852"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1129852"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}