// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. // If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/ // © Venture Lab – https://venturelab-bots.pages.dev/pine/ // // EMA cross with alerts. // What it does: plots a fast and a slow EMA, marks the bars where the fast EMA crosses above or below the // slow EMA and offers alerts for both directions. // What it does NOT do: it does not filter false signals in sideways markets, it does not size positions, // it is not a trading system and it is not financial advice. Crosses can appear and disappear while a bar is // still open; with "Confirm on bar close" enabled, marks and alerts only use closed bars. //@version=6 indicator("EMA Cross with Alerts [Venture Lab]", shorttitle = "EMA Cross", overlay = true) // ---- Inputs int fastLen = input.int(9, "Fast EMA length", minval = 1) int slowLen = input.int(21, "Slow EMA length", minval = 2) float src = input.source(close, "Source") bool confirmed = input.bool(true, "Confirm on bar close") // ---- Calculation float fastEma = ta.ema(src, fastLen) float slowEma = ta.ema(src, slowLen) bool rawUp = ta.crossover(fastEma, slowEma) bool rawDown = ta.crossunder(fastEma, slowEma) bool barOk = not confirmed or barstate.isconfirmed bool crossUp = rawUp and barOk bool crossDown = rawDown and barOk // ---- Plots p1 = plot(fastEma, "Fast EMA", color = color.teal, linewidth = 2) p2 = plot(slowEma, "Slow EMA", color = color.orange, linewidth = 2) fill(p1, p2, color = fastEma > slowEma ? color.new(color.teal, 85) : color.new(color.red, 85), title = "EMA zone") plotshape(crossUp, "Cross up", shape.triangleup, location.belowbar, color.teal, size = size.small) plotshape(crossDown, "Cross down", shape.triangledown, location.abovebar, color.red, size = size.small) // ---- Alerts // Option 1: in the alert dialog choose "EMA cross up" or "EMA cross down". alertcondition(crossUp, "EMA cross up", "Fast EMA crossed above slow EMA on {{ticker}} ({{interval}}), close {{close}}") alertcondition(crossDown, "EMA cross down", "Fast EMA crossed below slow EMA on {{ticker}} ({{interval}}), close {{close}}") // Option 2: choose "Any alert() function call" to get one alert with the direction in the message. if rawUp alert("Fast EMA (" + str.tostring(fastLen) + ") crossed above slow EMA (" + str.tostring(slowLen) + ") on " + syminfo.ticker + ", close " + str.tostring(close), alert.freq_once_per_bar_close) if rawDown alert("Fast EMA (" + str.tostring(fastLen) + ") crossed below slow EMA (" + str.tostring(slowLen) + ") on " + syminfo.ticker + ", close " + str.tostring(close), alert.freq_once_per_bar_close)