Calculate Price % from pre-Covid crash

tw44

New member
In looking through scans and watchlists under the current bearish market conditions, I would like to know;

1) what % below the 52 week high is the current price (I've figured that out) and,
2) what % above the pre-Covid market crash is the current price. Rationale: as the Fed drains post-covid liquidity (how much/how fast ?), I'd like to see how stocks react if/when approaching that pre-crash price level. This is a more Macro consideration, and not an actionable trading metric in itself.
- I'm using Feb 19, 2020 as the pre-Covid crash date
- I'd also like to plot a horizontal line from 2/19/20 (high or close or either).

So, it's about using price from a fixed past date in time to calculate today's % above/below that specific price. For the life of me, I am unable to find code examples to base my efforts on or make any progress.

Any help is appreciated, and thanks in advance !

this is the simple code I used re the % below the 52 week high;

## To calculate % below 52 week high
Def data=Round(close/Highest(high,252)-1);
AddLabel(1,AsPercent(data), color.white);
 
In looking through scans and watchlists under the current bearish market conditions, I would like to know;

1) what % below the 52 week high is the current price (I've figured that out) and,
2) what % above the pre-Covid market crash is the current price. Rationale: as the Fed drains post-covid liquidity (how much/how fast ?), I'd like to see how stocks react if/when approaching that pre-crash price level. This is a more Macro consideration, and not an actionable trading metric in itself.
- I'm using Feb 19, 2020 as the pre-Covid crash date
- I'd also like to plot a horizontal line from 2/19/20 (high or close or either).

So, it's about using price from a fixed past date in time to calculate today's % above/below that specific price. For the life of me, I am unable to find code examples to base my efforts on or make any progress.

Any help is appreciated, and thanks in advance !

this is the simple code I used re the % below the 52 week high;

## To calculate % below 52 week high
Def data=Round(close/Highest(high,252)-1);
AddLabel(1,AsPercent(data), color.white);
To better phrase what I'm trying to get;
---> I want to create a horizontal price-line at the High on Feb 19,2020 extending to the current date.

The best I've been able to come up with is a a horizontal line for each aggregation period, not the one-single horizontal line from the high of Feb 19,2020 extending to the current date.

So, I need to find out how to;
1) specify the price high for 2/19/2020 only
2) create a horizontal line from that high to the current date

This is the code I have so far. Any help is much appreciated...

###To plot horizontal line from 2/19/2020, the pre-covid Market collapse
input StartDate = 20200219;
def cond = GetYYYYMMDD() == StartDate;
plot price = high;
price.SetPaintingStrategy(PaintingStrategy.horizontal);

Once I have this done, then I want to calculate the current price % above/below the 2/19/20 high ... but one step at a time.

Thanks in advance for any help on this !
 
To better phrase what I'm trying to get;
---> I want to create a horizontal price-line at the High on Feb 19,2020 extending to the current date.

The best I've been able to come up with is a a horizontal line for each aggregation period, not the one-single horizontal line from the high of Feb 19,2020 extending to the current date.

So, I need to find out how to;
1) specify the price high for 2/19/2020 only
2) create a horizontal line from that high to the current date

This is the code I have so far. Any help is much appreciated...

###To plot horizontal line from 2/19/2020, the pre-covid Market collapse
input StartDate = 20200219;
def cond = GetYYYYMMDD() == StartDate;
plot price = high;
price.SetPaintingStrategy(PaintingStrategy.horizontal);

Once I have this done, then I want to calculate the current price % above/below the 2/19/20 high ... but one step at a time.

Thanks in advance for any help on this !

draw a horizontal line from the close of a date
labels show the date , close price, and days back to the date.

set the chart to day with enough years to see the date, 3Y 1D.
i left the 2nd aggregation in, from experimenting.
if need to look at a date farther back, could use a week chart, pick week in edit studies, and enter a monday date.

there are many test dates, commented out in the code, for experimenting.
check for holidays and weekends when entering dates.

Ruby:
# datehorzline_00b

#this is the simple code I used re the % below the 52 week high;
## To calculate % below 52 week high
#Def data1 = Round(close/Highest(high,252)-1);
#AddLabel(1, AsPercent(data1), color.white);

###To plot horizontal line from 2/19/2020, the pre-covid Market collapse

# notes:
# if using aggregation of week, the date needs to be a monday

def na = double.nan;

input agg = AggregationPeriod.day;
def daycls = close(period = agg);
#def daycls = close(period = AggregationPeriod.day);

