Multi-time frame scanner true within x bars

mapesd

New member
I am trying to create a multi-time frame scanner that is true when the last of 4 conditions becomes true within x bars.

Conditions:
  • # Close > 10wk moving average
  • # Close > 40wk moving average
  • # Close > 200day moving average
  • # Relative strength vs S&P500 is increasing
I created the study below to illustrate what I’m trying to do. It does not include detecting the last of 4 conditions becoming true within X bars.

The scanner requires an aggregation period per study. I can’t use the study below since it has multi-aggregation periods. I can’t produce a way to re-create the study in scanner.

Any pointers appreciated.

Thanks,
Dave

Code:
# Used for scanner
# Search for companies that meet this criteria
#  Close > 10wk moving average
#  Close > 40wk moving average
#  Close > 200day moving average
#  Relative strength vs S&P500 is increasing

declare lower;

input price = FundamentalType.CLOSE;
input wklyaggregationPeriod = AggregationPeriod.WEEK;
input shortwklylength = 10;
input longwklylength = 40;
input dailyaggregationPeriod = AggregationPeriod.DAY;
input longdaylength = 200;
input displace = 0;
input showOnlyLastPeriod = no;

def shortwklySMA;

if showOnlyLastPeriod and !IsNaN(close(period = wklyaggregationPeriod)[-1]) {
    shortwklySMA = Double.NaN;
} else {
    shortwklySMA = Average(Fundamental(price, period = wklyaggregationPeriod)[-displace], shortwklylength);
}

def longwklySMA;

if showOnlyLastPeriod and !IsNaN(close(period = wklyaggregationPeriod)[-1]) {
    longwklySMA = Double.NaN;
} else {
    longwklySMA = Average(Fundamental(price, period = wklyaggregationPeriod)[-displace], longwklylength);
}

def longdailySMA;

if showOnlyLastPeriod and !IsNaN(close(period = dailyaggregationPeriod)[-1]) {
    longdailySMA = Double.NaN;
} else {
    longdailySMA = Average(Fundamental(price, period = dailyaggregationPeriod)[-displace], longdaylength);
}

#Relative strength
input CorrelationWithSecurity = "SPX";
def close2 = close(CorrelationWithSecurity);

def RS = if close2 == 0 then 0 else close/close2;

input RSAvgLength = 50;
input averageType = AverageType.SIMPLE;

def RSAvg = MovingAverage(averageType, RS, RSAvgLength);

def RSAvgInc = RSAvg[1] < RSAvg;

plot GreaterThanAvgs = close > shortwklySMA and close > longwklySMA and close > longdailySMA and RSAvgInc;
 
Four of which bars? For example - would the weekly conditions need to be within four weeks, and the daily conditions within four days? Or, would all of it just need to be within four days? Also, confirm for me that the RSI is based on the daily aggregation, that part was left a bit ambiguous.
 
I tried what you are suggesting. It didn't work. I think what it was doing is only triggering when all the criteria triggered within the 5 days. Said differently, say 3 of the 4 indicators trigger 3 weeks before the 4th indicator. The 4th indicator triggers. The scanner will not find it because it is more than 5 days since the 3 other indicators trigger.

Need a sticky trigger somehow.

Thanks,
Dave

Four of which bars? For example - would the weekly conditions need to be within four weeks, and the daily conditions within four days? Or, would all of it just need to be within four days? Also, confirm for me that the RSI is based on the daily aggregation, that part was left a bit ambiguous.
The high level explanation is I want to find stocks that have recently met 4 criteria. There are a lot of stocks that meet the criteria. I want to find the 'new' ones with the scan. I don't always get a chance to scan every day so the 5 day cushion. I'm using daily and weekly aggregations.

Here is a summary of the indicators that I'm currently using and their aggregations.

Indicator, Aggregation
10 wk SMA - Weekly
40 wk SMA - Weekly
200 daily SMA - Daily
Rel Strength vs S&P increasing - Daily

You mentioned RSI. Is that a typo for RS?

Thanks,
Dave
 
Last edited by a moderator:
Run this using the Daily aggregation in the scanner.

Code:
def weekMA;
def FortyWeek;
def DOW =
    GetdayOfWeek(getyyYYMMDD());
def isFriday =
    DOW == 5 or DOW > DOW[-1];
if !isNaN(close) and isNaN(close[-5]) {
    weekMA = ((Fold Index = 0 to Max(0,BarNumber())
    with Data while Data < 10000000 do
    if GetValue(isFriday,Index) == Yes or index == 0
    then Data + 1000000 + GetValue(Close,Index)
    else Data) % 10000000) / 10;
    FortyWeek = ((Fold I = 0 to Max(0,BarNumber())
    with D while D < 40000000 do
    if GetValue(isFriday,I) == Yes or I == 0
    then D + 1000000 + GetValue(Close,I)
    else D) % 10000000) / 40;
} else {
    weekMA = Double.NaN;
    fortyweek = Double.NaN;
};
def close2 = close("SPY");
def RS = if close2 == 0 then 0 else close/close2;
def RSAvgLength = 50;
def averageType = AverageType.SIMPLE;
def RSAvg = MovingAverage(averageType, RS, RSAvgLength);
def RSAvgInc = RSAvg[1] < RSAvg;


plot Scan = (
close > weekma 
and close > FortyWeek 
and close > average(close,200) 
and RSAvgInc) 
within 5 bars;
 

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
228 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top