ThinkorSwim First Triggers OCO Order

mastermind07

New member
New to ThinkorSwim. I have a basic question, can you place an order on thinkorswim whereby you place three OCO (One-Cancels-the-Other Order) orders for three different stocks, and two get canceled once any of them is placed?

Thanks.
 

soary

Member
I learned a lot on this site and others and I am exited to finally have a thinkscript that I can use to buy and sell and I already tested it for my use. Today, I set up the ticker in Tos with an order and added the condition by using the little gear thingy on the far right, but the order did not fill so I'm trying to figure out why and I'm sharing what I did for your input in case you have a better way. At first, I tried to set the order to buy but then I would not be able to set the sell order at the same time before the buy order goes live. Next, I click on the "ask" and chose "buy with custom" so that I would have access to the brackets under "advance order" and could place multiple orders at the same time. Then, I changed advance order to "1st triggers all" so that I can place 3 orders; the buy and sell order with the condition for each one and stop order all at the same time and it still didn't get filled. As I watched my working order and price movement, it finally dawned on me that I probably need to change from limit order to market order to get it to fill since it would be difficult to know at what price the condition would trigger, and since there was only a 1cent difference between bid and ask it would have minimal slippage. Finally, by the time I worked out all of this the price had moved from my ideal entry point. How do you all enter a buiy and sell order in TOS using thinkscript? In thinkscripts code price is named as the close, high, low and etc so does this mean the setting to set up the order should be as a market order? Thanks in advance.

As a follow up with TOS team it can be done by setting up a buy with custom OCO or sell with custom OCO order and you get 3 brackets, so then you enter your buy and sell orders with thinkscript conditions as I described previously and add your stop. If you already know how to use OCO you already know most of this, the difference is the addition of thinkscript conditions. In back testing the system creates new orders but like you said, in real it will not create new orders and will only work with the orders that are set up manually with or without conditions. It's done as a market order to get filled so I will use it with tight bid-ask spreads and the thinkscript conditions will rule the trades. I'm not a licensed financial professional and only trade for myself, so if anyone follows this practice it in paper trade first.
 
Last edited:

TraderJacque

New member
Hello thinkscript forum,

I have a question about scripting. I am trying to write an order where one order triggers another order. I right click on the chart of XYZ and select OCO. I enter Stock, Buy, X(# of shares), XYZ(symbol), skip Exp,Strike Type and link, accept the limit price,choose LIMIT for Order, GTC for trade type, and BEST for the last entry.

Then , I click on the sprocket symbol and make sure the trade info is correctly pre-populated from the order entry previously entered. I right click under the symbol and enter XYZ . I right click under the method and select Study and then Edit. Here I use the condition Wizard to enter 2 conditions: RateofChange(5,14,close) < a11 and RateofChange(1,14,close) > b12. Where a11= 0,b12=0. This is a buy order(first event). The second event on the second line is basically the same except it is a sell order and RateofChange (5,14,close) > a21 and RateofChange(1,14,close) < b22. I tried executing about 40 of these auto trades and only about 2 worked. I believe I may have used the limit prices that were too high or should have used a STOP LIMIT on the second event(sell) . I would appreciate any input on your behalf.

Also, In my testing of scripts, I found out that if I use a wider aperture such as a11< 0.0 and b12 >-1.0 for the buy or open order my orders would succeed more often. Of course, this would mean that the aperture for the sell order(the next line down on the order ticket) would change to a21>0.0 and b22 <1.0 for the sell order. Please let me know what you think. Yes, I know I can increase the apertures sufficiently so that I get all of my auto trades to work but the purchase price might not be profitable. I am trying to buy on a dip and sell on a crest of a wave.
Thanks.
 

XeoNoX

Well-known member
VIP

rad14733

Moderator
Staff member
VIP
@TraderJacque As you have found, the use of Trigger and OCO Conditional Orders can be very difficult to fine-tune if there is any amount of complexity... I tried a complex strategy about a year ago and a TOS Chat Support assistant and I played for a couple of days, on and off, in an attempt to get it working but all I ended up doing was burning through a lot of trading fees at roughly $1.35 per round-trip trade, plus any losses per trade, with trades entering and exiting within seconds... Now if I use Conditional Orders I use the KISS concept...
 

xaman

New member
Hi,

I'm trying to set up an OCO Bracket for a stock.

1) Buy order
I want to set a conditional order on TOS to activate buying when the last done price is 5% above the low price.

