Recall from part one that the core engine driving the ThinkScripter AutoFibLines script is TOS’ built in ZigZagSign() function. I chose to harness the power of this function rather than re-invent the wheel to capture the swing highs and lows needed to draw Fibonacci lines. With this piece of the code complete, the script enables any bar to determine if it is a swing high or swing low and set an appropriate flag if so.
At this point I should explain an important nuance here. If you have ever watched the TOS ZigZag in action you will have noticed that it often “Changes its mind” about whether to draw an inflection point or not. This is just the nature of the way the ZigZag works on any platform. So, as a result, a bar may get its swing high/low flag set only to have the ZigZag change a bar or two later. We don’t want false high/lows flagged so we force our entire chart to redraw each bar with the declare fullrange statement up front. Therefore, if a flag is set and then nullified a bar later, the redraw of the chart will remove the false flag.
Onward….In order to draw the Fibonacci levels at the final bar, that bar needs to know the previously recorded values of the swing high and swing low. Enter the rec statement. Many folks get the def and rec statements confused. In this case, we need to carry along a value in a variable using rec (short for recursion). Any variable definition that refers to a previous value of itself requires the use of this command. To properly seed the rec variable, you need to use the compoundValue() statement. The compoundValue() statement sets the value of the variable to one value if less than a certain number of bars have passed and to another value when they have. We’ll dive deeper into this one in another segment. For now, just push the “I believe” button. Here’s the resulting code to hold the swing high/low values and carry them forward from bar to bar:
rec prevLow = compoundValue(1, if(isLow[1], min(lowest(low,3),low[-1]), prevLow[1]));
rec prevHigh = compoundValue(1, if(isHigh[1], max(highest(high,3),high[-1]), prevHigh[1]));
Since the ZigZag function does not always grab the absolute high/low, I’ve added a little code to search the identified high/low bar for larger extreme values on adjacent bars.
Again, in plain english: set the value of prevLow to be the lowest low from this bar or the previous/following bars if and only if this bar has been identified as a swing low and if it is not then carry the value of the swing low from the previous bar forward. Similar for the prevHigh variable.
Tune in next time and we’ll dive deeper into the methodology for drawing in the blank space to the right of the last bar. Again, the AutoFibLines code is available in the forum if you’d like it.




Eric,
I like how you think.
There’s a lot going on under the hood with this study. I have been analyzing this code to some degree since the date you originally released it.
To understand the inner workings of this code and the study as a whole, one must have a deeper understanding of the code’s components. Now having a much clearer understanding of how this indicator was blueprinted and assembled, it reminds me much of Kinesiology, in its context.
I have seen code written and have myself jockeyed the REC statement in place of the DEF statement, for lack of a better understanding of the usage. Your example and brief explanations of recursion in this article sheds some light on proper usage for REC statements.
I’m looking forward to the upcoming installments.
Thanx Eric,
I really dig the education …
Keep up the great work.
Peace-