# PEG Ratio scan study for ThinkorSwim

#### TomM

##### New member
I'm trying to create a script to scan for PEG ratio. Price to Earnings / Growth where growth is the average trailing 5 years growth ideally. See https://www.investopedia.com/ask/answers/06/pegratioearningsgrowthrate.asp

I'd also like to have it show up as a column in the scan results if possible. I came up with something but it's not working. This is my first attempt at thinkscript after looking at some examples online. I'm not sure if this is the best way to get the anuallized trailing earnings or if there is something better.

Can someone please help me figure this out?

Code:
``````def AE = if IsNaN(GetActualEarnings()) then 0 else GetActualEarnings();
def EPS_TTM = Sum(AE, 252);#The sum of the trailing twelve months EPS
def EPS_24= Sum(AE, 252);#The sum of the trailing 24 months EPS
def EPS_PY = EPS_24 - EPS_TTM; # prioryear last 24 - last 12
def GROWTH = (EPS_TTM - EPS_PY)/EPS_PY * 100;
def PE = close/ EPS_TTM;
def PEG = PE/GROWTH;
plot scan = PEG;``````

#### traderintheroom

##### Member
I will try to play with this over the weekend. I manually track this from Finviz into Thinkorswim.

#### TomM

##### New member
I will try to play with this over the weekend. I manually track this from Finviz into Thinkorswim.
Thank you. Looking forward to seeing how you do it.

#### urinedeep

##### New member
I'm trying to create a script to scan for PEG ratio. Price to Earnings / Growth where growth is the average trailing 5 years growth ideally. See https://www.investopedia.com/ask/answers/06/pegratioearningsgrowthrate.asp

I'd also like to have it show up as a column in the scan results if possible. I came up with something but it's not working. This is my first attempt at thinkscript after looking at some examples online. I'm not sure if this is the best way to get the anuallized trailing earnings or if there is something better.

Can someone please help me figure this out?

Code:
``````def AE = if IsNaN(GetActualEarnings()) then 0 else GetActualEarnings();
def EPS_TTM = Sum(AE, 252);#The sum of the trailing twelve months EPS
def EPS_24= Sum(AE, 252);#The sum of the trailing 24 months EPS
def EPS_PY = EPS_24 - EPS_TTM; # prioryear last 24 - last 12
def GROWTH = (EPS_TTM - EPS_PY)/EPS_PY * 100;
def PE = close/ EPS_TTM;
def PEG = PE/GROWTH;
plot scan = PEG;``````
I think for the "EPS_24" you have to use 404(as number of days) to account for 24 months, otherwise you would get a 0 for EPS_PY and you (also can't divide by zero) def AE = if IsNaN(GetActualEarnings()) then 0 else GetActualEarnings();
def EPS_TTM = Sum(AE, 252);#The sum of the trailing twelve months EPS
def EPS_24= Sum(AE, 404);#The sum of the trailing 24 months EPS
def EPS_PY = EPS_24 - EPS_TTM; # prioryear last 24 - last 12
def GROWTH = ((EPS_TTM - EPS_PY)/EPS_PY) * 100;
def PE = close/ EPS_TTM;
def PEG = PE/GROWTH;
def undervalued = PEG <1;
plot scan = undervalued;