a) I went into the Conditional Wizard and selected "Price" -> "Ask" (does not have "Last") -> "is greater than" -> "Low".
Then, there's a selection for "Trigger" when I exit the "Study Order Condition" but I don't understand what the numbers represent. Is it the actual price? I'm unable to find where to input the percentage value for the conditional order to trigger based on percentages.

b) How can I set the Price to "Last"? I tried to change it in the Thinkscript editor but it is not allowed.

c) I'm not sure which "Low" price is being used if GTC is chosen. For example, if I enter the conditional order on Day 1 and the low price is $1 but it does not trigger the buy conditional order because the price did not go up to 5% of the low price of $1. The next day, the low price drops to $0.90, will the conditional order calculate the percentage based on $1 or $0.90? I want the conditional order to follow the lowest price regardless of duration. So, if the Low price of $1 (Day 1) -> $0.90 (Day 2) -> $1 (Day 3), is it possible to get the conditional order to still use $0.90 as the Low price to calculate the conditional buy order?

2) Sell order
I want to set a conditional order to activate selling when the last done price is 5% below the high price.

I understand that I can use "TrailStop" to activate the sell condition. Is it correct if I set the Link to "Mark" and the Price to "-5%"?

3) Stop-loss order
I want to set a conditional order to activate stop-loss when the last done price is 3% below my buy price.

Is it correct if I set the Link to "Mark", the Price to "-5%" and the Order to "STD"?
I suppose "STD" means standard but how does it differ from "MARK"?


Thanks in advance for your help.
 

rad14733

Moderator
Staff member
VIP
@xaman First, welcome to the usethinkscript.com forums... You sure dove in deep for your first post here but not knowing your level of experience I'll try to help you along as best I can...

Let's address your question about Last... Unless you specify otherwise Thinkorswim will always assume Close to be the closing price of the previous bar for whatever Aggregation Period you have selected for your Conditional Order... However, just to confuse things further, in some situations Close is actually Last... And, unfortunately for us, that quirk is not well documented anywhere that I have found... How it is used in Conditional Orders is subjective at best...

I'll need to research the use of percentages as when I have used Conditional Orders I have used pricing based on my custom code entered into the editor... I'll use Moving Average Crossover and associated confirmations as an example... Therefore entry or exit price would either be code based or market price...

I think the best recommendation I can make is for you to take some time and do ample research in the Thinkscript Learning Center on the subject of Conditional Orders... There should be documentation as well as training videos... I would also suggest searching YouTube for videos on the subject as well... In reality, a very small percentage of Thinkorswim users actually use Conditional Orders or at least not of any complexity...

I hope some of this makes sense and I know it doesn't provide a working solution but with a bit more research I'm sure you'll get there... Good luck and, again, welcome to the forums...
 

xaman

New member
@xaman First, welcome to the usethinkscript.com forums... You sure dove in deep for your first post here but not knowing your level of experience I'll try to help you along as best I can...

Let's address your question about Last... Unless you specify otherwise Thinkorswim will always assume Close to be the closing price of the previous bar for whatever Aggregation Period you have selected for your Conditional Order... However, just to confuse things further, in some situations Close is actually Last... And, unfortunately for us, that quirk is not well documented anywhere that I have found... How it is used in Conditional Orders is subjective at best...

I'll need to research the use of percentages as when I have used Conditional Orders I have used pricing based on my custom code entered into the editor... I'll use Moving Average Crossover and associated confirmations as an example... Therefore entry or exit price would either be code based or market price...

I think the best recommendation I can make is for you to take some time and do ample research in the Thinkscript Learning Center on the subject of Conditional Orders... There should be documentation as well as training videos... I would also suggest searching YouTube for videos on the subject as well... In reality, a very small percentage of Thinkorswim users actually use Conditional Orders or at least not of any complexity...

I hope some of this makes sense and I know it doesn't provide a working solution but with a bit more research I'm sure you'll get there... Good luck and, again, welcome to the forums...
@rad14733, I didn't realize my query is difficult to achieve. I thought it is quite an easy condition as it is based purely on price and not on any indicators. I also thought it is because I'm not familiar with TOS so I could not configure it properly.

I'm a beginner in trading and coding. I tried reading the Thinkscript manual but it is too much info for me and I thought it will be easier to just fiddle with the Conditional Orders to achieve what I want. Is it easier to achieve the OCO Bracket conditions that I want using Thinkscript instead?
 

rad14733

Moderator
Staff member
VIP
@xaman Now that I've had my breakfast, let me take a deeper look into this for you... My brain may not have been firing on all cylinders when I first read your request... I'll be back in a bit...
 

rad14733