#------------
# test dates
#------------
#input StartDate1 = 20170227; # monday
#------------
#input StartDate1 = 20180226; # monday
#------------
#input StartDate1 = 20191111; # monday
#------------
#input StartDate1 = 20200217; # monday
input StartDate1 = 20200219; # wed
#------------
# just less than 2 years ago
# need 2yr  1day  chart  to see this, on day agg
#input StartDate1 = 20200224; # monday
#------------
#input StartDate1 = 20200309; # monday
#input StartDate1 = 20200323; # monday
#------------
#input StartDate1 = 20210215; # monday presidents day
#input StartDate1 = 20210216; # tuesday
#input StartDate1 = 20210219; # friday
#input StartDate1 = 20210222; # monday
#input StartDate1 = 20210301; # monday
#input StartDate1 = 20210309; # monday
#------------
#input StartDate1 = 20220107; # monday
#------------

# this works if the date is visible on chart
def dateclose1 = if GetYYYYMMDD() < StartDate1 then na else if GetYYYYMMDD() == StartDate1 then daycls else dateclose1[1];
plot dateline1 = dateclose1;
dateline1.setdefaultcolor(color.yellow);
#dateline1.hidebubble();


addlabel(1, "draw line from close, on " + asprice(startdate1), (if dateclose1 == 0 then color.cyan else color.yellow));
addlabel(1, (if dateclose1 == 0 then "ERROR , can't read from date" else ("$" + dateclose1)), (if dateclose1 == 0 then color.cyan else color.yellow));

# -------------------
def t =  DaysFromDate(StartDate1);
addlabel(1, "days back " + t, color.yellow);
#






i was experimenting with another way, that sort of works, except that it was reading data from the wrong date.
maybe there is something i'm overlooking...?
. get days to date , DaysFromDate(StartDate1)
. read aggregation of chart
. calc bars from last bar to date
. use getvalue to read the data from x bars back


copy this to the end of the first code
Ruby:
# this section sort of works for older dates
# it pulls data , but not from correct date

# calc a bar offset back to date, if chart is day or week
def t =  DaysFromDate(StartDate1);
addlabel(1, "days back " + t, color.yellow);

# calc bars back
def dateoffset1;
if chartday == 1 then {
  # day chart
  dateoffset1 = t;
} else if chartday == 7 then {
  # week chart
  dateoffset1 = t/7;
# intraday chart times don't work. max days is 360.
#} else if chartmin == 240 then {
#  # 4 hour chart, 2 bars/day
#  dateoffset1 = t*2;
#} else if chartmin == 60 then {
#  # hour chart, 7 bars/day
#  dateoffset1 = t*7;
} else {
  dateoffset1 = na;
}

# on the last bar, look back x bars and read close (from the date)
def dateclose2 = if lastbar then getvalue(close, dateoffset1) else dateclose2[1];


# https://usethinkscript.com/threads/current-price-line-indicator-for-thinkorswim.8793/
input barsBack = 1600;

def c = if !IsNaN(close) and IsNaN(close[-1])
        then dateclose2
        else c[1];
plot line = if isNaN(close[-barsBack])
            then c[-barsBack]
            else Double.NaN;
line.SetLineWeight(2);
line.SetDefaultColor(Color.magenta);
line.SetStyle(Curve.MEDIUM_DASH);


addlabel(1, "close from, " + asprice(StartDate1), color.magenta);
addlabel(1, t + " days ago", color.magenta);
addlabel(1, dateoffset1 + " bars back", color.magenta);
addlabel(1, line, color.magenta);
#


2020 holidays
https://www.nasdaq.com/articles/2020-stock-market-holidays-and-bond-market-holidays-2019-12-28

2021 holidays
https://www.nasdaq.com/market-activity/stock-market-holiday-calendar

2022 holidays
https://www.nasdaq.com/market-activity/2022-stock-market-holiday-calendar
 
draw a horizontal line from the close of a date
labels show the date , close price, and days back to the date.

set the chart to day with enough years to see the date, 3Y 1D.
i left the 2nd aggregation in, from experimenting.
if need to look at a date farther back, could use a week chart, pick week in edit studies, and enter a monday date.

there are many test dates, commented out in the code, for experimenting.
check for holidays and weekends when entering dates.

Ruby:
# datehorzline_00b

#this is the simple code I used re the % below the 52 week high;
## To calculate % below 52 week high
#Def data1 = Round(close/Highest(high,252)-1);
#AddLabel(1, AsPercent(data1), color.white);

###To plot horizontal line from 2/19/2020, the pre-covid Market collapse

# notes:
# if using aggregation of week, the date needs to be a monday

def na = double.nan;

input agg = AggregationPeriod.day;
def daycls = close(period = agg);
#def daycls = close(period = AggregationPeriod.day);

