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.
 

lmk99

Member
VIP
I need to use the 1min period for a scan, but make it get yesterday's daily volume. I've been trying to wrap my head around how to reference yesterday's volume as a secondary period volume (the DAY total) in the scan. It's not clear to me how it could be done.

For example: "if getTime() crosses above RegularTradingEnd(getYYYYMMDD()) then close[1] else RTH_close[1]" just by replacing "close" with "volume" will result in the volume of a 1minute bar from the previous day, since the period is still 1 minute.

Maybe the answer is to use the compound value function so that all the one minute bars' volumes can be added together? Something like this from XeoNox's bar count study?

def var = close > open;
def barUpCount = CompoundValue(1, if var then barUpCount[1] + 1 else 0, 0);
AddLabel (yes, "COUNT " + barUpCount );

Another possibility could be trying the TotalSum function e.g. "plot data = TotalSum(volume);"

I doubt it will work, I'm kind of shooting in the dark, but I'll try:

Code:
def RTH_volume = if getTime() crosses above RegularTradingEnd(getYYYYMMDD()) then TotalSum(volume[1]) else RTH_volume[1];

def Daily_Total_Vol =  RTH_volume; #instead of "volume(period = AggregationPeriod.DAY);"

EDIT: Can now confirm that this doesn't work.
 
Last edited by a moderator:
I am trying to Create a Scanner based on a Study which uses the Previous Days Close Information. I am getting an error message saying "Secondary period not allowed: Day." Does Anyone know if there is a work around for this.?
 

MerryDay

Administrative
Staff member
Staff
VIP
Thank you. I am trying to work with some code that was posted previously, by piecing it together. I have the Plot showing on the chart, but still haven't been able to get the scanner part to work yet.
Mobius's solution to the second aggregation error sounds so simple:
https://usethinkscript.com/threads/...llowed-in-thinkorswim-scanner.927/#post-77684
But the math required to incorporate his 'simple' instructions are definitely for advance programmers! o_O
 

mashume

Well-known member
VIP
I need to use the 1min period for a scan, but make it get yesterday's daily volume. I've been trying to wrap my head around how to reference yesterday's volume as a secondary period volume (the DAY total) in the scan. It's not clear to me how it could be done.

For example: "if getTime() crosses above RegularTradingEnd(getYYYYMMDD()) then close[1] else RTH_close[1]" just by replacing "close" with "volume" will result in the volume of a 1minute bar from the previous day, since the period is still 1 minute.

Maybe the answer is to use the compound value function so that all the one minute bars' volumes can be added together? Something like this from XeoNox's bar count study?

def var = close > open;
def barUpCount = CompoundValue(1, if var then barUpCount[1] + 1 else 0, 0);
AddLabel (yes, "COUNT " + barUpCount );

Another possibility could be trying the TotalSum function e.g. "plot data = TotalSum(volume);"

I doubt it will work, I'm kind of shooting in the dark, but I'll try:

Code:
def RTH_volume = if getTime() crosses above RegularTradingEnd(getYYYYMMDD()) then TotalSum(volume[1]) else RTH_volume[1];

def Daily_Total_Vol =  RTH_volume; #instead of "volume(period = AggregationPeriod.DAY);"

EDIT: Can now confirm that this doesn't work.
This should:
Code:
def Yesterday = if GetDay() == GetLastDay() - 1 then 1 else 0;
def rth = if secondsFromTime(0930) >= 0 and secondsTillTime(1600) >= 0 then 1 else 0;

def prev_vol = if yesterday == 1 and rth == 1 then prev_vol[1] + VOLUME else prev_vol[1];

plot previous_day_volume = prev_vol;

addLabel(yes, "Previous Day Volume: " + prev_vol, color.orange);

Perhaps that gets you going in a workable direction.

happy trading,
mashume
 

lmk99

Member
VIP
This should:
Code:
def Yesterday = if GetDay() == GetLastDay() - 1 then 1 else 0;
def rth = if secondsFromTime(0930) >= 0 and secondsTillTime(1600) >= 0 then 1 else 0;

def prev_vol = if yesterday == 1 and rth == 1 then prev_vol[1] + VOLUME else prev_vol[1];

