Correlation Mandate for Relational Analysis
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © greenmask9

//@version=5
indicator("Correlation Mandate", shorttitle = "CM")


i_ticker = input.symbol(defval = "", title = "Symbol")
i_mode = input.string(defval = "Independent", title = "Mode", options = ["Independent","Correlational","Anti-Correlational"], tooltip = "[1] independent will use 1st color for bullish candles, 2nd color for bearish candles. [2] correlational will use 1st color when both the chart symbol and the selected symbol are in agreement, 2nd color for disagreement")

bull_col = input.color(#672bfd, "1st Color")
bear_col = input.color(#ffeb3b, "2nd Color")
invis_col = color.new(color.red, 100) //input.color(#ef5350, "2nd Color")

op = request.security(i_ticker, "", open, lookahead= barmerge.lookahead_on)
hh = request.security(i_ticker, "", high, lookahead= barmerge.lookahead_on)
cs = request.security(i_ticker, "", close, lookahead= barmerge.lookahead_on)
lw = request.security(i_ticker, "", low, lookahead= barmerge.lookahead_on)

//op = request.security(i_ticker, "", open, lookahead= barmerge.lookahead_off)
//hh = request.security(i_ticker, "", high, lookahead= barmerge.lookahead_off)
//cs = request.security(i_ticker, "", close, lookahead= barmerge.lookahead_off)
//lw = request.security(i_ticker, "", low, lookahead= barmerge.lookahead_off)//

growth_chart = close > open
growth_symbol = cs > op
agreement = growth_chart and growth_symbol

c_1 = op == op[1]
c_2 = hh == hh[1]
c_3 = cs == cs[1]
c_4 = lw == lw[1]
cc = not (c_1 and c_2 and c_3 and c_4)


col = cc ? (i_mode == "Independent" ? (growth_symbol ? bull_col : bear_col ) :
 i_mode == "Correlational" ? (agreement ? bull_col : bear_col ) :
 i_mode == "Anti-Correlational" ? (not agreement ? bull_col : bear_col ) : color.fuchsia) : invis_col

plotcandle(op, hh, lw, cs, color = col, wickcolor = col, bordercolor = invis_col)

///
///
///Correlation

corr_length = input.int(300, "Correlation length", group = "Correlation")
corr = ta.correlation(close, cs, corr_length)*100



///
///
///Tables

i_bgcolor = input.color(color.new(color.blue,100), "Table Background", group = "Tables")
i_table_col = input.color(color.new(color.yellow, 10), "Table Text", group = "Tables")

i_description = input.string("", title = "Description", inline = "1", group = "Tables")
i_act_name = input.bool(false, title = "Act", inline = "1", group = "Tables")

i_act_corr = input.bool(false, title = "Correlation Act", group = "Tables")

var table perfTable = table.new(position.top_right, 1, 3, border_width = 1)


_cellText1 = str.tostring(i_ticker)

table.cell(perfTable, 0, 0, _cellText1, bgcolor = i_bgcolor, text_color = i_table_col, width = 10, text_halign = text.align_right)

if i_act_name
    table.cell(perfTable, 0, 1, i_description, bgcolor = i_bgcolor, text_color = i_table_col, width = 10, text_halign = text.align_right)
if i_act_corr
    table.cell(perfTable, 0, 2, str.tostring(corr, format.percent), bgcolor = i_bgcolor, text_color = i_table_col, width = 7, text_halign = text.align_right)


///
///
/// EMA and SMA

ema_enabler = input.bool(true, "50 & 200 EMA", group = "MAs")
sma_enabler = input.bool(true, "50 & 200 SMA", group = "MAs")

//200 EMA Daily
daily200ema = ema_enabler ? request.security(syminfo.tickerid, "D", ta.ema(cs, 200)[1], lookahead=barmerge.lookahead_on) : na
ema200d = plot(daily200ema, title="200 EMA Daily", color= color.black, linewidth=1)

//50 EMA Daily
daily50ema = ema_enabler ? request.security(syminfo.tickerid, "D", ta.ema(cs, 50)[1], lookahead=barmerge.lookahead_on) : na
ema50d = plot(daily50ema, title="50 EMA Daily", color= color.red, linewidth=1)


//200 SMA Daily
daily200sma = sma_enabler ? request.security(syminfo.tickerid, "D", ta.sma(cs, 200)[1], lookahead=barmerge.lookahead_on) : na
sma200d = plot(daily200sma, title="200 EMA Daily", color= color.black, linewidth=1)

//50 SMA Daily
daily50sma = sma_enabler ? request.security(syminfo.tickerid, "D", ta.sma(cs, 50)[1], lookahead=barmerge.lookahead_on) : na
sma50d = plot(daily50sma, title="50 EMA Daily", color= color.red, linewidth=1)
