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 )
最佳答案
所以我已经使用 API 几个星期了,通过 python 和 MySQL 数据库来保存“待处理交易”和“活跃交易”。我通过下达买单来执行自己的条件订单,然后在执行买单后下达单个止损卖单。然后我每五分钟检查一次,并通过下达新的止损单来提高止损单(当我这样做时,它会自动取消之前的止损单)。
您无需禁用高级功能即可执行此操作!我不确定的一件事是通过 API 进行 future 交易,我想做但尚未研究。
关于python - 在 TD Ameritrade API 上创建触发止损订单时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68641521/