Stock Portfolio Organizer

The ultimate porfolio management solution.

Shares, Margin, CFD's, Futures and Forex
EOD and Realtime
Dividends and Trust Distributions
And Much More ....
For Portfolio Manager Click Here

WiseTrader Toolbox

#1 Selling Amibroker Plugin featuring:

Advanced Adaptive Indicators
Advanced Pattern Exploration
Neural Networks
And Much More ....
Find Out More Here

Ultimate Divergence Detector for Amibroker (AFL)
bkonia
over 10 years ago
Amibroker (AFL)

Rating:
3 / 5 (Votes 18)
Tags:
oscillator, rsi, divergence, amibroker

Divergence is one of the most reliable trading signals, but it’s tricky to identify divergences in real time and if you’re not paying attention, it’s easy to miss a divergence. There are a bunch of divergence detectors posted here, so why another one? Well, I tried all the ones I found and none of them really met my needs, so I decided to create my own. Here’s what makes mine different:

1. Most divergence detectors look for peaks and troughs in price and then try to find a nearby peak or trough in RSI. I took the opposite approach. UDD looks for higher RSI troughs and lower RSI peaks. It then looks for divergences between price and RSI at the current RSI peak/trough, compared with the previous one. I feel this is a more reliable method of identifying divergences, because RSI is less noisy than price. If you want to use a different indicator from RSI, it just involves changing one line of code.

2. UDD plots the RSI and overlays the Zig-Zag indicator on top of it. It then plots a green dot identifying each bullish divergence and a red dot identifying each bearish divergence. This makes it easy for the user to see the divergence. Just follow the Zig-Zag line back to the prior zig/zag point on the graph. The prior point is where the divergence began and the red or green dot is the trade entry point. Peaks or troughs that have no divergence are shown as grey dots.

3. UDD allows the user to specify the RSI length and the percentage change threshold. As you adjust these values, the graph dynamically updates, so you can see the effect of the change in real time. The default RSI length of 14 and percentage threshold of 10% may be too noisy for many markets. If you see too many false signals on the graph, just increase these values until you’re comfortable with how the graph looks.

4. UDD uses pure array processing, no loops, so it’s extremely fast and the code is compact and easy to understand.

5. UDD includes a simple trading system with backtesting. The system buys on each bullish divergence and goes short on each bearish divergence. It exits the trade at the next point on the zig-zag line. For instance, if it goes short on a bearish divergence, it will exit the trade at the next trough. The actual entries and exits occur one bar after the peak or trough is identified. If you backtest this against a variety of markets, you may be surprised at how profitable it is.

Screenshots

Similar Indicators / Formulas

RSI of volume weighted moving average
Submitted by kaiji almost 15 years ago
MACD Divergence Detector
Submitted by prasadbrao over 12 years ago
BSonSto
Submitted by projsx over 12 years ago
KPShortTermTrend Bias
Submitted by knifeman almost 14 years ago
Score Card
Submitted by Nahid almost 14 years ago
MA() vs Close (BillCapital 2014).afl
Submitted by billcapital over 10 years ago

Indicator / Formula

Copy & Paste Friendly
// ********************************************
// INDICATOR	: Ultimate Divergence Detector
// DATE			: 07-04-2014
// PROGRAMMER	: Brad Konia
// COPYRIGHTS	: Public Domain
// ********************************************

indName = "RSI"; // Specify the name of the indicator. This is cosmetic only.
length = Param(indName + " Length", 14, 8, 100, 1); // Indicator length
threshold = Param("Zig/Zag Threshold %", 10, 1, 50, 1); // Minimum % change in indicator value to be considered a zig/zag
indVal = RSI(length); // Load the indVal array with the values from the indicator of your choice
indZig = Zig(indVal, threshold); // Set the zig/zag values for the indicator
indPeakVal1 = Peak(indVal, threshold, 1); // Most recent peak value
indPeakBars1 = PeakBars(indVal, threshold, 1); // Bars since most recent peak
indPeakVal2 = Peak(indVal, threshold, 2); // Second most recent peak value
indPeakBars2 = PeakBars(indVal, threshold, 2); // Bars since second most recent peak
indTroughVal1 = Trough(indVal, threshold, 1); // Most recent trough value
indTroughBars1 = TroughBars(indVal, threshold, 1); // Bars since most recent trough
indTroughVal2 = Trough(indVal, threshold, 2); // Second most recent trough value
indTroughBars2 = TroughBars(indVal, threshold, 2); // Bars since second most recent trough

