PSAR Optimization Script
// 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("My script", overlay = true)

pType = input.string(defval = "Cumulative Gain", title = "Plot SAR and Data Based on Highest: ", options = ["Cumulative Gain", "Average Gain"], tooltip = "Cumulative Gain: The Table Will Display SAR Performance Based on Cumulative Gains \n\nAverage Gain: The Table Will Display SAR Performance Based on Average Gains")
style = input.string(defval = "Diamond", title = "SAR Style", options = ["Diamond", "Circles"])

sMB = input.float(0.01, step = 0.01, minval = 0, title = "Benchmark Starting Acceleration Factor", group = "Benchmark SAR", tooltip = "Becnhmark SAR (SAR #1) Starting Acceleration Factor. Adjusting This Parameter Changes the Initial Acceleration Factor Following a Change in SAR (Rising to Falling : Falling to Rising)")
iMB = input.float(0.01, step = 0.01, minval = 0, title = "Benchmark Acceleration Factor Rate of Increase", tooltip = "Benchmark SAR (SAR #1) Acceleration Factor Rate of Increase. Adjusting This Parameter Changes the Rate at Which SAR Increases (When Rising) Or Decreases (When Falling)")
mMB = input.float(0.01, step = 0.01, minval = 0, title = "Benchmark Acceleration Factor Max Value", tooltip = "Benchmark SAR (SAR #1) Acceleration Factor Maximum Value. Adjusting This Parameter Changes the Maximum Rate at Which SAR Will Increase (When Rising) Or Decrease (When Falling)")
sMT = input.float(0.01, step = 0.01, minval = 0, title =  "Test Starting Acceleration Factor", group = "Test SAR", tooltip = "Test SAR (SAR #2) Starting Acceleration Factor. Adjusting This Parameter Changes the Initial Acceleration Factor Following a Change in SAR (Rising to Falling : Falling to Rising)")
iMT = input.float(0.01, step = 0.01, minval = 0, title = "Test Acceleration Factor Rate of Increase", tooltip = "Test SAR (SAR #2) Acceleration Factor Rate of Increase. Adjusting This Parameter Changes the Rate at Which SAR Increases (When Rising) Or Decreases (When Falling) ")
mMT= input.float(0.01, step = 0.01, minval = 0, title = "Test Acceleration Factor Max Value", tooltip = "Test SAR (SAR #2) Acceleration Factor Maximum Value. Adjusting This Parameter Changes the Maximum Rate at Which SAR Will Increase (When Rising) Or Decrease (When Falling)")
ovr = input.string(defval = "Off", title = "Plot SAR Override", options = ["Off", "On"], group = "SAR Override (Visualization Purposes)", tooltip = 'Select "On" to Stop the Script From Plotting the Best Performing SAR. Instead, the Script Will Plot SAR With the Parameters of Your Choice, While Still Displaying the Best Performing SAR Parameters Between Benchmark SAR and Test SAR.')
sMO = input.float(0.01, step = 0.01, minval = 0, title =  "Override Starting Acceleration Factor", tooltip = "Override SAR Starting Acceleration Factor. Adjusting This Parameter Changes the Initial Acceleration Factor Following a Change in SAR (Rising to Falling : Falling to Rising)")
iMO = input.float(0.01, step = 0.01, minval = 0, title = "Override Acceleration Factor Rate of Increase", tooltip = "Override SAR Acceleration Factor Rate of Increase. Adjusting This Parameter Changes the Rate at Which SAR Increases (When Rising) Or Decreases (When Falling)")
mMO= input.float(0.01, step = 0.01, minval = 0, title = "Override Acceleration Factor Max Value", tooltip = "Override SAR Acceleration Factor Maximum Value. Adjusting This Parameter Changes the Maximum Rate at Which SAR Will Increase (When Rising) Or Decrease (When Falling)")