Moderator
Staff member
VIP
@xaman I need a bit more clarification... How are you determining what you are calling "last done price is 5% above the low price"...??? What Last, or Close, are you referring to, timeframe-wise...??? Are timeframe is the Chart you are using set to...??? I want to make sure we're both on the same page here... I watch multiple timeframes ranging from 30m down to 1m... Specifically, I watch 30m, 15m, 10m, 5m, 3m, and 1m... Are you watching intraday, daily, weekly, or other...???
 

xaman

New member
@xaman I need a bit more clarification... How are you determining what you are calling "last done price is 5% above the low price"...??? What Last, or Close, are you referring to, timeframe-wise...??? Are timeframe is the Chart you are using set to...??? I want to make sure we're both on the same page here... I watch multiple timeframes ranging from 30m down to 1m... Specifically, I watch 30m, 15m, 10m, 5m, 3m, and 1m... Are you watching intraday, daily, weekly, or other...???
Hmmm...won't the last done price be the same for all time frames? I'm not watching any specific time frames.

Example:
5Zvno5y.png


Now, I will manually find the lowest price in the 1Y/1D chart to find the lowest price, i.e. white line in the chart above. Then, I will multiply 1.05 to find the price I want to buy at and enter the order accordingly. For stop loss, it will be X * 1.05 * 0.97, which is 3% of the purchase price. For Trailing stop, I put it as -5%.

So, I will let the order run until it triggers, whether intraday or over multiple days. Hence, I do not have any specific timeframes to watch.

Edit: And the green line shows the price that I sold the shares because it has dropped 5% from the top. Then, I will use the following day's Low price at the yellow line to calculate the new purchase price, trailing stop and stop-loss. If everything happens within 1 day, I will look at the relevant timeframe (5m, 15m, 30m, 1h or 4h) to observe the price changes. If the price continues to fall, the conditional order will not be triggered and I will adjust my purchase price and stop-loss accordingly. So now, I'm wondering if it is possible to change the low price in the script to maintain the lowest price over the day(s)/week(s) (Edit 2: from the day I start the script) until the conditional order is triggered.
 
Last edited:

rad14733

Moderator
Staff member
VIP
@xaman The term "last done price" isn't an actual stock market term which makes it somewhat confusing and there is no way to determine such a term using Thinkscript... In trading we deal with timeframes and aggregation periods to calculate things like prices, moving average crossovers, squeezes, etc... Therein lies the issue... And because a Conditional Order isn't really based on a Chart we need to be very specific... You have to view this from a coding perspective rather than looking at a Chart in front of you perspective... We have to parse historic data...

I know you view it as simple, but it needs to be coded with exacting precision, otherwise you'll constantly lose money... I went through this last year with Conditional Orders that were misfiring and spent several days, on and off, with TOS Chat Support attempting to resolve the issue but we could never pinpoint the culprit... I was losing transaction fees and whatever losses with virtually instantaneous entries and exits... We never determined whether it was logic or TOS related and I ditched that trading scenario and moved on...

I'll work off the last paragraph in your previous post and see how that works in testing and report back... But I still need an aggregation period to go from to find the low... For now I'll use 1Y 1d... The aggregation period will definitely make a difference in which Low is used... High would be based off of entry price...

I'll also clarify that it isn't my practice to write code upon request... Modifications, yes... But we expect members to learn coding for themselves rather than ask others to write free code for them so they can profit from it... You're a new member so I'm giving you a "by", this time... I'll be back... ;)
 

xaman

New member
@xaman The term "last done price" isn't an actual stock market term which makes it somewhat confusing and there is no way to determine such a term using Thinkscript... In trading we deal with timeframes and aggregation periods to calculate things like prices, moving average crossovers, squeezes, etc... Therein lies the issue... And because a Conditional Order isn't really based on a Chart we need to be very specific... You have to view this from a coding perspective rather than looking at a Chart in front of you perspective... We have to parse historic data...

I know you view it as simple, but it needs to be coded with exacting precision, otherwise you'll constantly lose money... I went through this last year with Conditional Orders that were misfiring and spent several days, on and off, with TOS Chat Support attempting to resolve the issue but we could never pinpoint the culprit... I was losing transaction fees and whatever losses with virtually instantaneous entries and exits... We never determined whether it was logic or TOS related and I ditched that trading scenario and moved on...

I'll work off the last paragraph in your previous post and see how that works in testing and report back... But I still need an aggregation period to go from to find the low... For now I'll use 1Y 1d... The aggregation period will definitely make a difference in which Low is used... High would be based off of entry price...

