In that previous post I linked to another post that gave an example of how to do this on an intraday time frame. That solution requires the use of several functions related to date and time as well as the use of recursive variables. So in that respect, that example I suggested does provide most of the required elements.
For this current request, we need to work with weeks and days. So the functions used to reference time are not needed. I'll briefly describe the steps required for the code to accomplish this goal. (everything here requires the Study Filter to be set to the Daily time frame)
- Use the GetWeek() function to determine when a new week has started
- https://toslc.thinkorswim.com/center/reference/thinkScript/Functions/Date---Time/GetWeek.html
- When a new week has begun, use recursion to grab and hold the close of the previous daily bar (Friday's close)
- For each day of the week, check if the current day's close is less than the prior week close
- Then we use the GetDayOfWeek() function to customize the scan signal for each day of the week
- https://toslc.thinkorswim.com/center/reference/thinkScript/Functions/Date---Time/GetDayOfWeek.html
- For each day of the week, we check if the current and all prior days of the current week meet the requirement
Here is the code:
def newWeek = GetWeek() <> GetWeek()[1];
rec priorWeeklyClose = if newWeek then close[1] else priorWeeklyClose[1];
def dailyCloseCondition = close < priorWeeklyClose;
def dayOfWeek = GetDayOfWeek(GetYYYYMMDD());
def belowWeeklyClose;
if dayOfWeek == 1 {
belowWeeklyClose = dailyCloseCondition;
} else {
if dayOfWeek == 2 {
belowWeeklyClose = Lowest(dailyCloseCondition, 2) > 0;
} else {
if dayOfWeek == 3 {
belowWeeklyClose = Lowest(dailyCloseCondition, 3) > 0;
} else {
if dayOfWeek == 4 {
belowWeeklyClose = Lowest(dailyCloseCondition, 4) > 0;
} else {
belowWeeklyClose = Lowest(dailyCloseCondition, 5) > 0;
}
}
}
}
plot scan = belowWeeklyClose;
This code I just posted does not include the indentations that make it easier to read the if/then/else structures. So I am providing a screenshot below that shows how the code looks when the proper indentations have been applied. Also attaching a plain text version of this code which retains the indentations (file name is DailyBelowPreviousWeek.txt)
If you want to see what this looks like on a chart, create a new custom chart study and add the following statement anywhere in the code (preferably at the very top):
declare lower;
Since secondary aggregation periods are not permitted for scans there is no point in using that as your question title. Rather what you should use for a question title is how to scan for stocks that have daily closes of the current week less than the weekly close of the previous week. And that's a mouth-full so how do we describe that within 6-8 words?
"Daily closes less than previous weekly close"
Yep, I think that about does it. That gives the general context (for the benefit of the rest of our viewers).
I will change the title of this question before posting the solution. This is a fairly complex problem to solve.