tastyware / tastytrade

An unofficial, sync/async Python SDK for Tastytrade!
https://tastyworks-api.rtfd.io
MIT License
126 stars 43 forks source link

KeyError: 'data' when placing limit order #136

Closed zwocram closed 8 months ago

zwocram commented 8 months ago

Hi, using the example from https://tastyworks-api.readthedocs.io/en/latest/orders.html I get the following error:

File ".........../venv/lib/python3.11/site-packages/tastytrade/account.py", line 1032, in place_order data = response.json()['data']


KeyError: 'data'

This is my code. I use a sandbox.
```
       account = Account.get_account(session, account_number)

        symbol = Equity.get_equity(session, 'USO')
        leg = symbol.build_leg(Decimal('5'), OrderAction.BUY_TO_OPEN)  # buy to open 5 shares

        order = NewOrder(
            time_in_force=OrderTimeInForce.DAY,
            order_type=OrderType.LIMIT,
            legs=[leg],  # you can have multiple legs in an order
            price=Decimal('10'),  # limit price, $10/share for a total value of $50
            price_effect=PriceEffect.DEBIT
        )

        response = account.place_order(session, order, dry_run=True)

 ```
Am I doing something wrong?
thanks!
regards
Graeme22 commented 8 months ago

Hello! This is working for me with a certification session. Could you provide more data please?

zwocram commented 8 months ago

Hi, you mean the traceback maybe? I copied most of the code from readthedocs so my code is practically the same. THis is the code:

from tastytrade import CertificationSession
session = CertificationSession(username, password)

account = Account.get_account(session, account_number)
symbol = Equity.get_equity(session, 'USO')
leg = symbol.build_leg(Decimal('5'), OrderAction.BUY_TO_OPEN)  # buy to open 5 shares

order = NewOrder(
    time_in_force=OrderTimeInForce.DAY,
    order_type=OrderType.LIMIT,
    legs=[leg],  # you can have multiple legs in an order
    price=Decimal('10'),  # limit price, $10/share for a total value of $50
    price_effect=PriceEffect.DEBIT
)
response = account.place_order(session, order, dry_run=True)  # a test order

This is the traceback:

ERROR:tastytrade:internal_server_error: Internal server error. Unique customer support identifier: 1d290d56d33340f0a2f99d242a9d3059
Traceback (most recent call last):
  File "/home/........................./tests.py", line 32, in <module>
    response = account.place_order(session, order, dry_run=True)  # a test order
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/...................................../venv/lib/python3.11/site-packages/tastytrade/account.py", line 1032, in place_order
    data = response.json()['data']
           ~~~~~~~~~~~~~~~^^^^^^^^
KeyError: 'data'
Graeme22 commented 8 months ago

What happens if you use a ProductionSession? (As long as you do dry_run=True the trade won't actually be placed.) I suspect it's a problem with your cert account.

zwocram commented 8 months ago

Thanks for the suggestion, I tried that and it looks like no errors for the PRD session. Strange, isn't it? I tried in postman for the certificationsession and no errors there; order was sent. Well, I'll close this question for now. Thanks for your time, I'll dive more into this. Kind regards