SARC
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Sultan_shaya

//@version=5
indicator("SARC", overlay = true, max_bars_back=5000)		//SCR = Sultan Asset Correlation - Candles

//--------------------------------------------------------------------------------------------
Maxbar = input(defval=1000, title='Max LookBack length?')
Correlation_factor = input.int(defval=30, title='Correlation Factor')
SC = input.symbol('BTCUSDT', title='Base Asset')
Show_Correlation = input(defval=false, title='Show Correlation Candles')

f() =>
    [open, close]
[SC_O, SC_C] = request.security(SC, timeframe.period, f())      //CC = Second Chart
First_bar = Maxbar
if (math.abs(ta.barssince(barstate.isfirst)) < Maxbar)
    First_bar := math.abs(ta.barssince(barstate.isfirst))
//--------------------------------------------------------------------------------------------
prcentage(O, C) =>  //O = Candle Open   //C = Candle Close
    (C - O) / (O  / 100)

Correlation(C1O, C1C, C2O, C2C) =>  //C1O = Candle 1 Open   //C1C = Candle 1 Close
    FP = prcentage(C1O, C1C)  //FP = First candle Precentage
    SP = prcentage(C2O, C2C)  //SP = Second candle Precentage
    float R = (SP/FP)*100
    R
Change_Source(B_O,B_C,B_O1,B_C1,B_O2,B_C2,O,C,O1,C1,O2,C2) =>
    if (((B_O<B_C) and (B_O1<B_C1) and (B_O2>B_C2)) and ((O<C) and (O1<C1) and (O2<C2))) or (((B_O>B_C) or (B_O1>B_C1) and (B_O2<B_C2)) and ((O>C) and (O1>C1) and (O2>C2)))
        [true,false]        //[Current_follow_base, Base_follow_current]
    else if (((B_O<B_C) and (B_O1<B_C1) and (B_O2<B_C2)) and ((O<C) and (O1<C1) and (O2>C2))) or (((B_O>B_C) or (B_O1>B_C1) and (B_O2>B_C2)) and ((O>C) and (O1>C1) and (O2<C2)))
        [false,true]
    else
        [false,false]
//--------------------------------------------------------------------------------------------

if barstate.islast
    direct = 0
    inverse = 0
    Current_follow_Base = 0
    Base_follow_Current = 0
    BFCBFC = 0
    for i = 0 to First_bar by 1
        if close[i] == na
            break
        else
        //-------------------------------------------------------------------
        //-------------------------------------------------------------------
            Res = Correlation(SC_O[i+1], SC_C[i+1], open[i+1], close[i+1])              // Resistence calculation
            if (Res >= Correlation_factor)                                              // If candles are opposite add to counter
                direct += 1
            else if (Res <= -Correlation_factor)                                        // If candles have correlation, add to counter and check which one follows the other
                inverse += 1
                if ( Correlation(SC_O[i+2], SC_C[i+2], open[i+2], close[i+2]) > 30) and ( Correlation(SC_O[i+3], SC_C[i+3], open[i+3], close[i+3]) > 30)
                    [CFB,BFC] = Change_Source(SC_O[i+3], SC_C[i+3],SC_O[i+2], SC_C[i+2],SC_O[i+1], SC_C[i+1],open[i+3], close[i+3],open[i+2], close[i+2],open[i+1], close[i+1])
                    if BFC
                        BFCBFC += 1
                    if ( Correlation(SC_O[i+1], SC_C[i+1], open[i], close[i]) > 0) and CFB
                        Current_follow_Base += 1
                        if Show_Correlation
                            line.new(bar_index[i+1],high[i+1],bar_index[i+1],high[i+1]*5,color = color.green, width = 5)
                    else if ( Correlation(SC_O[i], SC_C[i], open[i+1], close[i+1]) > 0) and BFC
                        Base_follow_Current += 1
                        if Show_Correlation
                            line.new(bar_index[i+1],high[i+1],bar_index[i+1],high[i+1]*5,color = color.red, width = 5)
            TextBoxs = str.tostring(math.round(Res,2))
            //-------------------------------------------------------------------
            //-------------------------------------------------------------------
    Last_Cor = Correlation(SC_O, SC_C, open, close)
    if (Last_Cor >= 30)
        direct += 1
    else
        inverse += 1
        
        
    output = "No Correlation Found"                 //Preparing final result
    Correlation_precentage = 0
    if ((direct*(3/5))>=inverse)
        Correlation_precentage := (direct/First_bar)*100
        output := "Direct Correlation"+"\nCorrelation Precentage : "+str.tostring(math.round(Correlation_precentage))+"%"
        if ((Current_follow_Base*(3/5))>=Base_follow_Current)
            output += "\nCurrent Asset Follows Base"
        else if ((Base_follow_Current*(3/5))>=Current_follow_Base)
            output += "\nBase Follows Current Asset"
        
    else if ((inverse*(2/3))>=direct)
        Correlation_precentage := (inverse/First_bar)*100
        output := "Inverse Correlation"+"\nCorrelation Precentage : "+str.tostring(math.round(Correlation_precentage))+"%"
        
    TextBox = output
    label.new(bar_index, high+high/20, text=TextBox, textcolor=color.black, size=size.huge, color=color.silver)