# Program 65 | Zig zag function with Fibonacci ratio detection

## Zig zag function with Fibonacci detection

Program 65 consists of a new zig zag function and a companion show me study that demonstrates how to call the function. Both programs are included in the download.

When the function is called it calculates and draws zig zag lines on the chart together with text at the zig zag pivots. The text shows the vertical height of each zig zag leg. If the vertical ratio of two most recent zig zag lines is close to a Fibonacci ratio, text is also displayed to show which Fibonacci ratio it is close to and the zig zag line is drawn with a heavier weight.

Both Fibonacci retracements and extensions (projections) are looked for. The following ratios are compared in the calculation:

• 38.2
• 61.8
• 100.0
• 138.2
• 161.8

The function also returns 4 vectors:

• The UpMoves vector contains the last MaxVectorCount up moves. The most recent can be accessed using UpMoves.items[0] astype double the second most recent can be accessed using UpMoves.items[1] astype double etc

• The DnMoves vector contains the last MaxVectorCount down moves. The most recent can be accessed using DnMoves.items[0] astype double
• The HiZZ vector contains the bar numbers of the last MaxVectorCount high zig zag pivots. The most recent can be accessed using HiZZ.items[0] astype int, the second most recent can be accessed using HiZZ.items[1] astype int etc
• The LoZZ vector contains the bar numbers of the last MaxVectorCount low zig zag pivots. The most recent can be accessed using LoZZ.items[0] astype int

Before attempting to access an element of a vector it is important to check that the element exists by taking a count of the vector. This is achieved by using LoZZ.Count.

The function does not contain a mechanism for regulating the number of text objects on the chart.

### Calling the function

The function is called as follows:

Value1 = _Program65( RetracePerc, Tolerance, ZigZagColUp, ZigZagColDn, ColorOfText, LineThickness, UpMoves, DnMoves, HiZZ, LoZZ, MaxVectorCount, Font, FontSize );

With the following inputs and color options:

### Inputs

RetracePerc( 0.2 ), // Percentage above running pivot that pivot must be formed to start a new zig zag
Tolerance( 10 ), // Percentage tolerance calculating a range above and below last change
ZigZagColUp( “AliceBlue” ), // Color of up zig zag line (see the program web page for available colors)
ZigZagColDn( “Orange” ), // Color of up zig zag line
ColorOfText( “Yellow” ), // Color of number label
LineThickness( 3 ), // Thickness of line if new zig zag if close to a Fibonacci ratio (valid options are 0, 1, 2, 3, 4, 5, and 6)
MaxVectorCount( 100 ), // The maximum number of elements in the UpMoves, DnMoves, HiZZ, and LoZZ vectors
Font( “Ariel” ), // Font for text objects
FontSize( 16 ); // Font size for text objects

#### Color options

 AliceBlue DarkSlateGray LightSalmon PaleVioletRed AntiqueWhite DarkTurquoise LightSeaGreen PapayaWhip Aqua DarkViolet LightSkyBlue PeachPuff Aquamarine DeepPink LightSlateGray Peru Azure DeepSkyBlue LightSteelBlue Pink Beige DimGray LightYellow Plum Bisque DodgerBlue Lime PowderBlue Black Firebrick LimeGreen Purple BlanchedAlmond FloralWhite Linen Red Blue ForestGreen Magenta RosyBrown BlueViolet Fuschia Maroon RoyalBlue Brown Gainsboro MediumAquamarine SaddleBrown Burlywood GhostWhite MediumBlue Salmon CadetBlue Gold MediumOrchid SandyBrown Chartreuse Goldenrod MediumPurple SeaGreen Chocolate Gray MediumSeaGreen Seashell Coral Green MediumSlateBlue Sienna CornflowerBlue GreenYellow MediumSpringGreen Silver Cornsilk Honeydew MediumTurquoise SkyBlue Cyan HotPink MediumVioletRed SlateBlue DarkBlue IndianRed MidnightBlue SlateGray DarkBrown Indigo MintCream Snow DarkCyan Ivory MistyRose SpringGreen DarkGoldenrod Khaki Moccasin SteelBlue DarkGray Lavender NavajoWhite Tan DarkGreen LavenderBlush Navy Teal DarkKhaki LawnGreen OldLace Thistle Dark Magenta LemonChiffon Olive Tomato DarkOliveGreen LightBlue OliveDrab Turquoise DarkOrange LightCoral Orange Violet DarkOrchid LightCyan OrangeRed Wheat DarkRed LightGoldenrodYellow Orchid White DarkSalmon LightGray PaleGoldenrod WhiteSmoke DarkSeaGreen LightGreen PaleGreen Yellow DarkSlateBlue LightPink PaleTurquoise YellowGreen

