P/E Ratio

A custom indicator created by TrendSpider Team on TrendSpider. You can import this custom indicator into your TrendSpider account. Don't have TrendSpider? Create an account first, then import the custom indicator.

Chart featuring the P/E Ratio indicator

This indicator paints the trailing P/E ratio for a given ticker symbol.

Source code

This indicator had been implemented by TrendSpider Team in JavaScript on TrendSpider. Check out the developer documentation to learn more about JS on TrendSpider.

describe_indicator('P/E Ratio', 'lower');

const earnings = await request.earnings(constants.ticker);

if (!Array.isArray(earnings)) {
	return;
}

const pastEarnings = earnings.filter(record => !record.isFuture);

const epsMathType = input('P/E Type', 'Trailing', ['Qtr', 'Trailing']);
let resultingLine = null;

const epsLanded = land_points_onto_series(
	pastEarnings.map(record => record.timestamp),
	pastEarnings.map(record => record.eps),
	time,
	'ge'
);

if (epsMathType == 'Qtr') {	

	const epsInterpolated = interpolate_sparse_series(epsLanded, 'constant');
	resultingLine = for_every(close, epsInterpolated, (c, eps) => c / eps); 

}
else if (epsMathType == 'Trailing') {	
	resultingLine = close.map((c, index) => {
		const last4QtrEPS = epsLanded.
			slice(0, index).
			filter(Boolean).
			slice(-4).
			reduce((result, value) => result + value, 0);
			
		return c / last4QtrEPS;
	});

}
else {
	throw Error('unknown pe type requested');
}

paint(resultingLine, { name: 'PE Ratio', color: 'grey' });