plot previous_day_volume = prev_vol;

Perhaps that gets you going in a workable direction.

happy trading,
mashume

This is fantastic! If I then also want to get the cumulative volume of the Day period for the current day, is it as simple as:

just change "def Yesterday = if GetDay() == GetLastDay() - 1 then 1 else 0;" to "def Today = if GetDay() == GetLastDay() - 0 then 1 else 0;"

and then change "def prev_vol = if yesterday == 1 and rth == 1 then prev_vol[1] + VOLUME else prev_vol[1];" to "def today_vol = if Today == 1 and rth == 1 then today_vol[1] + VOLUME else today_vol[1];"

Is that going to make it return the Day volume so far for the current day with the scan using a 1min aggregation period, like I'm thinking?

Maybe it's kind of convoluted if there's a simpler way to get the current Day volume, but I want to make sure I understand the logic, in which case today's Day volume with a 1min period scan would be calculated with this:

Code:
def Today = if GetDay() == GetLastDay() - 0 then 1 else 0;
def rth = if secondsFromTime(0930) >= 0 and secondsTillTime(1600) >= 0 then 1 else 0;

def today_vol = if Today == 1 and rth == 1 then today_vol[1] + VOLUME else today_vol[1];

def total_day_volume = today_vol;
 

varaamo74

New member
Can you please help me with converting the following to scan:

i would like to scan for price touching these ADR lines (ADR_H or ADR_L) on 5 minute period. Using your logic, I can get obtained yeterday's range.. but how obtaining prev 5 day range?

#Average Daily Range Levels
#By Vah for TOS, based on cristian.d CD_Average Daily Range Zones - highs and lows of the day

#The number of days you'd like average
input lenght = 5

def dayHigh = DailyHighLow(AggregationPeriod.DAY).Dailyhigh;
def dayLow = DailyHighLow(AggregationPeriod.DAY).DailyLow;
def dayrange = dayHigh - dayLow;
def AvgRange = average(DayRange, lenght);

plot ADR_H = open(period = AggregationPeriod.DAY) + (AvgRange/2);
plot ADR_L = open(period = AggregationPeriod.DAY) - (AvgRange/2);
 
Last edited by a moderator:

MerryDay

Administrative
Staff member
Staff
VIP
Can you please help me with converting the following to scan:

i would like to scan for price touching these ADR lines (ADR_H or ADR_L) on 5 minute period. Using your logic, I can get obtained yeterday's range.. but how obtaining prev 5 day range?
Unfortunately, secondary aggregations won't work in the scan hacker. Mobius provided the workaround explained in this thread. He is not a member of our forum. Attempting to apply this is complex, if you have the programming background the basis is provided here:
https://usethinkscript.com/threads/...llowed-in-thinkorswim-scanner.927/#post-77684
 
Last edited:

ziongotoptions

New member
VIP
I am trying to set up a scanner that tells me when a stocks pivot range (PH-PL) is less than the pivot range from the day before, is that possible? I am currently getting an error that says "'from date' must be before 'toDate' in 'counttradingdays'"


Code:
def today_ = yes;
def today = today_ and GetDay() == GetLastDay();

def tw = SecondsFromTime(400) >= 0 and SecondsFromTime(2000) < 0;
def yhigh = if GetDay() != GetDay()[1] and GetDay() != GetLastDay() then 0 else if tw and high > yhigh[1] then high else yhigh[1];
def yhigh2 = if GetDay() != GetDay()[1] then yhigh[1] else yhigh2[1];
def ylow = if GetDay() == GetLastDay() then ylow[1] else if GetDay() != GetDay()[1] and GetDay() != GetLastDay() then 0 else if tw and ylow[1] == 0 then low else if tw and low < ylow[1] then low else ylow[1];
def yclose = if GetDay() == GetLastDay() then yclose[1] else if GetDay() != GetDay()[1] and GetDay() != GetLastDay() then 0 else if tw then close else yhigh[1];
def ylow2 = if GetDay() != GetDay()[1] then ylow[1] else ylow2[1];
def yclose2 = if GetDay() != GetDay()[1] then yclose[1] else yclose2[1];
def close1 = if SecondsFromTime(1600) < 0 then close else close1[1];
def yclose3 = if GetDay() != GetDay()[1] then close1[1] else yclose3[1];
def AVH = (yhigh2+ylow2)/2;
def pp = (yhigh2 + ylow2 + yclose2) / 3;
def PD = pp - avh;
def PH = pp + pd;
def PL = pp - pd;
def range = PH-PL;

