Daily closes less than previous weekly close


Category:
0
0

Hi Pete,

I’m interested in adding a stock screen that requires all the daily closes of the current week to be below the weekly close of the prior week.  I understand that TOS doesn’t allow secondary aggregation periods in their scans, but you mentioned there is a work-around, although complex: https://www.hahn-tech.com/ans/secondary-aggregation-not-allowed-in-scans.  I’ve read through that post, but don’t fully follow.  Is the idea to define the larger time frame in terms of the lower time frame?  I could imagine doing that to create a rolling 5 days, but how could you delineate between Friday being last week, and Monday being this week?  Is there any resources that you could point me into to accomplish this work around?

Thanks so much for your help,

Cheers,

David

Marked as spam
Posted by (Questions: 8, Answers: 3)
Asked on October 21, 2019 7:55 am
221 views
0

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.

( at October 21, 2019 9:58 am)
0
Private answer

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)

  1. Use the GetWeek() function to determine when a new week has started
    1. https://toslc.thinkorswim.com/center/reference/thinkScript/Functions/Date---Time/GetWeek.html
  2. When a new week has begun, use recursion to grab and hold the close of the previous daily bar (Friday's close)
  3. For each day of the week, check if the current day's close is less than the prior week close
  4. Then we use the GetDayOfWeek() function to customize the scan signal for each day of the week
    1. https://toslc.thinkorswim.com/center/reference/thinkScript/Functions/Date---Time/GetDayOfWeek.html
  5. 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;

Marked as spam
Posted by (Questions: 37, Answers: 4118)
Answered on October 21, 2019 11:35 am
0
Thanks Pete.  That's very helpful - both the code and the explanation.  
( at October 22, 2019 7:28 am)