Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keep getting requests.exceptions.HTTPError: 400 Client Error: Bad Request for url when trying to place options order. #77

Open
rockwoodquant opened this issue Feb 16, 2023 · 9 comments
Labels

Comments

@rockwoodquant
Copy link

rockwoodquant commented Feb 16, 2023

I am trying to send an options order using pyetrade and got bad url error. Other modules seem to work fine - e.g. market / accounts etc.

Can someone please help? I'm out of ideas now - thanks!

here is what I was trying to do:

-----------------code below----------------

  action = "BUY_OPEN"
  symbol = "SPX"
  callPut = "CALL"
  expiryDate = "2023-02-16"
  strikePrice = 4140
  quantity = 1
  limitPrice=0.25
  orderTerm = "GOOD_UNTIL_CANCEL"  # "IMMEDIATE_OR_CANCEL"  # "GOOD_FOR_DAY"
  marketSession = "REGULAR"
  priceType = "LIMIT"
  account_id = ACCOUNT_ID_KEY
  clientOrderId = "888"
  
  resp = orders.place_option_order(
        accountId = account_id,
        symbol = symbol,
        callPut=callPut,
        expiryDate=expiryDate,
        strikePrice=strikePrice,
        orderAction=action,
        clientOrderId=clientOrderId,
        priceType= priceType,
        limitPrice=limitPrice,
        allOrNone=False,
        quantity=quantity,
        orderTerm=orderTerm,
        marketSession=marketSession,
      )

------------end of code---------------

This gets the following error --

-----------start of error---------------

  File "<input>", line 15, in <module>
  File "C:\Users\KeyUser\PycharmProjects\ETradeTrading\venv\lib\site-packages\pyetrade\order.py", line 449, in place_option_order
    return self.place_equity_order(resp_format, **kwargs)
  File "C:\Users\KeyUser\PycharmProjects\ETradeTrading\venv\lib\site-packages\pyetrade\order.py", line 475, in place_equity_order
    preview = self.preview_equity_order(resp_format, **kwargs)
  File "C:\Users\KeyUser\PycharmProjects\ETradeTrading\venv\lib\site-packages\pyetrade\order.py", line 417, in preview_equity_order
    return self.perform_request(self.session.post, resp_format, api_url, payload)
  File "C:\Users\KeyUser\PycharmProjects\ETradeTrading\venv\lib\site-packages\pyetrade\order.py", line 302, in perform_request
    return get_request_result(req, resp_format, {})
  File "C:\Users\KeyUser\PycharmProjects\ETradeTrading\venv\lib\site-packages\pyetrade\order.py", line 49, in get_request_result
    req.raise_for_status()
  File "C:\Users\KeyUser\PycharmProjects\ETradeTrading\venv\lib\site-packages\requests\models.py", line 1021, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 400 Client Error: Bad Request for url: https://api.etrade.com/v1/accounts/[...my account id key...]/orders/preview

------------end of error------------

Thanks in advance...

-RQ

@rockwoodquant
Copy link
Author

I tried to use account_id instead of accountIdKey, i also tried to add in resp_type = "xml" in the function call. every time so far got to the same error...

@xabaddonx1
Copy link

xabaddonx1 commented Feb 22, 2023

Same issue here.

edit I figured it out, the expiry date format needs to be month-day-year

@rockwoodquant
Copy link
Author

@xabaddonx1 I tried your suggestion, same error...

@xabaddonx1
Copy link

You must have another issue. I think the client order id needs to be unique for each order, i generate it with
clientOrderId = dt.datetime.timestamp(presentDate) * 1000000
maybe that's it.

@rockwoodquant
Copy link
Author

Again, same error with this change... @xabaddonx1

@xozxro
Copy link

xozxro commented Mar 5, 2023

Any update on this?

Running into the identical issue and have taken identical steps with no luck. Is this a bug in PyEtrade or picky E*Trade request formatting?

Odd that it works for some... @xabaddonx1 would you mind sharing your working request code and param formatting for option orders? Would be a large help.

@xabaddonx1
Copy link

Here's mine, a couple things I thought of:

It could be the clientOrderId needs to be an int.

    resp = orders.place_option_order(
        resp_format='xml',
        accountId=brokerageAccountIdKey,
        symbol=symbol,
        callPut=callPut,
        expiryDate=expiryDate,
        strikePrice=formatNumber(strikePrice),
        orderAction=action,
        clientOrderId=int(clientOrderId),
        priceType=priceType,
        limitPrice=limitPrice,
        allOrNone=False,
        quantity=quantity,
        orderTerm="GOOD_FOR_DAY",
        marketSession='REGULAR')

i'm running the code below on strikePrice, I ran into an issue where sometimes it would say 12.0 and the api doesn't like that :


def formatNumber(num):
  if num % 1 == 0:
    return int(num)
  else:
    return num

@Robert-Zacchigna
Copy link
Contributor

Robert-Zacchigna commented Mar 21, 2023

The client_order_id needs to be an int cast as a str.

This is what i have working in my code without issue:

from random import randint

client_order_id = str(randint(1000000000, 9999999999))

Copy link

github-actions bot commented Nov 8, 2024

This issue is stale because it has been open 90 days with no activity. Remove stale label or comment or this will be closed in 5 days.

@github-actions github-actions bot added the Stale label Nov 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants