Reputation: 21
I'm currently new to algo trading and am trying to place an order using the TD Ameritrade API that when executed, triggers a stop loss order to protect against losses. The first json order which is just a simple market order works fine and returns a status of 200. However, the second one returns an error of 400. The only difference between the 2 post requests was the json I passed in. I ran them through a json format checker and they seem to be fine syntax wise. I used this as reference https://developer.tdameritrade.com/content/place-order-samples. I greatly appreciate any help. Thanks!
buyurl = fr'https://api.tdameritrade.com/v1/accounts/{account_id}/savedorders'
payload = {
"orderType": "MARKET",
"session": "NORMAL",
"duration": "DAY",
"orderStrategyType": "SINGLE",
"orderLegCollection": [
{
"instruction": "Buy",
"quantity": 1,
"instrument": {
"symbol": "AAPL",
"assetType": "EQUITY"
}
}
]
}
orderpayload = {
"orderType": "MARKET",
"session": "NORMAL",
"duration": "DAY",
"orderStrategyType": "TRIGGER",
"orderLegCollection": [
{
"instruction": "BUY",
"quantity": 1,
"instrument": {
"symbol": "AAPL",
"assetType": "EQUITY"
}
}
],
"childOrderStrategies": [
{
"orderType": "STOP",
"session": "NORMAL",
"stopPrice": "98.40",
"duration": "DAY",
"orderStrategyType": "SINGLE",
"orderLegCollection": [
{
"instruction": "SELL",
"quantity": 1,
"instrument": {
"symbol": "AAPL",
"assetType": "EQUITY"
}
}
]
}
]
}
header = {'Authorization':"Bearer {}".format(access_token),
"Content-Type":"application/json"}
orderreq = requests.post(buyurl, headers = header, json = orderpayload )
Upvotes: 2
Views: 1260
Reputation: 36
Note: I was trying to build API request to trigger STOP out on a vertical options spread. Unable to figure out from TD Docs (nor via any testing) so asked TD Support. tl;dr - you can't set special trigger conditions for OPTIONS trading via the api. Response from TD Support below:
So when using the API to place orders on multi-level option orders, the only available order types would be Net Credit, Net Debit, or Market.
With a Net Debit, you would set the overall premium you are willing to pay for the spread. For example, if your strategy involves buying an option and selling another option at the same time, you would get a net debit if the premium paid for the purchase is more than the premium received from the sale.
Alternatively, with a Net Credit, you set the overall amount you are will accept for the spread. For example, if your strategy involves buying an option and selling another option at the same time, you would get a net credit if the premium paid for the purchase is less than the premium received from the sale.
The ability to place a stop or trailing stop order on an option contract would only be available using the thinkorswim platform. The API and thinkorswim are completely separate trading environments, and the capabilities and limitations are different for each.
Upvotes: 2
Reputation: 15
So I've been using the API for a few weeks now with python and a MySQL database to hold "pending trades" and "active trades". I do my own conditional orders by placing my buy order, and then making a single stop-loss sell order once my buy order has executed. Then I check every five minutes and raise the stop-loss order by placing a new s/l (when I do this, it automatically cancels the previous s/l order).
You can do this without having to disable advanced features! The one thing I'm not sure of is futures trading via the API, which I want to do but haven't researched yet.
Upvotes: 1
Reputation: 491
I am facing the same issue and tda support sent me this. it can be resolved but you will lose a lot of features. TDA do support complex orders as well as all advanced benefits but in old servers. in new servers they do not support complex order placing with advanced features turned on. On a personal note I would not suggest you to turn off the features.
If we were to turn off advanced features, here is a more complete list of features you will LOSE within the thinkorswim platform:
Loss of Portfolio Margin privileges. Reg-T margin will still be available.
Loss of Futures and Forex trading
Loss of custom grouping of positions within thinkorswim
Loss of the ability to go back in time to see previous account history beyond 60 days. (This information beyond 60 days will always be available through www.TDAmeritrade.com)
Turning Advanced features off will allow you to gain the following features for your account:
Gain the ability to place OCO and other conditional orders through our API
Gain the ability to use the Save Orders endpoint through the API.
Gain the ability to place AON, FOK, or DNR orders
Gain the ability to have real time order status updates through the API.
The above list of items are the larger changes, but is not 100% of the list of changes. Turning off advanced features is something that we must do for you. This change occurs overnight. When turning this off or on, make sure you check your account the next day to ensure any potential working orders you had from the previous day are actually still working.
If you would like me to turn off Advanced Features for you, please reply directly to this email with the email address associated with your TD Ameritrade account and the last 4 digits of each of the TD Ameritrade account numbers you would like me to turn off for you. Please note that we ask that you log off thinkorswim at the end of your trading day to allow the backend changes to work overnight. We hope to remove this type of separation in the future, but it is an extremely large back end change that has to occur before things can be merged.
Upvotes: 0