{"id":1902,"date":"2025-02-16T07:39:00","date_gmt":"2025-02-15T23:39:00","guid":{"rendered":"https:\/\/blog.laoyulaoyu.top\/?p=1902"},"modified":"2025-01-16T09:54:54","modified_gmt":"2025-01-16T01:54:54","slug":"%e7%ae%97%e6%b3%95%e4%ba%a4%e6%98%93%e6%96%b0%e7%aa%81%e7%a0%b4%ef%bc%9apython-%e4%bc%98%e5%8c%96-rsi-%e5%85%a8%e6%94%bb%e7%95%a5%ef%bc%8c%e8%bd%bb%e6%9d%be%e6%8f%90%e5%8d%87%e7%9b%88%e5%88%a9","status":"publish","type":"post","link":"https:\/\/laoyulaoyu.com\/index.php\/2025\/02\/16\/%e7%ae%97%e6%b3%95%e4%ba%a4%e6%98%93%e6%96%b0%e7%aa%81%e7%a0%b4%ef%bc%9apython-%e4%bc%98%e5%8c%96-rsi-%e5%85%a8%e6%94%bb%e7%95%a5%ef%bc%8c%e8%bd%bb%e6%9d%be%e6%8f%90%e5%8d%87%e7%9b%88%e5%88%a9\/","title":{"rendered":"\u7b97\u6cd5\u4ea4\u6613\u65b0\u7a81\u7834\uff1aPython \u4f18\u5316 RSI \u5168\u653b\u7565\uff0c\u8f7b\u677e\u63d0\u5347\u76c8\u5229\u80fd\u529b\uff01"},"content":{"rendered":"\n<p>\u4f5c\u8005\uff1a<a href=\"https:\/\/www.laoyulaoyu.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">\u8001\u4f59\u635e\u9c7c<\/a><\/p>\n\n\n\n<p><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-cyan-bluish-gray-color\">\u539f\u521b\u4e0d\u6613\uff0c\u8f6c\u8f7d\u8bf7\u6807\u660e\u51fa\u5904\u53ca\u539f\u4f5c\u8005\u3002<\/mark><\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/www.laoyulaoyu.com\/wp-content\/uploads\/2025\/01\/1_ndEiEiJfROf7zg5nRKTXnQ.webp\" alt=\"\" class=\"wp-image-3932\"\/><\/figure>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<pre class=\"wp-block-verse\"><strong>\u5199\u5728\u524d\u9762\u7684\u8bdd\uff1a<\/strong>\u5728\u8fd9\u7bc7\u6587\u7ae0\u91cc\uff0c\u6211\u8be6\u7ec6\u5206\u4eab\u4e86<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">\u5982\u4f55\u7528Python\u4f18\u5316\u7b97\u6cd5\u4ea4\u6613\u4e2d\u7684RSI\u6307\u6807\u3002\u901a\u8fc7\u8c03\u6574\u5468\u671f\u3001\u8d85\u4e70\/\u8d85\u5356\u9608\u503c\u548c\u6b62\u635f\u767e\u5206\u6bd4\uff0c\u663e\u8457\u63d0\u5347\u4e86\u4ea4\u6613\u7b56\u7565\u7684\u8868\u73b0<\/mark>\u3002\u7ed3\u5408\u82f9\u679c\u80a1\u7968\u6570\u636e\u7684\u56de\u6d4b\u7ed3\u679c\uff0c\u6548\u679c\u4e00\u76ee\u4e86\u7136\uff01\u5982\u679c\u4f60\u4e5f\u60f3\u63d0\u9ad8\u4ea4\u6613\u7ee9\u6548\uff0c\u8fd9\u4e9b\u5b9e\u7528\u6280\u5de7\u7edd\u5bf9\u4e0d\u5bb9\u9519\u8fc7\uff0c\u8d76\u7d27\u6765\u5b66\u4e60\u5427\uff01<\/pre>\n<\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u4e00\u3001\u4f18\u5316 RSI \u6307\u6807<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><br><strong>1.1 \u4ec0\u4e48\u662f RSI \uff1f<\/strong><\/h3>\n\n\n\n<p>RSI \uff08Relative Strength Index\uff09\u662f\u4e00\u79cd\u52a8\u91cf\u9707\u8361\u6307\u6807\uff0c\u7528\u4e8e\u6d4b\u91cf\u4ef7\u683c\u53d8\u52a8\u7684\u901f\u5ea6\u548c\u53d8\u5316\u3002\u5b83\u5728 0 \u548c 100 \u4e4b\u95f4\u9707\u8361\uff0c\u901a\u5e38\u5728\u56fe\u8868\u4e0a\u663e\u793a\u4e3a\u4e00\u6761\u7ebf\uff0c\u968f\u7740\u8d44\u4ea7\u4ef7\u683c\u7684\u53d8\u52a8\u800c\u53d8\u52a8\u3002<\/p>\n\n\n\n<p><strong>RSI \u7684\u8ba1\u7b97\u516c\u5f0f\u4e3a\uff1aRSI = 100 \u00d7 RS \/ (1 + RS)\uff0c<\/strong>\u5176\u4e2dRS = N\u5929\u5185\u6536\u5e02\u4ef7\u4e0a\u6da8\u6570\u4e4b\u548c\u7684\u5e73\u5747\u503c \/ N\u5929\u5185\u6536\u5e02\u4ef7\u4e0b\u8dcc\u6570\u4e4b\u548c\u7684\u5e73\u5747\u503c\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/www.laoyulaoyu.com\/wp-content\/uploads\/2025\/01\/image-95.png\" alt=\"\" class=\"wp-image-3933\"\/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1.2 RSI \u5e94\u7528<\/strong><\/h3>\n\n\n\n<p>RSI \u662f\u4e00\u79cd\u52a8\u91cf\u9707\u8361\u6307\u6807\uff0c\u901a\u8fc7\u8bc4\u4f30\u4ef7\u683c\u53d8\u52a8\u7684\u901f\u5ea6\u548c\u5e45\u5ea6\uff0c\u5e2e\u52a9\u5224\u65ad\u5e02\u573a\u7684\u8d85\u4e70\u6216\u8d85\u5356\u72b6\u6001\u3002\u5f53 RSI \u4f4e\u4e8e 30 \u65f6\uff0c\u53ef\u80fd\u6697\u793a\u7740\u4e70\u5165\u673a\u4f1a\uff1b\u800c\u5f53 RSI \u8d85\u8fc7 70\uff0c\u5219\u53ef\u80fd\u662f\u5356\u51fa\u7684\u4fe1\u53f7\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/www.laoyulaoyu.com\/wp-content\/uploads\/2025\/01\/image-96.png\" alt=\"\" class=\"wp-image-3934\"\/><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u8d85\u4e70\u4e0e\u8d85\u5356\uff1a<\/strong>\u901a\u5e38\uff0cRSI\u503c\u8d85\u8fc770\u88ab\u8ba4\u4e3a\u5e02\u573a\u5904\u4e8e\u8d85\u4e70\u72b6\u6001\uff0c\u53ef\u80fd\u9762\u4e34\u56de\u8c03\u6216\u4e0b\u8dcc\u538b\u529b\uff1b\u800cRSI\u503c\u4f4e\u4e8e30\u5219\u88ab\u89c6\u4e3a\u8d85\u5356\u72b6\u6001\uff0c\u53ef\u80fd\u5373\u5c06\u51fa\u73b0\u53cd\u5f39\u6216\u4e0a\u6da8\u3002<\/li>\n\n\n\n<li><strong>\u8d8b\u52bf\u5224\u65ad\uff1a<\/strong>RSI\u503c\u5927\u4e8e50\u901a\u5e38\u8868\u793a\u5e02\u573a\u5904\u4e8e\u591a\u65b9\u5e02\u573a\uff08\u5373\u4e0a\u6da8\u8d8b\u52bf\uff09\uff0c\u800c\u5c0f\u4e8e50\u5219\u8868\u793a\u5e02\u573a\u5904\u4e8e\u7a7a\u65b9\u5e02\u573a\uff08\u5373\u4e0b\u8dcc\u8d8b\u52bf\uff09\u3002<\/li>\n\n\n\n<li><strong>\u4ea4\u53c9\u4fe1\u53f7\uff1a<\/strong>\u77ed\u671fRSI\uff08\u59826\u65e5RSI\uff09\u4e0e\u957f\u671fRSI\uff08\u598212\u65e5RSI\uff09\u7684\u4ea4\u53c9\u60c5\u51b5\u53ef\u4ee5\u63d0\u4f9b\u4ea4\u6613\u4fe1\u53f7\u3002\u5f53\u77ed\u671fRSI\u4ece\u4e0b\u5411\u4e0a\u7a81\u7834\u957f\u671fRSI\u65f6\uff0c\u89c6\u4e3a\u4e70\u5165\u4fe1\u53f7\uff1b\u53cd\u4e4b\uff0c\u4ece\u4e0a\u5411\u4e0b\u7a81\u7834\u65f6\uff0c\u5219\u4e3a\u5356\u51fa\u4fe1\u53f7\u3002<\/li>\n\n\n\n<li><strong>\u80cc\u79bb\u73b0\u8c61\uff1a<\/strong>\u5f53\u8d44\u4ea7\u4ef7\u683c\u7ee7\u7eed\u4e0a\u6da8\u800cRSI\u4e0d\u518d\u521b\u65b0\u9ad8\uff0c\u6216\u4ef7\u683c\u7ee7\u7eed\u4e0b\u8dcc\u800cRSI\u4e0d\u518d\u521b\u65b0\u4f4e\u65f6\uff0c\u8fd9\u79cd\u80cc\u79bb\u73b0\u8c61\u53ef\u80fd\u9884\u793a\u7740\u5e02\u573a\u8d8b\u52bf\u7684\u5373\u5c06\u9006\u8f6c\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1.3 <strong>\u4f18\u5316<\/strong><\/strong><strong>RSI <\/strong><\/h3>\n\n\n\n<p>\u5982\u679c\u8fd0\u7528\u5f97\u5f53\uff0cRSI \u6307\u6807\u80fd\u591f\u901a\u8fc7\u7cbe\u51c6\u8bc6\u522b\u4e70\u5356\u70b9\uff0c\u663e\u8457\u63d0\u5347\u7b97\u6cd5\u4ea4\u6613\u7b56\u7565\u7684\u76c8\u5229\u80fd\u529b\u3002\u7136\u800c\uff0c\u9ed8\u8ba4\u53c2\u6570\uff08\u6bd4\u5982\u5e38\u89c1\u7684 14 \u5929\u5468\u671f\uff09\u5e76\u4e0d\u9002\u5408\u6240\u6709\u5e02\u573a\u6216\u8d44\u4ea7\uff0c\u4f18\u5316\u8c03\u6574\u624d\u80fd\u53d1\u6325\u6700\u5927\u6548\u679c\u3002\u4f18\u5316 RSI \u6d89\u53ca\u8c03\u6574\u5176\u53c2\u6570\uff0c\u6bd4\u5982\u5468\u671f\u548c\u8d85\u4e70\/\u8d85\u5356\u9608\u503c\uff0c\u4ee5\u63d0\u9ad8\u7b56\u7565\u6027\u80fd\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/www.laoyulaoyu.com\/wp-content\/uploads\/2025\/01\/\u5982\u4f55\u7ba1\u7406\u957f\u671f\u7684\u80a1\u7968\u6295\u8d44\u7ec4\u5408-visual-selection.png\" alt=\"\" class=\"wp-image-3936\"\/><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>RSI \u5468\u671f\uff1a<\/strong>\u8c03\u6574\u5468\u671f\u957f\u5ea6\u4f1a\u6781\u5927\u5730\u5f71\u54cd\u6307\u6807\u5bf9\u4ef7\u683c\u53d8\u52a8\u7684\u53cd\u5e94\u80fd\u529b\u3002\u5468\u671f\u8d8a\u77ed\u53ef\u80fd\u8d8a\u654f\u611f\uff0c\u4ea7\u751f\u7684\u4fe1\u53f7\u8d8a\u591a\uff0c\u4f46\u566a\u97f3\u4e5f\u8d8a\u5927\u3002\u5468\u671f\u8d8a\u957f\uff0c\u4fe1\u53f7\u8d8a\u5e73\u6ed1\uff0c\u4f46\u53ef\u80fd\u4f1a\u9519\u8fc7\u5e02\u573a\u65b9\u5411\u7684\u5feb\u901f\u53d8\u5316\u3002<\/li>\n\n\n\n<li><strong>\u8d85\u4e70\/\u8d85\u5356\u9608\u503c\uff1a<\/strong>\u6807\u51c6\u7684\u8d85\u4e70\/\u8d85\u5356\u6c34\u5e73\u662f 70 \u548c 30\uff0c\u4f46\u901a\u8fc7\u5386\u53f2\u6570\u636e\u4f18\u5316\u8fd9\u4e9b\u9608\u503c\uff0c\u53ef\u4ee5\u66f4\u7cbe\u51c6\u5730\u6355\u6349\u5e02\u573a\u72b6\u6001\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u5982\u679c\u4f60\u80fd\u6df1\u5165\u7406\u89e3\u8fd9\u4e24\u4e2a\u6307\u6807\uff0c\u5e76\u638c\u63e1\u4f18\u5316\u5b83\u4eec\u6027\u80fd\u7684\u65b9\u6cd5\uff0c\u5c31\u80fd\u7075\u6d3b\u8fd0\u7528 RSI\uff0c\u5b9e\u73b0\u4ea4\u6613\u5229\u6da6\u7684\u6700\u5927\u5316\uff01<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u4e8c\u3001Python \u4f18\u5316 RSI<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><br><strong>2.1 \u8ba1\u7b97 RSI<\/strong><\/h3>\n\n\n\n<p>\u4e0b\u56fe\u4e3a\u82f9\u679c\uff08AAPL\uff09\u80a1\u7968\u4ef7\u683c\u4e0e RSI \u7684\u5bf9\u6bd4\u56fe\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/www.laoyulaoyu.com\/wp-content\/uploads\/2025\/01\/image-99.png\" alt=\"\" class=\"wp-image-3938\"\/><\/figure>\n\n\n\n<p>\u6211\u4eec\u53ef\u7528\u4f7f\u7528\u4e0b\u9762\u7684 Python \u4ee3\u7801\u5b9e\u73b0\uff0c\u5e76\u6839\u636e\u9700\u8981\u8c03\u6574\u65f6\u95f4\u6bb5\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import yfinance as yf\nimport pandas as pd\nimport matplotlib.pyplot as plt\n\ndef calculate_rsi(data, period=14):\n    \"\"\"\n    Calculate the Relative Strength Index (RSI).\n    :param data: DataFrame with 'Close' column containing closing prices.\n    :param period: Period for calculating RSI, default is 14.\n    :return: DataFrame with RSI values.\n    \"\"\"\n    delta = data&#91;'Close'].diff(1)\n    gain = delta.where(delta &gt; 0, 0)\n    loss = -delta.where(delta &lt; 0, 0)\n\n    avg_gain = gain.rolling(window=period).mean()\n    avg_loss = loss.rolling(window=period).mean()\n\n    rs = avg_gain \/ avg_loss\n    rsi = 100 - (100 \/ (1 + rs))\n    data&#91;'RSI'] = rsi\n\n    return data\n\ndef plot_rsi(data, ticker):\n    \"\"\"\n    Plot closing prices and RSI.\n    :param data: DataFrame with 'Close' and 'RSI' columns.\n    :param ticker: Stock ticker symbol for the title.\n    \"\"\"\n    fig, axes = plt.subplots(2, figsize=(12, 8), sharex=True)\n\n    # Plot closing price\n    axes&#91;0].plot(data.index, data&#91;'Close'], label='Close Price')\n    axes&#91;0].set_title(f'{ticker} Closing Prices')\n    axes&#91;0].set_ylabel('Price')\n    axes&#91;0].legend()\n\n    # Plot RSI\n    axes&#91;1].plot(data.index, data&#91;'RSI'], label='RSI', color='orange')\n    axes&#91;1].axhline(70, color='red', linestyle='--', label='Overbought (70)')\n    axes&#91;1].axhline(30, color='green', linestyle='--', label='Oversold (30)')\n    axes&#91;1].set_title(f'{ticker} RSI')\n    axes&#91;1].set_ylabel('RSI')\n    axes&#91;1].set_xlabel('Date')\n    axes&#91;1].legend()\n\n    plt.tight_layout()\n    plt.show()\n\n# Fetch stock data\nticker = \"AAPL\"  # Replace with your preferred stock ticker\ndata = yf.download(ticker, start=\"2014-01-01\", end=\"2024-01-01\")\ndata_clean = data.copy()\ndata_clean = data_clean.ffill()\ndata_clean.columns = &#91;'Adj Close','Close','High','Low','Open','Volume']\ndata = data_clean.copy()\n\n# Calculate RSI\ndata = calculate_rsi(data)\n\n# Plot RSI\nplot_rsi(data, ticker)<\/code><\/pre>\n\n\n\n<p><code>calculate_rsi<\/code>&nbsp;\u51fd\u6570\u4f1a\u6839\u636e\u6307\u5b9a\u65f6\u95f4\u6bb5\uff08\u9ed8\u8ba4 14 \u5929\uff09\u7684\u80a1\u7968\u6536\u76d8\u4ef7\u8ba1\u7b97 RSI \u503c\u3002\u5b83\u901a\u8fc7\u6bd4\u8f83\u5e73\u5747\u6da8\u5e45\u548c\u5e73\u5747\u8dcc\u5e45\u7684\u6bd4\u4f8b\uff0c\u751f\u6210\u4e00\u4e2a 0 \u5230 100 \u4e4b\u95f4\u7684\u9707\u8361\u6307\u6807\uff0c\u5e2e\u52a9\u5224\u65ad\u5e02\u573a\u72b6\u6001\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"f537\"><strong>2.2 \u56de\u6d4b RSI \u4ea4\u6613\u7b56\u7565<\/strong><\/h3>\n\n\n\n<p id=\"31b4\">\u8ba1\u7b97\u51fa RSI \u503c\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u56de\u6eaf\u6d4b\u8bd5\u6a21\u62df\u7b56\u7565\u7684\u5386\u53f2\u8868\u73b0\u3002\u5177\u4f53\u89c4\u5219\u662f\uff1a\u5f53 RSI \u4ece\u4e0b\u65b9\u7a81\u7834\u8d85\u5356\u6c34\u5e73\uff08\u901a\u5e38\u4e3a 30\uff09\u65f6\u4e70\u5165\uff0c\u5f53 RSI \u4ece\u4e0a\u65b9\u8dcc\u7834\u8d85\u4e70\u6c34\u5e73\uff08\u901a\u5e38\u4e3a 70\uff09\u65f6\u5356\u51fa\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def backtest_rsi(data, rsi_period=14, overbought=70, oversold=30, stop_loss_pct=0.05, initial_balance=10000):\n    data = calculate_rsi(data, period=rsi_period)\n    balance = initial_balance\n    position = 0\n    entry_price = 0\n\n    for i in range(1, len(data)):\n        row = data.iloc&#91;i]\n        prev_row = data.iloc&#91;i - 1]\n\n        # Buy signal: RSI crosses above the oversold level\n        if position == 0 and prev_row&#91;'RSI'] &lt; oversold and row&#91;'RSI'] &gt;= oversold:\n            position = balance \/ row&#91;'Close']\n            entry_price = row&#91;'Close']\n            balance = 0\n\n        # Sell signal: RSI crosses below the overbought level or stop-loss triggered\n        elif position &gt; 0:\n            stop_loss_price = entry_price * (1 - stop_loss_pct)\n            if (row&#91;'RSI'] &lt;= overbought and prev_row&#91;'RSI'] &gt; overbought) or row&#91;'Close'] &lt;= stop_loss_price:\n                balance = position * row&#91;'Close']\n                position = 0\n\n    if position &gt; 0:\n        balance += position * data.iloc&#91;-1]&#91;'Close']\n\n    return balance<\/code><\/pre>\n\n\n\n<p id=\"69a5\">\u5728\u8fd9\u4e2a\u529f\u80fd\u4e2d\uff0c\u6211\u4eec\u5c06\u4f7f\u7528\u5386\u53f2\u80a1\u7968\u6570\u636e\u8fdb\u884c\u56de\u6eaf\u6d4b\u8bd5\u3002\u6a21\u62df\u7b56\u7565\u4ece\u521d\u59cb\u8d44\u91d1\uff08\u4f8b\u5982 10,000 \u7f8e\u5143\uff09\u5f00\u59cb\uff0c\u6839\u636e RSI \u4fe1\u53f7\u6267\u884c\u4e70\u5356\u64cd\u4f5c\u3002\u4e3a\u4e86\u6709\u6548\u7ba1\u7406\u98ce\u9669\uff0c\u6211\u4eec\u8fd8\u52a0\u5165\u4e86\u6b62\u635f\u529f\u80fd\uff1a\u5f53\u4ef7\u683c\u8dcc\u7834\u5165\u573a\u4ef7\u7684\u6307\u5b9a\u767e\u5206\u6bd4\u65f6\uff0c\u81ea\u52a8\u5356\u51fa\uff0c\u907f\u514d\u8fdb\u4e00\u6b65\u4e8f\u635f\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\" id=\"af69\"><img decoding=\"async\" src=\"https:\/\/cdn-images-1.readmedium.com\/v2\/resize:fit:800\/1*-VSQhVQ0X_FocDvHUwluiQ.png\" alt=\"\"\/><\/figure>\n\n\n\n<p id=\"6bdd\">\u6839\u636e\u9ed8\u8ba4\u53c2\u6570\uff08RSI \u5468\u671f=14\u3001\u8d85\u4e70\u6c34\u5e73=70\u3001\u8d85\u5356\u6c34\u5e73=30\uff09\u7684\u56de\u6d4b\u7ed3\u679c\uff0c\u521d\u59cb\u8d44\u91d1\u4ece 10,000 \u7f8e\u5143\u4ec5\u589e\u957f\u5230 10,167 \u7f8e\u5143\uff0c\u8868\u73b0\u5e73\u5e73\uff0c\u751a\u81f3\u4e0d\u5982\u7b80\u5355\u7684\u201c\u4e70\u5165\u5e76\u6301\u6709\u201d\u7b56\u7565\u3002\u663e\u7136\uff0c\u6211\u4eec\u9700\u8981\u4f18\u5316\u53c2\u6570\uff0c\u627e\u5230\u66f4\u4f73\u7684\u7ec4\u5408\u6765\u63d0\u5347\u7b56\u7565\u6548\u679c\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"2234\"><strong>2.3 \u4f18\u5316 RSI \u53c2\u6570<\/strong><\/h3>\n\n\n\n<p id=\"3cef\">\u4e0b\u4e00\u6b65\u662f\u627e\u5230 RSI \u5173\u952e\u53c2\u6570\u7684\u6700\u4f73\u503c\uff0c\u5982 RSI \u5468\u671f\u3001\u8d85\u4e70\u548c\u8d85\u5356\u6c34\u5e73\uff0c\u4ee5\u53ca\u6b62\u635f\u767e\u5206\u6bd4\u3002\u6211\u4eec\u901a\u8fc7\u4e00\u4e2a\u4f18\u5316\u51fd\u6570\u6765\u63a2\u7d22\u8fd9\u4e9b\u53c2\u6570\u7684\u5404\u79cd\u7ec4\u5408\uff0c\u4ece\u800c\u5b9e\u73b0\u8fd9\u4e00\u76ee\u6807\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from itertools import product\n\ndef optimize_rsi(data, rsi_periods, overbought_levels, oversold_levels, stop_loss_pcts):\n    results = &#91;]\n    for rsi_period, overbought, oversold, stop_loss_pct in product(rsi_periods, overbought_levels, oversold_levels, stop_loss_pcts):\n        if overbought &lt;= oversold:\n            continue  # Skip invalid combinations\n\n        final_balance = backtest_rsi(data, rsi_period, overbought, oversold, stop_loss_pct)\n        results.append({\n            'RSI Period': rsi_period,\n            'Overbought Level': overbought,\n            'Oversold Level': oversold,\n            'Stop Loss %': stop_loss_pct,\n            'Final Balance': final_balance\n        })\n\n    results_df = pd.DataFrame(results)\n    return results_df\n\n# Optimize RSI parameters\nrsi_periods = range(5, 21, 1)  # RSI periods from 5 to 20\noverbought_levels = range(65, 86, 5)  # Overbought levels from 65 to 85\noversold_levels = range(15, 36, 5)  # Oversold levels from 15 to 35\nstop_loss_pcts = &#91;0.02, 0.05, 0.1]  # Stop-loss percentages\n\nresults_df = optimize_rsi(data, rsi_periods, overbought_levels, oversold_levels, stop_loss_pcts)\n\n# Find the best parameters\nbest_params = results_df.loc&#91;results_df&#91;'Final Balance'].idxmax()]\nprint(\"Best Parameters:\")\nprint(best_params)\n\n# Plot optimization results\nplot_optimization_results(results_df)<\/code><\/pre>\n\n\n\n<p id=\"84bb\">\u6211\u4eec\u5229\u7528&nbsp;<code>itertools.product<\/code>&nbsp;\u51fd\u6570\u904d\u5386\u6240\u6709\u53ef\u80fd\u7684\u53c2\u6570\u7ec4\u5408\u3002\u9488\u5bf9\u6bcf\u4e00\u79cd\u7ec4\u5408\uff0c\u90fd\u4f1a\u8fd0\u884c\u4e00\u6b21\u56de\u6eaf\u6d4b\u8bd5\uff0c\u5e76\u5c06\u6700\u7ec8\u7684\u8d26\u6237\u4f59\u989d\u8bb0\u5f55\u5728 DataFrame \u4e2d\uff0c\u65b9\u4fbf\u540e\u7eed\u5206\u6790\u548c\u6bd4\u8f83\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"f98c\"><strong>2.4 \u4f18\u5316\u7ed3\u679c\u53ef\u89c6\u5316<\/strong><\/h3>\n\n\n\n<p id=\"3f8f\">\u4f18\u5316\u53c2\u6570\u540e\uff0c\u6211\u4eec\u9700\u8981\u5c06\u7ed3\u679c\u53ef\u89c6\u5316\uff0c\u4ee5\u4fbf\u5feb\u901f\u627e\u5230\u6700\u4f73\u7ec4\u5408\u3002\u6bd4\u5982\uff0c\u53ef\u4ee5\u901a\u8fc7\u521b\u5efa\u70ed\u56fe\u6765\u76f4\u89c2\u5c55\u793a\u4e0d\u540c\u53c2\u6570\u8bbe\u7f6e\u4e0b\u7684\u6700\u7ec8\u8d26\u6237\u4f59\u989d\u53d8\u5316\uff0c\u5e2e\u52a9\u6211\u4eec\u9501\u5b9a\u6700\u6709\u5229\u7684\u7b56\u7565\u914d\u7f6e\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def plot_optimization_results(results_df):\n    pivot = results_df.pivot_table(\n        index='RSI Period',\n        columns='Overbought Level',\n        values='Final Balance',\n        aggfunc='max'\n    )\n\n    plt.figure(figsize=(10, 8))\n    plt.title(\"Optimization Results: Final Balance\")\n    sns.heatmap(pivot, annot=True, fmt=\".2f\", cmap=\"viridis\")\n    plt.xlabel(\"Overbought Level\")\n    plt.ylabel(\"RSI Period\")\n    plt.show()<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image\" id=\"7f22\"><img decoding=\"async\" src=\"https:\/\/cdn-images-1.readmedium.com\/v2\/resize:fit:800\/1*NRFkD3pv4DoXRYtMlhWNwQ.png\" alt=\"\"\/><\/figure>\n\n\n\n<p id=\"fe3a\"><strong>\u6700\u4f73\u5efa\u8bae\u53c2\u6570<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>RSI \u5468\u671f\uff1a12<\/li>\n\n\n\n<li>\u8d85\u4e70\u6c34\u5e73\uff1a85<\/li>\n\n\n\n<li>\u8d85\u5356\u6c34\u5e73\uff1a35<\/li>\n\n\n\n<li>\u6b62\u635f % : 0.1<\/li>\n\n\n\n<li>\u6700\u7ec8\u4f59\u989d : 59341.05<\/li>\n<\/ul>\n\n\n\n<p id=\"9029\">\u8fd9\u5f20\u70ed\u56fe\u5c55\u793a\u4e86\u6700\u7ec8\u8d26\u6237\u4f59\u989d\u5982\u4f55\u968f RSI \u5468\u671f\u548c\u8d85\u4e70\u6c34\u5e73\u7684\u4e0d\u540c\u7ec4\u5408\u800c\u53d8\u5316\u3002\u4f46\u7531\u4e8e\u53ea\u6709\u4e24\u4e2a\u7ef4\u5ea6\uff0c\u8d85\u5356\u6c34\u5e73\u8fd9\u4e00\u5173\u952e\u53c2\u6570\u672a\u80fd\u5145\u5206\u4f53\u73b0\u3002\u4e3a\u4e86\u66f4\u597d\u5730\u5206\u6790\uff0c\u6211\u4eec\u5c06\u5207\u6362\u5230\u4e09\u7ef4\u56fe\uff0c\u5168\u9762\u5c55\u793a\u53c2\u6570\u4e4b\u95f4\u7684\u5173\u7cfb\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"a7f9\"><strong>2.5 \u589e\u5f3a\u53ef\u89c6\u5316\u7684 3D \u7ed8\u56fe<\/strong><\/h3>\n\n\n\n<p id=\"0d70\">\u5982\u679c\u9700\u8981\u66f4\u8be6\u7ec6\u7684\u5206\u6790\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e09\u7ef4\u6563\u70b9\u56fe\uff0c\u5c06\u6700\u7ec8\u8d26\u6237\u4f59\u989d\u4e0e RSI \u5468\u671f\u3001\u8d85\u4e70\u6c34\u5e73\u548c\u8d85\u5356\u6c34\u5e73\u8fdb\u884c\u5bf9\u6bd4\uff0c\u4ece\u800c\u66f4\u5168\u9762\u5730\u8bc4\u4f30\u53c2\u6570\u7ec4\u5408\u7684\u6548\u679c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from mpl_toolkits.mplot3d import Axes3D\nimport matplotlib.pyplot as plt\n\ndef plot_3d_optimization_results(results_df):\n    \"\"\"\n    Plot the optimization results in 3D.\n    \"\"\"\n    fig = plt.figure(figsize=(12, 8))\n    ax = fig.add_subplot(111, projection='3d')\n\n    # Extract data for the plot\n    rsi_period = results_df&#91;'RSI Period']\n    overbought = results_df&#91;'Overbought Level']\n    oversold = results_df&#91;'Oversold Level']\n    final_balance = results_df&#91;'Final Balance']\n\n    # Create a scatter plot\n    scatter = ax.scatter(rsi_period, overbought, oversold, c=final_balance, cmap='viridis', alpha=0.8)\n\n    # Add labels and title\n    ax.set_title('3D Optimization Results: Final Balance', pad=20)\n    ax.set_xlabel('RSI Period', labelpad=10)\n    ax.set_ylabel('Overbought Level', labelpad=10)\n    ax.set_zlabel('Oversold Level', labelpad=10)\n\n    # Add color bar for balance\n    cbar = fig.colorbar(scatter, ax=ax, shrink=0.6, aspect=10)\n    cbar.set_label('Final Balance')\n\n    plt.show()\n\n# Plot the 3D optimization results\nplot_3d_optimization_results(results_df)<\/code><\/pre>\n\n\n\n<p id=\"0266\">\u8fd9\u79cd\u4e09\u7ef4\u56fe\u53ef\u4ee5\u66f4\u8be6\u7ec6\u5730\u663e\u793a\u53c2\u6570\u4e4b\u95f4\u7684\u76f8\u4e92\u4f5c\u7528\u4ee5\u53ca\u6700\u7ec8\u7684\u5e73\u8861\uff0c\u5e2e\u52a9\u4ea4\u6613\u8005\u505a\u51fa\u6570\u636e\u9a71\u52a8\u7684\u51b3\u7b56\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\" id=\"9766\"><img decoding=\"async\" src=\"https:\/\/cdn-images-1.readmedium.com\/v2\/resize:fit:800\/1*ndEiEiJfROf7zg5nRKTXnQ.png\" alt=\"\"\/><\/figure>\n\n\n\n<p id=\"7ff3\">\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e4b\u524d\u7f16\u7801\u7684 backtest_rsi \u51fd\u6570\u67e5\u770b\u66f4\u8be6\u7ec6\u7684\u56de\u6eaf\u6d4b\u8bd5\u7ed3\u679c\uff0c\u5982\u4e0b\u56fe\u6240\u793a\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Backtest with best parameters\n_, _, best_equity_curve = backtest_rsi(\n    data,\n    rsi_period=12,\n    overbought=85,\n    oversold=35,\n    stop_loss_pct=0.1\n)\n\n# Plot equity curve\nplot_equity_curve(best_equity_curve)<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image\" id=\"f363\"><img decoding=\"async\" src=\"https:\/\/cdn-images-1.readmedium.com\/v2\/resize:fit:800\/1*Nn9FIg1nAk_TEHwmjmrUIw.png\" alt=\"\"\/><\/figure>\n\n\n\n<p id=\"81ea\">\u4ece\u4f18\u5316\u540e\u7684\u7ed3\u679c\u6765\u770b\uff0c\u521d\u59cb\u8d44\u91d1\u4ece 10,000 \u7f8e\u5143\u589e\u957f\u5230\u4e86 59,341.05 \u7f8e\u5143\uff0c\u4e14\u6700\u5927\u56de\u64a4\u63a7\u5236\u5728 25% \u4ee5\u5185\uff0c\u8868\u73b0\u76f8\u5f53\u51fa\u8272\u3002\u4e0d\u8fc7\u4ece\u5b9e\u6218\u51fa\u53d1\uff0c\u5982\u679c\u9009\u62e9\u4e70\u5165\u5e76\u6301\u6709 AAPL \u80a1\u7968 10 \u5e74\uff0c\u5c3d\u7ba1\u6ce2\u52a8\u98ce\u9669\u66f4\u9ad8\uff0c\u4f46\u6536\u76ca\u53ef\u80fd\u66f4\u4e3a\u53ef\u89c2\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u4e09\u3001\u89c2\u70b9\u603b\u7ed3<\/strong><\/h2>\n\n\n\n<p>\u5728\u6784\u5efa\u4ea4\u6613\u7cfb\u7edf\u65f6\uff0c\u6709\u4e00\u4e2a\u5173\u952e\u539f\u5219\u5fc5\u987b\u7262\u8bb0\uff1a\u5728\u5c06\u7cfb\u7edf\u6295\u5165\u5b9e\u9645\u4ea4\u6613\u4e4b\u524d\uff0c\u4e00\u5b9a\u8981\u8fdb\u884c\u524d\u5411\u4f18\u5316\u6d4b\u8bd5\u3002\u53ea\u6709\u8fd9\u6837\uff0c\u624d\u80fd\u786e\u4fdd\u60a8\u7684\u7cfb\u7edf\u548c\u53c2\u6570\u5728\u5f53\u524d\u5e02\u573a\u4e2d\u4f9d\u7136\u6709\u6548\uff0c\u907f\u514d\u76f2\u76ee\u90e8\u7f72\u5e26\u6765\u7684\u98ce\u9669\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>RSI \u662f\u8861\u91cf\u4ef7\u683c\u53d8\u52a8\u901f\u5ea6\u548c\u53d8\u5316\u7684\u91cd\u8981\u6307\u6807<\/strong>\uff0c\u5bf9\u4e8e\u8bc6\u522b\u4ea4\u6613\u673a\u4f1a\u81f3\u5173\u91cd\u8981\u3002<\/li>\n\n\n\n<li><strong>\u9ed8\u8ba4\u7684 RSI \u53c2\u6570\u53ef\u80fd\u9700\u8981\u6839\u636e\u5177\u4f53\u5e02\u573a\u548c\u8d44\u4ea7\u8fdb\u884c\u8c03\u6574<\/strong>\uff0c\u4ee5\u63d0\u9ad8\u7b56\u7565\u7684\u6709\u6548\u6027\u3002<\/li>\n\n\n\n<li><strong>\u56de\u6eaf\u6d4b\u8bd5\u662f\u8bc4\u4f30\u548c\u4f18\u5316\u4ea4\u6613\u7b56\u7565\u7684\u5173\u952e\u5de5\u5177<\/strong>\uff0c\u53ef\u4ee5\u5e2e\u52a9\u4ea4\u6613\u8005\u4e86\u89e3\u5386\u53f2\u8868\u73b0\u5e76\u8c03\u6574\u7b56\u7565\u3002<\/li>\n\n\n\n<li><strong>\u4f18\u5316 RSI \u53c2\u6570\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u4ea4\u6613\u7b56\u7565\u7684\u76c8\u5229\u80fd\u529b<\/strong>\uff0c\u5c24\u5176\u662f\u901a\u8fc7\u8c03\u6574 RSI \u5468\u671f\u3001\u8d85\u4e70\/\u8d85\u5356\u9608\u503c\u548c\u6b62\u635f\u673a\u5236\u3002<\/li>\n\n\n\n<li><strong>\u53ef\u89c6\u5316\u4f18\u5316\u7ed3\u679c\u6709\u52a9\u4e8e\u4ea4\u6613\u8005\u505a\u51fa\u66f4\u660e\u667a\u7684\u51b3\u7b56<\/strong>\uff0c\u70ed\u56fe\u548c\u4e09\u7ef4\u6563\u70b9\u56fe\u662f\u4e24\u79cd\u6709\u6548\u7684\u53ef\u89c6\u5316\u65b9\u6cd5\u3002<\/li>\n\n\n\n<li><strong>\u524d\u5411\u4f18\u5316\u662f\u786e\u4fdd\u4ea4\u6613\u7cfb\u7edf\u5728\u5b9e\u9645\u5e02\u573a\u4e2d\u6709\u6548\u6027\u7684\u91cd\u8981\u6b65\u9aa4<\/strong>\uff0c\u5e94\u5728\u5b9e\u9645\u4ea4\u6613\u4e4b\u524d\u8fdb\u884c\u3002<\/li>\n\n\n\n<li><strong>\u4ea4\u6613\u8005\u5e94\u8be5\u6301\u7eed\u5b66\u4e60\u548c\u9002\u5e94\u5e02\u573a\u53d8\u5316<\/strong>\uff0c\u4ee5\u4fdd\u6301\u4ea4\u6613\u7b56\u7565\u7684\u76f8\u5173\u6027\u548c\u76c8\u5229\u80fd\u529b\u3002<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n\n\n\n<p><em>\u611f\u8c22\u60a8\u9605\u8bfb\u5230\u6700\u540e\uff0c\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u4e3a\u60a8\u5e26\u6765\u4e86\u65b0\u7684\u542f\u53d1\u548c\u5b9e\u7528\u7684\u77e5\u8bc6\uff01\u5982\u679c\u89c9\u5f97\u6709\u5e2e\u52a9\uff0c\u8bf7\u4e0d\u541d\u70b9\u8d5e\u548c\u5206\u4eab\uff0c\u60a8\u7684\u652f\u6301\u662f\u6211\u6301\u7eed\u521b\u4f5c\u7684\u52a8\u529b\u3002\u795d\u60a8\u6295\u8d44\u987a\u5229\uff0c\u6536\u76ca\u957f\u8679\uff01\u5982\u679c\u5bf9\u6587\u4e2d\u5185\u5bb9\u6709\u4efb\u4f55\u7591\u95ee\uff0c\u6b22\u8fce\u7559\u8a00\uff0c\u6211\u4f1a\u5c3d\u5feb\u56de\u590d\uff01<\/em><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p class=\"has-text-align-center\" id=\"a1c6\">\u672c\u6587\u5185\u5bb9\u4ec5\u9650\u6280\u672f\u63a2\u8ba8\u548c\u5b66\u4e60\uff0c\u4e0d\u6784\u6210\u4efb\u4f55\u6295\u8d44\u5efa\u8bae\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f5c\u8005\uff1a\u8001\u4f59\u635e\u9c7c \u539f\u521b\u4e0d\u6613\uff0c\u8f6c\u8f7d\u8bf7\u6807\u660e\u51fa\u5904\u53ca\u539f\u4f5c\u8005\u3002&#8230;<\/p>\n<div class=\"more-link-wrapper\"><a class=\"more-link\" href=\"https:\/\/laoyulaoyu.com\/index.php\/2025\/02\/16\/%e7%ae%97%e6%b3%95%e4%ba%a4%e6%98%93%e6%96%b0%e7%aa%81%e7%a0%b4%ef%bc%9apython-%e4%bc%98%e5%8c%96-rsi-%e5%85%a8%e6%94%bb%e7%95%a5%ef%bc%8c%e8%bd%bb%e6%9d%be%e6%8f%90%e5%8d%87%e7%9b%88%e5%88%a9\/\">Continue reading<span class=\"screen-reader-text\">\u7b97\u6cd5\u4ea4\u6613\u65b0\u7a81\u7834\uff1aPython \u4f18\u5316 RSI \u5168\u653b\u7565\uff0c\u8f7b\u677e\u63d0\u5347\u76c8\u5229\u80fd\u529b\uff01<\/span><\/a><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[4,5],"class_list":{"0":"post-1902","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"hentry","6":"category-aiinvest","7":"tag-ai","9":"entry"},"_links":{"self":[{"href":"https:\/\/laoyulaoyu.com\/index.php\/wp-json\/wp\/v2\/posts\/1902","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/laoyulaoyu.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/laoyulaoyu.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/laoyulaoyu.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/laoyulaoyu.com\/index.php\/wp-json\/wp\/v2\/comments?post=1902"}],"version-history":[{"count":1,"href":"https:\/\/laoyulaoyu.com\/index.php\/wp-json\/wp\/v2\/posts\/1902\/revisions"}],"predecessor-version":[{"id":1903,"href":"https:\/\/laoyulaoyu.com\/index.php\/wp-json\/wp\/v2\/posts\/1902\/revisions\/1903"}],"wp:attachment":[{"href":"https:\/\/laoyulaoyu.com\/index.php\/wp-json\/wp\/v2\/media?parent=1902"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/laoyulaoyu.com\/index.php\/wp-json\/wp\/v2\/categories?post=1902"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/laoyulaoyu.com\/index.php\/wp-json\/wp\/v2\/tags?post=1902"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}