For the past while I have been trying to get this crypto trading bot to work. The problem is that while I can get/list accounts when I place an order I get a 400 error.
The code:
def place_order(symbol, side, amount, price=None): url = 'https://api.coinbase.com/api/v3/brokerage/orders' headers = {'Authorization': 'Bearer '+encryption_build.build_jwt('POST api.coinbase.com/api/v3/brokerage/orders', api_key, api_secret),'Content-Type': 'application/json' } client_order_id = uuid.uuid4() try: # Order details order_data = {"client_order_id": str(client_order_id),"product_id": symbol,"side": side,"order_configuration": {"market_market_ioc": {"base_size": str(amount) } },"retail_portfolio_id": "41864a16-1fd4-50bb-a633-278d9c39f846" } res = requests.post(url=url, headers=headers, data=json.dumps(order_data)) except Exception as e: print(f"An error occurred: {e}")place_order('BTC-USD', 'SELL', amount=0.01)
The response:
{"error":"INVALID_ARGUMENT","error_details":"account is not available","message":"account is not available"}
The API key has view/trade permissions. The List Accounts endpoint returns my accounts on Coinbase. A BTC and fiat account in CAD are present. Both are active. Specifying the retail portfolio, or using other libraries to do this didn't make a difference.
I expect a 200 status code with the order pending or filled on Coinbase's end.
EDIT: Running it through Postman produced the same error.
POST https://api.coinbase.com/api/v3/brokerage/orders400111 msPOST /api/v3/brokerage/orders HTTP/1.1Content-Type: application/jsonAuthorization: Bearer eyJhbGciOiJFUzI1NiIsImtpZCI6Im9yZ2FuaXphdGlvbnMvNjUwYjZhYjEtMTk2My00ZTQ1LThhYWMtNmUyYTdkNGU0MmQxL2FwaUtleXMvOTg0ZTE4ZjMtZDEzZi00NjllLTk3ZDItNTNkZDEzYzM1NDJhIiwibm9uY2UiOiJlODE3MmZmZDZjMzQ3ZDdhNzRjZDk3YWFlNmJkZTEzZTYyZWY5ZTk3ZjEyZTBlOTU4NTgwNzMyMzUwZTBlMmFiIiwidHlwIjoiSldUIn0.eyJzdWIiOiJvcmdhbml6YXRpb25zLzY1MGI2YWIxLTE5NjMtNGU0NS04YWFjLTZlMmE3ZDRlNDJkMS9hcGlLZXlzLzk4NGUxOGYzLWQxM2YtNDY5ZS05N2QyLTUzZGQxM2MzNTQyYSIsImlzcyI6ImNkcCIsIm5iZiI6MTcyMDU2MTE2MSwiZXhwIjoxNzIwNTYxMjgxLCJ1cmkiOiJQT1NUIGFwaS5jb2luYmFzZS5jb20vYXBpL3YzL2Jyb2tlcmFnZS9vcmRlcnMifQ.0ECu5nM8XTTLv7caGPxSBmoVUDoqW-z07z4Jz8wDljjIf2rreGjZ-L-ZAzMcDMU8FafSEQeo81FExUkUe2TVwwUser-Agent: PostmanRuntime/7.39.0Accept: */*Postman-Token: 9506a059-e326-402b-8a4f-7aefcba39eafHost: api.coinbase.comAccept-Encoding: gzip, deflate, brConnection: keep-aliveContent-Length: 231Cookie: __cf_bm=wG1twa3RotRq8PB5plk5UPsC8Luskeyddh9famNqHE4-1720561189-1.0.1.1-lWThs07H99ZxAB78K2ZUdY.4aDP0LEg49rLxEqPyrbL2ivpjLLQFKbsSMcieMCZ.eVdyusgBCyVspRfrdP.5LQ; cb_dm=16c8bf71-6e14-4c79-8c05-83251661ce3b{"client_order_id": "b372a61a-7585-4dd1-bb6c-f5990967412b", "product_id": "BTC-USD", "side": "BUY", "order_configuration": {"market_market_ioc": {"base_size": "0.01"}}, "retail_portfolio_id": "d641518f-dd81-49fc-96d1-ce1e782f697f"}HTTP/1.1 400 Bad RequestDate: Tue, 09 Jul 2024 21:40:17 GMTContent-Type: application/json; charset=utf-8Content-Length: 108Connection: keep-aliveaccess-control-allow-headers: Content-Type, Accept, Second-Factor-Proof-Token, Client-Id, Access-Token, X-Cb-Project-Name, X-Cb-Is-Logged-In, X-Cb-Platform, X-Cb-Session-Uuid, X-Cb-Pagekey, X-Cb-Ujs, Fingerprint-Tokens, X-Cb-Device-Id, X-Cb-Version-Nameaccess-control-allow-methods: GET,POST,DELETE,PUTaccess-control-allow-private-network: trueaccess-control-expose-headers:access-control-max-age: 7200Cache-Control: no-storestrict-transport-security: max-age=31536000; includeSubDomains; preloadtrace-id: 7311873497407553797trace-id: 7311873497407553797vary: Originx-content-type-options: nosniffx-dns-prefetch-control: offx-download-options: noopenx-frame-options: SAMEORIGINx-xss-protection: 1; mode=blockx-envoy-upstream-service-time: 51CF-Cache-Status: DYNAMICServer: cloudflareCF-RAY: 8a0b78b7bf7caca0-YYZ{"error":"INVALID_ARGUMENT","error_details":"account is not available","message":"account is not available"}
There is very little on this on the internet. Any help would be appreciated. Thank you.