Average Quarterly and Annual Gain/Loss (Color Divided)
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © KioseffTrading

//@version=5
indicator("Average Quarterly and Annual Gain/Loss", overlay = true, max_lines_count = 500, max_labels_count = 500, max_bars_back = 500)

// _______________________________________________________
//
// Input Tab to Change Label Text Size and Divider Colors
// _______________________________________________________

benchmark = input.symbol(defval = "SPX", title = "Benchmark Asset", tooltip = "Changing the Symbol Allows for Comparison of Performance Between the Asset on Your Chart and the Asset Selected Here!")
warn = input.string(defval = "No", title = "Remove Top Right Instructions?", options = ["No", "Yes"])
size = input.string(defval = "Small", title = "Label Size", options = ["Small", "Tiny"], group = "Hover over labels for additional information")
hide = input.string(defval = "Off", title = "Hide Lines and Labels ? (Color Divided Quarters Only)", options = ["Off", "On"])
Q1color = input.color(defval = color.blue, title = "Q1 Color Scheme")
Q2color = input.color(defval = color.green, title = "Q2 Color Scheme")
Q3color = input.color(defval = color.red, title = "Q2 Color Scheme")
Q4color = input.color(defval = color.yellow, title = "Q4 Color Scheme")
annualColorFixed = input.color(defval = color.white, title = "Annual Fixed Plot Color")
annualColorRunning = input.color(defval = color.black, title = "Annual Running Color")

// _______________________________________________________
//
// Defining Fiscal Quarter Periods
// _______________________________________________________

var bool startCalculations = false
if bar_index > 0 and year(time) != year(time[1]) 
    startCalculations := true

var bool Q1 = false
if time >= timestamp(year(time), 01,01,00,00,00) and time <= timestamp(year(time), 03, 31, 23, 59, 59) and startCalculations == true
    Q1 := true
else 
    Q1 := false

var bool Q2 = false
if time >= timestamp(year(time), 04, 1, 00, 00, 00) and time <= timestamp(year(time), 06, 30, 23, 59, 59) and startCalculations == true
    Q2 := true
else
    Q2 := false

var bool Q3 = false
if time >= timestamp(year(time), 07, 1, 00, 00, 00) and time <= timestamp(year(time), 09, 30, 23, 59, 59) and startCalculations == true
    Q3 := true
else
    Q3 := false
    
var bool Q4 = false
if time >= timestamp(year(time), 10, 1, 00, 00, 00) and time <= timestamp(year(time), 12, 31, 23, 59, 59) and startCalculations == true  
    Q4 := true
else
    Q4 := false


// _______________________________________________________
//
// Pulling Index Data                             
// _______________________________________________________

SPXindex() => [close, open, high, low]
[spClose, spOpen, spHigh, spLow] = request.security(benchmark, "D", SPXindex())

// _______________________________________________________
//
// Quarter 1 Calculations                                 
// _______________________________________________________

var int Q1startIndex = 0
var int totalQ1 = 0
var bool onOffQ1 = false
var float Q1startClose = 0.0
var float Q1startHigh = 0.0
var float CumulativeQ1GainOrLoss = 0.0
var float Q1highest = 0.0
var float Q1lowest = 0.0

Q1highestHigh = ta.highest(high, bar_index + 1 - Q1startIndex)[1]
Q1lowestLow = ta.lowest(low, bar_index + 1 - Q1startIndex)[1]

if Q1 == true and Q1[1] == false 
    onOffQ1 := true
    Q1startClose := close
    Q1startIndex := bar_index
    Q1startHigh := high

if Q1[1] == true and Q1 == false and onOffQ1[1] == true
    CumulativeQ1GainOrLoss := ((close[1] / Q1startClose) - 1) * 100
    CumulativeQ1GainOrLoss := CumulativeQ1GainOrLoss[1] + CumulativeQ1GainOrLoss
    Q1highest := Q1highestHigh
    Q1lowest := Q1lowestLow

if Q1[1] == true and Q1 == false and onOffQ1 == true
    totalQ1 += 1
    onOffQ1 := false


avgQ1GainOrLoss = CumulativeQ1GainOrLoss / totalQ1

// _______________________________________________________
//
// Quarter 1 Index Calculations                                 
// _______________________________________________________

var int SPQ1startIndex = 0
var int totalSPQ1 = 0
var bool onOffSPQ1 = false
var float SPQ1startClose = 0.0
var float SPQ1startHigh = 0.0
var float CumulativeSPQ1GainOrLoss = 0.0
var float SPQ1highest = 0.0
var float SPQ1lowest = 0.0

var bool spxStart = false
if bar_index > 0 and year(time) != year(time[1])
    spxStart := true

SPQ1highestHigh = ta.highest(spHigh, bar_index + 1 - SPQ1startIndex)[1]
SPQ1lowestLow = ta.lowest(spLow, bar_index + 1 - SPQ1startIndex)[1]

if Q1 == true and Q1[1] == false and spxStart == true
    onOffSPQ1 := true
    SPQ1startClose := spClose
    SPQ1startIndex := bar_index
    SPQ1startHigh := spHigh

