Negative Correlation Signals
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Blockhead305

//@version=5
indicator(title="Negative Correlation Signals", shorttitle="Negative Correlation Signals", overlay = true)

instrument_1 = input.symbol(defval = "OANDA:GBPJPY", title = "pair")
instrument_2 = input.symbol(defval = "PEPPERSTONE:JPYX", title = "index")
smoothK = input.int(3, "K", minval=1)
smoothD = input.int(3, "D", minval=1)
lengthRSI = input.int(14, "RSI Length", minval=1)
lengthStoch = input.int(14, "Stochastic Length", minval=1)

[open_1, high_1, low_1, close_1, atr_1] = request.security(instrument_1, timeframe.period, [open, high, low, close, ta.sma(math.abs(open - close), 14)])
[open_2, high_2, low_2, close_2, atr_2] = request.security(instrument_2, timeframe.period, [open, high, low, close, ta.sma(math.abs(open - close), 14)])


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Stoch RSI instrument 1

rsi1 = ta.rsi(close_1, lengthRSI)
k = ta.sma(ta.stoch(rsi1, rsi1, rsi1, lengthStoch), smoothK)
d = ta.sma(k, smoothD)
// plot(k, "K", color=#2962FF)
// plot(d, "D", color=#FF6D00)
// h0 = hline(80, "Upper Band", color=#787B86)
// hline(50, "Middle Band", color=color.new(#787B86, 50))
// h1 = hline(20, "Lower Band", color=#787B86)
// fill(h0, h1, color=color.rgb(33, 150, 243, 90), title="Background")


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Stoch RSI instrument 2

rsi2 = ta.rsi(close_2, lengthRSI)
k_2 = ta.sma(ta.stoch(rsi2, rsi2, rsi2, lengthStoch), smoothK)
d_2 = ta.sma(k_2, smoothD)
// plot(k_2, "K 2", color=#2962FF)
// plot(d_2, "D 2", color=#FF6D00)

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// candles

dominant_wick_multiple = input.float(defval = 2.0, title = "dominant wick multiple", minval = 1.0, maxval = 10.0, step = 0.1, group = "candlestick")
recessive_wick_multiple = input.float(defval = 0.7, title = "recessive wick multiple", minval = 0.1, maxval = 10.0, step = 0.1, group = "candlestick")
body_multiple = input.float(defval = 0.5, title = "body multiple", minval = 0.1, maxval = 10.0, step = 0.1, group = "candlestick")

body_1 = math.abs(close_1 - open_1)
upper_wick_1 = math.abs(math.max(open_1, close_1) - high_1)
lower_wick_1 = math.abs(math.min(open_1, close_1) - low_1)

ATR_upper_wick_1 = ta.sma(upper_wick_1, 200)
ATR_lower_wick_1 = ta.sma(lower_wick_1, 200)
ATR_body_1 = ta.sma(body_1, 200)

upper_wick_dominant_1 = (upper_wick_1 > body_1) and (upper_wick_1 > (ATR_upper_wick_1 * dominant_wick_multiple)) and (body_1 < (ATR_body_1 * body_multiple)) and (lower_wick_1 < (ATR_lower_wick_1 * recessive_wick_multiple))
lower_wick_dominant_1 = (lower_wick_1 > body_1) and (lower_wick_1 > (ATR_lower_wick_1 * dominant_wick_multiple)) and (body_1 < (ATR_body_1 * body_multiple)) and (upper_wick_1 < (ATR_upper_wick_1 * recessive_wick_multiple))
// bgcolor(upper_wick_dominant_1 ? color.green : lower_wick_dominant_1 ? color.red : na)

body_2 = math.abs(close_2 - open_2)
upper_wick_2 = math.abs(math.max(open_2, close_2) - high_2)
lower_wick_2 = math.abs(math.min(open_2, close_2) - low_2)

ATR_upper_wick_2 = ta.sma(upper_wick_2, 200)
ATR_lower_wick_2 = ta.sma(lower_wick_2, 200)
ATR_body_2 = ta.sma(body_2, 200)

upper_wick_dominant_2 = (upper_wick_2 > body_2) and (upper_wick_2 > (ATR_upper_wick_2 * dominant_wick_multiple)) and (body_2 < (ATR_body_2 * body_multiple)) and (lower_wick_2 < (ATR_lower_wick_2 * recessive_wick_multiple))
lower_wick_dominant_2 = (lower_wick_2 > body_2) and (lower_wick_2 > (ATR_lower_wick_2 * dominant_wick_multiple)) and (body_2 < (ATR_body_2 * body_multiple)) and (upper_wick_2 < (ATR_upper_wick_2 * recessive_wick_multiple))


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// signals



k_overbougt_and_dominant_upper_1 = k > 80 and upper_wick_dominant_1
k_oversold_and_dominant_lower_1 = k < 20 and lower_wick_dominant_1
k_overbougt_and_dominant_upper_2 = k_2 > 80 and upper_wick_dominant_2
k_oversold_and_dominant_lower_2 = k_2 < 20 and lower_wick_dominant_2

sell = k_overbougt_and_dominant_upper_1 and k_oversold_and_dominant_lower_2
buy = k_oversold_and_dominant_lower_1 and k_overbougt_and_dominant_upper_2

plotchar(sell, title = "sell", char = "S", location = location.abovebar, color = color.red, size = size.small)
plotchar(buy, title = "buy", char = "B", location = location.belowbar, color = color.green, size = size.small)

// signal_1

if buy and barstate.isconfirmed
    alert("buy found", freq = alert.freq_once_per_bar_close)
if sell and barstate.isconfirmed
    alert("sell found", freq = alert.freq_once_per_bar_close)

