Value_Investor
Value_Investor

Reputation: 175

Data sent with the requests.delete method is disregarded

While trying to connect to an API from Alpaca (a broker that accepts automated orders), I found that I was unable to send data with the requests.delete method. Here is some code:

def close_position(symbol, percentage, api_key=api_key, secret_key=secret_key, base_url=base_url):
    data = {"percentage": percentage}
    headers = {
        "APCA-API-KEY-ID": api_key,
        "APCA-API-SECRET-KEY": secret_key
    }
    url = f"{base_url}/v2/positions/{symbol}"
    order = requests.delete(url, json=data, headers=headers)
    return order.json()

This code is supposed to close (i.e., liquidate) a specified percentage of a position. However, it seems that the data sent using the delete method is disregarded; this function always closes my entire position instead of the specified percentage. Here is the documentation for the Alpaca API: https://alpaca.markets/docs/api-references/trading-api/positions/#close-a-position

I have also tried the data and params parameter and json.dumps(data), but to no avail.

Any idea how to send data with the requests.delete method would be appreciated.

Upvotes: 0

Views: 26

Answers (0)

Related Questions