timkpaine / tdameritrade

Python interface to TD Ameritrade (https://developer.tdameritrade.com)
Apache License 2.0
520 stars 202 forks source link

Cannot Place the Order, Error 415 #134

Closed qililiu closed 2 years ago

qililiu commented 3 years ago

Describe the bug The order cannot be placed using TDClinent.placeOrder(accountId, order) command.

To Reproduce Steps to reproduce the behavior:

  1. Create TDClient using TD America account: TDClient = td.TDClient(client_id=client_id, refresh_token=refresh_token)
  2. Create order JSON as below:

order_json = {"orderType": "MARKET", "session": "NORMAL", "duration": "DAY", "orderStrategyType": "SINGLE", "orderLegCollection": [ { "instruction": "Buy", "quantity": quantity, "instrument": { "symbol": symbol, "assetType": "EQUITY" } } ] }

  1. Place an BUY order: TDClient.placeOrder(int(accountId), order_json)

Expected behavior It was expected to place an order successfully. But an error of 415 was returned.

Screenshots Please see attached. Error 415

Desktop (please complete the following information):

Additional context Add any other context about the problem here.

bob-mcrae commented 3 years ago

I had the same issue, but found the culprit is that the request header is missing "Content-Type: application/json". While typing to figure out the best way to add this header to the placeOrder method, I saw that python request also accepts a json parameter. So, the solution is quite simple: replace the parameter order with json in the placeOrder method (client.py, line 490); like so: return self._request(PLACE_ORDER.format(accountId=accountId), method='POST', json=order)

edit#2: I also removed the .json() at the end, as that was causing issues.

Also, I have read from folks that the order number is not returned, but it actually is; like so:

order_json  = json.loads('{"orderTy..."}')
resp = placeOrder(accountId, order_json)
order_number = resp.headers['Location'].split('/')[-1]
qililiu commented 3 years ago

I had the same issue, but found the culprit is that the request header is missing "Content-Type: application/json". While typing to figure out the best way to add this header to the placeOrder method, I saw that python request also accepts a json parameter. So, the solution is quite simple: replace the parameter order with json in the placeOrder method (client.py, line 490); like so: return self._request(PLACE_ORDER.format(accountId=accountId), method='POST', json=order)

edit#2: I also removed the .json() at the end, as that was causing issues.

Also, I have read from folks that the order number is not returned, but it actually is; like so:

order_json  = json.loads('{"orderTy..."}')
resp = placeOrder(accountId, order_json)
order_number = resp.headers['Location'].split('/')[-1]

Thanks a lot, my friend. Can you/author update the Github repository?

bob-mcrae commented 3 years ago

Hi Tim,

I considered doing that, but -- while I have a fair amount of programming experience -- I have never gone through the process of a pull request on github. Also, given I just started digging into the library's architecture, I was a bit reluctant to propose a change; I'd want to be sure my proposed change was consistent with the overall library's style and techniques. Lastly, as I started to develop the functionality I need, I found that I was having to modify more of the library and was considering just writing my own (thin) client. That said, I would welcome the opportunity to collaborate on the development of your library if it could be truly collaborative. That is, if you could help me understand the architecture and bit more AND give serious consideration to my proposed changes, then I'd be happy to engage on this project.

Thoughts?

On Sat, Feb 20, 2021 at 9:06 AM qililiu notifications@github.com wrote:

I had the same issue, but found the culprit is that the request header is missing "Content-Type: application/json". While typing to figure out the best way to add this header to the placeOrder method, I saw that python request also accepts a json parameter. So, the solution is quite simple: replace the parameter order with json in the placeOrder method (client.py, line 490); like so: return self._request(PLACE_ORDER.format(accountId=accountId), method='POST', json=order)

edit#2: I also removed the .json() at the end, as that was causing issues.

Also, I have read from folks that the order number is not returned, but it actually is; like so:

order_json = json.loads('{"orderTy..."}') resp = placeOrder(accountId, order_json) order_number = resp.headers['Location'].split('/')[-1]

Thanks a lot, my friend. Can you/author update the Github repository?

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/timkpaine/tdameritrade/issues/134#issuecomment-782706229, or unsubscribe https://github.com/notifications/unsubscribe-auth/AR6XMBXH6HZR6B7AGDDWLYDS77MWTANCNFSM4XYRTJAQ .

timkpaine commented 3 years ago

PRs are welcome, this library doesn't do anything complicated and if you run into issues I can help.