Programming a visual and audible alert for the TradingView platform in PineScript Language.
The same indicator (disparity index SMA by DRodriguezFX) is applied to two highly correlated instruments. The indicator values are then compared to each other to find instances when they diverge. Divergence is determined using simple maths. Divergence can occur in 3 ways:
1) indicators move in opposite directions. (one increases, while the other decreases)
2) One remains unchanged, while other increases or decreases.
3) Both move in same direction, but one moves a greater magnitude. In this case the first step involved is to determine by how much % the indicator has moved from the last value / one candle / period ago.
Example: Disparity indicator on instrument 1 moves from 0.0070 to 0.0080 = +0.0010
So 0.0010 / 0.0070 = 0.143 X 100 = 14.30%
Disparity indicator on instrument 2 moves from 0.0070 to 0.0075 = +0.0005
so 0.0005 / 0.0070 = 0.0714 X 100 = 7.14%
Now take the greater value and divide by smaller value then multiply by 100
= 14.30 / 7.14 X 100 = 200.28% Therefore one indicator moved 200.28% the value of the other. This % threshold value will be a custom input to determine if divergence has occurred. In other words if they both move in the same direction, divergence will only be shown if one moves by X% of the value of the first.
Once divergence has been confirmed, an arrow will be drawn on price in the opposite direction of the closing candle. So for example if divergence is confirmed and the candle closed higher, then a down arrow would be drawn above this candle and vice versa. If the candles close and open are the same, the previous candles range is used instead.
I will be uploading a video to Youtube soon to better explain what im after.
Note two instances of this indicator plotted for two different instruments cant be loaded in the same window. But as long as the corresponding values are compared, this shouldn't be a problem.
Indicator should be constantly monitoring every bar close to determine if divergence has occurred.