# Auto draw ATR Lines after placing trade?

#### Learnbot

##### Active member
I am not sure if this is possible but what I am trying to see if there is a way in which TOS automatically draws two lines when I place a buy order. so here are the details:

Let's say I placed and filled a market buy order for 1 stock of TSLA at \$1000 at which point the ATR is 1.0. What I am trying to see (if possible) two lines get drawn at this time one stop loss line valued at 1000 - (2*ATR) and profit line valued at 1000+(4*ATR).

i dont think it's possible but maybe some of you smarter individuals than I, know more about me and maybe there is a way...

thank you for your help and time as always.
J

Last edited by a moderator:

#### Miket

##### Member
Does anyone have a script where 50% of the 10 day ATR is automatically drawn on your chart?

#### BenTen

Staff member
Staff
VIP
@Miket Don't know if this is correct but give it a try.

Code:
``````input aggregationPeriod = AggregationPeriod.DAY;
def open = open(period = aggregationPeriod);
def high = high(period = aggregationPeriod);
def low = low(period = aggregationPeriod);
def close = close(period = aggregationPeriod);

input length = 10;
input averageType = AverageType.WILDERS;

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

plot line = open + (ATR / 2);``````

The blue line represent the 50% of the 10 days ATR.

#### Miket

##### Member
It works thank you. How can I also make it calculate minus the 50% atr?

#### scott69

##### Active member
@Learnbot I have thought about doing what you want with an ATR band for stops, but what I do is I have a 1 min chart with the ATR(5) running. I make the ATR indicator area as small as possible, so that all I can really see is the ATR level bubble. I only want to see that number all day. When I go to put on a position based on indicators, price pattern, S/R levels, etc. (you guys know the drill, ) I will look at the ATR # and then set my initial stop as a multiple of that, usually X2. (As an aside, I am somewhat of a disciple of Chuck LeBeau). So if it says 6, for example, I will set the Trail stop at 15 (2 X 6 rounded up).After the trade is going, I will flatten the positions prior to the trail getting hit if price runs into resistance or some other indicator that tells me that the position is running into trouble. I also use the ATR # to keep me out of trades. i.e. if it is 30+, that tells me that the action is too rich for my blood, meaning 2 X 30 would be a 60 point stop. No thank you. Even with one NQ contract, that would be a \$1200 stop at one level. So that is my deux centimes about ATRs. To each his/her own. Whatever works. Hope it gives you all something to think about. Talking about stops could take several books full, as you all know....

#### BenTen

Staff member
Staff
VIP
@Mike Take a look at the code above, pay attention to the following:

Code:
``plot line = open + (ATR / 2);``

You can change + to -

#### Miket

##### Member
Yep, I figured it out messing around. Thanks for the help

#### TyDollaSign31

##### New member
Good evening,

I was wondering if there is a function in thinkscipt that will determine weather you have a position in XYZ ticker, and if so draw a line that represents your stop-loss target price automatically. so if i buy 5 contracts of XYZ, it will find the high/low of the last 5 min period (arbitrary time frame) and will put a price level line at the high/low of that time frame, representing your stop.

#### Slippage

##### Well-known member
This will plot your average cost. You can add more plots with some math/logic to draw your stops and/or targets. You can look back at previous trades on your charts while testing this and it should plot where you had positions.
Ruby:
``plot AveragePrice = if GetQuantity() != 0 then GetAveragePrice() else Double.NaN;``

#### XeoNoX

##### Well-known member
VIP
Good evening,

I was wondering if there is a function in thinkscipt that will determine weather you have a position in XYZ ticker, and if so draw a line that represents your stop-loss target price automatically. so if i buy 5 contracts of XYZ, it will find the high/low of the last 5 min period (arbitrary time frame) and will put a price level line at the high/low of that time frame, representing your stop.

yes it can be done, look at this

#### XeoNoX

##### Well-known member
VIP
you can try this, i didnt test it but you just have to combine the codes similar to something like this:

Code:
``````def plBuy = if GetQuantity() != 0 then GetAveragePrice() else Double.NaN;

plot x=plStraightLine;
x.SetDefaultColor(Color.RED);
x.SetLineWeight(3);

# pl1 is Price Level 10%
plot pl1 = x * 1.10;
pl1.SetDefaultColor(Color.GREEN);
pl1.SetLineWeight(3);

#### TyDollaSign31

##### New member
Thanks for the responses everyone, much appreciated!

Does the GetQuanitity() function work only for commons or for option positions as well? i guess that would return greater than 1 regardless of if its commons or options?

@XeoNoX thank you so much for getting back to me. I'm a computer science student (Senior) I know java, I know thinkscript is derived from java, what do you recommend the best way to become familiar with thinkscript is? any youtube channels you'd recommend?

Staff member
Staff
VIP

#### XeoNoX

##### Well-known member
VIP
Thanks for the responses everyone, much appreciated!

Does the GetQuanitity() function work only for commons or for option positions as well? i guess that would return greater than 1 regardless of if its commons or options?

@XeoNoX thank you so much for getting back to me. I'm a computer science student (Senior) I know java, I know thinkscript is derived from java, what do you recommend the best way to become familiar with thinkscript is? any youtube channels you'd recommend?
just playing/attempting to write the scripts, using the thinkscript website and other peoples works for examples and then piecing it together. the longer you take trying to figure things out and then figuring it out, the more you learn. I accidently learned it over time and asking few questions and examples here and there, on the slow market days i just would tend to mess with it out of boredom to make my day go by faster and next thing i knew i just happened to learn it.

#### TyDollaSign31

##### New member
you can try this, i didnt test it but you just have to combine the codes similar to something like this:

Code:
``````def plBuy = if GetQuantity() != 0 then GetAveragePrice() else Double.NaN;

plot x=plStraightLine;
x.SetDefaultColor(Color.RED);
x.SetLineWeight(3);

# pl1 is Price Level 10%
plot pl1 = x * 1.10;
pl1.SetDefaultColor(Color.GREEN);
pl1.SetLineWeight(3);

Thank you!

#### Ninja Bull

##### Member
Hello everyone, I found these ATR Lines by Mobius. They work very well depending on the length you choose and time frame. You will have to adjust the ATR multiplier for the underlying in question.

Here is an ask for the coders of the forum: can you have the study below calculate the ATR for the underlying automatically and that way the ATR multiplier adjusts itself.

I think that by automating the step above, you will only have to worry about the time frame and length.

Here is the code below

Code:
``````# 08:47 Mobius: bennet -

# ATR Lines

# Mobius

# Chat Room Request 11.05.2018

input n = 20;

input ATRmultiplier = 1.5;

def avg = Average(close, n);

def TR = TrueRange(high, close, low);

def ATR = Average(TR, n);

plot upper = HighestAll(if isNaN(close[-1])

then avg + (ATRmultiplier * ATR)

else double.nan);

plot lower = HighestAll(if isNaN(close[-1])

then avg - (ATRmultiplier * ATR)

else double.nan);``````

Last edited:

## 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?