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.
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”
Ok, thanks for the update.