if Q1[1] == true and Q1 == false and onOffSPQ1[1] == true and spxStart == true
    CumulativeSPQ1GainOrLoss := ((spClose[1] / SPQ1startClose) - 1) * 100
    CumulativeSPQ1GainOrLoss := CumulativeSPQ1GainOrLoss[1] + CumulativeSPQ1GainOrLoss
    SPQ1highest := SPQ1highestHigh
    SPQ1lowest := SPQ1lowestLow

if Q1[1] == true and Q1 == false and onOffSPQ1 == true and spxStart == true
    totalSPQ1 += 1
    onOffSPQ1 := false


avgSPQ1GainOrLoss = CumulativeSPQ1GainOrLoss / totalSPQ1

// _______________________________________________________
//
// Quarter 2 Calculations                                 
// _______________________________________________________

var int Q2startIndex = 0
var int totalQ2 = 0
var bool onOffQ2 = false
var float Q2startClose = 0.0
var float Q2startHigh = 0.0
var float CumulativeQ2GainOrLoss = 0.0
var float Q2highest = 0.0
var float Q2lowest = 0.0 

Q2highestHigh = ta.highest(high, bar_index + 1 - Q2startIndex)[1]
Q2lowestLow = ta.lowest(low, bar_index + 1 - Q2startIndex)[1]

if Q2 == true and Q2[1] == false
    onOffQ2 := true
    Q2startClose := close
    Q2startIndex := bar_index
    Q2startHigh := high

if Q2[1] == true and Q2 == false
    CumulativeQ2GainOrLoss := ((close[1] / Q2startClose) - 1) * 100
    CumulativeQ2GainOrLoss := CumulativeQ2GainOrLoss[1] + CumulativeQ2GainOrLoss
    Q2highest := Q2highestHigh
    Q2lowest := Q2lowestLow

if Q2[1] == true and Q2 == false and onOffQ2 == true
    totalQ2 += 1
    onOffQ2 := false

avgQ2GainOrLoss = CumulativeQ2GainOrLoss / totalQ2

// _______________________________________________________
//
// Quarter 2 Index Calculations                                 
// _______________________________________________________

var int SPQ2startIndex = 0
var int totalSPQ2 = 0
var bool onOffSPQ2 = false
var float SPQ2startClose = 0.0
var float SPQ2startHigh = 0.0
var float CumulativeSPQ2GainOrLoss = 0.0
var float SPQ2highest = 0.0
var float SPQ2lowest = 0.0

SPQ2highestHigh = ta.highest(spHigh, bar_index + 1 - SPQ2startIndex)[1]
SPQ2lowestLow = ta.lowest(spLow, bar_index + 1 - SPQ2startIndex)[1]

if Q2 == true and Q2[1] == false and spxStart == true
    onOffSPQ2 := true
    SPQ2startClose := spClose
    SPQ2startIndex := bar_index
    SPQ2startHigh := spHigh

if Q2[1] == true and Q2 == false and onOffSPQ2[1] == true and spxStart == true
    CumulativeSPQ2GainOrLoss := ((spClose[1] / SPQ2startClose) - 1) * 100
    CumulativeSPQ2GainOrLoss := CumulativeSPQ2GainOrLoss[1] + CumulativeSPQ2GainOrLoss
    SPQ2highest := SPQ2highestHigh
    SPQ2lowest := SPQ2lowestLow

if Q2[1] == true and Q2 == false and onOffSPQ2 == true and spxStart == true
    totalSPQ2 += 1
    onOffSPQ2 := false

avgSPQ2GainOrLoss = CumulativeSPQ2GainOrLoss / totalSPQ2


// _______________________________________________________
//
// Quarter 3 Calculations                                 
// _______________________________________________________

var int Q3startIndex = 0
var int totalQ3 = 0
var bool onOffQ3 = false
var float Q3startClose = 0.0
var float Q3startHigh = 0.0
var float CumulativeQ3GainOrLoss = 0.0
var float Q3highest = 0.0
var float Q3lowest = 0.0

Q3highestHigh = ta.highest(high, bar_index + 1 - Q3startIndex)[1]
Q3lowestLow = ta.lowest(low, bar_index + 1 - Q3startIndex)[1]

if Q3 == true and Q3[1] == false
    onOffQ3 := true
    Q3startClose := close
    Q3startIndex := bar_index
    Q3startHigh := high

if Q3[1] == true and Q3 == false
    CumulativeQ3GainOrLoss := ((close[1] / Q3startClose) - 1) * 100
    CumulativeQ3GainOrLoss := CumulativeQ3GainOrLoss[1] + CumulativeQ3GainOrLoss
    Q3highest := Q3highestHigh
    Q3lowest := Q3lowestLow

if Q3 == false and Q3[1] == true and onOffQ3 == true
    totalQ3 += 1
    onOffQ3 := false

avgQ3GainOrLoss = CumulativeQ3GainOrLoss / totalQ3


// _______________________________________________________
//
// Quarter 3 Index Calculations                                 
// _______________________________________________________

