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

https://usethinkscript.com/threads/auto-trade-algo-in-tos.7546/#post-32123
post6

https://usethinkscript.com/threads/thinkorswim-first-triggers-oco-order.2880/

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

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
 
Solution
i read this reply as, convert the study to strategy to test the studies i had?

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

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
449 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

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?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top