{"id":1041627,"date":"2024-12-31T12:50:43","date_gmt":"2024-12-31T04:50:43","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1041627.html"},"modified":"2024-12-31T12:50:45","modified_gmt":"2024-12-31T04:50:45","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e8%ae%a1%e7%ae%97%e8%82%a1%e7%a5%a8%e6%af%8f%e6%ac%a1%e8%bf%9e%e6%b6%a8%e8%b7%8c","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1041627.html","title":{"rendered":"\u5982\u4f55\u7528python\u8ba1\u7b97\u80a1\u7968\u6bcf\u6b21\u8fde\u6da8\u8dcc"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/78a4c6fa-5725-4542-9b56-d6c09902ed4f.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"\u5982\u4f55\u7528python\u8ba1\u7b97\u80a1\u7968\u6bcf\u6b21\u8fde\u6da8\u8dcc\" \/><\/p>\n<p><p> <strong>\u5982\u4f55\u7528Python\u8ba1\u7b97\u80a1\u7968\u6bcf\u6b21\u8fde\u6da8\u8dcc<\/strong><\/p>\n<\/p>\n<p><p>\u8981\u7528Python\u8ba1\u7b97\u80a1\u7968\u6bcf\u6b21\u8fde\u6da8\u8dcc\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u5b9e\u73b0\uff1a<strong>\u83b7\u53d6\u80a1\u7968\u6570\u636e\u3001\u8ba1\u7b97\u6bcf\u65e5\u6da8\u8dcc\u5e45\u3001\u8bc6\u522b\u8fde\u7eed\u6da8\u8dcc\u3001\u7edf\u8ba1\u8fde\u6da8\u8dcc\u6b21\u6570<\/strong>\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u63cf\u8ff0\u5176\u4e2d\u4e00\u4e2a\u6b65\u9aa4\uff1a\u83b7\u53d6\u80a1\u7968\u6570\u636e\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Python\u7684<code>pandas<\/code>\u548c<code>yfinance<\/code>\u5e93\u4eceYahoo Finance\u83b7\u53d6\u80a1\u7968\u5386\u53f2\u6570\u636e\u3002\u7136\u540e\u901a\u8fc7\u8ba1\u7b97\u6bcf\u4e2a\u4ea4\u6613\u65e5\u7684\u6da8\u8dcc\u5e45\uff0c\u8bc6\u522b\u51fa\u8fde\u7eed\u6da8\u8dcc\u7684\u533a\u95f4\u5e76\u8bb0\u5f55\u6bcf\u6b21\u8fde\u6da8\u8dcc\u7684\u5929\u6570\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u83b7\u53d6\u80a1\u7968\u6570\u636e<\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u83b7\u53d6\u80a1\u7968\u7684\u5386\u53f2\u6570\u636e\u3002\u53ef\u4ee5\u4f7f\u7528<code>yfinance<\/code>\u5e93\u4eceYahoo Finance\u83b7\u53d6\u6570\u636e\u3002<code>yfinance<\/code>\u662f\u4e00\u4e2aPython\u5e93\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u83b7\u53d6\u91d1\u878d\u6570\u636e\uff0c\u5c24\u5176\u662f\u80a1\u7968\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import yfinance as yf<\/p>\n<h2><strong>\u5b9a\u4e49\u80a1\u7968\u4ee3\u7801\u548c\u65f6\u95f4\u8303\u56f4<\/strong><\/h2>\n<p>stock_code = &#39;AAPL&#39;<\/p>\n<p>start_date = &#39;2020-01-01&#39;<\/p>\n<p>end_date = &#39;2023-01-01&#39;<\/p>\n<h2><strong>\u4e0b\u8f7d\u80a1\u7968\u6570\u636e<\/strong><\/h2>\n<p>data = yf.download(stock_code, start=start_date, end=end_date)<\/p>\n<p>print(data.head())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u5c06\u4e0b\u8f7d\u82f9\u679c\u516c\u53f8\uff08AAPL\uff09\u4ece2020\u5e741\u67081\u65e5\u52302023\u5e741\u67081\u65e5\u7684\u80a1\u7968\u6570\u636e\uff0c\u5e76\u8f93\u51fa\u524d\u51e0\u884c\u6570\u636e\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u8ba1\u7b97\u6bcf\u65e5\u6da8\u8dcc\u5e45<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u9700\u8981\u8ba1\u7b97\u6bcf\u65e5\u7684\u6da8\u8dcc\u5e45\u3002\u53ef\u4ee5\u4f7f\u7528<code>pandas<\/code>\u5e93\u6765\u8ba1\u7b97\u6bcf\u65e5\u7684\u6da8\u8dcc\u5e45\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u8ba1\u7b97\u6bcf\u65e5\u6da8\u8dcc\u5e45<\/strong><\/h2>\n<p>data[&#39;D<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>ly Change&#39;] = data[&#39;Close&#39;].pct_change()<\/p>\n<p>print(data.head())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>pct_change()<\/code>\u51fd\u6570\u8ba1\u7b97\u6bcf\u4e2a\u4ea4\u6613\u65e5\u7684\u767e\u5206\u6bd4\u53d8\u5316\uff0c\u751f\u6210\u65b0\u5217&#39;Daily Change&#39;\uff0c\u8868\u793a\u6bcf\u65e5\u7684\u6da8\u8dcc\u5e45\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u8bc6\u522b\u8fde\u7eed\u6da8\u8dcc<\/p>\n<\/p>\n<p><p>\u8bc6\u522b\u8fde\u7eed\u6da8\u8dcc\u9700\u8981\u901a\u8fc7\u904d\u5386\u6bcf\u65e5\u6da8\u8dcc\u5e45\u6570\u636e\u6765\u5b9e\u73b0\u3002\u5982\u679c\u5f53\u524d\u65e5\u7684\u6da8\u8dcc\u5e45\u4e0e\u524d\u4e00\u65e5\u76f8\u540c\u65b9\u5411\uff08\u6b63\u6216\u8d1f\uff09\uff0c\u5219\u8ba4\u4e3a\u662f\u8fde\u7eed\u6da8\u8dcc\uff1b\u5426\u5219\uff0c\u8ba4\u4e3a\u662f\u65b0\u7684\u6da8\u8dcc\u5468\u671f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data[&#39;Trend&#39;] = data[&#39;Daily Change&#39;].apply(lambda x: &#39;up&#39; if x &gt; 0 else (&#39;down&#39; if x &lt; 0 else &#39;flat&#39;))<\/p>\n<h2><strong>\u521d\u59cb\u5316\u53d8\u91cf<\/strong><\/h2>\n<p>previous_trend = None<\/p>\n<p>streak_count = 0<\/p>\n<p>streaks = []<\/p>\n<h2><strong>\u904d\u5386\u6bcf\u65e5\u6570\u636e<\/strong><\/h2>\n<p>for index, row in data.iterrows():<\/p>\n<p>    current_trend = row[&#39;Trend&#39;]<\/p>\n<p>    if current_trend == previous_trend:<\/p>\n<p>        streak_count += 1<\/p>\n<p>    else:<\/p>\n<p>        if previous_trend is not None:<\/p>\n<p>            streaks.append((previous_trend, streak_count))<\/p>\n<p>        streak_count = 1<\/p>\n<p>        previous_trend = current_trend<\/p>\n<h2><strong>\u6dfb\u52a0\u6700\u540e\u4e00\u4e2a\u8d8b\u52bf<\/strong><\/h2>\n<p>if previous_trend is not None:<\/p>\n<p>    streaks.append((previous_trend, streak_count))<\/p>\n<p>print(streaks)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6b64\u4ee3\u7801\u904d\u5386\u6bcf\u65e5\u7684\u6da8\u8dcc\u5e45\u6570\u636e\uff0c\u8bc6\u522b\u51fa\u8fde\u7eed\u6da8\u8dcc\u7684\u5929\u6570\uff0c\u5e76\u8bb0\u5f55\u5230\u5217\u8868<code>streaks<\/code>\u4e2d\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u7edf\u8ba1\u8fde\u6da8\u8dcc\u6b21\u6570<\/p>\n<\/p>\n<p><p>\u6700\u540e\uff0c\u53ef\u4ee5\u7edf\u8ba1\u6bcf\u79cd\u8fde\u6da8\u8dcc\u7684\u6b21\u6570\u548c\u6700\u5927\u8fde\u6da8\u8dcc\u5929\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import Counter<\/p>\n<h2><strong>\u7edf\u8ba1\u8fde\u6da8\u8dcc\u6b21\u6570<\/strong><\/h2>\n<p>streak_counter = Counter(streaks)<\/p>\n<h2><strong>\u8ba1\u7b97\u6700\u5927\u8fde\u6da8\u8dcc\u5929\u6570<\/strong><\/h2>\n<p>max_up_streak = max([streak[1] for streak in streaks if streak[0] == &#39;up&#39;], default=0)<\/p>\n<p>max_down_streak = max([streak[1] for streak in streaks if streak[0] == &#39;down&#39;], default=0)<\/p>\n<p>print(&#39;\u8fde\u6da8\u6b21\u6570:&#39;, streak_counter[(&#39;up&#39;, max_up_streak)])<\/p>\n<p>print(&#39;\u6700\u5927\u8fde\u6da8\u5929\u6570:&#39;, max_up_streak)<\/p>\n<p>print(&#39;\u8fde\u8dcc\u6b21\u6570:&#39;, streak_counter[(&#39;down&#39;, max_down_streak)])<\/p>\n<p>print(&#39;\u6700\u5927\u8fde\u8dcc\u5929\u6570:&#39;, max_down_streak)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6b64\u4ee3\u7801\u4f7f\u7528<code>Counter<\/code>\u7c7b\u7edf\u8ba1\u8fde\u6da8\u8dcc\u7684\u6b21\u6570\uff0c\u5e76\u8ba1\u7b97\u6700\u5927\u8fde\u6da8\u548c\u8fde\u8dcc\u7684\u5929\u6570\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u53ef\u89c6\u5316\u7ed3\u679c<\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u66f4\u76f4\u89c2\u5730\u5c55\u793a\u7ed3\u679c\uff0c\u53ef\u4ee5\u4f7f\u7528<code>matplotlib<\/code>\u5e93\u5bf9\u7ed3\u679c\u8fdb\u884c\u53ef\u89c6\u5316\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u7ed8\u5236\u80a1\u7968\u6536\u76d8\u4ef7<\/strong><\/h2>\n<p>plt.figure(figsize=(14, 7))<\/p>\n<p>plt.plot(data.index, data[&#39;Close&#39;], label=&#39;Close Price&#39;)<\/p>\n<p>plt.title(&#39;Stock Close Price&#39;)<\/p>\n<p>plt.xlabel(&#39;Date&#39;)<\/p>\n<p>plt.ylabel(&#39;Close Price&#39;)<\/p>\n<p>plt.legend()<\/p>\n<p>plt.show()<\/p>\n<h2><strong>\u7ed8\u5236\u6bcf\u65e5\u6da8\u8dcc\u5e45<\/strong><\/h2>\n<p>plt.figure(figsize=(14, 7))<\/p>\n<p>plt.plot(data.index, data[&#39;Daily Change&#39;], label=&#39;Daily Change&#39;, color=&#39;orange&#39;)<\/p>\n<p>plt.title(&#39;Daily Change&#39;)<\/p>\n<p>plt.xlabel(&#39;Date&#39;)<\/p>\n<p>plt.ylabel(&#39;Percentage Change&#39;)<\/p>\n<p>plt.legend()<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6b64\u4ee3\u7801\u4f7f\u7528<code>matplotlib<\/code>\u5e93\u7ed8\u5236\u80a1\u7968\u6536\u76d8\u4ef7\u548c\u6bcf\u65e5\u6da8\u8dcc\u5e45\u7684\u56fe\u8868\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u6b65\u9aa4\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u7528Python\u8ba1\u7b97\u80a1\u7968\u6bcf\u6b21\u8fde\u6da8\u8dcc\uff0c\u5e76\u7edf\u8ba1\u548c\u53ef\u89c6\u5316\u7ed3\u679c\u3002\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5e2e\u52a9\u6295\u8d44\u8005\u66f4\u597d\u5730\u7406\u89e3\u80a1\u7968\u7684\u6da8\u8dcc\u8d8b\u52bf\uff0c\u4ece\u800c\u505a\u51fa\u66f4\u660e\u667a\u7684\u6295\u8d44\u51b3\u7b56\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u7528Python\u83b7\u53d6\u80a1\u7968\u7684\u5386\u53f2\u4ef7\u683c\u6570\u636e\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528\u591a\u4e2a\u5e93\u6765\u83b7\u53d6\u80a1\u7968\u7684\u5386\u53f2\u4ef7\u683c\u6570\u636e\u3002\u6700\u5e38\u7528\u7684\u5e93\u662f<code>pandas_datareader<\/code>\uff0c\u5b83\u53ef\u4ee5\u4eceYahoo Finance\u3001Alpha Vantage\u7b49\u6765\u6e90\u83b7\u53d6\u6570\u636e\u3002\u6b64\u5916\uff0c<code>yfinance<\/code>\u5e93\u4e5f\u662f\u4e00\u4e2a\u975e\u5e38\u6d41\u884c\u7684\u9009\u62e9\uff0c\u5141\u8bb8\u7528\u6237\u8f7b\u677e\u4e0b\u8f7d\u80a1\u7968\u6570\u636e\u3002\u901a\u8fc7\u4ee5\u4e0b\u4ee3\u7801\u53ef\u4ee5\u83b7\u53d6\u67d0\u53ea\u80a1\u7968\u7684\u5386\u53f2\u6570\u636e\uff1a<\/p>\n<pre><code class=\"language-python\">import yfinance as yf\n\n# \u83b7\u53d6\u7279\u5b9a\u80a1\u7968\u7684\u5386\u53f2\u6570\u636e\nstock_data = yf.download(&#39;AAPL&#39;, start=&#39;2020-01-01&#39;, end=&#39;2023-01-01&#39;)\n<\/code><\/pre>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u8bc6\u522b\u80a1\u7968\u7684\u8fde\u6da8\u548c\u8fde\u8dcc\uff1f<\/strong><br \/>\u8bc6\u522b\u80a1\u7968\u7684\u8fde\u6da8\u548c\u8fde\u8dcc\u53ef\u4ee5\u901a\u8fc7\u8ba1\u7b97\u6bcf\u65e5\u6536\u76d8\u4ef7\u7684\u53d8\u5316\u7387\u6765\u5b9e\u73b0\u3002\u53ef\u4ee5\u904d\u5386\u4ef7\u683c\u6570\u636e\uff0c\u68c0\u67e5\u8fde\u7eed\u51e0\u5929\u7684\u4ef7\u683c\u53d8\u5316\u662f\u5426\u90fd\u662f\u4e0a\u6da8\u6216\u4e0b\u8dcc\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-python\">import pandas as pd\n\n# \u8ba1\u7b97\u6bcf\u65e5\u6536\u76ca\u7387\nstock_data[&#39;Returns&#39;] = stock_data[&#39;Close&#39;].pct_change()\n# \u6807\u8bb0\u8fde\u6da8\u548c\u8fde\u8dcc\nstock_data[&#39;Trend&#39;] = stock_data[&#39;Returns&#39;].apply(lambda x: &#39;\u4e0a\u6da8&#39; if x &gt; 0 else (&#39;\u4e0b\u8dcc&#39; if x &lt; 0 else &#39;\u5e73&#39;))\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u4f7f\u7528Python\u7ed8\u5236\u80a1\u7968\u4ef7\u683c\u7684\u53d8\u5316\u8d8b\u52bf\u56fe\uff1f<\/strong><br \/>\u5229\u7528<code>matplotlib<\/code>\u5e93\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u7ed8\u5236\u80a1\u7968\u4ef7\u683c\u7684\u53d8\u5316\u8d8b\u52bf\u56fe\u3002\u60a8\u53ea\u9700\u5c06\u65e5\u671f\u8bbe\u7f6e\u4e3aX\u8f74\uff0c\u5c06\u6536\u76d8\u4ef7\u8bbe\u7f6e\u4e3aY\u8f74\u3002\u4ee5\u4e0b\u662f\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-python\">import matplotlib.pyplot as plt\n\n# \u7ed8\u5236\u80a1\u7968\u6536\u76d8\u4ef7\u53d8\u5316\u8d8b\u52bf\u56fe\nplt.figure(figsize=(12, 6))\nplt.plot(stock_data.index, stock_data[&#39;Close&#39;], label=&#39;\u6536\u76d8\u4ef7&#39;)\nplt.title(&#39;\u80a1\u7968\u6536\u76d8\u4ef7\u53d8\u5316\u8d8b\u52bf&#39;)\nplt.xlabel(&#39;\u65e5\u671f&#39;)\nplt.ylabel(&#39;\u4ef7\u683c&#39;)\nplt.legend()\nplt.show()\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"\u5982\u4f55\u7528Python\u8ba1\u7b97\u80a1\u7968\u6bcf\u6b21\u8fde\u6da8\u8dcc \u8981\u7528Python\u8ba1\u7b97\u80a1\u7968\u6bcf\u6b21\u8fde\u6da8\u8dcc\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u5b9e\u73b0\uff1a\u83b7\u53d6\u80a1\u7968\u6570\u636e\u3001 [&hellip;]","protected":false},"author":3,"featured_media":1041630,"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\/1041627"}],"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=1041627"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1041627\/revisions"}],"predecessor-version":[{"id":1041632,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1041627\/revisions\/1041632"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1041630"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1041627"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1041627"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1041627"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}