# calculate difference between today and specific previous date

#### abotoni

##### New member
would like to ask for help creating a study that calculates the difference between last price and the closing price on a certain previous date like 05/13/2021

Not a script, but the trend line drawing tool will do that for you. Make sure in the settings that "snap drawings to" is set to OHLC, then on the chart middle mouse click or choose drawing tools from chart menu, choose the trend line tool -> click on the two points of interest. You will get time, number of bars, move points, move percent, angle of line. Please note these before clicking on the second point, as this data disappears. I use this all the time to get points and percent of moves.

Thanks. That helps for one stock at a time but I want to use it as a scanning tool.

Thanks. That helps for one stock at a time but I want to use it as a scanning tool.

See if this helps. The label code is only included for debugging and needs to be removed for scanning.

Code:
input date      = 20210513;
def prior_close = if GetYYYYMMDD() == date
then close
else prior_close[1];
def close_diff  = close - prior_close;
plot scan       = close_diff > 0;;

input showlabel = yes;
AddLabel(showlabel,  "Today Close: " + AsText(close)
+ "  " + AsPrice(date) + " Close: " + AsText(prior_close)
+ "  Diff: " + AsText(close_diff),
if close_diff >= 0
then Color.LIGHT_GREEN
else Color.PINK);

See if this helps. The label code is only included for debugging and needs to be removed for scanning.

Code:
input date      = 20210513;
def prior_close = if GetYYYYMMDD() == date
then close
else prior_close[1];
def close_diff  = close - prior_close;
plot scan       = close_diff > 0;;

input showlabel = yes;
AddLabel(showlabel,  "Today Close: " + AsText(close)
+ "  " + AsPrice(date) + " Close: " + AsText(prior_close)
+ "  Diff: " + AsText(close_diff),
if close_diff >= 0
then Color.LIGHT_GREEN
else Color.PINK);
works like magic! thanks

87k+ Posts
277 Online

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