This indicator had been implemented by TrendSpider in JavaScript on TrendSpider. Check out the developer documentation to learn more about JS on TrendSpider.
// This script had been cloned from "P/E Ratio" at 16 Jan 2024, 12:03
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: 'black' });