Horizontal Lines In ThinkOrSwim

How do I plot a straight horizontal line to be constantly changing and is always 25% above the last trade price so I could know where I’m taking profit if I only want to make 25% a trade, or have it instantly plotted whenever I place my buy order
 

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

This is a script that I created to have horizontal lines at multiples of an ATR above and below the moving price. I use it for stops, or just to visualise the volatility of the price action. You should be able to modify the ATR lines to be percentages or what ever you want. I use this in conjunction with the "current price line" which can be found here by searching.

( also please note that if you have these lines constantly plotting at 25% above the current price - always moving - you will theoretically never hit that price for you to sell, for that you should program a horizontal line at a fixed percentage "above or below" your initial entry price. )

Code:
##### Begin Code #####

# SD_ATR_Lines
# Scott Da####
# Version: 01
# Original: January 16, 2021
# Last Mod: January 16, 2021

declare upper;

input aggregationPeriod = AggregationPeriod.MIN;
input price=close;
input ATRValue = 5;
input averageType = AverageType.WILDERS;
input StopFactor = 2;
input offset=0;
input LineLength = 10;
input ShowLabel = yes;

def open = open(period = aggregationPeriod);
def high = high(period = aggregationPeriod);
def low = low(period = aggregationPeriod);
def close = close(period = aggregationPeriod);

def ATR = MovingAverage(averageType, TrueRange(high, close, low), ATRValue);

# Top Line
def sma1 = SimpleMovingAvg((Close + (ATR*StopFactor)), 1, LineLength);
rec line1 = if IsNaN(sma1) then line1[1] else sma1[offset];
plot ATRLine1=if isnan(sma1) then line1 else double.nan;
ATRLine1.setpaintingStrategy(paintingStrategy.LINE);
ATRLine1.setlineWeight(1);
ATRLine1.setdefaultColor(color.yellow);
ATRLine1.hideBubble();

# Bottom Line
def sma2 = SimpleMovingAvg((Close - (ATR*StopFactor)), 1, LineLength);
rec line2 = if IsNaN(sma2) then line2[1] else sma2[offset];
plot ATRLine2=if isnan(sma2) then line2 else double.nan;
ATRLine2.setpaintingStrategy(paintingStrategy.LINE);
ATRLine2.setlineWeight(1);
ATRLine2.setdefaultColor(color.yellow);
ATRLine2.hideBubble();

##### End Code #####
 
This is a script that I created to have horizontal lines at multiples of an ATR above and below the moving price. I use it for stops, or just to visualise the volatility of the price action. You should be able to modify the ATR lines to be percentages or what ever you want. I use this in conjunction with the "current price line" which can be found here by searching.

( also please note that if you have these lines constantly plotting at 25% above the current price - always moving - you will theoretically never hit that price for you to sell, for that you should program a horizontal line at a fixed percentage "above or below" your initial entry price. )

Code:
##### Begin Code #####

# SD_ATR_Lines
# Scott Da####
# Version: 01
# Original: January 16, 2021
# Last Mod: January 16, 2021

declare upper;

input aggregationPeriod = AggregationPeriod.MIN;
input price=close;
input ATRValue = 5;
input averageType = AverageType.WILDERS;
input StopFactor = 2;
input offset=0;
input LineLength = 10;
input ShowLabel = yes;

def open = open(period = aggregationPeriod);
def high = high(period = aggregationPeriod);
def low = low(period = aggregationPeriod);
def close = close(period = aggregationPeriod);

def ATR = MovingAverage(averageType, TrueRange(high, close, low), ATRValue);

# Top Line
def sma1 = SimpleMovingAvg((Close + (ATR*StopFactor)), 1, LineLength);
rec line1 = if IsNaN(sma1) then line1[1] else sma1[offset];
plot ATRLine1=if isnan(sma1) then line1 else double.nan;
ATRLine1.setpaintingStrategy(paintingStrategy.LINE);
ATRLine1.setlineWeight(1);
ATRLine1.setdefaultColor(color.yellow);
ATRLine1.hideBubble();

