{"id":1072917,"date":"2025-01-08T11:20:36","date_gmt":"2025-01-08T03:20:36","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1072917.html"},"modified":"2025-01-08T11:20:38","modified_gmt":"2025-01-08T03:20:38","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e5%81%9a%e7%a8%8b%e5%ba%8f%e5%8c%96%e4%ba%a4%e6%98%93-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1072917.html","title":{"rendered":"\u5982\u4f55\u7528python\u505a\u7a0b\u5e8f\u5316\u4ea4\u6613"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25102951\/9262caca-2cb3-44d6-82b6-3c2dadcbfb6d.webp\" alt=\"\u5982\u4f55\u7528python\u505a\u7a0b\u5e8f\u5316\u4ea4\u6613\" \/><\/p>\n<p><p> \u5728\u7a0b\u5e8f\u5316\u4ea4\u6613\u4e2d\uff0cPython\u662f\u4e00\u79cd\u975e\u5e38\u53d7\u6b22\u8fce\u7684\u7f16\u7a0b\u8bed\u8a00\u3002<strong>Python\u7b80\u6d01\u6613\u5b66\u3001\u62e5\u6709\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u80fd\u529b\u3001\u4e30\u5bcc\u7684\u91d1\u878d\u5206\u6790\u5e93\u3001\u793e\u533a\u652f\u6301\u5f3a\u5927<\/strong>\u3002\u9996\u5148\uff0c\u4f60\u9700\u8981\u9009\u62e9\u4e00\u4e2a\u5408\u9002\u7684\u4ea4\u6613\u5e73\u53f0\u548cAPI\uff0c\u5982Alpaca\u3001Interactive Brokers\u7b49\u6765\u83b7\u53d6\u5e02\u573a\u6570\u636e\u5e76\u6267\u884c\u4ea4\u6613\u3002\u7136\u540e\uff0c\u4f7f\u7528Python\u7684pandas\u3001numpy\u7b49\u5e93\u6765\u5904\u7406\u6570\u636e\u548c\u8fdb\u884c\u6280\u672f\u5206\u6790\u3002\u6700\u540e\uff0c\u7f16\u5199\u4ea4\u6613\u7b56\u7565\u548c\u98ce\u9669\u7ba1\u7406\u89c4\u5219\uff0c\u5e76\u8fdb\u884c\u56de\u6d4b\u548c\u4f18\u5316\u3002<\/p>\n<\/p>\n<p><p><strong>Python\u7b80\u6d01\u6613\u5b66<\/strong>\uff1aPython\u5177\u6709\u7b80\u6d01\u7684\u8bed\u6cd5\u548c\u4e30\u5bcc\u7684\u5e93\uff0c\u4f7f\u5f97\u5373\u4f7f\u662f\u7f16\u7a0b\u65b0\u624b\u4e5f\u80fd\u8fc5\u901f\u4e0a\u624b\u3002\u5728\u7a0b\u5e8f\u5316\u4ea4\u6613\u4e2d\uff0cPython\u7684\u7b80\u6d01\u6027\u5141\u8bb8\u5f00\u53d1\u8005\u66f4\u4e13\u6ce8\u4e8e\u7b56\u7565\u5f00\u53d1\u548c\u6570\u636e\u5206\u6790\uff0c\u800c\u4e0d\u662f\u88ab\u590d\u6742\u7684\u7f16\u7a0b\u7ec6\u8282\u6240\u56f0\u6270\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u9009\u62e9\u4ea4\u6613\u5e73\u53f0\u548cAPI<\/h3>\n<\/p>\n<p><p>\u9009\u62e9\u4e00\u4e2a\u5408\u9002\u7684\u4ea4\u6613\u5e73\u53f0\u548cAPI\u662f\u5f00\u59cb\u7a0b\u5e8f\u5316\u4ea4\u6613\u7684\u7b2c\u4e00\u6b65\u3002\u4e0d\u540c\u7684\u5e73\u53f0\u548cAPI\u63d0\u4f9b\u4e0d\u540c\u7684\u529f\u80fd\u548c\u670d\u52a1\uff0c\u56e0\u6b64\u9009\u62e9\u65f6\u9700\u8981\u6839\u636e\u81ea\u8eab\u9700\u6c42\u8fdb\u884c\u8bc4\u4f30\u3002<\/p>\n<\/p>\n<p><h4>1\u3001Alpaca<\/h4>\n<\/p>\n<p><p>Alpaca\u662f\u4e00\u4e2a\u6d41\u884c\u7684\u514d\u4f63\u91d1\u80a1\u7968\u4ea4\u6613\u5e73\u53f0\uff0c\u63d0\u4f9b\u4e86\u4e00\u4e2a\u5f3a\u5927\u7684API\uff0c\u9002\u5408\u7a0b\u5e8f\u5316\u4ea4\u6613\u3002\u5176\u4e3b\u8981\u7279\u70b9\u5305\u62ec\u5b9e\u65f6\u5e02\u573a\u6570\u636e\u3001\u5386\u53f2\u6570\u636e\u8bbf\u95ee\u3001\u8ba2\u5355\u7ba1\u7406\u7b49\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>API\u4f7f\u7528\u65b9\u4fbf<\/strong>\uff1aAlpaca\u63d0\u4f9b\u4e86\u6613\u4e8e\u4f7f\u7528\u7684Python SDK\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u5feb\u901f\u96c6\u6210\u5e76\u5f00\u59cb\u7f16\u5199\u4ea4\u6613\u7b56\u7565\u3002<\/li>\n<li><strong>\u514d\u4f63\u91d1\u4ea4\u6613<\/strong>\uff1aAlpaca\u4e0d\u6536\u53d6\u4ea4\u6613\u4f63\u91d1\uff0c\u8fd9\u5bf9\u4e8e\u9891\u7e41\u4ea4\u6613\u7684\u7a0b\u5e8f\u5316\u4ea4\u6613\u6765\u8bf4\u975e\u5e38\u6709\u5229\u3002<\/li>\n<\/ul>\n<p><h4>2\u3001Interactive Brokers<\/h4>\n<\/p>\n<p><p>Interactive Brokers\uff08IB\uff09\u662f\u4e00\u4e2a\u8001\u724c\u7684\u4ea4\u6613\u5e73\u53f0\uff0c\u63d0\u4f9b\u4e86\u5e7f\u6cdb\u7684\u4ea4\u6613\u4ea7\u54c1\u548c\u529f\u80fd\u3002\u5176API\u529f\u80fd\u5f3a\u5927\uff0c\u9002\u5408\u9ad8\u9891\u4ea4\u6613\u548c\u590d\u6742\u7b56\u7565\u7684\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u591a\u6837\u5316\u7684\u4ea4\u6613\u4ea7\u54c1<\/strong>\uff1aIB\u652f\u6301\u80a1\u7968\u3001\u671f\u6743\u3001\u671f\u8d27\u3001\u5916\u6c47\u7b49\u591a\u79cd\u4ea4\u6613\u4ea7\u54c1\uff0c\u9002\u5408\u591a\u8d44\u4ea7\u7b56\u7565\u3002<\/li>\n<li><strong>\u5f3a\u5927\u7684API\u529f\u80fd<\/strong>\uff1aIB API\u652f\u6301\u5b9e\u65f6\u5e02\u573a\u6570\u636e\u3001\u8ba2\u5355\u7ba1\u7406\u3001\u8d26\u6237\u7ba1\u7406\u7b49\u529f\u80fd\uff0c\u9002\u5408\u590d\u6742\u7684\u7a0b\u5e8f\u5316\u4ea4\u6613\u9700\u6c42\u3002<\/li>\n<\/ul>\n<p><h3>\u4e8c\u3001\u6570\u636e\u5904\u7406\u548c\u6280\u672f\u5206\u6790<\/h3>\n<\/p>\n<p><p>\u5728\u83b7\u53d6\u5e02\u573a\u6570\u636e\u4e4b\u540e\uff0c\u4e0b\u4e00\u6b65\u662f\u5904\u7406\u6570\u636e\u5e76\u8fdb\u884c\u6280\u672f\u5206\u6790\u3002Python\u7684pandas\u548cnumpy\u5e93\u662f\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u7684\u5f3a\u5927\u5de5\u5177\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u6570\u636e\u83b7\u53d6<\/h4>\n<\/p>\n<p><p>\u4ece\u4ea4\u6613\u5e73\u53f0\u83b7\u53d6\u6570\u636e\u901a\u5e38\u5305\u62ec\u5b9e\u65f6\u6570\u636e\u548c\u5386\u53f2\u6570\u636e\u3002\u4ee5Alpaca\u4e3a\u4f8b\uff0c\u53ef\u4ee5\u4f7f\u7528\u5176Python SDK\u83b7\u53d6\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import alpaca_trade_api as tradeapi<\/p>\n<p>api = tradeapi.REST(&#39;APCA-API-KEY-ID&#39;, &#39;APCA-API-SECRET-KEY&#39;, base_url=&#39;https:\/\/paper-api.alpaca.markets&#39;)<\/p>\n<p>data = api.get_barset(&#39;AAPL&#39;, &#39;day&#39;, limit=100)<\/p>\n<p>df = data.df[&#39;AAPL&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u6570\u636e\u6e05\u6d17<\/h4>\n<\/p>\n<p><p>\u83b7\u53d6\u6570\u636e\u540e\uff0c\u901a\u5e38\u9700\u8981\u8fdb\u884c\u6570\u636e\u6e05\u6d17\u548c\u9884\u5904\u7406\u3002\u4f8b\u5982\uff0c\u5904\u7406\u7f3a\u5931\u503c\u3001\u53bb\u9664\u5f02\u5e38\u503c\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5904\u7406\u7f3a\u5931\u503c<\/p>\n<p>df.fillna(method=&#39;ffill&#39;, inplace=True)<\/p>\n<h2><strong>\u53bb\u9664\u5f02\u5e38\u503c<\/strong><\/h2>\n<p>df = df[(df[&#39;close&#39;] &gt; 0) &amp; (df[&#39;volume&#39;] &gt; 0)]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u6280\u672f\u6307\u6807\u8ba1\u7b97<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528pandas\u548cta-lib\u7b49\u5e93\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8ba1\u7b97\u5404\u79cd\u6280\u672f\u6307\u6807\uff0c\u5982\u79fb\u52a8\u5e73\u5747\u7ebf\uff08SMA\uff09\u3001\u76f8\u5bf9\u5f3a\u5f31\u6307\u6570\uff08RSI\uff09\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import talib<\/p>\n<p>df[&#39;SMA&#39;] = talib.SMA(df[&#39;close&#39;], timeperiod=20)<\/p>\n<p>df[&#39;RSI&#39;] = talib.RSI(df[&#39;close&#39;], timeperiod=14)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u7f16\u5199\u4ea4\u6613\u7b56\u7565<\/h3>\n<\/p>\n<p><p>\u4ea4\u6613\u7b56\u7565\u662f\u7a0b\u5e8f\u5316\u4ea4\u6613\u7684\u6838\u5fc3\u90e8\u5206\u3002\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u7b56\u7565\u662f\u57fa\u4e8e\u5747\u7ebf\u4ea4\u53c9\u7684\u7b56\u7565\uff0c\u5f53\u77ed\u671f\u5747\u7ebf\u4e0a\u7a7f\u957f\u671f\u5747\u7ebf\u65f6\u4e70\u5165\uff0c\u5f53\u77ed\u671f\u5747\u7ebf\u4e0b\u7a7f\u957f\u671f\u5747\u7ebf\u65f6\u5356\u51fa\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u7b80\u5355\u5747\u7ebf\u7b56\u7565<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">short_window = 40<\/p>\n<p>long_window = 100<\/p>\n<p>df[&#39;short_mavg&#39;] = df[&#39;close&#39;].rolling(window=short_window, min_periods=1).mean()<\/p>\n<p>df[&#39;long_mavg&#39;] = df[&#39;close&#39;].rolling(window=long_window, min_periods=1).mean()<\/p>\n<p>df[&#39;signal&#39;] = 0.0<\/p>\n<p>df[&#39;signal&#39;][short_window:] = np.where(df[&#39;short_mavg&#39;][short_window:] &gt; df[&#39;long_mavg&#39;][short_window:], 1.0, 0.0)<\/p>\n<p>df[&#39;positions&#39;] = df[&#39;signal&#39;].diff()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u7b56\u7565\u5b9e\u73b0<\/h4>\n<\/p>\n<p><p>\u5728\u7b56\u7565\u5b9e\u73b0\u8fc7\u7a0b\u4e2d\uff0c\u9700\u8981\u6839\u636e\u751f\u6210\u7684\u4e70\u5356\u4fe1\u53f7\u6267\u884c\u4ea4\u6613\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for i in range(len(df)):<\/p>\n<p>    if df[&#39;positions&#39;].iloc[i] == 1:<\/p>\n<p>        api.submit_order(<\/p>\n<p>            symbol=&#39;AAPL&#39;,<\/p>\n<p>            qty=10,<\/p>\n<p>            side=&#39;buy&#39;,<\/p>\n<p>            type=&#39;market&#39;,<\/p>\n<p>            time_in_force=&#39;gtc&#39;<\/p>\n<p>        )<\/p>\n<p>    elif df[&#39;positions&#39;].iloc[i] == -1:<\/p>\n<p>        api.submit_order(<\/p>\n<p>            symbol=&#39;AAPL&#39;,<\/p>\n<p>            qty=10,<\/p>\n<p>            side=&#39;sell&#39;,<\/p>\n<p>            type=&#39;market&#39;,<\/p>\n<p>            time_in_force=&#39;gtc&#39;<\/p>\n<p>        )<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u56de\u6d4b\u548c\u4f18\u5316<\/h3>\n<\/p>\n<p><p>\u5728\u771f\u5b9e\u4ea4\u6613\u4e4b\u524d\uff0c\u56de\u6d4b\u662f\u9a8c\u8bc1\u7b56\u7565\u6709\u6548\u6027\u7684\u91cd\u8981\u6b65\u9aa4\u3002\u56de\u6d4b\u53ef\u4ee5\u5e2e\u52a9\u53d1\u73b0\u7b56\u7565\u6f5c\u5728\u7684\u95ee\u9898\uff0c\u5e76\u8fdb\u884c\u4f18\u5316\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u56de\u6d4b\u6846\u67b6<\/h4>\n<\/p>\n<p><p>\u6709\u8bb8\u591a\u5f00\u6e90\u7684\u56de\u6d4b\u6846\u67b6\u53ef\u4ee5\u4f7f\u7528\uff0c\u5982Backtrader\u3001Zipline\u7b49\u3002\u4ee5Backtrader\u4e3a\u4f8b\uff0c\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u56de\u6d4b\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import backtrader as bt<\/p>\n<p>class SmaCross(bt.SignalStrategy):<\/p>\n<p>    def __init__(self):<\/p>\n<p>        sma1, sma2 = bt.ind.SMA(period=10), bt.ind.SMA(period=30)<\/p>\n<p>        crossover = bt.ind.CrossOver(sma1, sma2)<\/p>\n<p>        self.signal_add(bt.SIGNAL_LONG, crossover)<\/p>\n<p>cerebro = bt.Cerebro()<\/p>\n<p>cerebro.addstrategy(SmaCross)<\/p>\n<p>data = bt.feeds.PandasData(dataname=df)<\/p>\n<p>cerebro.adddata(data)<\/p>\n<p>cerebro.run()<\/p>\n<p>cerebro.plot()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u53c2\u6570\u4f18\u5316<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7\u56de\u6d4b\u53ef\u4ee5\u53d1\u73b0\u7b56\u7565\u7684\u8868\u73b0\uff0c\u5e76\u8fdb\u884c\u53c2\u6570\u4f18\u5316\u3002\u4f18\u5316\u53ef\u4ee5\u901a\u8fc7\u7f51\u683c\u641c\u7d22\u3001\u9057\u4f20\u7b97\u6cd5\u7b49\u65b9\u6cd5\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from itertools import product<\/p>\n<p>short_windows = range(10, 50, 10)<\/p>\n<p>long_windows = range(50, 200, 50)<\/p>\n<p>best_performance = -float(&#39;inf&#39;)<\/p>\n<p>best_params = None<\/p>\n<p>for short, long in product(short_windows, long_windows):<\/p>\n<p>    df[&#39;short_mavg&#39;] = df[&#39;close&#39;].rolling(window=short, min_periods=1).mean()<\/p>\n<p>    df[&#39;long_mavg&#39;] = df[&#39;close&#39;].rolling(window=long, min_periods=1).mean()<\/p>\n<p>    # \u8ba1\u7b97\u7b56\u7565\u8868\u73b0\uff0c\u5047\u8bbe\u7528\u67d0\u79cd\u65b9\u6cd5\u83b7\u5f97\u7b56\u7565\u6536\u76ca<\/p>\n<p>    performance = compute_strategy_performance(df)<\/p>\n<p>    if performance &gt; best_performance:<\/p>\n<p>        best_performance = performance<\/p>\n<p>        best_params = (short, long)<\/p>\n<p>print(f&quot;Best parameters: short window={best_params[0]}, long window={best_params[1]}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u98ce\u9669\u7ba1\u7406<\/h3>\n<\/p>\n<p><p>\u98ce\u9669\u7ba1\u7406\u5728\u7a0b\u5e8f\u5316\u4ea4\u6613\u4e2d\u81f3\u5173\u91cd\u8981\u3002\u6709\u6548\u7684\u98ce\u9669\u7ba1\u7406\u53ef\u4ee5\u5e2e\u52a9\u63a7\u5236\u635f\u5931\uff0c\u4fdd\u62a4\u8d44\u672c\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u8bbe\u7f6e\u6b62\u635f\u548c\u6b62\u76c8<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7\u8bbe\u7f6e\u6b62\u635f\u548c\u6b62\u76c8\uff0c\u53ef\u4ee5\u5728\u4e00\u5b9a\u8303\u56f4\u5185\u63a7\u5236\u635f\u5931\u548c\u9501\u5b9a\u5229\u6da6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">stop_loss_pct = 0.02<\/p>\n<p>take_profit_pct = 0.05<\/p>\n<p>for i in range(len(df)):<\/p>\n<p>    if df[&#39;positions&#39;].iloc[i] == 1:<\/p>\n<p>        entry_price = df[&#39;close&#39;].iloc[i]<\/p>\n<p>        stop_loss_price = entry_price * (1 - stop_loss_pct)<\/p>\n<p>        take_profit_price = entry_price * (1 + take_profit_pct)<\/p>\n<p>        api.submit_order(<\/p>\n<p>            symbol=&#39;AAPL&#39;,<\/p>\n<p>            qty=10,<\/p>\n<p>            side=&#39;buy&#39;,<\/p>\n<p>            type=&#39;market&#39;,<\/p>\n<p>            time_in_force=&#39;gtc&#39;,<\/p>\n<p>            stop_loss={&#39;stop_price&#39;: stop_loss_price},<\/p>\n<p>            take_profit={&#39;limit_price&#39;: take_profit_price}<\/p>\n<p>        )<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u4ed3\u4f4d\u7ba1\u7406<\/h4>\n<\/p>\n<p><p>\u5408\u7406\u7684\u4ed3\u4f4d\u7ba1\u7406\u53ef\u4ee5\u5e2e\u52a9\u5206\u6563\u98ce\u9669\uff0c\u9632\u6b62\u5355\u4e00\u5934\u5bf8\u5bf9\u6574\u4f53\u8d26\u6237\u9020\u6210\u91cd\u5927\u5f71\u54cd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">account_value = api.get_account().cash<\/p>\n<p>position_size = account_value * 0.01  # \u6bcf\u7b14\u4ea4\u6613\u5360\u7528\u8d26\u6237\u8d44\u91d1\u76841%<\/p>\n<p>for i in range(len(df)):<\/p>\n<p>    if df[&#39;positions&#39;].iloc[i] == 1:<\/p>\n<p>        shares_to_buy = position_size \/\/ df[&#39;close&#39;].iloc[i]<\/p>\n<p>        api.submit_order(<\/p>\n<p>            symbol=&#39;AAPL&#39;,<\/p>\n<p>            qty=shares_to_buy,<\/p>\n<p>            side=&#39;buy&#39;,<\/p>\n<p>            type=&#39;market&#39;,<\/p>\n<p>            time_in_force=&#39;gtc&#39;<\/p>\n<p>        )<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u76d1\u63a7\u548c\u7ef4\u62a4<\/h3>\n<\/p>\n<p><p>\u7a0b\u5e8f\u5316\u4ea4\u6613\u7cfb\u7edf\u9700\u8981\u6301\u7eed\u76d1\u63a7\u548c\u7ef4\u62a4\uff0c\u4ee5\u786e\u4fdd\u5176\u7a33\u5b9a\u6027\u548c\u6709\u6548\u6027\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b9e\u65f6\u76d1\u63a7<\/h4>\n<\/p>\n<p><p>\u5b9e\u65f6\u76d1\u63a7\u4ea4\u6613\u7cfb\u7edf\u7684\u8fd0\u884c\u72b6\u6001\uff0c\u5305\u62ec\u8ba2\u5355\u6267\u884c\u60c5\u51b5\u3001\u8d26\u6237\u4f59\u989d\u3001\u6301\u4ed3\u60c5\u51b5\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>while True:<\/p>\n<p>    orders = api.list_orders(status=&#39;all&#39;)<\/p>\n<p>    for order in orders:<\/p>\n<p>        print(order)<\/p>\n<p>    time.sleep(60)  # \u6bcf\u5206\u949f\u68c0\u67e5\u4e00\u6b21<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u7b56\u7565\u8c03\u6574<\/h4>\n<\/p>\n<p><p>\u5e02\u573a\u73af\u5883\u4e0d\u65ad\u53d8\u5316\uff0c\u7b56\u7565\u9700\u8981\u6839\u636e\u5e02\u573a\u53d8\u5316\u8fdb\u884c\u8c03\u6574\u548c\u4f18\u5316\u3002\u5b9a\u671f\u56de\u987e\u7b56\u7565\u8868\u73b0\uff0c\u8fdb\u884c\u5fc5\u8981\u7684\u8c03\u6574\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u671f\u56de\u6d4b\u548c\u4f18\u5316\u7b56\u7565<\/p>\n<p>if is_time_to_reoptimize():<\/p>\n<p>    best_params = optimize_strategy(df)<\/p>\n<p>    update_strategy_params(best_params)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528Python\u8fdb\u884c\u7a0b\u5e8f\u5316\u4ea4\u6613\u6d89\u53ca\u9009\u62e9\u5408\u9002\u7684\u4ea4\u6613\u5e73\u53f0\u548cAPI\u3001\u6570\u636e\u5904\u7406\u548c\u6280\u672f\u5206\u6790\u3001\u7f16\u5199\u4ea4\u6613\u7b56\u7565\u3001\u56de\u6d4b\u548c\u4f18\u5316\u3001\u98ce\u9669\u7ba1\u7406\u3001\u4ee5\u53ca\u7cfb\u7edf\u7684\u76d1\u63a7\u548c\u7ef4\u62a4\u3002Python\u7684\u7b80\u6d01\u6027\u548c\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u80fd\u529b\u4f7f\u5176\u6210\u4e3a\u7a0b\u5e8f\u5316\u4ea4\u6613\u7684\u7406\u60f3\u9009\u62e9\u3002\u901a\u8fc7\u5408\u7406\u7684\u7b56\u7565\u8bbe\u8ba1\u548c\u6709\u6548\u7684\u98ce\u9669\u7ba1\u7406\uff0c\u53ef\u4ee5\u5728\u91d1\u878d\u5e02\u573a\u4e2d\u83b7\u5f97\u7a33\u5b9a\u7684\u6536\u76ca\u3002<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u64cd\u4f5c\u4e2d\uff0c\u8fd8\u9700\u8981\u4e0d\u65ad\u5b66\u4e60\u548c\u63d0\u5347\uff0c\u4ee5\u9002\u5e94\u5e02\u573a\u7684\u53d8\u5316\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u80fd\u4e3a\u4f60\u63d0\u4f9b\u4e00\u4e2a\u5168\u9762\u7684\u6307\u5bfc\uff0c\u5e2e\u52a9\u4f60\u5728\u7a0b\u5e8f\u5316\u4ea4\u6613\u7684\u9053\u8def\u4e0a\u53d6\u5f97\u6210\u529f\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u7a0b\u5e8f\u5316\u4ea4\u6613\u7684\u57fa\u672c\u6982\u5ff5\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u7a0b\u5e8f\u5316\u4ea4\u6613\u662f\u6307\u5229\u7528\u8ba1\u7b97\u673a\u7a0b\u5e8f\u81ea\u52a8\u6267\u884c\u4ea4\u6613\u7b56\u7565\u7684\u8fc7\u7a0b\u3002\u8fd9\u79cd\u65b9\u6cd5\u901a\u5e38\u4f9d\u8d56\u4e8e\u7b97\u6cd5\u548c\u9884\u8bbe\u7684\u6761\u4ef6\uff0c\u80fd\u591f\u5feb\u901f\u5206\u6790\u5e02\u573a\u6570\u636e\u5e76\u505a\u51fa\u51b3\u7b56\uff0c\u4ece\u800c\u63d0\u9ad8\u4ea4\u6613\u6548\u7387\u5e76\u51cf\u5c11\u4eba\u4e3a\u9519\u8bef\u3002Python\u4f5c\u4e3a\u4e00\u79cd\u5f3a\u5927\u7684\u7f16\u7a0b\u8bed\u8a00\uff0c\u56e0\u5176\u4e30\u5bcc\u7684\u5e93\u548c\u6846\u67b6\uff0c\u6210\u4e3a\u4e86\u7a0b\u5e8f\u5316\u4ea4\u6613\u9886\u57df\u7684\u70ed\u95e8\u9009\u62e9\u3002<\/p>\n<p><strong>\u6211\u9700\u8981\u638c\u63e1\u54ea\u4e9bPython\u5e93\u6765\u8fdb\u884c\u7a0b\u5e8f\u5316\u4ea4\u6613\uff1f<\/strong><br \/>\u4e3a\u4e86\u6709\u6548\u8fdb\u884c\u7a0b\u5e8f\u5316\u4ea4\u6613\uff0c\u5efa\u8bae\u719f\u6089\u4e00\u4e9b\u5173\u952e\u7684Python\u5e93\uff0c\u5982Pandas\u7528\u4e8e\u6570\u636e\u5206\u6790\uff0cNumPy\u7528\u4e8e\u6570\u503c\u8ba1\u7b97\uff0cMatplotlib\u548cSeaborn\u7528\u4e8e\u6570\u636e\u53ef\u89c6\u5316\uff0c\u4ee5\u53caTA-Lib\u7528\u4e8e\u6280\u672f\u5206\u6790\u3002\u6b64\u5916\uff0c\u50cfBacktrader\u548cZipline\u8fd9\u6837\u7684\u5e93\u5219\u80fd\u591f\u5e2e\u52a9\u7528\u6237\u8fdb\u884c\u7b56\u7565\u56de\u6d4b\uff0c\u786e\u4fdd\u4ea4\u6613\u7b56\u7565\u5728\u5386\u53f2\u6570\u636e\u4e0a\u7684\u6709\u6548\u6027\u3002<\/p>\n<p><strong>\u5982\u4f55\u83b7\u53d6\u5e02\u573a\u6570\u636e\u4ee5\u8fdb\u884c\u7a0b\u5e8f\u5316\u4ea4\u6613\uff1f<\/strong><br \/>\u83b7\u53d6\u5e02\u573a\u6570\u636e\u662f\u7a0b\u5e8f\u5316\u4ea4\u6613\u7684\u57fa\u7840\u3002\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u83b7\u53d6\u5b9e\u65f6\u548c\u5386\u53f2\u6570\u636e\uff0c\u5e38\u89c1\u7684\u9009\u62e9\u5305\u62ec\u91d1\u878d\u6570\u636eAPI\uff08\u5982Alpha Vantage\u3001Yahoo Finance\u3001IEX Cloud\u7b49\uff09\u548c\u4ea4\u6613\u5e73\u53f0\u7684API\uff08\u5982Binance\u3001Interactive Brokers\u7b49\uff09\u3002\u8fd9\u4e9bAPI\u901a\u5e38\u63d0\u4f9b\u4e30\u5bcc\u7684\u5e02\u573a\u6570\u636e\uff0c\u7528\u6237\u53ef\u4ee5\u6839\u636e\u81ea\u5df1\u7684\u9700\u6c42\u8fdb\u884c\u9009\u62e9\u548c\u5b9a\u5236\u3002<\/p>\n<p><strong>\u5728\u4f7f\u7528Python\u8fdb\u884c\u7a0b\u5e8f\u5316\u4ea4\u6613\u65f6\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u98ce\u9669\uff1f<\/strong><br \/>\u5c3d\u7ba1\u7a0b\u5e8f\u5316\u4ea4\u6613\u80fd\u63d0\u9ad8\u6548\u7387\u548c\u7cbe\u786e\u5ea6\uff0c\u4f46\u4ecd\u9700\u6ce8\u610f\u98ce\u9669\u7ba1\u7406\u3002\u5e02\u573a\u6ce2\u52a8\u3001\u7b97\u6cd5\u7f3a\u9677\u548c\u6570\u636e\u5ef6\u8fdf\u7b49\u56e0\u7d20\u53ef\u80fd\u5bfc\u81f4\u610f\u5916\u635f\u5931\u3002\u56e0\u6b64\uff0c\u5efa\u8bae\u5728\u5b9e\u9645\u4ea4\u6613\u524d\u8fdb\u884c\u5145\u5206\u7684\u7b56\u7565\u56de\u6d4b\uff0c\u8bbe\u7f6e\u6b62\u635f\u548c\u6b62\u76c8\u70b9\uff0c\u5e76\u4e0d\u65ad\u76d1\u63a7\u5e02\u573a\u60c5\u51b5\u3002\u540c\u65f6\uff0c\u4fdd\u6301\u5bf9\u5e02\u573a\u52a8\u6001\u7684\u5173\u6ce8\uff0c\u4ee5\u4fbf\u53ca\u65f6\u8c03\u6574\u4ea4\u6613\u7b56\u7565\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728\u7a0b\u5e8f\u5316\u4ea4\u6613\u4e2d\uff0cPython\u662f\u4e00\u79cd\u975e\u5e38\u53d7\u6b22\u8fce\u7684\u7f16\u7a0b\u8bed\u8a00\u3002Python\u7b80\u6d01\u6613\u5b66\u3001\u62e5\u6709\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u80fd\u529b\u3001\u4e30\u5bcc\u7684\u91d1 [&hellip;]","protected":false},"author":3,"featured_media":1072926,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1072917"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1072917"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1072917\/revisions"}],"predecessor-version":[{"id":1072928,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1072917\/revisions\/1072928"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1072926"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1072917"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1072917"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1072917"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}