jessecooper / pyetrade

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

Issues placing equity order #43

Closed JhnAshlnd closed 1 week ago

JhnAshlnd commented 3 years ago

` orders = pyetrade.ETradeOrder( consumer_key, consumer_secret, oauth_token, oauth_token_secret, dev=True )

print(orders.list_orders(accountID, resp_format='json'))

orders.place_equity_order(resp_format='json',clientOrderId='33ad33f3fdc',priceType='MARKET',accountId=accountID,symbol='GOOG',orderAction='BUY',quantity='10000',marketSession='REGULAR',orderTerm='GOOD_FOR_DAY')`

This is the code I have but I get a "400 Client Error: Bad Request for url: https://apisb.etrade.com/v1/accounts/6_Dpy0rmuQ9cu9IbTfvF2A/orders/preview" error no matter which account id key I try, as you can see I have a line to print the order list commented out and it works with the account id key but it wont work to place an equity order

JhnAshlnd commented 3 years ago

Turns out if you specify the response format as json it doesn't work, switching it back to xml makes it work; how do I obtain the xml response as it doesn't appear on the terminal?

jessecooper commented 2 years ago

@JhnAshlnd sorry for the late response to this. I am only now finding some open time to continue work on this project. I am going to refactor the orders API to be more like the market, account and alert API in regards to how the resp_format is set and change the xml parsing lib over to xmltodict. A lot of the etrade v1 APIs only work with xml but there is no documentation around what supports json and what will not. Because of this I am going to make all methods default to xml but the return will give a dict just the same as if it was a json response. This is the behavior of interfaces that have been refactored. The refactor on orders is currently in motion but I do not have an eta on that.

Now with the current order implementation if you set resp_format=None it should return a parsed dict of the xml response. If you set it to xml it should just give you the raw string of the response from the API and you can use whatever parser you want. I understand this is odd and not very intuitive behavior that I will address in the refactor.

chotaGit commented 2 years ago

1) For JSON, the application type has to be json

        "Content-Type": "application/json", "consumerKey": {'CONSUMER_KEY'}

2) All values passed in the json payload have to be in quotes including numbers. 3) Before submitting the order, the order has to be previewed. And all the fields of the previewd order have to be included in the PlaceOrderRequest Payload

    "PreviewOrderRequestPayload": {
        "PreviewOrderRequest": {
            "orderType": "$orderType",
            "clientOrderId": "$clientOrderId",
            "Order": [{
                    "allOrNone": "false",
                    "priceType": "LIMIT",
                    "limitPrice": "$limitPrice",
                    "stopPrice": "0",
                    "orderTerm": "GOOD_FOR_DAY",
                    "marketSession": "REGULAR",
                    "Instrument": [{
                            "Product": {
                                "symbol": "$symbol",
                                "securityType": "$securityType",
                                "callPut": "$callPut",
                                "expiryYear": "$expiryYear",
                                "expiryMonth": "$expiryMonth",
                                "expiryDay": "$expiryDay",
                                "strikePrice": "$strikePrice"
                            },
                            "orderAction": "$orderAction",
                            "orderedQuantity": "$orderedQuantity",
                            "quantity": "$quantity"
                        }
                    ]
                }
            ]
        }
    },
    "PlaceOrderRequestPayload": {
        "PlaceOrderRequest": {
            "orderType": "$orderType",
            "clientOrderId": "$clientOrderId",
            "PreviewIds": [{
                    "previewId": "$previewId"
                }
            ],
            "Order": [{
                    "allOrNone": "false",
                    "priceType": "LIMIT",
                    "limitPrice": "$limitPrice",
                    "stopPrice": "0",
                    "orderTerm": "GOOD_FOR_DAY",
                    "marketSession": "REGULAR",
                    "Instrument": [{
                            "Product": {
                                "symbol": "$symbol",
                                "securityType": "$securityType",
                                "callPut": "$callPut",
                                "expiryYear": "$expiryYear",
                                "expiryMonth": "$expiryMonth",
                                "expiryDay": "$expiryDay",
                                "strikePrice": "$strikePrice"
                            },
                            "orderAction": "$orderAction",
                            "orderedQuantity": "$orderedQuantity",
                            "quantity": "$quantity"
                        }
                    ]
                }
            ]
        }
    },
bitbugprime commented 2 years ago

3. Before submitting the order, the order has to be previewed

It does? The API docs claim that preview is unnecessary and that you can submit orders directly. Preview is stupid anyway for algorithmic trading.

github-actions[bot] commented 2 weeks ago

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.