Anyone know if there is a way to set an OCO order on an open position? Basically I would like to be able to set a take profit and a stop loss on an open position
1 Like
I think there are examples in github provided by tradovate. but here’s what I did. Not all code is provided but should serve as a decent example
async enterMarketWithStop(accountId, action, symbol, size, stopPoints) {
const marketOrder = {
accountId: Number(accountId),
accountSpec: process.env.TRADOVATE_name,
symbol,
action,
orderQty: size, isAutomated: true, orderType: "Market"
}
this.send(`order/placeorder\n${++this.messageCount}\n\n${JSON.stringify(marketOrder)}`)
await this.checkOrder('isPositionOpen')
//assume there is only ever one position one at a time. Brittle..
const position = this.getPosition(symbol)
assert(position?.netPrice, 'There must be a fillprice. if we are out of margin the order fails')
const stopPrice = (action == 'Buy') ? position.netPrice + stopPoints*-1 : position.netPrice + stopPoints
log.info('stopPrice:', stopPrice)
const stopOrder = {
accountId: Number(accountId),
accountSpec: process.env.TRADOVATE_name,
symbol,
action: (action == 'Buy') ? 'Sell' : 'Buy',
orderQty: size, isAutomated: true, orderType: "Stop", stopPrice
}
this.send(`order/placeorder\n${++this.messageCount}\n\n${JSON.stringify(stopOrder)}`)
await this.checkOrder('isOrderOpen')
}
The above uses the websocket api and doesn’t show position/order update code.