// Determine if current bar is a peak or trough
peakBar = indPeakBars1 == 0;
troughBar = indTroughBars1 == 0;

// Bearish divergence
divergeBear = IIf(peakBar AND (indPeakVal1 < indPeakVal2) AND  High > Ref(High, -indPeakBars2), True, False);
Short = Ref(divergeBear, -1); // Enter short if the previous bar was a bearish divergence
Cover = Ref(troughBar, -1); // Cover short if the previous bar was a trough

// Bullish divergence
divergeBull = IIf((indTroughBars1 == 0) AND (indTroughVal1 > indTroughVal2) AND Low < Ref(Low, -indTroughBars2), True, False);
Buy = Ref(divergeBull, -1); // Enter long if the previous bar was a bullish divergence
Sell = Ref(peakBar, -1); // Close long if the previous bar was a peak

Plot(indVal, indName, colorGrey40);
Plot(indZig, "", colorWhite);
PlotShapes(IIf(peakBar OR troughBar, shapeCircle, shapeNone), colorGrey40, 0, indVal, 0);
PlotShapes(IIf(divergeBear, shapeCircle, shapeNone), colorRed, 0, indVal, 0);
PlotShapes(IIf(divergeBull, shapeCircle, shapeNone),  colorBrightGreen, 0, indVal, 0);

19 comments

1. kv_maligi

Hi Brad Konia,

Really Ultimate. Thanks for sharing. My rating 5/5

Please describe when to book profits & SL also
Is it possible to develop explorer/scan for buy Sell?

2. kv_maligi

Hi Brad Konia,

I am back testing on daily Time frame. Results are good but lots of whisphaws because of threshold=10.

As per you experience, for daily time frame charts, what should be optimal value for threshold???

Thanks once again
Viswanath

3. r.siva79

looks into future quates

4. Prathitkumarr

Hi Brad,
Excellent indicator. Just want to clear one thing. Suppose i see a greendot(Bullish divergence) at 10:00am in 15 min timeframe chart. At what time was that green dot actually formed i.e. at close of 10:15am, open of 10:30am or close of 10:30am candle. Please clarify.

5. rajenndraa

Seems quite useful; but I cannot see the Zig-Zag line. Please advise.

6. farhad1348

THATS VERY USEFUL.THNKS A LOT……

7. snehil2010

Doesn’t it look into future?

8. woraweew

Exceptional and Unbelievable Results. Thanks for sharing

9. dranokar

It looks in to the future

10. K Gadkari

I have just downloaded the indicator …But the look of it gives real confidence . Thanks a lot for sharing such a good indicator.
How do I use it for MACD if At all I can ?

11. K Gadkari

Can anybody help change the above code for getting divergence on CLOSING of bar instead of PEAK and TROUGH . As RSI is calculated on basis of CLOSE of the bar . So divergence also has to be observed for CLOSE of the bar.
Thanjs and Regards .
All in all good indicator 5/5

12. thangnd.1211

very good indicator for trading, thank you !

13. valueinvest32

Dear Admin,

Can this system be used for real time trading ? Somebody said this looks into the future, what does this mean? Please reply.

Thanks,
Vishal

14. administrator

Future looking means it uses future quotes and results can’t be replicated in real-time. The best way to check is to use bar replay and try paper trading.

15. SanjeevDad

its awesome i change a little and it is real so nice

16. creative

Can you share changes made?

Comment hidden - Show
18. Aniket

It’s skipping big moves, and can cause big losses. Found this thing in ‘forward testing’ (bar replay). I don’t know AFL coding so unable to edit it.

19. ASHISH SHETTY

The system is forward looking…..The exit is plotted after 3-4 bars of it actually happening…..This cannot be replicated in real time ….. Very good for testing purpose but not practical

Leave Comment

Please login here to leave a comment.

Back