[Python] How to create a LIMIT Order for MEXC using python?

Discussão em 'Python' iniciado por Stack, Junho 18, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to create a limit order for MEXC. I defined two functions: one is used to generate a signature and the seconde one creates the limit order.

    Generate a signature:

    def generate_signature(params):

    # 1. Sort parameter alphabetically
    sorted_params = sorted(params.items())

    # 2. Change to Query-String (key1=value1&key2=value2)
    query_string = urlencode(sorted_params)

    # 3. Generate HMAC-SHA256 Signatur
    signature = hmac.new(
    self.api_secret.encode('utf-8'),
    query_string.encode('utf-8'),
    hashlib.sha256
    ).hexdigest()

    return signature


    Create a limit order:

    def create_limit_order():
    try:
    params = {
    "symbol": "BTCUSDT", # "BTCUSDT", String
    "side": "BUY", # "BUY" oder "SELL", String
    "type": "LIMIT", # String
    "price":limit_price, # float
    "quantity": quantity, # float
    "timestamp": int(time.time() * 1000), #float
    }

    signature = generate_signature(params)

    headers = {
    "X-MEXC-APIKEY": api_key,
    'Content-Type': 'application/json'
    }

    response = requests.post(
    "https://api.mexc.com/api/v3/order",
    params={**params, "signature": signature},
    headers=headers,
    timeout=10,
    )

    if response.status_code == 200:
    response = response.json()
    print(f"Order platziert! Order ID: {response['orderId']}")
    return order_data['orderId']
    else:
    print(f"Error {response.status_code}: {response.text}")
    return None

    except Exception as e:
    print(f"Order placement failed: {str(e)}")
    return None




    def main():
    create_limit_order()

    if __name__ == "__main__":
    main()


    In the main I am calling the function to create the limit order, but unfortunately I receive the following error:

    Status-Code: 400
    Antwort: {"code":700002,"msg":"Signature for this request is not valid."}


    What is the problem? Can anyone help?

    I tried to find the problem about the signature, but I cant find the root cause. I changed the data type for "price" and "quantity" to strings but the result remains the same.

    Continue reading...

Compartilhe esta Página