desktop:desktop_chart_scripting_overview

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

desktop:desktop_chart_scripting_overview [2022/01/11 13:22] robdesktop:desktop_chart_scripting_overview [2023/09/21 19:58] (current) – external edit 127.0.0.1
Line 556: Line 556:
  
 The script interpreter cannot compute xval until it computes yval. It also cannot compute yval until it computes xval. A naive interpreter might get stuck in an endless loop trying to solve these equations, however the script interpreter has a mechanism to detect these situations and report them as errors. The script interpreter cannot compute xval until it computes yval. It also cannot compute yval until it computes xval. A naive interpreter might get stuck in an endless loop trying to solve these equations, however the script interpreter has a mechanism to detect these situations and report them as errors.
 +
 +====== Recursion ======
 +
 +There is a special case where you can use circular-like syntax called recursion. Here is an example:
 +
 +----
 +
 +<code lang | Example>
 +xval = IF(INDEX = 0, 0, yval[-1]);
 +yval = xval + 1;
 +</code>
 +
 +or
 +
 +<code lang | Example2>
 +yval = IF(INDEX = 0, 0, yval[-1] + 1);
 +</code>
 +
 +----
 +
 +In this case, because you using the offset mechanism to refer to a previously calculated value, no circular reference error is generated. However, you must be sure to check the stop condition and assign it a value. In this case our stop condition is the first bar. On the first bar, the previous value of yval would evaulate to NIL and that would short-circuit all following computations to NIL. We used the IF statement to initialize yval on the first bar.
 +
 +Recursion is considered an extremely advanced topic. It is rarely needed for most indicator calculations. However there are some calculations which are impossible without it (Parabolic SAR is one example). If find find yourself using recursion to solve a computation problem, please feel free to contact support for assistance, further documentation and possibly more examples.
  
  • desktop/desktop_chart_scripting_overview.1641907367.txt.gz
  • Last modified: 2023/09/21 19:55
  • (external edit)