# peg_ratio_lower_0
# https://usethinkscript.com/threads/add-a-column-to-a-watchlist-that-shows-the-peg-ratio.12043/
#Add a column to a Watchlist that shows the PEG Ratio
declare lower;
def bn = barnumber();
def na = double.nan;
def isyear = if GetLastYear() == GetYear() then 1 else 0;
def isprevyear = if GetLastYear() == GetYear()+1 then 1 else 0;
def eps_yr = round(EarningsPerShareTTM (fiscalPeriod = FiscalPeriod.YEAR), 2);
def eps_prev_yr = if bn == 1 then 0 else if isprevyear then eps_yr else eps_prev_yr[1];
# formulas from investopedia , see below
# PE_ratio = share price / EPS
# Earnings growth rate = 100 * (( EPS this year / EPS last year ) - 1)
# PEG ratio = PE ratio / Earnings growth rate
def share = close;
def pe_ratio = (share / eps_yr);
def earnings_growth_rate = 100 * ((eps_yr / eps_prev_yr) - 1);
def peg_ratio = (pe_ratio / earnings_growth_rate);
addlabel(1, "EPS this year " + eps_yr, color.yellow);
addlabel(1, "EPS previous year " + eps_prev_yr, color.yellow);
addlabel(1, " ", color.black);
addlabel(1, "PE ratio " + pe_ratio, color.yellow);
addlabel(1, "Earnings growth rate " + earnings_growth_rate, color.yellow);
addlabel(1, " ", color.black);
addlabel(1, "PEG ratio " + peg_ratio, color.yellow);
#--------------------------------
# test stuff
input test1_lines = no;
plot zeps = eps_yr;
zeps.setdefaultcolor(color.yellow);
zeps.setHiding(!test1_lines);
plot zpeps = eps_prev_yr;
zpeps.setdefaultcolor(color.magenta);
zpeps.setHiding(!test1_lines);
input test2_bubbles = no;
#addchartbubble(test2_bubbles, 0, isyear, (if isyear then color.yellow else color.gray), yes);
addchartbubble(test2_bubbles, 0, isprevyear, (if isprevyear then color.yellow else color.gray), no);
#https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Stock-Fundamentals/EarningsPerShareTTM
#EarningsPerShareTTM
#EarningsPerShareTTM ( Symbol symbol, int fiscalPeriod);
#Default values:
#symbol: current symbol
#fiscalPeriod: FiscalPeriod.YEAR
#Description
#The EarningsPerShareTTM function returns the value of trailing 12-month earnings per share for the specified symbol. The trailing 12-month earnings per share is calculated as the ratio of company's profit to the number of its outstanding shares of common stock over the last 12 months. By default, the value is returned for the currently selected symbol and based on the annual reporting data. To specify a different symbol for this function, modify the value of the symbol input parameter. To use quarterly data instead of annual, assign the fiscalPeriod.QUARTER constant to the fiscalPeriod input parameter.
#https://www.investopedia.com/terms/p/pegratio.asp
#According to well-known investor Peter Lynch, a company's P/E and expected growth should be equal, which denotes a fairly valued company and supports a PEG ratio of 1.0. When a company's PEG exceeds 1.0, it's considered overvalued while a stock with a PEG of less than 1.0 is considered undervalued.
#Example of How to Use the PEG Ratio
#The PEG ratio provides useful information to compare companies and see which stock might be the better choice for an investor's needs, as follows.
#Assume the following data for two hypothetical companies, Company A and Company B:
#Company A:
#Price per share = $46
#EPS this year = $2.09
#EPS last year = $1.74
#Company B
#Price per share = $80
#EPS this year = $2.67
#EPS last year = $1.78
#Given this information, the following data can be calculated for each company.
#Company A
#P/E ratio = $46 / $2.09 = 22
#Earnings growth rate = ($2.09 / $1.74) - 1 = 20%
#PEG ratio = 22 / 20 = 1.1
#Company B
#P/E ratio = $80 / $2.67 = 30
#Earnings growth rate = ($2.67 / $1.78) - 1 = 50%
#PEG ratio = 30 / 50 = 0.6
#Many investors may look at Company A and find it more attractive since it has a lower P/E ratio between the two companies. But compared to Company B, it doesn't have a high enough growth rate to justify its P/E. Company B is trading at a discount to its growth rate and investors purchasing it are paying less per unit of earnings growth.
#