# Bottom Line
def sma2 = SimpleMovingAvg((Close - (ATR*StopFactor)), 1, LineLength);
rec line2 = if IsNaN(sma2) then line2[1] else sma2[offset];
plot ATRLine2=if isnan(sma2) then line2 else double.nan;
ATRLine2.setpaintingStrategy(paintingStrategy.LINE);
ATRLine2.setlineWeight(1);
ATRLine2.setdefaultColor(color.yellow);
ATRLine2.hideBubble();

##### End Code #####
Ok thank you! I’ll play around with this code and see if I can get it
 
It sounds like you're executing trades on TOS which means you can use GetAveragePrice() and then calculate a target based on that and plot a line.

However, you could also just place a limit order so you'll take profit at that target automatically and not need a line on the chart. The order being visible on the chart would be the line.
 
@Slippage great point, I was thinking the same thing, i.e. if you place an order with a stop (at whatever limit, price, percent) then the line would be drawn on the chart as a standing order and would be executed when hit. Probably the simplest way to do it. Thanks
 
You're right that sounds easier how can I do that, can I use the add order script to automatically place a limit order 25% above my entry price?
 
You're right that sounds easier how can I do that, can I use the add order script to automatically place a limit order 25% above my entry price?

You are aware that AddOrder() is only for backtesting within Strategies, right...??? Thinkorswim doesn't have auto-trading... The closest you can come to that is using Conditional Orders in the Order Entry panel...
 
You're right that sounds easier how can I do that, can I use the add order script to automatically place a limit order 25% above my entry price?

Unfortunately, AddOrder() doesn't create real orders. It's only useful for back-testing. But this is very simple anyway.

The quickest/easiest way I know of to do what you want is via the Active Trader window's templates. Active Trader is one of the tabs on the side of your charts. Once you open it, click the > icon to the left of the buttons to see more features.
ctive-Trader-490509867-Main-thinkorswim-build-1969.png


With the TRG w/ bracket template selected I clicked where you see the % signs in order to make those %. It defaults to a +/- $ offset. Then I changed the LIMIT and STOP prices to +25 and -25. If I enter a position from this Active Trader window with these settings, whether I click a button like Buy MKT or click a price in the price ladder to enter with a STOP or LIMIT, when my entry order is filled I will automatically already have target and stoploss orders in the market waiting to fill. If one of them executes the other will automatically cancel. If you really only want the target order you can uncheck the box on the left of the STOP order.
 
@viswanarahari Try something like this

Code:
AddLabel(yes, GetSymbol());

plot p1;
plot p2;
plot p3;
plot p4;

def nan = double.NaN;


if (GetSymbol() == "ZM") then {
p1 = 100;
P2 = 207;
P3 = 319;
P4 = 429;
} else if (GetSymbol() == "AAPL") then {
p1 = 100;
p2 = 250;
p3 = 275;
p4 = nan;
} else {
p1 = nan;
p2 = nan;
p3 = nan;
p4 = nan;}

@viswanarahari Try something like this

Code:
AddLabel(yes, GetSymbol());

plot p1;
plot p2;
plot p3;
plot p4;

def nan = double.NaN;


if (GetSymbol() == "ZM") then {
p1 = 100;
P2 = 207;
P3 = 319;
P4 = 429;
} else if (GetSymbol() == "AAPL") then {
p1 = 100;
p2 = 250;
p3 = 275;
p4 = nan;
} else {
p1 = nan;
p2 = nan;
p3 = nan;
p4 = nan;}
Thank you for sharing this code. Is there a way to create alert so that every time when that specific ticker price will cross any of the level , there will be an alert?
 
@viswanarahari Just to clarify, Thinkorswim/Thinkscript does not support arrays at this time...

