{"id":1898,"date":"2025-02-14T07:18:00","date_gmt":"2025-02-13T23:18:00","guid":{"rendered":"https:\/\/blog.laoyulaoyu.top\/?p=1898"},"modified":"2025-01-15T10:33:03","modified_gmt":"2025-01-15T02:33:03","slug":"%e5%ad%a6%e4%bc%9a%e8%bf%99%e4%b8%aa%e7%9f%ad%e7%ba%bf%e5%88%86%e6%9e%90%e5%b0%8f%e6%8a%80%e5%b7%a7%ef%bc%8c%e8%bd%bb%e6%9d%be%e6%8f%90%e5%8d%87%e4%bd%a0%e7%9a%84%e6%8a%95%e8%b5%84%e7%bb%84%e5%90%88","status":"publish","type":"post","link":"https:\/\/laoyulaoyu.com\/index.php\/2025\/02\/14\/%e5%ad%a6%e4%bc%9a%e8%bf%99%e4%b8%aa%e7%9f%ad%e7%ba%bf%e5%88%86%e6%9e%90%e5%b0%8f%e6%8a%80%e5%b7%a7%ef%bc%8c%e8%bd%bb%e6%9d%be%e6%8f%90%e5%8d%87%e4%bd%a0%e7%9a%84%e6%8a%95%e8%b5%84%e7%bb%84%e5%90%88\/","title":{"rendered":"\u5b66\u4f1a\u8fd9\u4e2a\u77ed\u7ebf\u5206\u6790\u5c0f\u6280\u5de7\uff0c\u8f7b\u677e\u63d0\u5347\u4f60\u7684\u6295\u8d44\u7ec4\u5408\u6536\u76ca\uff01\uff08\u9644\u6e90\u7801\uff09"},"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_a1x-RLvRyCvfoBOLwCZEGw-1.webp\" alt=\"\" class=\"wp-image-3917\"\/><\/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\u5c06\u5e26\u60a8\u63a2\u7d22<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">\u5982\u4f55\u901a\u8fc7\u77ed\u7ebf\u5206\u6790\u4f18\u5316\u6295\u8d44\u7b56\u7565\uff0c\u5229\u7528\u652f\u6491\u4f4d\u548c\u963b\u529b\u4f4d\u5206\u6790\u63d0\u5347\u80a1\u7968\u7ec4\u5408\u8868\u73b0<\/mark>\u3002\u901a\u8fc7Python\u4ee3\u7801\u793a\u4f8b\uff0c\u5c06\u4e3a\u60a8\u5c55\u793a\u5177\u4f53\u64cd\u4f5c\u65b9\u6cd5\uff0c\u5e76\u9610\u8ff0\u5982\u4f55\u8ba1\u7b97\u4ea4\u6613\u51c0\u6536\u76ca\u3002\u8fd9\u4e9b\u6280\u5de7\u80fd\u5e2e\u52a9\u6295\u8d44\u8005\u5728\u7ba1\u7406\u6295\u8d44\u7ec4\u5408\u65f6\u66f4\u52a0\u9ad8\u6548\u548c\u7cbe\u51c6\u3002<\/pre>\n<\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u6982\u8ff0<\/strong><\/h2>\n\n\n\n<p>\u5047\u8bbe\u60a8\u5df2\u7ecf\u6709\u4e86\u4e00\u4e2a\u957f\u671f\u7684\u80a1\u7968\u6295\u8d44\u7ec4\u5408\u2014\u2014\u53ef\u80fd\u662f\u542c\u4e86\u67d0\u4e2a\u5389\u5bb3\u7684\u5206\u6790\u5e08\u7684\u5efa\u8bae\uff0c\u6216\u8005\u4f60\u81ea\u5df1\u7814\u7a76\u4e86\u4e00\u756a\u540e\u9009\u7684\u3002\u4f60\u8fd8\u6253\u7b97\u672a\u6765\u5f88\u957f\u4e00\u6bb5\u65f6\u95f4\u90fd\u62ff\u7740\u8fd9\u4e2a\u80a1\u7968\u7ec4\u5408\u4e0d\u52a8\u3002\u4e0d\u8fc7\uff0c\u5c31\u50cf\u6240\u6709\u806a\u660e\u7684\u6295\u8d44\u8005\u4e00\u6837\uff0c\u4f60\u4e5f\u8bb8\u4f1a\u65f6\u4e0d\u65f6\u5730\u8c03\u6574\u4e00\u4e0b\uff0c\u8ba9\u6295\u8d44\u7ec4\u5408\u7684\u98ce\u9669\u548c\u6536\u76ca\u4fdd\u6301\u5728\u4e00\u4e2a\u4f60\u89c9\u5f97\u6700\u8212\u670d\u7684\u72b6\u6001\u3002<\/p>\n\n\n\n<p>\u4e00\u822c\u6765\u8bf4\uff0c\u4f60\u53ef\u80fd\u4f1a\u7528\u5230\u4e00\u4e9b\u5de5\u5177\uff0c\u6bd4\u5982\u516c\u5141\u4ef7\u503c\u4f30\u7b97\u3001\u76ee\u6807\u4ef7\u683c\u6216\u8005\u8499\u7279\u5361\u7f57\u6a21\u62df\u6765\u5e2e\u4f60\u51b3\u5b9a\u6bcf\u53ea\u80a1\u7968\u8be5\u4e70\u591a\u5c11\u3002\u8fd9\u4e9b\u65b9\u6cd5\u786e\u5b9e\u80fd\u5e2e\u4f60\u5224\u65ad\u54ea\u4e9b\u80a1\u7968\u503c\u5f97\u7559\u7740\uff0c\u4ee5\u53ca\u6bcf\u53ea\u80a1\u7968\u8be5\u5360\u591a\u5c11\u6bd4\u4f8b\u3002\u4e0d\u8fc7\uff0c\u5f53\u4f60\u6253\u7b97\u52a0\u4ed3\u6216\u8005\u51cf\u4ed3\u7684\u65f6\u5019\uff08\u6bd4\u5982\u5f80\u6295\u8d44\u7ec4\u5408\u91cc\u52a0\u94b1\uff0c\u6216\u8005\u5728\u8c03\u6574\u65f6\u5356\u6389\u4e00\u90e8\u5206\uff09\uff0c\u77ed\u671f\u7684\u6280\u672f\u5206\u6790\u4e5f\u80fd\u7ed9\u4f60\u4e00\u4e9b\u989d\u5916\u7684\u53c2\u8003\u3002\u7279\u522b\u662f\u770b\u770b\u80a1\u7968\u7684\u652f\u6491\u4f4d\u548c\u963b\u529b\u4f4d\uff0c\u80fd\u8ba9\u4f60\u77e5\u9053\u5b83\u73b0\u5728\u7684\u4f4d\u7f6e\u79bb\u6700\u8fd1\u7684\u9ad8\u70b9\u6216\u4f4e\u70b9\u6709\u591a\u8fdc\u2014\u2014\u8fd9\u4e9b\u4fe1\u606f\u5bf9\u4f60\u8c03\u6574\u4e70\u5356\u70b9\u5f88\u6709\u5e2e\u52a9\uff0c\u800c\u4e14\u4e0d\u4f1a\u5f71\u54cd\u4f60\u957f\u671f\u7684\u6301\u80a1\u8ba1\u5212\u3002<\/p>\n\n\n\n<p>\u63a5\u4e0b\u6765\uff0c\u6211\u4f1a\u4e00\u6b65\u6b65\u6559\u4f60\u5982\u4f55\u7cfb\u7edf\u5730\u627e\u51fa\u652f\u6491\u4f4d\u548c\u963b\u529b\u4f4d\uff0c\u6a21\u62df\u4e00\u4e9b\u5047\u8bbe\u7684\u4ea4\u6613\uff0c\u6700\u540e\u751f\u6210\u4e00\u4e2a\u6e05\u6670\u6613\u61c2\u7684\u6570\u636e\u8868\u683c\u3002\u8fd9\u4e2a\u8868\u683c\u4e0d\u4ec5\u4f1a\u5217\u51fa\u4ea4\u6613\u7ec6\u8282\uff0c\u8fd8\u4f1a\u5305\u542b\u4e00\u4e2a\u201c\u6700\u65b0\u68c0\u6d4b\u201d\u7684\u5feb\u7167\u3002\u8fd9\u4e2a\u5feb\u7167\u80fd\u544a\u8bc9\u4f60\u54ea\u4e9b\u80a1\u7968\u73b0\u5728\u53ef\u80fd\u63a5\u8fd1\u652f\u6491\u4f4d\u6216\u963b\u529b\u4f4d\u2014\u2014\u8fd9\u4e9b\u4fe1\u606f\u53ef\u80fd\u4f1a\u5f71\u54cd\u4f60\u4e0b\u6b21\u8c03\u6574\u6295\u8d44\u7ec4\u5408\u65f6\u7684\u8d44\u91d1\u5206\u914d\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"8bec\"><strong>\u6b65\u9aa4 1\uff1a\u8bbe\u7f6e\u548c\u6570\u636e\u4e0b\u8f7d<\/strong><\/h2>\n\n\n\n<p id=\"5884\">\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u8bbe\u7f6e\u811a\u672c\uff0c\u7528\u6765\u5b9a\u4e49\u4e00\u4e9b\u6838\u5fc3\u53c2\u6570\uff0c\u5e76\u4ece\u96c5\u864e\u8d22\u7ecf\u83b7\u53d6\u6570\u636e\u3002\u8fd9\u4e9b\u53c2\u6570\u5305\u62ec\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>STOP_LOSS\uff1a10% \u7684\u540d\u4e49\u9608\u503c\uff0c\u5728\u6211\u4eec\u7684\u793a\u4f8b\u60c5\u666f\u4e2d\uff0c\u5982\u679c\u5e02\u573a\u8d70\u52bf\u5bf9\u5047\u8bbe\u5934\u5bf8\u4e0d\u5229\uff0c\u5219\u5f3a\u5236\u9000\u51fa\u3002<\/li>\n\n\n\n<li>\u7a0e\u7387\uff1a\u5bf9\u76c8\u5229\u4ea4\u6613\u6263\u7a0e 15%\uff0c\u4ee5\u6a21\u62df\u73b0\u5b9e\u4e16\u754c\u4e2d\u51c0\u6536\u76ca\u7684\u7a0e\u6536\u5f71\u54cd\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>import pandas as pd\nimport numpy as np\nimport yfinance as yf\nfrom datetime import datetime\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\nSTART_DATE = \"2020-09-01\"\nEND_DATE = \"2025-01-01\"\nINTERVAL = \"1d\"\n\nSTOP_LOSS = 0.10  # 10% hypothetical stop loss\nTAX_RATE = 0.15   # 15% tax rate on gains\n\n# Example: a portfolio of 10 stocks already held for the long term\nsymbols = &#91;\n    \"PRIO3.SA\", \"ITUB4.SA\", \"VALE3.SA\", \"ABEV3.SA\",\n    \"RENT3.SA\", \"WEGE3.SA\", \"B3SA3.SA\", \"JBSS3.SA\",\n    \"VIVT3.SA\", \"EGIE3.SA\"\n]<\/code><\/pre>\n\n\n\n<p id=\"edb1\">\u4e3a\u4fbf\u4e8e\u6f14\u793a\uff0c\u6211\u4eec\u6311\u9009\u4e86\u5341\u53ea\u80a1\u7968\uff08\u4ee5 .SA \u7ed3\u5c3e\uff09\u3002\u5b9e\u9645\u4e0a\uff0c\u60a8\u53ef\u4ee5\u5c06\u540c\u6837\u7684\u903b\u8f91\u5e94\u7528\u4e8e\u60a8\u62e5\u6709\u7684\u4efb\u4f55\u4e00\u7ec4\u80a1\u7968\u6216 ETF\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"72c4\"><strong>\u6b65\u9aa4 2\uff1a\u68c0\u6d4b\u5c40\u90e8\u6700\u5c0f\u503c\u548c\u6700\u5927\u503c<\/strong><\/h2>\n\n\n\n<p id=\"37f5\">\u4e0b\u9762\u662f\u5c06\u8d2f\u7a7f\u6211\u4eec\u6574\u4e2a\u4ee3\u7801\u7684\u4e09\u4e2a\u8f85\u52a9\u51fd\u6570\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def is_local_min(df, idx):\n    window_lows = df&#91;'Low'].iloc&#91;idx-2 : idx+3]\n    return df&#91;'Low'].iloc&#91;idx] == window_lows.min()\n\ndef is_local_max(df, idx):\n    window_highs = df&#91;'High'].iloc&#91;idx-2 : idx+3]\n    return df&#91;'High'].iloc&#91;idx] == window_highs.max()\n\ndef sufficiently_far(new_price, accepted_prices, threshold):\n    return all(abs(new_price - ap) &gt; threshold for ap in accepted_prices)<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>is_local_max\uff1a\u5bf9 &#8220;<code>High<\/code>&#8220;\u503c\u8fdb\u884c\u53cd\u5411\u64cd\u4f5c\uff0c\u5c06\u5176\u6807\u8bb0\u4e3a\u963b\u529b\u3002<\/li>\n\n\n\n<li><code><strong>sufficiently_far<\/strong><\/code>\uff1a\u4e3a\u4e86\u907f\u514d\u8bb0\u5f55\u8fc7\u4e8e\u63a5\u8fd1\u7684\u591a\u4e2a\u6c34\u5e73\uff0c\u6211\u4eec\u4f1a\u7528\u6700\u8fd1\u7684\u5e73\u5747K\u7ebf\u9ad8\u5ea6\u4f5c\u4e3a\u201c\u9608\u503c\u201d\uff08threshold\uff09\u3002\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u5927\u5bb6\u53ea\u5173\u6ce8\u90a3\u4e9b\u6709\u610f\u4e49\u7684\u652f\u6491\u4f4d\u548c\u963b\u529b\u4f4d\uff0c\u800c\u4e0d\u662f\u90a3\u4e9b\u8fc7\u4e8e\u63a5\u8fd1\u3001\u53ef\u80fd\u6ca1\u4ec0\u4e48\u53c2\u8003\u4ef7\u503c\u7684\u6c34\u5e73\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"1bb5\"><strong>\u672c\u5730\u6700\u5c0f\u503c\/\u6700\u5927\u503c\u793a\u4f8b<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5982\u679c\u60a8\u6709 [D-2\u3001D-1\u3001D\u3001D+1\u3001D+2] \u5929\u7684\u4ef7\u683c [10.00\u30019.80\u30019.75\u30019.84\u30019.90]\uff0c\u90a3\u4e48 D \u5929\u7684\u4f4e\u70b9 9.75 \u53ef\u80fd\u7b26\u5408\u672c\u5730\u652f\u6491\u6761\u4ef6&#8211;\u56e0\u4e3a\u5b83\u662f\u8be5 5 \u5929\u7a97\u53e3\u4e2d\u7684\u6700\u4f4e\u70b9\u3002<\/li>\n\n\n\n<li>\u76f8\u53cd\uff0c\u5982\u679c D \u65e5\u662f\u8fd9 5 \u5929\u5185\u7684\u6700\u9ad8\u70b9\uff0c\u5219\u4f1a\u88ab\u6807\u8bb0\u4e3a\u963b\u529b\u4f4d\u3002<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"1508\"><strong>\u6b65\u9aa4 3\uff1a\u6784\u5efa\u6570\u636e\u6846\u5e76\u7b5b\u9009\u652f\u6491\u4f4d\/\u963b\u529b\u4f4d<\/strong><\/h2>\n\n\n\n<p id=\"768f\">\u5efa\u7acb\u4e00\u4e2a\u7ed3\u6784\u6765\u5b58\u50a8\u5206\u6790\u7ed3\u679c\u2014\u2014\u65e2\u5305\u62ec\u5047\u8bbe\u7684\u4ea4\u6613\u65b9\u6848\uff0c\u4e5f\u6709\u201c\u6700\u540e\u68c0\u6d4b\u201d\uff08last detection\uff09\u7684\u6982\u89c8\u3002\u63a5\u7740\u4e3a\u6bcf\u53ea\u80a1\u7968\u4e0b\u8f7d\u5386\u53f2\u6570\u636e\uff0c\u6807\u8bb0\u51fa\u5c40\u90e8\u7684\u6700\u4f4e\u70b9\u548c\u6700\u9ad8\u70b9\uff0c\u7136\u540e\u8fc7\u6ee4\u6389\u4e0d\u91cd\u8981\u7684\u90e8\u5206\uff0c\u53ea\u4fdd\u7559\u90a3\u4e9b\u771f\u6b63\u6709\u53c2\u8003\u4ef7\u503c\u7684\u652f\u6491\u4f4d\u548c\u963b\u529b\u4f4d\u3002\u8fd9\u6837\uff0c\u6211\u4eec\u5c31\u80fd\u66f4\u6e05\u6670\u5730\u770b\u5230\u54ea\u4e9b\u4f4d\u7f6e\u53ef\u80fd\u5bf9\u672a\u6765\u7684\u4ea4\u6613\u51b3\u7b56\u6709\u5e2e\u52a9\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>all_trades_long = &#91;]\nall_trades_short = &#91;]\nlast_detection_list = &#91;]\n\nfor symbol in symbols:\n    data = yf.download(symbol, start=START_DATE, end=END_DATE, interval=INTERVAL)\n    if data.empty:\n        continue\n\n    # Adjust multi-index if needed\n    if isinstance(data.columns, pd.MultiIndex):\n        data.columns = data.columns.droplevel(0)\n    data.columns = &#91;'Adj Close', 'Close', 'High', 'Low', 'Open', 'Volume']\n    data.reset_index(inplace=True)\n\n    df = data&#91;&#91;'Date', 'Open', 'High', 'Low', 'Close']].copy()\n    df&#91;'isSupport'] = False\n    df&#91;'isResistance'] = False\n    df&#91;'AvgCandleHeight'] = np.nan\n\n    # Mark local minima\/maxima\n    for t in range(len(df)):\n        j = t - 2\n        if j - 2 &gt;= 0 and j + 2 &lt; len(df):\n            if is_local_min(df, j):\n                df.loc&#91;j, 'isSupport'] = True\n            if is_local_max(df, j):\n                df.loc&#91;j, 'isResistance'] = True\n\n        # Rolling ~32-day average candle height\n        window_start = max(0, t - 32 + 1)\n        sub_df = df.iloc&#91;window_start : t+1]\n        if len(sub_df) &gt; 0:\n            df.loc&#91;t, 'AvgCandleHeight'] = np.mean(sub_df&#91;'High'] - sub_df&#91;'Low'])\n\n    # Gather raw levels\n    raw_levels = &#91;]\n    for i in range(len(df)):\n        if df&#91;'isSupport'].iloc&#91;i]:\n            raw_levels.append({'index': i, 'price': df&#91;'Low'].iloc&#91;i], 'type': 'support'})\n        elif df&#91;'isResistance'].iloc&#91;i]:\n            raw_levels.append({'index': i, 'price': df&#91;'High'].iloc&#91;i], 'type': 'resistance'})\n\n    # Filter out levels that are too close\n    final_levels = &#91;]\n    unique_prices = &#91;]\n    for lvl in raw_levels:\n        idx = lvl&#91;'index']\n        candle_height = df&#91;'AvgCandleHeight'].iloc&#91;idx]\n        if np.isnan(candle_height):\n            continue\n        if sufficiently_far(lvl&#91;'price'], unique_prices, candle_height):\n            final_levels.append(lvl)\n            unique_prices.append(lvl&#91;'price'])<\/code><\/pre>\n\n\n\n<p id=\"43f4\">\u5047\u8bbe\u5e73\u5747K\u7ebf\u9ad8\u5ea6\u662f 0.50 \u7f8e\u5143\uff0c\u800c\u4e14\u6211\u4eec\u5df2\u7ecf\u786e\u5b9a\u4e86 20.00 \u7f8e\u5143\u662f\u4e00\u4e2a\u652f\u6491\u4f4d\u3002\u5982\u679c\u63a5\u4e0b\u6765\u53c8\u51fa\u73b0\u4e86\u4e00\u4e2a\u6f5c\u5728\u7684\u652f\u6491\u4f4d\uff0c\u6bd4\u5982 20.10 \u7f8e\u5143\uff0c\u6211\u4eec\u4f1a\u8ba1\u7b97\u5b83\u4e0e\u4e4b\u524d\u652f\u6491\u4f4d\u7684\u8ddd\u79bb\u3002\u5982\u679c abs(20.10 &#8211; 20.00) &lt; 0.50\uff0c\u4e5f\u5c31\u662f\u8bf4\u4e24\u8005\u5dee\u8ddd\u5c0f\u4e8e 0.50 \u7f8e\u5143\uff0c\u90a3\u53ef\u80fd\u4f1a\u5ffd\u7565\u8fd9\u4e2a\u65b0\u7684\u652f\u6491\u4f4d\u3002\u8fd9\u6837\u4e00\u6765\uff0c\u5c31\u80fd\u907f\u514d\u5728\u56fe\u8868\u4e0a\u6807\u8bb0\u51fa\u592a\u591a\u51e0\u4e4e\u76f8\u540c\u7684\u6c34\u5e73\u7ebf\uff0c\u8ba9\u6700\u7ec8\u7684\u652f\u6491\u4f4d\u548c\u963b\u529b\u4f4d\u66f4\u52a0\u6e05\u6670\u3001\u66f4\u6709\u53c2\u8003\u4ef7\u503c\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"fdaf\"><strong>\u6b65\u9aa4 4\uff1a\u8bb0\u5f55 &#8220;\u6700\u540e\u68c0\u6d4b<strong>&#8220;<\/strong>\uff08Last Detection\uff09<\/strong><\/h2>\n\n\n\n<p id=\"303a\">\u5bf9\u4e8e\u6bcf\u4e2a\u7b26\u53f7\uff0c\u6211\u4eec\u90fd\u4f1a\u67e5\u770b\u6700\u8fd1\uff08\u4e0a\u6b21\uff09\u786e\u5b9a\u7684\u652f\u6491\u4f4d\/\u963b\u529b\u4f4d\u3002\u8fd9\u5728\u60a8\u51b3\u5b9a\u5f53\u524d\u4ef7\u683c\u662f\u5426\u662f\u652f\u6491\u4f4d\/\u963b\u529b\u4f4d\u65f6\u5c24\u4e3a\u91cd\u8981\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u9ad8\u4e8e\u8fd1\u671f\u652f\u6491\u4f4d\uff08\u6216\u8bb8\u662f\u4e2a\u597d\u5146\u5934\uff0c\u6216\u9ad8\u4e8e\u8be5\u652f\u6491\u4f4d ~5%\uff09\u3002<\/li>\n\n\n\n<li>\u4f4e\u4e8e\u8fd1\u671f\u963b\u529b\u4f4d\uff08\u4f8b\u5982\uff0c\u5982\u679c\u963b\u529b\u4f4d\u4e0b\u4ec5\u6709-3%\uff0c\u53ef\u80fd\u8981\u8c28\u614e\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>    if len(final_levels) &gt; 0:\n        last_level = final_levels&#91;-1]\n        last_detection_idx = last_level&#91;'index']\n        last_detection_date = df&#91;'Date'].iloc&#91;last_detection_idx]\n        last_detection_price = last_level&#91;'price']\n        last_close_price = df&#91;'Close'].iloc&#91;-1]\n\n        diff_rel = (last_close_price \/ last_detection_price) - 1.0\n\n        last_detection_list.append({\n            'symbol': symbol,\n            'last_detection_type': last_level&#91;'type'],\n            'last_detection_date': last_detection_date.strftime('%Y-%m-%d'),\n            'last_detection_price': round(last_detection_price, 2),\n            'last_date': df&#91;'Date'].iloc&#91;-1].strftime('%Y-%m-%d'),\n            'last_close_price': round(last_close_price, 2),\n            'diff_relative (%)': round(diff_rel * 100, 2)\n        })<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"fd28\"><strong>\u5feb\u901f\u793a\u4f8b<\/strong><\/h3>\n\n\n\n<p id=\"3d6b\">\u5982\u679c\u4e0a\u4e00\u6b21\u68c0\u6d4b\u5230\u7684\u652f\u6491\u4f4d\u662f 30 \u7f8e\u5143\uff0c\u800c\u6700\u7ec8\u6536\u76d8\u4ef7\u662f 33 \u7f8e\u5143\uff0c\u90a3\u4e48<code>diff_relative (%)<\/code>\u5927\u7ea6\u4e3a +10%\u3002\u8fd9\u53ef\u80fd\u8868\u660e\uff0c\u8be5\u80a1\u5728\u5176\u6700\u8fd1\u7684\u652f\u6491\u4f4d\u4e0a\u65b9\u5904\u4e8e\u8212\u9002\u7684\u4f4d\u7f6e\u3002\u4ece\u518d\u5e73\u8861\u7684\u89d2\u5ea6\u6765\u8bf4\uff0c\u4f60\u53ef\u4ee5\u628a\u5b83\u7406\u89e3\u4e3a &#8220;\u8be5\u80a1\u6700\u8fd1\u4ece\u652f\u6491\u4f4d\u53cd\u5f39&#8221;\uff0c\u8fd9\u53ef\u80fd\u662f\u4f60\u51b3\u7b56\u8fc7\u7a0b\u7684\u4e00\u90e8\u5206\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"3e87\"><strong>\u6b65\u9aa4 5\uff1a &#8220;Long&#8221;\u548c &#8220;Short&#8221;\u65b9\u6848<\/strong><\/h2>\n\n\n\n<p id=\"8c1c\">\u6211\u4eec\u6a21\u62df\u4e86\u4e24\u79cd\u5047\u8bbe\u65b9\u6cd5\uff0c\u4ee5\u8bf4\u660e\u5728\u6295\u8d44\u7ba1\u7406\u4e2d\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u652f\u6491\u4f4d\/\u963b\u529b\u4f4d\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5728\u652f\u6491\u4f4d\u540e 3 \u5929\u4e70\u5165\uff08\u505a\u591a\uff09\uff0c\u5728\u4e0b\u4e00\u4e2a\u963b\u529b\u4f4d\u540e 3 \u5929\u5356\u51fa\u3002<\/li>\n\n\n\n<li>\u5728\u963b\u529b\u4f4d\u540e 3 \u5929\u505a\u7a7a\uff08\u5356\u51fa\uff09\uff0c\u5728\u4e0b\u4e00\u4e2a\u652f\u6491\u4f4d\u540e 3 \u5929\u4e70\u56de\u6765\u3002<\/li>\n<\/ul>\n\n\n\n<p id=\"47e6\">3 \u5929\u7684\u5ef6\u8fdf\u5e76\u4e0d\u662f\u968f\u4fbf\u5b9a\u7684\u2014\u2014\u8fd9\u662f\u56e0\u4e3a\u6211\u4eec\u9700\u8981\u591a\u89c2\u5bdf 2 \u5929\uff0c\u624d\u80fd\u786e\u8ba4\u67d0\u4e00\u5929\u7684\u4ef7\u683c\u662f\u4e0d\u662f\u5c40\u90e8\u7684\u6700\u4f4e\u70b9\u6216\u6700\u9ad8\u70b9\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u53ea\u6709\u7b49\u5230\u7b2c 3 \u5929\uff0c\u6211\u4eec\u624d\u80fd\u6709\u8db3\u591f\u7684\u4f9d\u636e\u6765\u5224\u65ad\u662f\u5426\u771f\u7684\u51fa\u73b0\u4e86\u652f\u6491\u4f4d\u6216\u963b\u529b\u4f4d\uff0c\u7136\u540e\u624d\u80fd\u51b3\u5b9a\u662f\u5426\u91c7\u53d6\u884c\u52a8\u3002\u8fd9\u6837\u505a\u7684\u76ee\u7684\u662f\u4e3a\u4e86\u907f\u514d\u8bef\u5224\uff0c\u786e\u4fdd\u6211\u4eec\u7684\u5206\u6790\u66f4\u51c6\u786e\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"1520\"><strong>\u957f\u671f\uff08Long\uff09\u6218\u7565\u793a\u4f8b<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>    trades_long = &#91;]\n    for i in range(len(final_levels) - 1):\n        if final_levels&#91;i]&#91;'type'] == 'support' and final_levels&#91;i+1]&#91;'type'] == 'resistance':\n            support_idx = final_levels&#91;i]&#91;'index']\n            resistance_idx = final_levels&#91;i+1]&#91;'index']\n            if resistance_idx &lt;= support_idx:\n                continue\n\n            real_entry_idx = support_idx + 3\n            real_exit_idx = resistance_idx + 3\n            if real_entry_idx &gt;= len(df) or real_exit_idx &gt;= len(df):\n                continue\n\n            real_entry_price = df&#91;'Close'].iloc&#91;real_entry_idx]\n            real_exit_price = df&#91;'Close'].iloc&#91;real_exit_idx]\n\n            # Hypothetical 10% stop-loss\n            max_loss_price = real_entry_price * (1 - STOP_LOSS)\n            slice_min = df&#91;'Low'].iloc&#91;real_entry_idx : real_exit_idx+1].min()\n            if slice_min &lt; max_loss_price:\n                stop_day = df&#91;'Low'].iloc&#91;real_entry_idx : real_exit_idx+1].idxmin()\n                real_exit_idx = stop_day\n                real_exit_price = max_loss_price\n\n            gross_return = (real_exit_price \/ real_entry_price) - 1.0\n            net_return = gross_return * (1 - TAX_RATE) if gross_return &gt; 0 else gross_return\n\n            support_price = final_levels&#91;i]&#91;'price']\n            resistance_price = final_levels&#91;i+1]&#91;'price']\n            entry_vs_support_pct_diff = (real_entry_price \/ support_price) - 1.0\n            exit_vs_resistance_pct_diff = (real_exit_price \/ resistance_price) - 1.0\n            trade_days = (df&#91;'Date'].iloc&#91;real_exit_idx] - df&#91;'Date'].iloc&#91;real_entry_idx]).days\n            daily_net_return = (1 + net_return)**(1 \/ trade_days) - 1 if trade_days &gt; 0 else 0.0\n\n            trades_long.append({\n                'support_date': df&#91;'Date'].iloc&#91;support_idx].strftime('%Y-%m-%d'),\n                'resistance_date': df&#91;'Date'].iloc&#91;resistance_idx].strftime('%Y-%m-%d'),\n                'support_price': round(support_price, 2),\n                'resistance_price': round(resistance_price, 2),\n                'real_entry_date': df&#91;'Date'].iloc&#91;real_entry_idx].strftime('%Y-%m-%d'),\n                'real_exit_date': df&#91;'Date'].iloc&#91;real_exit_idx].strftime('%Y-%m-%d'),\n                'real_entry_price': round(real_entry_price, 2),\n                'real_exit_price': round(real_exit_price, 2),\n                'entry_vs_support_pct_diff': round(entry_vs_support_pct_diff * 100, 2),\n                'exit_vs_resistance_pct_diff': round(exit_vs_resistance_pct_diff * 100, 2),\n                'gross_return (%)': round(gross_return * 100, 2),\n                'net_return (%)': round(net_return * 100, 2),\n                'daily_net_return (%)': round(daily_net_return * 100, 2),\n                'symbol': symbol\n            })\n\n    all_trades_long.extend(trades_long)<\/code><\/pre>\n\n\n\n<p id=\"717d\">\u5728\u5b9e\u9645\u7684\u6295\u8d44\u7ba1\u7406\u4e2d\uff0c\u8fd9\u79cd\u505a\u6cd5\u53ef\u80fd\u76f8\u5f53\u4e8e\uff1a\u5f53\u4f60\u53d1\u73b0\u67d0\u53ea\u80a1\u7968\u7684\u4ef7\u683c\u660e\u663e\u89e6\u5e95\uff08\u652f\u6491\u4f4d\uff09\u65f6\uff0c\u8003\u8651\u662f\u5426\u8981\u52a0\u4ed3\uff1b\u800c\u5f53\u4ef7\u683c\u63a5\u8fd1\u9876\u90e8\uff08\u963b\u529b\u4f4d\uff09\u65f6\uff0c\u8003\u8651\u662f\u5426\u8981\u51cf\u4ed3\uff0c\u964d\u4f4e\u98ce\u9669\u3002<\/p>\n\n\n\n<p id=\"717d\">\u4e0d\u8fc7\uff0c\u8fd9\u4e9b\u64cd\u4f5c\u53ea\u662f\u4f5c\u4e3a\u957f\u671f\u6295\u8d44\u7b56\u7565\u7684\u8865\u5145\uff0c\u76ee\u7684\u662f\u5728\u4fdd\u6301\u957f\u671f\u6301\u4ed3\u7684\u540c\u65f6\uff0c\u5229\u7528\u77ed\u671f\u7684\u5e02\u573a\u6ce2\u52a8\u6765\u4f18\u5316\u6536\u76ca\u548c\u98ce\u9669\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"f7fb\"><strong>\u77ed\u671f\uff08Short\uff09\u7b56\u7565\u793a\u4f8b<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>    trades_short = &#91;]\n    for i in range(len(final_levels) - 1):\n        if final_levels&#91;i]&#91;'type'] == 'resistance' and final_levels&#91;i+1]&#91;'type'] == 'support':\n            resistance_idx = final_levels&#91;i]&#91;'index']\n            support_idx = final_levels&#91;i+1]&#91;'index']\n            if support_idx &lt;= resistance_idx:\n                continue\n\n            real_entry_idx = resistance_idx + 3\n            real_exit_idx = support_idx + 3\n            if real_entry_idx &gt;= len(df) or real_exit_idx &gt;= len(df):\n                continue\n\n            real_entry_price = df&#91;'Close'].iloc&#91;real_entry_idx]\n            real_exit_price = df&#91;'Close'].iloc&#91;real_exit_idx]\n\n            # 10% stop-loss if price rises\n            max_loss_price = real_entry_price * (1 + STOP_LOSS)\n            slice_max = df&#91;'High'].iloc&#91;real_entry_idx : real_exit_idx+1].max()\n            if slice_max &gt; max_loss_price:\n                stop_day = df&#91;'High'].iloc&#91;real_entry_idx : real_exit_idx+1].idxmax()\n                real_exit_idx = stop_day\n                real_exit_price = max_loss_price\n\n            gross_return = (real_entry_price - real_exit_price) \/ real_entry_price\n            net_return = gross_return * (1 - TAX_RATE) if gross_return &gt; 0 else gross_return\n\n            resistance_price = final_levels&#91;i]&#91;'price']\n            support_price = final_levels&#91;i+1]&#91;'price']\n            entry_vs_resistance_pct_diff = (real_entry_price \/ resistance_price) - 1.0\n            exit_vs_support_pct_diff = (real_exit_price \/ support_price) - 1.0\n            trade_days = (df&#91;'Date'].iloc&#91;real_exit_idx] - df&#91;'Date'].iloc&#91;real_entry_idx]).days\n            daily_net_return = (1 + net_return)**(1 \/ trade_days) - 1 if trade_days &gt; 0 else 0.0\n\n            trades_short.append({\n                'resistance_date': df&#91;'Date'].iloc&#91;resistance_idx].strftime('%Y-%m-%d'),\n                'support_date': df&#91;'Date'].iloc&#91;support_idx].strftime('%Y-%m-%d'),\n                'resistance_price': round(resistance_price, 2),\n                'support_price': round(support_price, 2),\n                'real_entry_date': df&#91;'Date'].iloc&#91;real_entry_idx].strftime('%Y-%m-%d'),\n                'real_exit_date': df&#91;'Date'].iloc&#91;real_exit_idx].strftime('%Y-%m-%d'),\n                'real_entry_price': round(real_entry_price, 2),\n                'real_exit_price': round(real_exit_price, 2),\n                'entry_vs_resistance_pct_diff': round(entry_vs_resistance_pct_diff * 100, 2),\n                'exit_vs_support_pct_diff': round(exit_vs_support_pct_diff * 100, 2),\n                'gross_return (%)': round(gross_return * 100, 2),\n                'net_return (%)': round(net_return * 100, 2),\n                'daily_net_return (%)': round(daily_net_return * 100, 2),\n                'symbol': symbol\n            })\n\n    all_trades_short.extend(trades_short)<\/code><\/pre>\n\n\n\n<p id=\"86c2\">\u867d\u7136\u505a\u7a7a\uff08\u7a7a\u5934\u5934\u5bf8\uff09\u901a\u5e38\u4e0d\u662f\u957f\u671f\u3001\u57fa\u672c\u9762\u9a71\u52a8\u7684\u6295\u8d44\u7ec4\u5408\u7684\u5e38\u89c1\u64cd\u4f5c\uff0c\u4f46\u4e86\u89e3\u8fd9\u4e9b\u4fe1\u53f7\u4ecd\u7136\u5f88\u6709\u7528\u3002\u6bd4\u5982\uff0c\u5982\u679c\u4e00\u53ea\u80a1\u7968\u591a\u6b21\u5728\u67d0\u4e2a\u963b\u529b\u4f4d\u9644\u8fd1\u6389\u5934\u4e0b\u8dcc\uff0c\u4f60\u53ef\u80fd\u4f1a\u66f4\u613f\u610f\u7b49\u5f85\u4e00\u4e2a\u66f4\u597d\u7684\u4e70\u5165\u673a\u4f1a\uff0c\u800c\u4e0d\u662f\u5728\u63a5\u8fd1\u963b\u529b\u4f4d\u7684\u65f6\u5019\u52a0\u4ed3\u3002\u8fd9\u6837\u4e00\u6765\uff0c\u4f60\u5c31\u80fd\u907f\u514d\u5728\u9ad8\u70b9\u9644\u8fd1\u4e70\u5165\uff0c\u964d\u4f4e\u77ed\u671f\u98ce\u9669\uff0c\u540c\u65f6\u4f9d\u7136\u4fdd\u6301\u5bf9\u957f\u671f\u6295\u8d44\u7684\u4fe1\u5fc3\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"a8aa\"><strong>\u6b65\u9aa4 6\uff1a\u5408\u5e76\u548c\u89e3\u91ca\u7ed3\u679c<\/strong><\/h2>\n\n\n\n<p id=\"9c04\">\u5f53\u6211\u4eec\u5904\u7406\u5b8c\u6240\u6709\u80a1\u7968\u540e\uff0c\u4f1a\u5206\u522b\u521b\u5efa\u201c\u591a\u5934\u201d\u548c\u201c\u7a7a\u5934\u201d\u4ea4\u6613\u7684 DataFrames\uff0c\u7136\u540e\u8ba1\u7b97\u6bcf\u53ea\u80a1\u7968\u7684\u76f8\u5173\u7edf\u8ba1\u6570\u636e\uff0c\u6bd4\u5982\u6b63\u51c0\u6536\u76ca\u7684\u6982\u7387\u3001\u4ef7\u683c\u4e0e\u652f\u6491\u4f4d\/\u963b\u529b\u4f4d\u7684\u5e73\u5747\u5dee\u8ddd\u7b49\u3002\u63a5\u7740\uff0c\u628a\u8fd9\u4e9b\u7edf\u8ba1\u6570\u636e\u6574\u5408\u5230\u6700\u540e\u68c0\u6d4b\u6570\u636e\u96c6\uff08last_detection_df\uff09\u4e2d\uff0c\u8fd9\u6837\u6bcf\u53ea\u80a1\u7968\u90fd\u4f1a\u6709\u4e00\u4e2a\u6e05\u6670\u7684\u5feb\u7167\uff0c\u65b9\u4fbf\u4f60\u5feb\u901f\u4e86\u89e3\u5b83\u7684\u5f53\u524d\u72b6\u6001\u548c\u6f5c\u5728\u4ea4\u6613\u673a\u4f1a\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>trade_results_long = pd.DataFrame(all_trades_long)\ntrade_results_short = pd.DataFrame(all_trades_short)\n\n# Probability of net return &gt; 0, etc.\nif not trade_results_long.empty:\n    group_long = trade_results_long.groupby('symbol')\n    df_long_metrics = group_long.agg({\n        'net_return (%)': lambda x: np.mean(x &gt; 0) * 100,\n        'entry_vs_support_pct_diff': 'mean'\n    }).reset_index()\n    df_long_metrics.rename(columns={\n        'net_return (%)': 'prob_win_long (%)',\n        'entry_vs_support_pct_diff': 'avg_entry_vs_support_pct_diff'\n    }, inplace=True)\nelse:\n    df_long_metrics = pd.DataFrame(columns=&#91;'symbol','prob_win_long (%)','avg_entry_vs_support_pct_diff'])\n\nif not trade_results_short.empty:\n    group_short = trade_results_short.groupby('symbol')\n    df_short_metrics = group_short.agg({\n        'net_return (%)': lambda x: np.mean(x &gt; 0) * 100,\n        'entry_vs_resistance_pct_diff': 'mean'\n    }).reset_index()\n    df_short_metrics.rename(columns={\n        'net_return (%)': 'prob_win_short (%)',\n        'entry_vs_resistance_pct_diff': 'avg_entry_vs_resistance_pct_diff'\n    }, inplace=True)\nelse:\n    df_short_metrics = pd.DataFrame(columns=&#91;'symbol','prob_win_short (%)','avg_entry_vs_resistance_pct_diff'])\n\ndf_merged_metrics = pd.merge(df_long_metrics, df_short_metrics, on='symbol', how='outer')\n\nlast_detection_df = pd.DataFrame(last_detection_list)\nlast_detection_df = pd.merge(last_detection_df, df_merged_metrics, on='symbol', how='left')\n\nlast_detection_df.rename(columns={\n    'prob_win_long (%)': 'prob_win_long',\n    'avg_entry_vs_support_pct_diff': 'avg_entry_vs_support_pct_diff',\n    'prob_win_short (%)': 'prob_win_short',\n    'avg_entry_vs_resistance_pct_diff': 'avg_entry_vs_resistance_pct_diff'\n}, inplace=True)\n\nlast_detection_df&#91;&#91;'prob_win_long','avg_entry_vs_support_pct_diff',\n                   'prob_win_short','avg_entry_vs_resistance_pct_diff']] = \\\n    round(last_detection_df&#91;&#91;'prob_win_long','avg_entry_vs_support_pct_diff',\n                       'prob_win_short','avg_entry_vs_resistance_pct_diff']].fillna(0), 2)<\/code><\/pre>\n\n\n\n<p id=\"8e7d\">\u6700\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u5bf9\u8868\u683c\u8fdb\u884c\u68c0\u67e5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>print(\"=== TRADE RESULTS (LONG) ===\\n\")\nprint(trade_results_long)\n\nprint(\"\\n=== TRADE RESULTS (SHORT) ===\\n\")\nprint(trade_results_short)\n\nprint(\"\\n=== LAST DETECTION DF ===\\n\")\nprint(last_detection_df)<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"204d\"><strong>\u6b65\u9aa4 7\uff1a\u8f93\u51fa\u7ed3\u679c<\/strong><\/h2>\n\n\n\n<p id=\"f371\">\u4ece\u6837\u672c\u6570\u636e\u6765\u770b\uff0c\u6211\u4eec\u53ef\u80fd\u4f1a\u53d1\u73b0\u6709 63 \u7b14\u201c\u591a\u5934\u201d\u4ea4\u6613\u548c 62 \u7b14\u201c\u7a7a\u5934\u201d\u4ea4\u6613\u3002\u66f4\u91cd\u8981\u7684\u662f\uff0clast_detection_df \u4f1a\u663e\u793a\u6bcf\u53ea\u80a1\u7968\u6700\u8fd1\u7684\u652f\u6491\u4f4d\u6216\u963b\u529b\u4f4d\u4e8b\u4ef6\uff0c\u4ee5\u53ca\u5f53\u524d\u6536\u76d8\u4ef7\u4e0e\u8fd9\u4e9b\u5173\u952e\u6c34\u5e73\u7684\u5173\u7cfb\u3002\u4e3e\u4e2a\u4f8b\u5b50\uff0c\u5982\u679c\u67d0\u53ea\u80a1\u7968\u7684\u6536\u76d8\u4ef7\u63a5\u8fd1\u652f\u6491\u4f4d\uff0c\u8fd9\u53ef\u80fd\u662f\u4e00\u4e2a\u6f5c\u5728\u7684\u4e70\u5165\u673a\u4f1a\uff1b\u800c\u5982\u679c\u5b83\u63a5\u8fd1\u963b\u529b\u4f4d\uff0c\u5219\u53ef\u80fd\u610f\u5473\u7740\u9700\u8981\u8c28\u614e\uff0c\u751a\u81f3\u8003\u8651\u51cf\u4ed3\u3002\u8fd9\u4e2a\u6570\u636e\u96c6\u80fd\u5e2e\u4f60\u5feb\u901f\u5224\u65ad\u6bcf\u53ea\u80a1\u7968\u7684\u5f53\u524d\u72b6\u6001\uff0c\u4e3a\u4e0b\u4e00\u6b65\u64cd\u4f5c\u63d0\u4f9b\u53c2\u8003\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>last_detection_type \u7684\u652f\u6491\u4ef7\u4e3a 29.72 \u7f8e\u5143\uff082024-12-18\uff09\uff1b<\/li>\n\n\n\n<li><code>last_close_price<\/code>\uff08\u6700\u540e\u6536\u76d8\u4ef7\uff09\u4e3a 32.20 \u7f8e\u5143\uff1b<\/li>\n\n\n\n<li>diff_relative (%) \u4e3a +8.34\u3002<\/li>\n<\/ul>\n\n\n\n<p id=\"727e\">\u8fd9\u8868\u660e\u80a1\u4ef7\u6bd4\u6700\u8fd1\u7684\u652f\u6491\u4f4d\u9ad8\u51fa\u7ea6 8.34%&#8211;\u53ef\u80fd\u662f\u77ed\u671f\u52bf\u5934\u7a33\u5b9a\u7684\u8ff9\u8c61\u3002\u53e6\u4e00\u4e2a\u7b26\u53f7\u53ef\u80fd\u4f1a\u51fa\u73b0\u963b\u529b\u68c0\u6d4b\uff0c\u4ef7\u683c\u5728\u8be5\u963b\u529b\u4f4d\u4e0b-16.15%\uff0c\u8868\u660e\u80a1\u4ef7\u53ef\u80fd\u4ece\u9ad8\u4f4d\u533a\u57df\u56de\u843d\u3002<\/p>\n\n\n\n<p id=\"b535\">\u8fd8\u8bf7\u6ce8\u610f prob_win_long \u548c prob_win_short\u4e24\u680f\uff0c\u5b83\u4eec\u53cd\u6620\u4e86\u5728 &#8220;\u505a\u591a &#8220;\u6216 &#8220;\u505a\u7a7a &#8220;\u6a21\u62df\u4e2d\uff0c\u6bcf\u4e2a\u7b26\u53f7\u7684\u5047\u8bbe\u51c0\u6536\u76ca\u4ee5\u6b63\u6570\u7ed3\u675f\u7684\u9891\u7387\uff08\u4ee5\u767e\u5206\u6bd4\u8868\u793a\uff09\u3002\u5982\u679c\u67d0\u4e2a\u80a1\u7968\u7684\u591a\u5934\u6982\u7387\u8f83\u9ad8\uff08\u4f8b\u5982 66.7%\uff09\uff0c\u5219\u8868\u793a\u6211\u4eec\u6a21\u62df\u7684\u8be5\u80a1\u7968\u591a\u5934\u4ea4\u6613\u5728\u6263\u9664\u7a0e\u6b3e\u548c\u6b62\u635f\u68c0\u67e5\u540e\u901a\u5e38\u90fd\u80fd\u83b7\u5f97\u6536\u76ca\u3002\u4e0e\u6b64\u540c\u65f6\uff0c\u5982\u679c\u67d0\u4e2a\u80a1\u7968\u7684\u8d62\u5229\u6982\u7387\uff08prob_win_short\uff09\u8f83\u9ad8\uff0c\u5219\u8868\u660e\u8be5\u80a1\u7968\u7ecf\u5e38\u4ece\u5c40\u90e8\u6700\u5927\u503c\u56de\u64a4\uff0c\u56e0\u4e3a\u8fd9\u4e9b\u5047\u5b9a\u7684 &#8220;\u7a7a\u5934 &#8220;\u4ea4\u6613\u5728\u4e00\u534a\u4ee5\u4e0a\u7684\u65f6\u95f4\u91cc\u90fd\u80fd\u83b7\u5f97\u6b63\u6536\u76ca\u3002<\/p>\n\n\n\n<p id=\"c7c2\">\u4e0b\u9762\u662f\u6700\u7ec8\u8f93\u51fa\u7ed3\u679c\u7684\u793a\u4f8b\u7247\u6bb5\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image\" id=\"89fa\"><img decoding=\"async\" src=\"https:\/\/cdn-images-1.readmedium.com\/v2\/resize:fit:800\/1*vlJRt5akfNwosx6SRGT7UQ.png\" alt=\"\"\/><\/figure>\n\n\n\n<p id=\"9965\">\u8bf7\u6ce8\u610f\uff0c\u4e0d\u540c\u7b26\u53f7\u7684 prob_win_long \u548c prob_win_short \u5dee\u5f02\u5f88\u5927\u3002\u60a8\u53ef\u80fd\u4f1a\u53d1\u73b0\uff0c\u50cf ABEV3.SA \u8fd9\u6837\u7684\u80a1\u7968\uff0c\u5176\u6982\u7387_win_short \u4e3a 80%\uff0c\u8fd9\u8868\u660e\u5728\u6211\u4eec\u7684\u6a21\u62df\u4e2d\uff0c\u5b83\u7ecf\u5e38\u4f1a\u4ece\u5c40\u90e8\u5cf0\u503c\u56de\u8c03\uff1b\u800c\u50cf RENT3.SA \u8fd9\u6837\u7684\u80a1\u7968\uff0c\u5176\u6982\u7387_win_long \u4e3a 80%\uff0c\u8fd9\u8868\u660e\u5b83\u5f80\u5f80\u4f1a\u4ece\u652f\u6491\u4f4d\u53cd\u5f39\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"403d\"><strong>\u6b65\u9aa4 8\uff1a\u6dfb\u52a0\u89c6\u89c9\u5143\u7d20\uff08\u6982\u7387\u4e0e\u5206\u5e03<\/strong>\uff09<\/h2>\n\n\n\n<p id=\"2989\">\u4e3a\u4e86\u5feb\u901f\u4e86\u89e3\u8fd9\u4e9b\u6982\u7387\uff0c\u6211\u4eec\u53ef\u4ee5\u7ed8\u5236\u7b80\u5355\u7684\u8ba1\u91cf\u56fe\uff0c\u6bd4\u8f83\u6b63\u51c0\u6536\u76ca\u7684\u6bd4\u4f8b\uff08\u6240\u6709\u591a\u5934\u4ea4\u6613\u4e0e\u6240\u6709\u7a7a\u5934\u4ea4\u6613\u7684\u6bd4\u4f8b\uff09\u3002\u8981\u60f3\u83b7\u5f97\u66f4\u7cbe\u7ec6\u7684\u89c6\u56fe\uff0c\u8fd8\u53ef\u4ee5\u6309\u7b26\u53f7\u7ed8\u5236\u6bcf\u65e5\u51c0\u56de\u62a5\u7684\u201c\u5c0f\u63d0\u7434\u56fe\u201d\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u8bf4\u660e\u5982\u4f55\u521b\u5efa\u4e24\u4e2a\u5e76\u6392\u7684\u8ba1\u91cf\u56fe\uff0c\u4e00\u4e2a\u662f\u591a\u5934\u6982\u7387\u6c47\u603b\u56fe\uff0c\u53e6\u4e00\u4e2a\u662f\u7a7a\u5934\u6982\u7387\u6c47\u603b\u56fe\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\npositive_returns_long = trade_results_long&#91;'daily_net_return (%)'] &gt; 0\nprobability_positive_long = positive_returns_long.mean()\n\npositive_returns_short = trade_results_short&#91;'daily_net_return (%)'] &gt; 0\nprobability_positive_short = positive_returns_short.mean()\n\n# --- 1) Gauge Charts Side by Side ---\nfig, (ax1, ax2) = plt.subplots(1, 2, figsize=(20, 1.5))\n\n# Left: LONG\ncolor_long = 'green' if probability_positive_long &gt; 0.5 else 'red'\nax1.barh(&#91;0], &#91;probability_positive_long], color=color_long, height=0.4, alpha=0.8)\nax1.barh(&#91;0], &#91;0.5], color='gray', height=0.4, alpha=0.3)\nax1.set_xlim(0, 1)\nax1.set_yticks(&#91;])\nax1.set_xticks(np.linspace(0, 1, 11))\nax1.set_xticklabels(&#91;f\"{int(x*100)}%\" for x in np.linspace(0, 1, 11)], fontsize=9)\nax1.set_title('Prob. Daily Return &gt; 0 (LONG)', fontsize=10, fontweight='bold')\nax1.axvline(0.5, color='black', linestyle='--', alpha=0.7)\nax1.text(probability_positive_long, 0.05, f\"{probability_positive_long:.1%}\",\n         ha='center', fontsize=10, fontweight='bold', color='black')\n\n# Right: SHORT\ncolor_short = 'green' if probability_positive_short &gt; 0.5 else 'red'\nax2.barh(&#91;0], &#91;probability_positive_short], color=color_short, height=0.4, alpha=0.8)\nax2.barh(&#91;0], &#91;0.5], color='gray', height=0.4, alpha=0.3)\nax2.set_xlim(0, 1)\nax2.set_yticks(&#91;])\nax2.set_xticks(np.linspace(0, 1, 11))\nax2.set_xticklabels(&#91;f\"{int(x*100)}%\" for x in np.linspace(0, 1, 11)], fontsize=9)\nax2.set_title('Prob. Daily Return &gt; 0 (SHORT)', fontsize=10, fontweight='bold')\nax2.axvline(0.5, color='black', linestyle='--', alpha=0.7)\nax2.text(probability_positive_short, 0.05, f\"{probability_positive_short:.1%}\",\n         ha='center', fontsize=10, fontweight='bold', color='black')\n\nplt.tight_layout()\nplt.show()<\/code><\/pre>\n\n\n\n<p id=\"0925\">\u8fd8\u80fd\u76f4\u89c2\u663e\u793a\u6309\u7b26\u53f7\u5212\u5206\u7684\u6bcf\u65e5\u51c0\u6536\u76ca\u5206\u5e03\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># --- 2) Violin Plots Side by Side ---\nfig, (ax1, ax2) = plt.subplots(1, 2, figsize=(20, 6))\n\n# Left: LONG Distribution\nsns.violinplot(\n    data=trade_results_long,\n    x='symbol',\n    y='daily_net_return (%)',\n    hue='symbol',\n    palette='muted',\n    inner='box',\n    ax=ax1\n)\nax1.set_title('Distribution of Daily Returns (LONG)', fontsize=14, fontweight='bold')\nax1.set_xlabel('Asset', fontsize=12)\nax1.set_ylabel('Net Daily Return (%)', fontsize=12)\nax1.tick_params(axis='x', rotation=45, labelsize=10)\nax1.tick_params(axis='y', labelsize=10)\nax1.grid(axis='y', linestyle='--', alpha=0.7)\n\n# Right: SHORT Distribution\nsns.violinplot(\n    data=trade_results_short,\n    x='symbol',\n    y='daily_net_return (%)',\n    hue='symbol',\n    palette='muted',\n    inner='box',\n    ax=ax2\n)\nax2.set_title('Distribution of Daily Returns (SHORT)', fontsize=14, fontweight='bold')\nax2.set_xlabel('Asset', fontsize=12)\nax2.set_ylabel('Net Daily Return (%)', fontsize=12)\nax2.tick_params(axis='x', rotation=45, labelsize=10)\nax2.tick_params(axis='y', labelsize=10)\nax2.grid(axis='y', linestyle='--', alpha=0.7)\n\nplt.tight_layout()\nplt.show()<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image\" id=\"1e10\"><img decoding=\"async\" src=\"https:\/\/cdn-images-1.readmedium.com\/v2\/resize:fit:800\/1*SUR8kva-ZsrAymVxrsTG3w.png\" alt=\"\"\/><\/figure>\n\n\n\n<p id=\"7827\">\u8fd9\u4e9b\u56fe\u63ed\u793a\u4e86\u54ea\u4e9b\u80a1\u7968\u7684\u65e5\u6536\u76ca\u7387\u8303\u56f4\u66f4\u7a84\u3001\u66f4\u4e00\u81f4\uff08\u77ed\u671f\u5185\u53ef\u80fd\u66f4\u7a33\u5b9a\uff09\uff0c\u54ea\u4e9b\u80a1\u7968\u7684\u6ce2\u52a8\u66f4\u5927\u6216\u5c3e\u90e8\u66f4\u957f\u3002\u5982\u679c\u4e00\u4e2a\u7b26\u53f7\u7684\u957f\u7ebf\u5c0f\u63d0\u7434\u56fe\u5177\u6709\u660e\u663e\u7684\u6b63\u504f\u659c\uff0c\u8fd9\u53ef\u80fd\u4e0e\u8f83\u9ad8\u7684\u957f\u7ebf\u8d62\u5229\u6982\u7387\uff08prob_win_long\uff09\u76f8\u4e00\u81f4\uff0c\u4ece\u800c\u5f3a\u5316\u4e86 &#8220;\u5728\u652f\u6491\u4f4d\u9644\u8fd1\u4e70\u5165 &#8220;\u7684\u77ed\u671f\u65b9\u6848\u4e00\u822c\u90fd\u80fd\u594f\u6548\u7684\u89c2\u70b9&#8211;\u8fd9\u5728\u51b3\u5b9a\u6295\u8d44\u7ec4\u5408\u914d\u7f6e\u65f6\u662f\u5f88\u6709\u4ef7\u503c\u7684\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"cfb3\"><strong>\u89c2\u70b9\u603b\u7ed3<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u901a\u8fc7\u7cfb\u7edf\u5730\u5206\u6790\u652f\u6491\u4f4d\u548c\u963b\u529b\u4f4d\uff0c\u5e76\u6a21\u62df\u5047\u8bbe\u7684\u4ea4\u6613\u7ed3\u679c\uff08\u5305\u62ec\u7a0e\u540e\u51c0\u6536\u76ca\u548c\u6b62\u635f\u70b9\uff09\uff0c\u4f60\u53ef\u4ee5\u4e3a\u957f\u671f\u6295\u8d44\u7ec4\u5408\u7684\u8c03\u6574\u548c\u8d44\u91d1\u5206\u914d\u63d0\u4f9b\u989d\u5916\u7684\u53c2\u8003\u3002<\/li>\n\n\n\n<li>\u8fd9\u79cd\u65b9\u6cd5\u5e76\u4e0d\u4f1a\u53d6\u4ee3\u4f60\u7684\u57fa\u672c\u9762\u5206\u6790\u6216\u8499\u7279\u5361\u6d1b\u6a21\u62df\u7b56\u7565\uff0c\u800c\u662f\u4e3a\u4f60\u63d0\u4f9b\u4e86\u4e00\u79cd\u65b0\u7684\u89c6\u89d2\uff0c\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u4e86\u89e3\u6bcf\u53ea\u80a1\u7968\u5f53\u524d\u7684\u4ef7\u683c\u76f8\u5bf9\u4e8e\u8fd1\u671f\u5173\u952e\u8f6c\u6298\u70b9\u7684\u4f4d\u7f6e\u3002<\/li>\n\n\n\n<li>\u5728\u5b9e\u9645\u64cd\u4f5c\u4e2d\uff0c\u8fd9\u4e9b\u4fe1\u606f\u53ef\u80fd\u4f1a\u5f71\u54cd\u4f60\u5982\u4f55\u5206\u914d\u65b0\u8d44\u91d1\u6216\u8c03\u6574\u73b0\u6709\u4ed3\u4f4d\uff0c\u6bd4\u5982\u5728\u63a5\u8fd1\u652f\u6491\u4f4d\u65f6\u52a0\u4ed3\uff0c\u6216\u8005\u5728\u63a5\u8fd1\u963b\u529b\u4f4d\u65f6\u51cf\u4ed3\uff0c\u4ece\u800c\u4f18\u5316\u4f60\u7684\u6295\u8d44\u7ec4\u5408\u8868\u73b0\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u5982\u9700\u83b7\u53d6\u5b8c\u6574\u4ee3\u7801\u53ca\u66f4\u591a\u8be6\u7ec6\u8d44\u6599\uff0c\u6b22\u8fce\u8bbf\u95ee\u6211\u7684GitHub\u4ed3\u5e93\u3002\u60a8\u53ef\u4ee5\u6839\u636e\u81ea\u8eab\u6295\u8d44\u7b56\u7565\u7684\u9700\u6c42\uff0c\u81ea\u7531\u8c03\u6574\u3001\u6d4b\u8bd5\u5e76\u6574\u5408\u8fd9\u4e9b\u77ed\u671f\u4ea4\u6613\u4fe1\u53f7\uff0c\u4ee5\u4f18\u5316\u60a8\u7684\u6295\u8d44\u51b3\u7b56\u6d41\u7a0b\u3002<\/p>\n\n\n\n<p>\u5730\u5740\uff1a<a href=\"https:\/\/github.com\/alexyu2013\/Stocks2025\/blob\/master\/evalPortfolio\/Supercharge_AssetRebalancing.ipynb\">https:\/\/github.com\/alexyu2013\/Stocks2025\/blob\/master\/evalPortfolio\/Supercharge_AssetRebalancing.ipynb<\/a><\/p>\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\/14\/%e5%ad%a6%e4%bc%9a%e8%bf%99%e4%b8%aa%e7%9f%ad%e7%ba%bf%e5%88%86%e6%9e%90%e5%b0%8f%e6%8a%80%e5%b7%a7%ef%bc%8c%e8%bd%bb%e6%9d%be%e6%8f%90%e5%8d%87%e4%bd%a0%e7%9a%84%e6%8a%95%e8%b5%84%e7%bb%84%e5%90%88\/\">Continue reading<span class=\"screen-reader-text\">\u5b66\u4f1a\u8fd9\u4e2a\u77ed\u7ebf\u5206\u6790\u5c0f\u6280\u5de7\uff0c\u8f7b\u677e\u63d0\u5347\u4f60\u7684\u6295\u8d44\u7ec4\u5408\u6536\u76ca\uff01\uff08\u9644\u6e90\u7801\uff09<\/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,6],"class_list":{"0":"post-1898","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"hentry","6":"category-aiinvest","7":"tag-ai","9":"tag-6","10":"entry"},"_links":{"self":[{"href":"https:\/\/laoyulaoyu.com\/index.php\/wp-json\/wp\/v2\/posts\/1898","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=1898"}],"version-history":[{"count":1,"href":"https:\/\/laoyulaoyu.com\/index.php\/wp-json\/wp\/v2\/posts\/1898\/revisions"}],"predecessor-version":[{"id":1899,"href":"https:\/\/laoyulaoyu.com\/index.php\/wp-json\/wp\/v2\/posts\/1898\/revisions\/1899"}],"wp:attachment":[{"href":"https:\/\/laoyulaoyu.com\/index.php\/wp-json\/wp\/v2\/media?parent=1898"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/laoyulaoyu.com\/index.php\/wp-json\/wp\/v2\/categories?post=1898"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/laoyulaoyu.com\/index.php\/wp-json\/wp\/v2\/tags?post=1898"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}