{"id":151,"date":"2025-05-31T17:54:47","date_gmt":"2025-06-04T10:10:47","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=151"},"modified":"2025-06-04T17:54:47","modified_gmt":"2025-06-04T10:10:47","slug":"python-binance%e5%b8%81%e5%ae%89%e5%88%a9%e7%94%a8ccxt%e5%ae%8c%e6%88%90%e8%99%9b%e6%93%ac%e8%b2%a8%e5%b9%a3%e8%87%aa%e5%8b%95%e5%8c%96%e4%ba%a4%e6%98%93","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/python\/python-binance%e5%b8%81%e5%ae%89%e5%88%a9%e7%94%a8ccxt%e5%ae%8c%e6%88%90%e8%99%9b%e6%93%ac%e8%b2%a8%e5%b9%a3%e8%87%aa%e5%8b%95%e5%8c%96%e4%ba%a4%e6%98%93\/","title":{"rendered":"\u4f7f\u7528 Python CCXT \u5957\u4ef6\u5be6\u73fe Binance \u81ea\u52d5\u5316\u4ea4\u6613\uff1a2025 \u6700\u65b0\u6559\u5b78"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2020\/07\/ea8e3c00-171a-11ea-9a81-e79b426fe455.png\" alt=\"Python CCXT \u5957\u4ef6\u793a\u7bc4\u5716\" title=\"\"><\/p>\n<blockquote><p>\n  &#8212;2025\/06\/01 \u66f4\u65b0&#8212;\n<\/p><\/blockquote>\n<p>## Python CCXT \u5957\u4ef6\u4ecb\u7d39<\/p>\n<p>\u5728\u9019\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u5011\u5c07\u63a2\u8a0e\u5982\u4f55\u4f7f\u7528 CCXT \u5957\u4ef6\u4f86\u5be6\u73fe\u81ea\u52d5\u5316\u865b\u64ec\u8ca8\u5e63\u4ea4\u6613\u3002CCXT \u662f\u4e00\u500b\u7528\u65bc\u52a0\u5bc6\u4ea4\u6613\u7684 Python \u5957\u4ef6\uff0c\u652f\u6301\u6578\u767e\u5bb6\u52a0\u5bc6\u4ea4\u6613\u6240\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u5011\u5c07\u4f7f\u7528 CCXT \u5957\u4ef6\u4f86\u5be6\u73fe\u81ea\u52d5\u5316\u4ea4\u6613\uff0c\u4e26\u4f7f\u7528 Binance\uff08\u5e01\u5b89\uff09\u4f5c\u70ba\u793a\u7bc4\u4ea4\u6613\u6240\u3002<\/p>\n<p>## Step 1: \u5b89\u88dd CCXT \u5957\u4ef6<\/p>\n<p>\u5728\u958b\u59cb\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u5b89\u88dd CCXT \u5957\u4ef6\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u4f86\u5b89\u88dd\uff1a<\/p>\n<p>&#8220;`bash<br \/>\npip3 install ccxt<br \/>\n&#8220;`<\/p>\n<p>CCXT \u7684 GitHub \u7db2\u5740\uff1a[https:\/\/pypi.org\/project\/ccxt\/](https:\/\/pypi.org\/project\/ccxt\/)<\/p>\n<p><a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2020\/07\/wp_editor_md_2a384a443180c3bc161a58cb874cf542.jpg\"><img decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2020\/07\/wp_editor_md_2a384a443180c3bc161a58cb874cf542.jpg\" alt=\"CCXT \u5b89\u88dd\u793a\u7bc4\u5716\" title=\"\"><\/a><\/p>\n<p>## Step 2: \u9023\u63a5 Binance API<\/p>\n<p>\u5728\u958b\u59cb\u4ea4\u6613\u4e4b\u524d\uff0c\u9700\u8981\u4f7f\u7528 Binance API \u9023\u63a5\u5230 Binance \u4ea4\u6613\u6240\u3002\u8981\u9023\u63a5\u5230 Binance API\uff0c\u9700\u8981\u5728 Binance \u8a3b\u518a\u4e00\u500b\u5e33\u6236\uff0c\u7136\u5f8c\u751f\u6210 API \u5bc6\u9470\u3002\u5728\u751f\u6210 API \u5bc6\u9470\u5f8c\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u78bc\u9023\u63a5\u5230 Binance API\uff1a<\/p>\n<p>### \u9023\u63a5\u65b9\u6cd5 1<\/p>\n<p>&#8220;`python<br \/>\nimport ccxt<\/p>\n<p>exchange = ccxt.binance({<br \/>\n    &#8216;apiKey&#8217;: &#8216;YOUR_API_KEY&#8217;,<br \/>\n    &#8216;secret&#8217;: &#8216;YOUR_SECRET_KEY&#8217;,<br \/>\n})<br \/>\n&#8220;`<\/p>\n<p>### \u9023\u63a5\u65b9\u6cd5 2<\/p>\n<p>&#8220;`python<br \/>\nimport ccxt<\/p>\n<p>exchange = ccxt.binance()<br \/>\nexchange.apiKey = &#8216;YOUR_API_KEY&#8217;<br \/>\nexchange.secret = &#8216;YOUR_SECRET_KEY&#8217;<br \/>\n&#8220;`<\/p>\n<p>## Step 3: \u67e5\u8a62\u5e02\u5834\u6578\u64da<\/p>\n<p>\u5728\u958b\u59cb\u4ea4\u6613\u4e4b\u524d\uff0c\u9700\u8981\u67e5\u8a62\u5e02\u5834\u6578\u64da\uff0c\u4ee5\u4e86\u89e3\u76ee\u524d\u5e02\u5834\u7684\u72c0\u6cc1\u548c\u50f9\u683c\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u78bc\u67e5\u8a62\u5e02\u5834\u6578\u64da\uff1a<\/p>\n<p>&#8220;`python<br \/>\nmarkets = exchange.load_markets()<\/p>\n<p>for market in markets:<br \/>\n    print(market)<br \/>\n&#8220;`<\/p>\n<p>\u9019\u6bb5\u4ee3\u78bc\u6703\u8f38\u51fa\u6240\u6709\u53ef\u7528\u7684\u5e02\u5834\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u5e02\u5834\u7684\u7b26\u865f\uff08symbol\uff09\u4f86\u6307\u5b9a\u8981\u4ea4\u6613\u7684\u8ca8\u5e63\u5c0d\u3002\u4f8b\u5982\uff1a<\/p>\n<p>&#8220;`json<br \/>\n{<br \/>\n    &#8220;id&#8221;: &#8220;btcusd&#8221;,<br \/>\n    &#8220;symbol&#8221;: &#8220;BTC\/USD&#8221;,<br \/>\n    &#8220;base&#8221;: &#8220;BTC&#8221;,<br \/>\n    &#8220;quote&#8221;: &#8220;USD&#8221;,<br \/>\n    &#8230;<br \/>\n}<br \/>\n&#8220;`<\/p>\n<p>## Step 4: \u8cec\u6236\u8cc7\u7522\u67e5\u8a62<\/p>\n<p>\u4f7f\u7528 CCXT \u9032\u884c\u865b\u64ec\u8ca8\u5e63\u4ea4\u6613\u6642\uff0c\u78ba\u8a8d\u5728\u4ea4\u6613\u6240\u7684\u5e33\u6236\u4e2d\u64c1\u6709\u591a\u5c11\u8cc7\u91d1\u548c\u8cc7\u7522\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u9019\u53ef\u4ee5\u901a\u904e\u8abf\u7528 `fetch_balance()` \u65b9\u6cd5\u4f86\u5b8c\u6210\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u793a\u4f8b\u4ee3\u78bc\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528 `fetch_balance()` \u65b9\u6cd5\u4f86\u7372\u53d6\u8cc7\u7522\u548c\u8cc7\u91d1\uff1a<\/p>\n<p>&#8220;`python<br \/>\nbalance = exchange.fetch_balance()<br \/>\nprint(balance)  # \u7372\u5f97\u6240\u6709\u9322\u5305\u8cc7\u8a0a<br \/>\nprint(balance[&#8216;free&#8217;])  # \u53ef\u7528\u7684\u8cc7\u7522\u6578\u91cf<br \/>\nprint(balance[&#8216;used&#8217;])  # \u5df2\u7d93\u4f7f\u7528\u7684\u8cc7\u7522\u6578\u91cf<br \/>\nprint(balance[&#8216;total&#8217;])  # \u7e3d\u8cc7\u7522\u6578\u91cf<br \/>\n&#8220;`<\/p>\n<p>### \u6253\u5370 USDT \u8cc7\u7522<\/p>\n<p>&#8220;`python<br \/>\nprint(balance[&#8216;USDT&#8217;])  # USDT\u9019\u500b\u8cc7\u7522\u7684\u6578\u91cf<br \/>\n&#8220;`<\/p>\n<p>## Step 5: \u81ea\u52d5\u5316\u4ea4\u6613<\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u81ea\u52d5\u5316\u4ea4\u6613\u7b56\u7565\u7684\u4ee3\u78bc\uff0cLEND\/USDT\u5e02\u5834\u4e0a\u9032\u884c\u7c21\u55ae\u7684\u4ea4\u6613\uff1a<\/p>\n<p>&#8220;`python<br \/>\nsymbol = &#8216;LEND\/USDT&#8217;<br \/>\namount = balance[&#8216;LEND&#8217;][&#8216;total&#8217;]  # \u8ce3\u51fa\u6240\u6709 LEND<\/p>\n<p># \u5e02\u50f9\u55ae\uff0c\u5e02\u50f9\u55ae\u4e0d\u9700\u8981\u8f38\u5165\u50f9\u683c<br \/>\norder_info = exchange.create_market_buy_order(symbol=symbol, amount=amount)  # \u8cb7\u55ae<br \/>\norder_info = exchange.create_market_sell_order(symbol=symbol, amount=amount)  # \u8ce3\u55ae<br \/>\n&#8220;`<\/p>\n<p>\u53e6\u4e00\u500b\u7c21\u55ae\u7684\u81ea\u52d5\u5316\u4ea4\u6613\u7b56\u7565\u4ee3\u78bc\uff0c\u7528\u65bc\u5728 BTC\/USDT \u5e02\u5834\u4e0a\u9032\u884c\u7c21\u55ae\u7684\u4ea4\u6613\uff1a<\/p>\n<p>&#8220;`python<br \/>\nimport time<\/p>\n<p>while True:<br \/>\n    ticker = exchange.fetch_ticker(&#8216;BTC\/USDT&#8217;)<br \/>\n    balance = exchange.fetch_balance()<\/p>\n<p>    # \u5982\u679c BTC \u7684\u50f9\u683c\u5c0f\u65bc 41000 USDT \u4e26\u4e14\u8cec\u6236\u4e2d\u6709\u8db3\u5920\u7684 USDT<br \/>\n    if ticker[&#8216;last&#8217;] < 41000 and balance['USDT']['free'] > 100:<br \/>\n        order = exchange.create_order(symbol=&#8217;BTC\/USDT&#8217;, type=&#8217;limit&#8217;, side=&#8217;buy&#8217;, amount=0.001, price=40000)<br \/>\n        print(order)<\/p>\n<p>    # \u5982\u679c BTC \u7684\u50f9\u683c\u5927\u65bc 45000 USDT \u4e26\u4e14\u8cec\u6236\u4e2d\u6709\u8db3\u5920\u7684 BTC<br \/>\n    elif ticker[&#8216;last&#8217;] > 45000 and balance[&#8216;BTC&#8217;][&#8216;free&#8217;] > 0.001:<br \/>\n        order = exchange.create_order(symbol=&#8217;BTC\/USDT&#8217;, type=&#8217;limit&#8217;, side=&#8217;sell&#8217;, amount=0.001, price=46000)<br \/>\n        print(order)<\/p>\n<p>    time.sleep(60)<br \/>\n&#8220;`<\/p>\n<p>## \u5b8c\u6574\u7bc4\u4f8b<\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u4f7f\u7528 CCXT Python \u5eab\u9032\u884c\u865b\u64ec\u8ca8\u5e63\u4ea4\u6613\u7684\u5b8c\u6574\u7bc4\u4f8b\uff0c\u9019\u500b\u7bc4\u4f8b\u53ef\u4ee5\u5be6\u73fe\u4ee5\u4e0b\u4e09\u500b\u6b65\u9a5f\uff1a<\/p>\n<p>1. \u67e5\u8a62\u53ef\u4ea4\u6613\u7684\u5e63\u7a2e\u3002<br \/>\n2. \u67e5\u8a62\u4f60\u5728\u4ea4\u6613\u6240\u4e2d\u7684\u8cc7\u6e90\u548c\u8cc7\u91d1\u3002<br \/>\n3. \u4e0b\u55ae\u8cfc\u8cb7 BTC\u3002<\/p>\n<p>&#8220;`python<br \/>\nimport ccxt<\/p>\n<p># \u521d\u59cb\u5316\u4ea4\u6613\u6240\u5c0d\u8c61<br \/>\nexchange = ccxt.binance({<br \/>\n    &#8216;apiKey&#8217;: &#8216;YOUR_API_KEY&#8217;,<br \/>\n    &#8216;secret&#8217;: &#8216;YOUR_SECRET_KEY&#8217;,<br \/>\n    &#8216;enableRateLimit&#8217;: True,<br \/>\n})<\/p>\n<p># \u67e5\u8a62\u53ef\u4ea4\u6613\u5e63\u7a2e<br \/>\nmarkets = exchange.load_markets()<br \/>\nprint(&#8216;\u53ef\u4ea4\u6613\u5e63\u7a2e\uff1a&#8217;)<br \/>\nfor market in markets:<br \/>\n    print(market)<\/p>\n<p># \u67e5\u8a62\u4f60\u5728\u4ea4\u6613\u6240\u4e2d\u7684\u8cc7\u6e90\u548c\u8cc7\u91d1<br \/>\nbalance = exchange.fetch_balance()<br \/>\nprint(&#8216;\u4f60\u7684\u8cc7\u6e90\u548c\u8cc7\u91d1\uff1a&#8217;)<br \/>\nfor key, value in balance[&#8216;total&#8217;].items():<br \/>\n    print(key, &#8216;:&#8217;, value)<\/p>\n<p># \u4e0b\u55ae\u8cfc\u8cb7 BTC<br \/>\nsymbol = &#8216;BTC\/USDT&#8217;  # \u4ea4\u6613\u5c0d<br \/>\namount = 0.001  # \u8cfc\u8cb7\u6578\u91cf<br \/>\nprice = exchange.fetch_ticker(symbol)[&#8216;ask&#8217;]  # \u8cfc\u8cb7\u50f9\u683c<br \/>\norder = exchange.create_order(symbol, &#8216;limit&#8217;, &#8216;buy&#8217;, amount, price)<br \/>\nprint(&#8216;\u4e0b\u55ae\u6210\u529f\uff1a&#8217;, order)<br \/>\n&#8220;`<\/p>\n<div style=\"font-family: 'times new roman'; text-align: left;\">\n<span style=\"font-size: x-large;\"><br \/>\n<b style=\"background-color: white;\">Python \u66f4\u591a\u6587\u7ae0<\/b><br \/>\n<\/span>\n<\/div>\n<p><span style=\"font-family: times new roman; font-size: large;\"><br \/>\n<a href=\"https:\/\/badgameshow.com\/steven\/python\/python-\u7b46\u8a18\" target=\"_blank\" rel=\"noopener\">Python \u6aa2\u67e5 dict \u662f\u5426\u6709\u9019\u500b key<\/a><br \/>\n<\/span><\/p>\n<hr \/>\n<p>## Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/p>\n<p>### Q1: CCXT \u5957\u4ef6\u652f\u63f4\u54ea\u4e9b\u4ea4\u6613\u6240\uff1f<br \/>\nA1: CCXT \u5957\u4ef6\u652f\u63f4\u6578\u767e\u5bb6\u52a0\u5bc6\u8ca8\u5e63\u4ea4\u6613\u6240\uff0c\u5305\u62ec Binance\u3001Coinbase\u3001Kraken \u7b49\u7b49\u3002\u4f60\u53ef\u4ee5\u5728\u5b98\u65b9\u6587\u6a94\u4e2d\u67e5\u770b\u5b8c\u6574\u5217\u8868\u3002<\/p>\n<p>### Q2: \u5982\u4f55\u7372\u5f97 Binance \u7684 API \u5bc6\u9470\uff1f<br \/>\nA2: \u4f60\u9700\u8981\u5728 Binance \u8a3b\u518a\u4e00\u500b\u5e33\u6236\uff0c\u7136\u5f8c\u5728\u7528\u6236\u4e2d\u5fc3\u751f\u6210 API \u5bc6\u9470\u3002\u78ba\u4fdd\u59a5\u5584\u4fdd\u7ba1\u9019\u4e9b\u5bc6\u9470\uff0c\u4ee5\u514d\u6d29\u6f0f\u3002<\/p>\n<p>### Q3: \u5982\u4f55\u8655\u7406\u4ea4\u6613\u932f\u8aa4\uff1f<br \/>\nA3: \u5728\u9032\u884c\u4ea4\u6613\u6642\uff0c\u5efa\u8b70\u4f7f\u7528 try-except \u8a9e\u53e5\u4f86\u6355\u7372\u53ef\u80fd\u51fa\u73fe\u7684\u932f\u8aa4\uff0c\u4e26\u9032\u884c\u9069\u7576\u7684\u932f\u8aa4\u8655\u7406\uff0c\u4f8b\u5982\u91cd\u8a66\u6a5f\u5236\u6216\u8a18\u9304\u932f\u8aa4\u4fe1\u606f\u3002<\/p>\n<p>&#8212; <\/p>\n<p>\u5e0c\u671b\u9019\u7bc7\u6587\u7ae0\u80fd\u5e6b\u52a9\u4f60\u66f4\u8f15\u9b06\u5730\u4f7f\u7528 Python CCXT \u5957\u4ef6\u9032\u884c\u865b\u64ec\u8ca8\u5e63\u4ea4\u6613\uff01\u5982\u679c\u4f60\u6709\u66f4\u591a\u554f\u984c\uff0c\u96a8\u6642\u53ef\u4ee5\u53c3\u8003 [\u9019\u7bc7\u6559\u5b78](https:\/\/vocus.cc\/article\/642d9b47fd897300019e78cc) \u7372\u53d6\u66f4\u591a\u8cc7\u6e90\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b89\u88ddccxt\u81f3python3<br \/>\n\u5275\u5efa\u5e63\u5b89\u4ea4\u6613\u6240\u9023\u7d50<br \/>\n\u5169\u7a2e\u65b9\u6cd5<br \/>\napiKey\u8ddfsecret\u79c1\u9470\u8acb\u81f3\u5e63\u5b89\u7533\u8acb<\/p>\n","protected":false},"author":1,"featured_media":7791,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[18],"tags":[26,25,15,27,28],"class_list":["post-151","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-binance","tag-ccxt","tag-python","tag-27","tag-28"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2020\/07\/ea8e3c00-171a-11ea-9a81-e79b426fe455.png","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-2r","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/151","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=151"}],"version-history":[{"count":13,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/151\/revisions"}],"predecessor-version":[{"id":13081,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/151\/revisions\/13081"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/7791"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=151"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=151"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=151"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}