If you feel that it is something worthy of adding you can contact support and make a request... If they get enough requests they might consider the addition for a future release but it would be off in the future as it would need to be fully implemented and tested...
Thanks for the reply. Not sure I thnked you for this.
 
Is there a script to make a price line at 50% of the previous candle's range. (from top of the wick to bottom of the wick)

This should be what you are requesting

Ruby:
input daysback       = 1;
plot close_priordays = HighestAll(if IsNaN(close[-1]) and !IsNaN(close)
                                  then hl2(period = AggregationPeriod.DAY)[daysback]
                                  else Double.NaN);
 
This should be what you are requesting

Ruby:
input daysback       = 1;
plot close_priordays = HighestAll(if IsNaN(close[-1]) and !IsNaN(close)
                                  then hl2(period = AggregationPeriod.DAY)[daysback]
                                  else Double.NaN);
Thank you SleepyZ. that worked. Trying to figure out how to customize the input to also put it on lower timeframes.
 
Thank you SleepyZ. that worked. Trying to figure out how to customize the input to also put it on lower timeframes.

Try this

Ruby:
input agg            = AggregationPeriod.DAY;
input price          = FundamentalType.HL2;
input barsback       = 1;
plot close_priordays = HighestAll(if IsNaN(close[-1]) and !IsNaN(close)
                                  then Fundamental(price, period = agg)[barsback]
                                  else Double.NaN);
 
Try this

Ruby:
input agg            = AggregationPeriod.DAY;
input price          = FundamentalType.HL2;
input barsback       = 1;
plot close_priordays = HighestAll(if IsNaN(close[-1]) and !IsNaN(close)
                                  then Fundamental(price, period = agg)[barsback]
                                  else Double.NaN);
Yes. thank you. are you also able to make it stay within the 1 bar rather than the full chart. i tried input length or displace -1 but it doesn't work
 
Yes. thank you. are you also able to make it stay within the 1 bar rather than the full chart. i tried input length or displace -1 but it doesn't work

This has options to plot just on the next bar or across the chart

Ruby:
input agg = aggregationPeriod.FIFTEEN_MIN;
plot Data = if isnan(close[-1]) and !isnan(close)
            then  hl2(period=agg)[1]
            else double.nan;
data.setdefaultColor(color.blue);
data.setpaintingStrategy(paintingStrategy.HORIZONTAL);
data.setlineWeight(5);
 
Last edited:
This has options to plot just on the next bar or across the chart

Decided to make it simpler and works better. I have replaced that version with the following. It only plots on the last bar the hl2[1] of the aggregation input.

Ruby:
input agg = aggregationPeriod.FIFTEEN_MIN;
plot Data = if isnan(close[-1]) and !isnan(close)
            then  hl2(period=agg)[1]
            else double.nan;
data.setdefaultColor(color.blue);
data.setpaintingStrategy(paintingStrategy.HORIZONTAL);
data.setlineWeight(5);
 
Last edited:
That one isn't working. it doesn't plot anything
Sorry, needed to define horizontal line and line weight
Ruby:
input agg = aggregationPeriod.FIFTEEN_MIN;
plot Data = if isnan(close[-1]) and !isnan(close)
            then  hl2(period=agg)[1]
            else double.nan;
data.setdefaultColor(color.blue);
data.setpaintingStrategy(paintingStrategy.HORIZONTAL);
data.setlineWeight(5);
Capture.jpg
 
Last edited:
That one isn't working. it doesn't plot anything

I have a feeling I was confused about what you wanted and made it more complicated than needed from the start. If you just want to plot 50% of the prior bar onto the last bar, both at the same chart aggregation, then this should work.

Ruby:
plot Data = if isnan(close[-1]) and !isnan(close)
            then  hl2[1]
            else double.nan;
data.setdefaultColor(color.blue);
data.setpaintingStrategy(paintingStrategy.HORIZONTAL);
data.setlineWeight(5);
Capture.jpg
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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