Unable to submit orders cancel orders on paper account through python script

Please can somebody help?
I am new to Alpaca. I am an individual investor in trading for stocks. I am trying to write a python script to submit orders, cancel orders - for personal consumption only.

I am using paper trading only at this point. My script written on a ubuntu virtual machine with Python 3.8 is able to login to my account an provide account details with the commands:

I have an open order. Through the script I want to cancel it and raise another order. However when I try to cancel it gives me error - as per Python traceback, indicating that an error occurred while using the Alpaca Trade API library to cancel an order

I am using api.list_orders() to get orders and then using client_order_id to cancel order using api.cancel_order(order.client_order_id). However I am getting the following error messages. What am I missing? Do I need any special permission? How do I check?

Traceback (most recent call last):
File “/home/vmadmin/.local/lib/python3.8/site-packages/alpaca_trade_api/rest.py”, line 234, in _one_request
resp.raise_for_status()
File “/home/vmadmin/.local/lib/python3.8/site-packages/requests/models.py”, line 1021, in raise_for_status
raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 404 Client Error: Not Found for url: https://paper-api.alpaca.markets/v2/orders/<Client_Order_id>

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File “alpacaapi.py”, line 74, in
api.cancel_order(order.client_order_id)
File “/home/vmadmin/.local/lib/python3.8/site-packages/alpaca_trade_api/rest.py”, line 488, in cancel_order
self.delete(‘/orders/{}’.format(order_id))
File “/home/vmadmin/.local/lib/python3.8/site-packages/alpaca_trade_api/rest.py”, line 262, in delete
return self._request(‘DELETE’, path, data)
File “/home/vmadmin/.local/lib/python3.8/site-packages/alpaca_trade_api/rest.py”, line 213, in _request
return self._one_request(method, url, opts, retry)
File “/home/vmadmin/.local/lib/python3.8/site-packages/alpaca_trade_api/rest.py”, line 242, in _one_request
raise APIError(error, http_error)
alpaca_trade_api.rest.APIError: order not found

1 Like