# Stop Loss Conditional Order on Bid For ThinkOrSwim

#### JoeBloke

##### New member
Hi All,

Can you please review my script below to submit/trigger a conditional order when the Option's current price (mark price) is 15% below the high price. The intention is to mimic a Trailing Stop Order. I want to go this scripting route because ThinkorSwim rejects an OCO order that contains a Stop Order and a Trailing Stop Order.

I want to attach the script as a Study (1 minute chart) in the Order Rules applet:

def highPrice = if high > high[1] then high else high[1];
plot signal = if (bid + ask)/2 <= (highPrice * 0.85) then 1 else 0;

Please note I want to use this for day trading. This is not for back testing.

Will the above script work, or can you suggest something better?

Thanks!

@JoeBloke
So you want a stop limit if the price falls 15% from the high of any candle? This does that:
(bid + ask)/2 <= (high * 0.85)

Last edited:
@JoeBloke
So you want a stop limit if the price falls 15%? Where price is defined as the high of any candle?
Yes I want to submit a Stop order when price falls 15%.

However, price is defined as the highest price after I entered the trade.

I hope the example below will make things clearer:
• I entered the trade at 9:31:00 AM, and the high for that 1 minute candle is 100, therefore my high price is 100. After hitting 100, the price didn't fall 15% for that candle.
• For the 9:32 AM candle, the high is 95. My high price remains 100
• For the 9:33 AM candle, the high is 105. However, my high price changes to 105
As you can see above, I define price as my high price, not just the high of the current 1 minute candle. I will like the code to monitor and store my high price, and trigger the conditional order when the option's mark price falls 15% of my high price.

Thanks!

Yes I want to submit a Stop order when price falls 15%.

However, price is defined as the highest price after I entered the trade.

I hope the example below will make things clearer:
• I entered the trade at 9:31:00 AM, and the high for that 1 minute candle is 100, therefore my high price is 100. After hitting 100, the price didn't fall 15% for that candle.
• For the 9:32 AM candle, the high is 95. My high price remains 100
• For the 9:33 AM candle, the high is 105. However, my high price changes to 105
As you can see above, I define price as my high price, not just the high of the current 1 minute candle. I will like the code to monitor and store my high price, and trigger the conditional order when the option's mark price falls 15% of my high price.

Thanks!
Ahhhh.... I see says the blind man... That is where the trailing part of the equation come in.
Next question... Is this just the trailing high for that day. Are you out of the trade by the end of the day?
PS: option's mark price is not available so let's stick w/ your original formula: (bid + ask)/2

Ahhhh.... I see says the blind man... That is where the trailing part of the equation come in.
Next question... Is this just the trailing high for that day. Are you out of the trade by the end of the day?
PS: option's mark price is not available so let's stick w/ your original formula: (bid + ask)/2
Thanks for your reply. I'm out by the end of the day.

Hi All,

I have a conditional Trailing Stop order that I want submitted when the current bid price is 10% higher than the opening price of the option at 9:31:00 AM - the time I bought the option.

I added thinkscript as a study to the Trailing Stop order conditions, but the order status still remains in "WAIT COND" even though the condition has been satisfied - the current option price is now over 20% of the price I bought the option (at 9:31:00 AM).

I set the Study Order Condition to 1 Minute candle, unchecked the "Include Extended-Hours Trading session" checkbox, and set the "Trigger If" to "True"

Below is the script:

Code:
``````#For the 1 minute candle, the 2nd bar is formed at 9:31:00 AM. I'm using this as a proxy for EntryPrice(), because EntryPrice() doesn't work for buy/sell orders
def myEntry = if BarNumber() == 2 then open else myEntry[1];
plot _data_ = bid() >= (myEntry * 1.1);``````

Can anyone let me know why the order remains in "WAIT COND" even though the condition has been satisfied?

Thanks.

Silly question. What is the time frame on your option chart? Is it today 1m ? I am asking because your logic is based on barnumber and that changes based on timeframe.

Silly question. What is the time frame on your option chart? Is it today 1m ? I am asking because your logic is based on barnumber and that changes based on timeframe.
Thanks for your response. The time frame on the chart is today 1 minute.

I've been troubleshooting, and I've found a better way of getting the option's open price at 9:31 AM without the use of BarNumber. I used the code below:
Code:
``````def t = SecondsFromTime(930) > 0 and SecondsFromTime(932) < 0;
def myEntryPrice = if t then open else myEntryPrice[1];``````

To test if the value returned for myEntryPrice is correct, I created a label using the code below:
Code:
``Addlabel(yes, "My Entry price is = " + myEntryPrice , color.WHITE);``

Now I need to test if the code "bid()" gives the real-time bids or not. I don't yet know how to test that.

@JoeBloke Here is some syntax that might point you in the right direction:
Ruby:
``````def bid = close(priceType = PriceType.BID);

AddLabel(yes, "Bid: " + Round(bid, 2), Color.GRAY);

@JoeBloke
- even when my option chart was set to today 1 M, the condition preview in the study actually showed me yesterday and today's option price chart (a lot more data than expected)
- I wonder if this behaviour changes when the market is open
- If this is the case, you may need to restrict based on both time and also date
- In scans and other areas, TOS uses its own time length for aggregation and I wonder if this is similar to where your selected chart time frame has no bearing and TOS picks its own choice.

Hope this is not a red herring. Please keep us posted on what you find. I will try to run this during market time tomorrow.

You may want to check SecondsFromTime logic. If the method returns the number of seconds, wouldn't you need a specific second rather than the 0931 minute? I will reread your code when near a computer.

@JoeBloke Here is some syntax that might point you in the right direction:
Ruby:
``````def bid = close(priceType = PriceType.BID);

AddLabel(yes, "Bid: " + Round(bid, 2), Color.GRAY);
Thanks @MerryDay, appreciate it! I'll try this tomorrow and will keep you posted.

@JoeBloke
- even when my option chart was set to today 1 M, the condition preview in the study actually showed me yesterday and today's option price chart (a lot more data than expected)
- I wonder if this behaviour changes when the market is open
- If this is the case, you may need to restrict based on both time and also date
- In scans and other areas, TOS uses its own time length for aggregation and I wonder if this is similar to where your selected chart time frame has no bearing and TOS picks its own choice.

Hope this is not a red herring. Please keep us posted on what you find. I will try to run this during market time tomorrow.

You may want to check SecondsFromTime logic. If the method returns the number of seconds, wouldn't you need a specific second rather than the 0931 minute? I will reread your code when near a computer.
The code gave me exactly what I needed i.e. the open price at 9:31 AM of the current day.

I confirmed this by using the AddLabel function to display the result

87k+ Posts
363 Online

## The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
• Exclusive indicators
• Proven strategies & setups
• Private Discord community
• Exclusive members-only content
• 1 full year of unlimited support

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?