var int SPQ3startIndex = 0
var int totalSPQ3 = 0
var bool onOffSPQ3 = false
var float SPQ3startClose = 0.0
var float SPQ3startHigh = 0.0
var float CumulativeSPQ3GainOrLoss = 0.0
var float SPQ3highest = 0.0
var float SPQ3lowest = 0.0

SPQ3highestHigh = ta.highest(spHigh, bar_index + 1 - SPQ3startIndex)[1]
SPQ3lowestLow = ta.lowest(spLow, bar_index + 1 - SPQ3startIndex)[1]

if Q3 == true and Q3[1] == false and spxStart == true
    onOffSPQ3 := true
    SPQ3startClose := spClose
    SPQ3startIndex := bar_index
    SPQ3startHigh := spHigh

if Q3[1] == true and Q3 == false and onOffSPQ3[1] == true and spxStart == true
    CumulativeSPQ3GainOrLoss := ((spClose[1] / SPQ3startClose) - 1) * 100
    CumulativeSPQ3GainOrLoss := CumulativeSPQ3GainOrLoss[1] + CumulativeSPQ3GainOrLoss
    SPQ3highest := SPQ3highestHigh
    SPQ3lowest := SPQ3lowestLow

if Q3[1] == true and Q3 == false and onOffSPQ3 == true and spxStart == true
    totalSPQ3 += 1
    onOffSPQ3 := false


avgSPQ3GainOrLoss = CumulativeSPQ3GainOrLoss / totalSPQ3

// _______________________________________________________
//
// Quarter 4 Calculations                                 
// _______________________________________________________

var int Q4startIndex = 0
var int totalQ4 = 0
var bool onOffQ4 = false
var float Q4startClose = 0.0
var float Q4startHigh = 0.0
var float CumulativeQ4GainOrLoss = 0.0
var float Q4highest = 0.0
var float Q4lowest = 0.0 

Q4highestHigh = ta.highest(high, bar_index + 1 - Q4startIndex)[1]
Q4lowestLow = ta.lowest(low, bar_index + 1 - Q4startIndex)[1]

if Q4 == true and Q4[1] == false
    onOffQ4 := true
    Q4startClose := close
    Q4startIndex := bar_index
    Q4startHigh := high

if Q4[1] == true and Q4 == false
    CumulativeQ4GainOrLoss := ((close[1] / Q4startClose) - 1) * 100
    CumulativeQ4GainOrLoss := CumulativeQ4GainOrLoss[1] + CumulativeQ4GainOrLoss
    Q4highest := Q4highestHigh
    Q4lowest := Q4lowestLow

if Q4 == false and Q4[1] == true and onOffQ4 == true
    totalQ4 += 1
    onOffQ4 := false

avgQ4GainOrLoss = CumulativeQ4GainOrLoss / totalQ4

// _______________________________________________________
//
// Quarter 4 Index Calculations                                 
// _______________________________________________________

var int SPQ4startIndex = 0
var int totalSPQ4 = 0
var bool onOffSPQ4 = false
var float SPQ4startClose = 0.0
var float SPQ4startHigh = 0.0
var float CumulativeSPQ4GainOrLoss = 0.0
var float SPQ4highest = 0.0
var float SPQ4lowest = 0.0

SPQ4highestHigh = ta.highest(spHigh, bar_index + 1 - SPQ4startIndex)[1]
SPQ4lowestLow = ta.lowest(spLow, bar_index + 1 - SPQ4startIndex)[1]

if Q4 == true and Q4[1] == false and spxStart == true
    onOffSPQ4 := true
    SPQ4startClose := spClose
    SPQ4startIndex := bar_index
    SPQ4startHigh := spHigh

if Q4[1] == true and Q4 == false and onOffSPQ4[1] == true and spxStart == true
    CumulativeSPQ4GainOrLoss := ((spClose[1] / SPQ4startClose) - 1) * 100
    CumulativeSPQ4GainOrLoss := CumulativeSPQ4GainOrLoss[1] + CumulativeSPQ4GainOrLoss
    SPQ4highest := SPQ4highestHigh
    SPQ4lowest := SPQ4lowestLow

if Q4[1] == true and Q4 == false and onOffSPQ4 == true and spxStart == true
    totalSPQ4 += 1
    onOffSPQ4 := false


avgSPQ4GainOrLoss = CumulativeSPQ4GainOrLoss / totalSPQ4

// _______________________________________________________
//
// Annual Calculations                                 
// _______________________________________________________

yearChange = year(time) != year(time[1])
annualClosePrev = ta.valuewhen(yearChange, close, 1)

var float yearClose = 0.0    
var float yearClosePrev = 0.0
var float cumulativeAnnualPerformance = 0.0 
var int annualCounter = -1

if yearChange and startCalculations == true 
    yearClose := close
    yearClosePrev := annualClosePrev
    annualCounter += 1
    cumulativeAnnualPerformance := ((close[1] / yearClosePrev) - 1) * 100
if cumulativeAnnualPerformance[1] != cumulativeAnnualPerformance and startCalculations[10] == true 
    cumulativeAnnualPerformance := cumulativeAnnualPerformance[1] + cumulativeAnnualPerformance
