Place a TP + SL order via API

Hi

I’m new to the API and I’m trying to figure out how to place a take profit and stop loss order in one order. Is that possible?

My order should be similar to the one in the mobile app (market order) where you place a tick based order with a take profit of let’s tay 10 ticks, and a stop loss at -10 ticks.

Could someone please give an example of such an order?

It would be much appreciated.

Best
Samuel

Please take a look here for api documentation pertaining to your question.

I would also recommend getting familiar with their example repo here:

Hi there. Thank you for your reply. Much appreciated.

However, I have studied the API and also the examples in GitHub and none of them cover the example I’m looking for, a “take profit and stop loss” order via API. Also it does not state if you can place a market order based on ticks.

Does anyone have an example of a TP + SL order?

Do you mean you’ve already placed a marker order and now you want to place another separate bracket order? That would just be placeOCO instead of placeOSO.

What do you mean “based on ticks”? If you are receiving tick data you’ll have to process those packets into a more user friendly format. They give examples in the tick data section of the api on how to do that.

If you can share more specifics about what you are trying to do I’d be happy to help you further.

you are right. I realize its a bracket order I want to place. Thank you.

but just to follow up.

Is this the way to do it then, example of a buy order?

Just for this example, the price when placing the order is 17500.

// this I want to exit if reached upwards
const exitUp = {
action: ‘Buy’,
orderType: ‘Stop’,
price: +200
}
// this I want to exit if reached upwards
const exitDown = {
action: ‘Sell’,
orderType: ‘Stop’,
price: -200
}

// the order I place to enter the market
const oco = {
accountSpec: yourUserName,
accountId: yourAcctId,
action: “Buy”,
symbol: “NQH4”,
orderQty: 1,
orderType: “Market”,
isAutomated: true,
activationTime: theDateAndTimeIwantTheOrderToBePlaced?
other: exitDown,
other: exitUp,
}

Will this work?

Additional question.
activationTime: can I use this to schedule an order?

Most of that is correct!

You’ll want to change your exit up order to be a Sell Limit inside of a Buy Stop.

You’ll also want to put in there the actual prices for your bracket order and not plus or minus 200.

On activation time, I don’t know what the purpose of that key is, there isn’t any documentation for it anywhere that I’ve seen. If you are interested in filtering out trades by time I would write the logic for that yourself.

Ok so like this?

// this I want to exit if reached upwards
const exitUp = {
action: ‘Sell’,
orderType: ‘Limit’,
price: +200
}
// this I want to exit if reached upwards
const exitDown = {
action: ‘Buy’,
orderType: ‘Stop’,
price: -200,
other: exitUp
}

// the order I place to enter the market
const oco = {
accountSpec: yourUserName,
accountId: yourAcctId,
action: “Buy”,
symbol: “NQH4”,
orderQty: 1,
orderType: “Market”,
isAutomated: true,
other: exitDown,
}

The problem with the actual prices is that I don’t have them since it is a “Market” order. I buy at any price at the given time.

So finally I think I’ve got it.

const symbol = “NQH4”;

const currentPrice = getCurrentPrice(symbol);

const takeProfit = {
“action”: “Sell”,
“orderType”: “Limit”,
“price”: currentPrice+5
}

const oco = {
“accountSpec”: MY_USER_NAME,
“accountId”: MY_ACCOUNT_ID,
“action”: “Buy”,
“symbol”: symbol,
“orderQty”: 1,
“orderType”: “Market”,
“price”: currentPrice,
“stopPrice”: currentPrice-5, // stop loss
“isAutomated”: “true”,
“other”: takeProfit
}

Hey, I’m doing the same thing as you are now, did this work for you? I would test to check myself, but the markets are closed today.

Thank you in advance.

Actually no. This forum is not very active besides Mr unsupervised, but I think he disappeared too. :slight_smile: I’ve also emailed the support with the same questions but no answer yet and it’s been more than a week.

What’s your discord? Let’s work together

Add me, my discord username is: opsecfx

Hi, so sorry. I don’t use discord. I´ll send a PM.