Current bar crossing under low of previous ema crossunder


I would like to be alerted at the low of the 5 minute bar when a stocks price crosses and closes below the 20 period EMA.

Moderator Comments: This request required a bit more explanation. After emailing the author of this question the following additional details where uncovered:

  1. Condition One: price crosses below moving average
  2. Alert Trigger: price crosses below the low of the crossing bar
  3. The closing price of the trigger bar must also close below the moving average
  4. Item 3 requires that we wait until the trigger bar has closed before an alert can be triggered
Marked as spam
Posted by (Questions: 1, Answers: 0)
Asked on March 14, 2023 6:14 pm
Private answer

Since you posted this question in the "Alerts and Notifications" topic of the forum the solution I provide can be used as a Study Alert or as a custom scan. This does not work as a chart study, but considerable modifications can adapt this code to a chart study.

The following solution performs according to the 4 bullet points I added to the question after getting clarifications on the missing details of this request. The alert will trigger once all the conditions have been satisfied and the bar closes (the next bar opens).

input maLengthOne = 20;
input maTypeOne = AverageType.EXPONENTIAL;
input maPriceOne = close;
def maOne = MovingAverage(maTypeOne, maPriceOne, maLengthOne);
def priceCrossBelowAverage = close[1] > maOne[1] and close < maOne;
def triggerAlert = priceCrossBelowAverage[1] and low < low[1] and close < maOne;
plot signal = triggerAlert[1];

Marked as spam
Posted by (Questions: 37, Answers: 3976)
Answered on March 15, 2023 3:44 pm