jessecooper / pyetrade

Python E-Trade API Wrapper
GNU General Public License v3.0
202 stars 94 forks source link

Issue with placing a new equity order #35

Open arighna opened 3 years ago

arighna commented 3 years ago

I am trying to place an equity order in the sandbox environment. I am able to list the accounts. However, I am probably going wrong with the payload. Below is the script I have put together for the same.

`

import pyetrade from requests_oauthlib import OAuth1Session

base_url = "https://apisb.etrade.com" account_id = 'account_id'
consumer_key = 'consumer_key' consumer_secret = 'consumer_secret'

oauth = pyetrade.ETradeOAuth(consumer_key, consumer_secret) print(oauth.get_request_token()) # Use the printed URL

verifier_code = input("Enter verification code: ") tokens = oauth.get_access_token(verifier_code) print(tokens)

accounts = pyetrade.ETradeAccounts( consumer_key, consumer_secret, tokens['oauth_token'], tokens['oauth_token_secret'] )

print(accounts.list_accounts())

orders = pyetrade.order.ETradeOrder(
consumer_key, consumer_secret, tokens['oauth_token'], tokens['oauth_token_secret'], dev=True )

account_id = 'account_id'
symbol = 'symbol'

orders.place_equity_order( accountId = account_id, symbol = symbol, orderAction="BUY", clientOrderId= "1a2b3c", priceType="MARKET", quantity=100, orderTerm="GOOD_UNTIL_CANCEL", marketSession="REGULAR", )

`

Below is the error I am getting.

`


HTTPError Traceback (most recent call last)

in 27 quantity=100, 28 orderTerm="GOOD_UNTIL_CANCEL", ---> 29 marketSession="REGULAR", 30 ) ~/opt/anaconda3/lib/python3.7/site-packages/pyetrade/order.py in place_equity_order(self, resp_format, **kwargs) 525 "because of an Etrade bug as of 1/1/2019" 526 ) --> 527 preview = self.preview_equity_order(resp_format, **kwargs) 528 if resp_format == "xml": 529 preview = jxmlease.parse(preview) ~/opt/anaconda3/lib/python3.7/site-packages/pyetrade/order.py in preview_equity_order(self, resp_format, **kwargs) 483 payload = self.build_order_payload("PreviewOrderRequest", **kwargs) 484 --> 485 return self.perform_request(self.session.post, resp_format, api_url, payload) 486 487 def change_preview_equity_order(self, resp_format=None, **kwargs): ~/opt/anaconda3/lib/python3.7/site-packages/pyetrade/order.py in perform_request(self, method, resp_format, api_url, payload) 195 196 LOGGER.debug(req.text) --> 197 req.raise_for_status() 198 199 if resp_format == "json": ~/opt/anaconda3/lib/python3.7/site-packages/requests/models.py in raise_for_status(self) 939 940 if http_error_msg: --> 941 raise HTTPError(http_error_msg, response=self) 942 943 def close(self): HTTPError: 400 Client Error: Bad Request for url: https://apisb.etrade.com/v1/accounts/83405188/orders/preview `
jessecooper commented 3 years ago

https://apisb.etrade.com/docs/api/order/api-order-v1.html#/definition/orderPlace

make sure you are using the accountIdKey this area of code could use an update to align better with the terminology of V1 but it still works.

arighna commented 3 years ago

https://apisb.etrade.com/docs/api/order/api-order-v1.html#/definition/orderPlace

make sure you are using the accountIdKey this area of code could use an update to align better with the terminology of V1 but it still works.

Thank you for your response. I got passed the listing order section, thanks to your suggestion. I am more interested in the order placing post request though. Below is the error I am getting for the same.

``

AttributeError Traceback (most recent call last)

in 84 } 85 ---> 86 req = session.post(api_url, json=payload, timeout=30) 87 req.json() 88 ~/opt/anaconda3/lib/python3.7/site-packages/requests/sessions.py in post(self, url, data, json, **kwargs) 576 """ 577 --> 578 return self.request('POST', url, data=data, json=json, **kwargs) 579 580 def put(self, url, data=None, **kwargs): ~/opt/anaconda3/lib/python3.7/site-packages/rauth/session.py in request(self, method, url, header_auth, realm, **req_kwargs) 179 url, 180 oauth_params, --> 181 req_kwargs) 182 183 if header_auth and 'oauth_signature' not in \ ~/opt/anaconda3/lib/python3.7/site-packages/rauth/oauth.py in sign(self, consumer_secret, access_token_secret, method, url, oauth_params, req_kwargs) 139 140 oauth_params = \ --> 141 self._normalize_request_parameters(oauth_params, req_kwargs) 142 parameters = map(self._escape, [method, url, oauth_params]) 143 ~/opt/anaconda3/lib/python3.7/site-packages/rauth/oauth.py in _normalize_request_parameters(self, oauth_params, req_kwargs) 78 if 'Content-Type' in headers and \ 79 headers['Content-Type'] == FORM_URLENCODED: ---> 80 for k, v in data.items(): 81 normalized += [(k, v)] 82 AttributeError: 'NoneType' object has no attribute 'items' ``
arighna commented 3 years ago

I accidentally closed the issue!

arighna commented 3 years ago

accountIdKey

is the 'accountIdKey' same as accountId?

smokinu commented 3 years ago

'accountIdKey' and 'accountId' are different parameters on E-trade.

accountId | string | The user's account ID accountIdKey | string | The unique account key

Were you able to get the code to work?

arighna commented 3 years ago

'accountIdKey' and 'accountId' are different parameters on E-trade.

accountId | string | The user's account ID accountIdKey | string | The unique account key

Were you able to get the code to work?

I found the accountIdKey and I am able to list orders. However, I am not able to structure the payload dictionary. Below is what I am using to place the order.

api_url = f'https://apisb.etrade.com/v1/accounts/{account_id_key}/orders/place'

payload = { "accountId": str(account_id), "clientOrderId": str(clientOrderId), "limitPrice": str(limitPrice), "quantity": str(quantity), "symbol": symbol, "orderAction": orderAction, "priceType": "LIMIT", "marketSession": "REGULAR", "orderTerm": "GOOD_FOR_DAY" }