Re: 🛢️ High-Volume Oil (WTI) Scalping Strategy – 18 Years of Price Action Distilled
Posted: Mon Aug 31, 2026 9:31 pm
To implement a daily maximum drawdown limit, the EA needs to capture a "snapshot" of your account balance at the very first tick of a new trading day. It will then continuously compare your live floating equity against that starting balance.
If your equity drops below the allowed threshold, a lockout flag is triggered, preventing any new trades until the broker server rolls over to the next day.
Here is how to integrate this safety net into your MQL5 script.
1. Add the Drawdown Inputs and Global Variables
Place these new variables at the top of your script, right below your existing risk management inputs. We need global variables to store the state of the current day so the EA doesn't lose track of the limit between ticks.
Add the Drawdown Check Function
Add this custom function at the bottom of your script. This function detects when a new day begins, resets the tracking variables, and calculates the live drawdown percentage.
If your equity drops below the allowed threshold, a lockout flag is triggered, preventing any new trades until the broker server rolls over to the next day.
Here is how to integrate this safety net into your MQL5 script.
1. Add the Drawdown Inputs and Global Variables
Place these new variables at the top of your script, right below your existing risk management inputs. We need global variables to store the state of the current day so the EA doesn't lose track of the limit between ticks.
Code: Select all
//--- Daily Drawdown Limits
input double MaxDailyDrawdown = 5.0; // Max daily drawdown % (0 to disable)
//--- Global variables for Drawdown Tracker
double dayStartBalance = 0.0;
int currentDay = -1;
bool dailyLimitHit = false;Add this custom function at the bottom of your script. This function detects when a new day begins, resets the tracking variables, and calculates the live drawdown percentage.
Code: Select all
//+------------------------------------------------------------------+
//| Custom Function: Check Daily Drawdown Limit |
//+------------------------------------------------------------------+
bool IsDailyDrawdownHit()
{
// If set to 0, the filter is disabled
if(MaxDailyDrawdown <= 0) return false;
MqlDateTime timeStruct;
TimeCurrent(timeStruct);
// 1. Detect a new server day
if(timeStruct.day_of_year != currentDay)
{
currentDay = timeStruct.day_of_year;
// Snapshot the balance at the start of the day
dayStartBalance = AccountInfoDouble(ACCOUNT_BALANCE);
dailyLimitHit = false; // Reset the lockout flag
Print("New day started. Starting balance snapshot: ", dayStartBalance);
}
// 2. If the limit was already hit today, stay locked
if(dailyLimitHit) return true;
// 3. Calculate current drawdown against the starting balance
double currentEquity = AccountInfoDouble(ACCOUNT_EQUITY);
if(currentEquity < dayStartBalance)
{
double drawdownAmount = dayStartBalance - currentEquity;
double currentDrawdownPercent = (drawdownAmount / dayStartBalance) * 100.0;
if(currentDrawdownPercent >= MaxDailyDrawdown)
{
Print("WARNING: Daily Drawdown Limit (", MaxDailyDrawdown, "%) Hit! Trading locked for today.");
dailyLimitHit = true;
return true;
}
}
return false;
}