Z-Score
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © sosacur01

//@version=5
indicator(title="Z-Score", overlay = true)

//-----------------------------------------

//Z-SCORE INPUTS
z_score_signal  = input.bool(title='ON/ Signal Z-Score at threshold (or OFF/ Signal Z-Score at Pullback from threshold)?', defval=false, group = "Z-Score Inputs")
z_score_trigger = input.float(defval=1, group="Z-Score Inputs", title="Z-Score", minval=0, maxval=4, step=0.5, tooltip="Z-Score Signal")
averageType_z    = input.string(defval="SMA", group="Z-Score Inputs", title="MA Type", options=["SMA", "EMA", "WMA", "HMA", "RMA", "SWMA", "ALMA", "VWMA", "VWAP"])
averageLength_z  = input.int(defval=20, group="Z-Score Inputs", title="Length", minval=0)
averageSource_z  = input(close, title="Source", group="Z-Score Inputs")


//MA TYPE
MovAvgType_z(averageType_z, averageSource_z, averageLength_z) =>
	switch str.upper(averageType_z)
        "SMA"  => ta.sma(averageSource_z, averageLength_z)
        "EMA"  => ta.ema(averageSource_z, averageLength_z)
        "WMA"  => ta.wma(averageSource_z, averageLength_z)
        "HMA"  => ta.hma(averageSource_z, averageLength_z)
        "RMA"  => ta.rma(averageSource_z, averageLength_z)
        "SWMA" => ta.swma(averageSource_z)
        "ALMA" => ta.alma(averageSource_z, averageLength_z, 0.85, 6)
        "VWMA" => ta.vwma(averageSource_z, averageLength_z)
        "VWAP" => ta.vwap(averageSource_z)
        => runtime.error("Moving average type '" + averageType_z + 
             "' not found!"), na


//Z-SCORE VALUES
MA       = MovAvgType_z(averageType_z, averageSource_z, averageLength_z)
dev      = ta.stdev(averageSource_z, averageLength_z)
z_score  = (close - MA)/dev 

//Z-SCORE CONDITIONS
long_z  = (z_score_signal ? z_score < - z_score_trigger : ta.crossover(z_score, - z_score_trigger))
short_z = (z_score_signal ? z_score > z_score_trigger : ta.crossunder(z_score, z_score_trigger))

//PLOT-Z-SCORE
bgcolor(short_z ? color.new(color.red, 80) : long_z ? color.new(color.green, 80) : na, title="Z-Score Signal")

//Z-Score Table
var table myTable = table.new(position.bottom_right, 1, 8, border_width = 1)

if barstate.islast
    text1 = "Moving Average Type\n" + str.tostring(averageType_z)
    text2 = "Moving Average Length\n" + str.tostring(averageLength_z)
    text3 = "Moving Average\n" + str.tostring(MA)
    text4 = "Standard Deviation\n" + str.tostring(dev)
    text5 = "Price\n" + str.tostring(close)
    text6 = "Z-score\n" + str.tostring(z_score)
    table.cell(myTable, 0, 1, text=text1, bgcolor = color.black, text_color = color.white) 
    table.cell(myTable, 0, 2, text=text2, bgcolor = color.black, text_color = color.white) 
    table.cell(myTable, 0, 3, text=text3, bgcolor = color.black, text_color = color.white) 
    table.cell(myTable, 0, 4, text=text4, bgcolor = color.black, text_color = color.white) 
    table.cell(myTable, 0, 5, text=text5, bgcolor = color.black, text_color = color.white) 
    table.cell(myTable, 0, 6, text=text6, bgcolor = color.black, text_color = color.white) 





