Number of consecutive increasing or decreasing earnings


Category:
0
0

Hello,

How would I create a custom column that displays the number of consecutive increasing earnings or decreasing earnings? I was thinking the number of consecutive increasing earnings would be displayed as a positive number and the number of consecutive decreasing earnings would be displayed as a negative number.

I thought it was going to be easy, but I keep running into challenges.

Thanks!

-Charles

Marked as spam
Posted by (Questions: 5, Answers: 4)
Asked on March 6, 2020 3:43 pm
363 views
0
To be clear, when you say "negative or positive earnings". Are you referring to what happens to the price after earnings? Or are you referring to the actual earnings reported? Negative if the company reports a loss and positive if the company reports a profit. Or.... and bare with me, are you referring to the comparison of expected versus actual earnings? Such as if the company missed earnings it's a negative or if the company beats earnings it's a positive. Are there any more ways this could be interpreted? Not sure, but those are the first that came to mind. Oh, and we need to make sure everyone understands this will have to be done on the daily time frame and there is only 1 year of historical data available in a custom watchlist column. So you will only get four data points to work with here, at best.
( at March 6, 2020 3:48 pm)
0
I would like to count the number of consecutive increases or decreases of actual earnings. Below is what I have so far. It only looks back two. ############################ def ActualEarnings1 = GetValue(GetActualEarnings(), GetEventOffset(Events.EARNINGS, -1)); def ActualEarnings2 = GetValue(GetActualEarnings(), GetEventOffset(Events.EARNINGS, -2)); plot ConsecIncDec = if ActualEarnings1 > ActualEarnings2 then 1 else (if ActualEarnings1 < ActualEarnings2 then -1 else 0);
( at March 6, 2020 4:53 pm)
0
Private answer

#This custom column displays the number of consecutive increasing earnings or decreasing earnings looking back four earnings reports

#Feel free to make this code more elegent and look back further then four earnings reports

def ActualEarnings1 = GetValue(GetActualEarnings(), GetEventOffset(Events.EARNINGS, -1));

def ActualEarnings2 = GetValue(GetActualEarnings(), GetEventOffset(Events.EARNINGS, -2));

def ActualEarnings3 = GetValue(GetActualEarnings(), GetEventOffset(Events.EARNINGS, -3));

def ActualEarnings4 = GetValue(GetActualEarnings(), GetEventOffset(Events.EARNINGS, -4));

def ConsecIncDec = if ActualEarnings1 > ActualEarnings2 then (if ActualEarnings2 > ActualEarnings3 then (if ActualEarnings3 > ActualEarnings4 then 3 else 2) else 1) else (if ActualEarnings1 < ActualEarnings2 then (if ActualEarnings2 < ActualEarnings3 then (if  ActualEarnings3 < ActualEarnings4 then -3 else -2) else -1) else 0);

plot x = ConSecIncDec;

AssignBackgroundColor(if x is greater than 0 then Color.BLUE else (if x is less than 0 then Color.RED else Color.GRAY));

Marked as spam
Posted by (Questions: 5, Answers: 4)
Answered on March 7, 2020 2:53 pm
-1
Private answer

After receiving a bit more detail in the comments section above I can provide a solution to this request.

Setup

It is crucial everyone understand that this solution requires the custom column to be set to a daily time frame or higher. Furthermore, the time frame selected will directly impact the amount of historical data that can be included. For example a daily time frame will include only a signal year of earnings data while a weekly time frame will include 3 years of earnings data. Full details here:

https://tlc.thinkorswim.com/center/howToTos/thinkManual/MarketWatch/Quotes/customquotes

IMPORTANT NOTE: Despite this code working for the vast majority of stocks I tested there are some stocks that do not display accurate values. Case in point is the ticker symbol HES set to a weekly time frame. The code is 100% accurate when added to a custom chart study and plotted on the lower subgraph of the chart. However in the custom watchlist column the values are not computed accurately. There is no explanation for this. I suspect this is a bug in the platform that will need to be addressed by one of the developers at TD Ameritrade.

The Tools

The main tools we are going to use are a function named GetActualEarnings() and we are going to use recursion to count the number of instances. You can get all the details about that built-in function here:

