TDAmeritradeAPI
TDAmeritradeAPI copied to clipboard
Setting up cancel order as a child order
I am looking to do this, but I don't know if it is possible.
- build a buy order with a stop
- create a sell order higher than being bought with the stop order
- attach a child cancel order to the sell order ... The idea here is to auto cancel the 'stop' part of the stop order if the 'sell' order goes through first
- attach a child cancel order to the buy stop order ... similarly, the idea is to cancel the 'sell' order if the stop was executed due to the price dropping
Here's what I came up with so far, but it won't work because the 'cancel_order' doesn't return an 'order' object, it just executes. Is there a way to make this happen?
stop_build_order = execute.SimpleOrderBuilder.Equity.Stop.Build(stock, quantity, True, True, stop_price)
sell_order = execute.SimpleOrderBuilder.Equity.Build(stock, quantity, False, False, sell_price)
################
# can I do this? --- no this won't work because cancel_order either returns 'true' or throws, it doesn't return an order object
cancel_stop_order = execute.cancel_order(creds, account_id, stop_builder_oid)
sell_order.add_child(cancel_stop_order)
cancel_sell_order = execute.cancel_order(creds, account_id, sell_oid)
stop_build_order.add_child(cancel_sell_order)
###############
stop_builder_oid = execute.send_order(creds, account_id, stop_build_order)
sell_oid = execute.send_order(creds, account_id, sell_order)
Note: I do realize that I have to run the 'send_order' of the 'sell' or 'stop_builder' first in order to obtain the oid used in the 'cancel_order', but I don't think this would be the case if there was a way to build the cancel order before executing it. Or at the very least, I'd have the choice to choose 1 to tie to the other (opposed to tying them both to each other)
Sorry I missed this.
I'm not 100% sure what you're trying to do. It sounds like you are trying to buy and use a OCO bracket to exit. If so you would:
- enter a buy order
- when filled,, enter an OCO order w/ a stop below the market and a limit above
This could probably be combined into an OTO w/ the initial buy being the 'primary' order and the OCO being the 'conditional' order. (Have never tried a OCO w/ an OTO so not sure how this would work.)
NOTE: when you use the Equity.Stop builder you are simply creating a stop order, not an order w/ a separate/conditional stop order attached. To do that you would use an OTO. Also, just adding a child order directly won't do anything unless you also use the .set_strategy_type(...) method - in these cases its best to use the OCO or OTO builders.