Reputation: 1
I just worked out a trading strategy and am now on coding a bot to execute the trades easier.
I am working on the futures order module right now, and I always get the same error, no matter what I do. (Yes i am coding with chatgpt because im not a fulltime coder) Here's the error:
Error placing the order: {"code":"40009","msg":"sign signature error","requestTime":1738701156172,"data":null}
Hier ist mein code:
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.")
If someone could help me with this I would be very grateful
Upvotes: 0
Views: 78
Reputation: 161
You have "sign signature error".
I suggest to use official bitget python library
https://github.com/BitgetLimited/v3-bitget-api-sdk
I used following code to place spot orders:
import bitget.bitget_api as baseApi
from bitget.exceptions import BitgetAPIException
apiKey = "your_key"
secretKey = "your_secret"
passphrase = "your_phrase"
sell_order = {
"symbol": "BTCUSDT", # Spot trading pair
"side": "sell", # "buy" or "sell"
"orderType": "market", # Market order
"size": "0.005", # Order size in BTC
"force": "fok" # "fok" (Fill Or Kill) for market orders
}
buy_order = {
"symbol": "BTCUSDT", # Spot trading pair
"side": "buy", # "buy" or "sell"
"orderType": "limit", # Market order
"size": "0.005", # Order size in BTC
"force": "gtc", # gtc: Normal limit order, good till cancelled
"price": "96600"
}
#### main ####
baseApi = baseApi.BitgetApi(apiKey, secretKey, passphrase,first=True)
try:
response = baseApi.post("/api/v2/spot/trade/place-order",sell_order)
print(response)
response = baseApi.post("/api/v2/spot/trade/place-order",buy_order)
print(response)
except BitgetAPIException as e:
print("error:" + e.message)
If you want to use demo account with virtual money, then modify file utils.py adding header
header['PAPTRADING'] = '1'
Upvotes: 0