Risk Management Coding: Implementing Max Daily Loss Logic in Your EA (The Survival Code)
In the world of proprietary trading, profit is optional, but risk control is mandatory. The single most common failure point for Expert Advisors (EAs) in prop firm challenges is the violation of the Maximum Daily Loss (MDL) rule. Unlike the overall drawdown, which may be several percentage points, the MDL is a hard, daily limit—typically 4% to 5%—and crossing it results in immediate disqualification. Therefore, your Prop Firm Expert Advisor must not only be profitable; it must be completely self-policing.
Implementing robust MDL logic directly into your EA’s code is not just a feature; it is the Survival Code. It acts as an autonomous circuit breaker, overriding all other trading logic when the defined risk threshold is breached. This ensures that market volatility, unexpected slippage, or a sequence of losing trades cannot cascade into a challenge failure. This is arguably the most essential piece of code you will ever write for a prop firm EA.
The Core Principle: Tracking Floating Equity, Not Just Balance
The first mistake in MDL coding is tracking the balance. Prop firms calculate MDL based on the higher of the starting balance or the equity at the end of the previous trading day. But your EA must track live floating equity throughout the day.
The Tracking Mechanism: Your code needs a variable that is updated at the start of the trading day (after midnight server time), let’s call it DailyStartEquity. Throughout the day, the EA must constantly compare the CurrentFloatingEquity to this starting point.
// Define the limit (e.g., 4.5% to create a buffer for a 5% rule)
double MAX_DAILY_LOSS_PERCENT = 0.045;
double MaxLossAmount = DailyStartEquity * MAX_DAILY_LOSS_PERCENT;
double HardStopEquityLevel = DailyStartEquity - MaxLossAmount;
The Loop Check: Every time the EA runs (on a new tick), it must execute a check:
if (AccountInfoDouble(ACCOUNT_EQUITY) < HardStopEquityLevel) {
// TRIGGER THE CIRCUIT BREAKER
}
This constant monitoring of the current equity against the pre-calculated daily hard stop is the backbone of your MDL defense.
The Circuit Breaker: Immediate and Comprehensive Action
When the MDL threshold is breached, the EA cannot simply stop opening new trades. It must execute a definitive, three-step “circuit breaker” action to ensure compliance.
Step 1: Close All Open Trades Immediately: The EA must iterate through all open positions and issue an immediate market close for every trade, regardless of whether it is in profit or loss. This stops the bleeding right at the breach point. A well-coded EA will log this event and the resulting equity level.
Step 2: Delete All Pending Orders: Limit orders, stop orders, and pending limits/stops must be deleted. A pending order that is triggered after the MDL breach could immediately push the equity further below the limit, compounding the violation.
Step 3: Lock the EA for the Day: The most critical step. The EA must set a persistent variable (e.g., in a global variable or external file) that flags the account as “Daily Limit Reached.” The trading logic (the OnTick() function) should immediately exit if this flag is set and the current time is before the next daily reset (usually midnight server time).
Incorporating Trailing Drawdown Logic
While the MDL is critical, many prop firms also use a Trailing Drawdown (TD) which trails the highest attained equity peak. This is far more complex to code but essential for long-term
Your code needs a secondary persistent variable: HighestEquityPeak.
// Update the peak constantly
if (AccountInfoDouble(ACCOUNT_EQUITY) > HighestEquityPeak) {
HighestEquityPeak = AccountInfoDouble(ACCOUNT_EQUITY);
}
// Check against the Trailing Drawdown Limit (e.g., 10%)
double TrailingStopLevel = HighestEquityPeak * (1 - TRAILING_DRAWDOWN_PERCENT);
if (AccountInfoDouble(ACCOUNT_EQUITY) < TrailingStopLevel) {
// This is a permanent failure; trigger a full stop (not just daily)
}
The TD rule is usually a challenge killer, so the code’s logic must be precise. This is a key part of
Integrating External Compliance Filters
The Survival Code shouldn’t only worry about money; it should also worry about time. Your risk management logic should also include external checks related to the prop firm’s “Do’s and Don’ts”:
-
News Filter Integration: Use external libraries or hard-coded dates/times to prevent the EA from executing trades during high-impact news. This prevents unexpected slippage and market manipulation claims. Refer to
for non-monetary compliance.The Do’s and Don’ts of EA Trading on Prop Firm Platforms (Rules Checklist) -
Maximum Position Size: Ensure your EA’s lot-sizing logic never attempts to open a trade that exceeds the firm’s maximum lot size per instrument, regardless of available margin. This prevents a common account violation.
Implementing the Max Daily Loss logic is the ultimate expression of the strategy you vetted when you were deciding