#------------
# test dates
#------------
#input StartDate1 = 20170227; # monday
#------------
#input StartDate1 = 20180226; # monday
#------------
#input StartDate1 = 20191111; # monday
#------------
#input StartDate1 = 20200217; # monday
input StartDate1 = 20200219; # wed
#------------
# just less than 2 years ago
# need 2yr  1day  chart  to see this, on day agg
#input StartDate1 = 20200224; # monday
#------------
#input StartDate1 = 20200309; # monday
#input StartDate1 = 20200323; # monday
#------------
#input StartDate1 = 20210215; # monday presidents day
#input StartDate1 = 20210216; # tuesday
#input StartDate1 = 20210219; # friday
#input StartDate1 = 20210222; # monday
#input StartDate1 = 20210301; # monday
#input StartDate1 = 20210309; # monday
#------------
#input StartDate1 = 20220107; # monday
#------------

# this works if the date is visible on chart
def dateclose1 = if GetYYYYMMDD() < StartDate1 then na else if GetYYYYMMDD() == StartDate1 then daycls else dateclose1[1];
plot dateline1 = dateclose1;
dateline1.setdefaultcolor(color.yellow);
#dateline1.hidebubble();


addlabel(1, "draw line from close, on " + asprice(startdate1), (if dateclose1 == 0 then color.cyan else color.yellow));
addlabel(1, (if dateclose1 == 0 then "ERROR , can't read from date" else ("$" + dateclose1)), (if dateclose1 == 0 then color.cyan else color.yellow));

# -------------------
def t =  DaysFromDate(StartDate1);
addlabel(1, "days back " + t, color.yellow);
#






i was experimenting with another way, that sort of works, except that it was reading data from the wrong date.
maybe there is something i'm overlooking...?
. get days to date , DaysFromDate(StartDate1)
. read aggregation of chart
. calc bars from last bar to date
. use getvalue to read the data from x bars back


copy this to the end of the first code
Ruby:
# this section sort of works for older dates
# it pulls data , but not from correct date

# calc a bar offset back to date, if chart is day or week
def t =  DaysFromDate(StartDate1);
addlabel(1, "days back " + t, color.yellow);

# calc bars back
def dateoffset1;
if chartday == 1 then {
  # day chart
  dateoffset1 = t;
} else if chartday == 7 then {
  # week chart
  dateoffset1 = t/7;
# intraday chart times don't work. max days is 360.
#} else if chartmin == 240 then {
#  # 4 hour chart, 2 bars/day
#  dateoffset1 = t*2;
#} else if chartmin == 60 then {
#  # hour chart, 7 bars/day
#  dateoffset1 = t*7;
} else {
  dateoffset1 = na;
}

# on the last bar, look back x bars and read close (from the date)
def dateclose2 = if lastbar then getvalue(close, dateoffset1) else dateclose2[1];


# https://usethinkscript.com/threads/current-price-line-indicator-for-thinkorswim.8793/
input barsBack = 1600;

def c = if !IsNaN(close) and IsNaN(close[-1])
        then dateclose2
        else c[1];
plot line = if isNaN(close[-barsBack])
            then c[-barsBack]
            else Double.NaN;
line.SetLineWeight(2);
line.SetDefaultColor(Color.magenta);
line.SetStyle(Curve.MEDIUM_DASH);


addlabel(1, "close from, " + asprice(StartDate1), color.magenta);
addlabel(1, t + " days ago", color.magenta);
addlabel(1, dateoffset1 + " bars back", color.magenta);
addlabel(1, line, color.magenta);
#


2020 holidays
https://www.nasdaq.com/articles/2020-stock-market-holidays-and-bond-market-holidays-2019-12-28

2021 holidays
https://www.nasdaq.com/market-activity/stock-market-holiday-calendar

2022 holidays
https://www.nasdaq.com/market-activity/2022-stock-market-holiday-calendar
great - thanks !
.. wonder if there is a way to draw a horizontal line from the date of your help to the day of my follow up :)
.. in all seriousness, thank you for responding. I did not receive an auto notification of your response, so pls excuse the delayed gratitude
I will look/work through this. Again/thanks
 
great - thanks !
.. wonder if there is a way to draw a horizontal line from the date of your help to the day of my follow up :)
.. in all seriousness, thank you for responding. I did not receive an auto notification of your response, so pls excuse the delayed gratitude
I will look/work through this. Again/thanks
ok/alright !
I have it conf'gd just as I had hoped ! Thank you very much. I decided to use the weekly aggregation period (began on the Monday) and added code to calculate the current price as a % above/below the Pre-Covid pricing and have that as a label. Here is that code/simply;

###for % above/below the Pre-Covid level
def data = Round(close/dateclose1)-1;
addlabel(1,"% to Pre-Covid: " + AsPercent(data),color.orange);

For my purposes, I'll only use it on my "Weekly Chart Style" for when new stocks are brought to my attention. On a macro level, I want to quickly understand where they stand from the pre-covid Fed Liquidity flood and from its all time high ... then dig in from there.

Again, much appreciated !
 

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

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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