2026-08-13 09:00:54 -07:00 | | | //+------------------------------------------------------------------+
|
| | | //| PropFirmGuard.mq5 |
|
| | | //| Copyright 2026, MetaQuotes Ltd. |
|
| | | //| https://www.mql5.com |
|
| | | //+------------------------------------------------------------------+
|
2026-08-15 02:44:23 -07:00 | | | #property copyright "Copyright 2026, MetaQuotes Ltd."
|
| | | #property link "https://www.mql5.com"
|
| | | #property version "1.00"
|
| | |
|
| | | //+------------------------------------------------------------------+
|
| | | //| Standard Library |
|
| | | //+------------------------------------------------------------------+
|
| | | #include <Trade\Trade.mqh>
|
| | |
|
| | | //+------------------------------------------------------------------+
|
| | | //| Enumerations |
|
| | | //+------------------------------------------------------------------+
|
| | | //| Represents the general compliance condition of the account. |
|
| | | //+------------------------------------------------------------------+
|
| | | enum ENUM_COMPLIANCE_STATUS
|
| | | {
|
| | | COMPLIANCE_SAFE = 0, // Account is operating within configured limits
|
| | | COMPLIANCE_WARNING, // Account is approaching a compliance limit
|
| | | COMPLIANCE_CRITICAL, // Account is very close to a compliance limit
|
| | | COMPLIANCE_BREACHED // A configured compliance limit has been breached
|
| | | };
|
2026-08-30 05:53:24 +03:00 | | |
|
2026-08-15 02:44:23 -07:00 | | | //+------------------------------------------------------------------+
|
| | | //| Profit target status |
|
| | | //+------------------------------------------------------------------+
|
| | | //| Represents progress toward the configured account profit target. |
|
| | | //+------------------------------------------------------------------+
|
| | | enum ENUM_TARGET_STATUS
|
| | | {
|
2026-08-30 05:53:24 +03:00 | | | TARGET_IN_PROGRESS = 0, // Target is still being pursued
|
| | | TARGET_NEARING, // At least 90% of the target has been reached
|
| | | TARGET_REACHED // Required target balance has been achieved
|
2026-08-15 02:44:23 -07:00 | | | };
|
2026-08-30 05:53:24 +03:00 | | |
|
2026-08-15 02:44:23 -07:00 | | | //+------------------------------------------------------------------+
|
| | | //| Protection action |
|
| | | //+------------------------------------------------------------------+
|
| | | //| Defines what Prop Firm Guard should do when a monitored rule |
|
| | | //| requires protective action. |
|
| | | //+------------------------------------------------------------------+
|
| | | enum ENUM_PROTECTION_ACTION
|
| | | {
|
2026-08-30 05:53:24 +03:00 | | | PROTECTION_WARN_ONLY = 0, // Report the condition without closing positions
|
| | | PROTECTION_CLOSE_AFFECTED, // Close positions affected by the triggering rule
|
| | | PROTECTION_CLOSE_ALL // Close every open account position
|
2026-08-28 18:38:10 +03:00 | | | };
|
2026-08-31 18:18:41 +03:00 | | |
|
2026-08-30 06:30:53 +03:00 | | | //+------------------------------------------------------------------+
|
| | | //| Inputs |
|
| | | //+------------------------------------------------------------------+
|
| | | input group "General"
|
| | | input int InpTimerSeconds = 1; // Dashboard update interval in seconds
|
| | | input int InpDashboardX = 10; // Dashboard horizontal position
|
| | | input int InpDashboardY = 105; // Dashboard vertical position
|
| | | input bool InpEnableAlerts = true; // Enable terminal alerts
|
| | | input bool InpEnablePushNotifications = false; // Enable mobile push notifications
|
| | |
|
| | | input group "Prop Firm Rules"
|
| | | input double InpInitialAccountBalance = 20000.0; // Initial prop-firm account balance
|
| | | input double InpDailyDrawdownPercent = 5.0; // Maximum permitted daily drawdown
|
| | | input double InpOverallDrawdownPercent = 10.0; // Maximum permitted overall drawdown
|
| | | input double InpProfitTargetPercent = 8.0; // Required profit target
|
| | |
|
| | | input group "Warning Levels"
|
| | | input double InpWarningLevelPercent = 70.0; // Loss limit usage that triggers warning
|
| | | input double InpCriticalLevelPercent = 90.0; // Loss limit usage that triggers critical state
|
| | |
|
| | | input group "News Filter"
|
| | | input bool InpEnableNewsFilter = true; // Enable economic news monitoring
|
| | | input ENUM_CALENDAR_EVENT_IMPORTANCE InpNewsImportance = CALENDAR_IMPORTANCE_HIGH; // Minimum monitored importance
|
| | | input int InpMinutesBeforeNews = 10; // Restricted minutes before news
|
| | | input int InpMinutesAfterNews = 10; // Restricted minutes after news
|
| | | input bool InpAllowNewsHolding = true; // Allow positions to remain open during news
|
| | |
|
| | | input group "Protection Actions"
|
| | | input ENUM_PROTECTION_ACTION InpDailyDrawdownAction = PROTECTION_WARN_ONLY; // Daily breach action
|
| | | input ENUM_PROTECTION_ACTION InpOverallDrawdownAction = PROTECTION_WARN_ONLY; // Overall breach action
|
| | | input ENUM_PROTECTION_ACTION InpNewsRestrictionAction = PROTECTION_WARN_ONLY; // News restriction action
|
| | |
|
2026-08-30 11:53:44 +03:00 | | | //+------------------------------------------------------------------+
|
| | | //| Account state structure |
|
| | | //+------------------------------------------------------------------+
|
| | | //| Holds the live account values required by the monitoring logic. |
|
| | | //+------------------------------------------------------------------+
|
| | | struct SAccountState
|
| | | {
|
| | | long login; // Trading account login number
|
| | | double balance; // Current account balance
|
| | | double equity; // Current account equity
|
| | | double floatingPL; // Current floating profit or loss
|
| | | ENUM_COMPLIANCE_STATUS status; // Current overall compliance status
|
| | | };
|
| | |
|
2026-08-30 06:30:53 +03:00 | | | //+------------------------------------------------------------------+
|
| | | //| Account settings structure |
|
| | | //+------------------------------------------------------------------+
|
| | | //| Stores the main prop-firm rules associated with this account. |
|
| | | //+------------------------------------------------------------------+
|
| | | struct SAccountSettings
|
| | | {
|
2026-08-31 18:18:41 +03:00 | | | long accountLogin; // Trading account login
|
| | | string accountServer; // Broker trading server
|
| | | double initialBalance; // Initial prop-firm account balance
|
| | | double dailyDrawdownPercent; // Maximum daily drawdown
|
| | | double overallDrawdownPercent; // Maximum overall drawdown
|
| | | double profitTargetPercent; // Required profit target
|
| | | datetime updatedAt; // Last settings update time
|
2026-08-30 06:30:53 +03:00 | | | };
|
2026-08-31 18:18:41 +03:00 | | |
|
2026-08-30 11:53:44 +03:00 | | | //+------------------------------------------------------------------+
|
| | | //| Global variables |
|
| | | //+------------------------------------------------------------------+
|
| | | SAccountState g_accountState; // Latest monitored account state
|
| | | SAccountSettings g_accountSettings; // Persisted prop-firm settings
|
2026-08-31 19:38:11 +03:00 | | | string g_databaseName = "prop_firm_compliance.sqlite"; // SQLite database file
|
| | | int g_database = INVALID_HANDLE; // Active SQLite database handle
|
2026-08-30 11:53:44 +03:00 | | |
|
2026-08-30 12:36:36 +03:00 | | | //+------------------------------------------------------------------+
|
| | | //| Validate compliance inputs |
|
| | | //+------------------------------------------------------------------+
|
| | | bool ValidateComplianceInputs()
|
| | | {
|
2026-08-31 19:38:11 +03:00 | | | //--- Validate the initial account balance
|
2026-08-30 12:36:36 +03:00 | | | if(InpInitialAccountBalance <= 0.0)
|
| | | {
|
| | | Print("Prop Firm Guard: Initial account balance must be greater than zero.");
|
| | |
|
| | | return(false);
|
| | | }
|
| | |
|
2026-08-31 19:38:11 +03:00 | | | //--- Validate the daily drawdown percentage
|
2026-08-30 12:36:36 +03:00 | | | if(InpDailyDrawdownPercent <= 0.0 ||
|
| | | InpDailyDrawdownPercent > 100.0)
|
| | | {
|
| | | Print("Prop Firm Guard: Daily drawdown percentage must be between 0 and 100.");
|
| | |
|
| | | return(false);
|
| | | }
|
| | |
|
2026-08-31 19:38:11 +03:00 | | | //--- Validate the overall drawdown percentage
|
2026-08-30 12:36:36 +03:00 | | | if(InpOverallDrawdownPercent <= 0.0 ||
|
| | | InpOverallDrawdownPercent > 100.0)
|
| | | {
|
| | | Print("Prop Firm Guard: Overall drawdown percentage must be between 0 and 100.");
|
| | |
|
| | | return(false);
|
| | | }
|
| | |
|
2026-08-31 19:38:11 +03:00 | | | //--- Validate the profit target percentage
|
2026-08-30 12:36:36 +03:00 | | | if(InpProfitTargetPercent <= 0.0 ||
|
| | | InpProfitTargetPercent > 100.0)
|
| | | {
|
| | | Print("Prop Firm Guard: Profit target percentage must be between 0 and 100.");
|
| | |
|
| | | return(false);
|
| | | }
|
| | |
|
2026-08-31 19:38:11 +03:00 | | | //--- Validate the warning threshold
|
2026-08-30 12:36:36 +03:00 | | | if(InpWarningLevelPercent <= 0.0 ||
|
| | | InpWarningLevelPercent >= InpCriticalLevelPercent)
|
| | | {
|
| | | Print("Prop Firm Guard: Warning level must be greater than 0 and below the critical level.");
|
| | |
|
| | | return(false);
|
| | | }
|
| | |
|
2026-08-31 19:38:11 +03:00 | | | //--- Validate the critical threshold
|
2026-08-30 12:36:36 +03:00 | | | if(InpCriticalLevelPercent <= InpWarningLevelPercent ||
|
| | | InpCriticalLevelPercent >= 100.0)
|
| | | {
|
| | | Print("Prop Firm Guard: Critical level must be above the warning level and below 100.");
|
| | |
|
| | | return(false);
|
| | | }
|
| | |
|
2026-08-31 19:38:11 +03:00 | | | //--- Validate the news restriction windows
|
2026-08-30 12:36:36 +03:00 | | | if(InpMinutesBeforeNews < 0 ||
|
| | | InpMinutesAfterNews < 0)
|
| | | {
|
| | | Print("Prop Firm Guard: News restriction minutes cannot be negative.");
|
| | |
|
| | | return(false);
|
| | | }
|
| | |
|
| | | return(true);
|
| | | }
|
| | |
|
2026-08-30 11:53:44 +03:00 | | | //+------------------------------------------------------------------+
|
| | | //| Update account state |
|
| | | //+------------------------------------------------------------------+
|
| | | void UpdateAccountState()
|
| | | {
|
2026-08-31 19:38:11 +03:00 | | | //--- Read the current account values
|
2026-08-30 11:53:44 +03:00 | | | g_accountState.login = AccountInfoInteger(ACCOUNT_LOGIN);
|
| | | g_accountState.balance = AccountInfoDouble(ACCOUNT_BALANCE);
|
| | | g_accountState.equity = AccountInfoDouble(ACCOUNT_EQUITY);
|
| | |
|
2026-08-31 19:38:11 +03:00 | | | //--- Derive the current floating profit or loss
|
2026-08-30 11:53:44 +03:00 | | | g_accountState.floatingPL = g_accountState.equity - g_accountState.balance;
|
| | |
|
2026-08-31 19:38:11 +03:00 | | | //--- Initialize compliance status before rule calculations are added
|
2026-08-30 11:53:44 +03:00 | | | g_accountState.status = COMPLIANCE_SAFE;
|
| | | }
|
2026-08-31 18:18:41 +03:00 | | |
|
2026-08-30 13:06:01 +03:00 | | | //+------------------------------------------------------------------+
|
| | | //| Open database |
|
| | | //+------------------------------------------------------------------+
|
| | | bool OpenDatabase()
|
| | | {
|
2026-08-31 19:38:11 +03:00 | | | //--- Open or create the SQLite database
|
2026-08-30 13:06:01 +03:00 | | | ResetLastError();
|
| | |
|
| | | g_database = DatabaseOpen(g_databaseName,
|
| | | DATABASE_OPEN_READWRITE |
|
| | | DATABASE_OPEN_CREATE);
|
| | |
|
| | | if(g_database == INVALID_HANDLE)
|
| | | {
|
| | | Print("Prop Firm Guard: Could not open database ",
|
| | | g_databaseName,
|
| | | ". Error: ",
|
| | | GetLastError());
|
| | |
|
| | | return(false);
|
| | | }
|
| | |
|
| | | Print("Prop Firm Guard: Database opened successfully: ",
|
| | | g_databaseName);
|
| | |
|
| | | return(true);
|
| | | }
|
2026-08-31 18:18:41 +03:00 | | |
|
2026-08-30 13:06:01 +03:00 | | | //+------------------------------------------------------------------+
|
| | | //| Close database |
|
| | | //+------------------------------------------------------------------+
|
| | | void CloseDatabase()
|
| | | {
|
2026-08-31 19:38:11 +03:00 | | | //--- Ignore the request when no database connection is active
|
2026-08-30 13:06:01 +03:00 | | | if(g_database == INVALID_HANDLE)
|
| | | return;
|
| | |
|
2026-08-31 19:38:11 +03:00 | | | //--- Close the active SQLite connection
|
2026-08-30 13:06:01 +03:00 | | | ResetLastError();
|
| | |
|
| | | DatabaseClose(g_database);
|
| | |
|
| | | int errorCode = GetLastError();
|
| | |
|
| | | if(errorCode != 0)
|
| | | {
|
| | | Print("Prop Firm Guard: Database close reported error: ",
|
| | | errorCode);
|
| | | }
|
| | | else
|
| | | {
|
| | | Print("Prop Firm Guard: Database closed successfully.");
|
| | | }
|
| | |
|
2026-08-31 19:38:11 +03:00 | | | //--- Clear the stored database handle
|
2026-08-30 13:06:01 +03:00 | | | g_database = INVALID_HANDLE;
|
| | | }
|
2026-08-31 18:18:41 +03:00 | | |
|
2026-08-30 14:24:21 +03:00 | | | //+------------------------------------------------------------------+
|
| | | //| Ensure database tables exist |
|
| | | //+------------------------------------------------------------------+
|
| | | bool EnsureDatabaseTables()
|
| | | {
|
2026-08-31 19:38:11 +03:00 | | | //--- Define persistent storage for each account/server pair
|
2026-08-30 14:24:21 +03:00 | | | string settingsTable =
|
| | | "CREATE TABLE IF NOT EXISTS settings ("
|
| | | "account_login INTEGER NOT NULL,"
|
| | | "account_server TEXT NOT NULL,"
|
| | | "initial_balance REAL NOT NULL,"
|
| | | "daily_drawdown_percent REAL NOT NULL,"
|
| | | "overall_drawdown_percent REAL NOT NULL,"
|
| | | "profit_target_percent REAL NOT NULL,"
|
| | | "updated_at INTEGER NOT NULL,"
|
| | | "PRIMARY KEY(account_login, account_server)"
|
| | | ");";
|
| | |
|
2026-08-31 19:38:11 +03:00 | | | //--- Create the settings table when it does not already exist
|
2026-08-30 14:24:21 +03:00 | | | ResetLastError();
|
| | |
|
| | | if(!DatabaseExecute(g_database,
|
| | | settingsTable))
|
| | | {
|
| | | Print("Prop Firm Guard: Could not create settings table. Error: ",
|
| | | GetLastError());
|
| | |
|
| | | return(false);
|
| | | }
|
| | |
|
| | | return(true);
|
| | | }
|
2026-08-31 18:18:41 +03:00 | | |
|
2026-08-30 14:24:21 +03:00 | | | //+------------------------------------------------------------------+
|
| | | //| Finalize database request |
|
| | | //+------------------------------------------------------------------+
|
| | | void FinalizeDatabaseRequest(const int request)
|
| | | {
|
2026-08-31 19:38:11 +03:00 | | | //--- Ignore invalid database requests
|
2026-08-30 14:24:21 +03:00 | | | if(request == INVALID_HANDLE)
|
| | | return;
|
| | |
|
2026-08-31 19:38:11 +03:00 | | | //--- Release the prepared database request
|
2026-08-30 14:24:21 +03:00 | | | ResetLastError();
|
| | |
|
| | | DatabaseFinalize(request);
|
| | |
|
| | | int errorCode = GetLastError();
|
| | |
|
| | | if(errorCode != 0)
|
| | | {
|
| | | Print("Prop Firm Guard: Could not finalize database request. Error: ",
|
| | | errorCode);
|
| | | }
|
| | | }
|
2026-08-31 18:18:41 +03:00 | | |
|
2026-08-30 14:24:21 +03:00 | | | //+------------------------------------------------------------------+
|
| | | //| Execute prepared database request |
|
| | | //+------------------------------------------------------------------+
|
| | | bool ExecuteDatabaseRequest(const int request,
|
| | | const string operation)
|
| | | {
|
2026-08-31 19:38:11 +03:00 | | | //--- Execute the prepared database statement
|
2026-08-30 14:24:21 +03:00 | | | ResetLastError();
|
| | |
|
| | | bool result = DatabaseRead(request);
|
| | |
|
| | | int errorCode = GetLastError();
|
| | |
|
2026-08-31 19:38:11 +03:00 | | | //--- Write statements return no result rows after successful execution
|
2026-08-30 14:24:21 +03:00 | | | bool success =
|
| | | (result || errorCode == ERR_DATABASE_NO_MORE_DATA);
|
| | |
|
| | | if(!success)
|
| | | {
|
| | | Print("Prop Firm Guard: Database error while ",
|
| | | operation,
|
| | | ". Error: ",
|
| | | errorCode);
|
| | | }
|
| | |
|
2026-08-31 19:38:11 +03:00 | | | //--- Release the request after execution
|
2026-08-30 14:24:21 +03:00 | | | FinalizeDatabaseRequest(request);
|
| | |
|
| | | return(success);
|
| | | }
|
2026-08-31 18:18:41 +03:00 | | |
|
2026-08-30 19:24:48 +03:00 | | | //+------------------------------------------------------------------+
|
| | | //| Save account settings |
|
| | | //+------------------------------------------------------------------+
|
| | | bool SaveAccountSettings()
|
| | | {
|
2026-08-31 19:38:11 +03:00 | | | //--- Prepare the account-settings write statement
|
2026-08-30 19:24:48 +03:00 | | | string sql =
|
| | | "INSERT OR REPLACE INTO settings ("
|
| | | "account_login,"
|
| | | "account_server,"
|
| | | "initial_balance,"
|
| | | "daily_drawdown_percent,"
|
| | | "overall_drawdown_percent,"
|
| | | "profit_target_percent,"
|
| | | "updated_at"
|
| | | ") VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7);";
|
| | |
|
| | | int request = DatabasePrepare(g_database, sql);
|
| | |
|
| | | if(request == INVALID_HANDLE)
|
| | | {
|
| | | Print("Prop Firm Guard: Could not prepare settings save request. Error: ",
|
| | | GetLastError());
|
| | |
|
| | | return(false);
|
| | | }
|
| | |
|
2026-08-31 19:38:11 +03:00 | | | //--- Bind the current account settings to the prepared statement
|
2026-08-30 19:24:48 +03:00 | | | if(!DatabaseBind(request, 0, g_accountSettings.accountLogin) ||
|
| | | !DatabaseBind(request, 1, g_accountSettings.accountServer) ||
|
| | | !DatabaseBind(request, 2, g_accountSettings.initialBalance) ||
|
| | | !DatabaseBind(request, 3, g_accountSettings.dailyDrawdownPercent) ||
|
| | | !DatabaseBind(request, 4, g_accountSettings.overallDrawdownPercent) ||
|
| | | !DatabaseBind(request, 5, g_accountSettings.profitTargetPercent) ||
|
| | | !DatabaseBind(request, 6, g_accountSettings.updatedAt))
|
| | | {
|
| | | Print("Prop Firm Guard: Could not bind settings values. Error: ",
|
| | | GetLastError());
|
| | |
|
| | | FinalizeDatabaseRequest(request);
|
| | | return(false);
|
| | | }
|
| | |
|
2026-08-31 19:38:11 +03:00 | | | //--- Execute and finalize the prepared write request
|
2026-08-30 19:24:48 +03:00 | | | if(!ExecuteDatabaseRequest(request, "saving account settings"))
|
| | | return(false);
|
| | |
|
| | | return(true);
|
| | | }
|
2026-08-31 18:18:41 +03:00 | | |
|
2026-08-30 19:24:48 +03:00 | | | //+------------------------------------------------------------------+
|
| | | //| Load account settings |
|
| | | //+------------------------------------------------------------------+
|
| | | bool LoadAccountSettings()
|
| | | {
|
2026-08-31 19:38:11 +03:00 | | | //--- Build the current account/server lookup
|
2026-08-30 19:24:48 +03:00 | | | string accountServer = AccountInfoString(ACCOUNT_SERVER);
|
| | |
|
| | | string sql =
|
| | | "SELECT "
|
| | | "account_login,"
|
| | | "account_server,"
|
| | | "initial_balance,"
|
| | | "daily_drawdown_percent,"
|
| | | "overall_drawdown_percent,"
|
| | | "profit_target_percent,"
|
| | | "updated_at "
|
| | | "FROM settings "
|
| | | "WHERE account_login = ?1 AND account_server = ?2;";
|
| | |
|
| | | int request = DatabasePrepare(g_database, sql);
|
| | |
|
| | | if(request == INVALID_HANDLE)
|
| | | {
|
| | | Print("Prop Firm Guard: Could not prepare settings load request. Error: ",
|
| | | GetLastError());
|
| | |
|
| | | return(false);
|
| | | }
|
| | |
|
2026-08-31 19:38:11 +03:00 | | | //--- Bind the current account identity
|
2026-08-30 19:24:48 +03:00 | | | if(!DatabaseBind(request, 0, g_accountState.login) ||
|
| | | !DatabaseBind(request, 1, accountServer))
|
| | | {
|
| | | Print("Prop Firm Guard: Could not bind settings search values. Error: ",
|
| | | GetLastError());
|
| | |
|
| | | FinalizeDatabaseRequest(request);
|
| | | return(false);
|
| | | }
|
| | |
|
2026-08-31 19:38:11 +03:00 | | | //--- Read the matching settings record
|
2026-08-30 19:24:48 +03:00 | | | ResetLastError();
|
| | |
|
| | | if(!DatabaseRead(request))
|
| | | {
|
| | | int errorCode = GetLastError();
|
| | |
|
| | | FinalizeDatabaseRequest(request);
|
| | |
|
2026-08-31 19:38:11 +03:00 | | | //--- No row means this account/server pair has not been saved yet
|
2026-08-30 19:24:48 +03:00 | | | if(errorCode == ERR_DATABASE_NO_MORE_DATA)
|
| | | return(false);
|
| | |
|
| | | Print("Prop Firm Guard: Could not read account settings. Error: ",
|
| | | errorCode);
|
| | |
|
| | | return(false);
|
| | | }
|
| | |
|
2026-08-31 19:38:11 +03:00 | | | //--- Read and validate the stored column values
|
2026-08-30 19:24:48 +03:00 | | | long accountLogin;
|
| | | string storedServer;
|
| | | double initialBalance;
|
| | | double dailyDrawdown;
|
| | | double overallDrawdown;
|
| | | double profitTarget;
|
| | | long updatedAt;
|
| | |
|
| | | if(!DatabaseColumnLong(request, 0, accountLogin) ||
|
| | | !DatabaseColumnText(request, 1, storedServer) ||
|
| | | !DatabaseColumnDouble(request, 2, initialBalance) ||
|
| | | !DatabaseColumnDouble(request, 3, dailyDrawdown) ||
|
| | | !DatabaseColumnDouble(request, 4, overallDrawdown) ||
|
| | | !DatabaseColumnDouble(request, 5, profitTarget) ||
|
| | | !DatabaseColumnLong(request, 6, updatedAt))
|
| | | {
|
| | | Print("Prop Firm Guard: Could not read stored settings columns. Error: ",
|
| | | GetLastError());
|
| | |
|
| | | FinalizeDatabaseRequest(request);
|
| | | return(false);
|
| | | }
|
| | |
|
2026-08-31 19:38:11 +03:00 | | | //--- Restore the stored values into the account settings structure
|
2026-08-30 19:24:48 +03:00 | | | g_accountSettings.accountLogin = accountLogin;
|
| | | g_accountSettings.accountServer = storedServer;
|
| | | g_accountSettings.initialBalance = initialBalance;
|
| | | g_accountSettings.dailyDrawdownPercent = dailyDrawdown;
|
| | | g_accountSettings.overallDrawdownPercent = overallDrawdown;
|
| | | g_accountSettings.profitTargetPercent = profitTarget;
|
| | | g_accountSettings.updatedAt = (datetime)updatedAt;
|
| | |
|
2026-08-31 19:38:11 +03:00 | | | //--- Release the completed read request
|
2026-08-30 19:24:48 +03:00 | | | FinalizeDatabaseRequest(request);
|
| | |
|
| | | return(true);
|
| | | }
|
2026-08-31 18:18:41 +03:00 | | |
|
2026-08-30 19:24:48 +03:00 | | | //+------------------------------------------------------------------+
|
| | | //| Initialize account settings |
|
| | | //+------------------------------------------------------------------+
|
| | | bool InitializeAccountSettings()
|
| | | {
|
2026-08-31 19:38:11 +03:00 | | | //--- Check whether this account/server pair already has stored settings
|
2026-08-30 19:24:48 +03:00 | | | bool settingsFound = LoadAccountSettings();
|
| | |
|
2026-08-31 19:38:11 +03:00 | | | //--- Associate the settings with the current trading account
|
2026-08-30 19:24:48 +03:00 | | | g_accountSettings.accountLogin = g_accountState.login;
|
| | | g_accountSettings.accountServer = AccountInfoString(ACCOUNT_SERVER);
|
| | |
|
2026-08-31 19:38:11 +03:00 | | | //--- Synchronize persistence with the current Inputs values
|
2026-08-30 19:24:48 +03:00 | | | g_accountSettings.initialBalance = InpInitialAccountBalance;
|
| | | g_accountSettings.dailyDrawdownPercent = InpDailyDrawdownPercent;
|
| | | g_accountSettings.overallDrawdownPercent = InpOverallDrawdownPercent;
|
| | | g_accountSettings.profitTargetPercent = InpProfitTargetPercent;
|
| | | g_accountSettings.updatedAt = TimeCurrent();
|
| | |
|
2026-08-31 19:38:11 +03:00 | | | //--- Save the synchronized configuration
|
2026-08-30 19:24:48 +03:00 | | | if(!SaveAccountSettings())
|
| | | return(false);
|
| | |
|
2026-08-31 19:38:11 +03:00 | | | //--- Report whether the record was created or synchronized
|
2026-08-30 19:24:48 +03:00 | | | if(settingsFound)
|
| | | Print("Prop Firm Guard: Account settings restored and synchronized.");
|
| | | else
|
| | | Print("Prop Firm Guard: New account settings saved.");
|
| | |
|
| | | return(true);
|
| | | }
|
2026-08-15 02:44:23 -07:00 | | |
|
2026-08-13 09:00:54 -07:00 | | | //+------------------------------------------------------------------+
|
| | | //| Expert initialization function |
|
| | | //+------------------------------------------------------------------+
|
| | | int OnInit()
|
| | | {
|
2026-08-31 19:38:11 +03:00 | | | //--- Validate the future monitoring interval
|
2026-08-30 21:30:12 +03:00 | | | if(InpTimerSeconds < 1)
|
| | | {
|
| | | Print("Prop Firm Guard: Timer interval must be at least 1 second.");
|
| | |
|
| | | return(INIT_PARAMETERS_INCORRECT);
|
| | | }
|
| | |
|
2026-08-31 19:38:11 +03:00 | | | //--- Reject invalid compliance rules before initialization continues
|
2026-08-30 12:36:36 +03:00 | | | if(!ValidateComplianceInputs())
|
| | | return(INIT_PARAMETERS_INCORRECT);
|
| | |
|
2026-08-31 19:38:11 +03:00 | | | //--- Collect the current account identity and financial state
|
2026-08-30 11:53:44 +03:00 | | | UpdateAccountState();
|
| | |
|
2026-08-31 19:38:11 +03:00 | | | //--- Open the SQLite persistence layer
|
2026-08-30 13:06:01 +03:00 | | | if(!OpenDatabase())
|
| | | {
|
| | | Print("Prop Firm Guard: Database initialization failed.");
|
| | |
|
| | | return(INIT_FAILED);
|
| | | }
|
| | |
|
2026-08-31 19:38:11 +03:00 | | | //--- Ensure the required persistence schema exists
|
2026-08-30 14:24:21 +03:00 | | | if(!EnsureDatabaseTables())
|
| | | {
|
| | | Print("Prop Firm Guard: Could not initialize database tables.");
|
| | |
|
| | | CloseDatabase();
|
| | |
|
| | | return(INIT_FAILED);
|
| | | }
|
| | |
|
2026-08-31 19:38:11 +03:00 | | | //--- Restore and synchronize the account-specific settings
|
2026-08-30 19:24:48 +03:00 | | | if(!InitializeAccountSettings())
|
| | | {
|
| | | Print("Prop Firm Guard: Could not initialize account settings.");
|
| | |
|
| | | CloseDatabase();
|
| | |
|
| | | return(INIT_FAILED);
|
| | | }
|
| | |
|
2026-08-30 21:30:12 +03:00 | | | Print("Prop Firm Guard initialized successfully.");
|
| | |
|
2026-08-13 09:00:54 -07:00 | | | return(INIT_SUCCEEDED);
|
| | | }
|
2026-08-15 02:44:23 -07:00 | | |
|
2026-08-13 09:00:54 -07:00 | | | //+------------------------------------------------------------------+
|
| | | //| Expert deinitialization function |
|
| | | //+------------------------------------------------------------------+
|
| | | void OnDeinit(const int reason)
|
| | | {
|
2026-08-31 19:38:11 +03:00 | | | //--- Close the SQLite connection
|
2026-08-30 13:06:01 +03:00 | | | CloseDatabase();
|
| | |
|
2026-08-31 19:38:11 +03:00 | | | //--- Record the MetaTrader deinitialization reason
|
2026-08-30 13:06:01 +03:00 | | | Print("Prop Firm Guard removed. Deinitialization reason: ", reason);
|
2026-08-13 09:00:54 -07:00 | | | }
|
2026-08-15 02:44:23 -07:00 | | |
|
2026-08-13 09:00:54 -07:00 | | | //+------------------------------------------------------------------+
|
| | | //| Expert tick function |
|
| | | //+------------------------------------------------------------------+
|
| | | void OnTick()
|
| | | {
|
2026-08-31 18:18:41 +03:00 | | | }
|
2026-08-31 19:38:11 +03:00 | | | //+------------------------------------------------------------------+
|