Heikin-Ashi Trend Alert
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © backslash-f
//
// This script:
// - Adds a Heikin-Ashi line to the chart (EMA-based).
// - Provides alerts triggered when the color goes from green to red and vice versa.

//@version=5
indicator("Heikin-Ashi Trend Alert", overlay=true)

// EMA
ema_input                   = input.int(title="EMA", defval=77, minval=1)
ema_smoothing               = input.int(title="EMA Smoothing", defval=21, minval=1)
ema_open                    = ta.ema(open, ema_input)
ema_close                   = ta.ema(close, ema_input)

// Developer
shouldShowDebugInfo         = input(title="Show debug info", group="DEVELOPER", defval=false, tooltip="Check this box to see the values of the main variables on the chart, below bars. This is for debugging purposes only.")

// Heikin-Ashi
heikinashi                  = ticker.heikinashi(syminfo.tickerid)
heikinashi_open             = request.security(heikinashi, timeframe.period, ema_open)
heikinashi_close            = request.security(heikinashi, timeframe.period, ema_close)
heikinashi_open_smooth      = ta.ema(heikinashi_open, ema_smoothing)
heikinashi_close_smooth     = ta.ema(heikinashi_close, ema_smoothing)

// Trend
var trend                   = false
var last_trend              = trend
trend                       := heikinashi_close_smooth >= heikinashi_open_smooth

// Color
color                       = trend ? color.green : color.red
trend_color                 = color.new(color, 50)

// Alert
varip alert_count           = 0
alert_message_prefix        = '[' + syminfo.ticker + ', ' + timeframe.period + ']' // e.g. "[BTC, 15]"
alert_message_body          = " Heikin-Ashi new color: "
alert_message_sufix         = trend ? "🟢" : "🔴"
alert_message               = alert_message_prefix + alert_message_body + alert_message_sufix
if (trend != last_trend)
    alert_count             := alert_count + 1
    last_trend              := trend
    alert(alert_message, alert.freq_once_per_bar)

// Debugging label creation
var debuggingLabel          = label.new(na, na, color=color.new(color.blue, transp=100), textcolor=color.white, textalign=text.align_right, style=label.style_label_up, yloc=yloc.belowbar)
if shouldShowDebugInfo
    current_color_string    = "Current color: " + alert_message_sufix
    last_color_string       = "Last color: " + (last_trend ? "🟢" : "🔴")
    alert_count_string      = "Triggered alerts: " + str.tostring(alert_count)
    label.set_text(debuggingLabel, current_color_string + '\n' + last_color_string + '\n' + alert_count_string)
    label.set_color(debuggingLabel, color=color.new(color.blue, transp=0))
    label.set_x(debuggingLabel, last_bar_index)
    label.set_y(debuggingLabel, close)

// Lines
open_line               = plot(heikinashi_open_smooth, color=trend_color, title="Open Line")
close_line              = plot(heikinashi_close_smooth, color=trend_color, title="Close Line")
fill(open_line, close_line, color=trend_color, title="Heikin-Ashi Line")