Raise a Metatrader Alert on Price Movement in a Bar

Based on a request from a subscriber this is a simple indicator to raise an alert if the price moves more than a specified amount above or below the opening price for a bar, but only raise one alert for each direction.

This indicator does not use a plot buffer so you may get a warning message from the compiler in MT5 but you can ignore that.

First capture the movement size in inputs and create a global to store the input points converted to a price

input	int	InpMovementPoints	=	50;	//	Detect movement at points
double	MovementValue;

Inside the OnInit then convert the input points to a price value using the SymbolInfoDouble function to get the size of a point

	//	Convert points to a price value
	MovementValue	=	InpMovementPoints*SymbolInfoDouble(Symbol(), SYMBOL_POINT);

That’s the setup complete. Create a function to display the alert, then the rest will be in the OnCalculate function. I called the alert function AlertMe

void	AlertMe(double targetPrice, double currentPrice, string type, int seconds) {
	//	A simple alert message
	string msg = StringFormat("Current price %f has passed %s target price of %f in $i seconds", currentPrice, type, targetPrice, seconds);

This will just use the Metatrader Alert to display a message when called showing the target price, actual price, high or low being hit and the number of seconds from the bar opening. In very slow times, when there are no ticks for a bar, the number of seconds may be incorrect but that isn’t really the point here, it’s just about raising the alert.

I don’t really like the Metatrader alerts. Once an alert is raised you can’t erase it form the alert window without restarting Metatrader and if you close th alert window it isn’t easy to reopen but this was the request. You might prefer some other alert system.

Now everything is handled in OnCalculate.

Some static values to hold information about the current bar and the upper and lower targets

	//	Some statics to hold the current targets
	static datetime	currentBarTime	=	0;	//	Once only initialisation
	static double		highTarget		=	0;	//	Upper price target
	static double		lowTarget		=	0;	//	Lower price target

When this is first loaded the OnCalculate function can be called multiple times as more bars are loaded and that can cause multiple alerts for bars already closed. This line is a blunt mechanism to ignore the first tick on each bar

	//	Get out if bars are still loading
	if (rates_total!=prev_calculated) return(rates_total);

The first thing to don on each new bar is to reset the target values

	//	On a new bar reset the target values
	if (currentBarTime!=time[0]) {
		currentBarTime	=	time[0];
		highTarget		=	open[0] + MovementValue;
		lowTarget		=	open[0] - MovementValue;

Then just compare the current price to each target to see if the price has moved far enough

	//	Now just check if price has moved past a target, reset target when it does
	//	Note that I'm not using bid and ask here, you may want to substitute
	if (highTarget>0 && close[0]>=highTarget) {
		AlertMe(highTarget, close[0], "High", TimeCurrent()-time[0]);
		highTarget	=	0;
	//	Same in reverse for low target
	if (lowTarget>0 && close[0]<=lowTarget) {
		AlertMe(lowTarget, close[0], "Low", TimeCurrent()-time[0]);
		lowTarget	=	0;

And return


and that’s all there is.

2 thoughts on “Raise a Metatrader Alert on Price Movement in a Bar”

  1. This one is a little complex for a single video, and it has a condition to look for moving averages fanning out which means I have to come up with a computer understandable definition and create the function to confirm it. I’ll look into this but it may be more than one video to cover everything.

Leave a Comment

Scroll to Top