if cumulativeAnnualPerformance[1] != cumulativeAnnualPerformance and startCalculations[10] == false
    cumulativeAnnualPerformance := 0.0

avgAnnualGainOrLoss = cumulativeAnnualPerformance / annualCounter 
runningAnnualPerformance = ((close / yearClose) - 1) * 100

// _______________________________________________________
//
// Annual Index Calculations                                 
// _______________________________________________________

annualClosePrevSP = ta.valuewhen(yearChange, spClose, 1)

var float yearCloseSP = 0.0    
var float yearClosePrevSP = 0.0
var float cumulativeAnnualPerformanceSP = 0.0 
var int annualCounterSP = -1

if yearChange and startCalculations == true 
    yearCloseSP := spClose
    yearClosePrevSP := annualClosePrevSP
    annualCounterSP += 1
    cumulativeAnnualPerformanceSP := ((spClose[1] / yearClosePrevSP) - 1) * 100
if cumulativeAnnualPerformanceSP[1] != cumulativeAnnualPerformanceSP
    cumulativeAnnualPerformanceSP := cumulativeAnnualPerformanceSP[1] + cumulativeAnnualPerformanceSP

if cumulativeAnnualPerformanceSP[1] != cumulativeAnnualPerformanceSP and startCalculations[10] == false
    cumulativeAnnualPerformanceSP := 0.0


avgAnnualGainOrLossSP = 0.0
if syminfo.ticker == "SPX" and startCalculations == true
    avgAnnualGainOrLossSP := avgAnnualGainOrLoss
else
    avgAnnualGainOrLossSP := cumulativeAnnualPerformanceSP / annualCounterSP
runningAnnualPerformanceSP = ((spClose / yearCloseSP) - 1) * 100


// ___________________________________________________________________
//
// One-Time Initialization Variables for Lines, Labels, and Linefills
// ___________________________________________________________________

var line Q1divider1 = na
var line Q1divider2 = na
var line Q2divider1 = na
var line Q2divider2 = na
var line Q3divider1 = na
var line Q3divider2 = na
var line Q4divider1 = na
var line Q4divider2 = na
var line Q1connector = na
var line Q2connector = na
var line Q3connector = na
var line Q4connector = na
var line annualConnector = na

var linefill Q1Q1connector = na
var linefill Q1Q2connector = na
var linefill Q2Q2connector = na
var linefill Q2Q3connector = na
var linefill Q3Q3connector = na
var linefill Q4Q4connector = na
var linefill Q4Q1connector = na

var label Q1performance = na
var label Q2performance = na
var label Q3performance = na
var label Q4performance = na
var label annualPerformance = na
var label currentAnnualPerformance = na

// _____________________________________________________________________________
//
// User Input Variable for Calculating the % Gain/Loss Between Quarter Changes
// _____________________________________________________________________________

quarterPerformance(x, y) => 
    calculation = x / y
    calculation1 = calculation - 1
    finalCalculation = calculation1 * 100
    finalCalculation

currentQ1Performance = quarterPerformance(close, Q1startClose)
currentQ2Performance = quarterPerformance(close, Q2startClose)
currentQ3Performance = quarterPerformance(close, Q3startClose)
currentQ4Performance = quarterPerformance(close, Q4startClose)
currentSPQ1Performance = quarterPerformance(spClose[1], SPQ1startClose)
currentSPQ2Performance = quarterPerformance(spClose[1], SPQ2startClose)
currentSPQ3Performance = quarterPerformance(spClose[1], SPQ3startClose)
currentSPQ4Performance = quarterPerformance(spClose[1], SPQ4startClose)
runningSPQ1Performance = quarterPerformance(spClose, SPQ1startClose)
runningSPQ2Performance = quarterPerformance(spClose, SPQ2startClose)
runningSPQ3Performance = quarterPerformance(spClose, SPQ3startClose)
runningSPQ4Performance = quarterPerformance(spClose, SPQ4startClose)


// _____________________________________________________________________________
//
// Additional Calculations
// _____________________________________________________________________________


Q1comparison = currentSPQ1Performance - quarterPerformance(close[1], Q1startClose)
Q2comparison = currentSPQ2Performance - quarterPerformance(close[1], Q2startClose)
Q3comparison = currentSPQ3Performance - quarterPerformance(close[1], Q3startClose)
Q4comparison = currentSPQ4Performance - quarterPerformance(close[1], Q4startClose)
annualComparison = runningAnnualPerformanceSP[1] - runningAnnualPerformance[1]
runningQ1comparison = quarterPerformance(spClose, SPQ1startClose) - quarterPerformance(close, Q1startClose) 
runningQ2comparison = quarterPerformance(spClose, SPQ2startClose) - quarterPerformance(close, Q2startClose) 
runningQ3comparison = quarterPerformance(spClose, SPQ3startClose) - quarterPerformance(close, Q3startClose) 
runningQ4comparison = quarterPerformance(spClose, SPQ4startClose) - quarterPerformance(close, Q4startClose) 
runningAnnualComparison = runningAnnualPerformanceSP - runningAnnualPerformance



