Skip to Main Content

Painting Earnings on a Chart

This script paints pre-market range over the price action.

describe_indicator('Pre-market range');

// 30 is the highest time frame which still has candles not overlapping with
// the market session
const extHoursData = await request.history(constants.ticker, '30', { ext_session: true });

const preMarketRangeByDay = {};

// first, we compute { high, low } of pre-market
// per each day we have in our ext data session
for (let extHoursIndex = 0; extHoursIndex < extHoursData.time.length; extHoursIndex += 1) {
    const sessionAtCandle = session_of(
        extHoursData.time[extHoursIndex],
        constants.resolution,
        constants.ext_session_premarket
    );

    const dayOfCandle = sessionAtCandle.session;

    if (!preMarketRangeByDay[dayOfCandle]) {
        preMarketRangeByDay[dayOfCandle] = {
            high: extHoursData.high[extHoursIndex],
            low: extHoursData.low[extHoursIndex]
        };
    }
    else {
        preMarketRangeByDay[dayOfCandle].high = Math.max(preMarketRangeByDay[dayOfCandle].high, extHoursData.high[extHoursIndex]);
        preMarketRangeByDay[dayOfCandle].low = Math.min(preMarketRangeByDay[dayOfCandle].low, extHoursData.low[extHoursIndex]);
    }
}

const rangeHigh = series_of(null);
const rangeLow = series_of(null);

// now we assigning "pre-market high/low range" to each candle on the chart
for (let candleIndex = 0; candleIndex < time.length; candleIndex += 1) {
    const sessionAtCandle = session_of(time[candleIndex], constants.resolution);
    const extHoursRange = preMarketRangeByDay[sessionAtCandle.session];

    if (extHoursRange) {
        rangeHigh[candleIndex] = extHoursRange.high;
        rangeLow[candleIndex] = extHoursRange.low;
    }
}

fill(
    paint(rangeHigh, { hidden: true, style: 'ladder' }),
    paint(rangeLow, { hidden: true, style: 'ladder' }),
    'blue'
);
Painting Earnings on a Chart