{"id":43,"date":"2025-03-21T19:08:31","date_gmt":"2025-03-22T02:08:31","guid":{"rendered":"http:\/\/www.ashwang.net\/?p=43"},"modified":"2025-03-22T10:41:23","modified_gmt":"2025-03-22T17:41:23","slug":"tutorial-%e6%95%99%e7%a8%8b%ef%bc%9a%e7%94%a8-python-%e6%9e%84%e5%bb%ba%e6%af%94%e7%89%b9%e5%b8%81%e5%85%ad%e5%9b%a0%e5%ad%90%e9%87%8f%e5%8c%96%e7%ad%96%e7%95%a5%e6%a8%a1%e5%9e%8b","status":"publish","type":"post","link":"http:\/\/www.ashwang.net\/index.php\/2025\/03\/21\/tutorial-%e6%95%99%e7%a8%8b%ef%bc%9a%e7%94%a8-python-%e6%9e%84%e5%bb%ba%e6%af%94%e7%89%b9%e5%b8%81%e5%85%ad%e5%9b%a0%e5%ad%90%e9%87%8f%e5%8c%96%e7%ad%96%e7%95%a5%e6%a8%a1%e5%9e%8b\/","title":{"rendered":"\u7528 Python \u6784\u5efa\u6bd4\u7279\u5e01\u516d\u56e0\u5b50\u91cf\u5316\u7b56\u7565\u6a21\u578b"},"content":{"rendered":"\n<p>This tutorial walks through the development of a true 6-factor strategy model for Bitcoin using Python. We use technical indicators like EMA, MACD, RSI, KDJ, CCI, and OBV to build a signal classification model that outputs Buy, Sell, or Hold. \u672c\u6559\u7a0b\u5c06\u5e26\u4f60\u4ece\u5934\u5f00\u59cb\u6784\u5efa\u4e00\u4e2a\u771f\u6b63\u7684\u516d\u56e0\u5b50\u6bd4\u7279\u5e01\u4ea4\u6613\u6a21\u578b\uff0c\u5229\u7528\u591a\u4e2a\u7ecf\u5178\u6280\u672f\u6307\u6807\u751f\u6210\u4e70\u5356\u4fe1\u53f7\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1 \u6b65\u9aa4\u4e00\uff1a\u52a0\u8f7d\u6570\u636e<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>btc_data = pd.read_csv(\"btc_data.csv\", parse_dates=&#91;'Open time'])\nbtc_data.set_index('Open time', inplace=True)\nbtc_data.rename(columns=lambda x: x.lower(), inplace=True)\n<\/code><\/pre>\n\n\n\n<p>\u6211\u4eec\u52a0\u8f7d 15 \u5206\u949f\u7ea7\u522b\u7684 BTC \u6570\u636e\uff0c\u5e76\u5c06\u65f6\u95f4\u8bbe\u4e3a\u7d22\u5f15\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2 \u6b65\u9aa4\u4e8c\uff1a\u6dfb\u52a0\u6280\u672f\u6307\u6807\uff08Technical Indicators\uff09<\/h3>\n\n\n\n<p>\u6211\u4eec\u4f7f\u7528 <code>pandas_ta<\/code> \u6dfb\u52a0\u5982\u4e0b\u6307\u6807\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>EMA 6, EMA 18, EMA 60<\/li>\n\n\n\n<li>MACD, MACD Signal<\/li>\n\n\n\n<li>RSI\uff0814\u65e5\uff09<\/li>\n\n\n\n<li>CCI\uff0814\u65e5\uff09<\/li>\n\n\n\n<li>KDJ\uff08\u4e09\u7ebf\u6307\u6807\uff09<\/li>\n\n\n\n<li>OBV\uff08On-Balance Volume\uff09<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>import pandas_ta as ta\n\ndf&#91;'EMA_6'] = ta.ema(df&#91;'close'], length=6)\ndf&#91;'EMA_18'] = ta.ema(df&#91;'close'], length=18)\n...\ndf&#91;'OBV'] = ta.obv(df&#91;'close'], df&#91;'volume'])\ndf.dropna(inplace=True)\n<\/code><\/pre>\n\n\n\n<p>\u6bcf\u4e00\u4e2a\u56e0\u5b50\u4ee3\u8868\u4e86\u5e02\u573a\u60c5\u7eea\u3001\u52a8\u91cf\u6216\u8d44\u91d1\u6d41\u7684\u67d0\u4e2a\u7ef4\u5ea6\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Step 3 \u6b65\u9aa4\u4e09\uff1a\u4ef7\u683c\u9884\u6d4b\u6a21\u578b\uff08Price Regression Model\uff09<\/h3>\n\n\n\n<p>\u6211\u4eec\u7528\u8fd9\u4e9b\u56e0\u5b50\u9884\u6d4b\u4e0b\u4e00\u4e2a\u5468\u671f\u7684\u4ef7\u683c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>X = df&#91;&#91;\"EMA_6\", \"EMA_18\", ..., \"OBV\"]]\ny = df&#91;'close'].shift(-1)\n<\/code><\/pre>\n\n\n\n<p>\u8bad\u7ec3\u4e00\u4e2a\u7ebf\u6027\u6a21\u578b\u8fdb\u884c\u4ef7\u683c\u62df\u5408\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from sklearn.linear_model import LinearRegression\nmodel = LinearRegression()\nmodel.fit(X_train, y_train)\n<\/code><\/pre>\n\n\n\n<p>\u6a21\u578b\u8f93\u51fa\u4e5f\u53ef\u4ee5\u4f5c\u4e3a\u540e\u7eed\u51b3\u7b56\u6a21\u578b\u7684\u4e00\u90e8\u5206\u7279\u5f81\uff1a<code>Predicted_Close<\/code><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Step 4 \u6b65\u9aa4\u56db\uff1a\u51b3\u7b56\u4fe1\u53f7\u5206\u7c7b\u6a21\u578b\uff08Buy\/Sell\/Hold\uff09<\/h3>\n\n\n\n<p>\u76ee\u6807\u53d8\u91cf\u7531\u4ee5\u4e0b\u516d\u4e2a\u6761\u4ef6\u751f\u6210\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>y = np.where(\n    (EMA_6 &gt; EMA_18) &amp; (MACD &gt; MACD_signal) &amp; (RSI &gt; 30) &amp;\n    (K &gt; D) &amp; (CCI &gt; 100) &amp; (OBV &gt; OBV.shift(1)), 1,  # \u4e70\u5165\n    np.where(\n        (EMA_6 &lt; EMA_18) &amp; (MACD &lt; MACD_signal) &amp; (RSI &lt; 70) &amp;\n        (K &lt; D) &amp; (CCI &lt; -100) &amp; (OBV &lt; OBV.shift(1)), -1, 0  # \u5356\u51fa \/ \u6301\u6709\n    )\n)\n<\/code><\/pre>\n\n\n\n<p>\u7528 <code>RandomForestClassifier<\/code> \u8bad\u7ec3\u9884\u6d4b\u8fd9\u4e9b\u4e09\u5206\u7c7b\u4fe1\u53f7\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from sklearn.ensemble import RandomForestClassifier\nmodel = RandomForestClassifier(max_depth=5)\nmodel.fit(X_train, y_train)\n<\/code><\/pre>\n\n\n\n<p>\u6a21\u578b\u9884\u6d4b\u51fa Buy\/Sell\/Hold \u4fe1\u53f7\uff0c\u53ef\u7528\u4e8e\u7b56\u7565\u6267\u884c\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Step 5 \u6b65\u9aa4\u4e94\uff1a\u56fe\u8868\u5c55\u793a\uff08Interactive Plot\uff09<\/h3>\n\n\n\n<p>\u6211\u4eec\u7528 Plotly \u753b\u51fa\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5b9e\u9645 vs \u9884\u6d4b\u4ef7\u683c<\/li>\n\n\n\n<li>\u4e70\u5165\uff08\u7eff\u8272\u4e09\u89d2\uff09<\/li>\n\n\n\n<li>\u5356\u51fa\uff08\u7ea2\u8272\u5012\u4e09\u89d2\uff09<\/li>\n\n\n\n<li>\u6301\u6709\uff08\u9ec4\u8272\u5706\u70b9\uff09<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>fig.add_trace(go.Scatter(... name='Buy'))\nfig.add_trace(go.Scatter(... name='Sell'))\nfig.add_trace(go.Scatter(... name='Hold'))\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Summary \u603b\u7ed3<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\ud83d\udd0d \u56e0\u5b50\u57fa\u4e8e\u4f20\u7edf\u6280\u672f\u6307\u6807\uff1aMACD\u3001KDJ\u3001OBV \u7b49<\/li>\n\n\n\n<li>\ud83d\udd04 \u901a\u8fc7\u7ec4\u5408\u6761\u4ef6\u6784\u5efa\u6807\u7b7e\uff08Buy\/Sell\/Hold\uff09<\/li>\n\n\n\n<li>\ud83e\udd16 \u4f7f\u7528\u673a\u5668\u5b66\u4e60\u8fdb\u884c\u5206\u7c7b\u9884\u6d4b<\/li>\n\n\n\n<li>\ud83d\udcc8 \u56fe\u5f62\u5c55\u793a\u7b56\u7565\u6548\u679c\uff0c\u9002\u5408\u91cf\u5316\u4ea4\u6613\u5b9e\u8df5<\/li>\n<\/ul>\n\n\n\n<p>This model demonstrates how to combine multiple technical indicators into a rule-based signal system, and then enhance it with machine learning for decision-making.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udcce Download &amp; Connect<\/h3>\n\n\n\n<p>\ud83d\udce5 \u5b8c\u6574\u4ee3\u7801\u4e0e\u6570\u636e\u5c06\u4e0a\u4f20\u81f3 GitHub\uff0c\u6b22\u8fce\u53c2\u8003\u3002 \ud83d\udcec \u5982\u9700\u5408\u4f5c\u6216\u4ea4\u6d41\uff0c\u8bf7\u901a\u8fc7 <a href=\"https:\/\/www.linkedin.com\/in\/bingkun-wang-30a63231b\/\">LinkedIn<\/a> \u8054\u7cfb\u6211\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial walks through the development of a true 6-factor strategy model for Bitcoin using Python. We use technical indicators like EMA, MACD, RSI, KDJ, CCI, and OBV to build a signal classification model that outputs Buy, Sell, or Hold. \u672c\u6559\u7a0b\u5c06\u5e26\u4f60\u4ece\u5934\u5f00\u59cb\u6784\u5efa\u4e00\u4e2a\u771f\u6b63\u7684\u516d\u56e0\u5b50\u6bd4\u7279\u5e01\u4ea4\u6613\u6a21\u578b\uff0c\u5229\u7528\u591a\u4e2a\u7ecf\u5178\u6280\u672f\u6307\u6807\u751f\u6210\u4e70\u5356\u4fe1\u53f7\u3002 Step 1 \u6b65\u9aa4\u4e00\uff1a\u52a0\u8f7d\u6570\u636e \u6211\u4eec\u52a0\u8f7d 15 \u5206\u949f\u7ea7\u522b\u7684 BTC \u6570\u636e\uff0c\u5e76\u5c06\u65f6\u95f4\u8bbe\u4e3a\u7d22\u5f15\u3002 Step 2 \u6b65\u9aa4\u4e8c\uff1a\u6dfb\u52a0\u6280\u672f\u6307\u6807\uff08Technical Indicators\uff09 \u6211\u4eec\u4f7f\u7528 pandas_ta [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-43","post","type-post","status-publish","format-standard","hentry","category-tutorial"],"_links":{"self":[{"href":"http:\/\/www.ashwang.net\/index.php\/wp-json\/wp\/v2\/posts\/43","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.ashwang.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.ashwang.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.ashwang.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.ashwang.net\/index.php\/wp-json\/wp\/v2\/comments?post=43"}],"version-history":[{"count":3,"href":"http:\/\/www.ashwang.net\/index.php\/wp-json\/wp\/v2\/posts\/43\/revisions"}],"predecessor-version":[{"id":58,"href":"http:\/\/www.ashwang.net\/index.php\/wp-json\/wp\/v2\/posts\/43\/revisions\/58"}],"wp:attachment":[{"href":"http:\/\/www.ashwang.net\/index.php\/wp-json\/wp\/v2\/media?parent=43"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.ashwang.net\/index.php\/wp-json\/wp\/v2\/categories?post=43"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.ashwang.net\/index.php\/wp-json\/wp\/v2\/tags?post=43"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}