RSB = input.color(#ffeb3c, title = "Rising SAR Benchmark Color", group = "Benchmark SAR Colors")
FSB = input.color(#00ffd4, title = "Falling SAR Benchmark Color")
RST = input.color(#03ff00, title = "Rising SAR Test Color", group = "Test SAR Colors")
FST = input.color(#ff0500, title = "Falling SAR Test Color")

sarB = ta.sar(sMB, iMB, mMB)
sarT = ta.sar(sMT, iMT, mMT)
sarO = ta.sar(sMO, iMO, mMO)

// Calculations Rising SAR

var float x = 0.0
var float x1 = 0.0
var int y = 0
var int y1 = 0
var int y2 = 0
var int y4 = 0
if ta.change(sarB) and close > sarB and y == 0 
    y := 1 
    x := close
    y1 := bar_index
if ta.change(sarB) and close < sarB and y == 1
    y4 += 1
    y := 0
    x1 := ((close / x) - 1) * 100
    x1 := x1[1] + x1
    y2 := bar_index - y1
    y2 := y2[1] + y2
    
sarBP = x1 / y4
sarBC = y2 / y4

var float z = 0.0
var float z1 = 0.0
var int n = 0
var int n1 = 0
var int n2 = 0
var int n4 = 0
if ta.change(sarT) and close > sarT and n == 0 
    n := 1 
    z := close
    n1 := bar_index
if ta.change(sarT) and close < sarT and n == 1
    n4 += 1
    n := 0
    z1 := ((close / z) - 1) * 100
    z1 := z1[1] + z1
    n2 := bar_index - n1
    n2 := n2[1] + n2
    
sarTP = z1 / n4
sarTC = n2 / n4


// Calculations Falling SAR

var float j = 0.0
var float j1 = 0.0
var int u = 0
var int u1 = 0
var int u2 = 0
var int u4 = 0
if ta.change(sarB) and close < sarB and u == 0 
    u := 1 
    j := close
    u1 := bar_index
if ta.change(sarB) and close > sarB and u == 1
    u4 += 1
    u := 0
    j1 := ((close / j) - 1) * 100
    j1 := j1[1] + j1
    u2 := bar_index - u1
    u2 := u2[1] + u2
    
sarBPS = j1 / u4
sarBCS = u2 / u4


var float q = 0.0
var float q1 = 0.0
var int r = 0
var int r1 = 0
var int r2 = 0
var int r4 = 0
if ta.change(sarT) and close < sarT and r == 0 
    r := 1 
    q := close
    r1 := bar_index
if ta.change(sarT) and close > sarT and r == 1
    r4 += 1
    r := 0
    q1 := ((close / q) - 1) * 100
    q1 := q1[1] + q1
    r2 := bar_index - r1
    r2 := r2[1] + r2
    
sarTPS = q1 / r4
sarTCS = r2 / r4


plot(sarTP >= sarBP and pType == "Average Gain" and ovr == "Off" and style == "Diamond" ? sarT : na, style = plot.style_stepline_diamond, color = close > sarT ? 
     RST 
     : 
     FST
     )

plot(sarTP >= sarBP and pType == "Average Gain" and ovr == "Off" and style == "Circles" ? sarT : na, style = plot.style_circles, color = close > sarT ? 
     RST 
     : 
     FST
     )

plot(sarBP > sarTP and pType == "Average Gain" and ovr == "Off" and style == "Diamond" ? sarB : na, style = plot.style_stepline_diamond, color = close > sarB ? 
     RSB 
     : 
     FSB
     )
    
plot(sarBP > sarTP and pType == "Average Gain" and ovr == "Off" and style == "Circles" ? sarB : na, style = plot.style_circles, color = close > sarB ? 
     RSB 
     : 
     FSB
     )
     
plot(z1 >= x1 and pType == "Cumulative Gain" and ovr == "Off" and style == "Diamond" ? sarT : na, style = plot.style_stepline_diamond, color = close > sarT ? 
     RST 
     : 
     FST
     )

plot(z1 >= x1 and pType == "Cumulative Gain" and ovr == "Off" and style == "Circles" ? sarT : na, style = plot.style_circles, color = close > sarT ? 
     RST 
     : 
     FST
     )

plot(x1 > z1 and pType == "Cumulative Gain" and ovr == "Off" and style == "Diamond" ? sarB : na, style = plot.style_stepline_diamond, color = close > sarB ? 
     RSB 
     : 
     FSB
     )

plot(x1 > z1 and pType == "Cumulative Gain" and ovr == "Off" and style == "Circles" ? sarB : na, style = plot.style_circles, color = close > sarB ? 
     RSB 
     : 
     FSB
     )

plot(ovr == "On" and style == "Diamond" ? sarO : na, style = plot.style_stepline_diamond, color = close > sarO ?
     #a9ff8d
     :
     #ff8d8d
     )
 
plot(ovr == "On" and style == "Circles" ? sarO : na, style = plot.style_circles, color = close > sarO ?
     #a9ff8d
     :
     #ff8d8d
     )
 

t = table.new(position.bottom_right, 10, 10)

if pType == "Average Gain" and barstate.islast
    table.cell(
    
     t,
     0,
     0,
     text = "Rising SAR Performance by Average Gain", 
     text_color = color.blue
    
     )
   
    table.cell(
    
     t, 
     0, 
     1, 
     text = sarTP >= sarBP and sarBP >= 0.0 ? 
     "Test SAR Rising: " + str.tostring(sarTP, format.percent) +" \nAvg. # Of Candles Test SAR Remains Rising: " + str.tostring(sarTC, "#") + "\n\n" + 
     "Benchmark SAR Rising: " + str.tostring(sarBP, format.percent) +" \nAvg. # Of Candles Benchmark SAR Remains Rising: " + str.tostring(sarBC, "#") + "\n\n" 
     : 
     sarBP > sarTP and sarTP >= 0.0 ?
     "Benchmark SAR Rising: " + str.tostring(sarBP, format.percent)+" \nAvg. # Of Candles Benchmark SAR Remains Rising: " + str.tostring(sarBC, "#") + "\n\n" +
     "Test SAR Rising: " + str.tostring(sarTP, format.percent)  +" \nAvg. # Of Candles Test SAR Remains Rising: " + str.tostring(sarTC, "#") + "\n\n" 
     :
     sarTP >= sarBP and sarTP >= 0.0 and sarBP < 0.0 ?
     "Test SAR Rising: " + str.tostring(sarTP, format.percent) +" \nAvg. # Of Candles Test SAR Remains Rising: " + str.tostring(sarTC, "#") + "\n\n" +
     "Benchmark SAR Rising: " + str.tostring(sarBP, format.percent)+" \nAvg. # Of Candles Benchmark SAR Remains Rising: " + str.tostring(sarBC, "#") + "\n(Long Position Loss!)" 
     :
     sarTP >= sarBP and sarTP < 0.0 ?
     "Test SAR Rising: " + str.tostring(sarTP, format.percent)  +" \nAvg. # Of Candles Test SAR Remains Rising: " + str.tostring(sarTC, "#") + "\n(Long Position Loss!)\n\n" +
     "Benchmark SAR Rising: " + str.tostring(sarBP, format.percent)+" \nAvg. # Of Candles Benchmark SAR Remains Rising: " + str.tostring(sarBC, "#") + "\n(Long Position Loss!)" 
     :
     sarBP > sarTP and sarTP < 0.0 and sarBP >= 0.0 ?
     "Benchmark SAR Rising: " + str.tostring(sarBP, format.percent)+" \nAvg. # Of Candles Benchmark SAR Remains Rising: " + str.tostring(sarBC, "#") + "\n\n" +
     "Test SAR Rising: " + str.tostring(sarTP, format.percent) +" \nAvg. # Of Candles Test SAR Remains Rising: " + str.tostring(sarTC, "#") + "\n(Long Position Loss!)" 
     :
     sarBP > sarTP and sarBP < 0.0 ?
     "Benchmark SAR Rising: " + str.tostring(sarBP, format.percent) +" \nAvg. # Of Candles Benchmark SAR Remains Rising: " + str.tostring(sarBC, "#") + "\n(Long Position Loss!)\n\n" +
     "Test SAR Rising: " + str.tostring(sarTP, format.percent) +" \nAvg. # Of Candles Test SAR Remains Rising: " + str.tostring(sarTC, "#") + "\n(Long Position Loss!)" 
     :
     na,
     text_color = color.white,
     bgcolor = sarTP >= sarBP ? 
     color.new(RST, 75)
     :
     color.new(RSB, 75)
     
     )

    table.cell(
    
     t, 
     0, 
     2, 
     text = "\n\nFalling SAR Performance by Average Downside Gain",
     text_color = color.blue
     
     )
     
    table.cell(
    
     t, 
     0, 
     3, 
     text = sarTPS <= sarBPS and sarBPS <= 0.0 ? 
     "Test SAR Falling: " + str.tostring(sarTPS, format.percent) + " \nAvg. # Of Candles Test SAR Remains Falling: " + str.tostring(sarTCS, "#") + "\n\n" + 
     "Benchmark SAR Falling: " + str.tostring(sarBPS, format.percent) + "\nAvg. # Of Candles Benchmark SAR Remains Falling: " + str.tostring(sarBCS, "#") 
     : 
     sarBPS < sarTPS and sarTPS <= 0.0 ?
     "Benchmark SAR Falling: " + str.tostring(sarBPS, format.percent) + "\nAvg. # Of Candles Benchmark SAR Remains Falling: " + str.tostring(sarBCS, "#") + "\n\n" + 
     "Test SAR Falling: " + str.tostring(sarTPS, format.percent)  + " \nAvg. # Of Candles Test SAR Remains Falling: " + str.tostring(sarTCS, "#") 
     :
     sarTPS <= sarBPS and sarTPS <= 0.0 and sarBPS > 0.0 ?
     "Test SAR Falling: " + str.tostring(sarTPS, format.percent) + " \nAvg. # Of Candles Test SAR Remains Falling: " + str.tostring(sarTCS, "#") + "\n\n" +  
     "Benchmark SAR Falling: " + str.tostring(sarBPS, format.percent) + "\nAvg. # Of Candles Benchmark SAR Remains Falling: " + str.tostring(sarBCS, "#") + "\n(Short Position Loss!)" 
     :
     sarTPS <= sarBPS and sarTPS > 0.0 ?
     "Test SAR Falling: " + str.tostring(sarTPS, format.percent)  + " \nAvg. # Of Candles Test SAR Remains Falling: " + str.tostring(sarTCS, "#") + "\n(Short Position Loss!)\n\n" + 
     "Benchmark SAR Falling: " + str.tostring(sarBPS, format.percent) + "\nAvg. # Of Candles Benchmark SAR Remains Falling: " + str.tostring(sarBCS, "#") + "\n(Short Position Loss!)"
     :
     sarBPS < sarTPS and sarTPS > 0.0 and sarBPS <= 0.0 ?
     "Benchmark SAR Falling: " + str.tostring(sarBPS, format.percent)+ "\nAvg. # Of Candles Benchmark SAR Remains Falling: " + str.tostring(sarBCS, "#") + "\n\n" + 
     "Test SAR Falling: " + str.tostring(sarTPS, format.percent) + " \nAvg. # Of Candles Test SAR Remains Falling: " + str.tostring(sarTCS, "#") + "\n(Short Position Loss!)" 
     :
     sarBPS < sarTPS and sarBPS > 0.0 ?
     "Benchmark SAR Falling: " + str.tostring(sarBPS, format.percent)+ "\nAvg. # Of Candles Benchmark SAR Remains Falling: " + str.tostring(sarBCS, "#") + "\n(Short Position Loss!)\n\n" + 
     "Test SAR Falling: " + str.tostring(sarTPS, format.percent) + " \nAvg. # Of Candles Test SAR Remains Falling: " + str.tostring(sarTCS, "#") + "\n(Short Position Loss!)"
     :
     na,
     text_color = color.white,
     bgcolor = sarTPS <= sarBPS ? 
     color.new(FST, 75)
     :
     color.new(FSB, 75)
     
     )

else if pType == "Cumulative Gain" and barstate.islast
    table.cell(
    
     t,
     0,
     0,
     text = "Rising SAR Performance by Cumulative Gain", 
     text_color = color.orange
    
     )
   
    table.cell(
    
     t, 
     0, 
     1, 
     text = z1 >= x1 and x1 >= 0.0 ? 
     "Test SAR Rising: " + str.tostring(z1, format.percent) +" \nAvg. # Of Candles Test SAR Remains Rising: " + str.tostring(sarTC, "#") + "\n\n" + 
     "Benchmark SAR Rising: " + str.tostring(x1, format.percent) +" \nAvg. # Of Candles Benchmark SAR Remains Rising: " + str.tostring(sarBC, "#") + "\n\n" 
     : 
     x1 > z1 and z1 >= 0.0 ?
     "Benchmark SAR Rising: " + str.tostring(x1, format.percent)+" \nAvg. # Of Candles Benchmark SAR Remains Rising: " + str.tostring(sarBC, "#") + "\n\n" +
     "Test SAR Rising: " + str.tostring(z1, format.percent)  +" \nAvg. # Of Candles Test SAR Remains Rising: " + str.tostring(sarTC, "#") + "\n\n" 
     :
     z1 >= x1 and z1 >= 0.0 and x1 < 0.0 ?
     "Test SAR Rising: " + str.tostring(z1, format.percent) +" \nAvg. # Of Candles Test SAR Remains Rising: " + str.tostring(sarTC, "#") + "\n\n" +
     "Benchmark SAR Rising: " + str.tostring(x1, format.percent)+" \nAvg. # Of Candles Benchmark SAR Remains Rising: " + str.tostring(sarBC, "#") + "\n(Long Position Loss!)" 
     :
     z1 >= x1 and z1 < 0.0 ?
     "Test SAR Rising: " + str.tostring(z1, format.percent)  +" \nAvg. # Of Candles Test SAR Remains Rising: " + str.tostring(sarTC, "#") + "\n(Long Position Loss!)\n\n" +
     "Benchmark SAR Rising: " + str.tostring(x1, format.percent)+" \nAvg. # Of Candles Benchmark SAR Remains Rising: " + str.tostring(sarBC, "#") + "\n(Long Position Loss!)" 
     :
     x1 > z1 and z1 < 0.0 and x1 >= 0.0 ?
     "Benchmark SAR Rising: " + str.tostring(x1, format.percent)+" \nAvg. # Of Candles Benchmark SAR Remains Rising: " + str.tostring(sarBC, "#") + "\n\n" +
     "Test SAR Rising: " + str.tostring(z1, format.percent) +" \nAvg. # Of Candles Test SAR Remains Rising: " + str.tostring(sarTC, "#") + "\n(Long Position Loss!)" 
     :
     x1 > z1 and x1 < 0.0 ?
     "Benchmark SAR Rising: " + str.tostring(x1, format.percent) +" \nAvg. # Of Candles Benchmark SAR Remains Rising: " + str.tostring(sarBC, "#") + "\n(Long Position Loss!)\n\n" +
     "Test SAR Rising: " + str.tostring(z1, format.percent) +" \nAvg. # Of Candles Test SAR Remains Rising: " + str.tostring(sarTC, "#") + "\n(Long Position Loss!)" 
     :
     "Test SAR Rising: " + str.tostring(z1, format.percent) +" \nAvg. # Of Candles Test SAR Remains Rising: " + str.tostring(sarTC, "#") + "\n\n" + 
     "Benchmark SAR Rising: " + str.tostring(x1, format.percent) +" \nAvg. # Of Candles Benchmark SAR Remains Rising: " + str.tostring(sarBC, "#") + "\n\n" ,
     text_color = color.white,
     bgcolor = x1 >= z1 ? 
     color.new(RST, 75)
     :
     color.new(RSB, 75)
     
     )

    table.cell(
    
     t, 
     0, 
     2, 
     text = "\n\nFalling SAR Performance by Cumulative Downside Gain",
     text_color = color.orange
     
     )
     
    table.cell(
    
     t, 
     0, 
     3, 
     text = q1 <= j1 and j1 <= 0.0 ? 
     "Test SAR Falling: " + str.tostring(q1, format.percent) + " \nAvg. # Of Candles Test SAR Remains Falling: " + str.tostring(sarTCS, "#") + "\n\n" + 
     "Benchmark SAR Falling: " + str.tostring(j1, format.percent) + "\nAvg. # Of Candles Benchmark SAR Remains Falling: " + str.tostring(sarBCS, "#") 
     : 
     j1 < q1 and q1 <= 0.0 ?
     "Benchmark SAR Falling: " + str.tostring(j1, format.percent) + "\nAvg. # Of Candles Benchmark SAR Remains Falling: " + str.tostring(sarBCS, "#") + "\n\n" + 
     "Test SAR Falling: " + str.tostring(q1, format.percent)  + " \nAvg. # Of Candles Test SAR Remains Falling: " + str.tostring(sarTCS, "#") 
     :
     q1 <= j1 and q1 <= 0.0 and j1 > 0.0 ?
     "Test SAR Falling: " + str.tostring(q1, format.percent) + " \nAvg. # Of Candles Test SAR Remains Falling: " + str.tostring(sarTCS, "#") + "\n\n" +  
     "Benchmark SAR Falling: " + str.tostring(j1, format.percent) + "\nAvg. # Of Candles Benchmark SAR Remains Falling: " + str.tostring(sarBCS, "#") + "\n(Short Position Loss!)" 
     :
     q1 <= j1 and q1 > 0.0 ?
     "Test SAR Falling: " + str.tostring(q1, format.percent)  + " \nAvg. # Of Candles Test SAR Remains Falling: " + str.tostring(sarTCS, "#") + "\n(Short Position Loss!)\n\n" + 
     "Benchmark SAR Falling: " + str.tostring(j1, format.percent) + "\nAvg. # Of Candles Benchmark SAR Remains Falling: " + str.tostring(sarBCS, "#") + "\n(Short Position Loss!)"
     :
     j1 < q1 and q1 > 0.0 and j1 <= 0.0 ?
     "Benchmark SAR Falling: " + str.tostring(j1, format.percent)+ "\nAvg. # Of Candles Benchmark SAR Remains Falling: " + str.tostring(sarBCS, "#") + "\n\n" + 
     "Test SAR Falling: " + str.tostring(q1, format.percent) + " \nAvg. # Of Candles Test SAR Remains Falling: " + str.tostring(sarTCS, "#") + "\n(Short Position Loss!)" 
     :
     j1 < q1 and j1 > 0.0 ?
     "Benchmark SAR Falling: " + str.tostring(j1, format.percent)+ "\nAvg. # Of Candles Benchmark SAR Remains Falling: " + str.tostring(sarBCS, "#") + "\n(Short Position Loss!)\n\n" + 
     "Test SAR Falling: " + str.tostring(q1, format.percent) + " \nAvg. # Of Candles Test SAR Remains Falling: " + str.tostring(sarTCS, "#") + "\n(Short Position Loss!)"
     :
     "Test SAR Falling: " + str.tostring(q1, format.percent) + " \nAvg. # Of Candles Test SAR Remains Falling: " + str.tostring(sarTCS, "#") + "\n\n" + 
     "Benchmark SAR Falling: " + str.tostring(j1, format.percent) + "\nAvg. # Of Candles Benchmark SAR Remains Falling: " + str.tostring(sarBCS, "#"),
     text_color = color.white,
     bgcolor = q1 <= j1 ? 
     color.new(FST, 75)
     :
     color.new(FSB, 75)
     
     )

instructions = input.string(group = "Instructions", title = "Indicator Instructions", defval = "Off", options = ["Off", "On"])

if instructions == "On" and barstate.islast
    
    table.cell(
     t,
     0,
     4,
     text = "This Script Calculates the Cumulative and Average Gain/Loss \nof Rising SAR Following a Price Crossover of SAR. \n\nThe Cumulative and Average Gain/Loss of \nFalling SAR Following a Price Crossunder Is Also Measured. \n\nChanges to the Parameters of SAR Will \nReturn the Requisite Calculations for Evaluating Performance. \n\nBenchmark SAR Can Be Used to \nCompare Performance Against Test SAR",
     text_color = color.white
    
     )
     
     
plotchar(close < sarO[1] and close > sarO, location = location.abovebar, color = color.blue, text = "Long", char = "⬇️", size = size.tiny)
plotchar(close > sarO[1] and close < sarO, location = location.belowbar, color = color.orange, text = "Short", char = "⬆️", size = size.tiny)