Find stocks with earnings release during the past week


Category:
0
0

 

Hi Pete:

I am trying to write code to find all stocks which released earnings [hasEarnings()] over the past n days.  I could not do this because hasEarnings()[n] is not allowed unless n is a constant.

I actually found that [n] variable does not give a syntax error when the n is passed through a fold statement, but I am not getting the result for each day of n in the following code:

def n=7; def ReleasedEarnings = fold idx = 0 to n do hasEarnings()[idx]; plot ans=ReleasedEarnings ;

This gives me the results for hasEarnings()[6] only.

Can you please help with a solution? Also can you please let me know why the above fold statement does not plot results for 0 to 5?

Thank you

 

Arun

Marked as spam
Posted by (Questions: 11, Answers: 16)
Asked on July 5, 2018 11:58 am
176 views
0
Private answer

Style notes:

Properly written code will capitalize the HasEarnings() method, exactly as it is done in the Thinkorswim language reference: http://toslc.thinkorswim.com/center/reference/thinkScript/Functions/Corporate-Actions/HasEarnings.html

Also note that user defined variables ALWAYS start with lower case. So ReleasedEarnings should be releasedEarnings. That’s how you can easily distinguish user defined variables from system methods, yes?

Ok, first piece of advice. Don’t try using the fold statement. It is a nightmare. Thinkorswim developers could not have done a better job of creating the most confusing tool for looping ever imagined. I hate it, personally. Most times that I have tried to use it, I give up and find an easier way. And to put that into perspective, I write enterprise class Java applications. So I know a few things about working with loops.

Having said that, when you do use the fold statement, you need to make use of the “GetValue()” method in order to get index values to work. You can’t even imagine how much more complexity this adds. But if you are a glutton for punishment and want to give yourself a heart attack….

In place of hasEarnings()[idx] you would use GetValue(HasEarnings(), idx)

But you have been warned. Regardless, the fold statement is only going to return the value of the last iteration of the loop. So it can never give you hasEarnings[1-6]. But you can add a while statement that terminates the loop as soon asGetValue(hasEarnings(), idx) equals true. More headaches. I did warn you, yes?

Ok so this is how I would get the job done. What we need is a variable to hold each bar’s value for “HasEarnings()”:

def earningsBar = HasEarnings();

Next, we simply check the most recent 7 bars to see if the value of that variable has been true at least one time:

def pastEarnings = Highest(earningsBar, 7) > 0;

In your example, the value you are using for looking back in time is fixed at n = 7. So there is no conflict when using that in a place that requires a constant value:

def pastEarnings = Highest(earningsBar, n) > 0;

Now, I see in your description the reason you tried using the fold statement was that:

I could not do this because hasEarnings()[n] is not allowed unless n is a constant.

But since you did not provide an example in which “n” was dynamically set, we really can’t address that here.

 

Marked as spam
Posted by (Questions: 37, Answers: 4118)
Answered on July 5, 2018 12:43 pm
0
Now I saw that there is a built in scan already for future earnings dates. Wish I saw that first and modified it to look for the past. Thank you, Pete. Arun
( at July 5, 2018 5:12 pm)