WhiteboxSoftware

The easiest way to use divergences in your own Pine strategies

Éducation
BINANCE:BTCUSDT   Bitcoin / TetherUS
Detecting divergences in a Pine indicator / strategy is easy.

You simply have to compare the pivot lows and the pivot highs on the price and the oscillator, and if you can identify a difference between the last & previous pivots made on the price and the oscillator, you have likely found a divergence.

Using this theory, here is an example how you would detect a Regular Bearish divergence:


While the theory of divergence detection is simple, more often than not, things go wrong (the divergence indicator used in the example below is TradingView's built-in Divergence Indicator):


Would you identify this as a divergence? If not, why not? Is it because the divergence line is slicing through the candles? Or because the line is slicing through the oscillator? Or something else?

Wouldn't it be great if somehow you could filter out invalid divergences from code, such as this one?

We at Whitebox Software were wondering about the same thing, and decided to find a solution to this problem. This is when we realised that while detecting divergences is easy, detecting valid divergences is hard...

After several months in development, we are proud to present to you our divergence indicator called The Divergent.

The Divergent is an advanced divergence indicator with over 2500 lines of Pine Script, exposing over 30 different configuration options, including 9 built-in oscillators, to allow you to tweak every aspect of divergence detection to perfection.

For example, the Line of Sight™ filter in The Divergent would have easily filtered out this invalid divergence above. The Line of Sight™ filter will notice any interruption to the divergence line connecting the price or the oscillator, and will treat the divergence as invalid.


This filter is one of many, which has been created to reduce the false positive detections to a minimum. (In later publications, we will discuss each and every filter in detail).

Alright, so The Divergent knows how to detect accurate divergences, but how is it going to help you detect divergences in your own Pine strategy?

The Divergent is not simply a divergence indicator - it can also emit divergence signals* which you can catch and process in your own strategy. You can think of The Divergent being a DaaS (Divergences as a Service)!

*Please note, that divergence signals is a Pro only feature.

To use the signals, simply place The Divergent onto the same chart you have your strategy on, import "The Divergent Library" into your code, link your strategy to The Divergent using a "source" input, and act on the signals produced by The Divergent!

Here is a simple strategy which incorporates divergence signals produced by The Divergent in its entry condition. The strategy will only open a position, if the moving average cross is preceded by a regular bullish or bearish divergence (depending on the direction of the cross):

//@version=5
strategy("My Strategy with divergences", overlay=true, margin_long=100, margin_short=100)

import WhiteboxSoftware/TheDivergentLibrary/1 as tdl

float divSignal = input.source(title = "The Divergent Link", defval = close)

var bool[] tdlContext = tdl.init(divSignal, displayLinkStatus = true, debug = false)

// `divergence` can be one of the following values:
// na → No divergence was detected
// 1 → Regular Bull
// 2 → Regular Bull early
// 3 → Hidden Bull
// 4 → Hidden Bull early
// 5 → Regular Bear
// 6 → Regular Bear early
// 7 → Hidden Bear
// 8 → Hidden Bear early
//
// priceStart is the bar_index of the starting point of the divergence line drawn on price
// priceEnd is the bar_index of the ending point of the divergence line drawn on price
//
// oscStart is the bar_index of the starting point of the divergence line drawn on oscillator
// oscEnd is the bar_index of the ending point of the divergence line drawn on oscillator
[divergence, priceStart, priceEnd, oscStart, oscEnd] = tdl.processSignal(divSignal)

bool regularBullSignalledRecently = ta.barssince(divergence == 1) < 10
bool regularBearSignalledRecently = ta.barssince(divergence == 5) < 10

float slowSma = ta_sma(close, 28)
float fastSma = ta_sma(close, 14)

longCondition = ta.crossover(fastSma, slowSma) and regularBullSignalledRecently
if (barstate.isconfirmed and longCondition and strategy.position_size == 0)
  strategy.entry("Enter Long", strategy.long)
  strategy.exit("Exit Long", "Enter Long", limit = close * 1.04, stop = close * 0.98)
 
shortCondition = ta.crossunder(fastSma, slowSma) and regularBearSignalledRecently
if (barstate.isconfirmed and shortCondition and strategy.position_size == 0)
  strategy.entry("Enter Short", strategy.short)
  strategy.exit("Exit Short", "Enter Short", limit = close * 0.96, stop = close * 1.02)

plot(slowSma, color = color.white)
plot(fastSma, color = color.orange)

One important thing to note, is that TradingView limits the number of "source" inputs you can use in an indicator / strategy to 1, so the source input linking your strategy and The Divergent is the only source input you can have in your strategy. There is a work around this limitation though. Simply convert the other source inputs to have a string type, and use a dropdown to provide the various sources:

string mySource = input.string("My source", defval = "close", options = ["close", "open", "high", "low"])

float sourceValue = switch mySource
  "close" => close
  "open" => open
  "high" => high
  "low" => low
  => na

---

This is where we are going to wrap up this article.

We hope you will find the signals produced by The Divergent a useful addition in your own strategies!

For more info on the The Divergent (Free) and The Divergent (Pro) indicators please see the linked pages.

If you have any questions, don't hesitate to reach out to us either via our website or via the comment section below.

If you found value in this article please give it a thumbs up!

Thank you!

Experiment with popular trading strategies without risking your capital: whitebox.so

Get access to The Divergent Pro indicator at thedivergent.io
Clause de non-responsabilité

Les informations et les publications ne sont pas destinées à être, et ne constituent pas, des conseils ou des recommandations en matière de finance, d'investissement, de trading ou d'autres types de conseils fournis ou approuvés par TradingView. Pour en savoir plus, consultez les Conditions d'utilisation.