python - 在 TD Ameritrade API 上创建触发止损订单时出现问题

标签 python

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/

相关文章:

python 字符串在一行

python - Python OpenCV从黑白蒙版检测圆圈

python - 我可以使用 SocksiPy 在函数内更改 SOCKS 代理吗?

python - 在 python 3.4 中序列化对象的错误字符

Python:当我的模块返回 "Ellipsis"时,这意味着什么?

python - 在无限循环中运行时如何处理GUI(例如pysimplegui)进入 'Not Responding'?

python - ValueError : Trying to create optimizer slot variable under the scope for tf. 分发。策略

python - Slugify 未将标题保存为 slug Django 2 : NoReverseMatch error

python - 类型错误 : KMeans() got an unexpected keyword argument 'n_clusters'

python - django.core.exceptions.ValidationError : ["' ' is not a valid UUID.“]