@
ccclo #7 把****替换成四空格,或者让 chatgpt 帮你改。
import time
import requests
import hmac
import hashlib
from urllib.parse import urljoin, urlencode
def market_order(symbol, quantity, side, reduceOnly='false', loop=True):
****API_KEY = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' #替换成自己的 API_KEY
****SECRET_KEY = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' #替换成自己的 SECRET_KEY
****BASE_URL = '
https://fapi.binance.com'
****headers = {'X-MBX-APIKEY': API_KEY}
****PATH = '/fapi/v1/order'
****url = urljoin(BASE_URL, PATH)
****params = {'symbol': symbol,
************ 'quantity': quantity,
************ 'side': side,
************ 'type': 'MARKET',
************ 'reduceOnly': reduceOnly}
****while True:
********try:
************if 'signature' in params:
****************del params['signature']
************params['timestamp'] = int(time.time()) * 1000
************query_string = urlencode(params)
************params['signature'] = hmac.new(SECRET_KEY.encode('utf-8'), query_string.encode('utf-8'), hashlib.sha256).hexdigest()
************r =
requests.post(url, headers=headers, params=params, timeout=4)
************# print(r.text) # 查看响应文本
************if r.status_code == 200:
****************r = r.json()
****************return r['orderId']
************if not loop:
****************return None
********except BaseException as e:
************print(e)
************if not loop:
****************return None
********time.sleep(0.1)
symbol = 'BNBUSDT' # 交易对
quantity = 0.03 # 下单的数量
side = 'BUY' # 下单的方向,BUY 或者 SELL
order = market_order(symbol, quantity, side)