{"id":1556,"date":"2024-10-27T07:03:00","date_gmt":"2024-10-26T23:03:00","guid":{"rendered":"https:\/\/blog.laoyulaoyu.top\/?p=1556"},"modified":"2024-10-03T12:10:18","modified_gmt":"2024-10-03T04:10:18","slug":"%e5%bf%85%e7%9c%8b%ef%bc%81%e5%9c%a8%e8%af%86%e5%88%ab%e8%82%a1%e5%b8%82%e8%b6%8b%e5%8a%bf%e4%b8%8a%ef%bc%8c%e5%93%aa%e7%a7%8d%e6%8a%80%e6%9c%af%e5%88%86%e6%9e%90%e6%96%b9%e6%b3%95%e6%9c%80%e7%89%9b","status":"publish","type":"post","link":"https:\/\/laoyulaoyu.com\/index.php\/2024\/10\/27\/%e5%bf%85%e7%9c%8b%ef%bc%81%e5%9c%a8%e8%af%86%e5%88%ab%e8%82%a1%e5%b8%82%e8%b6%8b%e5%8a%bf%e4%b8%8a%ef%bc%8c%e5%93%aa%e7%a7%8d%e6%8a%80%e6%9c%af%e5%88%86%e6%9e%90%e6%96%b9%e6%b3%95%e6%9c%80%e7%89%9b\/","title":{"rendered":"\u5fc5\u770b\uff01\u5728\u8bc6\u522b\u80a1\u5e02\u8d8b\u52bf\u4e0a\uff0c\u54ea\u79cd\u6280\u672f\u5206\u6790\u65b9\u6cd5\u6700\u725b\uff1f"},"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\/2024\/10\/1.png\" alt=\"\" class=\"wp-image-2322\"\/><\/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>\u672c\u6587<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">\u6bd4\u8f83\u4e86\u516d\u79cd\u6280\u672f\u5206\u6790\u65b9\u6cd5\uff0c\u7528\u4e8e\u8bc6\u522b\u80a1\u5e02\u8d8b\u52bf<\/mark>\u3002\u5e76\u901a\u8fc7Python\u4ee3\u7801\u5206\u6790\u4e86\u8fd9\u4e9b\u65b9\u6cd5\u5728SPY\u4ea4\u6613 traded fund\uff08ETF\uff09\u4e0a\u7684\u5e94\u7528\u548c\u6548\u679c\uff0c\u6700\u7ec8\u5f97\u51fa\u4e86\u5404\u79cd\u65b9\u6cd5\u7684\u56de\u62a5\u7387\u548c\u7edf\u8ba1\u6570\u636e\uff0c\u4ee5\u53ca\u5bf9\u6bd4\u4e86\u5b83\u4eec\u5728\u5176\u4ed6\u51e0\u53ea\u80a1\u7968\u4e0a\u7684\u8868\u73b0\u3002<\/pre>\n<\/blockquote>\n\n\n\n<p>\u5728\u6211\u524d\u9762\u53d1\u5e03\u7684\u4e0d\u5c11\u6587\u7ae0\u4e2d\u90fd\u6709\u4e0d\u5c11\u8bfb\u8005\u7559\u8a00\u8be2\u95ee\uff0c\u5230\u5e95\u7528\u54ea\u79cd\u6280\u672f\u65b9\u6cd5\u8bc6\u522b\u80a1\u7968\u7684\u53d1\u5c55\u8d8b\u52bf\u6700\u597d\uff1f\u8bc6\u522b\u8d8b\u52bf\u5728\u5728\u4ea4\u6613\u4e2d\u81f3\u5173\u91cd\u8981\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u5e2e\u52a9\u4ea4\u6613\u8005\u987a\u5e94\u5e02\u573a\u8d8b\u52bf\uff0c\u6700\u5927\u9650\u5ea6\u5730\u63d0\u9ad8\u5229\u6da6\uff0c\u964d\u4f4e\u98ce\u9669\u3002\u901a\u8fc7\u8bc6\u522b\u5e02\u573a\u65b9\u5411\uff0c\u4ea4\u6613\u8005\u53ef\u4ee5\u5bf9\u8fdb\u5165\u548c\u9000\u51fa\u70b9\u505a\u51fa\u660e\u667a\u7684\u51b3\u5b9a\uff0c\u4ece\u800c\u6539\u8fdb\u6574\u4f53\u7b56\u7565\uff0c\u907f\u514d\u5728\u6ce2\u52a8\u6761\u4ef6\u4e0b\u72af\u4e0b\u4ee3\u4ef7\u9ad8\u6602\u7684\u9519\u8bef\u3002\u4e5f\u8bb8\u770b\u5b8c\u672c\u6587\u60a8\u4f1a\u5f97\u5230\u81ea\u5df1\u60f3\u8981\u7684\u7b54\u6848\u3002<\/p>\n\n\n\n<p><strong>\u5728\u672c\u6587\u4e2d\uff0c\u60a8\u53ef\u4ee5\u770b\u5230\u4ee5\u4e0b\u5185\u5bb9\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u63cf\u8ff0\u4f7f\u7528\u6307\u6807\u8bc6\u522b\u8d8b\u52bf\u7684\u6700\u5e38\u7528\u76846\u79cd\u6280\u672f\u5206\u6790\u65b9\u6cd5\uff1a\u5feb\u6162\u79fb\u52a8\u5e73\u5747\u7ebf\u3001\u79fb\u52a8\u5e73\u5747\u7ebf+MACD\u3001RSI+\u5feb\u6162\u79fb\u52a8\u5e73\u5747\u7ebf\u3001\u5e03\u6797\u7ebf\u548cRSI\u3001ADX\u4e0e\u5feb\u6162\u79fb\u52a8\u5e73\u5747\u7ebf\u4ee5\u53ca\u4e00\u76ee\u5747\u8861\u4e91\u56fe\u548cMACD\u3002<\/li>\n\n\n\n<li>\u6bcf\u79cd\u65b9\u6cd5\u90fd\u901a\u8fc7Python\u4ee3\u7801\u5b9e\u73b0\uff0c\u4ee5\u8d8b\u52bf\u4e3a\u4fe1\u53f7\uff0c\u627e\u51fa\u6bcf\u79cd\u65b9\u6cd5\u7684\u603b\u56de\u62a5\u3002(\u6211\u5c06\u4f7f\u7528\u6bcf\u4e2a\u6307\u6807\u7684\u5e02\u573a\u5b9e\u8df5\u53c2\u6570\uff09\u3002<\/li>\n\n\n\n<li>\u4ee5\u6b64\u6765\u8ba1\u7b97\u603b\u56de\u62a5\u548c\u5176\u4ed6\u7edf\u8ba1\u6570\u636e\uff0c\u5982\u6700\u5927\u56de\u64a4\u3001\u590f\u666e\u6bd4\u7387\u7b49\uff0c\u5e76\u6bd4\u8f83\u5404\u79cd\u65b9\u6cd5\u7684\u7edf\u8ba1\u6570\u636e\u3002<\/li>\n\n\n\n<li>\u8fd0\u7528\u4e0a\u8ff0\u77e5\u8bc6\u8ba1\u7b97\u80a1\u7968\u6570\u91cf\u5e76\u7ed9\u51fa\u56fe\u8868\u5c55\u793a\u4ee5\u53ca\u76f8\u5e94\u7684\u5206\u6790\u7ed3\u679c\u3002<\/li>\n\n\n\n<li>\u6700\u540e\u56de\u7b54\u95ee\u9898\uff0c\u54ea\u79cd\u65b9\u6cd5\u662f\u8d62\u5bb6\uff1f<\/li>\n<\/ul>\n\n\n\n<p><strong>\u8bf7\u6ce8\u610f\uff0c\u5728\u672c\u6587\u4e2d\u6211\u4e0d\u4f1a\u5c31 \u201c\u4e70\u5165\u5e76\u6301\u6709\u201d \u5c55\u5f00\u4efb\u4f55\u6bd4\u8f83\u3002\u56e0\u6211\u7684\u5206\u6790\u8303\u7574\u662f\u53bb\u4e86\u89e3\u5e76\u5bf9\u6bd4\u5404\u7c7b\u65b9\u6cd5\uff0c\u800c\u4e0d\u662f\u5236\u5b9a\u201c\u81f4\u80dc\u7b56\u7565\u201d\u7684\u5de5\u4f5c\u3002<\/strong><\/p>\n\n\n\n<p>\u5728\u6211\u4eec\u6df1\u5165\u7814\u7a76\u6bcf\u79cd\u65b9\u6cd5\u4e4b\u524d\uff0c\u5982\u679c\u4f60\u60f3\u770b\u770b\u7528 Python \u662f\u5982\u4f55\u5b8c\u6210\u8be5\u9879\u5de5\u4f5c\u7684\uff0c\u53ef\u4ee5\u7ee7\u7eed\u5f80\u4e0b\u8bfb\u3002\u6216\u5219\u60a8\u53ef\u4ee5\u76f4\u63a5\u8df3\u8fc7\u4ee3\u7801\u5b9e\u73b0\u7684\u73af\u8282\uff0c\u76f4\u63a5\u53bb\u770b\u5206\u6790\u7ed3\u679c\uff0c\u4f46\u60a8\u4ecd\u7136\u4f1a\u53d1\u73b0\u4e00\u4e9b\u5206\u6790\u4f1a\u5f88\u6709\u8da3\uff01<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u4e00\u3001\u524d\u671f\u51c6\u5907\u5de5\u4f5c<\/strong><\/h3>\n\n\n\n<p>\u6211\u4eec\u5c06\u5bfc\u5165\u6574\u4e2a\u4ee3\u7801\u6240\u9700\u7684\u5e93\uff0c\u5e76\u4e0b\u8f7d\u5305\u542b SPY \u4ef7\u683c\u7684\u4e3b\u6570\u636e\u5e27\u3002\u6211\u4f7f\u7528 SPY\uff0c\u56e0\u4e3a\u5bf9\u6211\u6765\u8bf4\uff0c\u5728\u5206\u6790\u4e2a\u80a1\u4e4b\u524d\uff0c\u6700\u91cd\u8981\u7684\u8d8b\u52bf\u8bc6\u522b\u5c31\u662f\u4e86\u89e3\u5e02\u573a\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code># Import necessary libraries\nimport yfinance as yf\nimport pandas as pd\nimport pandas_ta as ta\nimport matplotlib.pyplot as plt\nimport numpy as np\n\n# Download the stock data\nticker = 'SPY' \ndf = yf.download(ticker, start='2023-01-01', end='2024-06-01')<\/code><\/code><\/pre>\n\n\n\n<p>\u7136\u540e\uff0c\u6211\u4eec\u6765\u521b\u5efa\u4e00\u4e2a\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u5c06\u5728\u4ee3\u7801\u7684\u5176\u4f59\u90e8\u5206\u4e2d\u7528\u4e8e\u83b7\u53d6\u8d8b\u52bf\u548c\u4e00\u4e9b\u7edf\u8ba1\u6570\u636e<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>def calculate_returns(df_for_returns, col_for_returns = 'Close', col_for_signal = 'Trend'):\n\n    stats = {}\n\n    # Calculate daily returns\n    df_for_returns&#91;'Daily_Returns'] = df&#91;col_for_returns].pct_change()\n    df_for_returns&#91;'Returns'] = df_for_returns&#91;'Daily_Returns'] * df_for_returns&#91;col_for_signal].shift(1)\n    df_for_returns&#91;'Returns'] = df_for_returns&#91;'Returns'].fillna(0)\n    df_for_returns&#91;'Equity Curve'] = 100 * (1 + df_for_returns&#91;'Returns']).cumprod()\n\n    equity_curve = df_for_returns&#91;'Equity Curve']\n    # Calculate the running maximum of the equity curve\n    cumulative_max = equity_curve.cummax()\n    drawdown = (equity_curve - cumulative_max) \/ cumulative_max\n    stats&#91;'max_drawdown'] = drawdown.min()\n\n    # calculate the sharpe ratio\n    stats&#91;'sharpe_ratio'] = (df_for_returns&#91;'Returns'].mean() \/ df_for_returns&#91;'Returns'].std()) * np.sqrt(252)\n\n    # calculate the total return\n    stats&#91;'total_return'] = (equity_curve.iloc&#91;-1] \/ equity_curve.iloc&#91;0]) - 1\n\n    # calculate the number of long signals\n    stats&#91;'number_of_long_signals'] = len(df_for_returns&#91;df_for_returns&#91;col_for_signal] == 1])\n\n    # calculate the number of short signals\n    stats&#91;'number_of_short_signals'] = len(df_for_returns&#91;df_for_returns&#91;col_for_signal] == -1])\n\n    return df_for_returns&#91;'Equity Curve'], stats<\/code><\/code><\/pre>\n\n\n\n<p>\u73b0\u5728\u6211\u4eec\u51c6\u5907\u597d\u4e86\uff01\u5f00\u59cb\u5427\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u4e8c\u3001\u5feb\u6162\u79fb\u52a8\u5e73\u5747\u7ebf<br>Fast and Slow Moving Averages<\/strong><\/h3>\n\n\n\n<p>\u8fd9\u79cd\u65b9\u6cd5\u662f\u6700\u57fa\u672c\u7684\u8d8b\u52bf\u5206\u6790\u65b9\u6cd5\uff0c\u4e5f\u662f\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u3002\u4f60\u6709\u4e24\u6761\u79fb\u52a8\u5e73\u5747\u7ebf\u3002\u5f53\u5feb\u901f\u79fb\u52a8\u5e73\u5747\u7ebf\u9ad8\u4e8e\u6162\u901f\u79fb\u52a8\u5e73\u5747\u7ebf\u65f6\uff0c\u8868\u660e\u8d8b\u52bf\u5411\u4e0a\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>def calculate_trend_2_ma(df_ohlc, period_slow=21, period_fast=9):\n    # Calculate Moving Averages (fast and slow) using pandas_ta\n    df_ohlc&#91;'MA_Fast'] = df_ohlc.ta.sma(close='Close', length=period_fast)\n    df_ohlc&#91;'MA_Slow'] = df_ohlc.ta.sma(close='Close', length=period_slow)\n\n    # Determine the trend based on Moving Averages\n    def identify_trend(row):\n        if row&#91;'MA_Fast'] &gt; row&#91;'MA_Slow']:\n            return 1\n        elif row&#91;'MA_Fast'] &lt; row&#91;'MA_Slow']:\n            return -1\n        else:\n            return 0\n        \n    df_ohlc = df_ohlc.assign(Trend=df_ohlc.apply(identify_trend, axis=1))\n    df_ohlc&#91;'Trend'] =  df_ohlc&#91;'Trend'].fillna('0')\n\n    return df_ohlc&#91;'Trend']\n\ndf&#91;'Trend'] = calculate_trend_2_ma(df, period_slow=21, period_fast=9)\ndf&#91;'Equity Curve'], stats = calculate_returns(df, col_for_returns = 'Close', col_for_signal = 'Trend')\n\n# Plotting with adjusted subplot heights\nfig, ax1 = plt.subplots(1, 1, figsize=(14, 7), sharex=True)\n\n# Plotting the close price with the color corresponding to the trend\nfor i in range(1, len(df)):\n    ax1.plot(df.index&#91;i-1:i+1], df&#91;'Close'].iloc&#91;i-1:i+1], \n             color='green' if df&#91;'Trend'].iloc&#91;i] == 1 else \n                   ('red' if df&#91;'Trend'].iloc&#91;i] == -1 else 'darkgrey'), linewidth=2)\n\n# Plot the Moving Averages\nax1.plot(df&#91;'MA_Fast'], label='9-day MA (Fast)', color='blue')\nax1.plot(df&#91;'MA_Slow'], label='21-day MA (Slow)', color='orange')\nax1.set_title(f'{ticker} - Price and Moving Averages')\nax1.text(0.5, 0.9, f'Total Return: {stats&#91;'total_return']:.2%}', transform=ax1.transAxes, ha='center', va='top', fontsize=14)\nax1.legend(loc='best')\n\nplt.show()<\/code><\/code><\/pre>\n\n\n\n<p>\u8fd0\u884c\u4ee3\u7801\u540e\uff0c\u60a8\u5c06\u5f97\u5230\u4ee5\u4e0b\u56fe\u8868\u3002\u770b\u770b\u6211\u662f\u5982\u4f55\u7ed8\u5236\u6536\u76d8\u4ef7\u7684\u3002\u5f53\u4fe1\u53f7\u4e3a\u4e0a\u5347\u8d8b\u52bf\u65f6\uff0c\u7ebf\u7684\u989c\u8272\u4e3a\u7eff\u8272\u3002\u5f53\u4fe1\u53f7\u4e3a\u4e0b\u964d\u8d8b\u52bf\u65f6\uff0c\u7ebf\u7684\u989c\u8272\u4e3a\u7ea2\u8272\u3002\u5f53\u6ca1\u6709\u4fe1\u53f7\u65f6\uff0c\u5219\u4e3a\u7070\u8272\u3002<\/p>\n\n\n\n<p><em>\u5982\u679c\u4f60\u5bf9\u5982\u4f55\u753b\u51fa\u53ef\u89c6\u5316\u4ea4\u6613\u4fe1\u53f7\u8fd8\u4e0d\u719f\u6089\uff0c\u53ef\u4ee5\u770b\u7684\u8fd9\u7bc7\u6587\u7ae0\u300a\u624b\u628a\u624b\u5e26\u4f60\u7528 Python \u753b\u51fa\u53ef\u89c6\u5316\u4ea4\u6613\u4fe1\u53f7\u300b<\/em><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/www.laoyulaoyu.com\/wp-content\/uploads\/2024\/10\/image.png\" alt=\"\" class=\"wp-image-2324\"\/><\/figure>\n\n\n\n<p><strong>\u603b\u56de\u62a5\u7387\u4e3a 14.03%<\/strong>\u3002\u5267\u900f\u4e00\u4e0b\uff01\u5230\u76ee\u524d\u4e3a\u6b62\uff0cSPY \u7684\u8fd9\u79cd\u65b9\u6cd5\u662f\u6700\u597d\u7684\u3002\u4f46\u8bf7\u7ee7\u7eed\u5f80\u4e0b\u9605\u8bfb\uff0c\u5b83\u540e\u9762\u4f1a\u8d70\u504f\u7684\uff01<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u4e09\u3001\u79fb\u52a8\u5e73\u5747\u7ebf + MACD<br>Moving Average + MACD<\/strong><\/h3>\n\n\n\n<p>\u8fd9\u4e2a\u7684\u6838\u5fc3\u662f\u4e24\u4e2a\u6307\u6807\u9700\u8981\u4fdd\u6301\u4e00\u81f4\u3002\u8fd9\u610f\u5473\u7740\uff0c\u8981\u8bc6\u522b\u4e0a\u5347\u8d8b\u52bf\uff0c\u6536\u76d8\u4ef7\u5e94\u9ad8\u4e8e\u79fb\u52a8\u5e73\u5747\u7ebf\uff0c\u800c MACD \u7ebf\u5e94\u9ad8\u4e8e MACD \u4fe1\u53f7\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>def calculate_trend_macd_ma(df_ohlc, ma_period=50, macd_fast=12, macd_slow=26, macd_signal=9):\n    # Calculate MACD using pandas_ta\n    df_ohlc.ta.macd(close='Close', fast=macd_fast, slow=macd_slow, signal=macd_signal, append=True)\n\n    # Calculate Moving Average\n    df_ohlc&#91;'MA'] = df_ohlc.ta.sma(close='Close', length=ma_period)\n\n    # Determine the trend based on MA and MACD\n    def identify_trend(row):\n        macd_name = f'{macd_fast}_{macd_slow}_{macd_signal}'\n        if row&#91;'Close'] &gt; row&#91;'MA'] and row&#91;f'MACD_{macd_name}'] &gt; row&#91;f'MACDs_{macd_name}']:\n            return 1\n        elif row&#91;'Close'] &lt; row&#91;'MA'] and row&#91;f'MACD_{macd_name}'] &lt; row&#91;f'MACDs_{macd_name}']:\n            return -1\n        else:\n            return 0\n\n    df_ohlc&#91;'Trend'] = df_ohlc.apply(identify_trend, axis=1)\n    return df_ohlc&#91;'Trend']\n\ndf&#91;'Trend'] = calculate_trend_macd_ma(df, ma_period=50, macd_fast=12, macd_slow=26, macd_signal=9)\ndf&#91;'Equity Curve'], stats = calculate_returns(df, col_for_returns = 'Close', col_for_signal = 'Trend')\n\n# Plotting with adjusted subplot heights\nfig, (ax1, ax2) = plt.subplots(2, 1, figsize=(14, 10), sharex=True, \n                               gridspec_kw={'height_ratios': &#91;3, 1]})\n\n# Plotting the close price with the color corresponding to the trend\nfor i in range(1, len(df)):\n    ax1.plot(df.index&#91;i-1:i+1], df&#91;'Close'].iloc&#91;i-1:i+1], \n             color='green' if df&#91;'Trend'].iloc&#91;i] == 1 else \n                   ('red' if df&#91;'Trend'].iloc&#91;i] == -1 else 'darkgrey'), linewidth=2)\n\n# Plot the Moving Average\nax1.plot(df&#91;'MA'], label=f'50-day MA', color='orange')\nax1.set_title(f'{ticker} - Price and Moving Average')\nax1.text(0.5, 0.9, f'Total Return: {stats&#91;'total_return']:.2%}', transform=ax1.transAxes, ha='center', va='top', fontsize=14)\nax1.legend(loc='best')\n\n# Plot MACD and Signal Line on the second subplot (smaller height)\nax2.plot(df.index, df&#91;'MACD_12_26_9'], label='MACD', color='blue')\nax2.plot(df.index, df&#91;'MACDs_12_26_9'], label='Signal Line', color='red')\nax2.set_title(f'{ticker} - MACD')\nax2.legend(loc='best')\n\nplt.show()<\/code><\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/www.laoyulaoyu.com\/wp-content\/uploads\/2024\/10\/image-1.png\" alt=\"\" class=\"wp-image-2325\"\/><\/figure>\n\n\n\n<p><strong>\u603b\u56de\u62a5\u7387\u662f 4.48%<\/strong>\uff0c\u6bd4 2-MA \u4f4e\u4e0d\u5c11\u5462\uff0c\u4f46\u4f60\u770b\uff0c\u6709\u597d\u591a\u7070\u8272\uff08\u4e2d\u6027\uff09\u7684\u65f6\u5019\uff0c\u8fd9\u6837\u81f3\u5c11\u6211\u4eec\u6ca1\u4e00\u76f4\u62c5\u7740\u98ce\u9669\u5440\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u56db\u3001RSI + \u5feb\u6162\u79fb\u52a8\u5e73\u5747\u7ebf<br>RSI + Fast and Slow Moving Averages<\/strong><\/h3>\n\n\n\n<p>\u4e0e\u4e0a\u8ff0\u7c7b\u4f3c\uff0c\u6211\u4eec\u5c06\u4f7f\u7528\u5feb\u901f\u548c\u6162\u901f\u79fb\u52a8\u5e73\u5747\u7ebf\uff0c\u4f46\u8981\u4e0e RSI \u4e00\u8d77\u4f7f\u7528\u3002\u6211\u4eec\u7684\u5047\u8bbe\u662f\uff0c\u8fd9\u4e24\u4e2a\u4fe1\u53f7\u5e94\u8be5\u4e00\u81f4\u3002\u5feb\u901f\u79fb\u52a8\u5e73\u5747\u7ebf\u5e94\u9ad8\u4e8e\u6162\u901f\u79fb\u52a8\u5e73\u5747\u7ebf\uff0c\u800c RSI &gt; 50 \u5219\u8868\u793a\u4e0a\u5347\u8d8b\u52bf\uff0c\u76f8\u53cd\u5219\u8868\u793a\u4e0b\u964d\u8d8b\u52bf\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>def calculate_trend_rsi_ma(df_ohlc, rsi_period=14, ma_fast=9, ma_slow=21):\n\n    # Calculate RSI using pandas_ta\n    df_ohlc&#91;'RSI'] = df.ta.rsi(close='Close', length=rsi_period)\n\n    # Calculate Moving Averages (14-day and 50-day) using pandas_ta\n    df_ohlc&#91;f'MA_{ma_fast}'] = df_ohlc.ta.sma(close='Close', length=14)\n    df_ohlc&#91;f'MA_{ma_slow}'] = df_ohlc.ta.sma(close='Close', length=50)\n\n    # Determine the trend based on RSI and Moving Averages\n    def identify_trend(row):\n        if row&#91;'RSI'] &gt; 50 and row&#91;f'MA_{ma_fast}'] &gt; row&#91;f'MA_{ma_slow}']:\n            return 1\n        elif row&#91;'RSI'] &lt; 50 and row&#91;f'MA_{ma_fast}'] &lt; row&#91;f'MA_{ma_slow}']:\n            return -1\n        else:\n            return 0\n\n    df_ohlc&#91;'Trend'] = df_ohlc.apply(identify_trend, axis=1)\n    return df_ohlc&#91;'Trend']\n\ndf&#91;'Trend'] = calculate_trend_rsi_ma(df, rsi_period=14, ma_fast=14, ma_slow=50)\ndf&#91;'Equity Curve'], stats = calculate_returns(df, col_for_returns = 'Close', col_for_signal = 'Trend')\n\n# Plotting with adjusted subplot heights\nfig, (ax1, ax2) = plt.subplots(2, 1, figsize=(14, 10), sharex=True, \n                               gridspec_kw={'height_ratios': &#91;3, 1]})\n\n# Plotting the close price with the color corresponding to the trend\nfor i in range(1, len(df)):\n    ax1.plot(df.index&#91;i-1:i+1], df&#91;'Close'].iloc&#91;i-1:i+1], \n             color='green' if df&#91;'Trend'].iloc&#91;i] == 1 else \n                   ('red' if df&#91;'Trend'].iloc&#91;i] == -1 else 'darkgrey'), linewidth=2)\n\n# Plot the Moving Averages\nax1.plot(df&#91;'MA_14'], label='14-day MA', color='blue')\nax1.plot(df&#91;'MA_50'], label='50-day MA', color='orange')\nax1.text(0.5, 0.9, f'Total Return: {stats&#91;'total_return']:.2%}', transform=ax1.transAxes, ha='center', va='top', fontsize=14)\nax1.set_title(f'{ticker} - Price, RSI and Fast and Slow Moving Average')\nax1.legend(loc='best')\n\n# Plot RSI on the second subplot (smaller height)\nax2.plot(df.index, df&#91;'RSI'], label='RSI', color='purple')\nax2.axhline(50, color='black', linestyle='--', linewidth=1)  # Add a horizontal line at RSI=50\nax2.set_title(f'{ticker} - RSI')\nax2.legend(loc='best')\n\nplt.show()<\/code><\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/www.laoyulaoyu.com\/wp-content\/uploads\/2024\/10\/image-2.png\" alt=\"\" class=\"wp-image-2326\"\/><\/figure>\n\n\n\n<p>\u7ed3\u679c\u660e\u663e<strong>\u56de\u62a5\u7387\u964d\u4f4e\u81f3 1.82%<\/strong>\u3002\u770b\u6765\uff0c\u52a0\u5165 RSI \u540e\uff0c\u5feb\u901f\u548c\u6162\u901f MAs \u7684\u6536\u76ca\u5e76\u4e0d\u9ad8&#8230;&#8230;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u4e94\u3001\u5e03\u6797\u7ebf\u548c RSI<br>Bollinger Bands and RSI<\/strong><\/h3>\n\n\n\n<p>\u73b0\u5728\uff0c\u8ba9\u6211\u4eec\u5c1d\u8bd5\u5c06\u5e03\u6797\u7ebf\u4e0e RSI \u7ed3\u5408\u8d77\u6765\u3002\u540c\u6837\uff0c\u4e24\u4e2a\u4fe1\u53f7\u9700\u8981\u4e00\u81f4\u3002\u5f53\u4ef7\u683c\u9ad8\u4e8e\u5e03\u6797\u5e26\u4e2d\u8f68\uff0c\u4e14 RSI \u5728 50 \u4ee5\u4e0a\u65f6\uff0c\u6211\u4eec\u5c31\u6709\u4e86\u4e0a\u5347\u8d8b\u52bf\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>def calculate_trend_bbands_rsi(df_ohlc, bbands_period=5, bbands_std=2, rsi_period=14):\n\n    # Calculate RSI using pandas_ta\n    df_ohlc&#91;'RSI'] = df_ohlc.ta.rsi(close='Close', length=rsi_period)\n\n    # Calculate Bollinger Bands using pandas_ta\n    bbands = df.ta.bbands(close='Close', length=bbands_period, std=bbands_std)\n    df_ohlc&#91;'BB_upper'] = bbands&#91;f'BBU_{bbands_period}_{bbands_std}.0']\n    df_ohlc&#91;'BB_middle'] = bbands&#91;f'BBM_{bbands_period}_{bbands_std}.0']\n    df_ohlc&#91;'BB_lower'] = bbands&#91;f'BBL_{bbands_period}_{bbands_std}.0']\n\n    # Determine the trend based on Bollinger Bands and RSI\n    def identify_trend(row):\n        if row&#91;'Close'] &gt; row&#91;'BB_middle'] and row&#91;'RSI'] &gt; 50:\n            return 1\n        elif row&#91;'Close'] &lt; row&#91;'BB_middle'] and row&#91;'RSI'] &lt; 50:\n            return -1\n        else:\n            return 0\n\n    df_ohlc&#91;'Trend'] = df_ohlc.apply(identify_trend, axis=1)\n    return df_ohlc&#91;'Trend']\n\ndf&#91;'Trend'] = calculate_trend_bbands_rsi(df, bbands_period=20, bbands_std=2, rsi_period=14)\ndf&#91;'Equity Curve'], stats = calculate_returns(df, col_for_returns = 'Close', col_for_signal = 'Trend')\n\n# Plotting with adjusted subplot heights\nfig, (ax1, ax2) = plt.subplots(2, 1, figsize=(14, 10), sharex=True, \n                               gridspec_kw={'height_ratios': &#91;3, 1]})\n\n# Plotting the close price with the color corresponding to the trend\nfor i in range(1, len(df)):\n    ax1.plot(df.index&#91;i-1:i+1], df&#91;'Close'].iloc&#91;i-1:i+1], \n             color='green' if df&#91;'Trend'].iloc&#91;i] == 1 else \n                   ('red' if df&#91;'Trend'].iloc&#91;i] == -1 else 'darkgrey'), linewidth=2)\n\n# Plot Bollinger Bands\nax1.plot(df&#91;'BB_upper'], label='Upper Band', color='blue', linestyle='--')\nax1.plot(df&#91;'BB_middle'], label='Middle Band', color='orange')\nax1.plot(df&#91;'BB_lower'], label='Lower Band', color='blue', linestyle='--')\nax1.text(0.5, 0.9, f'Total Return: {stats&#91;'total_return']:.2%}', transform=ax1.transAxes, ha='center', va='top', fontsize=14)\nax1.set_title(f'{ticker} - Price, RSI and Bollinger Bands')\nax1.legend(loc='best')\n\n# Plot RSI on the second subplot (smaller height)\nax2.plot(df.index, df&#91;'RSI'], label='RSI', color='purple')\nax2.axhline(50, color='black', linestyle='--', linewidth=1)  # Add a horizontal line at RSI=50\nax2.set_title(f'{ticker} - RSI')\nax2.legend(loc='best')\n\nplt.show()<\/code><\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/www.laoyulaoyu.com\/wp-content\/uploads\/2024\/10\/image-3.png\" alt=\"\" class=\"wp-image-2327\"\/><\/figure>\n\n\n\n<p>\u8fd9\u4e00\u7ec4\u5408\u7684\u603b<strong>\u56de\u62a5\u7387\u4e3a 4.56%<\/strong>\u3002RSI \u770b\u8d77\u6765\u53c8\u4e00\u6b21\u7834\u574f\u4e86\u641e\u94b1\u7684\u6c14\u6c1b&#8230;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u516d\u3001ADX \u4e0e\u6162\u901f\u548c\u5feb\u901f\u79fb\u52a8\u5e73\u5747\u7ebf<br>ADX with slow and fast Moving Average<\/strong><\/h3>\n\n\n\n<p>\u7ed3\u5408 ADX \u548c\u79fb\u52a8\u5e73\u5747\u7ebf\uff0c\u5f53 ADX \u8d85\u8fc7 25\uff08\u8868\u660e\u8d8b\u52bf\u5f3a\u52b2\uff09\u4e14\u5feb\u901f MA \u8d85\u8fc7\u6162\u901f MA \u65f6\uff0c\u6211\u4eec\u5c31\u80fd\u8bc6\u522b\u51fa\u4e0a\u5347\u8d8b\u52bf\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>def calculate_trend_adx_ma(df_ohlc, adx_period=14, fast_ma_period=14, slow_ma_period=50):\n    # Calculate ADX using pandas_ta\n    df_ohlc&#91;'ADX'] = df_ohlc.ta.adx(length=adx_period)&#91;f'ADX_{adx_period}']\n\n    # Calculate Moving Averages (14-day and 50-day) using pandas_ta\n    df_ohlc&#91;'MA_fast'] = df_ohlc.ta.sma(close='Close', length=fast_ma_period)\n    df_ohlc&#91;'MA_slow'] = df_ohlc.ta.sma(close='Close', length=slow_ma_period)\n\n    # Determine the trend based on ADX and Moving Averages\n    def identify_trend(row):\n        if row&#91;'ADX'] &gt; 25 and row&#91;'MA_fast'] &gt; row&#91;'MA_slow']:\n            return 1\n        elif row&#91;'ADX'] &gt; 25 and row&#91;'MA_fast'] &lt; row&#91;'MA_slow']:\n            return -1\n        else:\n            return 0\n\n    df_ohlc&#91;'Trend'] = df_ohlc.apply(identify_trend, axis=1)\n    return df_ohlc&#91;'Trend']\n\ndf&#91;'Trend'] = calculate_trend_adx_ma(df, adx_period=14, fast_ma_period=14, slow_ma_period=50)\ndf&#91;'Equity Curve'], stats = calculate_returns(df, col_for_returns = 'Close', col_for_signal = 'Trend')\n\n# Plotting with adjusted subplot heights\nfig, (ax1, ax2) = plt.subplots(2, 1, figsize=(14, 10), sharex=True, \n                               gridspec_kw={'height_ratios': &#91;3, 1]})\n\n# Plotting the close price with the color corresponding to the trend\nfor i in range(1, len(df)):\n    ax1.plot(df.index&#91;i-1:i+1], df&#91;'Close'].iloc&#91;i-1:i+1], \n             color='green' if df&#91;'Trend'].iloc&#91;i] == 1 else \n                   ('red' if df&#91;'Trend'].iloc&#91;i] == -1 else 'darkgrey'), linewidth=2)\n\n# Plot the Moving Averages\nax1.plot(df&#91;'MA_fast'], label='Fast MA', color='blue')\nax1.plot(df&#91;'MA_slow'], label='Slow MA', color='orange')\nax1.text(0.5, 0.9, f'Total Return: {stats&#91;'total_return']:.2%}', transform=ax1.transAxes, ha='center', va='top', fontsize=14)\nax1.set_title(f'{ticker} - Price, ADX and Moving Averages')\nax1.legend(loc='best')\n\n# Plot ADX on the second subplot (smaller height)\nax2.plot(df.index, df&#91;'ADX'], label='ADX', color='purple')\nax2.axhline(25, color='black', linestyle='--', linewidth=1)  # Add a horizontal line at ADX=25\nax2.set_title(f'{ticker} - ADX')\nax2.legend(loc='best')\n\nplt.show()<\/code><\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/www.laoyulaoyu.com\/wp-content\/uploads\/2024\/10\/image-4.png\" alt=\"\" class=\"wp-image-2328\"\/><\/figure>\n\n\n\n<p>\u770b\u8d77\u6765\u5f88\u6709\u8da3\uff01\u8fd9\u4e2a\u6280\u672f\u6307\u6807\u9a71\u52a8\u7684\u5165\u5e02\u65f6\u95f4\u8fd9\u4e48\u77ed\uff0c<strong>\u56de\u62a5\u7387\u5374\u9ad8\u8fbe 11.03%<\/strong>\u3002\u770b\u6765\u6211\u4eec\u5e94\u8be5\u8bb0\u4f4f\u5b83\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u4e03\u3001\u4e00\u76ee\u5747\u8861\u4e91\u56fe\u548c MACD<br>Ichimoku Cloud and MACD<\/strong><\/h3>\n\n\n\n<p>\u8ba9\u6211\u4eec\u7528\u4e00\u4e9b\u4e0d\u8ba4\u8bc6\u7684\u65e5\u8bed\u5355\u8bcd\u6765\u505a\u4e00\u4e9b\u82b1\u54e8\u7684\u4e8b\u60c5\uff01\u6211\u4eec\u5c06\u4f7f\u7528\u4e00\u76ee\u5747\u8861\u4e91\u56fe\u548c MACD\u3002\u5f53\u4ef7\u683c\u4f4d\u4e8e\u4e00\u76ee\u5747\u8861\u4e91\u56fe\u4e0a\u65b9\uff0c\u4e14 MACD \u4f4d\u4e8e\u4fe1\u53f7\u7ebf\u4e0a\u65b9\u65f6\uff0c\u6211\u4eec\u5c31\u80fd\u786e\u5b9a\u4e0a\u5347\u8d8b\u52bf\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>def calculate_trend_ichimoku_macd(df_ohlc, macd_fast=12, macd_slow=26, macd_signal=9, tenkan=9, kijun=26, senkou=52):\n\n    # Calculate Ichimoku Cloud components using pandas_ta\n    df_ichimoku = df_ohlc.ta.ichimoku(tenkan, kijun, senkou)&#91;0]\n\n    # Extract Ichimoku Cloud components\n    df_ohlc&#91;'Ichimoku_Conversion'] = df_ichimoku&#91;f'ITS_{tenkan}']  # Tenkan-sen (Conversion Line)\n    df_ohlc&#91;'Ichimoku_Base'] = df_ichimoku&#91;f'IKS_{kijun}']       # Kijun-sen (Base Line)\n    df_ohlc&#91;'Ichimoku_Span_A'] = df_ichimoku&#91;f'ITS_{tenkan}']         # Senkou Span A\n    df_ohlc&#91;'Ichimoku_Span_B'] = df_ichimoku&#91;f'ISB_{kijun}']        # Senkou Span B\n\n    # Calculate MACD using pandas_ta\n    df_ohlc.ta.macd(close='Close', fast=macd_fast, slow=macd_slow, signal=macd_signal, append=True)\n\n    # Determine the trend based on Ichimoku Cloud and MACD\n    def identify_trend(row):\n        if row&#91;'Close'] &gt; max(row&#91;'Ichimoku_Span_A'], row&#91;'Ichimoku_Span_B']) and row&#91;'MACD_12_26_9'] &gt; row&#91;'MACDs_12_26_9']:\n            return 1\n        elif row&#91;'Close'] &lt; min(row&#91;'Ichimoku_Span_A'], row&#91;'Ichimoku_Span_B']) and row&#91;'MACD_12_26_9'] &lt; row&#91;'MACDs_12_26_9']:\n            return -1\n        else:\n            return 0\n\n    df_ohlc&#91;'Trend'] = df_ohlc.apply(identify_trend, axis=1)\n    return df_ohlc&#91;'Trend']\n\ndf&#91;'Trend'] = calculate_trend_ichimoku_macd(df, macd_fast=12, macd_slow=26, macd_signal=9)\ndf&#91;'Equity Curve'], stats = calculate_returns(df, col_for_returns = 'Close', col_for_signal = 'Trend')\n\n# Plotting with adjusted subplot heights\nfig, (ax1, ax2) = plt.subplots(2, 1, figsize=(14, 10), sharex=True, \n                               gridspec_kw={'height_ratios': &#91;3, 1]})\n\n# Plotting the close price with the color corresponding to the trend\nfor i in range(1, len(df)):\n    ax1.plot(df.index&#91;i-1:i+1], df&#91;'Close'].iloc&#91;i-1:i+1], \n             color='green' if df&#91;'Trend'].iloc&#91;i] == 1 else \n                   ('red' if df&#91;'Trend'].iloc&#91;i] == -1 else 'darkgrey'), linewidth=2)\n\n# Plot Ichimoku Cloud\nax1.fill_between(df.index, df&#91;'Ichimoku_Span_A'], df&#91;'Ichimoku_Span_B'], \n                 where=(df&#91;'Ichimoku_Span_A'] &gt;= df&#91;'Ichimoku_Span_B']), color='lightgreen', alpha=0.5)\nax1.fill_between(df.index, df&#91;'Ichimoku_Span_A'], df&#91;'Ichimoku_Span_B'], \n                 where=(df&#91;'Ichimoku_Span_A'] &lt; df&#91;'Ichimoku_Span_B']), color='lightcoral', alpha=0.5)\n\nax1.plot(df&#91;'Ichimoku_Conversion'], label='Conversion Line (Tenkan-sen)', color='blue')\nax1.plot(df&#91;'Ichimoku_Base'], label='Base Line (Kijun-sen)', color='orange')\nax1.text(0.5, 0.9, f'Total Return: {stats&#91;'total_return']:.2%}', transform=ax1.transAxes, ha='center', va='top', fontsize=14)\nax1.set_title(f'{ticker} - Price and Ichimoku Cloud')\nax1.legend(loc='best')\n\n# Plot MACD and Signal Line on the second subplot (smaller height)\nax2.plot(df.index, df&#91;'MACD_12_26_9'], label='MACD', color='blue')\nax2.plot(df.index, df&#91;'MACDs_12_26_9'], label='Signal Line', color='red')\nax2.set_title(f'{ticker} - MACD')\nax2.legend(loc='best')\n\nplt.show()<\/code><\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/www.laoyulaoyu.com\/wp-content\/uploads\/2024\/10\/image-5.png\" alt=\"\" class=\"wp-image-2329\"\/><\/figure>\n\n\n\n<p><strong>7.63% \u7684\u56de\u62a5\u7387<\/strong>\u8fd8\u7b97\u4e0d\u9519\uff0c\u4f46\u5bf9\u6211\u6765\u8bf4\uff0c\u8fdb\u8fdb\u51fa\u51fa\u7684\u6b21\u6570\u592a\u591a\u4e86\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u516b\u3001\u603b\u7ed3 SPY \u7684\u7ed3\u679c<\/strong><\/h3>\n\n\n\n<p>\u4e3a\u4e86\u603b\u7ed3\u7ed3\u679c\uff0c\u6211\u4eec\u53ef\u4ee5\u8fd0\u884c\u4e0b\u9762\u7684\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code># Download the stock data\nticker = 'SPY'  # You can replace 'AAPL' with any other stock ticker or currency pair\ndf = yf.download(ticker, start='2023-01-01', end='2024-06-01')\n\ntrend_identification_methods = &#91;'2_ma', 'macd_ma', 'rsi_ma', 'bbands_rsi', 'adx_ma',  'ichimoku_macd']\ntrend_identification_results = &#91;]\n\ndef calculate_trend(df, method):\n    if method == '2_ma':\n        return calculate_trend_2_ma(df, period_slow=21, period_fast=9)\n    elif method == 'macd_ma':\n        return calculate_trend_macd_ma(df, ma_period=50, macd_fast=12, macd_slow=26, macd_signal=9)\n    elif method == 'rsi_ma':\n        return calculate_trend_rsi_ma(df, rsi_period=14, ma_fast=9, ma_slow=21)\n    elif method == 'bbands_rsi':\n        return calculate_trend_bbands_rsi(df, bbands_period=5, bbands_std=2, rsi_period=14)\n    elif method == 'adx_ma':\n        return calculate_trend_adx_ma(df, adx_period=14, fast_ma_period=14, slow_ma_period=50)\n    elif method == 'ichimoku_macd':\n        return calculate_trend_ichimoku_macd(df, macd_fast=12, macd_slow=26, macd_signal=9, tenkan=9, kijun=26, senkou=52)\n\nfor method in trend_identification_methods:\n\n    # Calculate results of returns for each method and append to the list\n    df_copy = df.copy()\n    d = {}\n    d&#91;'Method'] = method\n    df_copy&#91;'Trend'] = calculate_trend(df_copy, method)\n    df_copy&#91;'Equity Curve'], stats = calculate_returns(df_copy, col_for_returns = 'Close', col_for_signal = 'Trend')\n    d.update(stats)\n    trend_identification_results.append(d)\n\n    # Add trend line and equity curve to the df\n    df&#91;f'Trend_{method}'] = df_copy&#91;'Trend']\n    df&#91;f'Equity Curve_{method}'] = df_copy&#91;'Equity Curve']\n\n\ntrend_identification_results_df = pd.DataFrame(trend_identification_results)\n\ntrend_identification_results_df<\/code><\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/www.laoyulaoyu.com\/wp-content\/uploads\/2024\/10\/image-6.png\" alt=\"\" class=\"wp-image-2330\"\/><\/figure>\n\n\n\n<p>\u603b\u7ed3\u4e0b SPY \u7684\u7ed3\u679c\uff0c\u53d1\u73b0\u5feb\u901f\u548c\u6162\u901f MAs \u7684\u6536\u76ca\u7387\u6700\u9ad8\uff0c\u590f\u666e\u6bd4\u7387\u4e5f\u8fd8\u884c\uff0c\u4f46\u7f29\u6c34\u7387\u662f\u6700\u5dee\u7684\u3002\u8fd9\u53ef\u4ee5\u7406\u89e3\u4e3a\uff08\u4e0d\u7ba1\u662f\u597d\u6536\u76ca\u8fd8\u662f\u574f\u7f29\u6c34\uff09\uff0c\u8fd9\u662f\u4e2a\u4fe1\u53f7\u591a\u7684\u7b56\u7565\uff0c\u6240\u4ee5\u5b83\u5728\u5e02\u573a\u4e0a \u201c\u82b1\u8d39\u201d \u7684\u65f6\u95f4\uff08\u98ce\u9669\u655e\u53e3\uff09\u6700\u591a\u3002\u53e6\u5916\uff0c\u7528\u5feb\u901f\u548c\u6162\u901f MA \u7684 ADX \u4e5f\u662f\u7b2c\u4e8c\u597d\u7684\u7b56\u7565\uff0c\u5728\u5e02\u573a\u91cc\u66b4\u9732\u7684\u65f6\u95f4\u5c11\uff0c\u8fd9\u70b9\u633a\u6709\u610f\u601d\u7684\u3002<\/p>\n\n\n\n<p><strong>\u8ba9\u6211\u4eec\u518d\u52a0\u5165\u4e00\u4e9b\u80a1\u7968\u6765\u6d4b\u8bd5<\/strong><\/p>\n\n\n\n<p>\u73b0\u5728\uff0c\u6211\u4eec\u5c06\u8fd0\u884c SPY\uff0c\u5e76\u589e\u52a0 4 \u53ea\u80a1\u7968\uff08\u82f9\u679c\u3001\u4e9a\u9a6c\u900a\u3001\u8c37\u6b4c\u3001\u7279\u65af\u62c9\uff09\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">tickers = ['AAPL', 'AMZN', 'GOOG', 'TSLA', 'SPY']\nresults = []\n\nfor ticker in tickers:\n    df = yf.download(ticker, start='2023-01-01', end='2024-06-01')\n\n    trend_identification_methods = ['2_ma', 'macd_ma', 'rsi_ma', 'bbands_rsi', 'adx_ma',  'ichimoku_macd']\n    trend_identification_results = []\n\n    def calculate_trend(df, method):\n        if method == '2_ma':\n            return calculate_trend_2_ma(df, period_slow=21, period_fast=9)\n        elif method == 'macd_ma':\n            return calculate_trend_macd_ma(df, ma_period=50, macd_fast=12, macd_slow=26, macd_signal=9)\n        elif method == 'rsi_ma':\n            return calculate_trend_rsi_ma(df, rsi_period=14, ma_fast=9, ma_slow=21)\n        elif method == 'bbands_rsi':\n            return calculate_trend_bbands_rsi(df, bbands_period=5, bbands_std=2, rsi_period=14)\n        elif method == 'adx_ma':\n            return calculate_trend_adx_ma(df, adx_period=14, fast_ma_period=14, slow_ma_period=50)\n        elif method == 'ichimoku_macd':\n            return calculate_trend_ichimoku_macd(df, macd_fast=12, macd_slow=26, macd_signal=9, tenkan=9, kijun=26, senkou=52)\n\n\n    for method in trend_identification_methods:\n\n        # Calculate results of returns for each method and append to the list\n        df_copy = df.copy()\n        d = {}\n        d['Method'] = method\n        df_copy['Trend'] = calculate_trend(df_copy, method)\n        df_copy['Equity Curve'], stats = calculate_returns(df_copy, col_for_returns = 'Close', col_for_signal = 'Trend')\n        results.append({'ticker':ticker, 'method':method, 'total_return':stats['total_return']})\n\ntest_df = pd.DataFrame(results)\n\n# Pivot the DataFrame to prepare for plotting\npivot_df = test_df.pivot(index='ticker', columns='method', values='total_return')\n\n# Plotting\npivot_df.plot(kind='bar', figsize=(12, 8))\n\nplt.title('Comparison of total returns per trend method')\nplt.ylabel('Value')\nplt.xlabel('Stock')\nplt.legend(title='Indicators', bbox_to_anchor=(1.05, 1), loc='upper left')\nplt.xticks(rotation=45)\n\nplt.tight_layout()\nplt.show()<\/pre>\n\n\n\n<p>\u73b0\u5728\u7684\u60c5\u51b5\u6108\u53d1\u6df7\u4e71\u4e86\u2026\u2026<strong>\u6ca1\u6709\u54ea\u79cd\u6a21\u5f0f\u660e\u663e\u662f\u8d62\u5bb6<\/strong>\u3002\u4e0d\u8fc7\uff0c\u6211\u4e2a\u4eba\u6bd4\u8f83\u559c\u6b22\u5e26\u6709\u5feb\u901f\u548c\u6162\u901f MA \u7684 ADX\u3002\u5b83\u901a\u5e38\u8981\u4e48\u662f\u6700\u597d\u7684\uff0c\u8981\u4e48\u80fd\u8fdb\u5165\u524d\u4e09\uff0c\u5c31\u7b97\u4e0d\u662f\u6700\u597d\u7684\uff0c\u8868\u73b0\u4e5f\u4e0d\u4f1a\u5dee\u3002\u800c\u4e14\uff0c\u5c31\u50cf\u6211\u4eec\u4e4b\u524d\u8bf4\u7684\uff0c\u5b83\u7684\u98ce\u9669\u4e5f\u6bd4\u5176\u4ed6\u7684\u8981\u5c0f\u3002\u6211\u633a\u559c\u6b22\u5b83\u7684\u3002<\/p>\n\n\n\n<p>\u60a8\u53ef\u4ee5\u5728 \u6211\u7684 <a href=\"https:\/\/github.com\/alexyu2013\/Investment_and_Algorithmic_Trading_Notebooks\/blob\/main\/Technical_Analysis\/Compare_6_Trend_Identification_Methods.ipynb\">GitHub<\/a> \u4e0a\u627e\u5230 Python \u4ee3\u7801\uff0c<a href=\"https:\/\/github.com\/alexyu2013\/Investment_and_Algorithmic_Trading_Notebooks\/blob\/main\/Technical_Analysis\/Compare_6_Trend_Identification_Methods.ipynb\">\u70b9\u51fb\u6b64\u5904<\/a>\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u4e5d\u3001\u89c2\u70b9\u56de\u987e<\/strong><\/h3>\n\n\n\n<p>\u6211\u9996\u5148\u4ecb\u7ecd\u4e86\u8bc6\u522b\u80a1\u5e02\u8d8b\u52bf\u7684\u91cd\u8981\u6027\uff0c\u5e76\u63d0\u51fa\u4e86\u516d\u79cd\u4e0d\u540c\u7684\u6280\u672f\u5206\u6790\u65b9\u6cd5\uff1a\u5feb\u6162\u79fb\u52a8\u5e73\u5747\u7ebf\u3001\u79fb\u52a8\u5e73\u5747\u7ebf+MACD\u3001RSI+\u5feb\u6162\u79fb\u52a8\u5e73\u5747\u7ebf\u3001\u5e03\u6797\u7ebf\u548cRSI\u3001ADX\u4e0e\u5feb\u6162\u79fb\u52a8\u5e73\u5747\u7ebf\u4ee5\u53ca\u4e00\u76ee\u5747\u8861\u4e91\u56fe\u548cMACD\u3002<\/p>\n\n\n\n<p>\u6bcf\u79cd\u65b9\u6cd5\u90fd\u901a\u8fc7Python\u4ee3\u7801\u5b9e\u73b0\uff0c\u5e76\u5728SPY ETF\u4e0a\u8fdb\u884c\u4e86\u6d4b\u8bd5\uff0c\u4ee5\u6b64\u6765\u8ba1\u7b97\u603b\u56de\u62a5\u548c\u5176\u4ed6\u7edf\u8ba1\u6570\u636e\uff0c\u5982\u6700\u5927\u56de\u64a4\u3001\u590f\u666e\u6bd4\u7387\u7b49\u3002\u6587\u7ae0\u4e2d\u8be6\u7ec6\u5c55\u793a\u4e86\u6bcf\u79cd\u65b9\u6cd5\u7684\u4ee3\u7801\u5b9e\u73b0\u3001\u56fe\u8868\u5c55\u793a\u4ee5\u53ca\u76f8\u5e94\u7684\u5206\u6790\u7ed3\u679c\u3002<\/p>\n\n\n\n<p>\u5728\u5bf9\u6bd4\u4e86\u8fd9\u4e9b\u65b9\u6cd5\u5728SPY\u4e0a\u7684\u8868\u73b0\u540e\uff0c\u6587\u7ae0\u8fdb\u4e00\u6b65\u5c06\u8fd9\u4e9b\u65b9\u6cd5\u5e94\u7528\u4e8e\u5176\u4ed6\u56db\u53ea\u80a1\u7968\uff08AAPL\u3001AMZN\u3001GOOG\u3001TSLA\uff09\uff0c\u4ee5\u68c0\u9a8c\u4e0d\u540c\u80a1\u7968\u5bf9\u8fd9\u4e9b\u8d8b\u52bf\u8bc6\u522b\u65b9\u6cd5\u7684\u53cd\u5e94\u3002\u6700\u7ec8\u5f97\u51fa\u4e86\u6bcf\u79cd\u65b9\u6cd5\u5728\u4e0d\u540c\u80a1\u7968\u4e0a\u7684\u603b\u56de\u62a5\u7387\uff0c\u5e76\u5bf9\u8fd9\u4e9b\u65b9\u6cd5\u8fdb\u884c\u4e86\u6bd4\u8f83\uff0c\u6307\u51fa\u6ca1\u6709\u4e00\u79cd\u7edd\u5bf9\u7684\u8d62\u5bb6\u65b9\u6cd5\uff0c\u4f46\u6211\u4e2a\u4eba\u504f\u597dADX\u7ed3\u5408\u5feb\u6162\u79fb\u52a8\u5e73\u5747\u7ebf\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u5728\u591a\u6570\u60c5\u51b5\u4e0b\u8868\u73b0\u7a33\u5b9a\uff0c\u4e14\u98ce\u9669\u655e\u53e3\u8f83\u5c0f\u3002\u6700\u540e\uff0c\u6587\u7ae0\u63d0\u4f9b\u4e86Python\u4ee3\u7801\u7684\u94fe\u63a5\u3002<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u5feb\u6162\u79fb\u52a8\u5e73\u5747\u7ebf\u65b9\u6cd5<\/strong>\u5728SPY\u4e0a\u7684\u603b\u56de\u62a5\u7387\u6700\u9ad8\uff0c\u4e3a14.03%\uff0c\u4f46\u540c\u65f6\u5177\u6709\u6700\u5dee\u7684\u6700\u5927\u56de\u64a4\u7387\u3002<\/li>\n\n\n\n<li><strong>\u79fb\u52a8\u5e73\u5747\u7ebf+MACD<\/strong>\u65b9\u6cd5\u7684\u603b\u56de\u62a5\u7387\u8f83\u4f4e\uff0c\u4e3a4.48%\uff0c\u4f46\u6709\u8f83\u591a\u7684\u4e2d\u6027\u65f6\u671f\uff0c\u51cf\u5c11\u4e86\u5e02\u573a\u66b4\u9732\u65f6\u95f4\u3002<\/li>\n\n\n\n<li><strong>RSI+\u5feb\u6162\u79fb\u52a8\u5e73\u5747\u7ebf<\/strong>\u65b9\u6cd5\u7684\u603b\u56de\u62a5\u7387\u8f83\u4f4e\uff0c\u4e3a1.82%\uff0c\u8868\u660eRSI\u7684\u52a0\u5165\u5e76\u672a\u63d0\u9ad8\u56de\u62a5\u3002<\/li>\n\n\n\n<li><strong>\u5e03\u6797\u7ebf\u548cRSI<\/strong>\u7ed3\u5408\u7684\u65b9\u6cd5\u603b\u56de\u62a5\u7387\u4e3a4.56%\uff0c\u518d\u6b21\u8868\u660eRSI\u53ef\u80fd\u4e0d\u662f\u63d0\u9ad8\u56de\u62a5\u7684\u6709\u6548\u624b\u6bb5\u3002<\/li>\n\n\n\n<li><strong>ADX\u4e0e\u5feb\u6162\u79fb\u52a8\u5e73\u5747\u7ebf<\/strong>\u7ed3\u5408\u7684\u65b9\u6cd5\u5728SPY\u4e0a\u7684\u603b\u56de\u62a5\u7387\u4e3a11.03%\uff0c\u4e14\u5e02\u573a\u66b4\u9732\u65f6\u95f4\u8f83\u77ed\uff0c\u88ab\u8ba4\u4e3a\u662f\u4e00\u79cd\u7a33\u5b9a\u7684\u65b9\u6cd5\u3002<\/li>\n\n\n\n<li><strong>\u4e00\u76ee\u5747\u8861\u4e91\u56fe\u548cMACD<\/strong>\u7ed3\u5408\u7684\u65b9\u6cd5\u603b\u56de\u62a5\u7387\u4e3a7.63%\uff0c\u4f46\u8fdb\u51fa\u5e02\u573a\u7684\u6b21\u6570\u8f83\u591a\u3002<\/li>\n\n\n\n<li>\u5728\u5bf9\u6bd4\u4e86\u4e0d\u540c\u80a1\u7968\u7684\u8868\u73b0\u540e\uff0c\u6587\u7ae0\u6307\u51fa\u6ca1\u6709\u4e00\u79cd\u7edf\u4e00\u7684\u6700\u4f73\u8d8b\u52bf\u8bc6\u522b\u65b9\u6cd5\uff0c<strong>\u4e0d\u540c\u7684\u80a1\u7968\u53ef\u80fd\u5bf9\u4e0d\u540c\u7684\u6280\u672f\u5206\u6790\u65b9\u6cd5\u6709\u4e0d\u540c\u7684\u53cd\u5e94\u3002<\/strong><\/li>\n\n\n\n<li>\u6211\u5efa\u8bae\u5927\u5bb6<strong>\u53ef\u4ee5\u5c06\u8fd9\u4e9b\u65b9\u6cd5\u5f53\u6210\u8fc7\u6ee4\u5668\uff0c\u800c\u4e0d\u4ec5\u4ec5\u662f\u4f5c\u4e3a\u4ea4\u6613\u4fe1\u53f7<\/strong>\uff0c\u4ee5\u4fbf\u66f4\u5168\u9762\u5730\u5206\u6790\u5e02\u573a\u3002<\/li>\n<\/ol>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>\u611f\u8c22\u60a8\u9605\u8bfb\u5230\u6700\u540e\u3002\u5982\u679c\u5bf9\u6587\u4e2d\u7684\u5185\u5bb9\u6709\u4efb\u4f55\u7591\u95ee\uff0c\u8bf7\u7ed9\u6211\u7559\u8a00\uff0c\u5fc5\u590d\u3002<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p class=\"has-text-align-center\"><strong><mark style=\"background-color:#ffffff\" class=\"has-inline-color has-cyan-bluish-gray-color\">\u672c<\/mark><\/strong><strong><mark style=\"background-color:#ffffff\" class=\"has-inline-color has-cyan-bluish-gray-color\">\u6587\u5185\u5bb9\u4ec5\u4ec5\u662f\u6280\u672f\u63a2\u8ba8\u548c\u5b66\u4e60\uff0c\u5e76\u4e0d\u6784\u6210\u4efb\u4f55\u6295\u8d44\u5efa\u8bae\u3002<\/mark><\/strong><\/p>\n\n\n\n<p class=\"has-text-align-center\"><strong><mark style=\"background-color:#ffffff\" class=\"has-inline-color has-cyan-bluish-gray-color\">\u8f6c\u53d1\u8bf7\u6ce8\u660e\u539f\u4f5c\u8005\u548c\u51fa\u5904\u3002<\/mark><\/strong><\/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\/2024\/10\/27\/%e5%bf%85%e7%9c%8b%ef%bc%81%e5%9c%a8%e8%af%86%e5%88%ab%e8%82%a1%e5%b8%82%e8%b6%8b%e5%8a%bf%e4%b8%8a%ef%bc%8c%e5%93%aa%e7%a7%8d%e6%8a%80%e6%9c%af%e5%88%86%e6%9e%90%e6%96%b9%e6%b3%95%e6%9c%80%e7%89%9b\/\">Continue reading<span class=\"screen-reader-text\">\u5fc5\u770b\uff01\u5728\u8bc6\u522b\u80a1\u5e02\u8d8b\u52bf\u4e0a\uff0c\u54ea\u79cd\u6280\u672f\u5206\u6790\u65b9\u6cd5\u6700\u725b\uff1f<\/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":[5,6],"class_list":["post-1556","post","type-post","status-publish","format-standard","hentry","category-aiinvest","tag-ai","tag-6","entry"],"_links":{"self":[{"href":"https:\/\/laoyulaoyu.com\/index.php\/wp-json\/wp\/v2\/posts\/1556","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=1556"}],"version-history":[{"count":1,"href":"https:\/\/laoyulaoyu.com\/index.php\/wp-json\/wp\/v2\/posts\/1556\/revisions"}],"predecessor-version":[{"id":1557,"href":"https:\/\/laoyulaoyu.com\/index.php\/wp-json\/wp\/v2\/posts\/1556\/revisions\/1557"}],"wp:attachment":[{"href":"https:\/\/laoyulaoyu.com\/index.php\/wp-json\/wp\/v2\/media?parent=1556"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/laoyulaoyu.com\/index.php\/wp-json\/wp\/v2\/categories?post=1556"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/laoyulaoyu.com\/index.php\/wp-json\/wp\/v2\/tags?post=1556"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}