# Horizontal line

#### PennyFlipper101

##### New member
VIP
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

#### scott69

##### Member
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 #####``````

#### PennyFlipper101

##### New member
VIP
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

#### Slippage

##### Active member
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.

#### scott69

##### Member
@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

#### PennyFlipper101

##### New member
VIP
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?

##### Well-known member
VIP
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...

#### Slippage

##### Active member
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.

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.