https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Corporate-Actions/GetActualEarnings

Recursion is when we create a variable and assign its current value based on its previous values. We prefer that recursive variables are defined using the keyword "rec" instead of "def" so that we can easily identify where recursion is being applied.

The Specifications

Before we present the solution it's important to examine your specification and understand how to implement this. You requested a custom watchlist column that will display both the number of negative earnings as well as the number of positive earnings.

Well we know that each column in a watchlist can only display a single values. So I will be splitting this into two separate columns. It is possible to take these values and concatenate them into a text string and thereby display both values in the same column. However doing so will remove the ability to sort by that column so for most applications I don't believe that is the best solution.

Solution

So here is the code to display the number of positive earnings reports:

def positiveEarnings = if IsNaN(GetActualEarnings()) then 0 else if GetActualEarnings() > 0 then GetActualEarnings() else 0;
rec countPositiveEarnings = if positiveEarnings > 0 then countPositiveEarnings[1] + 1 else countPositiveEarnings[1];
plot data = countPositiveEarnings;

And here is the code to display the number of negative earnings reports:

def negativeEarnings = if IsNaN(GetActualEarnings()) then 0 else if GetActualEarnings() < 0 then GetActualEarnings() else 0;
rec countNegativeEarnings = if negativeEarnings < 0 then countNegativeEarnings[1] + 1 else countNegativeEarnings[1];
plot data = countNegativeEarnings;

Marked as spam
Posted by (Questions: 37, Answers: 4087)
Answered on March 7, 2020 8:49 am
0
Thank you for taking the time write the code. Unfortunately, this is not what I am trying to accomplish. I did not do a good job describing what I am trying to accomplish. I am trying to see the number of consecutive increasing earnings (growing earning) or decreasing earnings (shrinking earnings). My code below looks at the last two actual earnings and lets me know if earnings are growing (displays a 1) or shrinking (displays a -1). I am trying to see how many consecutive increases or decreases there have been. For example if the symbol had 3 consecutive earnings increases it would display a 3. If a company had 2 consecutive earnings decreases it would display a -2. All that being said, it looks like I will need to incorporate recursion in my code. ################################################## def ActualEarnings1 = GetValue(GetActualEarnings(), GetEventOffset(Events.EARNINGS, -1)); def ActualEarnings2 = GetValue(GetActualEarnings(), GetEventOffset(Events.EARNINGS, -2)); plot ConsecIncDec = if ActualEarnings1 > ActualEarnings2 then 1 else (if ActualEarnings1 < ActualEarnings2 then -1 else 0);
( at March 7, 2020 9:12 am)
0
Below is the code I came up with that looks at the past four earnings reports. It is very clunky. ########################################################################## def ActualEarnings1 = GetValue(GetActualEarnings(), GetEventOffset(Events.EARNINGS, -1)); def ActualEarnings2 = GetValue(GetActualEarnings(), GetEventOffset(Events.EARNINGS, -2)); def ActualEarnings3 = GetValue(GetActualEarnings(), GetEventOffset(Events.EARNINGS, -3)); def ActualEarnings4 = GetValue(GetActualEarnings(), GetEventOffset(Events.EARNINGS, -4)); plot ConsecIncDec = if ActualEarnings1 > ActualEarnings2 then (if ActualEarnings2 > ActualEarnings3 then (if ActualEarnings3 > ActualEarnings4 then 3 else 2) else 1) else (if ActualEarnings1 < ActualEarnings2 then (if ActualEarnings2 < ActualEarnings3 then (if ActualEarnings3 < ActualEarnings4 then -3 else -2) else -1) else 0);
( at March 7, 2020 9:54 am)
0
I can't figure out how to use recursion in my code above so that I can look back further without the code being so clunky. By the way, I would update my original question or comments, but I just get a blank screen when I try to edit my original question or comments. I have tried different browsers.
( at March 7, 2020 10:53 am)
0
Yes, recursion is a very advanced coding technique. I have already maxed out the time I permit for free solutions on this post. You are correct in that it is not possible to edit the details for existing questions.
( at March 7, 2020 2:05 pm)
0
Thank you for trying! I'll continue to use my code until I get around to making it more elegant.
( at March 7, 2020 2:48 pm)