New! Stock Portfolio Organizer is now available:
  • Shares, Margin, Futures and Forex
  • EOD and Realtime data
  • And Much More...
GoTo Stock Portfolio Organizer


Contribute an indicator to the library by clicking here. Note you must be logged in.


Popular Indicators

HARMONIC PATTERN DETECTION using ATR type Peaks and Troughs for Amibroker (AFL)

4 / 5 (Votes 4)

/* HARMONIC PATTERN DETECTION using ATR type Peaks and Troughs

Automatic Detection of Harmonic Patterns – Gartley, Bat, Butterfly and Crab.
Original code by, see:
file: (2009)

Modernized by E.M.Pottasch (Dec 2016).
- Using ATR based pivots instead of fractal based
- Improved visualisation of the patterns.
- Nomenclature of variables, constants and arrays, adjusted
following code from David Keleher.
- uncompleted pivots given white color
- multiple issues of the original code corrected for

The original code was based on “fractal type pivots”, this
code uses “ATR type pivots”. This code also includes a time
frame factor. To fully understand how to interpret a pattern
use the Amibroker playback utility and see how the patterns
develop. */

Here is a screenshot of how the indicator looks:

Similar Formulas



Will check

made a minor change in the display of the patterns:

error in formula detect on ami 6

my gosh you guys, I am shocked this is all I get, “error in formula”. I guess this will be my last post. Quite frankly this is superb code. But this will be the last I post, fed up with these “error in formula” posts and people just not responding. Why do you think I post this? Just for the idiots. Good luck

Dear Mr. Pottasch,

Please stay!

“I”, and I’m sure “WE” all, value your studies and afl sharing. Albeit, this is not a discussion board, so often reports are curt with no further explanation. This is compounded by the fact that most, if not all Wisestocktrader members, have English as a secondary language, which in itself creates issues in formula coding. Some probably cannot write English and are too shy to express themselves in descriptive sentences.

Please DO remain here, and continue to post your valuable experience, insight and afl codes.

With appreciation and gratefully yours – ‘Parfumeur’ in PA, USA

6. money12

Thanks for the great formula! I hope you will stay too. There are definitely those of us who appreciate what you have posted.

you do whatever you want,but letting your emotions fall prey to they posts of error codes is demeening to all of us that know your ideas and programming skills are unequalled here or anywhere else. Do not let yourself fall prey to those that are jellous of your skill. Best of luck and good health in 2017

8. crave

Thank you, Mr. Pottasch for your brilliant formula.
That is how formulas should be coded.
What I noticed: Bearish Crab overwritten on Bearish Butterfly.
(We shall study the formula and improve it a little.)
Good luck !

ok guys, thanks for the replies, I lost my cool a bit here (never post a comment when drinking beer ….).

with respect to double names, crab overwritten on Butterfly. Yes that sometimes may occur. Try to do a playback. Then you will see that first the Butterfly will appear and later the Crab will appear. In some cases both patterns are valid. This is how it is setup. This is done not by me but by the original author with respect to the parameter constraints. These patterns are only distinguished by the retracement ratios.

This is one of the points I am still working on. The original author uses ranges like GBmin en GBmax. But I am not sure if this is correct and it is hard to figure out what one needs to use. Like in Scott Carneys book Vol1 he says that for a Gartley B retracement over leg XA should be a precise 61.8%. In the code I posted a range is used => 55% to 72%. Maybe this is good enough.

10. mashraf

thank you so much for a nice piece of code and for your time .

Crave, will post latest code soon

this is the latest code on this.

it includes entry zones using the parameter settings

sometimes you see 1 pattern and multiple zones. To understand this do Playback. I only draw the latest pattern originating from point C. But sometimes a pattern starts off as a gartley, moves to a Butterfly and then a Crab. All these zones are displayd but not all the patterns, since this would get messy.

Thank you Ed, This is amazing code! Mark

someone found small error, corrected for:

15. epicbud

Thank you for taking the time to code this. It is greatly appreciated.

I was wondering if the script like this one is as reliable as manual harmonic pattern detection, done by a decent human trader ?

so far I did not do any tests with these patterns. I just use the parameter constraints the original author uses. But it would be great to get feedback from people who actually use these type of patterns for trading. Or maybe someone just uses 1 type of pattern and has more information about the parameters to use.

I have just been eyeballing the patterns and the impression I get is that the Gartley and Bat (and to lesser extent the Butterfly) seem to be most reliable especially as a trend continuation pattern

on some bars there were more than 1 pattern, like a Crab and a Butterfly. This sometimes led to a mess when drawing the entry zones. I now removed any overlap

dear empottasch is it possible to add exploration code with this chart so that we can scan the stocks to detect potential harmonic patterns with PRZ?

Please login here to leave a comment.

Main Menu