comparisonString1 = Q1comparison > 0.0 ? " Better Than Asset" : Q1comparison == 0.0 ? " Equal Performance to Asset" : " Worse Than Asset"
comparisonString2 = Q2comparison > 0.0 ? " Better Than Asset" : Q2comparison == 0.0 ? " Equal Performance to Asset" : " Worse Than Asset"
comparisonString3 = Q3comparison > 0.0 ? " Better Than Asset" : Q3comparison == 0.0 ? " Equal Performance to Asset" : " Worse Than Asset"
comparisonString4 = Q4comparison > 0.0 ? " Better Than Asset" : Q4comparison == 0.0 ? " Equal Performance to Asset" : " Worse Than Asset"
runningComparisonString1 = runningQ1comparison > 0.0 ? " Better Than Asset" : runningQ1comparison == 0.0 ? " Equal Performance to Asset" : " Worse Than Asset"
runningComparisonString2 = runningQ2comparison > 0.0 ? " Better Than Asset" : runningQ2comparison == 0.0 ? " Equal Performance to Asset" : " Worse Than Asset"
runningComparisonString3 = runningQ3comparison > 0.0 ? " Better Than Asset" : runningQ3comparison == 0.0 ? " Equal Performance to Asset" : " Worse Than Asset"
runningComparisonString4 = runningQ4comparison > 0.0 ? " Better Than Asset" : runningQ4comparison == 0.0 ? " Equal Performance to Asset" : " Worse Than Asset"
annualComparisonString = annualComparison > 0.0 ? " Better Than Asset" : annualComparison == 0.0 ? " Equal Performance to Asset" : " Worse Than Asset"
runningAnnualComparisonString = runningAnnualComparison > 0.0 ? " Better Than Asset" : runningAnnualComparison == 0.0 ? " Equal Performance to Asset" : " Worse Than Asset"


// _____________________________________________________________________________
//
// Y-axis Plot for Lables
// _____________________________________________________________________________

quarterPlot = ta.lowest(close, 63)
highL = timeframe.isdwm ? high * 1.25 : high * 1.05
lowL =  timeframe.isdwm ? low * .75 : low * .95
highL1 = timeframe.isdwm ? high * 1.26 : high * 1.06
lowL1 =  timeframe.isdwm ? low * .73 : low * .94


// __________________________________________________________
//
// Plotting Lines, Labels, and Linefills for Q1
// __________________________________________________________

if Q1[1] == false and Q1 == true
    line.delete(Q4divider2)
    line.delete(Q4connector)
    Q1divider1 := line.new(bar_index, highL, bar_index - 1, lowL, color = hide == "Off" ? color.new(Q1color, 0) : color.new(color.white, 100))
    Q4Q1connector := linefill.new(Q1divider1, Q4divider1, color.new(Q4color, 95))
    label.delete(Q4performance)
    Q4performance := startCalculations[10] == true ? label.new(bar_index - 30, quarterPlot * .90, color = hide == "Off" ? color.new(Q4color, 50) : color.new(color.white, 100), text = "Q4 " + str.tostring(year(time[1])) + " Performance: " + 
     str.tostring(currentQ4Performance[1], format.percent) + "\nAverage Q4 Performance: " + str.tostring(avgQ4GainOrLoss, format.percent) + "\n\nQ4 Highest High: $" + str.tostring(Q4highest, format.mintick) 
     + "\nQ4 Lowest Low: $" + str.tostring(Q4lowest, format.mintick), 
      tooltip = "Q4 " + str.tostring(year(time[1])) + " " + str.tostring(benchmark) + " Performance: " + str.tostring(currentSPQ4Performance, format.percent) + " \n(" + str.tostring(Q4comparison,format.percent) + " " + comparisonString4 + ")" + 
         "\nAverage Q4 " + str.tostring(benchmark) + " Performance: " + str.tostring(avgSPQ4GainOrLoss, format.percent) + "\n___________________________\n⬆️Average Performance Calculation Starts on the First Trading Session for the Asset on Your Chart",
     textcolor = hide == "Off" ? color.white : color.new(color.white, 100), style = label.style_label_center, size = size == "Small" ? size.small : size.tiny) : na
    Q1performance := label.new(bar_index + 1, highL1, text = "Current Q1 Performance: " + str.tostring(currentQ1Performance, format.percent) + 
     "\nAverage Q1 Performance: " + str.tostring(avgQ1GainOrLoss, format.percent), color = hide == "Off" ? color.new(Q1color, 50) : color.new(color.white, 100), textcolor = hide == "Off" ? color.white : color.new(color.white, 100))
    annualPerformance := startCalculations[10] == true ? label.new(Q1startIndex - 1, line.get_y2(Q1divider1) * .75, text = str.tostring(year(time[1])) + " Annual Performance: " + 
     str.tostring(runningAnnualPerformance[1], format.percent) + "\nAverage Annual Performace: " + str.tostring(avgAnnualGainOrLoss, format.percent),
     tooltip = str.tostring(year(time[1])) + " " + str.tostring(benchmark) + " Annual Performance: " + str.tostring(runningAnnualPerformanceSP[1], format.percent) + "\n(" + str.tostring(annualComparison, format.percent) + annualComparisonString 
         + ")\n" + str.tostring(benchmark) + " Average Annual Performance: " + str.tostring(avgAnnualGainOrLossSP, format.percent) +
         "\n___________________________\n⬆️Average Performance Calculation Starts on the First Trading Session for the Asset on Your Chart",
     color = hide == "Off" ? color.new(annualColorFixed, 50) : color.new(color.white, 100), textcolor = hide == "Off" ? color.black : color.new(color.white, 100), style = label.style_label_up, size = size == "Small" ? size.small : size.tiny) : na
    annualConnector := line.new(Q1startIndex - 1, lowL, Q1startIndex - 1, label.get_y(annualPerformance), color = annualColorFixed, style = line.style_dashed)

