1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Python] How Place a Market order on MEXC API KEY?

Discussão em 'Python' iniciado por Stack, Setembro 14, 2024.

  1. Stack

    Stack Membro Participativo

    I am sending in just a take profit price variable. Do I need to send in Stop Loss as well?

    def post_place(symbol, price, vol, side, type, openType, leverage=None, positionId=None, externalOid=None,
    stopLossPrice=None, takeProfitPrice=None, positionMode=None, reduceOnlt=None):
    """Place new order"""
    method = 'POST'
    path = '/api/v1/private/order/submit'
    url = '{}{}'.format(BASE_URL, path)

    print(f"Preparing to place trade with the following details:\n"
    f"Symbol: {symbol}, Price: {price}, Volume: {vol}, Side: {side}, Type: {type}, OpenType: {openType}, "
    f"Leverage: {leverage}, PositionId: {positionId}, ExternalOid: {externalOid}, "
    f"TakeProfitPrice: {takeProfitPrice}, StopLossPrice: {stopLossPrice}, PositionMode: {positionMode}, ReduceOnly: {reduceOnlt}")

    data_original = {
    'symbol': symbol,
    'price': price,
    'vol': vol,
    'side': side,
    'type': type,
    'openType': openType
    }

    if leverage:
    data_original.update({"leverage": leverage})
    if positionId:
    data_original.update({"positionId": positionId})
    if externalOid:
    data_original.update({"externalOid": externalOid})
    if takeProfitPrice:
    data_original.update({"takeProfitPrice": takeProfitPrice})
    if positionMode:
    data_original.update({"positionMode": positionMode})
    if reduceOnlt:
    data_original.update({"reduceOnlt": reduceOnlt})

    data = json.dumps(data_original)
    print(f"Data to be sent: {data}")

    params = _sign_v1(sign_params=data)
    headers = {
    "ApiKey": API_KEY,
    "Request-Time": str(_get_server_time()),
    "Signature": params,
    "Content-Type": "application/json"
    }
    print(f"Headers: {headers}")

    try:
    response = requests.request(method, url, data=data, headers=headers, timeout=10)
    print(f"Response received: Status Code: {response.status_code}, Response Text: {response.text}")
    response_json = response.json()
    print(f"Response JSON: {response_json}")
    return response_json
    except requests.exceptions.ConnectionError as e:
    print(f"Connection error occurred: {str(e)}")
    return {"error": "connection_error"}
    except requests.exceptions.Timeout:
    print("Error: The request timed out.")
    return {"error": "timeout"}
    except requests.exceptions.RequestException as e:
    print(f"Error occurred during the API request: {str(e)}")
    return {"error": str(e)}


    Am trying to send an order to MEXC to place an order on MEXC. The code freezes upon sending to API and I am not sure where the problem is. I don't think the issue is with my code, am just not sure if am not inputting all the values I am supposed to such as the Stop Loss.

    Continue reading...

Compartilhe esta Página