def daysago2 = CountTradingDays(20200101, GetYYYYMMDD()) == (CountTradingDays(20200101, HighestAll(GetYYYYMMDD())) - 2);
def high2 = if today then high2[1] else if daysago2 and SecondsFromTime(930) >= 0 and SecondsFromTime(1600) < 0 then close else high2[1];
def high2r = if today then high2r[1] else if daysago2 and SecondsFromTime(930) >= 0 and SecondsFromTime(1600) < 0 and high > high2r[1] then high else high2r[1];
def low2 = if today then low2[1] else if daysago2 and low2[1] == 0 and SecondsFromTime(930) >= 0 and SecondsFromTime(1600) < 0 then low else if daysago2 and SecondsFromTime(930) >= 0 and SecondsFromTime(1600) < 0 and low < low2[1] then low else low2[1];

def preprevclose = if GetDay() == GetLastDay() then high2 else Double.NaN;
def preprevhigh = if GetDay() == GetLastDay() then high2r else Double.NaN;
def preprevlow = if GetDay() == GetLastDay() then low2 else Double.NaN;

def ppp = (preprevhigh+preprevlow+preprevclose)/3;
def pavh = (preprevhigh-preprevlow)/2;
def ppd = ppp-pavh;
def PPH = ppp+ppd;
def PPL = ppp-ppd;
def prange = PPH-PPL;
plot "BO" = range <= prange;
 

Sascha_R

New member
I am facing the same problem with the secondary aggregation. My scan is supposed to show me all the higher lows as long as they are below the 200 weekly MA. I tried to use the workaround with getweek() and getlastweek() but it still ignores my moving average condition.
Code:
input MajorLeftStr = 13;
input MajorRightStr = 13;
input MinorLeftStr = 5;
input MinorRightStr = 5;
input SwingTickOffset = 2;
input AlertsOn = yes;

# This indicator must be applied to a bar interval larger than 5 ticks.

def offset = TickSize() * (HighestAll(high) - LowestAll(low)) * SwingTickOffset;

#Calculate Major Swings
def pivotH = if high > Highest(high[1], MajorLeftStr) and high > Highest(high[-MajorRightStr], MajorRightStr) then 1 else 0;
def pValH = if pivotH then high + offset else Double.NaN;
def pivotL = if low < Lowest(low[1], MajorLeftStr) and low < Lowest(low[-MajorRightStr], MajorRightStr) then 1 else 0;
def pValL = if pivotL then low - offset else Double.NaN;


#Previous High/Low compared to Last High/Low-----------------------------------
def ph1 = if !IsNaN(pValH) then pValH else ph1[1];
def ph2 = if ph1 != ph1[1] then ph1[1] else ph2[1];

#AddLabel(1, "SwH: " + AsText(ph1) + " " + AsText(ph2), if ph1 > ph2 #then Color.GREEN else Color.RED);

def pl1 = if !IsNaN(pValL) then pValL else pl1[1];
def pl2 = if pl1 != pl1[1] then pl1[1] else pl2[1];

#AddLabel(1, "SwL: " + AsText(pl1) + " " + AsText(pl2), if pl1 > pl2 #then Color.GREEN else Color.RED);

#Test Data for Higher Lows----------------------------------------
#Additional Prior Lows before pl1 and pl2--------------------------
def pl3 = if pl2 != pl2[1] then pl2[1] else pl3[1];
def pl4 = if pl3 != pl3[1] then pl3[1] else pl4[1];
def pl5 = if pl4 != pl4[1] then pl4[1] else pl5[1];
def pl6 = if pl5 != pl5[1] then pl5[1] else pl6[1];

input debug = no;

#Bar Counts between last X Prior Lows
def plow = if !IsNaN(pValL) then pValL else GetValue(plow, 1);
def plowct = if plow[1] != plow then 1 else if plow[1] == plow then plowct[1] + 1 else 0;

