Automatic line creation based on calculation possible?

badmf

New member
Hi everyone, thanks for taking the time to read. I think this may already be created/done but I'm not sure how to find it or how to create it..

I take the daily close value of a stock, 20% of the ATR value (7 days), then add/subtract from the close price to set a level for calls and puts. I currently do this on a spreadsheet, which I hate working with. I just move the line daily on stocks I regularly trade, or create it from scratch on new tickers. I would like this done automatically.

Example: GOOGL closed at 2375.69 today ATR was 4.22, 20% of that is 0.84.. I add that to the close and get 2376.53 then substract from the close and get 2374.84.. I put two lines on each of those levels and watch it.. Sometimes I even set alerts, so that would be a perk too.

Is there a way to do this automatically on ToS? Can someone direct me to some scripts I can read through and work with to figure it out. I script in other languages, if that's relevant.
 
Last edited:
Solution
Hi everyone, thanks for taking the time to read. I think this may already be created/done but I'm not sure how to find it or how to create it..

I take the daily close value of a stock, 20% of the ATR value (7 days), then add/subtract from the close price to set a level for calls and puts. I currently do this on a spreadsheet, which I hate working with. I just move the line daily on stocks I regularly trade, or create it from scratch on new tickers. I would like this done automatically.

Example: GOOGL closed at 2375.69 today ATR was 4.22, 20% of that is 0.84.. I add that to the close and get 2376.53 then substract from the close and get 2374.84.. I put two lines on each of those levels and watch it.. Sometimes I even set alerts, so...
Hi everyone, thanks for taking the time to read. I think this may already be created/done but I'm not sure how to find it or how to create it..

I take the daily close value of a stock, 20% of the ATR value (7 days), then add/subtract from the close price to set a level for calls and puts. I currently do this on a spreadsheet, which I hate working with. I just move the line daily on stocks I regularly trade, or create it from scratch on new tickers. I would like this done automatically.

Example: GOOGL closed at 2375.69 today ATR was 4.22, 20% of that is 0.84.. I add that to the close and get 2376.53 then substract from the close and get 2374.84.. I put two lines on each of those levels and watch it.. Sometimes I even set alerts, so that would be a perk too.

Is there a way to do this automatically on ToS? Can someone direct me to some scripts I can read through and work with to figure it out. I script in other languages, if that's relevant.

why do you say ATR of 7 days ?
if i am on a day chart , and ATR is length is 7, then the ATR is 87.
if i set the chart to 5 minutes, with ATR(7), then i get a value of 4.229

this creates 2 lines that matches what you mentioned above
it saves the close from the last bar of the day and the atr from the last bar of the day, and calculates 2 levels, that are drawn on the next day.

Code:
# https://usethinkscript.com/threads/finding-the-first-and-last-bar-of-the-day-in-thinkorswim.526/
# find last bar of day
def nan = Double.NaN;
def isRollover = GetYYYYMMDD() != GetYYYYMMDD()[1];
def beforeStart = GetTime() < RegularTradingStart(GetYYYYMMDD());
def afterEnd = GetTime() > RegularTradingEnd(GetYYYYMMDD());
def firstBarOfDay = if (beforeStart[1] == 1 and beforeStart == 0) or (isRollover and beforeStart == 0) then 1 else 0;
def lastBarOfDay = if
    (afterEnd[-1] == 1 and afterEnd == 0) or
    (isRollover[-1] and firstBarOfDay[-1])
    then 1
    else 0;


input agg = AggregationPeriod.DAY;
def dayclsa = close(period = agg)[0];

input atr_per = 20;
input atr_len = 7;

def atra = ATR(atr_len);

# if last bar then atr else keep
def dayclsb;
def atrb;
if lastbarofday then {
    dayclsb = dayclsa;
    atrb = atra;
} else {
    dayclsb = dayclsb[1];
    atrb = atrb[1];
}


def atr7per =  atr_per / 100 * atrb;

def hilevel = dayclsb + atr7per;
def lolevel = dayclsb - atr7per;

plot u = hilevel;
plot l = lolevel;

u.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
u.SetDefaultColor(Color.light_gray);
#u.setlineweight(1);
#u.hidebubble();

l.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
l.SetDefaultColor(Color.light_gray);
#l.setlineweight(1);
#l.hidebubble();
 
Last edited:
Solution

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

why do you say ATR of 7 days ?
if i am on a day chart , and ATR is length is 7, then the ATR is 87.
if i set the chart to 5 minutes, with ATR(7), then i get a value of 4.229

this creates 2 lines that matches what you mentioned above
it saves the close from the last bar of the day and the atr from the last bar of the day, and calculates 2 levels, that are drawn on the next day.

Code:
# https://usethinkscript.com/threads/finding-the-first-and-last-bar-of-the-day-in-thinkorswim.526/
# find last bar of day
def nan = Double.NaN;
def isRollover = GetYYYYMMDD() != GetYYYYMMDD()[1];
def beforeStart = GetTime() < RegularTradingStart(GetYYYYMMDD());
def afterEnd = GetTime() > RegularTradingEnd(GetYYYYMMDD());
def firstBarOfDay = if (beforeStart[1] == 1 and beforeStart == 0) or (isRollover and beforeStart == 0) then 1 else 0;
def lastBarOfDay = if
    (afterEnd[-1] == 1 and afterEnd == 0) or
    (isRollover[-1] and firstBarOfDay[-1])
    then 1
    else 0;


input agg = AggregationPeriod.DAY;
def dayclsa = close(period = agg)[0];

input atr_per = 20;
input atr_len = 7;

def atra = ATR(atr_len);

# if last bar then atr else keep
def dayclsb;
def atrb;
if lastbarofday then {
    dayclsb = dayclsa;
    atrb = atra;
} else {
    dayclsb = dayclsb[1];
    atrb = atrb[1];
}


def atr7per =  atr_per / 100 * atrb;

def hilevel = dayclsb + atr7per;
def lolevel = dayclsb - atr7per;

plot u = hilevel;
plot l = lolevel;

u.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
u.SetDefaultColor(Color.light_gray);
#u.setlineweight(1);
#u.hidebubble();

l.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
l.SetDefaultColor(Color.light_gray);
#l.setlineweight(1);
#l.hidebubble();
Thank you very much, this gives me something to work with.

In response to your ATR 7 question. It is because I want to capture an average of the prices range. Sometimes I use 5 days, or 10 days depending on the recent size of candles but I get this number from the Daily chart but the ATR indicator is set to 7 for length. I find 7 to work pretty well pretty often. Sometimes a stock will move very little but have a huge trend day that skews off the average so ATR needs to be played with. I use 20-30% of the ATR range (20% * value). This isn't an exact science it just lets me find a few levels to easily target for going long or short, often I manually adjust when I am looking at the chart on lower time frames to find a more precise significant level. But this keeps me out of using major pivots that would limit my ability to make profits. I trade mainly intraday, a long swing for me is 2-3 days at most and never over a weekend/holiday.

I compared the script to my manually created lines and something is not right. I'm going to try to explain what I'm trying to do automatically..

Market closed, stock closed at $10.00 today and the ATR for the day was $1.00 so the 20% of that would be .20 I would subtract that from the close and draw a line at $9.80 and I would take the .20 and add that to the close and draw a line at $10.20 this would be levels for tomorrow.

Thank you once again this is very helpful!
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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