if Q1 == true 
    line.delete(Q1divider2[1])
    Q1divider2 := line.new(bar_index + 1 , highL, bar_index + 1, lowL, color = hide == "Off" ? color.new(Q1color,0) : color.new(color.white, 100))
    Q1Q1connector := linefill.new(Q1divider1, Q1divider2, color.new(Q1color, 95))
    label.delete(Q1performance)
    Q1performance := label.new(bar_index + 1, highL1, text = "Current Q1 Performance: " + str.tostring(currentQ1Performance, format.percent) + 
     "\nAverage Q1 Performance: " + str.tostring(avgQ1GainOrLoss, format.percent), tooltip = 
         "Q1 " + str.tostring(year(time[1])) + " " + str.tostring(benchmark) + " Performance: "  + str.tostring(runningSPQ1Performance, format.percent) + 
             " \n(" + str.tostring(runningQ1comparison, format.percent) + runningComparisonString1 + ")" + "\nAverage Q1 " + str.tostring(benchmark) + " Performance: "  
             + str.tostring(avgSPQ1GainOrLoss, format.percent) + "\n___________________________\n⬆️Average Performance Calculation Starts on the First Trading Session for the Asset on Your Chart",
                 color = hide == "Off" ? color.new(Q1color, 50) : color.new(color.white, 100), textcolor = hide == "Off" ? color.white : color.new(color.white, 100))
    line.delete(Q1connector[1])
    Q1connector := line.new(Q1startIndex, Q1startHigh, bar_index, high, color = hide == "Off" ? Q1color : color.new(color.white, 100))

// __________________________________________________________
//
// Plotting Lines, Labels, and Linefills for Q2
// __________________________________________________________

if Q2[1] == false and Q2 == true
    line.delete(Q1divider2)
    line.delete(Q1connector)
    Q2divider1 := line.new(bar_index, highL, bar_index - 1, lowL, color = hide == "Off" ? Q2color : color.new(color.white, 100))
    Q1Q2connector := linefill.new(Q2divider1, Q1divider1, color.new(Q1color, 95))
    label.delete(Q1performance)
    Q1performance := label.new(bar_index - 30, quarterPlot * .90, color = hide == "Off" ? color.new(Q1color, 50) : color.new(color.white, 100), text = "Q1 " + str.tostring(year(time)) + " Performance: " + 
     str.tostring(currentQ1Performance[1], format.percent) + "\nAverage Q1 Performance: " + str.tostring(avgQ1GainOrLoss, format.percent) + "\n\nHighest Q1 High: $" + str.tostring(Q1highest, format.mintick) + "\nLowest Q1 Low: $" 
         + str.tostring(Q1lowest, format.mintick), 
          tooltip = "Q1 " + str.tostring(year(time[1])) + " " + str.tostring(benchmark) + " Performance: " + str.tostring(currentSPQ1Performance, format.percent) 
             + " \n(" + str.tostring(Q1comparison,format.percent) + " " + comparisonString1 + ")" +  "\nAverage Q1 " + str.tostring(benchmark) + " Performance: "
             + str.tostring(avgSPQ1GainOrLoss, format.percent) + "\n___________________________\n⬆️Average Performance Calculation Starts on the First Trading Session for the Asset on Your Chart",
     textcolor = hide == "Off" ? color.white : color.new(color.white, 100), style = label.style_label_center, size = size == "Small" ? size.small : size.tiny)
    Q2performance := label.new(bar_index + 1, highL1, text = "Current Q2 Performance: " + str.tostring(currentQ2Performance, format.percent) + 
     "\nAverage Q2 Performance: " + str.tostring(avgQ2GainOrLoss, format.percent), color = hide == "Off" ? color.new(Q2color, 50) : color.new(color.white, 100), textcolor = hide == "Off" ? color.white : color.new(color.white, 100))

