Back to website

Typical Volume by Time of Day

by TrendSpider Team
+ Add to TrendSpider

Similar to the previous indicator but focuses solely on time of day, highlighting volume columns above the average for a given time. Suitable for intraday charts.

Developer Info

Source Code

describe_indicator('Volume by time of a day', 'lower');

if (isNaN(constants.resolution)) {
	throw Error('This indicator can only work on intraday charts');
}

const volumeByTime = {};
const lookbackPeriod = input('Lookback', 5, { min: 1, max: 10 });

const averageVolumes = for_every(volume, time, (v, t) => {
	const timeOfDay = time_of(t);
	const timeHash = `${timeOfDay.hours}:${timeOfDay.minutes}`;

	if (!volumeByTime[timeHash]) {
		volumeByTime[timeHash] = [];
	}

	const avgVolume = volumeByTime[timeHash].
		slice(-lookbackPeriod).
		reduce((result, value) => result + value, 0) / lookbackPeriod;

	volumeByTime[timeHash].push(v); 

	return volumeByTime[timeHash].length >= lookbackPeriod + 1
		? avgVolume
		: null;
});

const volumeColors = for_every(close, open, averageVolumes, volume, (c, o, avg, vol) => {
	if (vol > avg) {
		return 'orange';
	}
	
	return '#999';
});

fill(
	paint(averageVolumes, { style: 'line', hidden: true }),
	paint(series_of(0), { style: 'line', hidden: true }),
	'#ffffff',
	0.2
);

paint(volume, { style: 'histogram', color: volumeColors });