Fix 'Secondary period not allowed' in ThinkorSwim Scanner?

S

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

BenTen

Administrative
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.
 
S

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
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:
BenTen

BenTen

Administrative
Staff
VIP
@showstopper In that case I'm not sure if it's possible.
 
RobertPayne

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.



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.
 
S

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

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.
 
S

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

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:
S

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

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.
 
J

JoshF

New member
@RobertPayne I am also struggling with a similar issue regarding a second period in the scanner. I am trying to compare a HeikinAshi value from the 1 minute period to the 5 minute period and create a study based on the interaction of the two. My study works, but I am unable to create a scan for it to get alerts, nor a standard study alert since HeikinAshi has recursive code. This is what I have, if you have any insight on how I could make this into a scan:

Code:
#### Start Agg1
input agg1 = AggregationPeriod.min;

def o1 = open(period = agg1);
def h1 = high(period = agg1);
def l1 = low(period = agg1);
def c1 = close(period = agg1);


#def haclose = (o1+h1+l1+c1)/4;

def haOpen = if haOpen[1] == 0 then haClose[1] else (haOpen[1] + haClose[1]) / 2;
def haHigh = Max(high, Max(haClose, haOpen));
def haLow = Min(low, Min(haClose, haOpen));

def trend1 = if haClose > haOpen then 1 else if haClose < haOpen then -1 else 0;
#### End Agg1

#### Start Agg2
input agg2 = AggregationPeriod.Five_min;

def o2 = open(period = agg2);
def h2 = high(period = agg2);
def l2 = low(period = agg2);
def c2 = close(period = agg2);

def haclose2 = (o2+h2+l2+c2)/4;

def haOpen2 = if haOpen2[1] == 0 then haClose2[1] else (haOpen2[1] + haClose2[1]) / 2;
def haHigh2 = Max(high, Max(haClose2, haOpen2));
def haLow2 = Min(low, Min(haClose2, haOpen2));

def trend2 = if haClose2 > haOpen2 then 1 else if haClose2 < haOpen2 then -1 else 0;

#### END Agg 2
    
#### TRADE LOGIC
def BuyCondition = if trend1 ==1 and trend2 ==1 then 1 else 0;

plot Buy = BuyCondition;

#plot Sell = if BuyCondition <> 1 then 1 else 0;

#### END TRADE LOGIC
 
B

bobmassie

New member
Hi, friends. I've been searching high and low for some explanation that will help me understand what "Secondary Period Not Allowed" means in ThinkScript. The problem is I'm completely ignorant. What is the Primary Period? How do I know if I'm scripting for a secondary period?

All I want to do is to write code that flags when the ohlc4 closes above the upper Bollinger Band on a 5 minute chart.

I'm pulling my hair out.

Thanks very much in advance for any help you can give me.

Bob
 
inthefutures

inthefutures

Active member
post you code so we can where the error come in
 
S

showstopper

New member
Awesome, this works. Thanks a lot for taking the time out to help me. Thank you so much.

Do you know how can I get a 5min crossover scanner for previous weeklyhigh or weeklylow?
def today = GetWeek() == GetLastWeek();
def mktOpen = SecondsFromTime(930) == 0 or GetWeek() <> GetWeek()[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
def loworhigh = (close crosses yHigh) or (close crosses YLow);

plot test4 = loworhigh;
test4.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
 
S

satajju

New member
Hi,

I need some help with my Scanner , TOS is not allowing to have an another period. I want to scan for below criteria .

Please help me

Code:
input price = FundamentalType.CLOSE;
input aggregationPeriod = AggregationPeriod.HOUR;
#input aggregationPeriodD = AggregationPeriod.DAY;
input length8 = 8;
input length34 = 34;
input length5 = 5;
input length13 = 13;
input averageType1 = AverageType.EXPONENTIAL;
input averageType2 = AverageType.EXPONENTIAL;
input crossingType = {default above, below};

def avg8 = MovingAverage(averageType1, Fundamental(price, period = aggregationPeriod), length8);
def avg34 = MovingAverage(averageType2, Fundamental(price, period = aggregationPeriod), length34);

def avg5 = MovingAverage(averageType1, Fundamental(price, period =  AggregationPeriod.DAY), length5);
def avg13 = MovingAverage(averageType2, Fundamental(price, period =  AggregationPeriod.DAY), length13);

def Crossabove513Daily = Crosses(avg5, avg13, crossingType == crossingType.above);

def Crossabove834Hour = Crosses(avg8, avg34, crossingType == crossingType.above);

plot mylist = Crossabove513 and Crossabove834;
 
Last edited:

Similar threads

Top