{"id":1111533,"date":"2025-01-08T17:30:10","date_gmt":"2025-01-08T09:30:10","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1111533.html"},"modified":"2025-01-08T17:30:14","modified_gmt":"2025-01-08T09:30:14","slug":"python%e5%a6%82%e4%bd%95%e6%8e%a5%e5%85%a5%e8%bd%af%e4%bb%b6%e8%bf%9b%e8%a1%8c%e6%9c%9f%e8%b4%a7%e4%ba%a4%e6%98%93","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1111533.html","title":{"rendered":"python\u5982\u4f55\u63a5\u5165\u8f6f\u4ef6\u8fdb\u884c\u671f\u8d27\u4ea4\u6613"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25073931\/d9ebc48b-6020-47c8-acc8-28ac0190fa87.webp\" alt=\"python\u5982\u4f55\u63a5\u5165\u8f6f\u4ef6\u8fdb\u884c\u671f\u8d27\u4ea4\u6613\" \/><\/p>\n<p><p> <strong>Python\u63a5\u5165\u8f6f\u4ef6\u8fdb\u884c\u671f\u8d27\u4ea4\u6613\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528API\u3001\u501f\u52a9\u4ea4\u6613\u5e93\u3001\u4e0e\u4ea4\u6613\u5e73\u53f0\u96c6\u6210\u3001\u81ea\u52a8\u5316\u4ea4\u6613\u7b56\u7565\u3002<\/strong>\u5176\u4e2d\uff0c\u4f7f\u7528API\uff08\u5e94\u7528\u7a0b\u5e8f\u7f16\u7a0b\u63a5\u53e3\uff09\u662f\u6700\u5e38\u89c1\u548c\u9ad8\u6548\u7684\u65b9\u6cd5\u4e4b\u4e00\u3002\u901a\u8fc7API\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u76f4\u63a5\u4e0e\u671f\u8d27\u4ea4\u6613\u5e73\u53f0\u8fdb\u884c\u4ea4\u4e92\uff0c\u83b7\u53d6\u5b9e\u65f6\u6570\u636e\u3001\u6267\u884c\u4ea4\u6613\u3001\u7ba1\u7406\u8d26\u6237\u7b49\u64cd\u4f5c\u3002\u63a5\u4e0b\u6765\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528API\u63a5\u5165\u8f6f\u4ef6\u8fdb\u884c\u671f\u8d27\u4ea4\u6613\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528API<\/p>\n<\/p>\n<p><p>\u4f7f\u7528API\u662fPython\u63a5\u5165\u671f\u8d27\u4ea4\u6613\u8f6f\u4ef6\u7684\u5173\u952e\u6b65\u9aa4\u3002\u8bb8\u591a\u671f\u8d27\u4ea4\u6613\u5e73\u53f0\u90fd\u63d0\u4f9bAPI\u63a5\u53e3\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u901a\u8fc7\u8fd9\u4e9b\u63a5\u53e3\u4e0e\u4ea4\u6613\u5e73\u53f0\u8fdb\u884c\u4ea4\u4e92\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528API\u63a5\u5165\u671f\u8d27\u4ea4\u6613\u8f6f\u4ef6\u7684\u8be6\u7ec6\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><p>1\u3001\u9009\u62e9\u4ea4\u6613\u5e73\u53f0<\/p>\n<\/p>\n<p><p>\u9996\u5148\u9700\u8981\u9009\u62e9\u4e00\u4e2a\u652f\u6301API\u7684\u671f\u8d27\u4ea4\u6613\u5e73\u53f0\u3002\u5e38\u89c1\u7684\u671f\u8d27\u4ea4\u6613\u5e73\u53f0\u5305\u62ecInteractive Brokers\u3001TD Ameritrade\u3001Alpaca\u7b49\u3002\u8fd9\u4e9b\u5e73\u53f0\u4e0d\u4ec5\u63d0\u4f9b\u4e30\u5bcc\u7684API\u63a5\u53e3\uff0c\u8fd8\u62e5\u6709\u826f\u597d\u7684\u6587\u6863\u548c\u652f\u6301\u670d\u52a1\u3002<\/p>\n<\/p>\n<p><p>2\u3001\u6ce8\u518c\u5e76\u83b7\u53d6API\u5bc6\u94a5<\/p>\n<\/p>\n<p><p>\u5728\u9009\u5b9a\u5e73\u53f0\u540e\uff0c\u9700\u8981\u6ce8\u518c\u4e00\u4e2a\u8d26\u6237\u5e76\u83b7\u53d6API\u5bc6\u94a5\u3002API\u5bc6\u94a5\u662f\u8bbf\u95ee\u4ea4\u6613\u5e73\u53f0\u7684\u51ed\u8bc1\uff0c\u4f7f\u7528\u5b83\u53ef\u4ee5\u8fdb\u884c\u8eab\u4efd\u9a8c\u8bc1\u548c\u6388\u6743\u3002\u901a\u5e38\uff0cAPI\u5bc6\u94a5\u5305\u542b\u4e24\u4e2a\u90e8\u5206\uff1aAPI Key\u548cAPI Secret\u3002<\/p>\n<\/p>\n<p><p>3\u3001\u5b89\u88c5API\u5e93<\/p>\n<\/p>\n<p><p>\u5927\u591a\u6570\u4ea4\u6613\u5e73\u53f0\u90fd\u4f1a\u63d0\u4f9b\u76f8\u5e94\u7684API\u5e93\uff0c\u65b9\u4fbf\u5f00\u53d1\u8005\u5728\u4e0d\u540c\u7f16\u7a0b\u8bed\u8a00\u4e2d\u4f7f\u7528\u3002\u4f8b\u5982\uff0cInteractive Brokers\u63d0\u4f9b\u4e86ibapi\u5e93\uff0cTD Ameritrade\u63d0\u4f9b\u4e86td-ameritrade-python-api\u5e93\u3002\u53ef\u4ee5\u4f7f\u7528pip\u547d\u4ee4\u5b89\u88c5\u8fd9\u4e9b\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">pip install ibapi<\/p>\n<p>pip install td-ameritrade-python-api<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>4\u3001\u8fde\u63a5\u5230\u4ea4\u6613\u5e73\u53f0<\/p>\n<\/p>\n<p><p>\u5b89\u88c5API\u5e93\u540e\uff0c\u9700\u8981\u5728\u4ee3\u7801\u4e2d\u8fde\u63a5\u5230\u4ea4\u6613\u5e73\u53f0\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528ibapi\u5e93\u8fde\u63a5Interactive Brokers\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from ibapi.client import EClient<\/p>\n<p>from ibapi.wrapper import EWrapper<\/p>\n<p>from ibapi.contract import Contract<\/p>\n<p>class IBApi(EWrapper, EClient):<\/p>\n<p>    def __init__(self):<\/p>\n<p>        EClient.__init__(self, self)<\/p>\n<p>def m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n():<\/p>\n<p>    app = IBApi()<\/p>\n<p>    app.connect(&quot;127.0.0.1&quot;, 7497, 0)<\/p>\n<p>    app.run()<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>5\u3001\u83b7\u53d6\u5b9e\u65f6\u6570\u636e<\/p>\n<\/p>\n<p><p>\u8fde\u63a5\u5230\u4ea4\u6613\u5e73\u53f0\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7API\u83b7\u53d6\u5b9e\u65f6\u6570\u636e\u3002\u4f8b\u5982\uff0c\u4ee5\u4e0b\u4ee3\u7801\u5c55\u793a\u4e86\u5982\u4f55\u83b7\u53d6\u67d0\u4e2a\u671f\u8d27\u5408\u7ea6\u7684\u5b9e\u65f6\u884c\u60c5\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class IBApi(EWrapper, EClient):<\/p>\n<p>    def __init__(self):<\/p>\n<p>        EClient.__init__(self, self)<\/p>\n<p>    def tickPrice(self, reqId, tickType, price, attrib):<\/p>\n<p>        print(f&quot;Tick Price. Ticker Id: {reqId}, tickType: {tickType}, Price: {price}&quot;)<\/p>\n<p>def main():<\/p>\n<p>    app = IBApi()<\/p>\n<p>    app.connect(&quot;127.0.0.1&quot;, 7497, 0)<\/p>\n<p>    contract = Contract()<\/p>\n<p>    contract.symbol = &quot;ES&quot;<\/p>\n<p>    contract.secType = &quot;FUT&quot;<\/p>\n<p>    contract.exchange = &quot;GLOBEX&quot;<\/p>\n<p>    contract.currency = &quot;USD&quot;<\/p>\n<p>    contract.lastTradeDateOrContractMonth = &quot;202212&quot;<\/p>\n<p>    app.reqMktData(1, contract, &quot;&quot;, False, False, [])<\/p>\n<p>    app.run()<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>6\u3001\u6267\u884c\u4ea4\u6613<\/p>\n<\/p>\n<p><p>\u83b7\u53d6\u5b9e\u65f6\u6570\u636e\u540e\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u8981\u6267\u884c\u4ea4\u6613\u3002\u4ee5\u4e0b\u4ee3\u7801\u5c55\u793a\u4e86\u5982\u4f55\u901a\u8fc7API\u53d1\u9001\u4e70\u5165\u8ba2\u5355\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from ibapi.order import Order<\/p>\n<p>def create_order(action, quantity, order_type=&quot;MKT&quot;):<\/p>\n<p>    order = Order()<\/p>\n<p>    order.action = action<\/p>\n<p>    order.totalQuantity = quantity<\/p>\n<p>    order.orderType = order_type<\/p>\n<p>    return order<\/p>\n<p>def main():<\/p>\n<p>    app = IBApi()<\/p>\n<p>    app.connect(&quot;127.0.0.1&quot;, 7497, 0)<\/p>\n<p>    contract = Contract()<\/p>\n<p>    contract.symbol = &quot;ES&quot;<\/p>\n<p>    contract.secType = &quot;FUT&quot;<\/p>\n<p>    contract.exchange = &quot;GLOBEX&quot;<\/p>\n<p>    contract.currency = &quot;USD&quot;<\/p>\n<p>    contract.lastTradeDateOrContractMonth = &quot;202212&quot;<\/p>\n<p>    order = create_order(&quot;BUY&quot;, 1)<\/p>\n<p>    app.placeOrder(1, contract, order)<\/p>\n<p>    app.run()<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u501f\u52a9\u4ea4\u6613\u5e93<\/p>\n<\/p>\n<p><p>\u9664\u4e86\u76f4\u63a5\u4f7f\u7528API\u5916\uff0c\u8fd8\u53ef\u4ee5\u501f\u52a9\u4e00\u4e9b\u5f00\u6e90\u7684\u4ea4\u6613\u5e93\u6765\u7b80\u5316\u5f00\u53d1\u8fc7\u7a0b\u3002\u8fd9\u4e9b\u4ea4\u6613\u5e93\u901a\u5e38\u5c01\u88c5\u4e86\u5e38\u7528\u7684\u4ea4\u6613\u529f\u80fd\uff0c\u4f7f\u5f97\u5f00\u53d1\u8005\u53ef\u4ee5\u66f4\u65b9\u4fbf\u5730\u8fdb\u884c\u671f\u8d27\u4ea4\u6613\u3002\u4ee5\u4e0b\u662f\u51e0\u4e2a\u5e38\u7528\u7684\u4ea4\u6613\u5e93\uff1a<\/p>\n<\/p>\n<p><p>1\u3001CCXT<\/p>\n<\/p>\n<p><p>CCXT\u662f\u4e00\u4e2a\u652f\u6301\u591a\u79cd\u52a0\u5bc6\u8d27\u5e01\u4ea4\u6613\u6240\u7684\u4ea4\u6613\u5e93\uff0c\u4f46\u5b83\u4e5f\u652f\u6301\u4e00\u4e9b\u671f\u8d27\u4ea4\u6613\u6240\u3002\u4f7f\u7528CCXT\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u8de8\u4ea4\u6613\u6240\u4ea4\u6613\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528CCXT\u83b7\u53d6\u671f\u8d27\u884c\u60c5\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import ccxt<\/p>\n<p>exchange = ccxt.binance()<\/p>\n<p>markets = exchange.load_markets()<\/p>\n<p>ticker = exchange.fetch_ticker(&#39;BTC\/USDT&#39;)<\/p>\n<p>print(ticker)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>2\u3001Backtrader<\/p>\n<\/p>\n<p><p>Backtrader\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u56de\u6d4b\u5e93\uff0c\u4f46\u5b83\u4e5f\u652f\u6301\u5b9e\u65f6\u4ea4\u6613\u3002\u4f7f\u7528Backtrader\u53ef\u4ee5\u65b9\u4fbf\u5730\u5f00\u53d1\u548c\u6d4b\u8bd5\u4ea4\u6613\u7b56\u7565\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528Backtrader\u8fdb\u884c\u671f\u8d27\u4ea4\u6613\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import backtrader as bt<\/p>\n<p>class MyStrategy(bt.Strategy):<\/p>\n<p>    def __init__(self):<\/p>\n<p>        self.data_close = self.datas[0].close<\/p>\n<p>    def next(self):<\/p>\n<p>        if self.data_close[0] &gt; self.data_close[-1]:<\/p>\n<p>            self.buy(size=1)<\/p>\n<p>        elif self.data_close[0] &lt; self.data_close[-1]:<\/p>\n<p>            self.sell(size=1)<\/p>\n<p>cerebro = bt.Cerebro()<\/p>\n<p>cerebro.addstrategy(MyStrategy)<\/p>\n<p>data = bt.feeds.YahooFinanceData(dataname=&#39;AAPL&#39;, fromdate=datetime(2021, 1, 1), todate=datetime(2021, 12, 31))<\/p>\n<p>cerebro.adddata(data)<\/p>\n<p>cerebro.run()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u4e0e\u4ea4\u6613\u5e73\u53f0\u96c6\u6210<\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u5b9e\u73b0\u66f4\u52a0\u590d\u6742\u7684\u4ea4\u6613\u529f\u80fd\uff0c\u53ef\u4ee5\u5c06Python\u4e0e\u4ea4\u6613\u5e73\u53f0\u8fdb\u884c\u6df1\u5ea6\u96c6\u6210\u3002\u8fd9\u9700\u8981\u4e86\u89e3\u4ea4\u6613\u5e73\u53f0\u7684\u67b6\u6784\u548c\u5de5\u4f5c\u539f\u7406\uff0c\u5e76\u6839\u636e\u9700\u8981\u5f00\u53d1\u76f8\u5e94\u7684\u63a5\u53e3\u548c\u5de5\u5177\u3002\u4ee5\u4e0b\u662f\u51e0\u4e2a\u5e38\u89c1\u7684\u96c6\u6210\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><p>1\u3001\u4f7f\u7528WebSocket<\/p>\n<\/p>\n<p><p>\u8bb8\u591a\u4ea4\u6613\u5e73\u53f0\u90fd\u652f\u6301WebSocket\u534f\u8bae\uff0c\u53ef\u4ee5\u901a\u8fc7WebSocket\u83b7\u53d6\u5b9e\u65f6\u6570\u636e\u548c\u6267\u884c\u4ea4\u6613\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528WebSocket\u8fde\u63a5\u4ea4\u6613\u5e73\u53f0\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import websocket<\/p>\n<p>import json<\/p>\n<p>def on_message(ws, message):<\/p>\n<p>    print(message)<\/p>\n<p>def on_error(ws, error):<\/p>\n<p>    print(error)<\/p>\n<p>def on_close(ws):<\/p>\n<p>    print(&quot;Connection closed&quot;)<\/p>\n<p>def on_open(ws):<\/p>\n<p>    subscribe_message = json.dumps({<\/p>\n<p>        &quot;type&quot;: &quot;subscribe&quot;,<\/p>\n<p>        &quot;channels&quot;: [{&quot;name&quot;: &quot;ticker&quot;, &quot;product_ids&quot;: [&quot;BTC-USD&quot;]}]<\/p>\n<p>    })<\/p>\n<p>    ws.send(subscribe_message)<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    websocket.enableTrace(True)<\/p>\n<p>    ws = websocket.WebSocketApp(&quot;wss:\/\/ws-feed.pro.coinbase.com&quot;,<\/p>\n<p>                                on_message=on_message,<\/p>\n<p>                                on_error=on_error,<\/p>\n<p>                                on_close=on_close)<\/p>\n<p>    ws.on_open = on_open<\/p>\n<p>    ws.run_forever()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>2\u3001\u4f7f\u7528RESTful API<\/p>\n<\/p>\n<p><p>RESTful API\u662f\u53e6\u4e00\u79cd\u5e38\u89c1\u7684\u96c6\u6210\u65b9\u6cd5\u3002\u901a\u8fc7RESTful API\u53ef\u4ee5\u8fdb\u884c\u6570\u636e\u67e5\u8be2\u3001\u8ba2\u5355\u7ba1\u7406\u7b49\u64cd\u4f5c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528RESTful API\u83b7\u53d6\u671f\u8d27\u884c\u60c5\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>def get_futures_ticker(symbol):<\/p>\n<p>    url = f&quot;https:\/\/api.exchange.com\/v1\/futures\/{symbol}\/ticker&quot;<\/p>\n<p>    response = requests.get(url)<\/p>\n<p>    return response.json()<\/p>\n<p>ticker = get_futures_ticker(&quot;BTC-USD&quot;)<\/p>\n<p>print(ticker)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u81ea\u52a8\u5316\u4ea4\u6613\u7b56\u7565<\/p>\n<\/p>\n<p><p>\u5b9e\u73b0\u81ea\u52a8\u5316\u4ea4\u6613\u7b56\u7565\u662fPython\u63a5\u5165\u671f\u8d27\u4ea4\u6613\u8f6f\u4ef6\u7684\u91cd\u8981\u5e94\u7528\u4e4b\u4e00\u3002\u81ea\u52a8\u5316\u4ea4\u6613\u7b56\u7565\u53ef\u4ee5\u6839\u636e\u9884\u8bbe\u7684\u89c4\u5219\u81ea\u52a8\u8fdb\u884c\u4ea4\u6613\uff0c\u51cf\u5c11\u4eba\u5de5\u5e72\u9884\uff0c\u63d0\u9ad8\u4ea4\u6613\u6548\u7387\u3002\u4ee5\u4e0b\u662f\u51e0\u4e2a\u5e38\u89c1\u7684\u81ea\u52a8\u5316\u4ea4\u6613\u7b56\u7565\uff1a<\/p>\n<\/p>\n<p><p>1\u3001\u5747\u7ebf\u4ea4\u53c9\u7b56\u7565<\/p>\n<\/p>\n<p><p>\u5747\u7ebf\u4ea4\u53c9\u7b56\u7565\u662f\u4e00\u79cd\u7b80\u5355\u4f46\u6709\u6548\u7684\u4ea4\u6613\u7b56\u7565\u3002\u5f53\u77ed\u671f\u5747\u7ebf\u4e0a\u7a7f\u957f\u671f\u5747\u7ebf\u65f6\uff0c\u4e70\u5165\uff1b\u5f53\u77ed\u671f\u5747\u7ebf\u4e0b\u7a7f\u957f\u671f\u5747\u7ebf\u65f6\uff0c\u5356\u51fa\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5b9e\u73b0\u5747\u7ebf\u4ea4\u53c9\u7b56\u7565\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>import numpy as np<\/p>\n<p>import backtrader as bt<\/p>\n<p>class MovingAverageCrossStrategy(bt.Strategy):<\/p>\n<p>    params = ((&#39;short_period&#39;, 50), (&#39;long_period&#39;, 200))<\/p>\n<p>    def __init__(self):<\/p>\n<p>        self.short_ma = bt.indicators.SimpleMovingAverage(self.data.close, period=self.params.short_period)<\/p>\n<p>        self.long_ma = bt.indicators.SimpleMovingAverage(self.data.close, period=self.params.long_period)<\/p>\n<p>    def next(self):<\/p>\n<p>        if self.short_ma[0] &gt; self.long_ma[0] and self.short_ma[-1] &lt; self.long_ma[-1]:<\/p>\n<p>            self.buy(size=1)<\/p>\n<p>        elif self.short_ma[0] &lt; self.long_ma[0] and self.short_ma[-1] &gt; self.long_ma[-1]:<\/p>\n<p>            self.sell(size=1)<\/p>\n<p>cerebro = bt.Cerebro()<\/p>\n<p>cerebro.addstrategy(MovingAverageCrossStrategy)<\/p>\n<p>data = bt.feeds.YahooFinanceData(dataname=&#39;AAPL&#39;, fromdate=datetime(2021, 1, 1), todate=datetime(2021, 12, 31))<\/p>\n<p>cerebro.adddata(data)<\/p>\n<p>cerebro.run()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>2\u3001\u52a8\u91cf\u4ea4\u6613\u7b56\u7565<\/p>\n<\/p>\n<p><p>\u52a8\u91cf\u4ea4\u6613\u7b56\u7565\u662f\u57fa\u4e8e\u4ef7\u683c\u52a8\u91cf\u8fdb\u884c\u4ea4\u6613\u3002\u5f53\u4ef7\u683c\u4e0a\u6da8\u65f6\u4e70\u5165\uff0c\u5f53\u4ef7\u683c\u4e0b\u8dcc\u65f6\u5356\u51fa\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5b9e\u73b0\u52a8\u91cf\u4ea4\u6613\u7b56\u7565\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class MomentumStrategy(bt.Strategy):<\/p>\n<p>    params = ((&#39;momentum_period&#39;, 20),)<\/p>\n<p>    def __init__(self):<\/p>\n<p>        self.momentum = bt.indicators.Momentum(self.data.close, period=self.params.momentum_period)<\/p>\n<p>    def next(self):<\/p>\n<p>        if self.momentum[0] &gt; 0:<\/p>\n<p>            self.buy(size=1)<\/p>\n<p>        elif self.momentum[0] &lt; 0:<\/p>\n<p>            self.sell(size=1)<\/p>\n<p>cerebro = bt.Cerebro()<\/p>\n<p>cerebro.addstrategy(MomentumStrategy)<\/p>\n<p>data = bt.feeds.YahooFinanceData(dataname=&#39;AAPL&#39;, fromdate=datetime(2021, 1, 1), todate=datetime(2021, 12, 31))<\/p>\n<p>cerebro.adddata(data)<\/p>\n<p>cerebro.run()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7efc\u4e0a\u6240\u8ff0\uff0cPython\u63a5\u5165\u8f6f\u4ef6\u8fdb\u884c\u671f\u8d27\u4ea4\u6613\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u6839\u636e\u81ea\u8eab\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6848\u3002\u65e0\u8bba\u662f\u4f7f\u7528API\u3001\u501f\u52a9\u4ea4\u6613\u5e93\uff0c\u8fd8\u662f\u4e0e\u4ea4\u6613\u5e73\u53f0\u96c6\u6210\uff0cPython\u90fd\u80fd\u63d0\u4f9b\u5f3a\u5927\u7684\u529f\u80fd\u548c\u7075\u6d3b\u6027\uff0c\u5e2e\u52a9\u5b9e\u73b0\u9ad8\u6548\u7684\u671f\u8d27\u4ea4\u6613\u3002\u901a\u8fc7\u4e0d\u65ad\u5b66\u4e60\u548c\u5b9e\u8df5\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u638c\u63e1\u66f4\u591a\u7684\u6280\u5de7\u548c\u7b56\u7565\uff0c\u4e3a\u671f\u8d27\u4ea4\u6613\u5e26\u6765\u66f4\u591a\u7684\u6536\u76ca\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u8fdb\u884c\u671f\u8d27\u4ea4\u6613\u7684\u57fa\u672c\u6b65\u9aa4\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u5728\u4f7f\u7528Python\u8fdb\u884c\u671f\u8d27\u4ea4\u6613\u65f6\uff0c\u60a8\u9700\u8981\u9996\u5148\u9009\u62e9\u4e00\u4e2a\u5408\u9002\u7684\u4ea4\u6613\u5e73\u53f0\uff0c\u8fd9\u4e9b\u5e73\u53f0\u901a\u5e38\u4f1a\u63d0\u4f9bAPI\u63a5\u53e3\u3002\u63a5\u7740\uff0c\u60a8\u9700\u8981\u5b89\u88c5\u76f8\u5173\u7684Python\u5e93\uff0c\u6bd4\u5982<code>pandas<\/code>\u7528\u4e8e\u6570\u636e\u5904\u7406\uff0c<code>requests<\/code>\u7528\u4e8eAPI\u8c03\u7528\u3002\u968f\u540e\uff0c\u521b\u5efa\u4e00\u4e2a\u8d26\u6237\u5e76\u83b7\u53d6API\u5bc6\u94a5\uff0c\u6700\u540e\u901a\u8fc7\u7f16\u5199Python\u811a\u672c\u6765\u5b9e\u73b0\u7b56\u7565\u7684\u81ea\u52a8\u5316\u4ea4\u6613\u3002<\/p>\n<p><strong>\u6709\u54ea\u4e9b\u5e38\u7528\u7684Python\u5e93\u9002\u5408\u671f\u8d27\u4ea4\u6613\uff1f<\/strong><br \/>\u5728\u671f\u8d27\u4ea4\u6613\u4e2d\uff0c\u5e38\u7528\u7684Python\u5e93\u5305\u62ec<code>ccxt<\/code>\uff0c\u5b83\u652f\u6301\u591a\u4e2a\u4ea4\u6613\u6240\u7684API\uff0c\u53ef\u4ee5\u8f7b\u677e\u83b7\u53d6\u5e02\u573a\u6570\u636e\u548c\u6267\u884c\u4ea4\u6613\u3002<code>Backtrader<\/code>\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u56de\u6d4b\u6846\u67b6\uff0c\u53ef\u4ee5\u5e2e\u52a9\u60a8\u6d4b\u8bd5\u4ea4\u6613\u7b56\u7565\u7684\u6709\u6548\u6027\u3002<code>NumPy<\/code>\u548c<code>Pandas<\/code>\u5219\u5728\u6570\u636e\u5206\u6790\u548c\u5904\u7406\u65b9\u9762\u975e\u5e38\u6709\u7528\uff0c\u5e2e\u52a9\u60a8\u66f4\u597d\u5730\u7406\u89e3\u5e02\u573a\u52a8\u6001\u3002<\/p>\n<p><strong>\u5982\u4f55\u786e\u4fdd\u6211\u7684Python\u4ea4\u6613\u7b56\u7565\u7684\u5b89\u5168\u6027\uff1f<\/strong><br \/>\u786e\u4fdd\u4ea4\u6613\u7b56\u7565\u7684\u5b89\u5168\u6027\u975e\u5e38\u91cd\u8981\u3002\u9996\u5148\uff0c\u4f7f\u7528\u5b89\u5168\u7684API\u5bc6\u94a5\uff0c\u5e76\u5b9a\u671f\u66f4\u6362\u5b83\u4eec\u3002\u5176\u6b21\uff0c\u4f7f\u7528\u865a\u62df\u73af\u5883\u6765\u9694\u79bbPython\u73af\u5883\uff0c\u9632\u6b62\u672a\u6388\u6743\u7684\u8bbf\u95ee\u3002\u6b64\u5916\uff0c\u60a8\u53ef\u4ee5\u5b9e\u65bd\u65e5\u5fd7\u8bb0\u5f55\uff0c\u76d1\u63a7\u4ea4\u6613\u6d3b\u52a8\uff0c\u53ca\u65f6\u53d1\u73b0\u5f02\u5e38\u884c\u4e3a\u3002\u6700\u540e\uff0c\u8fdb\u884c\u5145\u5206\u7684\u6d4b\u8bd5\u548c\u56de\u6d4b\uff0c\u4ee5\u786e\u4fdd\u7b56\u7565\u5728\u4e0d\u540c\u5e02\u573a\u6761\u4ef6\u4e0b\u7684\u8868\u73b0\u7a33\u5b9a\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u63a5\u5165\u8f6f\u4ef6\u8fdb\u884c\u671f\u8d27\u4ea4\u6613\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528API\u3001\u501f\u52a9\u4ea4\u6613\u5e93\u3001\u4e0e\u4ea4\u6613\u5e73\u53f0\u96c6\u6210\u3001\u81ea\u52a8\u5316\u4ea4\u6613\u7b56\u7565\u3002\u5176\u4e2d\uff0c\u4f7f\u7528A [&hellip;]","protected":false},"author":3,"featured_media":1111539,"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\/1111533"}],"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=1111533"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1111533\/revisions"}],"predecessor-version":[{"id":1111546,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1111533\/revisions\/1111546"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1111539"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1111533"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1111533"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1111533"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}