Camovia Tray™

Blog · Knowledge

How to Structure an Expert Advisor: From Fragile Scripts to an Architecture That Survives

Camovia Tray Team · 2026-09-17

You've just written what looks like a fantastic trading strategy. The logic is sound, the entry conditions are clear, and you have a solid set of rules for exits. You sit down in front of MetaEditor, excited to bring your idea to life.

The problem is, you're staring at a blank MQL5 file, unsure where to even begin.

This is where the vast majority of trading robots fail. Not because of a bad strategy or a coding error, but because they're built as a single, monolithic piece of code. They have an entry signal and little else: no structured risk layer, no defense against their own worst day, and no awareness of the rules they have to survive under . They might look impressive in a backtest, but the first time market conditions turn volatile, the whole system collapses.

This is the fate of a "clever idea." But a durable Expert Advisor is an engineered system. Let's break down how to build one.

The 4 Core Blocks of Every EA

Regardless of complexity, every Expert Advisor is built on the same foundational pillars: initialization, the main loop, and de-initialization . These correspond to three essential functions:

  • OnInit(): This is the "setup" phase, run once when you attach the EA to a chart. It's where you create your objects, load data, set initial parameters, and prepare everything your EA needs to operate .
  • OnTick(): This is the engine of your EA. It's called every time a new price quote (a tick) arrives. Most of your strategy logic—checking for entry signals, managing trailing stops, and controlling risk—will live here .
  • OnDeinit(): This is the "cleanup" phase, triggered when you remove the EA from a chart or shut down the terminal. Its job is to delete graphical objects, release allocated memory, and reset the chart to its original state .

While this is a complete EA, it is a fragile one. A well-structured EA follows a layered approach.

The 5-Layer Model: Architecture Over Code

A professional EA is built on a layered model where each layer has a specific responsibility and only communicates with the layers directly above and below it. This separation makes your code more robust, testable, and far easier to modify or fix later .

Imagine constructing your EA like this from the ground up:

  • Layer 0: Core Architecture – This is the foundation. It dictates the file structure, includes all necessary .mqh libraries, and defines the main framework. Before you write a single line of strategy logic, get this right. A poorly structured EA cannot be made reliable by clever strategy logic .
  • Layer 1: Risk Engine – This is your defense against your own worst day. This layer handles position sizing, maximum drawdown controls, and profit protection. It doesn't care what trade is being taken, only how much risk it represents .
  • Layer 2: Market Regime Filter – This layer is the gatekeeper. It analyzes the current market state—trending, ranging, high volatility—to decide if the conditions are suitable for your strategy. It prevents your EA from trading when the environment is unfavorable .
  • Layer 3: Signal Engine – This is where your trading idea lives. It generates the entry and exit signals based on your indicators and price action rules. By isolating this layer, you can swap strategies in and out without touching the rest of the system .
  • Layer 4: Trade Management – This layer is your execution manager. It's responsible for placing orders, setting stop losses and take profits, and managing open positions like trailing stops .
  • Layer 5: Analytics & Journal – This is the eyes and ears of your EA. It logs every action, error, and performance metric. When a trade fails or the system does something unexpected, this is the layer that tells you exactly what happened and why .

The Rule of Includes

Once you have your layers planned, you need to structure your code files. The standard practice is to split your EA's logic into separate include files (.mqh) and import them into your main EA_Name.mq5 file .

Your main EA file becomes a clean orchestration layer. It should contain only the OnInit(), OnDeinit(), and OnTick() functions, which then call out to your individual modules.

#include <CoreEngine.mqh>
#include <RiskManager.mqh>
#include <SignalEngine.mqh>
#include <TradeManager.mqh>

This modular approach means you can test, fix, or upgrade one part of your EA without breaking the entire system.

When Your EA Works Too Well (and Too Closely)

You've taken the time to structure your EA properly. You've architected the layers, written clean includes, and diligently tested it. Now it's running smoothly on your MetaTrader platform, executing trades based on your well-defined rules.

The challenge, however, is often not the automation itself, but the process of monitoring and trusting it.

Once you close your laptop, how do you confidently know your EA is operating as intended? How do you quickly check that a position is open or a new signal has been generated without opening the entire MT5 terminal and potentially interrupting its operation? This is where the need for a streamlined monitoring solution arises.

A tool like Camovia Tray is designed for this precise scenario. It provides a lightweight, non-intrusive way to keep an eye on your automated system [citation:knowledge]. By turning your MT5 into a system tray tool, you can:

  • Check live quotes and open positions without restoring the main MT5 window and potentially interfering with the EA's workflow. A quick hover over the tray icon reveals your current exposure [citation:knowledge].
  • Manage your positions directly from the system tray, including a simple two-step confirmation process for closing trades if you need to intervene manually [citation:knowledge].
  • Keep your data private, as the information is read directly from your local terminal and never leaves your computer [citation:knowledge].

The goal of structuring an EA is to build a robust, reliable, and fire-and-forget system. Tools like Camovia Tray fit into that workflow not by coding strategies, but by giving you the peace of mind to monitor your work effectively from a safe distance.

MT4/MT5 Tray Assistant

Silent tracking, one-click close - check quotes and manage positions right from the tray.

Download Camovia Tray

Frequently Asked Questions

Can the tray icon be disguised?

Yes. The tray icon can disguise itself as a cloud drive or a common system tool, so market watching stays low-key.

What is the tray lock feature?

Lock the tray function instantly at key moments to protect your privacy and prevent private information from leaking.

What information can I monitor?

Quotes and open positions: symbol, direction, open time, current P&L, and more - all visible in the popup positions tab. Click an order to close it.

Which languages are supported?

Chinese and English, switchable on both the website and the client.

Popular Symbols Trading Hours

Open/close times, weekend hours & live market status: