Bitget期货交易机器人下单失败:签名错误(code:40009)求助
Bitget期货交易机器人下单失败:签名错误(code:40009)求助
我刚琢磨出一个交易策略,现在正在开发一个自动交易机器人来简化期货下单操作。目前卡在期货下单模块,不管怎么调整代码,始终遇到同一个签名错误(我是借助ChatGPT来写代码的,毕竟不是全职开发者😅)。
错误信息
Error placing the order: {"code":"40009","msg":"sign signature error","requestTime":1738701156172,"data":null}
我的代码
import requests import time import hmac import hashlib import configparser import json # Lade die Konfiguration aus der config.ini Datei config = configparser.ConfigParser() config.read('config.ini') api_key = config['bitget']['api_key'] secret_key = config['bitget']['secret_key'] passphrase = config['bitget']['passphrase'] # Bitget API Endpoints BASE_URL = 'https://api.bitget.com' END_POINT = '/api/mix/v1/order/placeOrder' # Funktion zur Generierung der Signatur def generate_signature(secret_key, message): return hmac.new(secret_key.encode('utf-8'), message.encode('utf-8'), hashlib.sha256).hexdigest() # Funktion zur Erstellung der Headers def create_headers(api_key, secret_key, passphrase, request_path, body): timestamp = str(int(time.time() * 1000)) message = timestamp + request_path + json.dumps(body, separators=(',', ':')) # Wichtig: Keine Leerzeichen im JSON signature = generate_signature(secret_key, message) headers = { 'Content-Type': 'application/json', 'ACCESS-KEY': api_key, 'ACCESS-SIGN': signature, 'ACCESS-TIMESTAMP': timestamp, 'ACCESS-PASSPHRASE': passphrase } return headers # Funktion zum Kauf von Bitcoin im Futures-Markt def buy_bitcoin_futures(amount_usdt): request_path = END_POINT body = { "symbol": "BTCUSDT_UMCBL", "marginCoin": "USDT", "side": "open_long", "orderType": "market", "price": "0", "size": str(amount_usdt), "timeInForce": "normal" } headers = create_headers(api_key, secret_key, passphrase, request_path, body) response = requests.post(BASE_URL + request_path, headers=headers, json=body) if response.status_code == 200: print("Order erfolgreich platziert:", response.json()) return True # Erfolg else: print("Fehler beim Platzieren der Order:", response.text) return False # Fehler # Hauptfunktion if __name__ == "__main__": amount_usdt = 5 # 5 USDT max_retries = 10 # Maximale Anzahl von Versuchen retry_delay = 30 # Verzögerung zwischen den Versuchen in Sekunden for attempt in range(max_retries): print(f"Versuch {attempt + 1} von {max_retries}...") success = buy_bitcoin_futures(amount_usdt) if success: break # Erfolg, Schleife beenden else: print(f"Warte {retry_delay} Sekunden vor dem nächsten Versuch...") time.sleep(retry_delay) # Warte 30 Sekunden if not success: print("Maximale Anzahl von Versuchen erreicht. Order konnte nicht platziert werden.")
我的config.ini配置文件结构示例:
[bitget] api_key = 你的API密钥 secret_key = 你的密钥 passphrase = 你的密码短语
如果有人能帮我排查一下签名错误的原因,我会非常感激!
备注:内容来源于stack exchange,提问作者knolly
相关产品推荐
相关产品推荐

