{"id":1500,"date":"2025-02-02T10:20:23","date_gmt":"2025-02-02T02:20:23","guid":{"rendered":"https:\/\/crab.cool\/?p=1500"},"modified":"2025-02-02T10:20:23","modified_gmt":"2025-02-02T02:20:23","slug":"ds%e6%8f%90%e4%be%9b%e7%9a%84%e9%87%8f%e5%8c%96%e6%8a%95%e8%b5%84%e6%a1%86%e6%9e%b6","status":"publish","type":"post","link":"https:\/\/crab.cool\/index.php\/2025\/02\/02\/ds%e6%8f%90%e4%be%9b%e7%9a%84%e9%87%8f%e5%8c%96%e6%8a%95%e8%b5%84%e6%a1%86%e6%9e%b6\/","title":{"rendered":"DS\u63d0\u4f9b\u7684\u91cf\u5316\u6295\u8d44\u6846\u67b6"},"content":{"rendered":"<p>\u53ef\u4ee5\u57fa\u4e8e\u91cf\u5316\u6295\u8d44\u7684\u65b9\u6cd5\u5efa\u7acb\u4e00\u4e2a\u9488\u5bf9\u67d0\u53ea\u80a1\u7968\u7684\u6295\u8d44\u7b56\u7565\u6a21\u578b\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a<strong>\u5b8c\u6574\u6846\u67b6<\/strong>\uff08\u4ee5Python\u4e3a\u4f8b\uff09\uff0c\u5305\u542b\u6570\u636e\u83b7\u53d6\u3001\u7b56\u7565\u8bbe\u8ba1\u3001\u56de\u6d4b\u548c\u98ce\u9669\u7ba1\u7406\u6a21\u5757\uff1a<\/p>\n<hr \/>\n<h3><strong>1. \u6570\u636e\u51c6\u5907<\/strong><\/h3>\n<pre><code class=\"language-python\">import pandas as pd\nimport numpy as np\nimport yfinance as yf  # \u96c5\u864e\u8d22\u7ecf\u6570\u636e\u63a5\u53e3\n\n# \u83b7\u53d6\u80a1\u7968\u5386\u53f2\u6570\u636e\uff08\u793a\u4f8b\uff1a\u5b81\u5fb7\u65f6\u4ee3\uff0cA\u80a1\u4ee3\u7801300750.SZ\uff09\nstock_data = yf.download(&quot;300750.SZ&quot;, start=&quot;2018-01-01&quot;, end=&quot;2023-12-31&quot;)\n# \u6dfb\u52a0\u5b8f\u89c2\u7ecf\u6d4e\u6570\u636e\uff08\u4f8b\u5982\u9502\u7535\u6c60\u539f\u6750\u6599\u78b3\u9178\u9502\u4ef7\u683c\uff09\nlithium_price = yf.download(&quot;LIT&quot;, start=&quot;2018-01-01&quot;, end=&quot;2023-12-31&quot;)  # \u5168\u7403\u9502ETF<\/code><\/pre>\n<hr \/>\n<h3><strong>2. \u7b56\u7565\u903b\u8f91\u8bbe\u8ba1<\/strong><\/h3>\n<h4><strong>(1) \u591a\u56e0\u5b50\u9009\u80a1\u6a21\u578b<\/strong><\/h4>\n<p>\u7ed3\u5408\u57fa\u672c\u9762\u3001\u6280\u672f\u9762\u548c\u884c\u4e1a\u5468\u671f\u56e0\u5b50\uff1a<\/p>\n<pre><code class=\"language-python\"># \u56e0\u5b501\uff1a\u52a8\u6001\u5e02\u76c8\u7387\uff08TTM\uff09\u5206\u4f4d\u6570\uff08\u4f4e\u4f30\u503c\uff09\ndef calculate_pe_rank(data):\n    data[&#039;PE_rank&#039;] = data[&#039;Close&#039;].rolling(60).apply(lambda x: (x[-1] - x.min()) \/ (x.max() - x.min()))\n\n# \u56e0\u5b502\uff1a\u9502\u7535\u6c60\u884c\u4e1a\u666f\u6c14\u5ea6\uff08\u901a\u8fc7\u78b3\u9178\u9502\u4ef7\u683c\u8d8b\u52bf\u4ee3\u7406\uff09\ndef lithium_trend(stock_data, lithium_price):\n    stock_data[&#039;Lithium_3M_Return&#039;] = lithium_price[&#039;Close&#039;].pct_change(60)  # 3\u4e2a\u6708\u9502\u4ef7\u6da8\u5e45\n\n# \u56e0\u5b503\uff1a\u6280\u672f\u9762\u52a8\u91cf\uff0860\u65e5\u6da8\u5e45\uff09\nstock_data[&#039;Momentum_60D&#039;] = stock_data[&#039;Close&#039;].pct_change(60)\n\n# \u56e0\u5b504\uff1a\u653f\u7b56\u4e8b\u4ef6\uff08\u4eba\u5de5\u6807\u6ce8\u5173\u952e\u653f\u7b56\u65e5\u671f\uff0c\u5982\u8865\u8d34\u9000\u5761\u3001\u78b3\u4e2d\u548c\u76ee\u6807\u53d1\u5e03\uff09\npolicy_dates = {&#039;2020-11-02&#039;: &#039;\u4e2d\u56fd\u65b0\u80fd\u6e90\u89c4\u5212&#039;, &#039;2021-07-01&#039;: &#039;\u6b27\u6d32\u78b3\u5173\u7a0e\u63d0\u6848&#039;}<\/code><\/pre>\n<h4><strong>(2) \u4ea4\u6613\u4fe1\u53f7\u751f\u6210<\/strong><\/h4>\n<pre><code class=\"language-python\"># \u7efc\u5408\u4fe1\u53f7\uff08\u793a\u4f8b\uff1a\u7b80\u5355\u52a0\u6743\uff09\nstock_data[&#039;Signal&#039;] = (\n    0.4 * (1 - stock_data[&#039;PE_rank&#039;]) +  # \u4f4e\u4f30\u503c\u6743\u91cd40%\n    0.3 * stock_data[&#039;Lithium_3M_Return&#039;] +  # \u884c\u4e1a\u666f\u6c14\u5ea630%\n    0.3 * stock_data[&#039;Momentum_60D&#039;]  # \u52a8\u91cf30%\n)\n# \u751f\u6210\u4e70\u5356\u4fe1\u53f7\uff08\u9608\u503c\u89e6\u53d1\uff09\nstock_data[&#039;Position&#039;] = np.where(stock_data[&#039;Signal&#039;] &gt; 0.6, 1,  # \u4e70\u5165\n                         np.where(stock_data[&#039;Signal&#039;] &lt; 0.3, -1, 0))  # \u5356\u51fa<\/code><\/pre>\n<hr \/>\n<h3><strong>3. \u56de\u6d4b\u4e0e\u7ee9\u6548\u8bc4\u4f30<\/strong><\/h3>\n<pre><code class=\"language-python\"># \u8ba1\u7b97\u7b56\u7565\u6536\u76ca\nstock_data[&#039;Strategy_Return&#039;] = stock_data[&#039;Position&#039;].shift(1) * stock_data[&#039;Close&#039;].pct_change()\n\n# \u5bf9\u6bd4\u57fa\u51c6\uff08\u4e70\u5165\u6301\u6709\uff09\nstock_data[&#039;Benchmark_Return&#039;] = stock_data[&#039;Close&#039;].pct_change()\n\n# \u8ba1\u7b97\u7d2f\u8ba1\u6536\u76ca\nstock_data[[&#039;Cum_Strategy&#039;, &#039;Cum_Benchmark&#039;]] = (\n    (1 + stock_data[[&#039;Strategy_Return&#039;, &#039;Benchmark_Return&#039;]]).cumprod()\n)\n\n# \u7ee9\u6548\u6307\u6807\ndef calculate_performance(returns):\n    sharpe_ratio = returns.mean() \/ returns.std() * np.sqrt(252)  # \u5e74\u5316\u590f\u666e\u6bd4\u7387\n    max_drawdown = (returns.cumsum().expanding().max() - returns.cumsum()).max()  # \u6700\u5927\u56de\u64a4\n    return sharpe_ratio, max_drawdown\n\nsharpe, drawdown = calculate_performance(stock_data[&#039;Strategy_Return&#039;].dropna())<\/code><\/pre>\n<hr \/>\n<h3><strong>4. \u98ce\u9669\u7ba1\u7406\u6a21\u5757<\/strong><\/h3>\n<h4><strong>(1) \u52a8\u6001\u4ed3\u4f4d\u63a7\u5236<\/strong><\/h4>\n<pre><code class=\"language-python\"># \u6ce2\u52a8\u7387\u8c03\u6574\u4ed3\u4f4d\uff08\u4f8b\u5982\uff1aATR\u6307\u6807\uff09\nstock_data[&#039;ATR_14D&#039;] = talib.ATR(stock_data[&#039;High&#039;], stock_data[&#039;Low&#039;], stock_data[&#039;Close&#039;], timeperiod=14)\nstock_data[&#039;Position_Size&#039;] = 0.02 \/ (stock_data[&#039;ATR_14D&#039;] \/ stock_data[&#039;Close&#039;])  # \u56fa\u5b9a\u98ce\u9669\u6bd4\u4f8b<\/code><\/pre>\n<h4><strong>(2) \u6b62\u635f\u673a\u5236<\/strong><\/h4>\n<pre><code class=\"language-python\"># \u79fb\u52a8\u6b62\u635f\uff08\u8ddf\u8e2a\u8fc7\u53bbN\u65e5\u6700\u4f4e\u4ef7\uff09\nstop_loss_window = 20\nstock_data[&#039;Trailing_Stop&#039;] = stock_data[&#039;Close&#039;].rolling(stop_loss_window).min() * 0.95  # 5%\u6b62\u635f\u7ebf<\/code><\/pre>\n<hr \/>\n<h3><strong>5. \u6a21\u578b\u4f18\u5316\u4e0e\u9a8c\u8bc1<\/strong><\/h3>\n<ul>\n<li><strong>\u53c2\u6570\u654f\u611f\u6027\u6d4b\u8bd5<\/strong>\uff1a\u901a\u8fc7\u7f51\u683c\u641c\u7d22\u4f18\u5316\u56e0\u5b50\u6743\u91cd\u548c\u4fe1\u53f7\u9608\u503c\u3002<\/li>\n<li><strong>\u6837\u672c\u5916\u6d4b\u8bd5<\/strong>\uff1a\u5c06\u6570\u636e\u5206\u4e3a\u8bad\u7ec3\u96c6\uff082018-2021\uff09\u548c\u6d4b\u8bd5\u96c6\uff082022-2023\uff09\uff0c\u9632\u6b62\u8fc7\u62df\u5408\u3002<\/li>\n<li><strong>Monte Carlo\u6a21\u62df<\/strong>\uff1a\u968f\u673a\u6270\u52a8\u5386\u53f2\u6536\u76ca\u7387\uff0c\u68c0\u9a8c\u7b56\u7565\u7a33\u5065\u6027\u3002<\/li>\n<\/ul>\n<hr \/>\n<h3><strong>6. \u5b9e\u76d8\u90e8\u7f72\u5efa\u8bae<\/strong><\/h3>\n<ol>\n<li><strong>\u81ea\u52a8\u5316\u4ea4\u6613\u63a5\u53e3<\/strong>\uff1a\u901a\u8fc7\u5238\u5546API\uff08\u5982\u76c8\u900f\u8bc1\u5238\u3001Alpaca\uff09\u8fde\u63a5\u5b9e\u65f6\u6570\u636e\u3002<\/li>\n<li><strong>\u76d1\u63a7\u4e0e\u518d\u5e73\u8861<\/strong>\uff1a\u6bcf\u5468\u6216\u6bcf\u6708\u68c0\u67e5\u56e0\u5b50\u72b6\u6001\uff0c\u52a8\u6001\u8c03\u6574\u6301\u4ed3\u3002<\/li>\n<li><strong>\u9ed1\u5929\u9e45\u5e94\u5bf9<\/strong>\uff1a\u8bbe\u7f6e\u7194\u65ad\u6761\u4ef6\uff08\u5982\u5355\u65e5\u8dcc\u5e45\u8d8510%\u65f6\u5f3a\u5236\u5e73\u4ed3\uff09\u3002<\/li>\n<\/ol>\n<hr \/>\n<h3><strong>\u793a\u4f8b\u7ed3\u679c\uff08\u6a21\u62df\u6570\u636e\uff09<\/strong><\/h3>\n<table>\n<thead>\n<tr>\n<th>\u6307\u6807<\/th>\n<th>\u7b56\u7565\u6536\u76ca<\/th>\n<th>\u57fa\u51c6\u6536\u76ca<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\u5e74\u5316\u6536\u76ca\u7387<\/td>\n<td>18.7%<\/td>\n<td>9.2%<\/td>\n<\/tr>\n<tr>\n<td>\u590f\u666e\u6bd4\u7387<\/td>\n<td>1.4<\/td>\n<td>0.6<\/td>\n<\/tr>\n<tr>\n<td>\u6700\u5927\u56de\u64a4<\/td>\n<td>-22.3%<\/td>\n<td>-34.8%<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<hr \/>\n<h3><strong>\u6ce8\u610f\u4e8b\u9879<\/strong><\/h3>\n<ol>\n<li><strong>\u6570\u636e\u8d28\u91cf<\/strong>\uff1a\u9700\u5904\u7406\u7f3a\u5931\u503c\u3001\u5f02\u5e38\u503c\uff08\u5982\u5b81\u5fb7\u65f6\u4ee3\u57282018\u5e74\u4e0a\u5e02\uff0c\u65e9\u671f\u6570\u636e\u53ef\u80fd\u4e0d\u7a33\u5b9a\uff09\u3002<\/li>\n<li><strong>\u8fc7\u62df\u5408\u98ce\u9669<\/strong>\uff1a\u907f\u514d\u5728\u5355\u4e00\u80a1\u7968\u4e0a\u8fc7\u5ea6\u4f18\u5316\u53c2\u6570\u3002<\/li>\n<li><strong>\u5e02\u573a\u53d8\u5316<\/strong>\uff1a\u9502\u7535\u6c60\u6280\u672f\u8fed\u4ee3\uff08\u5982\u56fa\u6001\u7535\u6c60\uff09\u53ef\u80fd\u98a0\u8986\u884c\u4e1a\u903b\u8f91\uff0c\u9700\u5b9a\u671f\u66f4\u65b0\u6a21\u578b\u3002<\/li>\n<\/ol>\n<p>\u5982\u679c\u9700\u8981\u9488\u5bf9\u5177\u4f53\u80a1\u7968\uff08\u5982\u7279\u65af\u62c9\u3001\u6bd4\u4e9a\u8fea\uff09\u5b9a\u5236\u7b56\u7565\uff0c\u53ef\u8fdb\u4e00\u6b65\u7ed3\u5408\u5176\u8d22\u62a5\u53d1\u5e03\u65e5\u671f\u3001\u4ea7\u54c1\u5468\u671f\u7b49\u72ec\u7279\u56e0\u5b50\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u53ef\u4ee5\u57fa\u4e8e\u91cf\u5316\u6295\u8d44\u7684\u65b9\u6cd5\u5efa\u7acb\u4e00\u4e2a\u9488\u5bf9\u67d0\u53ea\u80a1\u7968\u7684\u6295\u8d44\u7b56\u7565\u6a21\u578b\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5b8c\u6574\u6846\u67b6\uff08\u4ee5Python\u4e3a\u4f8b\uff09\uff0c\u5305\u542b\u6570\u636e\u83b7 &hellip; <a href=\"https:\/\/crab.cool\/index.php\/2025\/02\/02\/ds%e6%8f%90%e4%be%9b%e7%9a%84%e9%87%8f%e5%8c%96%e6%8a%95%e8%b5%84%e6%a1%86%e6%9e%b6\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">DS\u63d0\u4f9b\u7684\u91cf\u5316\u6295\u8d44\u6846\u67b6<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[75,48],"class_list":["post-1500","post","type-post","status-publish","format-standard","hentry","category-world","tag-deepseek","tag-48"],"_links":{"self":[{"href":"https:\/\/crab.cool\/index.php\/wp-json\/wp\/v2\/posts\/1500","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/crab.cool\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/crab.cool\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/crab.cool\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/crab.cool\/index.php\/wp-json\/wp\/v2\/comments?post=1500"}],"version-history":[{"count":1,"href":"https:\/\/crab.cool\/index.php\/wp-json\/wp\/v2\/posts\/1500\/revisions"}],"predecessor-version":[{"id":1501,"href":"https:\/\/crab.cool\/index.php\/wp-json\/wp\/v2\/posts\/1500\/revisions\/1501"}],"wp:attachment":[{"href":"https:\/\/crab.cool\/index.php\/wp-json\/wp\/v2\/media?parent=1500"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/crab.cool\/index.php\/wp-json\/wp\/v2\/categories?post=1500"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/crab.cool\/index.php\/wp-json\/wp\/v2\/tags?post=1500"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}