# Create scan based on difference in Earnings

#### hdsouza

##### New member
I want to get a scan of all stocks which have at least a 100% Earnings increase over the last year.
For example for stock symbol PENN , Current earnings = 0.93 and the Earning a year ago is 0.38
So percentage Earnings increase over the year = ((0.93 * 100)/0.38) - 100 = 144

Here is the TOS script I wrote, but it does not give me any stocks when I run the scan.
Code:
``````declare lower;
def EPS = GetActualEarnings();
def EPS_YearAgo = EPS[-5];
def EPS_YearDiff = ((EPS * 100) / EPS_YearAgo) - 100;
def EPS_Good = EPS_YearDiff is greater than 100;
plot scan = EPS_Good;``````

Please help. Thanks

#### XeoNoX

##### Well-known member
VIP
You can test it with the label first in a chart and make sure the values are right as I didn't check for formulas to see if you are calculating right, you are going to have to aggregate by DAY so to get years back you have to count by trading days.

Code:
``````def Get_earnings = if IsNaN(GetActualEarnings()) then 0 else GetActualEarnings();
def EPS_YearAgo = Sum(Get_earnings, 252);
def EPS_2YearAgo = Sum(Get_earnings, 504);;
def EPS_YearDiff = ((EPS_YearAgo * 100) /  EPS_2YearAgo) - 100;
def EPS_Good = EPS_YearDiff is greater than 100;
def test = EPS_Good;

AddLabel (yes, "              TEST " +  (  EPS_YearDiff)  );``````

• hdsouza

#### hdsouza

##### New member
Thanks @XeoNoX . That set me on the right path. I was able to get it to work right. except when there is no EPS a year ago (stock = BRMK), then the EPSGrowthYoY becomes infinity.

To correct this if I add: if (EPS == 0) then {EPSGrowthYoY = 0;} it errors out.

Code:
``````declare lower;
def EPS = if !isNaN(GetActualEarnings()) then GetActualEarnings() else EPS;
def EPSGrowthYoY = 100*(EPS - EPS)/AbsValue(EPS);
#if (EPS == 0) then {EPSGrowthYoY = 0;}
def EPS_Good =  EPSGrowthYoY is greater than 100;
plot scan = EPS_Good;
AddLabel(yes, "EPSGrowthYoY% = " + EPSGrowthYoY+"%", color = Color.Blue);``````

• XeoNoX

#### XeoNoX

##### Well-known member
VIP
congrats and thanks for sharing!

#### trendr

##### Member
Thanks @XeoNoX . That set me on the right path. I was able to get it to work right. except when there is no EPS a year ago (stock = BRMK), then the EPSGrowthYoY becomes infinity.

To correct this if I add: if (EPS == 0) then {EPSGrowthYoY = 0;} it errors out.

Code:
``````declare lower;
def EPS = if !isNaN(GetActualEarnings()) then GetActualEarnings() else EPS;
def EPSGrowthYoY = 100*(EPS - EPS)/AbsValue(EPS);
#if (EPS == 0) then {EPSGrowthYoY = 0;}
def EPS_Good =  EPSGrowthYoY is greater than 100;
plot scan = EPS_Good;
AddLabel(yes, "EPSGrowthYoY% = " + EPSGrowthYoY+"%", color = Color.Blue);``````
where you able to fix the infinity error @hdsouza, also I have question if you don't mind. If want to scan for 30% I would just change the code "def EPS_Good = EPSGrowthYoY is greater than 30;" ? Thanks!

#### hdsouza

##### New member
I believe the infinity problem was solved with:
if EPS == 0 then {EPSGrowthYoY = 0;} else {EPSGrowthYoY = 100*(EPS - EPS)/AbsValue(EPS);}
So the final code is:
Code:
``````declare lower;
def EPS = if !isNaN(GetActualEarnings()) then GetActualEarnings() else EPS;
def EPSGrowthYoY;
if EPS == 0 then {EPSGrowthYoY = 0;} else {EPSGrowthYoY = 100*(EPS - EPS)/AbsValue(EPS);}
AddLabel(yes, "EPSGrowthYoY% = " + EPSGrowthYoY+"%", if EPSGrowthYoY < 100 then Color.red else Color.blue);
AddLabel(yes, "EPS = " + EPS, color = Color.DARK_GREEN);
AddLabel(yes, "EPS_YearAgo = " +  EPS, color = Color.DARK_GREEN);``````

• trendr

#### trendr

##### Member
I believe the infinity problem was solved with:
if EPS == 0 then {EPSGrowthYoY = 0;} else {EPSGrowthYoY = 100*(EPS - EPS)/AbsValue(EPS);}
So the final code is:
Code:
``````declare lower;
def EPS = if !isNaN(GetActualEarnings()) then GetActualEarnings() else EPS;
def EPSGrowthYoY;
if EPS == 0 then {EPSGrowthYoY = 0;} else {EPSGrowthYoY = 100*(EPS - EPS)/AbsValue(EPS);}
AddLabel(yes, "EPSGrowthYoY% = " + EPSGrowthYoY+"%", if EPSGrowthYoY < 100 then Color.red else Color.blue);
AddLabel(yes, "EPS = " + EPS, color = Color.DARK_GREEN);
AddLabel(yes, "EPS_YearAgo = " +  EPS, color = Color.DARK_GREEN);``````
Thank you!!

#### trendr

##### Member
Hey Guys, Thanks for all the work! I was wondering if someone could help me convert this scan to earnings for last two quarters > 40%. Any help would be greatly appreciated.

#### hdsouza

##### New member
The formula above is for 1 year.. which is 262 working days.
If instead of that you want 2 quarters then you have to replace 262 with that (in number of days).

• trendr

#### trendr

##### Member
The formula above is for 1 year.. which is 262 working days.
If instead of that you want 2 quarters then you have to replace 262 with that (in number of days).
Thank you sir!

#### danpadcas

##### New member
Hi

Once you add the customized study in the scanner which following criteria do you add in order to be able to start scanning through the market? Thank you all for your outstanding work.