Library "lib_divergence" offers a commonly usable function to detect divergences. This will take the default RSI or other symbols / indicators / oscillators as source data.
divergence(osc, pivot_left_bars, pivot_right_bars, div_min_range, div_max_range, ref_low, ref_high, min_divergence_offset_fraction, min_divergence_offset_dev_len, min_divergence_offset_atr_mul) Detects Divergences between Price and Oscillator action. For bullish divergences, look at trend lines between lows. For bearish divergences, look at trend lines between highs. (strong) oscillator trending, price opposing it | (medium) oscillator trending, price trend flat | (weak) price opposite trending, oscillator trend flat | (hidden) price trending, oscillator opposing it. Pivot detection is only properly done in oscillator data, reference price data is only compared at the oscillator pivot (speed optimization) Parameters: osc (float): (series float) oscillator data (can be anything, even another instrument price) pivot_left_bars (simple int): (simple int) optional number of bars left of a confirmed pivot point, confirming it is the highest/lowest in the range before and up to the pivot (default: 5) pivot_right_bars (simple int): (simple int) optional number of bars right of a confirmed pivot point, confirming it is the highest/lowest in the range from and after the pivot (default: 5) div_min_range (simple int): (simple int) optional minimum distance to the pivot point creating a divergence (default: 5) div_max_range (simple int): (simple int) optional maximum amount of bars in a divergence (default: 50) ref_low (float): (series float) optional reference range to compare the oscillator pivot points to. (default: low) ref_high (float): (series float) optional reference range to compare the oscillator pivot points to. (default: high) min_divergence_offset_fraction (simple float): (simple float) optional scaling factor for the offset zone (xDeviation) around the last oscillator H/L detecting following equal H/Ls (default: 0.01) min_divergence_offset_dev_len (simple int): (simple int) optional lookback distance for the deviation detection for the offset zone around the last oscillator H/L detecting following equal H/Ls. Used as well for the ATR that does the equal H/L detection for the reference price. (default: 14) min_divergence_offset_atr_mul (simple float): (simple float) optional scaling factor for the offset zone (xATR) around the last price H/L detecting following equal H/Ls (default: 1) return A tuple of deviation flags. [strong_bull, strong_bear, medium_bull, medium_bear, weak_bull, weak_bear, hidden_bull, hidden_bear, pivot_h, pivot_l]
リリースノート
v2 improved performance, fixed bug in 'weak bear' detection, improved naming and comments for function parameters
Updated: divergence(osc, pivot_confirmation_bars_left, pivot_confirmation_bars_right, pivot_min_x_distance, pivot_max_x_distance, ref_low, ref_high, pivot_min_y_distance_len, pivot_min_y_distance_dev_mult, pivot_min_y_distance_atr_mult) Detects Divergences between Price and Oscillator action. For bullish divergences, look at trend lines between lows. For bearish divergences, look at trend lines between highs. (strong) oscillator trending, price opposing it | (medium) oscillator trending, price trend flat | (weak) price opposite trending, oscillator trend flat | (hidden) price trending, oscillator opposing it. Pivot detection is only properly done in oscillator data, reference price data is only compared at the oscillator pivot (speed optimization) Parameters: osc (float): (series float) oscillator data (can be anything, even another instrument price) pivot_confirmation_bars_left (simple int): (simple int) optional number of bars left of a confirmed pivot point, confirming it is the highest/lowest in the range before and up to the pivot (default: 5) pivot_confirmation_bars_right (simple int): (simple int) optional number of bars right of a confirmed pivot point, confirming it is the highest/lowest in the range from and after the pivot (default: 3) pivot_min_x_distance (simple int): (simple int) optional minimum distance between pivot points considered for a divergence (should not be bigger than pivot_leftbars) (default: 5) pivot_max_x_distance (simple int): (simple int) optional maximum distance between pivot points considered for a divergence (should not be bigger than pivot_leftbars) (default: 50) ref_low (float): (series float) optional reference range to compare the oscillator pivot points to. (default: low) ref_high (float): (series float) optional reference range to compare the oscillator pivot points to. (default: high) pivot_min_y_distance_len (simple int): (simple int) optional lookback distance for the deviation detection for the offset zone around the last oscillator H/L when detecting following equal H/Ls. Used as well for the ATR that does the equal H/L detection for the reference price. (default: 14) pivot_min_y_distance_dev_mult (simple float): (simple float) optional scaling factor for the offset zone (xDeviation) around the last oscillator H/L when detecting following equal H/Ls (default: 0.1) pivot_min_y_distance_atr_mult (simple float): (simple float) optional scaling factor for the offset zone (xATR) around the last price H/L when detecting following equal H/Ls (default: 0.1) return A tuple of deviation flags. [strong_bull, strong_bear, medium_bull, medium_bear, weak_bull, weak_bear, hidden_bull, hidden_bear, osc_pivot_h, osc_pivot_l]