SuperTrend - Custom Screener and Dynamic Alerts
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © ralagh

//@version=4

strategy("Supertrend", overlay = true, format=format.price, precision=2)

alert_strings = array.new_string(2)
array.set(alert_strings, 0, "")
array.set(alert_strings, 1, "")

table_row_count = array.new_int(5)
array.set(table_row_count, 0, 1)
array.set(table_row_count, 1, 1)
array.set(table_row_count, 2, 1)
array.set(table_row_count, 3, 1)
array.set(table_row_count, 4, 1)

var UpTabl = table.new(position = position.top_center, columns = 5, rows = 20, bgcolor = color.rgb(255,255,255), border_width = 2, frame_color= color.black, frame_width = 2)
var DnTabl = table.new(position = position.bottom_center, columns = 5, rows = 20, bgcolor = color.rgb(255,255,255), border_width = 2, frame_color= color.black, frame_width = 2)

table.cell(table_id = UpTabl, column = 1, row = 0, text = "     SuperTrend Buy")
table.cell(table_id = DnTabl, column = 1, row = 0, text = "     SuperTrend Sell") 
table.cell(table_id = UpTabl, column = 2, row = 0, text = "     ▲▲")
table.cell(table_id = DnTabl, column = 2, row = 0, text = "     ▼▼") 

table.cell(table_id = UpTabl, column = 0, row = 1, text = "Ticker")
table.cell(table_id = UpTabl, column = 1, row = 1, text = "Close")
table.cell(table_id = UpTabl, column = 2, row = 1, text = "DHigh") 
table.cell(table_id = UpTabl, column = 3, row = 1, text = "DLow") 
table.cell(table_id = UpTabl, column = 4, row = 1, text = "% Change") 

table.cell(table_id = DnTabl, column = 0, row = 1, text = "Ticker")
table.cell(table_id = DnTabl, column = 1, row = 1, text = "Close") 
table.cell(table_id = DnTabl, column = 2, row = 1, text = "DHigh") 
table.cell(table_id = DnTabl, column = 3, row = 1, text = "DLow") 
table.cell(table_id = DnTabl, column = 4, row = 1, text = "% Change")

mult = input(type=input.float, defval=3)
period = input(type=input.integer, defval=10)
[superTrend, dir] = supertrend(mult, period)
colResistance = dir == 1 and dir == dir[1] ? color.new(color.red, 0) : color.new(color.red, 100)
colSupport = dir == -1 and dir == dir[1] ? color.new(color.green, 0) : color.new(color.green, 100)
plot(superTrend, color = colResistance, linewidth=2)
plot(superTrend, color = colSupport, linewidth=2)

if (dir == -1 and dir == dir[1])
	strategy.entry("Buy", strategy.long, comment="Buy")

if (dir == 1 and dir == dir[1])
	strategy.entry("Sell", strategy.short, comment="Sell")

f_SupCro(_ticker)=>
    
    
    p_chg = (close - close[1])/close[1] * 100
    
    [cl_dp, hi_dp, lo_dp, chg_dp] = security(_ticker, 'D', [close, high, low, p_chg], lookahead = barmerge.lookahead_on)
    
    [cl_p, ST_R] = security(_ticker, timeframe.period, [close, superTrend], lookahead = barmerge.lookahead_on)
    _msg = _ticker + "~Cls-" + tostring(round(cl_p * 100) / 100) + "/" + tostring(round(chg_dp * 100) / 100) + "% ,"
    
//cl_p > ST_R  (Uptrend)
//cl_p < ST_R  (Downtrend)

    if crossover(cl_p, ST_R)   
        _msg := _msg + "PHi~" + tostring(round(hi_dp * 100) / 100) + ", PLo~" + tostring(round(lo_dp * 100) / 100) + "-ST▲"
        array.set(alert_strings, 0, array.get(alert_strings, 0) + "\n" + _msg)
        array.set(table_row_count, 0, array.get(table_row_count, 0) + 1)
        //alert(_msg + "-Sup▲", alert.freq_once_per_bar_close ) // For Individual Stock Alerts
        
        if barstate.islast
            table.cell(table_id = UpTabl, column = 0, row = array.get(table_row_count, 0), text = _ticker)
            table.cell(table_id = UpTabl, column = 1, row = array.get(table_row_count, 0), text = tostring(round(cl_p * 100) / 100), bgcolor=color.rgb(0, 200, 100))
            table.cell(table_id = UpTabl, column = 2, row = array.get(table_row_count, 0), text = tostring(round(hi_dp * 100) / 100), bgcolor=color.rgb(0, 200, 100))
            table.cell(table_id = UpTabl, column = 3, row = array.get(table_row_count, 0), text = tostring(round(lo_dp * 100) / 100), bgcolor=color.rgb(0, 200, 100))
            table.cell(table_id = UpTabl, column = 4, row = array.get(table_row_count, 0), text = tostring(round(chg_dp * 100) / 100), bgcolor=color.rgb(0, 200, 100))
            
    else if crossunder(cl_p, ST_R)
        _msg := _msg + "PHi~" + tostring(round(hi_dp * 100) / 100) + ", PLo~" + tostring(round(lo_dp * 100) / 100) + "-ST▼"
        array.set(table_row_count, 1, array.get(table_row_count, 1) + 1)
        array.set(alert_strings, 1, array.get(alert_strings, 1) + "\n" + _msg)
        //alert(_msg + "-Sup▼", alert.freq_once_per_bar_close) // For Individual Stock Alerts

        if barstate.islast
            table.cell(table_id = DnTabl, column = 0, row = array.get(table_row_count, 1), text = _ticker)
            table.cell(table_id = DnTabl, column = 1, row = array.get(table_row_count, 1), text = tostring(round(cl_p * 100) / 100), bgcolor=color.rgb(255, 0, 0))
            table.cell(table_id = DnTabl, column = 2, row = array.get(table_row_count, 1), text = tostring(round(hi_dp * 100) / 100), bgcolor=color.rgb(255, 0, 0))
            table.cell(table_id = DnTabl, column = 3, row = array.get(table_row_count, 1), text = tostring(round(lo_dp * 100) / 100), bgcolor=color.rgb(255, 0, 0))
            table.cell(table_id = DnTabl, column = 4, row = array.get(table_row_count, 1), text = tostring(round(chg_dp * 100) / 100), bgcolor=color.rgb(255, 0, 0))
            
//plot(close, "Close", color=#8E1599)

f_SupCro(syminfo.tickerid)
f_SupCro(input('DJI', title=input.symbol)) 
f_SupCro(input('IXIC', title=input.symbol)) 
f_SupCro(input('SPX', title=input.symbol)) 
f_SupCro(input('RUT', title=input.symbol)) 
f_SupCro(input('AAPL', title=input.symbol))
f_SupCro(input('AMZN', title=input.symbol)) 
f_SupCro(input('FB', title=input.symbol)) 
f_SupCro(input('GOOG', title=input.symbol)) 
f_SupCro(input('NFLX', title=input.symbol))
f_SupCro(input('MSFT', title=input.symbol)) 
f_SupCro(input('NVDA', title=input.symbol)) 
f_SupCro(input('FDX', title=input.symbol)) 
f_SupCro(input('GLD', title=input.symbol))


if array.get(alert_strings, 0) != ""
    alert(array.get(alert_strings, 0),  alert.freq_once_per_bar_close)

if array.get(alert_strings, 1) != ""
    alert(array.get(alert_strings, 1),  alert.freq_once_per_bar_close)
