Recursion not working in Study Alerts


0
0

Hello,

Recently I have constructed a custom study using the thinkscript editor. While it has been successfully used on charts, it seems that it does not work with the ThinkorSwim stock/option scanners and the study alerts. After researching my problem, it appears that my study uses recursion. Being a novice coder, I am uncertain what needs to be done with the code. Any help would gratefully appreciated.

Thanks Again

Attachments:
RESOLVED
Marked as spam
Posted by (Questions: 4, Answers: 5)
Asked on September 7, 2018 8:41 am
363 views
0

You posted this question under the “Chart Studies” topic but your request applies to Study Alerts and Scans. I am moving this post into the “Alerts and Notifications” topic.

Your question title is not descriptive enough. Other visitors experiencing this issue will have difficulty finding it in the search results. So i will update the question title, but this will break the current URL (link) to this post.

The new question title will be: “Recursion not working in Study Alerts”

( at September 7, 2018 10:02 am)
0

Ok, thanks for the update.

( at September 7, 2018 12:56 pm)
0
Private answer

So we’ll address a few things here. One is I will explain what is recursion. Second I will explain where it is not permitted in the Thinkorswim platform. Lastly, I will point out the exact lines of code in which recursion is being used.

What is Recursion

This applies to variables within your code. A variable is said to be recursive, when it’s present value is computed from a previous value of itself.

Where Recursion is Prohibited

Recursive variable statements are not permitted in the Study Alerts nor in the Conditional Order. These are the only two places they are not permited. If you code fails to work within a Study Filter of a custom scan, recursion is not the issue. Something else is preventing it from working as a scan.

Which Lines in your Code Use Recursion

def fish = 0.5 * (log((1 + truncValue) / (1 - truncValue)) + fish[1]);

def haOpen2 = CompoundValue(1, ( (haOpen2[1] + (openMA[1] + highMA[1] + lowMA[1] + closeMA[1]) / 4.0) / 2.0), open);;

def haOpen = CompoundValue(1, (haOpen[1] + ohlc4[1]) / 2, ohlc4);

def upwSave = if upw or dnw then upw else upwSave[1];

def neutral = buy or (if longTermSell then no else neutral[1]);

Best Practices

It is customary, but not required, then when you define a variable that uses recursion you use rec in place of def. This makes it easier for others to identify recursive variables in your code. It also forces the code writer to be mindful of where recursion is being used.

I know the next question. How do I re-write the code to remove the recursion statements but retain it’s current behavior? Most of the time it is not possible. But sometimes there are tricks one can apply to address them. It is not within the scope of this user forum to diagnose something of this advanced nature.

Marked as spam
Posted by (Questions: 37, Answers: 4087)
Answered on September 7, 2018 3:13 pm
0

Thank you Mr. Hahn for the quick response. Although recursion still lingers in the code, you have brought to my attention which lines have been flagged, and where I need to go from here. Thanks again for the insight.

( at September 7, 2018 3:41 pm)