Fix 'Secondary period not allowed' in ThinkorSwim Scanner

showstopper

New member
Hi, I wanted to create a scanner with the following requirement - " give me all the stocks for which the 5min close is crossing above previous day close". I am unable to code it as it's constantly saying secondary timeperiod not allowed. I thought this was a very simple query for TOS to solve. Maybe I am not treating it properly.
 

BenTen

Administrative
Staff member
Staff
VIP
@showstopper Here is the code which plot previous day close on your chart.

Code:
input ShowTodayOnly = no;

def Today = if GetDay() == GetLastDay() then 1 else 0;

#Plot yesterday's high / low
plot prevClose = if ShowTodayOnly and !Today then Double.NaN else close(period = "day" )[1];

prevClose.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
prevClose.SetDefaultColor(Color.UPTICK);
prevClose.SetLineWeight(1);

Try using the scanner with it.
 

showstopper

New member
Thank you for the response, I have the code for previous day close but how do I use it conjugation with 5min close as these are two different timeframes and scanner only allows 1 timeframe?

This is what I have

Code:
input ShowTodayOnly = no;
def Today = if GetDay() == GetLastDay() then 1 else 0;
#Plot yesterday's high / low
def prevClose = if ShowTodayOnly and !Today then Double.NaN else close(period = "day" )[1];
Plot data = close(period = AggregationPeriod.FIVE_MIN) crosses above prevClose;

But this doesn't work as it errors out with this "Secondary period not allowed: 5 min"
 
Last edited by a moderator:

RobertPayne

Member
@showstopper In that case I'm not sure if it's possible.

It's possible.

As you are both aware, the scanner does not support the full feature set that Thinkscript does. In this case, you know that secondary aggregations are not allowed. So, if you cannot use close(period = "day" )[1], then another way must be found.

What if we set a variable to remember the close of the last 5 minute bar from yesterday?

Ruby:
def closeYesterday = if getday() <> getlastday() then close else closeYesterday[1];

Here's a visualization of that code.

YbeqAhg.jpg


When using that definition in the scanner, ensure that the aggregation is set to "5 min" and that "Include Extended-Hours Trading session" is not checked.
 

showstopper

New member
I tested it and it works as expected, this is amazing @RobertPayne. Thank you so much. I also wanted to get some inputs on the following -
1. How do I make it remember previous day's high? Basically I want to apply the same (if 5min close crosses above Previous day high)
2. How will the following work close 5min crosses below SVEPivots()."S3" The problem here is SVEPivots is taking daily aggregation but I want close of 5min.

Do you know how can 1 and 2 be coded?
 

RobertPayne

Member
Just substitute "high" for "close" in the example I showed you above. For number 2, you'll have to look at the formula for SVEPivots and calculate any of the daily values it uses (high, low, close, open) with the calculations I already showed you how to do.
 

showstopper

New member
Just substitute "high" for "close" in the example I showed you above. For number 2, you'll have to look at the formula for SVEPivots and calculate any of the daily values it uses (high, low, close, open) with the calculations I already showed you how to do.

I have this code - but it's not fetching the right results. To reiterate what I want is I want to scan for stocks which are crossing above their yesterday's high of the day on a current 5 min candle.

def highYesterday = if getday() <> getlastday() then high else highYesterday[1];
Plot data = close(period = AggregationPeriod.FIVE_MIN) crosses above highYesterday;
 

RobertPayne

Member
def highYesterday = if getday() <> getlastday() then high else highYesterday[1];
Plot data = close(period = AggregationPeriod.FIVE_MIN) crosses above highYesterday;
Oops. I spoke too quickly without thinking.

You can use any of the below definitions for yesterday's daily values on an intraday chart.

Ruby:
def today = GetDay() == GetLastDay();
def mktOpen = SecondsFromTime(0930) == 0 or GetDay() <> GetDay()[1];
def yOpen = if today then yOpen[1] else if mktOpen then open else yOpen[1]; #yesterday's open
def yHigh = if today then yHigh[1] else if mktOpen then high else if high > yHigh[1] then high else yHigh[1]; #yesterday's high
def yLow = if today then yLow[1] else if mktOpen then low else if low < yLow[1] then low else yLow[1]; #yesterday's low
def yClose = if today then yClose[1] else close; #yesterday's close
 
Last edited:

showstopper

New member
Remember, this is just like the first example you had. You cannot use secondary aggregation periods in the scanner.

So, set the scan to 5 min time frame and do NOT check extended hours.

def highYesterday = if getday() <> getlastday() then high else highYesterday[1];
Plot data = close crosses above highYesterday;

Actually, I did try that as well. The problem here is the results which are showing up on scanner doesn't match the criteria I want. Ex: At the moment GS is showing up on the scanner. Yesterday's high of GS was 210.85 current 5 min price of GS is 209.35-209.50. I hope I am able to cite the problem statement correctly here.
 

RobertPayne

Member
Actually, I did try that as well. The problem here is the results which are showing up on scanner doesn't match the criteria I want. Ex: At the moment GS is showing up on the scanner. Yesterday's high of GS was 210.85 current 5 min price of GS is 209.35-209.50. I hope I am able to cite the problem statement correctly here.
see above. I edited my post just as you were writing this one. I misspoke earlier.
 

Cecil

