Download and TEST https://www.mql5.com/en/market/product/196990
XauUsd Nostradamus M5 is a specialized automated trading Expert Advisor (EA) for MetaTrader 5, designed for algorithmic trading of XAUUSD (Gold) on the M5 timeframe.
The Expert Advisor is based on an event-driven processing model: the primary trading decision is made after a new bar is formed, using already closed price data. Open positions are managed separately — the dynamic Stop Loss is recalculated on every tick.
This separation allows signal generation to be handled independently from the management of an already open position.
Trading Algorithm Architecture
The operation of the Expert Advisor can be conceptually divided into several functional layers:
-
New bar detection
-
Price structure analysis
-
Trading state determination
-
Trading state reversal signal generation
-
Trading condition validation
-
Position opening or closing
-
Dynamic position management
The primary trading signal is not generated from an unfinished candle. When a new bar appears, the Expert Advisor analyzes the latest fully formed bar and uses closed price data to determine the current market state.
This prevents the current unfinished bar from being used as the primary source of the trading signal.
Trading State Model
The internal trading logic represents the market using two states:
-
BUY state — the market is in an upward trading state;
-
SELL state — the market is in a downward trading state.
The Expert Advisor does not open a new position on every bar. A trading event occurs when a confirmed state transition takes place.
The primary transitions are:
SELL → BUY
When the market transitions from SELL state to BUY state, the Expert Advisor:
-
checks for an existing Sell position;
-
attempts to close the opposite position;
-
verifies the result of the closing operation;
-
performs technical checks for the new order;
-
sends a Buy order.
BUY → SELL
When the market transitions from BUY state to SELL state, the reverse sequence is performed:
-
checks for an existing Buy position;
-
closes the existing opposite position;
-
verifies the result of the operation;
-
performs the required trading checks;
-
sends a Sell order.
If the current position already corresponds to the detected trading state, no additional position is opened.
Therefore, the Expert Advisor operates through changes in the trading state rather than by continuously adding new positions.
Closed-Bar Analysis
One of the key features of XauUsd Nostradamus M5 is the use of fully formed price data.
The trading cycle is tied to the appearance of a new bar:
New M5 bar → closed-bar analysis → state determination → condition checks → trading action
The unfinished candle is not used to generate the primary signal.
This is relevant for an algorithmic trading system because the values of the current candle can change with every incoming tick. Using a closed bar makes the signal-generation point deterministic relative to the completed price structure.
Algorithmic Direction Model
The core of the system uses an analysis of price direction based on smoothed price characteristics and local market structure.
The exact internal formulas, mathematical calculation sequence, and conditions used to generate the trading state are part of the Expert Advisor's proprietary trading logic and are not disclosed in full.
For the user, the result of these calculations is represented as one of two final states:
BUY state or SELL state.
The Amplitude parameter is one of the components of this model and affects the sensitivity of the algorithm to changes in local price structure.
Open Position Management
Signal generation and position management are handled independently.
After a trade is opened, the Expert Advisor begins dynamically managing its Stop Loss.
Unlike the trading signal, the trailing stop is processed on every tick.
The general workflow is:
Tick → current price detection → new Stop Loss calculation → broker restriction check → position modification
For Buy positions, the protective level can only move upward.
For Sell positions, the protective level can only move downward.
Therefore, an already established protective level is not moved in a direction that would increase the potential loss.
Trailing Stop Calculation
The primary position management parameter is:
TrailingStopPoints = 85
For a Buy position, the target level is calculated relative to Bid:
Stop Loss ≈ Bid − TrailingStopPoints × Point
For a Sell position:
Stop Loss ≈ Ask + TrailingStopPoints × Point
The actual level may differ from the simple mathematical calculation due to trading instrument restrictions.
The Expert Advisor takes into account, among other things:
-
Stop Level ;
-
Freeze Level ;
-
symbol digits;
-
minimum volume;
-
maximum volume;
-
volume step.
If the calculated Stop Loss violates the broker's trading restrictions, it is adjusted according to the symbol specification or the modification request is not executed.
No Fixed Take Profit
The Expert Advisor does not set a conventional fixed Take Profit .
The primary exit mechanisms are:
-
a change in the trading state;
-
the dynamic Stop Loss.
When the market moves in the direction of the position, the protective level may move along with the price.
If the price subsequently moves against the position, the Stop Loss may be triggered and the position closed.
Trading Condition Control
Before sending a new trading request, the Expert Advisor performs a set of technical checks.
The following conditions are checked:
-
whether algorithmic trading is enabled;
-
whether trading is permitted on the current account;
-
whether Expert Advisor trading is allowed;
-
whether the symbol supports the required trading mode;
-
whether the allowed number of positions has been exceeded;
-
whether the volume complies with the symbol specification;
-
whether sufficient free margin is available;
-
whether the current spread is within MaxSpreadPoints .
If a critical condition is not satisfied, the new trading request is not sent.
Margin Control
Before opening a position, the Expert Advisor uses the standard MetaTrader 5 mechanism:
OrderCalcMargin()
This function is used to estimate the required margin for the intended operation.
The Expert Advisor then checks whether sufficient free margin is available.
Therefore, the presence of a trading signal alone does not automatically result in a trade.
The logical sequence can be represented as:
Signal → Trading permissions → Symbol conditions → Spread → Volume → Margin → Order
Only after the required checks have been passed is the trading request sent.
Spread Filter
The Expert Advisor uses the following parameter to control market conditions:
MaxSpreadPoints = 50
Before opening a new position, the current SYMBOL_SPREAD value is checked.
If:
SYMBOL_SPREAD > MaxSpreadPoints
the new trading signal is ignored.
The filter is applied immediately before opening a new position.
It does not close an already existing position if the spread subsequently widens.
This is particularly important when trading XAUUSD, as spreads can change significantly during major economic releases, transitions between trading sessions, and periods of reduced liquidity.
Point and Actual Spread Value
MaxSpreadPoints is a technical MetaTrader 5 parameter measured in points.
The value of one point depends on the specification of the particular trading instrument.
Therefore:
50 points ≠ a universal monetary spread value.
Before running the Expert Advisor, the XAUUSD specification provided by the broker should be checked:
Market Watch → XAUUSD → Specification
Particular attention should be paid to:
-
Digits ;
-
Point ;
-
Spread ;
-
Contract Size ;
-
Tick Size ;
-
Tick Value ;
-
Volume Step .
Position Volume Management
The position size is defined by:
LotSize = 0.01
The Expert Advisor does not increase the lot size after a losing trade.
The volume of each new position is determined by the LotSize parameter while taking the symbol's trading restrictions into account.
The following are checked before an order is sent:
-
minimum volume;
-
maximum volume;
-
volume step;
-
permitted trading range.
For example:
LotSize = 0.01
means that the Expert Advisor attempts to open a position with a volume of 0.01 lots.
With:
LotSize = 0.10
the position volume is 0.10 lots.
Increasing the volume directly increases the monetary change in the trade result for the same price movement.
Magic Number and Position Identification
The Expert Advisor uses the following identifier for its own trades:
MagicNumber = 123456
The Magic Number allows the Expert Advisor to distinguish its own positions from:
-
manual trades;
-
positions opened by other Expert Advisors;
-
another instance of XauUsd Nostradamus M5.
Position management is performed based on the current symbol and the corresponding Magic Number.
If multiple instances of the Expert Advisor are used on the same trading account, it is recommended to assign different Magic Numbers to them.
Slippage Control
The parameter:
SlippagePoints = 100
is used to limit the acceptable price deviation when sending a trading request through the CTrade trading interface.
For XAUUSD, this parameter can be particularly relevant during rapid price movements.
Between the moment a signal is generated and the actual execution of the order, the market price may change.
A lower SlippagePoints value limits the acceptable deviation but may increase the probability of execution rejection during sharp market movements.
A higher value allows a wider deviation range and may facilitate execution in rapidly changing market conditions.
SlippagePoints is not a commission and does not determine the instrument's current spread.
Handling Opposite Positions
When an opposite trading signal is generated, the Expert Advisor does not simply open a new position on top of the existing one without first processing the current position.
For example, if a Buy position exists and a SELL state is detected, the algorithm first attempts to close the existing Buy position.
The actual position state is then verified.
Only after the position has been confirmed as closed does the Expert Advisor attempt to open a Sell position.
The logical sequence is:
BUY → SELL signal
→ Close Buy
→ Verify position state
→ Validate trading conditions
→ Open Sell
This reduces the possibility of simultaneous opposite positions associated with the Expert Advisor's trading logic.
The actual position-management behavior also depends on the MetaTrader 5 account type and the broker's trading restrictions.
No Grid or Martingale
XauUsd Nostradamus M5 does not use:
-
Grid;
-
Martingale;
-
position averaging;
-
automatic lot increases after a loss;
-
progressive volume increases designed to compensate for a previous trade.
LotSize is set by the user as the base trading volume.
The result of the previous operation is not used to automatically change the volume of the next position.
Main Parameters
| Parameter | Default | Purpose |
|---|---|---|
| LotSize | 0.01 | Position volume |
| Amplitude | 2 | Sensitivity of the market-state detection algorithm |
| TrailingStopPoints | 85 | Dynamic Stop Loss distance |
| MagicNumber | 123456 | Expert Advisor position identifier |
| SlippagePoints | 100 | Acceptable price deviation |
| MaxSpreadPoints | 50 | Maximum spread allowed for a new entry |
Recommended Configuration
The primary usage scenario is:
Symbol: XAUUSD
Timeframe: M5
LotSize: 0.01
Amplitude: 2
TrailingStopPoints: 85
MagicNumber: unique for each instance
SlippagePoints: 100
MaxSpreadPoints: 50
These values represent the base configuration of the Expert Advisor and do not imply that they are equally suitable for every broker, account type, or account size.
Particular attention should be paid to the XAUUSD specification because Point , Tick Size , Tick Value , Contract Size , and permitted trading volumes may differ between brokers.
Trading Infrastructure Requirements
The following environment is recommended for normal operation:
-
MetaTrader 5;
-
XAUUSD;
-
M5 timeframe;
-
algorithmic trading enabled;
-
a broker supporting XAUUSD trading;
-
sufficient free margin;
-
stable connection to the trading server.
A VPS can be used for continuous Expert Advisor operation.
It is preferable to use a VPS with low network latency to the broker's trading server, as latency can affect the transmission time of trading and position-modification requests.
XAUUSD Trading Characteristics
Gold is characterized by relatively high intraday volatility and can quickly change its trading range.
As a result, trading conditions can vary considerably depending on the time of day and market environment.
Particular attention should be paid to:
-
major macroeconomic data releases;
-
the U.S. trading session;
-
sharp movements in the U.S. dollar;
-
periods of low liquidity;
-
spread widening;
-
transitions between trading sessions.
MaxSpreadPoints prevents new entries when the configured spread threshold is exceeded, but it cannot control spread changes after a position has already been opened.
Complete Trading Cycle
The simplified XauUsd Nostradamus M5 workflow is:
1. Tick reception
The Expert Advisor receives a new market event.
2. Open position management
If a position exists, the current dynamic Stop Loss is calculated.
3. New bar detection
When a new M5 bar appears, the primary analysis is initiated.
4. Closed-data analysis
Completed price data is used.
5. Trading state determination
The algorithm determines either BUY state or SELL state.
6. State-change verification
If there is no new state transition, no new trade is opened.
7. Trading condition validation
Trading permissions, spread, volume, margin, and symbol restrictions are checked.
8. Current position handling
If required, the opposite position is closed.
9. Operation result verification
The Expert Advisor verifies the actual position state.
10. New position opening
If all conditions are satisfied, a Buy or Sell order is sent.
11. Continuous management
The open position is managed using a dynamic Stop Loss on every tick.
Technical Concept of the Expert Advisor
XauUsd Nostradamus M5 is an event-driven algorithm that operates on two different processing levels:
Bar-based logic
Used to generate the primary trading signal and determine changes in the market state.
Tick-based logic
Used to manage an already open position and update the protective Stop Loss.
This separation is an important part of the Expert Advisor's architecture:
New Bar → Signal Generation
Every Tick → Position Management
The trading decision is based on closed bars, while open-position risk management is performed at a higher frequency.
Conclusion
XauUsd Nostradamus M5 is a technically oriented MetaTrader 5 Expert Advisor specialized for algorithmic trading of XAUUSD on the M5 timeframe.
The Expert Advisor's architecture includes:
-
closed-bar analysis;
-
BUY/SELL trading-state model;
-
position opening on state transitions;
-
closing the opposite position before a reversal;
-
dynamic tick-based trailing Stop Loss;
-
Stop Level and Freeze Level control;
-
free-margin verification through OrderCalcMargin() ;
-
maximum-spread filtering;
-
trading-volume validation;
-
position identification through Magic Number;
-
price-deviation control;
-
no Grid or Martingale mechanisms.
Before using the Expert Advisor on a live account, the XAUUSD specification of the selected broker, execution conditions, spread characteristics, available margin, and position size should be checked.
It is recommended to test the Expert Advisor first in the MetaTrader 5 Strategy Tester and then on a demo account using settings appropriate for the specific broker.


