ThinkScripter AutoFibLines Code Explained – Part 1

UPDATE : This script has been rendered inoperable as of 12 Dec 09 due to changes in the TOS ZigZag function. I will leave it posted as a reference for coders to examine the script methodology.

I’ve recently had quite a few email exchanges with forum members about the AutoFibLines code and what is going on behind the curtains. The easiest way to explain the machinery is to use the code itself so I am releasing it in the public area of the ThinkScripter Forum today. If you haven’t had a chance to stop by the forum please head on over, register, and grab a copy of the script while you are there (Go to “All Things ThinkScript”->”Released ThinkScripterStudies”).

The code itself is deceptively simple. Rather than creating some intricate mechanism for determining swing highs and lows, I chose to harness TOS’ built-in ZigZagSign() study to do the heavy lifting. In case you are unfamiliar with this study, add it to one of your charts, set a reasonable reversal amount, and watch what happens. The ZigZag will convert price action into a series of linear segments, hence the name. A reversal will only be indicated when your threshold price is achieved from the previous high or low or thereabouts as the ZigZag doesn’t always grab the perfect swing high or low. Also note that the ZigZag is not always finalized until several bars later and can flip/flop in the process.

I use the following lines to grab the ZigZag data:

def ZZData = ZigZagSign("reversal amount" = Required_Reversal);
def ZZSign = ZigZagTrendSign("reversal amount" = Required_Reversal);

where the ZigZagTrendSign() function tells us if it is a Zig or a Zag :)

OK, that’s swell, how does that feed into automated Fibonacci retracements? If you look at a ZigZag high or low you will see a convenient mathematical description which can be formulated to isolate the reversals for our script. Namely, for a high, the ZigZag value will be greater than the high on the prior bar and also greater than the high on the following bar. So for a high we have something like the following:

def isHigh = if (barclosed and ZZConfirm and ZZData > ZZData[-1] and ZZData > ZZData[1] and ZZsign == 1, 1, 0);

Ignore the barclosed and ZZConfirm arguments for now and focus on the ZZData arguments. In plain english, if the value of the ZigZag function on the previous bar ([1] indexing) is less than the value on the current bar AND the value on the following bar ([-1] indexing) is also less than the current bar AND the ZigZagTrendSign is equal to 1…..we have a swing high. Tada! Not so hard right. You can see a similar statement can be used to grab the swing lows as well.

So now we have the swing high and low of the current ZigZag cycle. With this data we can find the difference and plot Fibonacci levels to our heart’s content in any ratio we desire. We’ll get into the meat and potatoes of that in the next installment.

Until then, happy thinkScripting and good luck trading to all.

This entry was posted in Tutorial and tagged , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>