Trailing Stop Based On Highs or Lows For ThinkOrSwim

This Indicator was born from the discussion here:
https://usethinkscript.com/threads/highest-indicator-value-coding-help.10707/
iE6jXBt.png


Here's the complete code:
Code:
# Trailing Stop based on highs or lows
# @tradecombine 4/17/22
# requested by @lishuimu88888888
# Reference: https://usethinkscript.com/threads/highest-indicator-value-coding-help.10707/
input length = 3;
input price = close;

def maHigh = highest(high, length);
def maLow = lowest(low, length);
def bp = if maHigh < maHigh[1] then maHigh else bp[1];
def sp = if maLow > maLow[1] then maLow else sp[1];

def state = {default init, short, long};
if (price > bp) {
state = state.long;
} else if (price < sp) {
state = state.short;
} else {
state = state.init;
}

plot BuyStop = if state == state.short && maHigh <= maHigh[1] then maHigh else Double.NaN;
plot SellStop = if state == state.long && maLow >= maLow[1] then maLow else Double.NaN;
BuyStop.SetDefaultColor(GetColor(0));
SellStop.SetDefaultColor(GetColor(1));
 
Last edited by a moderator:
Hello @tradecombine. Thanks for the entire code all put together and all your work here! Assumed the above code is for a ToS strategy. Is there a way to use it in an order? For instance, using a sell "Market Order" with the code that triggering to sell when the price hits the low price (for long and high for short) value in the code? Did I state that well enough to understand? If not, let me know and I can re-word. I have tried placing it in an order (similar to firing code based on an indicator or strategy) but I appear to be missing something. Any suggestions?
 
Last edited by a moderator:
Hello @tradecombine. Thanks for the entire code all put together and all your work here! Assumed the above code is for a ToS strategy. Is there a way to use it in an order? For instance, using a sell "Market Order" with the code that triggering to sell when the price hits the low price (for long and high for short) value in the code? Did I state that well enough to understand? If not, let me know and I can re-word. I have tried placing it in an order (similar to firing code based on an indicator or strategy) but I appear to be missing something. Any suggestions?
The issue with attempting to design your signals is that we need to test if a line develops. A line is defined as having two points, which means we have to wait at least two bars for a line to be established. So it isn't possible to design any signals accurately that didn't include significant lag. :(
 

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
435 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