SARW
// 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("SARW", overlay = true, max_bars_back=5000)		//SCRW = Sultan Asset Correlation - using Waves

//-------------------------------------------------------------------------------------------- BASIC INPUTS DECLARING
Maxbar = input(defval=1000, title='Max LookBack length?')
Swing_intensity = input.int(defval=20, title='Swing intensity')
Base_Asset = input.symbol('BTCUSDT', title='Base Asset')


f() =>
    [high, low]
[Base_Asset_High, Base_Asset_Low] = request.security(Base_Asset, 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))
//-------------------------------------------------------------------------------------------- MAIN VARS
float LastSwing_UP = 0
float LastSwing_DOWN = 0
float BaseAsset_LastSwing_UP = 0
float BaseAsset_LastSwing_DOWN = 0
bool LastIsHigh = true
float Final_Correlation = 0
int SwingCounter = 0
int Stander_ceiling = 100000
//-------------------------------------------------------------------------------------------- MAIN FUNCTIONS
Standardization (X) =>
    Result = X
    while math.abs(Result) > Stander_ceiling
        Result /= 10
    Result
    
prcentage (RecentSwing, PriviousSwing) =>
    (PriviousSwing - RecentSwing) / (RecentSwing  / 100)
    
Correlation (FirstAsset, SecondAsset) =>
    float R = (FirstAsset/SecondAsset)*100
    if math.abs(R) > Stander_ceiling
        R := Standardization(R)
    R
//--------------------------------------------------------------------------------------------

if barstate.islast

    LastSwing_UP := (low[Swing_intensity] + (high[Swing_intensity] - low[Swing_intensity]) / 2)              //Consider first candle as swing to start with
    LastSwing_DOWN := (low[Swing_intensity] + (high[Swing_intensity] - low[Swing_intensity]) / 2)
    BaseAsset_LastSwing_UP := (Base_Asset_Low[Swing_intensity] + (Base_Asset_High[Swing_intensity] - Base_Asset_Low[Swing_intensity]) / 2)
    BaseAsset_LastSwing_DOWN := (Base_Asset_Low[Swing_intensity] + (Base_Asset_High[Swing_intensity] - Base_Asset_Low[Swing_intensity]) / 2)

    
    for i = Swing_intensity to First_bar by 1  
 
        SwingLow = true
        SwingHigh = true

                                                                    //for loop to check if the candle is either high or low swing
        for j = 1 to Swing_intensity by 1
            if not(low[i] <= low[i - j]) or not(low[i] <= low[i + j]) or (low[i] == low[i + 1]) or (low[i] == low[i + 2])
                SwingLow := false
                SwingLow
            if not(high[i] >= high[i - j]) or not(high[i] >= high[i + j]) or (high[i] == high[i + 1]) or (high[i] == high[i + 2])
                SwingHigh := false
                SwingHigh
            
        if SwingLow
            if LastIsHigh                                   // If swing is low AND last swing was high
                SwingCounter +=1
                if (SwingCounter >= 2)
                    Current_prec = prcentage(LastSwing_UP, LastSwing_DOWN)
                    Base_prec = prcentage(BaseAsset_LastSwing_UP, BaseAsset_LastSwing_DOWN)
                    correlation_Result = Correlation(Current_prec, Base_prec)
                    Final_Correlation += correlation_Result
                    BaseAsset_LastSwing_DOWN := Base_Asset_Low[i]
                    LastSwing_DOWN := low[i]
                    LastIsHigh := false
                else                                        // For first time finding low swing
                    LastSwing_DOWN := low[i]
                    BaseAsset_LastSwing_DOWN := Base_Asset_Low[i]
                    LastIsHigh := false

            else if (low[i] < LastSwing_DOWN)             // If swing is low AND last swing was low too
                LastSwing_DOWN := low[i]
                BaseAsset_LastSwing_DOWN := Base_Asset_Low[i]
        
        if SwingHigh
            if not LastIsHigh or (SwingCounter < 1)            // (If swing is high AND last swing was low) OR (If this is the first swing ever)
                SwingCounter += 1
                if (SwingCounter >= 2)
                    Current_prec = prcentage(LastSwing_DOWN, LastSwing_UP)
                    Base_prec = prcentage(BaseAsset_LastSwing_DOWN, BaseAsset_LastSwing_UP)
                    correlation_Result = Correlation(Current_prec, Base_prec)
                    Final_Correlation += correlation_Result
                    BaseAsset_LastSwing_UP := Base_Asset_High[i]
                    LastSwing_UP := high[i]
                    LastIsHigh := true
                else
                    LastSwing_UP := high[i]
                    BaseAsset_LastSwing_UP := Base_Asset_High[i]
                    LastIsHigh := true

            else if (high[i] > LastSwing_UP)             // If swing is high AND last swing was high too
                LastSwing_UP := high[i]
                BaseAsset_LastSwing_UP := Base_Asset_High[i]
    SwingCounter -= 1    
    Final_Correlation := (Final_Correlation/SwingCounter)
    TextBox = "No Correlation"
    if math.abs(Final_Correlation) >= 40
        if Final_Correlation >= 40
            TextBox := "Direct Correlation"+"\nCorrelation Precentage : "+str.tostring(math.round(Final_Correlation))+"%"
        else
            TextBox := "Inverse Correlation"+"\nCorrelation Precentage : "+str.tostring(math.round(Final_Correlation))+"%"
    Lable = label.new(bar_index, high+high/5, text=TextBox, textcolor=color.black, size=size.huge, color=color.silver)
    Lable