Scan for above or below average daily range


Hi, I’m trying to use the below to scan for stocks closing above/below the respective ADR levels on an intraday time (2min, 5min, etc..).  Currently only works for daily closes. Thanks so much.
**Script below**

# Define length for ADR calculation
input length = 14;

# Calculate daily high, low, and range
def dayHigh = DailyHighLow(AggregationPeriod.DAY).Dailyhigh;
def dayLow = DailyHighLow(AggregationPeriod.DAY).DailyLow;
def dayRange = dayHigh – dayLow;

# Calculate average daily range
def AvgRange = Average(dayRange, length);

# Calculate ADR_H and ADR_L
def ADR_H = open + (AvgRange / 2);
def ADR_L = open – (AvgRange / 2);

# Output signals for scan
def isAboveADR_H = close >= ADR_H;
def isBelowADR_L = close <= ADR_L;

# Output for scan
plot scanCondition = isAboveADR_H or isBelowADR_L;

Marked as spam
Posted by (Questions: 1, Answers: 1)
Asked on February 5, 2024 9:31 am
Private answer

First and most important item. It is impossible to use your solution on an intraday time frame. Custom scans on Thinkorswim do not permit you to reference a higher time frame within a Study Filter. Since you are computing a daily average of the daily range, you must use a daily time frame.

And even if we reconfigured your code to synthesize the daily data within the intraday time frame, the scan is not able to look back any further than 10-11 days on an intraday time frame. So the length of 14 exceeds that value and even makes it impossible to use synthesized daily data to compute the values in your example.

There is actually an exception to that rule, but you would have to use at least a 1 hour intraday time frame to have access to more than 14 days of historical data.

So for the current version of Thinkorswim, you must use a daily time frame in your Study Filter in order to get this to work. And the following code will accomplish that:

input length = 14;
def dayRange = high – low;
def AvgRange = Average(dayRange, length);
def adrHigh = open + (AvgRange / 2);
def adrLow = open – (AvgRange / 2);
def isAboveAdrHigh = close >= adrHigh;
def isBelowAdrLow = close <= adrLow;
plot scanCondition = isAboveAdrHigh or isBelowAdrLow;

And yes, such a scan would give you immediate results as soon as the daily close passed through the levels from the average daily range you have computed. Meaning that even when using a daily time frame in your scan, you will get real time results from that scan throughout the trading session.

The screenshot below shows the elements of your scan on the price graph, including the signals. The lower subgraph shows when your scan would return a true result for this stock.

Marked as spam
Posted by (Questions: 37, Answers: 4084)
Answered on February 5, 2024 9:48 am
You're amazing, thanks!
( at February 5, 2024 10:17 pm)