{"id":1051448,"date":"2024-12-31T14:16:32","date_gmt":"2024-12-31T06:16:32","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1051448.html"},"modified":"2024-12-31T14:16:34","modified_gmt":"2024-12-31T06:16:34","slug":"%e5%a6%82%e4%bd%95%e5%b0%86python%e6%8e%a5%e5%85%a5%e4%ba%a4%e6%98%93%e6%89%80","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1051448.html","title":{"rendered":"\u5982\u4f55\u5c06python\u63a5\u5165\u4ea4\u6613\u6240"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/59cd107d-765a-4f94-a03a-6e71fa2df63f.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"\u5982\u4f55\u5c06python\u63a5\u5165\u4ea4\u6613\u6240\" \/><\/p>\n<p><p> <strong>\u8981\u5c06Python\u63a5\u5165\u4ea4\u6613\u6240\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u4ea4\u6613\u6240\u7684API\uff08\u5e94\u7528\u7a0b\u5e8f\u63a5\u53e3\uff09\u3001\u9009\u62e9\u5408\u9002\u7684\u5e93\u6216\u6846\u67b6\u3001\u5904\u7406API\u8bf7\u6c42\u548c\u54cd\u5e94\u3001\u8fdb\u884c\u8eab\u4efd\u9a8c\u8bc1\u548c\u5b89\u5168\u6027\u8bbe\u7f6e\u3002<\/strong> \u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8\u5982\u4f55\u5c06Python\u63a5\u5165\u4ea4\u6613\u6240\uff0c\u4ee5\u4fbf\u60a8\u80fd\u591f\u8fdb\u884c\u81ea\u52a8\u5316\u4ea4\u6613\u6216\u83b7\u53d6\u5e02\u573a\u6570\u636e\u3002<\/p>\n<\/p>\n<hr>\n<p><h2>\u4e00\u3001\u4ea4\u6613\u6240API\u4ecb\u7ecd<\/h2>\n<\/p>\n<p><p>\u4ea4\u6613\u6240API\u662f\u4e00\u4e2a\u63a5\u53e3\uff0c\u5141\u8bb8\u5f00\u53d1\u8005\u4e0e\u4ea4\u6613\u5e73\u53f0\u8fdb\u884c\u4ea4\u4e92\u3002\u5927\u591a\u6570\u4e3b\u6d41\u4ea4\u6613\u6240\uff0c\u5982Binance\u3001Coinbase Pro\u3001Kraken\u7b49\uff0c\u90fd\u63d0\u4f9b\u4e86REST API\u548cWebSocket API\u3002<\/p>\n<\/p>\n<p><h3>1.1 REST API<\/h3>\n<\/p>\n<p><p>REST API\u662f\u4e00\u79cd\u57fa\u4e8eHTTP\u7684\u63a5\u53e3\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u53d1\u9001HTTP\u8bf7\u6c42\u4e0e\u4ea4\u6613\u6240\u8fdb\u884c\u4ea4\u4e92\u3002\u5e38\u89c1\u7684\u64cd\u4f5c\u5305\u62ec\u83b7\u53d6\u5e02\u573a\u6570\u636e\u3001\u67e5\u8be2\u8d26\u6237\u4f59\u989d\u3001\u4e0b\u5355\u3001\u53d6\u6d88\u8ba2\u5355\u7b49\u3002<\/p>\n<\/p>\n<p><h3>1.2 WebSocket API<\/h3>\n<\/p>\n<p><p>WebSocket API\u662f\u4e00\u79cd\u53cc\u5411\u901a\u4fe1\u534f\u8bae\uff0c\u5141\u8bb8\u5b9e\u65f6\u6570\u636e\u4f20\u8f93\u3002\u5b83\u975e\u5e38\u9002\u5408\u9700\u8981\u5feb\u901f\u66f4\u65b0\u7684\u573a\u666f\uff0c\u5982\u4ea4\u6613\u6570\u636e\u548c\u4ef7\u683c\u53d8\u5316\u3002<\/p>\n<\/p>\n<p><h2>\u4e8c\u3001\u9009\u62e9\u5408\u9002\u7684\u5e93\u6216\u6846\u67b6<\/h2>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6709\u8bb8\u591a\u5e93\u53ef\u4ee5\u5e2e\u52a9\u60a8\u4e0e\u4ea4\u6613\u6240API\u8fdb\u884c\u4ea4\u4e92\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684\u5e93\uff1a<\/p>\n<\/p>\n<p><h3>2.1 ccxt<\/h3>\n<\/p>\n<p><p>ccxt\u662f\u4e00\u4e2a\u652f\u6301\u591a\u4e2a\u4ea4\u6613\u6240\u7684\u5e93\uff0c\u80fd\u591f\u7b80\u5316API\u8c03\u7528\u3002\u5b83\u652f\u6301\u8d85\u8fc7100\u4e2a\u4ea4\u6613\u6240\uff0c\u5177\u6709\u7edf\u4e00\u7684API\u63a5\u53e3\uff0c\u4f7f\u7528\u7b80\u5355\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import ccxt<\/p>\n<h2><strong>\u521d\u59cb\u5316\u4ea4\u6613\u6240<\/strong><\/h2>\n<p>exchange = ccxt.binance({<\/p>\n<p>    &#39;apiKey&#39;: &#39;YOUR_API_KEY&#39;,<\/p>\n<p>    &#39;secret&#39;: &#39;YOUR_API_SECRET&#39;,<\/p>\n<p>})<\/p>\n<h2><strong>\u83b7\u53d6\u5e02\u573a\u6570\u636e<\/strong><\/h2>\n<p>markets = exchange.load_markets()<\/p>\n<h2><strong>\u83b7\u53d6\u8d26\u6237\u4f59\u989d<\/strong><\/h2>\n<p>balance = exchange.fetch_balance()<\/p>\n<p>print(balance)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2.2 python-binance<\/h3>\n<\/p>\n<p><p>python-binance\u662fBinance\u4ea4\u6613\u6240\u7684\u4e13\u7528\u5e93\uff0c\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\u548c\u7b80\u5355\u7684API\u63a5\u53e3\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from binance.client import Client<\/p>\n<h2><strong>\u521d\u59cb\u5316\u5ba2\u6237\u7aef<\/strong><\/h2>\n<p>client = Client(&#39;YOUR_API_KEY&#39;, &#39;YOUR_API_SECRET&#39;)<\/p>\n<h2><strong>\u83b7\u53d6\u5e02\u573a\u4ef7\u683c<\/strong><\/h2>\n<p>prices = client.get_all_tickers()<\/p>\n<p>print(prices)<\/p>\n<h2><strong>\u4e0b\u5355<\/strong><\/h2>\n<p>order = client.order_limit_buy(<\/p>\n<p>    symbol=&#39;BTCUSDT&#39;,<\/p>\n<p>    quantity=0.01,<\/p>\n<p>    price=&#39;30000&#39;<\/p>\n<p>)<\/p>\n<p>print(order)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e09\u3001\u5904\u7406API\u8bf7\u6c42\u548c\u54cd\u5e94<\/h2>\n<\/p>\n<p><p>\u5728\u4e0e\u4ea4\u6613\u6240\u8fdb\u884c\u4ea4\u4e92\u65f6\uff0c\u60a8\u9700\u8981\u5904\u7406API\u8bf7\u6c42\u548c\u54cd\u5e94\u3002\u901a\u5e38\uff0c\u60a8\u9700\u8981\u53d1\u9001HTTP\u8bf7\u6c42\u5e76\u89e3\u6790\u8fd4\u56de\u7684JSON\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>3.1 \u53d1\u9001HTTP\u8bf7\u6c42<\/h3>\n<\/p>\n<p><p>\u60a8\u53ef\u4ee5\u4f7f\u7528Python\u7684requests\u5e93\u53d1\u9001HTTP\u8bf7\u6c42\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<h2><strong>\u53d1\u9001GET\u8bf7\u6c42<\/strong><\/h2>\n<p>response = requests.get(&#39;https:\/\/api.binance.com\/api\/v3\/ticker\/price?symbol=BTCUSDT&#39;)<\/p>\n<p>data = response.json()<\/p>\n<p>print(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3.2 \u89e3\u6790JSON\u54cd\u5e94<\/h3>\n<\/p>\n<p><p>\u5927\u591a\u6570API\u54cd\u5e94\u90fd\u662fJSON\u683c\u5f0f\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528json\u5e93\u89e3\u6790\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<h2><strong>\u89e3\u6790JSON\u6570\u636e<\/strong><\/h2>\n<p>data = json.loads(response.text)<\/p>\n<p>print(data[&#39;price&#39;])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u56db\u3001\u8eab\u4efd\u9a8c\u8bc1\u548c\u5b89\u5168\u6027\u8bbe\u7f6e<\/h2>\n<\/p>\n<p><p>\u4e3a\u4e86\u786e\u4fddAPI\u8bf7\u6c42\u7684\u5b89\u5168\u6027\uff0c\u60a8\u9700\u8981\u8fdb\u884c\u8eab\u4efd\u9a8c\u8bc1\u3002\u5927\u591a\u6570\u4ea4\u6613\u6240\u8981\u6c42\u4f7f\u7528API\u5bc6\u94a5\uff08API Key\uff09\u548c\u5bc6\u94a5\uff08Secret\uff09\u8fdb\u884c\u8eab\u4efd\u9a8c\u8bc1\u3002<\/p>\n<\/p>\n<p><h3>4.1 \u521b\u5efaAPI\u5bc6\u94a5<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u60a8\u9700\u8981\u5728\u4ea4\u6613\u6240\u521b\u5efaAPI\u5bc6\u94a5\u3002\u767b\u5f55\u5230\u4ea4\u6613\u6240\u8d26\u6237\uff0c\u627e\u5230API\u7ba1\u7406\u9875\u9762\uff0c\u521b\u5efa\u65b0\u7684API\u5bc6\u94a5\uff0c\u5e76\u8bb0\u5f55\u4e0b\u5bc6\u94a5\u548c\u5bc6\u94a5\u3002<\/p>\n<\/p>\n<p><h3>4.2 \u7b7e\u540d\u8bf7\u6c42<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u67d0\u4e9b\u64cd\u4f5c\uff0c\u5982\u4e0b\u5355\u548c\u67e5\u8be2\u8d26\u6237\u4fe1\u606f\uff0c\u60a8\u9700\u8981\u7b7e\u540d\u8bf7\u6c42\u3002\u7b7e\u540d\u8fc7\u7a0b\u901a\u5e38\u5305\u62ec\u5c06\u8bf7\u6c42\u53c2\u6570\u8fdb\u884c\u6392\u5e8f\u3001\u62fc\u63a5\u6210\u5b57\u7b26\u4e32\u3001\u4f7f\u7528\u5bc6\u94a5\u8fdb\u884c\u52a0\u5bc6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import hmac<\/p>\n<p>import hashlib<\/p>\n<p>import time<\/p>\n<p>api_key = &#39;YOUR_API_KEY&#39;<\/p>\n<p>api_secret = &#39;YOUR_API_SECRET&#39;<\/p>\n<h2><strong>\u5f53\u524d\u65f6\u95f4\u6233<\/strong><\/h2>\n<p>timestamp = int(time.time() * 1000)<\/p>\n<h2><strong>\u8bf7\u6c42\u53c2\u6570<\/strong><\/h2>\n<p>params = {<\/p>\n<p>    &#39;symbol&#39;: &#39;BTCUSDT&#39;,<\/p>\n<p>    &#39;side&#39;: &#39;BUY&#39;,<\/p>\n<p>    &#39;type&#39;: &#39;LIMIT&#39;,<\/p>\n<p>    &#39;timeInForce&#39;: &#39;GTC&#39;,<\/p>\n<p>    &#39;quantity&#39;: 0.01,<\/p>\n<p>    &#39;price&#39;: 30000,<\/p>\n<p>    &#39;recvWindow&#39;: 5000,<\/p>\n<p>    &#39;timestamp&#39;: timestamp<\/p>\n<p>}<\/p>\n<h2><strong>\u62fc\u63a5\u53c2\u6570\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>query_string = &#39;&amp;&#39;.join([f&quot;{key}={params[key]}&quot; for key in sorted(params)])<\/p>\n<h2><strong>\u751f\u6210\u7b7e\u540d<\/strong><\/h2>\n<p>signature = hmac.new(api_secret.encode(), query_string.encode(), hashlib.sha256).hexdigest()<\/p>\n<h2><strong>\u6dfb\u52a0\u7b7e\u540d\u5230\u53c2\u6570<\/strong><\/h2>\n<p>params[&#39;signature&#39;] = signature<\/p>\n<h2><strong>\u53d1\u9001\u8bf7\u6c42<\/strong><\/h2>\n<p>response = requests.post(&#39;https:\/\/api.binance.com\/api\/v3\/order&#39;, params=params, headers={&#39;X-MBX-APIKEY&#39;: api_key})<\/p>\n<p>print(response.json())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e94\u3001\u9519\u8bef\u5904\u7406\u548c\u5f02\u5e38\u5904\u7406<\/h2>\n<\/p>\n<p><p>\u5728\u4e0e\u4ea4\u6613\u6240\u8fdb\u884c\u4ea4\u4e92\u65f6\uff0c\u60a8\u9700\u8981\u5904\u7406\u53ef\u80fd\u51fa\u73b0\u7684\u9519\u8bef\u548c\u5f02\u5e38\u3002\u5e38\u89c1\u7684\u9519\u8bef\u5305\u62ec\u7f51\u7edc\u9519\u8bef\u3001API\u9650\u901f\u3001\u65e0\u6548\u53c2\u6570\u7b49\u3002<\/p>\n<\/p>\n<p><h3>5.1 \u7f51\u7edc\u9519\u8bef<\/h3>\n<\/p>\n<p><p>\u7f51\u7edc\u9519\u8bef\u901a\u5e38\u662f\u7531\u4e8e\u7f51\u7edc\u4e0d\u7a33\u5b9a\u6216\u4ea4\u6613\u6240\u670d\u52a1\u5668\u95ee\u9898\u5bfc\u81f4\u7684\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528try-except\u5757\u6355\u83b7\u5f02\u5e38\uff0c\u5e76\u8fdb\u884c\u91cd\u8bd5\u6216\u8bb0\u5f55\u65e5\u5fd7\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>try:<\/p>\n<p>    response = requests.get(&#39;https:\/\/api.binance.com\/api\/v3\/ticker\/price?symbol=BTCUSDT&#39;)<\/p>\n<p>    data = response.json()<\/p>\n<p>    print(data)<\/p>\n<p>except requests.exceptions.RequestException as e:<\/p>\n<p>    print(f&quot;\u7f51\u7edc\u9519\u8bef: {e}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>5.2 API\u9650\u901f<\/h3>\n<\/p>\n<p><p>\u5927\u591a\u6570\u4ea4\u6613\u6240\u5bf9API\u8bf7\u6c42\u6709\u901f\u7387\u9650\u5236\u3002\u60a8\u9700\u8981\u9075\u5b88\u9650\u901f\u89c4\u5219\uff0c\u5e76\u5728\u8d85\u8fc7\u9650\u901f\u65f6\u8fdb\u884c\u9002\u5f53\u7684\u7b49\u5f85\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>response = requests.get(&#39;https:\/\/api.binance.com\/api\/v3\/ticker\/price?symbol=BTCUSDT&#39;)<\/p>\n<p>if response.status_code == 429:<\/p>\n<p>    print(&quot;\u8d85\u8fc7API\u9650\u901f\uff0c\u7b49\u5f85\u4e00\u6bb5\u65f6\u95f4...&quot;)<\/p>\n<p>    time.sleep(60)<\/p>\n<p>    response = requests.get(&#39;https:\/\/api.binance.com\/api\/v3\/ticker\/price?symbol=BTCUSDT&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>5.3 \u65e0\u6548\u53c2\u6570<\/h3>\n<\/p>\n<p><p>\u65e0\u6548\u53c2\u6570\u901a\u5e38\u662f\u7531\u4e8e\u62fc\u5199\u9519\u8bef\u6216\u53c2\u6570\u503c\u8d85\u51fa\u8303\u56f4\u5bfc\u81f4\u7684\u3002\u60a8\u9700\u8981\u68c0\u67e5\u8bf7\u6c42\u53c2\u6570\uff0c\u5e76\u6839\u636eAPI\u6587\u6863\u8fdb\u884c\u4fee\u6b63\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">response = requests.get(&#39;https:\/\/api.binance.com\/api\/v3\/ticker\/price?symbol=INVALID&#39;)<\/p>\n<p>if response.status_code == 400:<\/p>\n<p>    print(&quot;\u65e0\u6548\u53c2\u6570\uff0c\u8bf7\u68c0\u67e5\u8bf7\u6c42\u53c2\u6570&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u516d\u3001\u5b9e\u6218\u6848\u4f8b\uff1a\u81ea\u52a8\u5316\u4ea4\u6613<\/h2>\n<\/p>\n<p><p>\u5728\u8fd9\u4e00\u90e8\u5206\uff0c\u6211\u4eec\u5c06\u7ed3\u5408\u524d\u9762\u7684\u5185\u5bb9\uff0c\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Python\u8fdb\u884c\u81ea\u52a8\u5316\u4ea4\u6613\u3002<\/p>\n<\/p>\n<p><h3>6.1 \u7b56\u7565\u8bbe\u8ba1<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u60a8\u9700\u8981\u8bbe\u8ba1\u4e00\u4e2a\u4ea4\u6613\u7b56\u7565\u3002\u7b56\u7565\u53ef\u4ee5\u662f\u7b80\u5355\u7684\u5747\u7ebf\u4ea4\u53c9\u3001RSI\u6307\u6807\uff0c\u6216\u590d\u6742\u7684<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u6a21\u578b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def simple_moving_average(data, window):<\/p>\n<p>    return sum(data[-window:]) \/ window<\/p>\n<p>def trading_strategy(prices):<\/p>\n<p>    short_window = 50<\/p>\n<p>    long_window = 200<\/p>\n<p>    if simple_moving_average(prices, short_window) &gt; simple_moving_average(prices, long_window):<\/p>\n<p>        return &#39;BUY&#39;<\/p>\n<p>    else:<\/p>\n<p>        return &#39;SELL&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>6.2 \u83b7\u53d6\u5e02\u573a\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u60a8\u9700\u8981\u5b9a\u671f\u83b7\u53d6\u5e02\u573a\u6570\u636e\uff0c\u5e76\u6839\u636e\u7b56\u7565\u8fdb\u884c\u5224\u65ad\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import ccxt<\/p>\n<p>import time<\/p>\n<p>exchange = ccxt.binance()<\/p>\n<p>symbol = &#39;BTC\/USDT&#39;<\/p>\n<p>while True:<\/p>\n<p>    try:<\/p>\n<p>        ohlcv = exchange.fetch_ohlcv(symbol, timeframe=&#39;1m&#39;, limit=200)<\/p>\n<p>        prices = [x[4] for x in ohlcv]<\/p>\n<p>        action = trading_strategy(prices)<\/p>\n<p>        print(f&quot;\u5f53\u524d\u884c\u52a8: {action}&quot;)<\/p>\n<p>        if action == &#39;BUY&#39;:<\/p>\n<p>            # \u4e0b\u4e70\u5355<\/p>\n<p>            exchange.create_market_buy_order(symbol, 0.01)<\/p>\n<p>        elif action == &#39;SELL&#39;:<\/p>\n<p>            # \u4e0b\u5356\u5355<\/p>\n<p>            exchange.create_market_sell_order(symbol, 0.01)<\/p>\n<p>        # \u7b49\u5f85\u4e00\u5206\u949f<\/p>\n<p>        time.sleep(60)<\/p>\n<p>    except Exception as e:<\/p>\n<p>        print(f&quot;\u9519\u8bef: {e}&quot;)<\/p>\n<p>        time.sleep(60)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>6.3 \u98ce\u9669\u7ba1\u7406<\/h3>\n<\/p>\n<p><p>\u5728\u8fdb\u884c\u81ea\u52a8\u5316\u4ea4\u6613\u65f6\uff0c\u98ce\u9669\u7ba1\u7406\u81f3\u5173\u91cd\u8981\u3002\u60a8\u9700\u8981\u8bbe\u7f6e\u6b62\u635f\u548c\u6b62\u76c8\uff0c\u63a7\u5236\u4ed3\u4f4d\uff0c\u907f\u514d\u8fc7\u5ea6\u4ea4\u6613\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def trading_strategy(prices):<\/p>\n<p>    short_window = 50<\/p>\n<p>    long_window = 200<\/p>\n<p>    if simple_moving_average(prices, short_window) &gt; simple_moving_average(prices, long_window):<\/p>\n<p>        return &#39;BUY&#39;<\/p>\n<p>    else:<\/p>\n<p>        return &#39;SELL&#39;<\/p>\n<p>def place_order(exchange, symbol, action, quantity):<\/p>\n<p>    if action == &#39;BUY&#39;:<\/p>\n<p>        exchange.create_market_buy_order(symbol, quantity)<\/p>\n<p>    elif action == &#39;SELL&#39;:<\/p>\n<p>        exchange.create_market_sell_order(symbol, quantity)<\/p>\n<p>def risk_management(exchange, symbol, balance):<\/p>\n<p>    max_position = balance * 0.1  # \u6bcf\u6b21\u4ea4\u6613\u4e0d\u8d85\u8fc7\u8d26\u6237\u4f59\u989d\u768410%<\/p>\n<p>    current_position = exchange.fetch_balance()[symbol.split(&#39;\/&#39;)[0]][&#39;free&#39;]<\/p>\n<p>    if current_position &gt; max_position:<\/p>\n<p>        return &#39;SELL&#39;, current_position - max_position<\/p>\n<p>    else:<\/p>\n<p>        return &#39;HOLD&#39;, 0<\/p>\n<p>exchange = ccxt.binance()<\/p>\n<p>symbol = &#39;BTC\/USDT&#39;<\/p>\n<p>while True:<\/p>\n<p>    try:<\/p>\n<p>        ohlcv = exchange.fetch_ohlcv(symbol, timeframe=&#39;1m&#39;, limit=200)<\/p>\n<p>        prices = [x[4] for x in ohlcv]<\/p>\n<p>        action = trading_strategy(prices)<\/p>\n<p>        print(f&quot;\u5f53\u524d\u884c\u52a8: {action}&quot;)<\/p>\n<p>        balance = exchange.fetch_balance()[&#39;total&#39;][&#39;USDT&#39;]<\/p>\n<p>        risk_action, risk_quantity = risk_management(exchange, symbol, balance)<\/p>\n<p>        if risk_action == &#39;SELL&#39;:<\/p>\n<p>            place_order(exchange, symbol, &#39;SELL&#39;, risk_quantity)<\/p>\n<p>        elif action != &#39;HOLD&#39;:<\/p>\n<p>            place_order(exchange, symbol, action, 0.01)<\/p>\n<p>        # \u7b49\u5f85\u4e00\u5206\u949f<\/p>\n<p>        time.sleep(60)<\/p>\n<p>    except Exception as e:<\/p>\n<p>        print(f&quot;\u9519\u8bef: {e}&quot;)<\/p>\n<p>        time.sleep(60)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e03\u3001\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>\u5c06Python\u63a5\u5165\u4ea4\u6613\u6240\u5e76\u8fdb\u884c\u81ea\u52a8\u5316\u4ea4\u6613\u9700\u8981\u4e86\u89e3\u4ea4\u6613\u6240API\u3001\u9009\u62e9\u5408\u9002\u7684\u5e93\u6216\u6846\u67b6\u3001\u5904\u7406API\u8bf7\u6c42\u548c\u54cd\u5e94\u3001\u8fdb\u884c\u8eab\u4efd\u9a8c\u8bc1\u548c\u5b89\u5168\u6027\u8bbe\u7f6e\u3001\u5904\u7406\u9519\u8bef\u548c\u5f02\u5e38\u3001\u8bbe\u8ba1\u4ea4\u6613\u7b56\u7565\u548c\u8fdb\u884c\u98ce\u9669\u7ba1\u7406\u3002\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u60a8\u53ef\u4ee5\u5728Python\u4e2d\u5b9e\u73b0\u4e0e\u4ea4\u6613\u6240\u7684\u4ea4\u4e92\uff0c\u5e76\u6839\u636e\u81ea\u5df1\u7684\u9700\u6c42\u8fdb\u884c\u81ea\u52a8\u5316\u4ea4\u6613\u3002<\/p>\n<\/p>\n<p><p><strong>\u91cd\u8981\u63d0\u793a\uff1a\u81ea\u52a8\u5316\u4ea4\u6613\u5177\u6709\u4e00\u5b9a\u7684\u98ce\u9669\uff0c\u8bf7\u786e\u4fdd\u5728\u8fdb\u884c\u81ea\u52a8\u5316\u4ea4\u6613\u524d\u5145\u5206\u4e86\u89e3\u76f8\u5173\u98ce\u9669\uff0c\u5e76\u8fdb\u884c\u5145\u5206\u6d4b\u8bd5\u548c\u98ce\u9669\u7ba1\u7406\u3002<\/strong><\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u9009\u62e9\u5408\u9002\u7684\u4ea4\u6613\u6240\u8fdb\u884cPython\u63a5\u5165\uff1f<\/strong><br \/>\u9009\u62e9\u5408\u9002\u7684\u4ea4\u6613\u6240\u662f\u63a5\u5165\u7684\u7b2c\u4e00\u6b65\u3002\u8bb8\u591a\u4ea4\u6613\u6240\u63d0\u4f9bAPI\u63a5\u53e3\uff0c\u5141\u8bb8\u7528\u6237\u901a\u8fc7Python\u8fdb\u884c\u4ea4\u6613\u548c\u6570\u636e\u83b7\u53d6\u3002\u5efa\u8bae\u4f18\u5148\u8003\u8651\u77e5\u540d\u5ea6\u9ad8\u3001\u7528\u6237\u8bc4\u4ef7\u597d\u7684\u4ea4\u6613\u6240\uff0c\u5982Binance\u3001Coinbase\u6216Kraken\u3002\u4e86\u89e3\u5404\u4e2a\u4ea4\u6613\u6240\u7684API\u6587\u6863\u3001\u652f\u6301\u7684\u4ea4\u6613\u5bf9\u53ca\u8d39\u7528\u7ed3\u6784\uff0c\u53ef\u4ee5\u5e2e\u52a9\u60a8\u505a\u51fa\u66f4\u660e\u667a\u7684\u9009\u62e9\u3002<\/p>\n<p><strong>\u5728\u63a5\u5165\u4ea4\u6613\u6240\u65f6\uff0c\u5982\u4f55\u4fdd\u8bc1\u6570\u636e\u7684\u5b89\u5168\u6027\uff1f<\/strong><br \/>\u6570\u636e\u5b89\u5168\u6027\u662f\u63a5\u5165\u4ea4\u6613\u6240\u65f6\u5fc5\u987b\u91cd\u89c6\u7684\u65b9\u9762\u3002\u4f7f\u7528API\u5bc6\u94a5\u65f6\uff0c\u52a1\u5fc5\u5c06\u5176\u4fdd\u5b58\u5728\u5b89\u5168\u7684\u73af\u5883\u4e2d\uff0c\u907f\u514d\u786c\u7f16\u7801\u5728\u4ee3\u7801\u4e2d\u3002\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u73af\u5883\u53d8\u91cf\u6216\u5b89\u5168\u5b58\u50a8\u65b9\u6848\u3002\u540c\u65f6\uff0c\u4e86\u89e3\u5e76\u4f7f\u7528HTTPS\u52a0\u5bc6\u4f20\u8f93\u6570\u636e\uff0c\u5b9a\u671f\u68c0\u67e5\u4ea4\u6613\u6240\u7684\u5b89\u5168\u516c\u544a\uff0c\u786e\u4fdd\u60a8\u7684\u8d26\u6237\u5b89\u5168\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u8fdb\u884c\u4ea4\u6613\u65f6\uff0c\u5982\u4f55\u5904\u7406\u5f02\u5e38\u548c\u9519\u8bef\uff1f<\/strong><br \/>\u5728\u8fdb\u884c\u4ea4\u6613\u65f6\uff0c\u5f02\u5e38\u548c\u9519\u8bef\u5904\u7406\u975e\u5e38\u91cd\u8981\u3002\u5efa\u8bae\u4f7f\u7528try-except\u5757\u6765\u6355\u83b7\u53ef\u80fd\u51fa\u73b0\u7684\u7f51\u7edc\u9519\u8bef\u3001API\u8c03\u7528\u9519\u8bef\u7b49\u3002\u6b64\u5916\uff0c\u8bbe\u7f6e\u5408\u7406\u7684\u91cd\u8bd5\u673a\u5236\uff0c\u5982\u5728\u8bf7\u6c42\u5931\u8d25\u65f6\u81ea\u52a8\u91cd\u8bd5\uff0c\u5e76\u8bb0\u5f55\u9519\u8bef\u65e5\u5fd7\uff0c\u80fd\u591f\u5e2e\u52a9\u60a8\u53ca\u65f6\u53d1\u73b0\u548c\u89e3\u51b3\u95ee\u9898\uff0c\u786e\u4fdd\u4ea4\u6613\u7684\u987a\u5229\u8fdb\u884c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5c06Python\u63a5\u5165\u4ea4\u6613\u6240\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u4ea4\u6613\u6240\u7684API\uff08\u5e94\u7528\u7a0b\u5e8f\u63a5\u53e3\uff09\u3001\u9009\u62e9\u5408\u9002\u7684\u5e93\u6216\u6846\u67b6\u3001\u5904\u7406API\u8bf7\u6c42\u548c\u54cd [&hellip;]","protected":false},"author":3,"featured_media":1051452,"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\/1051448"}],"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=1051448"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1051448\/revisions"}],"predecessor-version":[{"id":1051455,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1051448\/revisions\/1051455"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1051452"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1051448"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1051448"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1051448"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}