New member
VIP
Hello All! I have a scan I'd like to run that compares an EMA from a ONE MIN chart (720 EMA) to the Previous Day Close. I want to use this in a PreMarket scan.

I'm not sure how to workaround my error "Secondary Period Not Found/Allowed". I'm not sure if I should try to change the EMA period or if there's a different way to identify the Previous Day Close on a 1 Min chart. Any help would be greatly appreciated! Here's the scan code I've worked out so far:

## Scan to measure distance of EMA720 (on 1MIN chart) and PrevDailyClose (on Daily Chart). Looking for less than 75cent distance.

input Threshold = .75;

def Close = close(period = AggregationPeriod.DAY);

def EMA = MovAvgExponential("length"=720). "AvgExp" is less than close("period" = AggregationPeriod.MIN);
def range = (close - ema);

plot scan= range < threshold;
 

SleepyZ

Well-known member
Hello All! I have a scan I'd like to run that compares an EMA from a ONE MIN chart (720 EMA) to the Previous Day Close. I want to use this in a PreMarket scan.

I'm not sure how to workaround my error "Secondary Period Not Found/Allowed". I'm not sure if I should try to change the EMA period or if there's a different way to identify the Previous Day Close on a 1 Min chart. Any help would be greatly appreciated! Here's the scan code I've worked out so far:

## Scan to measure distance of EMA720 (on 1MIN chart) and PrevDailyClose (on Daily Chart). Looking for less than 75cent distance.

input Threshold = .75;

def Close = close(period = AggregationPeriod.DAY);

def EMA = MovAvgExponential("length"=720). "AvgExp" is less than close("period" = AggregationPeriod.MIN);
def range = (close - ema);

plot scan= range < threshold;

Try this workaround the aggregation period not allowed. The following is an example that worked in the scanner.

Code:
def pclose = if GetDay() == GetLastDay() - 1 and SecondsFromTime(1600)[1] == 0 then close else pclose[1];
def ema720 = ExpAverage(close, 720);
#AddLabel(1, AsText(ema720) + " " + AsText(pclose), Color.WHITE);
plot scan = (Round(ema720) - Round(pclose)) > .10;
 

2laggy4life

New member
Hello, I have a question regarding Scanner vs Alert..

When I right click on any symbol chart I can set an alert to do what I want which is an alert for a custom MTF_Ichimoku() indicator that I created.
The Alert looks something like this:

alert on 5min chart:
close crosses ichimoku_mtf()."FourHourTenkan";

So with this it is not telling me that secondary period is not allowed, it works perfectly.
But when I try to create a scanner that can scan or alert me whenever ANY symbol meets this criteria, it says 'secondary period not allowed'
The bypass for this would be to copy the 4Hour Tenkan line onto my 5minute timeframe chart using some type of a plot and then scan on that, right?

I was wondering if I can get some help with that, thank you
 

SleepyZ

Well-known member
Hello, I have a question regarding Scanner vs Alert..

When I right click on any symbol chart I can set an alert to do what I want which is an alert for a custom MTF_Ichimoku() indicator that I created.
The Alert looks something like this:

alert on 5min chart:
close crosses ichimoku_mtf()."FourHourTenkan";

So with this it is not telling me that secondary period is not allowed, it works perfectly.
But when I try to create a scanner that can scan or alert me whenever ANY symbol meets this criteria, it says 'secondary period not allowed'
The bypass for this would be to copy the 4Hour Tenkan line onto my 5minute timeframe chart using some type of a plot and then scan on that, right?

I was wondering if I can get some help with that, thank you
Since close is the same on all intraday aggregations, you can try running the scan settings at 4hr agg and use your code above.

See picture below with a 5m and 4hr chart with the bubbles for close are the same.

Capture.jpg
 
Last edited:

lmk99

Member
#Volume ForeCast v1.0 by XeoNoX via usethinkscript.com
[...]

def Daily_Total_Vol = volume(period = AggregationPeriod.DAY);

Hi, I notice that the use of this secondary period DAY prevents the indicator from working as a scan. If I want to use it with a 1min period as a scan, do you have an idea about how the secondary period reference could be removed and replaced with some substitution code that is scannable (maybe something like referencing 390 1min bars from the previous date)? Or do you think it's not possible to make a scannable version?
 

MerryDay

Administrative
Staff member
Staff
VIP
@lmk99 I saw this and thought you might be able to incorporate the idea:
Ruby:
# Mobius: Any bar, at any aggregation within the data of the scanner can be compared with any other bar in the data.
# You are looking for a simple X , Y coordinate comparison.
# Example of using a one minute aggregation close crossing yesterdays closing price:
#
def RTH_close = if getTime() crosses above RegularTradingEnd(getYYYYMMDD()) then close[1] else RTH_close[1];
plot cross_Is_True = close crosses above RTH_close;
 
Last edited:

lmk99

Member
@lmk99 I saw this and thought you might be able to incorporate the idea:
Ruby:
# Mobius: Any bar, at any aggregation within the data of the scanner can be compared with any other bar in the data.
# You are looking for a simple X , Y coordinate comparison.
# Example of using a one minute aggregation close crossing yesterdays closing price:
#
def RTH_close = if getTime() crosses above RegularTradingEnd(getYYYYMMDD()) then close[1] else RTH_close[1];
plot cross_Is_True = close crosses above RTH_close;

Awesome, I will try this workaround!
 

Similar threads

Top