♥ 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
|
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 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 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 as 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()”:
Next, we simply check the most recent 7 bars to see if the value of that variable has been true at least one time:
In your example, the value you are using for looking back in time is fixed at
Now, I see in your description the reason you tried using the fold statement was that:
But since you did not provide an example in which “n” was dynamically set, we really can’t address that here.
Marked as spam
|
Please log in to post questions.