I'll also clarify that it isn't my practice to write code upon request... Modifications, yes... But we expect members to learn coding for themselves rather than ask others to write free code for them so they can profit from it... You're a new member so I'm giving you a "by", this time... I'll be back... ;)
Thank you for your help. I was not expecting anyone to write code for me as I thought it can be easily solved by adding something extra to let it trigger based on percentages in the code: "ask() is greater than or equal to low", shown in the thinkscript editor.

I have been viewing videos on thinkscript and conditional orders but all of them use indicators and those look much more complicated than what I'm doing, so I did not know it is going to be require much more effort than expected even though on the chart it looks easy.

I really appreciate your help and I will definitely brush up on my thinkscript coding knowledge as I want to move on to a more robust algorithm in the future after I learn more about trading.
 

rad14733

Moderator
Staff member
VIP
@xaman Let's step back a bit... When you say Conditional Order are you referring to setting up an OCO Order or a true Conditional Order which requires clicking on the gear at the far right of the order entry panel...??? My reason for asking is that your logic won't work the way you described and I'll explain why... If you're on Discord we can chat about it rather than cluttering this topic...

But here's the problem... If we look back to find the lowest low of the past year, or 252 days, and wait for a 5% higher price, it may well be WAY lower than current prices... We'll use MRNA as an example... As of today the lowest low was $17.91... Adding 5% for an entry price results in $18.81... Todays trading range is around $177.27 and $187.00... So, as you can see, your logic won't work for obvious reasons... Now, in your image above you have a line at the first bar of the year which obviously isn't the lowest price on the 1Y chart... But considering the low of the first bar of the year we'd use $103.26 as our low for Year-To-Date, which requires more Thinkscript to extrapolate... So then our desired entry price would be $108.42... Again, WAY lower than current prices... I won't go further...

Essentially, your perceived logic is flawed from a coding point of view... I've invested far too much time already on what I originally presumed was flawed codable logic... If you can further clarify then perhaps I could assist further but as of now I can't waste additional time...
 
Last edited:

xaman

New member
@rad14733, I was trying to set up a conditional order by clicking on the far right gear icon.

It's easier if I explain the whole process.

When I go into any stock, I will look at the day's low. I don't care how it performed over the past days/weeks/years. I will just take the low price and multiply by 1.05. This will be my purchase price. If the price goes up by 5%, the order will be triggered. If the price falls, it will not be triggered. When I'm free, I will look at the stock price and if it goes lower, I will adjust my purchase price accordingly after some manual calculations. As I'm not free to monitor the price changes frequently and it is a chore to keep calculating the new purchase price, I wanted to use the condition wizard to automate the buying process.

Hence, I went into the gear icon to customize a study, selected "Price" -> "Ask" -> "is greater than or equal to" -> "Price" -> "Low". The thinkscript editor gave me the code "ask() is greater than or equal to low".

However, I could not find a location or option to put in the percentage price change. The "Trigger If" option in the condition wizard, below "Add condition", has no option to put in percentages. I tried selecting "At or above" -> "0.05" but nothing happens so I assume the number is for the actual price (please correct me if I'm wrong in this assumption).

Therefore, I posted in this forum asking for a way to include the percentage change in price in the conditional order. Maybe I can just add "by 5%" or something like that to the code: "ask() is greater than or equal to low".

Maybe it's the way I phrased the post that caused a misunderstanding. I hope this post will clarify what I'm trying to achieve with the condition wizard.
 

rad14733

Moderator
Staff member
VIP
@xaman Ok, that makes more sense... The missing piece of the puzzle that I'd been trying to pry out of you was the timeframe and only now did you say the days low... Your image showed 1Y 1d as the timeframe so that is what I was attempting to go off of, knowing that it couldn't possibly work...

If you want to use a percentage increase or decrease you have to calculate that price in your code... It will be very difficult to get what you want without adding some hand entered code... If Conditional Orders were that easy then a lot more people would be using them... There is no such thing as point-and-click everything... You'd want something like ask() is greater than or equal to low + (low * 0.05) in order to get the price 5% above the low... Or to completely hand code you could just use ask() >= low + (low * 0.05) and be done with it... Or ask() >= low * 1.05...

But there is still the problem of determining which low to use... The Conditional Order is always watching the current low unless we somehow specify otherwise... I still think your logic has a fundamental flaw... If the code was to even look for todays low up until you place the order it would use that but the price by the time your order is saved could change in either direction... It may never return to as low as your code calculates... So it's still a matter of the tail chasing the dog...
 
Last edited:

Similar threads

Top