{"id":1170601,"date":"2025-01-15T16:24:37","date_gmt":"2025-01-15T08:24:37","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1170601.html"},"modified":"2025-01-15T16:24:40","modified_gmt":"2025-01-15T08:24:40","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e7%94%bb%e6%8c%81%e4%bb%93%e4%bf%a1%e5%8f%b7","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1170601.html","title":{"rendered":"\u5982\u4f55\u7528python\u753b\u6301\u4ed3\u4fe1\u53f7"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26073159\/4cc963fd-2d75-4daa-a66d-357c39265def.webp\" alt=\"\u5982\u4f55\u7528python\u753b\u6301\u4ed3\u4fe1\u53f7\" \/><\/p>\n<p><p> <strong>\u8981\u7528Python\u753b\u6301\u4ed3\u4fe1\u53f7\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e9b\u6570\u636e\u53ef\u89c6\u5316\u5e93\uff0c\u4f8b\u5982Matplotlib\u548cPlotly<\/strong>\u3002<strong>\u4f60\u9700\u8981\u9996\u5148\u83b7\u53d6\u6240\u9700\u7684\u6570\u636e\u3001\u5904\u7406\u6570\u636e\u3001\u751f\u6210\u6301\u4ed3\u4fe1\u53f7\uff0c\u7136\u540e\u7ed8\u5236\u56fe\u8868<\/strong>\u3002<strong>\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u4f7f\u7528\u8fd9\u4e9b\u5e93\u7ed8\u5236\u6301\u4ed3\u4fe1\u53f7\u7684\u5177\u4f53\u6b65\u9aa4<\/strong>\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u83b7\u53d6\u548c\u5904\u7406\u6570\u636e<\/h2>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u83b7\u53d6\u91d1\u878d\u5e02\u573a\u7684\u6570\u636e\uff0c\u4f8b\u5982\u80a1\u7968\u4ef7\u683c\u6570\u636e\u3002\u53ef\u4ee5\u4f7f\u7528<code>yfinance<\/code>\u5e93\u6765\u83b7\u53d6\u6570\u636e\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import yfinance as yf<\/p>\n<h2><strong>\u83b7\u53d6\u80a1\u7968\u6570\u636e<\/strong><\/h2>\n<p>ticker = &#39;AAPL&#39;<\/p>\n<p>data = yf.download(ticker, start=&#39;2022-01-01&#39;, end=&#39;2023-01-01&#39;)<\/p>\n<p>print(data.head())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u4e0b\u8f7dApple\u516c\u53f8\u57282022\u5e74\u52302023\u5e74\u7684\u80a1\u7968\u6570\u636e\uff0c\u5e76\u663e\u793a\u524d\u51e0\u884c\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h2>\u4e8c\u3001\u751f\u6210\u6301\u4ed3\u4fe1\u53f7<\/h2>\n<\/p>\n<p><p>\u5728\u751f\u6210\u6301\u4ed3\u4fe1\u53f7\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u51b3\u5b9a\u4f7f\u7528\u54ea\u79cd\u7b56\u7565\u3002\u4f8b\u5982\uff0c\u8fd9\u91cc\u6211\u4eec\u4f7f\u7528\u7b80\u5355\u79fb\u52a8\u5e73\u5747\u7ebf\u4ea4\u53c9\u7b56\u7565\u6765\u751f\u6210\u4e70\u5356\u4fe1\u53f7\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8ba1\u7b97\u77ed\u671f\u548c\u957f\u671f\u79fb\u52a8\u5e73\u5747\u7ebf<\/p>\n<p>short_window = 40<\/p>\n<p>long_window = 100<\/p>\n<p>data[&#39;Short_MA&#39;] = data[&#39;Close&#39;].rolling(window=short_window).mean()<\/p>\n<p>data[&#39;Long_MA&#39;] = data[&#39;Close&#39;].rolling(window=long_window).mean()<\/p>\n<h2><strong>\u751f\u6210\u4e70\u5356\u4fe1\u53f7<\/strong><\/h2>\n<p>data[&#39;Signal&#39;] = 0<\/p>\n<p>data[&#39;Signal&#39;][short_window:] = np.where(data[&#39;Short_MA&#39;][short_window:] &gt; data[&#39;Long_MA&#39;][short_window:], 1, 0)<\/p>\n<p>data[&#39;Position&#39;] = data[&#39;Signal&#39;].diff()<\/p>\n<p>print(data[[&#39;Close&#39;, &#39;Short_MA&#39;, &#39;Long_MA&#39;, &#39;Signal&#39;, &#39;Position&#39;]].head())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u8ba1\u7b97\u4e86\u77ed\u671f\u548c\u957f\u671f\u7684\u79fb\u52a8\u5e73\u5747\u7ebf\uff0c\u5e76\u6839\u636e\u8fd9\u4e24\u4e2a\u5747\u7ebf\u7684\u4ea4\u53c9\u751f\u6210\u4e70\u5356\u4fe1\u53f7\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001\u4f7f\u7528Matplotlib\u7ed8\u56fe<\/h2>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u4f7f\u7528Matplotlib\u5e93\u6765\u7ed8\u5236\u56fe\u8868\u3002\u6211\u4eec\u5c06\u7ed8\u5236\u80a1\u7968\u7684\u6536\u76d8\u4ef7\u683c\u3001\u77ed\u671f\u548c\u957f\u671f\u79fb\u52a8\u5e73\u5747\u7ebf\uff0c\u4ee5\u53ca\u6301\u4ed3\u4fe1\u53f7\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>plt.figure(figsize=(14, 7))<\/p>\n<h2><strong>\u7ed8\u5236\u6536\u76d8\u4ef7\u683c<\/strong><\/h2>\n<p>plt.plot(data[&#39;Close&#39;], label=&#39;Close Price&#39;)<\/p>\n<h2><strong>\u7ed8\u5236\u77ed\u671f\u548c\u957f\u671f\u79fb\u52a8\u5e73\u5747\u7ebf<\/strong><\/h2>\n<p>plt.plot(data[&#39;Short_MA&#39;], label=&#39;40-Day Moving Average&#39;)<\/p>\n<p>plt.plot(data[&#39;Long_MA&#39;], label=&#39;100-Day Moving Average&#39;)<\/p>\n<h2><strong>\u7ed8\u5236\u4e70\u5356\u4fe1\u53f7<\/strong><\/h2>\n<p>plt.plot(data.loc[data[&#39;Position&#39;] == 1].index, <\/p>\n<p>         data[&#39;Short_MA&#39;][data[&#39;Position&#39;] == 1], <\/p>\n<p>         &#39;^&#39;, markersize=10, color=&#39;g&#39;, lw=0, label=&#39;Buy Signal&#39;)<\/p>\n<p>plt.plot(data.loc[data[&#39;Position&#39;] == -1].index, <\/p>\n<p>         data[&#39;Short_MA&#39;][data[&#39;Position&#39;] == -1], <\/p>\n<p>         &#39;v&#39;, markersize=10, color=&#39;r&#39;, lw=0, label=&#39;Sell Signal&#39;)<\/p>\n<p>plt.title(f&#39;{ticker} Price and Trading Signals&#39;)<\/p>\n<p>plt.xlabel(&#39;Date&#39;)<\/p>\n<p>plt.ylabel(&#39;Price&#39;)<\/p>\n<p>plt.legend()<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u4f7f\u7528Matplotlib\u7ed8\u5236\u4e86\u80a1\u7968\u6536\u76d8\u4ef7\u683c\u3001\u77ed\u671f\u548c\u957f\u671f\u79fb\u52a8\u5e73\u5747\u7ebf\uff0c\u4ee5\u53ca\u4e70\u5356\u4fe1\u53f7\u3002\u6211\u4eec\u7528\u7eff\u8272\u5411\u4e0a\u7684\u4e09\u89d2\u5f62\u8868\u793a\u4e70\u5165\u4fe1\u53f7\uff0c\u7528\u7ea2\u8272\u5411\u4e0b\u7684\u4e09\u89d2\u5f62\u8868\u793a\u5356\u51fa\u4fe1\u53f7\u3002<\/p>\n<\/p>\n<p><h2>\u56db\u3001\u4f7f\u7528Plotly\u7ed8\u56fe<\/h2>\n<\/p>\n<p><p>Plotly\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684\u4ea4\u4e92\u5f0f\u7ed8\u56fe\u5e93\uff0c\u5b83\u4f7f\u5f97\u7ed8\u56fe\u66f4\u52a0\u751f\u52a8\u548c\u4e92\u52a8\u3002\u4e0b\u9762\u662f\u4f7f\u7528Plotly\u7ed8\u5236\u6301\u4ed3\u4fe1\u53f7\u7684\u793a\u4f8b\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import plotly.graph_objects as go<\/p>\n<h2><strong>\u521b\u5efa\u56fe\u8868<\/strong><\/h2>\n<p>fig = go.Figure()<\/p>\n<h2><strong>\u6dfb\u52a0\u6536\u76d8\u4ef7\u683c<\/strong><\/h2>\n<p>fig.add_trace(go.Scatter(x=data.index, y=data[&#39;Close&#39;], mode=&#39;lines&#39;, name=&#39;Close Price&#39;))<\/p>\n<h2><strong>\u6dfb\u52a0\u77ed\u671f\u548c\u957f\u671f\u79fb\u52a8\u5e73\u5747\u7ebf<\/strong><\/h2>\n<p>fig.add_trace(go.Scatter(x=data.index, y=data[&#39;Short_MA&#39;], mode=&#39;lines&#39;, name=&#39;40-Day Moving Average&#39;))<\/p>\n<p>fig.add_trace(go.Scatter(x=data.index, y=data[&#39;Long_MA&#39;], mode=&#39;lines&#39;, name=&#39;100-Day Moving Average&#39;))<\/p>\n<h2><strong>\u6dfb\u52a0\u4e70\u5356\u4fe1\u53f7<\/strong><\/h2>\n<p>fig.add_trace(go.Scatter(x=data.loc[data[&#39;Position&#39;] == 1].index, <\/p>\n<p>                         y=data[&#39;Short_MA&#39;][data[&#39;Position&#39;] == 1], <\/p>\n<p>                         mode=&#39;markers&#39;, marker=dict(symbol=&#39;triangle-up&#39;, size=10, color=&#39;green&#39;), name=&#39;Buy Signal&#39;))<\/p>\n<p>fig.add_trace(go.Scatter(x=data.loc[data[&#39;Position&#39;] == -1].index, <\/p>\n<p>                         y=data[&#39;Short_MA&#39;][data[&#39;Position&#39;] == -1], <\/p>\n<p>                         mode=&#39;markers&#39;, marker=dict(symbol=&#39;triangle-down&#39;, size=10, color=&#39;red&#39;), name=&#39;Sell Signal&#39;))<\/p>\n<h2><strong>\u6dfb\u52a0\u6807\u9898\u548c\u6807\u7b7e<\/strong><\/h2>\n<p>fig.update_layout(title=f&#39;{ticker} Price and Trading Signals&#39;,<\/p>\n<p>                  xaxis_title=&#39;Date&#39;,<\/p>\n<p>                  yaxis_title=&#39;Price&#39;,<\/p>\n<p>                  legend_title=&#39;Legend&#39;)<\/p>\n<p>fig.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u4f7f\u7528Plotly\u7ed8\u5236\u4e86\u80a1\u7968\u6536\u76d8\u4ef7\u683c\u3001\u77ed\u671f\u548c\u957f\u671f\u79fb\u52a8\u5e73\u5747\u7ebf\uff0c\u4ee5\u53ca\u4e70\u5356\u4fe1\u53f7\u3002Plotly\u7684\u56fe\u8868\u662f\u4ea4\u4e92\u5f0f\u7684\uff0c\u53ef\u4ee5\u653e\u5927\u548c\u7f29\u5c0f\uff0c\u5e76\u63d0\u4f9b\u66f4\u597d\u7684\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n<\/p>\n<p><h2>\u4e94\u3001\u7ed3\u5408\u5176\u4ed6\u6280\u672f\u6307\u6807<\/h2>\n<\/p>\n<p><p>\u9664\u4e86\u7b80\u5355\u79fb\u52a8\u5e73\u5747\u7ebf\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u7ed3\u5408\u5176\u4ed6\u6280\u672f\u6307\u6807\u6765\u751f\u6210\u66f4\u590d\u6742\u7684\u6301\u4ed3\u4fe1\u53f7\u3002\u4f8b\u5982\uff0c\u7ed3\u5408\u76f8\u5bf9\u5f3a\u5f31\u6307\u6570\uff08RSI\uff09\u548c\u79fb\u52a8\u5e73\u5747\u6536\u655b\u6563\u5ea6\uff08MACD\uff09\u6765\u751f\u6210\u4e70\u5356\u4fe1\u53f7\u3002<\/p>\n<\/p>\n<p><h3>\u8ba1\u7b97RSI<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>def compute_rsi(data, window=14):<\/p>\n<p>    delta = data[&#39;Close&#39;].diff()<\/p>\n<p>    g<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n = (delta.where(delta &gt; 0, 0)).rolling(window=window).mean()<\/p>\n<p>    loss = (-delta.where(delta &lt; 0, 0)).rolling(window=window).mean()<\/p>\n<p>    rs = gain \/ loss<\/p>\n<p>    rsi = 100 - (100 \/ (1 + rs))<\/p>\n<p>    return rsi<\/p>\n<p>data[&#39;RSI&#39;] = compute_rsi(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u8ba1\u7b97\u4e86\u76f8\u5bf9\u5f3a\u5f31\u6307\u6570\uff08RSI\uff09\uff0c\u8fd9\u662f\u4e00\u4e2a\u8861\u91cf\u4ef7\u683c\u53d8\u52a8\u5f3a\u5ea6\u7684\u6280\u672f\u6307\u6807\u3002<\/p>\n<\/p>\n<p><h3>\u8ba1\u7b97MACD<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">def compute_macd(data, short_window=12, long_window=26, signal_window=9):<\/p>\n<p>    short_ema = data[&#39;Close&#39;].ewm(span=short_window, adjust=False).mean()<\/p>\n<p>    long_ema = data[&#39;Close&#39;].ewm(span=long_window, adjust=False).mean()<\/p>\n<p>    macd = short_ema - long_ema<\/p>\n<p>    signal = macd.ewm(span=signal_window, adjust=False).mean()<\/p>\n<p>    return macd, signal<\/p>\n<p>data[&#39;MACD&#39;], data[&#39;MACD_Signal&#39;] = compute_macd(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u8ba1\u7b97\u4e86\u79fb\u52a8\u5e73\u5747\u6536\u655b\u6563\u5ea6\uff08MACD\uff09\u548c\u4fe1\u53f7\u7ebf\uff0cMACD\u662f\u8861\u91cf\u4ef7\u683c\u53d8\u52a8\u8d8b\u52bf\u7684\u6280\u672f\u6307\u6807\u3002<\/p>\n<\/p>\n<p><h3>\u7ed3\u5408RSI\u548cMACD\u751f\u6210\u6301\u4ed3\u4fe1\u53f7<\/h3>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u7ed3\u5408RSI\u548cMACD\u6765\u751f\u6210\u66f4\u590d\u6742\u7684\u4e70\u5356\u4fe1\u53f7\u3002\u4f8b\u5982\uff0c\u5f53RSI\u4f4e\u4e8e30\u4e14MACD\u7ebf\u4ece\u4e0b\u65b9\u7a7f\u8fc7\u4fe1\u53f7\u7ebf\u65f6\u751f\u6210\u4e70\u5165\u4fe1\u53f7\uff0c\u5f53RSI\u9ad8\u4e8e70\u4e14MACD\u7ebf\u4ece\u4e0a\u65b9\u7a7f\u8fc7\u4fe1\u53f7\u7ebf\u65f6\u751f\u6210\u5356\u51fa\u4fe1\u53f7\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data[&#39;Signal&#39;] = 0<\/p>\n<p>data[&#39;Signal&#39;] = np.where((data[&#39;RSI&#39;] &lt; 30) &amp; (data[&#39;MACD&#39;] &gt; data[&#39;MACD_Signal&#39;]), 1, data[&#39;Signal&#39;])<\/p>\n<p>data[&#39;Signal&#39;] = np.where((data[&#39;RSI&#39;] &gt; 70) &amp; (data[&#39;MACD&#39;] &lt; data[&#39;MACD_Signal&#39;]), -1, data[&#39;Signal&#39;])<\/p>\n<p>data[&#39;Position&#39;] = data[&#39;Signal&#39;].diff()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u7ed3\u5408\u4e86RSI\u548cMACD\u751f\u6210\u4e70\u5356\u4fe1\u53f7\uff0c\u5e76\u8ba1\u7b97\u4e86\u6301\u4ed3\u72b6\u6001\u3002<\/p>\n<\/p>\n<p><h3>\u7ed8\u5236\u7ed3\u5408RSI\u548cMACD\u7684\u6301\u4ed3\u4fe1\u53f7<\/h3>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e4b\u524d\u4ecb\u7ecd\u7684Matplotlib\u548cPlotly\u6765\u7ed8\u5236\u7ed3\u5408RSI\u548cMACD\u751f\u6210\u7684\u6301\u4ed3\u4fe1\u53f7\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528Matplotlib\u7ed8\u5236\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plt.figure(figsize=(14, 7))<\/p>\n<h2><strong>\u7ed8\u5236\u6536\u76d8\u4ef7\u683c<\/strong><\/h2>\n<p>plt.plot(data[&#39;Close&#39;], label=&#39;Close Price&#39;)<\/p>\n<h2><strong>\u7ed8\u5236MACD\u548c\u4fe1\u53f7\u7ebf<\/strong><\/h2>\n<p>plt.plot(data[&#39;MACD&#39;], label=&#39;MACD&#39;)<\/p>\n<p>plt.plot(data[&#39;MACD_Signal&#39;], label=&#39;MACD Signal&#39;)<\/p>\n<h2><strong>\u7ed8\u5236\u4e70\u5356\u4fe1\u53f7<\/strong><\/h2>\n<p>plt.plot(data.loc[data[&#39;Position&#39;] == 1].index, <\/p>\n<p>         data[&#39;MACD&#39;][data[&#39;Position&#39;] == 1], <\/p>\n<p>         &#39;^&#39;, markersize=10, color=&#39;g&#39;, lw=0, label=&#39;Buy Signal&#39;)<\/p>\n<p>plt.plot(data.loc[data[&#39;Position&#39;] == -1].index, <\/p>\n<p>         data[&#39;MACD&#39;][data[&#39;Position&#39;] == -1], <\/p>\n<p>         &#39;v&#39;, markersize=10, color=&#39;r&#39;, lw=0, label=&#39;Sell Signal&#39;)<\/p>\n<p>plt.title(f&#39;{ticker} Price and Trading Signals&#39;)<\/p>\n<p>plt.xlabel(&#39;Date&#39;)<\/p>\n<p>plt.ylabel(&#39;Price&#39;)<\/p>\n<p>plt.legend()<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u4f7f\u7528Matplotlib\u7ed8\u5236\u4e86\u7ed3\u5408RSI\u548cMACD\u751f\u6210\u7684\u6301\u4ed3\u4fe1\u53f7\u3002<\/p>\n<\/p>\n<p><h2>\u516d\u3001\u4f18\u5316\u7b56\u7565<\/h2>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u64cd\u4f5c\u4e2d\uff0c\u4ea4\u6613\u7b56\u7565\u9700\u8981\u4e0d\u65ad\u4f18\u5316\u3002\u53ef\u4ee5\u4f7f\u7528\u5386\u53f2\u6570\u636e\u8fdb\u884c\u56de\u6d4b\uff0c\u8bc4\u4f30\u7b56\u7565\u7684\u6536\u76ca\u548c\u98ce\u9669\uff0c\u5e76\u8fdb\u884c\u53c2\u6570\u8c03\u6574\u548c\u4f18\u5316\u3002<\/p>\n<\/p>\n<p><h3>\u56de\u6d4b\u7b56\u7565<\/h3>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u56de\u6d4b\u7b56\u7565\u793a\u4f8b\uff0c\u6211\u4eec\u8ba1\u7b97\u7b56\u7565\u7684\u7d2f\u8ba1\u6536\u76ca\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">initial_capital = 10000<\/p>\n<p>positions = pd.DataFrame(index=data.index).fillna(0)<\/p>\n<p>positions[ticker] = data[&#39;Signal&#39;]<\/p>\n<p>portfolio = positions.multiply(data[&#39;Close&#39;], axis=0)<\/p>\n<p>pos_diff = positions.diff()<\/p>\n<p>portfolio[&#39;holdings&#39;] = (positions.multiply(data[&#39;Close&#39;], axis=0)).sum(axis=1)<\/p>\n<p>portfolio[&#39;cash&#39;] = initial_capital - (pos_diff.multiply(data[&#39;Close&#39;], axis=0)).sum(axis=1).cumsum()<\/p>\n<p>portfolio[&#39;total&#39;] = portfolio[&#39;cash&#39;] + portfolio[&#39;holdings&#39;]<\/p>\n<p>portfolio[&#39;returns&#39;] = portfolio[&#39;total&#39;].pct_change()<\/p>\n<p>print(portfolio.head())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u8ba1\u7b97\u4e86\u7b56\u7565\u7684\u6301\u4ed3\u3001\u73b0\u91d1\u548c\u603b\u8d44\u4ea7\uff0c\u5e76\u8ba1\u7b97\u4e86\u7d2f\u8ba1\u6536\u76ca\u3002<\/p>\n<\/p>\n<p><h3>\u4f18\u5316\u53c2\u6570<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528\u7f51\u683c\u641c\u7d22\u6216\u9057\u4f20\u7b97\u6cd5\u7b49\u65b9\u6cd5\u4f18\u5316\u7b56\u7565\u53c2\u6570\uff0c\u5bfb\u627e\u6700\u4f73\u53c2\u6570\u7ec4\u5408\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u7f51\u683c\u641c\u7d22\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sklearn.model_selection import ParameterGrid<\/p>\n<p>param_grid = {<\/p>\n<p>    &#39;short_window&#39;: [20, 40, 60],<\/p>\n<p>    &#39;long_window&#39;: [100, 150, 200]<\/p>\n<p>}<\/p>\n<p>best_params = None<\/p>\n<p>best_return = -np.inf<\/p>\n<p>for params in ParameterGrid(param_grid):<\/p>\n<p>    short_window = params[&#39;short_window&#39;]<\/p>\n<p>    long_window = params[&#39;long_window&#39;]<\/p>\n<p>    data[&#39;Short_MA&#39;] = data[&#39;Close&#39;].rolling(window=short_window).mean()<\/p>\n<p>    data[&#39;Long_MA&#39;] = data[&#39;Close&#39;].rolling(window=long_window).mean()<\/p>\n<p>    data[&#39;Signal&#39;] = 0<\/p>\n<p>    data[&#39;Signal&#39;][short_window:] = np.where(data[&#39;Short_MA&#39;][short_window:] &gt; data[&#39;Long_MA&#39;][short_window:], 1, 0)<\/p>\n<p>    data[&#39;Position&#39;] = data[&#39;Signal&#39;].diff()<\/p>\n<p>    positions = pd.DataFrame(index=data.index).fillna(0)<\/p>\n<p>    positions[ticker] = data[&#39;Signal&#39;]<\/p>\n<p>    portfolio = positions.multiply(data[&#39;Close&#39;], axis=0)<\/p>\n<p>    pos_diff = positions.diff()<\/p>\n<p>    portfolio[&#39;holdings&#39;] = (positions.multiply(data[&#39;Close&#39;], axis=0)).sum(axis=1)<\/p>\n<p>    portfolio[&#39;cash&#39;] = initial_capital - (pos_diff.multiply(data[&#39;Close&#39;], axis=0)).sum(axis=1).cumsum()<\/p>\n<p>    portfolio[&#39;total&#39;] = portfolio[&#39;cash&#39;] + portfolio[&#39;holdings&#39;]<\/p>\n<p>    portfolio[&#39;returns&#39;] = portfolio[&#39;total&#39;].pct_change()<\/p>\n<p>    total_return = portfolio[&#39;total&#39;][-1]<\/p>\n<p>    if total_return &gt; best_return:<\/p>\n<p>        best_return = total_return<\/p>\n<p>        best_params = params<\/p>\n<p>print(f&#39;Best parameters: {best_params}&#39;)<\/p>\n<p>print(f&#39;Best return: {best_return}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u4f7f\u7528\u7f51\u683c\u641c\u7d22\u4f18\u5316\u4e86\u77ed\u671f\u548c\u957f\u671f\u79fb\u52a8\u5e73\u5747\u7ebf\u7684\u7a97\u53e3\u5927\u5c0f\uff0c\u627e\u5230\u4e86\u6700\u4f73\u53c2\u6570\u7ec4\u5408\u3002<\/p>\n<\/p>\n<p><h2>\u4e03\u3001\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Python\u7ed8\u5236\u6301\u4ed3\u4fe1\u53f7\uff0c\u5305\u62ec\u6570\u636e\u83b7\u53d6\u3001\u6570\u636e\u5904\u7406\u3001\u751f\u6210\u6301\u4ed3\u4fe1\u53f7\u3001\u7ed8\u5236\u56fe\u8868\u4ee5\u53ca\u4f18\u5316\u7b56\u7565\u7b49\u65b9\u9762\u3002\u901a\u8fc7\u7ed3\u5408\u4e0d\u540c\u7684\u6280\u672f\u6307\u6807\uff0c\u6211\u4eec\u53ef\u4ee5\u751f\u6210\u66f4\u590d\u6742\u7684\u6301\u4ed3\u4fe1\u53f7\uff0c\u5e76\u901a\u8fc7\u56de\u6d4b\u548c\u4f18\u5316\u63d0\u9ad8\u7b56\u7565\u7684\u6536\u76ca\u548c\u7a33\u5b9a\u6027\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u80fd\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u5982\u4f55\u7528Python\u7ed8\u5236\u6301\u4ed3\u4fe1\u53f7\uff0c\u5e76\u5e94\u7528\u5230\u5b9e\u9645\u4ea4\u6613\u4e2d\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u7ed8\u5236\u6301\u4ed3\u4fe1\u53f7\u7684\u56fe\u8868\uff1f<\/strong><br \/>\u7ed8\u5236\u6301\u4ed3\u4fe1\u53f7\u7684\u56fe\u8868\u901a\u5e38\u9700\u8981\u4f7f\u7528\u6570\u636e\u53ef\u89c6\u5316\u5e93\uff0c\u4f8b\u5982Matplotlib\u6216Seaborn\u3002\u9996\u5148\uff0c\u60a8\u9700\u8981\u51c6\u5907\u597d\u76f8\u5173\u7684\u6301\u4ed3\u6570\u636e\uff0c\u5305\u62ec\u4e70\u5165\u548c\u5356\u51fa\u4fe1\u53f7\u3002\u5229\u7528\u8fd9\u4e9b\u6570\u636e\uff0c\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u65f6\u95f4\u5e8f\u5217\u56fe\uff0c\u5e76\u5728\u56fe\u4e2d\u7528\u4e0d\u540c\u7684\u6807\u8bb0\u8868\u793a\u4e70\u5165\u548c\u5356\u51fa\u4fe1\u53f7\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u4ee3\u7801\uff1a  <\/p>\n<pre><code class=\"language-python\">import matplotlib.pyplot as plt\nimport pandas as pd\n\n# \u5047\u8bbedf\u662f\u5305\u542b\u65e5\u671f\u548c\u6301\u4ed3\u4fe1\u53f7\u7684\u6570\u636e\u6846\n# df[&#39;buy_signal&#39;] \u548c df[&#39;sell_signal&#39;] \u662f\u5e03\u5c14\u578b\u5217\nplt.figure(figsize=(12,6))\nplt.plot(df[&#39;date&#39;], df[&#39;price&#39;], label=&#39;\u4ef7\u683c&#39;, color=&#39;blue&#39;)\n\n# \u753b\u51fa\u4e70\u5165\u4fe1\u53f7\nplt.scatter(df[df[&#39;buy_signal&#39;]][&#39;date&#39;], df[df[&#39;buy_signal&#39;]][&#39;price&#39;], marker=&#39;^&#39;, color=&#39;green&#39;, label=&#39;\u4e70\u5165\u4fe1\u53f7&#39;, s=100)\n\n# \u753b\u51fa\u5356\u51fa\u4fe1\u53f7\nplt.scatter(df[df[&#39;sell_signal&#39;]][&#39;date&#39;], df[df[&#39;sell_signal&#39;]][&#39;price&#39;], marker=&#39;v&#39;, color=&#39;red&#39;, label=&#39;\u5356\u51fa\u4fe1\u53f7&#39;, s=100)\n\nplt.title(&#39;\u6301\u4ed3\u4fe1\u53f7\u56fe&#39;)\nplt.xlabel(&#39;\u65e5\u671f&#39;)\nplt.ylabel(&#39;\u4ef7\u683c&#39;)\nplt.legend()\nplt.show()\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u5e2e\u52a9\u60a8\u5728\u56fe\u8868\u4e2d\u6e05\u6670\u5730\u6807\u8bc6\u51fa\u4e70\u5165\u548c\u5356\u51fa\u4fe1\u53f7\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5904\u7406\u6301\u4ed3\u4fe1\u53f7\u6570\u636e\uff1f<\/strong><br \/>\u5904\u7406\u6301\u4ed3\u4fe1\u53f7\u6570\u636e\u901a\u5e38\u5305\u62ec\u6570\u636e\u6e05\u7406\u3001\u7279\u5f81\u9009\u62e9\u548c\u4fe1\u53f7\u751f\u6210\u3002\u60a8\u9700\u8981\u786e\u4fdd\u6570\u636e\u7684\u51c6\u786e\u6027\uff0c\u53bb\u9664\u7f3a\u5931\u503c\u6216\u5f02\u5e38\u503c\u3002\u53ef\u4ee5\u4f7f\u7528Pandas\u5e93\u6765\u64cd\u4f5c\u6570\u636e\u6846\uff0c\u751f\u6210\u4e70\u5165\u548c\u5356\u51fa\u4fe1\u53f7\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u6839\u636e\u79fb\u52a8\u5e73\u5747\u7ebf\u7684\u4ea4\u53c9\u6765\u786e\u5b9a\u4e70\u5165\u548c\u5356\u51fa\u65f6\u673a\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u60a8\u53ef\u4ee5\u66f4\u6709\u6548\u5730\u4e3a\u6301\u4ed3\u4fe1\u53f7\u751f\u6210\u505a\u51c6\u5907\u3002<\/p>\n<p><strong>\u6709\u54ea\u4e9bPython\u5e93\u9002\u5408\u7ed8\u5236\u6301\u4ed3\u4fe1\u53f7\uff1f<\/strong><br \/>\u5728\u7ed8\u5236\u6301\u4ed3\u4fe1\u53f7\u65f6\uff0c\u5e38\u7528\u7684Python\u5e93\u5305\u62ecMatplotlib\u3001Seaborn\u3001Plotly\u548cBokeh\u3002Matplotlib\u662f\u6700\u57fa\u7840\u7684\u7ed8\u56fe\u5e93\uff0c\u9002\u5408\u7ed8\u5236\u7b80\u5355\u7684\u56fe\u8868\u3002Seaborn\u5728Matplotlib\u7684\u57fa\u7840\u4e0a\u8fdb\u884c\u4e86\u6269\u5c55\uff0c\u63d0\u4f9b\u4e86\u66f4\u7f8e\u89c2\u7684\u9ed8\u8ba4\u6837\u5f0f\u3002Plotly\u548cBokeh\u5219\u652f\u6301\u4ea4\u4e92\u5f0f\u56fe\u8868\uff0c\u9002\u5408\u9700\u8981\u5728\u7f51\u9875\u4e2d\u5c55\u793a\u52a8\u6001\u6570\u636e\u7684\u60c5\u51b5\u3002\u60a8\u53ef\u4ee5\u6839\u636e\u9879\u76ee\u7684\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5e93\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u7528Python\u753b\u6301\u4ed3\u4fe1\u53f7\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e9b\u6570\u636e\u53ef\u89c6\u5316\u5e93\uff0c\u4f8b\u5982Matplotlib\u548cPlotly\u3002\u4f60\u9700\u8981\u9996\u5148\u83b7\u53d6\u6240 [&hellip;]","protected":false},"author":3,"featured_media":1170608,"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\/1170601"}],"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=1170601"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1170601\/revisions"}],"predecessor-version":[{"id":1170611,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1170601\/revisions\/1170611"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1170608"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1170601"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1170601"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1170601"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}