## Technical lessons

### Legacy drawing objects

I created an earlier version of the program using both legacy drawing objects AND the more recently introduced drawing objects. I am including this ‘legacy’ program with the download (with the associated calling program) as you may be interested to see how to write code for either of these two technologies or to replace one technology with another.

#### Drawing legacy trendlines

Legacy trend lines are drawn using the following syntax:

Value1 = TL_New( StartDate, StartTime, StartPrice, EndDate, EndTime, EndPrice );

Dates use YYMMDD or YYYMMDD format (three digits are used to express the year 2000 and later), and times use 24-hour format, HHMM.

The problem with it is that on certain charts several bars might have the same HHMM stamp which can lead to unexpected results on tick and seconds charts.

In the following image the legacy drawing tools (the thinner lines) have difficulty positioning a drawing object accurately when more than one bar shares the same minute based time signature. The new drawing tools position the line accurately.

### New drawing objects

To draw lines with the new drawing objects we create a pair of BNPoint objects using a bar number and price. The trend line is created and styled and then added to the chart.

##### Gotcha 1

A BNPoint refers to the absolute bar index (zero-based) of the collection of bars in a chart. This is not the same as the BarNumber function or CurrentBar reserved word which count the number of bars available for analysis after MaxBarsBack. So to create a BNPoint for the current bar (BarNumber) would be:

NewBar = BarNumber + MaxBarsBack – 1;

##### Gotcha 2

Another issue that can cause what appear to be spurious results are the ‘Show empty daily trading periods for traditional chart types’ and ‘Show empty intraday session periods for traditional chart types’ settings. These can be found by clicking View – Chart Analysis Preferences

If these are selected, the chart includes blank bars which ‘confuse’ the bar number for BNPoints. For example, in the following daily chart of IBM, the ‘Show empty daily trading periods for traditional chart types’ is selected. The chart looks like the following (notice how the pivots and the lines do not line up correctly):

In the following chart all settings are identical apart from “Show empty intraday session trading periods for traditional chart types” is deselected.

Note that vectors do not store a history. In other words, you cannot look back 5 bars and see what the vector was storing then (like you could with an array).

Program 65 was developed for TradeStation 9.5 and TradeStation 10.0 and above and is not compatible with MultiCharts because of some of the drawing technology used.

An UNPROTECTED version (i.e. the TradeStation EasyLanguage is fully functional and you can see the source code) and associated unprotected function is available for for IMMEDIATE download for \$49- by clicking the ‘add to cart’ button. After download the program can be opened, you will be able to see the program code and modify it.

In addition I am including with a download a bonus show me study and function that include drawing object functionality in both the legacy and new drawing object types.

If you are a Gold Pass member make sure you enter the special coupon code to get 20% discount off these prices.

### Video demonstration of program 65 | introduction and using on a chart

Program 65 was developed for TradeStation 9.5 and above and is not compatible with MultiCharts.

For zig zag based programs see:

