OPEN-SOURCE SCRIPT
Telah dikemas kini

Filt ADR

78
🟠 Script Name: Filtered Average Daily Range (Filt ADR)
This script calculates a filtered version of the Average Daily Range (ADR) based on the last 14 daily candles. It's designed to reduce the influence of unusually high or low daily ranges (outliers) by applying a filter before calculating the average.

🔧 How It Works — Step by Step
1. Calculate Daily Ranges (High - Low)
It retrieves the daily price ranges (difference between daily high and low) for the last 14 days using request.security() with the "D" (daily) timeframe.

pinescript
Копировать
Редактировать
high[0] - low[0] // today's daily range
high[1] - low[1] // yesterday's daily range
...
These values are stored into individual variables dr0 to dr13.

2. Build an Array of Daily Ranges
An array named ranges is used to store the 14 daily ranges, but only if they are not na (missing data). This avoids errors during processing.

3. Calculate the Initial (Unfiltered) Average Range
The script sums all values in the ranges array and calculates their average:

pinescript
Копировать
Редактировать
avg_all = total sum of ranges / number of valid entries
4. Filter Out Outliers
Now it filters the values in ranges:

Only keeps the ranges that are between 0.5×avg_all and 2×avg_all.

This is to remove abnormally small or large daily ranges that could distort the average.

The filtered values are added to a second array called filtered.

5. Calculate the Filtered ADR
Finally, it calculates the average of the filtered daily ranges:

pinescript
Копировать
Редактировать
avg_filt = sum of filtered ranges / number of filtered values
This is the Filtered ADR.

6. Plot the Result
The result (avg_filt) is plotted as an orange line on the chart. It updates on each bar (depending on the current timeframe you're viewing) but the underlying data is based on the last 14 daily candles.

pinescript
Копировать
Редактировать
plot(avg_filt, title="Filtered ADR", color=color.orange, linewidth=2)
✅ Use Case
This script is useful for traders who use the Average Daily Range (ADR) to:

Estimate expected price movement during a day

Set volatility-based stop-loss or take-profit levels

Identify days with unusually high or low volatility

By filtering out extreme values, it provides a more stable and reliable estimate of daily volatility.
Nota Keluaran
🔧 General Idea:
The script displays the average daily range, but it excludes outlier days — those that are too small or too large compared to others. This makes the indicator more resistant to anomalies, such as gaps or high-volatility news days.

📌 Detailed Logic:
1. Collecting Daily Ranges for the Last 14 Days
pinescript
Копировать
Редактировать
drs = array.from(request.security(syminfo.tickerid, "D", high[0] - low[0]), ...)
Retrieves the daily range (high - low) for today and the previous 13 days.

request.security(..., "D", ...) is used to get daily data even when the indicator runs on a lower timeframe (e.g., 15-minute chart).

All values are stored in the drs array.

2. Calculating the Unfiltered Average
pinescript
Копировать
Редактировать
sum_all = 0.0
count_all = 0
for i = 0 to array.size(drs) - 1
val = array.get(drs, i)
if not na(val)
sum_all += val
count_all += 1
avg_all = count_all > 0 ? sum_all / count_all : na
Sums up all non-na values and counts how many were used.

Computes the simple average (avg_all) of all daily ranges.

3. Filtering Outliers
pinescript
Копировать
Редактировать
for i = 0 to array.size(drs) - 1
val = array.get(drs, i)
if not na(val) and val >= 0.5 * avg_all and val <= 2.0 * avg_all
sum_filt += val
count_filt += 1
avg_filt = count_filt > 0 ? sum_filt / count_filt : na
Keeps only those values between 0.5 and 2.0 times the unfiltered average.

This excludes unusually low (quiet days) or unusually high (volatile days) ranges.

Calculates the filtered average (avg_filt) based on the remaining data.

4. Displaying on the Chart
pinescript
Копировать
Редактировать
plot(avg_filt, title="Filtered ADR", color=color.new(color.orange, 0), linewidth=2)
Plots the avg_filt result as a line on the chart.

The line is orange, fully opaque (opacity = 0), with a width of 2.

✅ Result:
You get a smoothed average daily range over the last 14 days, excluding anomalies. This can be useful for:

Evaluating normal market volatility;

Building ATR-based reaction zones;

Setting stop-losses/take-profits based on average price movement.

Penafian

Maklumat dan penerbitan adalah tidak dimaksudkan untuk menjadi, dan tidak membentuk, nasihat untuk kewangan, pelaburan, perdagangan dan jenis-jenis lain atau cadangan yang dibekalkan atau disahkan oleh TradingView. Baca dengan lebih lanjut di Terma Penggunaan.