-
-
Notifications
You must be signed in to change notification settings - Fork 97
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
Need help: preview request works but cannot place order #23
Comments
Hello yangliu2. I am also having the same issue. Matter of fact I am not even able to execute the preview_equity_order() function. Getting "missing kwargs" error. Below are the details. Hope I am not hijacking your issue request but would you know what is causing this issue since I see you are able to execute preview_equity_order? if not all(param in kwargs for param in mandatory): Below is the kwargs dictionary I am using in SandBox environment |
No problem man! I'm having the same error. I manually checked the required keys AND modified source code to check if "mandatory" have it. No luck. I don't know what's it's checking. This is where I went to Etrade sample code instead. Also remember trade sandbox have fixed response for preview and place order, so you need to make sure your placing order payload is the same as the preview response, not whatever payload you send it to them. Now I'm sticking on error 9999 for placing order. |
Hello yangliu2 Thanks for your response. After trying a few things was able to make the preview order and place order to work. You simply have to make sure your kwargs dictionary is in the order in the mandatory list in the check order() function. mandatory = ["accountId","symbol","orderAction","clientOrderId","priceType","quantity","orderTerm","marketSession"] Ofcourse remember to use ** before kwargs in the paranthesis to show its a dictionary. All this was in dev environment. I haven't tried actual prod environment.. Let me know how it goes. |
Test these kwarg keys values, kwargs = {"accountId":"6_Dpy0rmuQ9cu9IbTfvF2A", Then after you instantiate the ETradeOrder class and create an object try calling the method below |
For some reason, if I use |
The JSON response works for only about half of the API. This is not documented and it has been discovered by trial and error. Numerous tickets have been submitted and we’ve talked to the “API team”, but it’s been many years, so I doubt it will ever be fixed.
The XML format (default) seems to be consistently functional.
… On Jun 6, 2020, at 1:44 PM, Yang Liu ***@***.***> wrote:
For some reason, if I use resp_format='json', then I get a 400 response error. Without the parameter, it seems to be fine. ¯\_(ツ)_/¯
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub, or unsubscribe.
|
Hello yangliu2, I am also facing exact same issue which you faced. Please let me know how you resolved this. Thanks a lot. |
Hi, @yangliu2 were you able to resolve this and place order successfully from python code? |
I gave up on the idea, since my model wasn't able to outperform the "buy
and hold strategy" over 6 months. I did got some code from etrade support
several months later, but didn't even bother trying.
…On Wed, Mar 10, 2021 at 12:33 PM tommyhuangli ***@***.***> wrote:
Hi, @yangliu2 <https://github.com/yangliu2> were you able to resolve this
and place order successfully from python code?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#23 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AA3JPASJGR6P7KMZA5KNPTDTC6NPJANCNFSM4NOYECGA>
.
|
I am also trying to place an order with the ETrade API and got the following error: {'code': 101, 'message': 'For your protection, we have timed out your original order request. If you would like to place this order, please resubmit it now.'} the preview order seems to be working ok. If anyone figures how to successfully place an order please let me know, thanks! |
@yangliu2 could you please share the code ETrade support provides? Thanks! |
I have the extract same issue. Looks like E*TRADE is limiting the
individuals to do things like this.
…On Sun, Mar 21, 2021 at 4:36 PM yui410122 ***@***.***> wrote:
@yangliu2 <https://github.com/yangliu2> could you please share the code
ETrade support provides?
Thanks!
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#23 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AADO4EEOCVWCXMESFVM6XGTTEZ7IHANCNFSM4NOYECGA>
.
|
Here is the code I got from Etrade. There weren't any formating, so you may have to retype.
|
Thanks @yangliu2 ! "Make sure you copy the fields from preview order into your place order and you have to pass the previewId from the response from the preview order into the place order request or it will not work." This comment is really useful! I tried to include the previewID and now placing the order works! Thanks a lot! |
@yangliu2 Can you please open a PR with any changes that are needed in pyetrade to better support this? |
I tried including the previewId in the order but I still get error 400 after the place_equity_order is called. Any clue why this isn't working??
|
I've tried all of above solutions but None is working for me, place order keep responding same error and preview order working correctly |
For anyone else who comes across this thread, i was unable to get the You basically need to make the request yourself using the underlying api endpoints instead of the functions. For these function to work, you need to have already setup pyetrades oauth (in my case its import json
import xmltodict
import xml.etree.ElementTree as ET
from dicttoxml2 import dicttoxml
from pyetrade.order import ETradeOrder
from pyetrade import ETradeAccounts, ETradeOAuth Function for making the post requests to the etrade api endpoints def _post_etrade_req(etrade_oauth: ETradeOAuth, etrade_order: ETradeOrder, account_id_key: str,
order_params: dict, post_endpoint: str, print_payload: bool, print_response: bool) -> dict:
order_payload = dicttoxml(order_params, root=False, attr_type=False).decode('UTF-8')
if print_payload:
pretty_xml = ET.XML(order_payload)
ET.indent(pretty_xml)
print(f'\n{post_endpoint.capitalize()} Order Request XML Payload:\n{"="*(27+len(post_endpoint))}\n'
f'{ET.tostring(pretty_xml, encoding="unicode")}')
headers = {"consumerKey": etrade_oauth.consumer_key, 'Content-Type': 'application/xml'}
post_endpoint_url = f'https://api.etrade.com/v1/accounts/{account_id_key}/orders/{post_endpoint}'
order_response = etrade_order.session.post(post_endpoint_url, headers=headers, data=order_payload)
order_data = json.loads(json.dumps(xmltodict.parse(order_response.text)))
if print_response:
print(f'\n{post_endpoint.capitalize()} Order JSON Response:\n{"="*(21+len(post_endpoint))}\n'
f'{json.dumps(order_data, indent=4)}')
return order_data My # order_action_values = ['BUY', 'SELL', 'BUY_TO_COVER', 'SELL_SHORT']
# price_types_values = ['MARKET', 'LIMIT', 'STOP', 'STOP_LIMIT', 'MARKET_ON_CLOSE']
# market_session_values = ['REGULAR', 'EXTENDED']
# order_term_values = ['GOOD_UNTIL_CANCEL', 'GOOD_FOR_DAY', 'IMMEDIATE_OR_CANCEL', 'FILL_OR_KILL']
# routing_destinations_values = ['AUTO', 'ARCA', 'NSDQ', 'NYSE']
# Reference number generated by developer. Used to ensure duplicate order is not submitted.
# Value can be of 20 alphanmeric characters or less Must be unique within this account.
# Does not appear in any API responses.
# Ref: https://apisb.etrade.com/docs/api/order/api-order-v1.html#/definitions/OrderDetail
def preview_shares_order(etrade_oauth: ETradeOAuth, etrade_order: ETradeOrder, account_id_key: str, symbol: str,
limit_price: float, quantity: int, print_payload: bool, print_response: bool,
order_type: str = 'EQ', order_action: str = 'BUY', all_or_none: bool = False,
client_order_id: str = str(randint(1000000000, 9999999999)), stop_price: float = 0,
price_type: str = 'LIMIT', market_session: str = 'REGULAR',
order_term: str = 'IMMEDIATE_OR_CANCEL', routing_destination: str = 'AUTO') -> tuple[dict, int]:
preview_order_params = {
'PreviewOrderRequest': {
'orderType': order_type,
'clientOrderId': client_order_id,
'Order': {
'allOrNone': all_or_none,
'priceType': price_type,
'orderTerm': order_term,
'marketSession': market_session,
'stopPrice': stop_price,
'limitPrice': limit_price,
'routingDestination': routing_destination,
'Instrument': {
'Product': {
'securityType': 'EQ',
'symbol': symbol
},
'orderAction': order_action,
'quantityType': 'QUANTITY',
'quantity': quantity,
}
}
}
}
preview_order_data = _post_etrade_req(etrade_oauth, etrade_order, account_id_key, preview_order_params,
'preview', print_payload, print_response)
return preview_order_params['PreviewOrderRequest'], preview_order_data['PreviewOrderResponse']['PreviewIds']['previewId'] My def place_shares_order(etrade_oauth: ETradeOAuth, etrade_order: ETradeOrder, account_id_key: str,
preview_order_params: dict, preview_id: int, print_payload: bool, print_response: bool) -> None:
# Insert preview_order_params and preview_id into new place_shares_order payload
place_order_params = {'PlaceOrderRequest': preview_order_params}
place_order_params['PlaceOrderRequest']['PreviewIds'] = {'previewId': preview_id}
place_order_data = _post_etrade_order_req(etrade_oauth, etrade_order, account_id_key, place_order_params, 'place',
print_payload, print_response)['PlaceOrderResponse']['Order']['messages']['Message']
for index, msg_codes in enumerate(place_order_data):
order_msg_code = msg_codes['code']
order_msg_desc = msg_codes['description'].replace('|', ' - ')
# Order success code = 1026
# Ref: https://apisb.etrade.com/docs/api/order/api-order-v1.html#/definitions/ErrorCodes
if order_msg_code != '1026' and index + 1 == len(place_order_data):
raise ValueError(f"Order Msg Code: {order_msg_code}\n Description: {order_msg_desc}")
print(f"Order Msg Code: {order_msg_code}\n Description: {order_msg_desc}\n") And calling them: preview_order_params, preview_id = preview_shares_order(etrade_oauth, etrade_order, account_id_key, 'CVNA',
1.00, 1, print_payload=False, print_response=False)
place_shares_order(etrade_oauth, etrade_order, account_id_key, preview_order_params,
preview_id, print_payload=False, print_response=False) If successful your output should be:
You can verify even further by printing out your order history: print('Orders:', etrade_order.list_orders(account_id_key, resp_format='json')) |
After one day of digging, I found the root cause of this stupid issue. The string key "previewIds" need be capitalized as "PreviewIds". Otherwise, the server won't recognize the previewId parameter. |
Looots of inconsistency in Etrade API. Sometimes the first character is capped and sometimes it isn't. |
I do have another issue with etrade api. During authentication, from time to time, I got this error. All I can do is to restart the code and then it could pass without error. Exception has occurred: UnicodeDecodeError |
|
Hi, I've been trying to automate some options trades using pytrade/etrade for some months and while I can get individual legs to excersise, there doesn't seem to be support for spreads using pyetrade. Hense I've been looking at your code example here and trying to modify that to suit my needs. Starting with the code as is, I don't seem able to get passed the etrade_oauth part. I see NameError: name 'etrade_oauth' is not defined. Did you mean: 'ETradeOAuth'? Would anyone be able to help me integrate the above code into Jesse's original outh example ? |
My code above assumed you already have setup ETradeOAuth and EtradeOrder For EtradeOAuth, in my case i named the variable etrade_oauth: etrade_oauth = ETradeOAuth(consumer_key, consumer_secret) You then need to create the ETradeOrder obj, in my case i named it etrade_order: etrade_order = ETradeOrder(
consumer_key, consumer_secret,
oauth_token, oauth_secret, dev=False # False for PROD, True for SANDBOX
) Once the above is done, then you can run my code above, which i think still works. I've made some changes since then i think. |
@Robert-Zacchigna Looks like there is a short coming in the pyetrade lib that is corrected by your function. Would you mind opening up a PR that would add your method to the ETradeOrder object? That would be very appreciated I am sure by everyone in the community. |
Thanks @Robert-Zacchigna that's working fine now for equity orders ! I will modify the fields to support Options orders insteadf of Equity orders and hopefully eventually get a spread to work also. Much apprecaited. |
@m4rkyt No problem, glad you were able to make it work. @jessecooper I'd be happy to, i haven't looked too deeply into the source code so it might take me a little bit to get up to speed. By method do you mean |
The fix for |
@jessecooper Ok so after digging through some things, I was actually able to get the functions already in This works to retrieve a previewId: client_order_id = str(randint(1000000000, 9999999999))
preview_id = etrade_order.preview_equity_order(accountId=account_id_key, symbol='SPY', clientOrderId=client_order_id, limitPrice=300.0,
quantity=1, orderType='EQ', orderAction='BUY', allOrNone=False, stopPrice=0,
priceType='LIMIT', marketSession='REGULAR', orderTerm='FILL_OR_KILL',
routingDestination='AUTO')['PreviewOrderResponse']['PreviewIds']['previewId']
print('PreviewID:', preview_id) This code also successfully places the equity order without a previewId (verified in etrade under the orders tab): client_order_id = str(randint(1000000000, 9999999999))
kwargs = {'accountId': account_id_key, 'symbol': 'SPY', 'orderAction': 'BUY', 'clientOrderId': client_order_id,
'priceType': 'LIMIT', 'quantity': 1, 'orderTerm': 'FILL_OR_KILL', 'marketSession': 'REGULAR', 'limitPrice': 300,
'orderType': 'EQ', 'allOrNone': False, 'stopPrice': 0, 'routingDestination': 'AUTO'}
print(etrade_order.place_equity_order(**kwargs)) AND client_order_id = str(randint(1000000000, 9999999999))
print(etrade_order.place_equity_order(accountId=account_id_key, symbol='SPY', orderAction='BUY',
clientOrderId=client_order_id, priceType='LIMIT', quantity=1,
orderTerm='FILL_OR_KILL', marketSession='REGULAR', limitPrice=300,
orderType='EQ', allOrNone=False, stopPrice=0, routingDestination='AUTO')) Looking at it all, i think whats going on is a combination of misunderstanding the variable
I can make the PR if you agree with the above, but please double check yourself that what i stated above is the same for you before we make any changes. Let me know what you find and if you agree with my assessment, thanks. |
I do agree. The |
@jessecooper Sounds good, I've given it all a first run through, please check it out here and let me know what you think: #78 |
@Robert-Zacchigna Thank you for doing this. Give me a bit of time to look at the changes. |
@m4rkyt are you able to get the spread to work? can you create a PR to share the code? |
Hi @mw66, |
I'd love to see the spread order working too. |
Hi all, I'm new here. I'm trying to call the
Below is what I pass in to preview_equity_order
Here is the Payload
Below is the preview order request.
I get the following response:
Thanks for taking a look! |
@korivernon I havent seen that error before, could be something with your network or something else.
|
Hey Robert! Thank you for your reply.
1) Yes, I'm able to make other calls with the API. I actually tried implementing the order functionality on my own before stumbling upon this repository.
I successfully previewed an order with my implementation, however, I was unable to place the order. I received the error I received the error that said something along the lines of: "we were unable to place your order as we think you placed it by mistake. Please resubmit if it wasn't a mistake."
2) I did try to place it outside of market hours... maybe that's the issue?!
3) I ran this with shares that I already have. Maybe it's a network issue; I'll try again tomorrow during Market hours.
Thanks, Robert!
…On Apr 16, 2023 at 14:14 -0400, Robert ***@***.***>, wrote:
@korivernon I havent seen that error before, could be something with your network or something else.
• Are you able to make other calls with the api?
• Did you try this during trading hours or after hours?
• Furthermore, one of the weird quirks of the API is that it wont let you preview or attempt to place an equity order if the order itself is more than the available cash you have to spend in your account. Don't know why it is that way, just something I've observed during my usage with the API.
• Although that would usually kick out a different error than the one above (which is why i think it could be something with your network connection).
—
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you were mentioned.Message ID: ***@***.***>
|
I'm still getting an issue trying to submit my order (using my own order version) during market hours... Successfully Previews but I get this
When I use the order functionality from EtradeOrder, I get this issue:
Does anyone mind sending their
Thanks, |
@korivernon I think you might be missing some params, try adding these 3 params as well to your kwargs: "allOrNone": False, "stopPrice": 0, "routingDestination": 'AUTO' The etrade api uses a mix of json and xml endpoints (who knows why), so the error above seems to indicate the xml payload is malformed (which could be the result of missing params). I dont have much experience with options orders but it should be similar enough to equity orders. |
It seems like that may have done the trick! Sometimes the read times out. Other times it seems to execute without issue. This may be because I'm trying to place the order outside of market hours, so I'll give it another try in the morning. In the meantime, have you ever gotten this:
Additionally, when it "seems to run without issue," this is the dictionary that is the result of the
Here are the kwargs I'm passing in:
|
I see you mentioned using a custom order function, did you by chance specify a timeout in the request? From what i can tell its possible the timeout is being reached before the request has time to be completed. As for the 400 error im not too sure, it strange that it happens when you call the I say that because if you look at the code for the order functions,
If the order is executed then i wouldn't be too concerned about it, i would take a guess that it might be related to the timeout issue. |
Has anyone stumbled across and figured out this issue? |
UPDATE: appears that my VPN was playing some role here. Connecting to VPN resolved the issue. |
First, I tried using your pyetrade package but not be able to place order.
Then I tried to use the Etrade provided sample code, but I got this error.
{'code': 101, 'message': '{com.etrade.api.v1.order.InvalidPreviewId}'
The second time I tried though, I got a different error.
{'code': 101, 'message': 'For your protection, we have timed out your original order request. If you would like to place this order, please resubmit it now.'}
Payload I sent is like this:
I hate that Etrade makes me preview order before placing one, but I see that pyetrade also need to preview order before placing it. If you seen this before, any help is appreciated.
If you can give me some code example of how to use pyetrade to preview and place order, I would also much prefer doing it that way. I'm kinda reading challenged when reading documentations without code examples. Thanks!
The text was updated successfully, but these errors were encountered: