# 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

Solution
thank you for the hard work. Can you please paste your scan code here? Could not figure that out and my scan does not produce any results
shared scan link: http://tos.mx/ZERVkCa Click here for --> Easiest way to load shared links

Ruby:
``````def EPS = if !isNaN(GetActualEarnings()) then GetActualEarnings() else EPS[1];
def EPSGrowthQoQ;
if EPS[65] == 0 then {EPSGrowthQoQ = 0;} else {EPSGrowthQoQ = 100*(EPS - EPS[50])/AbsValue(EPS[50]);}

def GQ = EPSGrowthQoQ ;

plot scan = GQ > 30 ;``````
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)  );``````

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[252] == 0) then {EPSGrowthYoY = 0;} it errors out.

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

congrats and thanks for sharing!

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[252] == 0) then {EPSGrowthYoY = 0;} it errors out.

Code:
``````declare lower;
def EPS = if !isNaN(GetActualEarnings()) then GetActualEarnings() else EPS[1];
def EPSGrowthYoY = 100*(EPS - EPS[252])/AbsValue(EPS[252]);
#if (EPS[252] == 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!

I believe the infinity problem was solved with:
if EPS[252] == 0 then {EPSGrowthYoY = 0;} else {EPSGrowthYoY = 100*(EPS - EPS[252])/AbsValue(EPS[252]);}
So the final code is:
Code:
``````declare lower;
def EPS = if !isNaN(GetActualEarnings()) then GetActualEarnings() else EPS[1];
def EPSGrowthYoY;
if EPS[252] == 0 then {EPSGrowthYoY = 0;} else {EPSGrowthYoY = 100*(EPS - EPS[252])/AbsValue(EPS[252]);}
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[252], color = Color.DARK_GREEN);``````

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.

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).

Can someone or @BenTen confirm if I am editing the code correctly please? Trying scan for stocks that exceeds their preview quarterly earning by 30% or more.
declare lower;
def EPS = if !isNaN(GetActualEarnings()) then GetActualEarnings() else EPS[1];
def EPSGrowthYoY = 30*(EPS - EPS[65])/AbsValue(EPS[65]);
#if (EPS[252] == 0) then {EPSGrowthYoY = 0;}
def EPS_Good = EPSGrowthYoY is greater than 30;
plot scan = EPS_Good;
#AddLabel(yes, "EPSGrowthYoY% = " + EPSGrowthYoY+"%", color = Color.Blue);

I made some changes to this code to show the %gain from quarter to quarter. It also properly colors the bubbles if the gain is negative.

declare lower;
declare Hide_on_intraday;

def EPS = if !isNaN(GetActualEarnings()) then GetActualEarnings() else EPS[1];
def EPSGrowthQoQ;
if EPS[65] == 0 then {EPSGrowthQoQ = 0;} else {EPSGrowthQoQ = 100*(EPS - EPS[50])/AbsValue(EPS[50]);}

plot EPS_line_chart = GetActualEarnings();
EPS_line_chart.EnableApproximation();
EPS_line_chart.SetDefaultColor(color.orange);

plot earnings_date = GetActualEarnings();
earnings_date.SetPaintingStrategy(PaintingStrategy.squares);

earnings_date.AssignValueColor(if EPS > EPS[1] then color.dark_green else color.black);
earnings_date.SetLineWeight(5);

### This part added by coping and pasting someone else's script
DefineGlobalColor(“lime”, Color.lime);
def price = close;
def estimate = GetEstimatedEarnings();
def actual = GetActualearnings();
def get_price = if estimate and actual then price else get_price[1];
def gain = (actual – estimate);
AddChartBubble(actual,actual, “EPS: ” + actual + ", " + EPSGrowthQoQ+"%" , if EPSGrowthQoQ < 0 then Color.red else Color.green);
# end code

I made some changes to this code to show the %gain from quarter to quarter. It also properly colors the bubbles if the gain is negative.

declare lower;
declare Hide_on_intraday;

def EPS = if !isNaN(GetActualEarnings()) then GetActualEarnings() else EPS[1];
def EPSGrowthQoQ;
if EPS[65] == 0 then {EPSGrowthQoQ = 0;} else {EPSGrowthQoQ = 100*(EPS - EPS[50])/AbsValue(EPS[50]);}

plot EPS_line_chart = GetActualEarnings();
EPS_line_chart.EnableApproximation();
EPS_line_chart.SetDefaultColor(color.orange);

plot earnings_date = GetActualEarnings();
earnings_date.SetPaintingStrategy(PaintingStrategy.squares);

earnings_date.AssignValueColor(if EPS > EPS[1] then color.dark_green else color.black);
earnings_date.SetLineWeight(5);

### This part added by coping and pasting someone else's script
DefineGlobalColor(“lime”, Color.lime);
def price = close;
def estimate = GetEstimatedEarnings();
def actual = GetActualearnings();
def get_price = if estimate and actual then price else get_price[1];
def gain = (actual – estimate);
AddChartBubble(actual,actual, “EPS: ” + actual + ", " + EPSGrowthQoQ+"%" , if EPSGrowthQoQ < 0 then Color.red else Color.green);
# end code
Thanks for updating the code. Is possible to convert this code for scan? I would like to scan for stocks that increased in their earnings.

Yes you can do it. In order to use this study in a scan you need to plot EPSGrowthQoQ instead of using def. When you do that the lower study will not look so good. My suggestion is if you want to use the study in it's current state and not have it look wonky, create a 2nd study for scanning purposes called Earnings_Scan. Then change def to plot. From there you can add a study filter into your scan, pick Earnings_Scan. Select EPSGrowthQoQ is greater than 30.

Just did it for fun, works like a charm

Last edited by a moderator:
Yes you can do it. In order to use this study in a scan you need to plot EPSGrowthQoQ instead of using def. When you do that the lower study will not look so good. My suggestion is if you want to use the study in it's current state and not have it look wonky, create a 2nd study for scanning purposes called Earnings_Scan. Then change def to plot. From there you can add a study filter into your scan, pick Earnings_Scan. Select EPSGrowthQoQ is greater than 30.

Just did it for fun, works like a charm
thank you for the hard work. Can you please paste your scan code here? Could not figure that out and my scan does not produce any results

thank you for the hard work. Can you please paste your scan code here? Could not figure that out and my scan does not produce any results
shared scan link: http://tos.mx/ZERVkCa Click here for --> Easiest way to load shared links

Ruby:
``````def EPS = if !isNaN(GetActualEarnings()) then GetActualEarnings() else EPS[1];
def EPSGrowthQoQ;
if EPS[65] == 0 then {EPSGrowthQoQ = 0;} else {EPSGrowthQoQ = 100*(EPS - EPS[50])/AbsValue(EPS[50]);}

def GQ = EPSGrowthQoQ ;

plot scan = GQ > 30 ;``````

### Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
442 Online

## The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
• Exclusive indicators
• Proven strategies & setups
• Private Discord community
• ‘Buy The Dip’ signal alerts
• Exclusive members-only content
• Add-ons and resources
• 1 full year of unlimited support

### Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.