if Q2 == true
    line.delete(Q2divider2[1])
    Q2divider2 := line.new(bar_index + 1 , high * 1.24, bar_index + 1, lowL, color = hide == "Off" ? color.new(Q2color,0) : color.new(color.white, 100))
    Q2Q2connector := linefill.new(Q2divider1, Q2divider2, color.new(Q2color, 95))
    label.delete(Q2performance)
    Q2performance := label.new(bar_index + 1, highL1, text = "Current Q2 Performance: " + str.tostring(currentQ2Performance, format.percent) + 
     "\nAverage Q2 Performance: " + str.tostring(avgQ2GainOrLoss, format.percent), 
      tooltip =  "Q2 " + str.tostring(year(time[1])) + " " + str.tostring(benchmark) + " Performance: " + str.tostring(runningSPQ2Performance, format.percent) + 
         " \n(" + str.tostring(runningQ2comparison, format.percent) + runningComparisonString2 + ")" +  "\nAverage Q2 " + str.tostring(benchmark) + " Performance: " 
             + str.tostring(avgSPQ2GainOrLoss, format.percent) + "\n___________________________\n⬆️Average Performance Calculation Starts on the First Trading Session for the Asset on Your Chart",
                 color = hide == "Off" ? color.new(Q2color, 50) : color.new(color.white, 100), textcolor = hide == "Off" ? color.white : color.new(color.white, 100))
    line.delete(Q2connector[1])
    Q2connector := line.new(Q2startIndex, Q2startHigh, bar_index, high, color = hide == "Off" ? Q2color : color.new(color.white, 100))

// __________________________________________________________
//
// Plotting Lines, Labels, and Linefills for Q3
// __________________________________________________________

if Q3 == true and Q3[1] == false
    line.delete(Q2divider2)
    line.delete(Q2connector)
    Q3divider1 := line.new(bar_index, highL, bar_index - 1, lowL, color = hide == "Off" ? Q3color : color.new(color.white, 100))
    Q2Q3connector := linefill.new(Q2divider1, Q3divider1, color.new(Q2color, 95))
    label.delete(Q2performance)
    Q2performance := label.new(bar_index - 30, quarterPlot * .90, color = hide == "Off" ? color.new(Q2color, 50) : color.new(color.white, 100), text = "Q2 " + str.tostring(year(time)) + " Performance: " + 
     str.tostring(currentQ2Performance[1], format.percent) + "\nAverage Q2 Performance: " + str.tostring(avgQ2GainOrLoss, format.percent) + "\n\nQ2 Highest High: $" + str.tostring(Q2highest, format.mintick) 
         + "\nQ2 Lowest Low: $" + str.tostring(Q2lowest, format.mintick), 
     textcolor = hide == "Off" ? color.white : color.new(color.white, 100), style = label.style_label_center, size = size == "Small" ? size.small : size.tiny, 
     tooltip = "Q2 " + str.tostring(year(time[1])) + " " + str.tostring(benchmark) + " Performance: " + str.tostring(currentSPQ2Performance, format.percent)  + " \n(" + str.tostring(Q2comparison,format.percent) + " " + comparisonString2 + ")" 
         + "\nAverage Q2" + " " + str.tostring(benchmark) + " Performance: " 
             + str.tostring(avgSPQ2GainOrLoss, format.percent) + "\n___________________________\n⬆️Average Performance Calculation Starts on the First Trading Session for the Asset on Your Chart")
    Q3performance := label.new(bar_index + 1, highL1, text = "Current Q3 Performance: " + str.tostring(currentQ3Performance, format.percent) + 
     "\nAverage Q2 Performance: " + str.tostring(avgQ3GainOrLoss, format.percent), color = hide == "Off" ? color.new(Q3color, 50) : color.new(color.white, 100), textcolor = hide == "Off" ? color.white : color.new(color.white, 100))

if Q3 == true 
    line.delete(Q3divider2)
    Q3divider2 := line.new(bar_index + 1 , highL, bar_index + 1, lowL, color = hide == "Off" ? color.new(Q3color ,0) : color.new(color.white, 100))
    Q3Q3connector := linefill.new(Q3divider1, Q3divider2, color.new(Q3color, 95))
    label.delete(Q3performance)
    Q3performance := label.new(bar_index + 1, highL1, text = "Current Q3 Performance: " + str.tostring(currentQ3Performance, format.percent) + 
     "\nAverage Q3 Performance: " + str.tostring(avgQ3GainOrLoss, format.percent),  
     tooltip = "Q3 " + str.tostring(year(time[1])) + " " + str.tostring(benchmark) + " Performance: " + str.tostring(runningSPQ3Performance, format.percent) + 
         " \n(" + str.tostring(runningQ3comparison, format.percent) + runningComparisonString3 + ")" + "\nAverage Q3 "+ str.tostring(benchmark) + " Performance: " 
             + str.tostring(avgSPQ3GainOrLoss, format.percent) + "\n___________________________\n⬆️Average Performance Calculation Starts on the First Trading Session for the Asset on Your Chart",
                 color = hide == "Off" ? color.new(Q3color, 50) : color.new(color.white, 100), textcolor = hide == "Off" ? color.white : color.new(color.white, 100))
    line.delete(Q3connector[1])
    Q3connector := line.new(Q3startIndex, Q3startHigh, bar_index, high, color = hide == "Off" ? Q3color : color.new(color.white, 100))

// __________________________________________________________
//
// Plotting Lines, Labels, and Linefills for Q4
// __________________________________________________________

