# How to create a "nested" conditional order for profit taking and stop loss at specific conditions

#### missingt

##### New member
What I would like to achieve:

• Enter a call/put position when a specific price condition is met on the underlying stock
• Create a OCO when position is opened
• If market price of contract is -10% of entry price, close the position
• If market price of contract is +10% of entry price, close 10% of position
• If position is still open, create a OCO
• If market price of contract is at entry price, close the position
• If market price of contract is at +30% of position, close 20% of position
• If position is still open
• create a trailing stop loss at -20% of current price
Example:

AAPL below \$100 buy 10 contracts of [defined option] for \$1.
• If value is \$0.90 close position
• If value is \$1.10 sell 1 contract
If position is still open:
• If value is \$1 close position
• If value is \$1.3 sell 2 contracts
If position is still open
• Set a trailing stop loss for -20% of current value(\$1.3)

My current attempt is via a saved 1st trigger 3-OCOs template w/custom studies.

1. Buy order w underlying price conditional
2. first OCO
1. stop limit at -10% of avg price, sell order of 100%
2. stop limit +10% of avg price, sell order of -10%
3. second OCO
1. stop limit at average price, sell order of 100% w/custom study
2. stop limit at +30%, sell order of 20% w/custom study
Code:
``````def quan = GetQuantity();
plot "e10" = quan > 0 and EntryPrice() * 1.1;``````
4. third OCO
1. trailing stop at -20% w/custom study
2. sell order at breakeven (should be unreachable) w/custom study
Code:
``````def quan = GetQuantity();
plot "e10" = quan > 0 and EntryPrice() * 1.3;``````

I'm still learning the thinkscript, but it seems like my values/functions just don't work in OCOs, i can't seem to nail down the logic on how to write it. Any guidance would be super helpful!

Would it just be best to use the API...?

Solution
What I would like to achieve:

• Enter a call/put position when a specific price condition is met on the underlying stock
• Create a OCO when position is opened
• If market price of contract is -10% of entry price, close the position
• If market price of contract is +10% of entry price, close 10% of position
• If position is still open, create a OCO
• If market price of contract is at entry price, close the position
• If market price of contract is at +30% of position, close 20% of position
• If position is still open
• create a trailing stop loss at -20% of current price
Example:

AAPL below \$100 buy 10 contracts of [defined option] for \$1.
• If value is \$0.90 close position
• If value...
What I would like to achieve:

• Enter a call/put position when a specific price condition is met on the underlying stock
• Create a OCO when position is opened
• If market price of contract is -10% of entry price, close the position
• If market price of contract is +10% of entry price, close 10% of position
• If position is still open, create a OCO
• If market price of contract is at entry price, close the position
• If market price of contract is at +30% of position, close 20% of position
• If position is still open
• create a trailing stop loss at -20% of current price
Example:

AAPL below \$100 buy 10 contracts of [defined option] for \$1.
• If value is \$0.90 close position
• If value is \$1.10 sell 1 contract
If position is still open:
• If value is \$1 close position
• If value is \$1.3 sell 2 contracts
If position is still open
• Set a trailing stop loss for -20% of current value(\$1.3)

My current attempt is via a saved 1st trigger 3-OCOs template w/custom studies.

1. Buy order w underlying price conditional
2. first OCO
1. stop limit at -10% of avg price, sell order of 100%
2. stop limit +10% of avg price, sell order of -10%
3. second OCO
1. stop limit at average price, sell order of 100% w/custom study
2. stop limit at +30%, sell order of 20% w/custom study
Code:
``````def quan = GetQuantity();
plot "e10" = quan > 0 and EntryPrice() * 1.1;``````
4. third OCO
1. trailing stop at -20% w/custom study
2. sell order at breakeven (should be unreachable) w/custom study
Code:
``````def quan = GetQuantity();
plot "e10" = quan > 0 and EntryPrice() * 1.3;``````

I'm still learning the thinkscript, but it seems like my values/functions just don't work in OCOs, i can't seem to nail down the logic on how to write it. Any guidance would be super helpful!

Would it just be best to use the API...?

oco code might not be able to read portfolio data?
https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Portfolio

------------------

these posts might help

post6

------------

i would create a strategy to test your rules and see how it works out. a strategy is a study, with addorder() functions, for simulating buying and selling.

----------

i haven't used the api, so don't know

this is a discord server, talking about the td api
name: tda-api
https://discord.gg/n9nMD5kt

make the studies as conditionals with executable blocks that return the expected order i hoped to achieve via custom study

87k+ Posts
449 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?