Here is the Expert Advisor converted for MetaTrader 5.
MQL5 handles account identifiers and chart objects slightly differently than MQL4. The most critical change here is upgrading the account login variable to a ulong (unsigned long) to accommodate MT5's larger account numbers, and calculating the exact chart dimensions dynamically to ensure the visual lockdown covers the screen perfectly.
Code: Select all
//+------------------------------------------------------------------+
//| OneBookGuardian.mq5 |
//| Continuous EA monitor to enforce trading discipline |
//+------------------------------------------------------------------+
#property copyright "Gemini"
#property version "1.00"
//--- Input Parameters
input ulong AuthorizedLiveAccount = 12345678; // Your SINGLE Authorized Live Account
input bool AllowDemoTrading = true; // Allow execution on Demo for research
//--- Global Variables
bool disciplineBreached = false;
string warningObjName = "DISCIPLINE_LOCKDOWN_LABEL";
string bgObjName = "DISCIPLINE_LOCKDOWN_BG";
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
// Set a 1-second timer to constantly check the account status
EventSetTimer(1);
// Run an immediate check on startup
VerifyEnvironment();
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
EventKillTimer();
UnlockChart(); // Clean up graphics when removed
}
//+------------------------------------------------------------------+
//| Expert tick function (Automated Trading Logic goes here) |
//+------------------------------------------------------------------+
void OnTick()
{
// RULE 1: If discipline is breached, kill all automated trading
if(disciplineBreached)
{
// The EA exits immediately without executing any trade logic
return;
}
// --- YOUR AUTOMATED TRADING LOGIC GOES BELOW THIS LINE ---
// If the EA gets to this point, you are safely in your main book.
}
//+------------------------------------------------------------------+
//| Timer function (Constant Environment Monitoring) |
//+------------------------------------------------------------------+
void OnTimer()
{
VerifyEnvironment();
if(disciplineBreached)
{
// Continuous psychological friction if the warning is ignored
Alert("RED FLAG: Unauthorized Account! Leakage trains the same hands.");
PlaySound("timeout.wav");
}
}
//+------------------------------------------------------------------+
//| Core Logic: Verifies the current account |
//+------------------------------------------------------------------+
void VerifyEnvironment()
{
// In MQL5, logins are long/ulong variables
ulong currentAccount = (ulong)AccountInfoInteger(ACCOUNT_LOGIN);
long tradeMode = AccountInfoInteger(ACCOUNT_TRADE_MODE);
bool isDemo = (tradeMode == ACCOUNT_TRADE_MODE_DEMO);
bool isAuthorized = false;
if(isDemo)
{
if(AllowDemoTrading)
isAuthorized = true;
}
else // Live Environment
{
if(currentAccount == AuthorizedLiveAccount)
isAuthorized = true;
}
// Handle state changes
if(!isAuthorized && !disciplineBreached)
{
disciplineBreached = true;
LockdownChart();
Print("Discipline breach detected. Automated trading disabled.");
}
else if(isAuthorized && disciplineBreached)
{
disciplineBreached = false;
UnlockChart();
Print("Authorized environment confirmed. Systems restored.");
}
}
//+------------------------------------------------------------------+
//| UI functions: Psychological deterrents |
//+------------------------------------------------------------------+
void LockdownChart()
{
// Create a massive red background blocking the chart
if(ObjectFind(0, bgObjName) < 0)
{
ObjectCreate(0, bgObjName, OBJ_RECTANGLE_LABEL, 0, 0, 0);
ObjectSetInteger(0, bgObjName, OBJPROP_XDISTANCE, 0);
ObjectSetInteger(0, bgObjName, OBJPROP_YDISTANCE, 0);
// Attempt to cover the entire screen dynamically using MQL5 chart properties
int width = (int)ChartGetInteger(0, CHART_WIDTH_IN_PIXELS);
int height = (int)ChartGetInteger(0, CHART_HEIGHT_IN_PIXELS);
ObjectSetInteger(0, bgObjName, OBJPROP_XSIZE, (width > 0) ? width : 4000);
ObjectSetInteger(0, bgObjName, OBJPROP_YSIZE, (height > 0) ? height : 4000);
ObjectSetInteger(0, bgObjName, OBJPROP_BGCOLOR, clrRed);
ObjectSetInteger(0, bgObjName, OBJPROP_BACK, false);
ObjectSetInteger(0, bgObjName, OBJPROP_SELECTABLE, false);
ObjectSetInteger(0, bgObjName, OBJPROP_HIDDEN, true); // Keep out of object list
}
// Add giant warning text
if(ObjectFind(0, warningObjName) < 0)
{
ObjectCreate(0, warningObjName, OBJ_LABEL, 0, 0, 0);
ObjectSetInteger(0, warningObjName, OBJPROP_XDISTANCE, 50);
ObjectSetInteger(0, warningObjName, OBJPROP_YDISTANCE, 100);
ObjectSetString(0, warningObjName, OBJPROP_TEXT, "UNAUTHORIZED PLAY ACCOUNT");
ObjectSetString(0, warningObjName, OBJPROP_FONT, "Arial Black");
ObjectSetInteger(0, warningObjName, OBJPROP_FONTSIZE, 36);
ObjectSetInteger(0, warningObjName, OBJPROP_COLOR, clrWhite);
ObjectSetInteger(0, warningObjName, OBJPROP_BACK, false);
ObjectSetInteger(0, warningObjName, OBJPROP_SELECTABLE, false);
ObjectSetInteger(0, warningObjName, OBJPROP_HIDDEN, true);
}
ChartRedraw(0);
}
void UnlockChart()
{
ObjectDelete(0, bgObjName);
ObjectDelete(0, warningObjName);
ChartRedraw(0);
}
//+------------------------------------------------------------------+