Skip to content

Commit

Permalink
1.10
Browse files Browse the repository at this point in the history
Warning! The MT5 version now uses UTF-8 settings file format. After you update the Account Protector to version 1.10, it will likely cause a failure to load your old settings file correctly.

1. Added an option to calculate only realized profit/loss in the daily profit/loss conditions (CountFloatingInDailyPL).
2. Added a trigger delay option (ConditionDelay).
3. Added an option to load your custom settings file with panel fields' values (Settings file).
4. Added four new conditions based on the number of positions and pending orders.
5. Added a global ON/OFF state for the panel.
6. Added a new filter to the Filters tab to target long and short trades separately.
7. Changed how you can control the way the positions are sorted before closing.
8. Fixed the file encoding used for the panel's settings file in MT5. It is now UTF-8.
9. Fixed a bug in the MT4 version of the EA that resulted in deposits and withdrawals being counted in the daily profit/loss conditions.
10. Fixed decimal places normalization in some condition fields.
11. Fixed the placement of fields on the Filters tab to avoid overlapping problems in MT4.
12. Optimized the EA initialization and deinitialization phases to significantly improve the loading speed when switching the chart symbol.
13. Removed the PanelOnTopOfChart input parameter. Use the 'Chart on foreground' checkbox in the chart's settings instead.
  • Loading branch information
EarnForex authored Jun 20, 2023
1 parent bfc91b7 commit 39684ac
Show file tree
Hide file tree
Showing 7 changed files with 935 additions and 149 deletions.
65 changes: 50 additions & 15 deletions MQL4/Experts/Account Protector/Account Protector.mq4
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
//+------------------------------------------------------------------+
#property copyright "EarnForex.com"
#property link "https://www.earnforex.com/metatrader-expert-advisors/Account-Protector/"
#property version "1.091"
string Version = "1.091";
#property version "1.10"
string Version = "1.10";
#property strict

#property description "Protects account balance by applying given actions when set conditions trigger."
Expand All @@ -15,15 +15,15 @@ string Version = "1.091";

#include "Account Protector.mqh";

input int Slippage = 2; // Slippage
input string LogFileName = "log.txt"; // Log file name
input string ____Main = "";
input bool EnableEmergencyButton = false; // Enable emergency button
input bool PanelOnTopOfChart = true; // PanelOnTopOfChart: Draw chart as background?
input bool DoNotDisableConditions = false; // DoNotDisableConditions: Don't disable conditions on trigger?
input bool DoNotDisableActions = false; // DoNotDisableActions: Don't disable actions on trigger?
input bool DoNotDisableEquityTS = false; // DoNotDisableEquityTS: Don't disable equity TS on trigger?
input bool DoNotDisableTimer = false; // DoNotDisableTimer: Don't disable timer on trigger?
input bool AlertOnEquityTS = false; // AlertOnEquityTS: Alert when equity trailing stop triggers?
input int ConditionDelay = 0; // ConditionDelay: How long should condition be active to trigger?
input bool CountFloatingInDailyPL = true; // CountFloatingInDailyPL: Count floating P/L in daily P/L?
input string ____Conditions = "";
input bool DisableFloatLossRisePerc = false; // Disable floating loss rises % condition.
input bool DisableFloatLossFallPerc = true; // Disable floating loss falls % condition.
input bool DisableFloatLossRiseCurr = false; // Disable floating loss rises currency units condition.
Expand All @@ -50,25 +50,49 @@ input bool DisableDailyProfitLossPointsGE = true; // Disable daily profit/loss g
input bool DisableDailyProfitLossPointsLE = true; // Disable daily profit/loss level less or equal points condition.
input bool DisableDailyProfitLossPercGE = true; // Disable daily profit/loss greater or equal percentage condition.
input bool DisableDailyProfitLossPercLE = true; // Disable daily profit/loss level less or equal percentage condition.
input bool DisableNumberOfPositionsGE = true; // Disable number of positions greater or equal condition.
input bool DisableNumberOfOrdersGE = true; // Disable number of pending orders greater or equal condition.
input bool DisableNumberOfPositionsLE = true; // Disable number of positions less or equal condition.
input bool DisableNumberOfOrdersLE = true; // Disable number of pending orders less or equal condition.
input string ____Trading = "";
input int DelayOrderClose = 0; // DelayOrderClose: Delay in milliseconds.
input bool UseTotalVolume = false; // UseTotalVolume: enable if trading with many small trades and partial position closing.
input ENUM_CLOSE_TRADES CloseFirst = ENUM_CLOSE_TRADES_DEFAULT; // CloseFirst: Close which trades first?
input bool BreakEvenProfitInCurrencyUnits = false; // BreakEvenProfitInCurrencyUnits: currency instead of points.
input string ____Miscellaneous = "";
input bool AlertOnEquityTS = false; // AlertOnEquityTS: Alert when equity trailing stop triggers?
input double AdditionalFunds = 0; // AdditionalFunds: Added to balance, equity, and free margin.
input string Instruments = ""; // Instruments: Default list of trading instruments for order filtering.
input bool CloseMostDistantFirst = false; // CloseMostDistantFirst: Close most distant trades first?
input bool BreakEvenProfitInCurrencyUnits = false; // BreakEvenProfitInCurrencyUnits: currency instead of points.
input bool GlobalSnapshots = false; // GlobalSnapshots: AP instances share equity & margin snapshots.
input int Slippage = 2; // Slippage
input string LogFileName = "log.txt"; // Log file name
input string SettingsFileName = ""; // Settings file: Load custom panel settings from \Files\ folder.