Program 29 (array version) is a TradeStation and MultiCharts compatible ShowMe study based on Program 29. The original program used TradeStation EasyLanguage vectors and so did not work with MultiCharts. This program replaces the vectors with arrays. It uses historic horizontal zig zag moves (measured in numbers of bars) to attempt to estimate future zig zag turning points.
Price: \$64.95
Program 29 is a TradeStation and compatible ShowMe study which uses the vector class. It uses historic horizontal zig zag moves (measured in numbers of bars) to attempt to estimate future zig zag turning points.
Price: \$64.95
Program 14 is a ShowMe study that calculates zig zag patterns and stores the times at which the turning points occur in an array. Each time a new turning point is confirmed the time at which it occurred is compared with those already in the array. If the time is already in the array its 'significance index' is incremented by one. A user input determines how many lines should be drawn and the program sorts the array and draws vertical lines on the times where the most number of turning points occurred previously.
Price: \$74.95
This program finds turning points in price action using the zig zag method. Once a new turning point is found, its price level is compared with price levels already stored in an array (if any). If the new level is within a user-specified tolerance of an existing line then the two lines are 'combined' by increasing the 'significance index' and updating the date and time associated with the level.
Price: \$69.95
This Fibonacci-Confluence show-me study works by drawing percentage based zig zag lines and each time a new zig zag line is confirmed, Fibonacci levels are calculated. An array is created which stores Fibonacci levels. If a new Fibonacci level is found to be within a certain tolerance of an existing Fibonacci level stored in the array, it is 'merged' with the existing line. This is done by increasing the thickness parameter of the line. Lines are extended to the right if their thickness parameter is greater than an input value. The maximum thickness of a line plotted is TradeStation line thickness 4.
Price: \$74.95

Please note that any screen shots on this site are examples and are not meant to imply that any of these programs will generate profitable trades. THESE INDICATORS, SHOW ME STUDIES, STRATEGIES AND OTHER PROGRAMS HAVE BEEN INCLUDED SOLELY FOR EDUCATIONAL PURPOSES.

TO THE BEST OF MARKPLEX CORPORATION’S KNOWLEDGE, ALL OF THE INFORMATION ON THIS PAGE IS CORRECT, AND IT IS PROVIDED IN THE HOPE THAT IT WILL BE USEFUL. HOWEVER, MARKPLEX CORPORATION ASSUMES NO LIABILITY FOR ANY DAMAGES, DIRECT OR OTHERWISE, RESULTING FROM THE USE OF THIS INFORMATION AND/OR PROGRAM(S) DESCRIBED, AND NO WARRANTY IS MADE REGARDING ITS ACCURACY OR COMPLETENESS. USE OF THIS INFORMATION AND/OR PROGRAMS DESCRIBED IS AT YOUR OWN RISK.

ANY EASYLANGUAGE OR POWERLANGUAGE TRADING STRATEGIES, SIGNALS, STUDIES, INDICATORS, SHOWME STUDIES, PAINTBAR STUDIES, PROBABILITYMAP STUDIES, ACTIVITYBAR STUDIES, FUNCTIONS (AND PARTS THEREOF) AND ASSOCIATED TECHNIQUES REFERRED TO, INCLUDED IN OR ATTACHED TO THIS TUTORIAL OR PROGRAM DESCRIPTION ARE EXAMPLES ONLY, AND HAVE BEEN INCLUDED SOLELY FOR EDUCATIONAL PURPOSES. MARKPLEX CORPORATION. DOES NOT RECOMMEND THAT YOU USE ANY SUCH TRADING STRATEGIES, SIGNALS, STUDIES, INDICATORS, SHOWME STUDIES, PAINTBAR STUDIES, PROBABILITYMAP STUDIES, ACTIVITYBAR STUDIES, FUNCTIONS (OR ANY PARTS THEREOF) OR TECHNIQUES. THE USE OF ANY SUCH TRADING STRATEGIES, SIGNALS, STUDIES, INDICATORS, SHOWME STUDIES, PAINTBAR STUDIES, PROBABILITYMAP STUDIES, ACTIVITYBAR STUDIES, FUNCTIONS AND TECHNIQUES DOES NOT GUARANTEE THAT YOU WILL MAKE PROFITS, INCREASE PROFITS, OR MINIMIZE LOSSES.