Change alert on bar close to alert in real time


Category:
0
0

Hello Pete,

I have a chart study that alerts me when the price crosses above or below a Moving Average.  Problem is is that triggers the alert at bar close/open and not in real time.  Using 1 hour bars, the alert is not very helpful with the delay.  Any help to make it alert as the crossover occurs would be helpful.

Thank you

Here is the code:

def ema = ExpAverage(close, 200);
def crossbelow = low crosses below MovAvgExponential(“length” = 200);
def crossabove = high crosses above MovAvgExponential(“length” = 200);

plot signalHigh = crossabove[0];
signalHigh.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
signalHigh.SetDefaultColor(Color.MAGENTA);
plot signalLow = crossbelow[0];
signalLow.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
signalLow.SetDefaultColor(Color.CYAN);
Alert(signalHigh[1], “############# 200 EMA touch Alert ##############”, Alert.BAR, Sound.RING);
Alert(signalLow[1], “############# 200 EMA touch Alert ##############”, Alert.BAR, Sound.RING);

Marked as spam
Posted by (Questions: 1, Answers: 1)
Asked on August 28, 2023 7:41 am
105 views
0
Private answer

I think you are going to be shocked to see how small of an adjustment is required to make this adjustment to your code. The study you are using was intentionally designed to wait until the bar closed before triggering the alert. In most cases, this is a feature which helps prevent being alerted to signals until they have fully locked in.

As you apply this modification, be very careful to check for signals which trigger the alert, but the crossing signal vanishes before the bar closes. This would be a very rare occurrence for your particular signal, but it is possible.

The Modification:

Here are the original alert lines from your chart study script:

Alert(signalHigh[1], “############# 200 EMA touch Alert ##############”, Alert.BAR, Sound.RING);
Alert(signalLow[1], “############# 200 EMA touch Alert ##############”, Alert.BAR, Sound.RING);

And here are those same two lines after the modification has been applied:

Alert(signalHigh, “############# 200 EMA touch Alert ##############”, Alert.BAR, Sound.RING);
Alert(signalLow, “############# 200 EMA touch Alert ##############”, Alert.BAR, Sound.RING);

Take a few minutes to examine the before and after. The changes are very subtle, and easy to miss.

But that's it. Just replace those two lines of code and the alerts will fire off at the moment the signal appears on the chart. But watch for cases where the signal vanishes before the bar closes.

Marked as spam
Posted by (Questions: 37, Answers: 4118)
Answered on August 28, 2023 8:08 am