CAccountProtector ExtDialog;

int DeinitializationReason = -1;

//+------------------------------------------------------------------+
//| Initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
if (DeinitializationReason == REASON_CHARTCHANGE)
{
EventSetTimer(1);
return INIT_SUCCEEDED;
}

MathSrand(GetTickCount() + 2202051901); // Used by CreateInstanceId() in Dialog.mqh (standard library). Keep the second number unique across other panel indicators/EAs.

if (SettingsFileName != "") // Load a custom settings file if given via input parameters.
{
ExtDialog.SetFileName(SettingsFileName);
}

if (!ExtDialog.LoadSettingsFromDisk())
{
sets.OnOff = false;
sets.CountCommSwaps = true;
sets.UseTimer = false;
sets.Timer = TimeToString(TimeCurrent() - 7200, TIME_MINUTES);
Expand Down Expand Up @@ -98,6 +122,7 @@ int OnInit()
}
sets.OrderCommentary = "";
sets.intOrderCommentaryCondition = 0;
sets.intOrderDirection = 0;
sets.MagicNumbers = "";
sets.boolExcludeMagics = false;
sets.intInstrumentFilter = 0;
Expand Down Expand Up @@ -138,6 +163,10 @@ int OnInit()
sets.boolDailyProfitLossPointsLE = false;
sets.boolDailyProfitLossPercGE = false;
sets.boolDailyProfitLossPercLE = false;
sets.boolNumberOfPositionsGE = false;
sets.boolNumberOfOrdersGE = false;
sets.boolNumberOfPositionsLE = false;
sets.boolNumberOfOrdersLE = false;
sets.doubleLossPerBalance = 0;
sets.doubleLossQuanUnits = 0;
sets.intLossPoints = 0;
Expand Down Expand Up @@ -172,6 +201,10 @@ int OnInit()
sets.intDailyProfitLossPointsLE = 0;
sets.doubleDailyProfitLossPercGE= 0;
sets.doubleDailyProfitLossPercLE = 0;
sets.intNumberOfPositionsGE = 0;
sets.intNumberOfOrdersGE = 0;
sets.intNumberOfPositionsLE = 0;
sets.intNumberOfOrdersLE = 0;
sets.ClosePos = true;
sets.doubleClosePercentage = 100;
sets.CloseWhichPositions = All;
Expand Down Expand Up @@ -230,7 +263,6 @@ int OnInit()
sets.boolBreakEvenExtra = false;
}

EventSetTimer(1);
if (!ExtDialog.Create(0, Symbol() + " Account Protector (ver. " + Version + ")", 0, 20, 20)) return(-1);
ExtDialog.Run();
ExtDialog.IniFileLoad();
Expand All @@ -242,19 +274,21 @@ int OnInit()
ExtDialog.RefreshPanelControls();
ExtDialog.RefreshValues();

ChartSetInteger(0, CHART_FOREGROUND, !PanelOnTopOfChart);
EventSetTimer(1);

return(INIT_SUCCEEDED);
return INIT_SUCCEEDED;
}

//+------------------------------------------------------------------+
//| Deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
DeinitializationReason = reason; // Remember reason to avoid recreating the panel in the OnInit() if it is not deleted here.
EventKillTimer();
if ((reason == REASON_REMOVE) || (reason == REASON_CHARTCLOSE) || (reason == REASON_PROGRAM))
{
ExtDialog.DeleteSettingsFile();
if (SettingsFileName == "") ExtDialog.DeleteSettingsFile(); // Only delete settings file if no custom file name is given.
Print("Trying to delete ini file.");
if (!FileIsExist(ExtDialog.IniFileName() + ".dat")) Print("File doesn't exist.");
else if (!FileDelete(ExtDialog.IniFileName() + ".dat")) Print("Failed to delete file: " + ExtDialog.IniFileName() + ".dat. Error: " + IntegerToString(GetLastError()));
Expand All @@ -266,14 +300,13 @@ void OnDeinit(const int reason)
ExtDialog.SilentLogging = false;
ExtDialog.Logging_Current_Settings();
}
else
else if (reason != REASON_CHARTCHANGE)
{
if (reason == REASON_PARAMETERS) GlobalVariableSet("AP-" + IntegerToString(ChartID()) + "-Parameters", 1);
ExtDialog.SaveSettingsOnDisk();
ExtDialog.IniFileSave();
}
ExtDialog.Destroy();
EventKillTimer();
if (reason != REASON_CHARTCHANGE) ExtDialog.Destroy();
}

//+------------------------------------------------------------------+
Expand Down Expand Up @@ -303,6 +336,7 @@ void OnChartEvent(const int id,
void OnTick()
{
ExtDialog.RefreshValues();
if (!sets.OnOff) return;
ExtDialog.Trailing();
ExtDialog.EquityTrailing();
ExtDialog.MoveToBreakEven();
Expand All @@ -316,6 +350,7 @@ void OnTick()
void OnTimer()
{
ExtDialog.RefreshValues();
if (!sets.OnOff) return;
ExtDialog.Trailing();
ExtDialog.EquityTrailing();
ExtDialog.MoveToBreakEven();
Expand Down
Loading

0 comments on commit 39684ac

Please sign in to comment.