Percent change within a specific time period


Category:
0
0
I know how to do  percent change scan in TOS but would like to know how to do a percent change scan within a specific time period in the past. For example:

stocks with a percentage change increase more than 3% from 07.19.2021 - 07.25.2021.

Thanks
Marked as spam
Posted by (Questions: 1, Answers: 1)
Asked on September 7, 2021 10:42 am
392 views
0
Private answer

For this solution I have included three user inputs at the top of the code. You can adjust the start date and end date as well as the percent change. The code forms a target period within which the close of the first bar of the target period is carried forward indefinitely. Then it does the same with the high for the target period, tracking the highest high within the target period. Each of these values is carried forward to the current bar. At which point the code measures the percent change between the close and the highest high of the target period.

FYI, your end date was a date the markets were closed. But the code is written so that it can adapt to those types of input errors. You can only use this on the daily time frame or higher. it will utterly fail if you try to use it on any intraday time frame.

input startDate = 20210719;
input endDate = 20210725;
input percentThreshold = 3.0;
def targetPeriod = DaysFromDate(startDate) >= 0 and DaysTillDate(endDate) >= 0;
rec trackHighest = if targetPeriod and !targetPeriod[1] then high else if targetPeriod and high > trackHighest[1] then high else trackHighest[1];
rec trackStartPrice = if targetPeriod and !targetPeriod[1] then close else trackStartPrice[1];
def percentChange = 100 * (trackHighest / trackStartPrice - 1);
plot scan = percentChange > percentThreshold;

Marked as spam
Posted by (Questions: 37, Answers: 4118)
Answered on September 7, 2021 12:57 pm
0
Thanks a lot Pete Hahn! I'm going to check this out during my daily review in about 2 hours and circle back to you with any feedback.
( at September 7, 2021 1:05 pm)
0
Everything seems to be working well with this scan Pete. 2 quick questions And if I wanted to scan for stocks that had a percent change decrease less than a specific number I would have to change "plot scan = percentchange > percentthreshold" to plot scan = percentchange < percentthreshold correct? The section of the code below is a bit confusing to me - regarding my original question and my follow up one above in this specific reply, do I have to worry about changing anything in it? "def targetPeriod = DaysFromDate(startDate) >= 0 and DaysTillDate(endDate) >= 0; rec trackHighest = if targetPeriod and !targetPeriod[1] then high else if targetPeriod and high > trackHighest[1] then high else trackHighest[1]; rec trackStartPrice = if targetPeriod and !targetPeriod[1] then close else trackStartPrice[1]; def percentChange = 100 * (trackHighest / trackStartPrice - 1);"
( at September 11, 2021 1:45 pm)
0
Sorry but while providing my original solution I have already exceeded the maximum time I allocate to each free solution I provide in this forum. If you had included this along with the original request its possible I may have been able to include that with my solution. I do not have any remaining time available to provide any alterations.
( at September 11, 2021 3:36 pm)
0
ok no worries and thanks again
( at September 12, 2021 10:26 am)