How to Detect a Crossover of Indicators with MQL4
Camovia Tray Team · 2026-09-11
There's a distinct moment when watching a chart—the fast line slices through the slow line, and you feel it. That subtle shift in momentum, the potential turning point. For traders who rely on technical analysis, detecting indicator crossovers isn't just a technical exercise; it's the core of countless trading strategies. The challenge, however, lies in translating that visual event into reliable, automated code.
The Logic Behind Crossover Detection
At its simplest, a crossover occurs when the relative position of two indicator lines changes from one candle to the next. The logic is straightforward: if indicator A was above indicator B on the previous bar but is now below it, a crossover has occurred.
In MQL4, the key is comparing the current state of your indicators with their state on the preceding bar. Here's the core logic, often used for moving averages but applicable to any two indicator buffers:
// Detect a crossover
if((ma_fast[1] > ma_slow[1] && ma_fast[2] < ma_slow[2]) ||
(ma_fast[1] < ma_slow[1] && ma_fast[2] > ma_slow[2]))
{
// Crossover detected
} This condition checks both directions—whether the fast MA crosses above or below the slow MA . The indices [1] and [2] represent the current and previous completed bars, ensuring the cross is confirmed rather than detected mid-candle.
Practical Implementation: Finding the Last Crossover
To find the most recent crossover, you would loop backward through historical bars. The approach involves retrieving the indicator values for each bar and comparing their relationship to the bar that follows it.
int manyCandles = 1000;
int crossOverCandle = 0;
double crossPrice = 0;
for(int i = 1; i < manyCandles; i++){
double fastMA = iMA(_Symbol, _Period, fastPeriod, 0, MODE_SMA, PRICE_CLOSE, i);
double slowMA = iMA(_Symbol, _Period, slowPeriod, 0, MODE_SMA, PRICE_CLOSE, i);
double fastMA_prev = iMA(_Symbol, _Period, fastPeriod, 0, MODE_SMA, PRICE_CLOSE, i+1);
double slowMA_prev = iMA(_Symbol, _Period, slowPeriod, 0, MODE_SMA, PRICE_CLOSE, i+1);
if((fastMA_prev > slowMA_prev && fastMA < slowMA) ||
(fastMA_prev < slowMA_prev && fastMA > slowMA))
{
crossOverCandle = i;
crossPrice = Open[i];
break; // Found the most recent cross
}
} This method uses the iMA function to fetch moving average values, but the same principle applies to iCustom for other indicators .
The Broader Workflow: From Detection to Decision
Coding a crossover detector is just the first step. The real work involves integrating it into a functional EA, managing trade logic, and—crucially—continually monitoring your positions. This is where many traders find themselves spending more time in front of the terminal than they'd like.
When you're actively trading crossovers, you need to monitor:
- Current bar state: Is a new crossover happening now?
- Open positions: Are they aligned with the latest signal?
- Account overview: What's your current exposure, profit/loss, and margin?
Constantly switching back to the MT4 terminal to check these details disrupts focus. The very act of trading requires attention, and every second spent toggling windows is a second not spent analyzing the next potential setup.
A Better Way to Monitor Your Trades
This is where a tool like Camovia Tray comes into play. It keeps MT4 in the system tray—out of the way but always accessible. You can quickly check live quotes, view open positions, and even close trades directly from the tray icon, all without reopening the full terminal window. For traders running crossover strategies, this means staying on top of your account without constantly alt-tabbing back to the platform. The entire workflow stays on your machine, fast and private .
Wrapping Up
Detecting indicator crossovers with MQL4 boils down to comparing the relative positions of two indicator lines across consecutive bars. Whether you're coding a simple moving average crossover or a more complex multi-indicator system, the logic remains the same. And once your EA is up and running, having a streamlined way to monitor and manage your trades can make a tangible difference in how smoothly your trading day goes.
Turn MT5 / MT4 into a Tray Tool
Check quotes, manage positions, and hide in one click.
Download Camovia TrayFrequently Asked Questions
Are my quotes and positions uploaded anywhere?
No. Quote and position data is read 100% from your local MT5/MT4 terminal and never leaves your computer. See the privacy policy for details.
Which systems and terminals are supported?
Windows 10 / 11, with MetaTrader 5 or MetaTrader 4 (installed and logged in; MT4 needs the bridge EA attached once).
Is the MT4 bridge EA safe? What do I need to enable?
Yes. The bridge EA (CamoviaBridge) is bundled with the app - it only reads quotes/positions locally and executes close commands; no DLLs, no data uploads. Closing positions requires turning on AutoTrading in the MT4 toolbar.
How much does it cost? Is there a free trial?
Subscription pricing starts at $2.49/month (also $6.99/3 months, $13.49/6 months, $23.99/year), all plans with full features. New users can try it free before subscribing.
Popular Symbols Trading Hours
Open/close times, weekend hours & live market status:
Related Articles
How to Handle Drawdown Recovery Without Losing Your Mind (or Your Trades)
Discover how Camovia Tray helps you manage drawdown recovery with local MT5/MT4 data, one-click position closing, and privacy-focused tray tools—no emotional overload.
The Essential MT4 Keyboard Shortcuts List: Trade Faster, Smarter, and with Less Distraction
Master MT4 keyboard shortcuts to trade faster, plus discover how Camovia Tray turns your terminal into a tray tool for quick quotes and position checks—without keeping MT4 on screen.
Why Your MT5 Risk Reward Tool Isn’t Working (And How to Actually Fix Your Workflow)
Simplify risk management by monitoring positions and quotes right from your system tray. Camovia Tray keeps MT5/MT4 data local and always accessible.
MT4 EA Development Cost: 5 Myths That Are Costing You Money
Discover the hidden costs of MT4 EA development and how Camovia Tray reduces monitoring overhead—live quotes, position management, and one-click close from your system tray.
The Commodity Channel Index: A Truly Versatile Indicator for Every Trader's Toolkit
Transform your MT5/MT4 trading with Camovia Tray. Monitor CCI signals, manage positions, and close trades directly from your system tray—all data stays local, no terminal needed.