Penguin Trifecta
 // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © ajbrittle
//@version=5
indicator(title='Penguin Trifecta')
var stoch_sell_trigger = false
var sell_trigger = false
var max_macd = 0.0
stoch_count = 0, macd_count = 0, rsi_count = 0
stoch_trigger = false
macd_trigger = false
rsi_trigger = false
trifecta_color = color.red
smoothK = input.int(14, minval=1), smoothD = input.int(3, minval=1)
k = ta.sma(ta.stoch(close, high, low, smoothK), 3)
d = ta.sma(k, smoothD)
if ta.crossover(k[0], d[0]) or ta.crossover(k[1], d[1]) or ta.crossover(k[2], d[2]) or ta.crossover(k[3], d[3])
    stoch_trigger := true
    stoch_count += 1
else
    stoch_trigger := false
fast = 12, slow = 26
fastMA = ta.ema(close, fast)
slowMA = ta.ema(close, slow)
macd = fastMA - slowMA
signal = ta.sma(macd, 9)
if math.abs(macd[0]) > max_macd
    max_macd := math.abs(macd[0])
// Account for circumstance where MACD crosses at top of peak
// but stochastic is topped out
if (ta.crossover(macd, signal) or (macd > signal)) and k < 65 
    macd_trigger := true
    macd_count += 1
else
    macd_trigger := false
if ta.rsi(close, 14) < 75
    rsi_trigger := true
    rsi_count += 1
else
    rsi_trigger := false
trifecta_series = stoch_count + rsi_count + macd_count
if trifecta_series == 0
    trifecta_color := color.red
else if trifecta_series == 1
    trifecta_color := color.orange
else if trifecta_series == 2
    trifecta_color := color.yellow
else
    trifecta_color := color.green
sell_sum = 0
stoch_sell_trigger := ta.crossunder(k, d) ? true : false
stoch_range = math.max(k[0], k[1], k[2]) - math.min(k[0], k[1], k[2])
stoch_diff = (math.abs(k[0] - k[1]) + math.abs(k[1] - k[2])) / 2
macd_diff = math.abs(macd-signal) / math.abs(signal)
if stoch_sell_trigger
    sell_sum -= 1
if stoch_range < 3
    sell_sum -= 1
if stoch_diff <= 2
    sell_sum -= 1
if macd_diff < .05
    sell_sum -= 1
if stoch_sell_trigger // There has been a crossover or stoch is moving substantially
    trifecta_series := sell_sum
    trifecta_color := color.from_gradient(sell_sum, -4, 0, color.rgb(235, 52, 52), color.rgb(255, 204, 255))
    if sell_sum <= -3
        stoch_sell_trigger := false
        
plot(trifecta_series, title='Penguin Trifecta', style=plot.style_columns, linewidth=9, color=trifecta_color)