def bc1 = if plow != plow[1] then plowct[1] else bc1[1];
def bc2 = if bc1 != bc1[1] then bc1[1] else bc2[1];
def bc3 = if bc2 != bc2[1] then bc2[1] else bc3[1];
def bc4 = if bc3 != bc3[1] then bc3[1] else bc4[1];
def bc5 = if bc4 != bc4[1] then bc4[1] else bc5[1];


def hl = if hl[1] == 0 and plow > plow[1] then 1 else if plow < plow[1] then 0 else if hl[1] >= 1 and plow > plow[1] then hl[1] + plowct[1] else hl[1] ;

def closeLastWeek = if getweek() <> getlastweek() then close else closeLastWeek[1];

#def ma1 = if isnan(Average(closeLastWeek, 200)) then Average(close(period = AggregationPeriod.DAY), 200) else #Average(closeLastWeek, 200);


#Bar Count Label and Chart Bubbles at Swing Lows---------------------
input showcountlabel = yes;
#Capture Bar Count Past First Higher Low-----------------------------
def temphl = if hl[1] == 0 then 0 else if hl[1] >= 1 then hl[1] + plowct[1] else temphl[1];

#Some symbols don't have 200 weekly MA and this case we use 200 daily MA
def signal = if (temphl - MajorRightStr)>0 and plow<if isnan(Average(closelastweek, 200)) then Average(close(period = AggregationPeriod.DAY), 200) else Average(closelastweek,200)
then (temphl - MajorRightStr) else 0;

def test = if signal > 0 then signal else 0;
plot data = test;

Any help would be greatly appreciated? I have already spent ages on trying to solve it.
 
Last edited by a moderator:

Sascha_R

New member
There is another EMA thread that is attempting to use the same workaround. If you are proficient at programming For-While statements, it presents the additional snippet of code that you would need:
https://usethinkscript.com/threads/manually-calculated-ema-not-matching-expected-value.9071/
I am not familiar with the fold function:

I tried this:
def lastweek = if getweek()<>getlastweek()-1 then 1 else 0;
def rth = if secondsFromTime(0930) >= 0 and secondsTillTime(1600) >= 0 then 1 else 0;
def closeLastWeek = if lastweek==1 and rth==1 then close else
closeLastWeek[1];

def weeklyMA= fold index = 0 to 200 with p do getvalue(closelastweek,index)+weeklyMA[1];

plot data = weeklyMA/200;

But there is still something wrong with the fold statement. Can you help me?
 

halcyonguy

Well-known member
VIP
I am not familiar with the fold function:

I tried this:
def lastweek = if getweek()<>getlastweek()-1 then 1 else 0;
def rth = if secondsFromTime(0930) >= 0 and secondsTillTime(1600) >= 0 then 1 else 0;
def closeLastWeek = if lastweek==1 and rth==1 then close else
closeLastWeek[1];

def weeklyMA= fold index = 0 to 200 with p do getvalue(closelastweek,index)+weeklyMA[1];

plot data = weeklyMA/200;

But there is still something wrong with the fold statement. Can you help me?
try changing this
def weeklyMA= fold index = 0 to 200
with p
do getvalue( closelastweek, index) + weeklyMA[1];

to this
def weeklyMA= fold index = 0 to 200
with p
do p + getvalue( closelastweek,index);

for each value of index, the getvalue() value is added to the previous value, which is held in p.
 

Sascha_R

New member
try changing this
def weeklyMA= fold index = 0 to 200
with p
do getvalue( closelastweek, index) + weeklyMA[1];

to this
def weeklyMA= fold index = 0 to 200
with p
do p + getvalue( closelastweek,index);

for each value of index, the getvalue() value is added to the previous value, which is held in p.

I tried the following:
def newWeek = GetWeek() <> GetWeek()[1];
def closelastweek = if newWeek then close[1] else closelastweek[1];
input length = 200;
def weeklyMA= fold n = 0 to length with mov_avg do mov_avg+ getvalue(closelastweek,n);
plot data = weeklyMA/200;

closelastweek provides last week's close price correctly. But, the fold function still doesn't work.

Any idea? Can any of your experts put your bright brains into it?
 
Last edited:

Similar threads

Top