if Q4 == true and Q4[1] == false
    line.delete(Q3divider2)
    line.delete(Q3connector)
    Q4divider1 := line.new(bar_index, highL, bar_index - 1, lowL, color = hide == "Off" ? Q4color : color.new(color.white, 100))
    Q4Q1connector := linefill.new(Q4divider1, Q3divider1, color.new(Q3color, 95))
    label.delete(Q3performance)
    Q3performance := label.new(bar_index - 30, quarterPlot * .90, color = hide == "Off" ? color.new(Q3color, 50) : color.new(color.white, 100), text = "Q3 " + str.tostring(year(time)) + " Performance: " + 
     str.tostring(currentQ3Performance[1], format.percent) + "\nAverage Q3 Performance: " + str.tostring(avgQ3GainOrLoss, format.percent) + "\n\nQ3 Highest High: $" + str.tostring(Q3highest, format.mintick) 
     + "\nQ3 Lowest Low: $" + str.tostring(Q3lowest, format.mintick), 
      tooltip = "Q3 " + str.tostring(year(time[1])) + " " + str.tostring(benchmark) + " Performance: "+ str.tostring(currentSPQ3Performance, format.percent) + 
         " \n(" + str.tostring(Q3comparison,format.percent) + " " + comparisonString3 + ")" + "\nAverage Q1" + " " + str.tostring(benchmark) + " Performance: " 
         + str.tostring(avgSPQ3GainOrLoss, format.percent) + "\n___________________________\n⬆️Average Performance Calculation Starts on the First Trading Session for the Asset on Your Chart",
     textcolor = hide == "Off" ? color.white : color.new(color.white, 100), style = label.style_label_center, size = size == "Small" ? size.small : size.tiny)
    Q4performance := label.new(bar_index + 1, highL1, text = "Current Q4 Performance: " + str.tostring(currentQ4Performance, format.percent)  
     + " (" + str.tostring(Q1comparison,format.percent) + " " + comparisonString1 + ")" + 
     "\nAverage Q4 Performance: " + str.tostring(avgQ4GainOrLoss, format.percent), color = hide == "Off" ? color.new(Q4color, 50) : color.new(color.white, 100), textcolor = hide == "Off" ? color.white : color.new(color.white, 100))

if Q4 == true 
    line.delete(Q4divider2)
    Q4divider2 := line.new(bar_index + 1 , highL, bar_index + 1, lowL, color = hide == "Off" ? color.new(Q4color ,0) : color.new(color.white, 100))
    Q4Q4connector := linefill.new(Q4divider1, Q4divider2, color.new(Q4color, 95))
    label.delete(Q4performance)
    Q4performance := label.new(bar_index + 1, highL1, text = "Current Q4 Performance: " + str.tostring(currentQ4Performance, format.percent) + 
     "\nAverage Q4 Performance: " + str.tostring(avgQ4GainOrLoss, format.percent), 
      tooltip = "Q4 " + str.tostring(year(time[1])) + " " + str.tostring(benchmark) + " Performance: " + str.tostring(runningSPQ4Performance, format.percent) + 
         " \n(" + str.tostring(runningQ4comparison, format.percent) + runningComparisonString4 + ")" + "\nAverage Q4"+ " " + str.tostring(benchmark) + " Performance: "
             + str.tostring(avgSPQ4GainOrLoss, format.percent) + "\n___________________________\n⬆️Average Performance Calculation Starts on the First Trading Session for the Asset on Your Chart",
                 color = hide == "Off" ? color.new(Q4color, 50) : color.new(color.white, 100), textcolor = hide == "Off" ? color.white : color.new(color.white, 100))
    line.delete(Q4connector[1])
    Q4connector := line.new(Q4startIndex, Q4startHigh, bar_index, high, color = hide == "Off" ? Q4color : color.new(color.white, 100))

// __________________________________________________________
//
// Plotting Label for Current Annual % Gain/Loss
// __________________________________________________________

if barstate.islast
    label.delete(currentAnnualPerformance[1])
    currentAnnualPerformance := label.new(bar_index + 1, lowL, style = label.style_label_up, color = hide == "Off" ? color.black : color.new(color.white, 100), textcolor = hide == "Off" ? color.white : color.new(color.white, 100), 
     text = "Current Annual Performance: " +
     str.tostring(runningAnnualPerformance, format.percent) + "\nAverage Annual Performance: " + str.tostring(avgAnnualGainOrLoss, format.percent),
     tooltip = str.tostring(benchmark) + " Current Annual Performance: " + str.tostring(runningAnnualPerformanceSP, format.percent) + 
         " \n(" + str.tostring(runningAnnualComparison, format.percent) + runningAnnualComparisonString + ")" + "\nAverage " + str.tostring(benchmark) + " Annual Performance: " + 
     str.tostring(avgAnnualGainOrLossSP, format.percent) + "\n___________________________\n⬆️Average Performance Calculation Starts on the First Trading Session for the Asset on Your Chart")
    
t = table.new(position.top_right, 1, 1)
if barstate.islast and warn == "No"
    table.cell(t, 0, 0, text = "Hover Over Labels to View the Performance of the Asset on Your Chart Against the Benchmark Asset!", text_size = size.auto, text_color = color.white)


if timeframe.isintraday
    runtime.error("Try Daily Chart")