Skip to Main Content
Back to website
Indicator Disclaimer | Report this indicator

General Idea

This indicator "Timeframe ATR Bands" #TSBuild25 creates ATR (Average True Range) bands based on a higher timeframe. It paints several lines on the chart representing the mid-point, high and low bands, and extended high and low bands. These bands are calculated using the ATR from a higher timeframe and the opening price of each period in that timeframe.

Usage

This indicator can be used to identify potential support and resistance levels based on volatility from a higher timeframe. The bands can help traders visualize price action in relation to higher timeframe volatility, potentially aiding in decision-making for entries, exits, and stop placements.

Detailed Explanation

  1. The indicator fetches data from a higher timeframe (selectable by the user) and calculates the ATR on this timeframe.
  2. It then maps this higher timeframe ATR onto the current timeframe chart.
  3. For each period in the higher timeframe, it calculates several lines:
    • A mid-line based on the opening price of the period
    • High and low lines at half the ATR above and below the mid-line
    • Extended high and low lines at the full ATR above and below the mid-line
  4. These lines are painted on the chart, with the areas between the high/low lines and their extensions filled with a light color.
  5. The indicator adapts to the selected higher timeframe, recalculating the bands at the start of each new period in that timeframe.

This approach allows traders to see how price moves in relation to the volatility of a higher timeframe, which can be particularly useful for identifying potential areas of support and resistance or for gauging whether the current price action is relatively calm or volatile compared to the higher timeframe.

// ░▒▓████████▓▒░▒▓███████▓▒░ ░▒▓██████▓▒░░▒▓███████▓▒░░▒▓████████▓▒░       ░▒▓███████▓▒░▒▓████████▓▒░▒▓████████▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓████████▓▒░▒▓███████▓▒░ ░▒▓███████▓▒░ 
//    ░▒▓█▓▒░   ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░             ░▒▓█▓▒░      ░▒▓█▓▒░      ░▒▓█▓▒░      ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░      ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░        
//    ░▒▓█▓▒░   ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░             ░▒▓█▓▒░      ░▒▓█▓▒░      ░▒▓█▓▒░      ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░      ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░        
//    ░▒▓█▓▒░   ░▒▓███████▓▒░░▒▓████████▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓██████▓▒░         ░▒▓██████▓▒░░▒▓██████▓▒░ ░▒▓██████▓▒░ ░▒▓███████▓▒░░▒▓██████▓▒░ ░▒▓███████▓▒░ ░▒▓██████▓▒░  
//    ░▒▓█▓▒░   ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░                    ░▒▓█▓▒░▒▓█▓▒░      ░▒▓█▓▒░      ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░      ░▒▓█▓▒░░▒▓█▓▒░      ░▒▓█▓▒░ 
//    ░▒▓█▓▒░   ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░                    ░▒▓█▓▒░▒▓█▓▒░      ░▒▓█▓▒░      ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░      ░▒▓█▓▒░░▒▓█▓▒░      ░▒▓█▓▒░ 
//    ░▒▓█▓▒░   ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓███████▓▒░░▒▓████████▓▒░      ░▒▓███████▓▒░░▒▓████████▓▒░▒▓████████▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓████████▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓███████▓▒░                                                                                                                                                                      

describe_indicator('Timeframe ATR Bands', { decimals: 2, warmup: '500' });

// inputs
const timeframeInput = input.select('Timeframe', 'W', constants.time_frames);
const atrLengthInput = input.number('ATR Length', 52, { min: 1, max: 500 });

// logic
// Fetch data from higher timeframe
const timeframeData = await request.history(current.ticker, timeframeInput);
assert(!timeframeData.error, `Error fetching data: ${timeframeData.error}`);

// Calculate ATR on higher timeframe
const avgTrueRange = atr(timeframeData.high, timeframeData.low, timeframeData.close, atrLengthInput);

// Land ATR onto current timeframe
const atrLanded = land_points_onto_series(timeframeData.time, avgTrueRange, time, 'ge');
const atrInterpolated = interpolate_sparse_series(atrLanded, 'constant');

// Initialize series for our lines
const midLine = series_of(null);
const highLine = series_of(null);
const lowLine = series_of(null);
const highExtension = series_of(null);
const lowExtension = series_of(null);

// Identify the start of each higher timeframe period
const periodsStart = [];
for (let i = 0; i < time.length; i++) {
    if (i === 0 || bar_at(time[i], timeframeInput).session !== bar_at(time[i-1], timeframeInput).session) {
        periodsStart.push(i);
    }
}

// Calculate lines
for (let i = 0; i < periodsStart.length; i++) {
    const startIndex = periodsStart[i];
    const endIndex = i < periodsStart.length - 1 ? periodsStart[i+1] : time.length;
    const periodOpen = open[startIndex];
    const periodAtr = atrInterpolated[startIndex];
    
    for (let j = startIndex; j < endIndex; j++) {
        midLine[j] = periodOpen;
        highLine[j] = periodOpen + periodAtr / 2;
        lowLine[j] = periodOpen - periodAtr / 2;
        highExtension[j] = periodOpen + periodAtr;
        lowExtension[j] = periodOpen - periodAtr;
    }
}

// plots
const midLinePainted = paint(midLine, { color: 'orange', name: 'Mid', style: 'ladder' });
const highLinePainted = paint(highLine, { color: 'red', name: 'High', style: 'dotted' });
const lowLinePainted = paint(lowLine, { color: 'green', name: 'Low', style: 'dotted' });
const highExtensionPainted = paint(highExtension, { color: 'red', name: 'High Extension', style: 'ladder' });
const lowExtensionPainted = paint(lowExtension, { color: 'green', name: 'Low Extension', style: 'ladder' });

fill(highLinePainted, highExtensionPainted, 'red', 0.05, 'High Fill');
fill(lowLinePainted, lowExtensionPainted, 'green', 0.05, 'Low Fill');