|
1 | 1 | //+------------------------------------------------------------------+ |
2 | 2 | //| Account Protector.mqh | |
3 | | -//| Copyright © 2017-2023, EarnForex.com | |
| 3 | +//| Copyright © 2017-2024, EarnForex.com | |
4 | 4 | //| https://www.earnforex.com/ | |
5 | 5 | //+------------------------------------------------------------------+ |
6 | 6 | #include "Defines.mqh" |
|
11 | 11 | int GetAncestor(int, int); |
12 | 12 | #import |
13 | 13 |
|
14 | | -//+------------------------------------------------------------------+ |
15 | | -//| | |
16 | | -//+------------------------------------------------------------------+ |
17 | 14 | class CAccountProtector : public CAppDialog |
18 | 15 | { |
19 | 16 | private: |
@@ -634,7 +631,10 @@ bool CAccountProtector::CreateObjects() |
634 | 631 | if (!CheckBoxCreate(m_ChkBreakEvenExtra, first_column_start, y, panel_end, y + element_height, "m_ChkBreakEvenExtra", "Breakeven extra profit value (points):")) return false; |
635 | 632 | if (!EditCreate(m_EdtBreakEvenExtra, last_input_start, y, last_input_end, y + element_height, "m_EdtBreakEvenExtra", "0")) return false; |
636 | 633 | y += element_height + v_spacing; |
637 | | - if (!CheckBoxCreate(m_ChkEquityTrailingStop, first_column_start, y, panel_end, y + element_height, "m_ChkEquityTrailingStop", "Equity trailing stop (hidden), USD:")) return false; |
| 634 | + string ets = "Equity trailing stop (hidden), "; |
| 635 | + if (EquityTrailingStopInPercentage) ets += "%:"; |
| 636 | + else ets += "USD:"; |
| 637 | + if (!CheckBoxCreate(m_ChkEquityTrailingStop, first_column_start, y, panel_end, y + element_height, "m_ChkEquityTrailingStop", ets)) return false; |
638 | 638 | if (!EditCreate(m_EdtEquityTrailingStop, last_input_start, y, last_input_end, y + element_height, "m_EdtEquityTrailingStop", "0")) return false; |
639 | 639 | y += element_height + v_spacing; |
640 | 640 | if (!LabelCreate(m_LblCurrentEquityStopLoss, first_column_start, y, first_column_start + normal_label_width, y + element_height, "m_LblCurrentEquityStopLoss", "Current equity stop-loss, USD: ")) return false; |
@@ -1160,11 +1160,12 @@ bool CAccountProtector::RefreshValues() |
1160 | 1160 | string account_currency = AccountCurrency(); |
1161 | 1161 | if (account_currency != "") |
1162 | 1162 | { |
1163 | | - m_ChkEquityTrailingStop.Text("Equity trailing stop (hidden), " + account_currency + ":"); |
| 1163 | + if (!EquityTrailingStopInPercentage) m_ChkEquityTrailingStop.Text("Equity trailing stop (hidden), " + account_currency + ":"); |
1164 | 1164 | if (BreakEvenProfitInCurrencyUnits) |
1165 | 1165 | { |
1166 | 1166 | m_ChkBreakEven.Text("Profit value (" + account_currency + ") to set SL to breakeven:"); |
1167 | 1167 | } |
| 1168 | + if ((sets.boolEquityTrailingStop) && (sets.doubleEquityTrailingStop > 0)) m_LblCurrentEquityStopLoss.Text("Current equity stop-loss: " + DoubleToString(sets.doubleCurrentEquityStopLoss, 2) + " " + account_currency + "."); |
1168 | 1169 | } |
1169 | 1170 | if (IsANeedToContinueClosingOrders) Close_All_Positions(); |
1170 | 1171 | if (IsANeedToContinueDeletingPendingOrders) Delete_All_Pending_Orders(); |
@@ -3743,9 +3744,14 @@ void CAccountProtector::HideShowMaximize(bool max = true) |
3743 | 3744 | void CAccountProtector::Check_Status() |
3744 | 3745 | { |
3745 | 3746 | if (sets.Triggered) return; |
3746 | | - if ((!TerminalInfoInteger(TERMINAL_TRADE_ALLOWED)) || (!MQLInfoInteger(MQL_TRADE_ALLOWED))) |
| 3747 | + if (!TerminalInfoInteger(TERMINAL_TRADE_ALLOWED)) |
| 3748 | + { |
| 3749 | + m_LblStatus.Text("Status: Autotrading is disabled (platform)."); |
| 3750 | + return; |
| 3751 | + } |
| 3752 | + else if (!MQLInfoInteger(MQL_TRADE_ALLOWED)) |
3747 | 3753 | { |
3748 | | - m_LblStatus.Text("Status: Autotrading is disabled."); |
| 3754 | + m_LblStatus.Text("Status: Autotrading is disabled (EA)."); |
3749 | 3755 | return; |
3750 | 3756 | } |
3751 | 3757 | else if ((!MQLInfoInteger(MQL_DLLS_ALLOWED)) && ((sets.DisAuto) || (sets.EnableAuto))) |
@@ -4462,10 +4468,12 @@ void CAccountProtector::EquityTrailing() |
4462 | 4468 | MoveAndResize(); |
4463 | 4469 | } |
4464 | 4470 | // If equity stop-loss should be trailed - update the stop-loss. |
4465 | | - else if ((AE - sets.doubleEquityTrailingStop > sets.doubleCurrentEquityStopLoss) || (sets.doubleCurrentEquityStopLoss == 0)) |
| 4471 | + if (((!EquityTrailingStopInPercentage) && ((AE - sets.doubleEquityTrailingStop > sets.doubleCurrentEquityStopLoss) || (sets.doubleCurrentEquityStopLoss == 0))) || // Currency equity trailing stop should be moved. |
| 4472 | + ((EquityTrailingStopInPercentage) && ((AE * (1 - sets.doubleEquityTrailingStop / 100.0) > sets.doubleCurrentEquityStopLoss) || (sets.doubleCurrentEquityStopLoss == 0)))) // Percentage equity trailing stop should be moved. |
4466 | 4473 | { |
4467 | 4474 | double old_value = sets.doubleCurrentEquityStopLoss; |
4468 | | - sets.doubleCurrentEquityStopLoss = AE - sets.doubleEquityTrailingStop; |
| 4475 | + if (!EquityTrailingStopInPercentage) sets.doubleCurrentEquityStopLoss = AE - sets.doubleEquityTrailingStop; // $ |
| 4476 | + else sets.doubleCurrentEquityStopLoss = AE * (1 - sets.doubleEquityTrailingStop / 100.0); // % |
4469 | 4477 | SaveSettingsOnDisk(); |
4470 | 4478 | string account_currency = AccountCurrency(); |
4471 | 4479 | if (account_currency != "") m_LblCurrentEquityStopLoss.Text("Current equity stop-loss: " + DoubleToString(sets.doubleCurrentEquityStopLoss, 2) + " " + account_currency + "."); |
@@ -4958,29 +4966,44 @@ void CAccountProtector::CheckAllConditions() |
4958 | 4966 | if ((!DisableSpreadLE) && (SymbolInfoInteger(Symbol(), SYMBOL_SPREAD) <= sets.intSpreadLE)) |
4959 | 4967 | CheckOneCondition(sets.intSpreadLE, sets.boolSpreadLE, "Spread less or equal to " + IntegerToString(sets.intSpreadLE)); |
4960 | 4968 |
|
4961 | | -// Daily profit/loss greater or equal to <value> currency units. |
4962 | | - if ((!DisableDailyProfitLossUnitsGE) && (daily_profit_loss_units >= sets.doubleDailyProfitLossUnitsGE)) |
4963 | | - CheckOneCondition(sets.doubleDailyProfitLossUnitsGE, sets.boolDailyProfitLossUnitsGE, "Daily profit/loss greater or equal to " + DoubleToString(sets.doubleDailyProfitLossUnitsGE, 2) + " " + AccountInfoString(ACCOUNT_CURRENCY)); |
| 4969 | + bool CheckDailyConditions = true; |
| 4970 | + if ((DoNotDisableConditions) && (DoNotDisableActions)) // Two main circumstances that could lead to infinite attempts to do something. |
| 4971 | + { |
| 4972 | + if (((!sets.ClosePos) || ((sets.ClosePos) && (market == 0))) && // Close all, but there is nothing to close. |
| 4973 | + ((!sets.DeletePend) || ((sets.DeletePend) && (pending == 0))) && // Delete all pending, but there is nothing to delete. |
| 4974 | + ((!sets.DisAuto) || ((sets.DisAuto) && (!TerminalInfoInteger(TERMINAL_TRADE_ALLOWED)))) && // Disable AT, but it's already disabled. |
| 4975 | + (!sets.ClosePlatform) && (!sets.EnableAuto) && (!sets.CloseAllOtherCharts) && (!sets.RecaptureSnapshots)) // All other actions (except notifications should be disabled). |
| 4976 | + { |
| 4977 | + CheckDailyConditions = false; // No need to check daily conditions. |
| 4978 | + } |
| 4979 | + } |
4964 | 4980 |
|
| 4981 | + if (CheckDailyConditions) |
| 4982 | + { |
| 4983 | +// Daily profit/loss greater or equal to <value> currency units. |
| 4984 | + if ((!DisableDailyProfitLossUnitsGE) && (daily_profit_loss_units >= sets.doubleDailyProfitLossUnitsGE)) |
| 4985 | + CheckOneCondition(sets.doubleDailyProfitLossUnitsGE, sets.boolDailyProfitLossUnitsGE, "Daily profit/loss greater or equal to " + DoubleToString(sets.doubleDailyProfitLossUnitsGE, 2) + " " + AccountInfoString(ACCOUNT_CURRENCY)); |
| 4986 | + |
4965 | 4987 | // Daily profit/loss less or equal to <value> currency units. |
4966 | | - if ((!DisableDailyProfitLossUnitsLE) && (daily_profit_loss_units <= sets.doubleDailyProfitLossUnitsLE)) |
4967 | | - CheckOneCondition(sets.doubleDailyProfitLossUnitsLE, sets.boolDailyProfitLossUnitsLE, "Daily profit/loss less or equal to " + DoubleToString(sets.doubleDailyProfitLossUnitsLE, 2) + " " + AccountInfoString(ACCOUNT_CURRENCY)); |
4968 | | - |
| 4988 | + if ((!DisableDailyProfitLossUnitsLE) && (daily_profit_loss_units <= sets.doubleDailyProfitLossUnitsLE)) |
| 4989 | + CheckOneCondition(sets.doubleDailyProfitLossUnitsLE, sets.boolDailyProfitLossUnitsLE, "Daily profit/loss less or equal to " + DoubleToString(sets.doubleDailyProfitLossUnitsLE, 2) + " " + AccountInfoString(ACCOUNT_CURRENCY)); |
| 4990 | + |
4969 | 4991 | // Daily profit/loss greater or equal to <value> points. |
4970 | | - if ((!DisableDailyProfitLossPointsGE) && (daily_profit_loss_points >= sets.intDailyProfitLossPointsGE)) |
4971 | | - CheckOneCondition(sets.intDailyProfitLossPointsGE, sets.boolDailyProfitLossPointsGE, "Daily profit/loss greater or equal to " + IntegerToString(sets.intDailyProfitLossPointsGE)); |
4972 | | - |
| 4992 | + if ((!DisableDailyProfitLossPointsGE) && (daily_profit_loss_points >= sets.intDailyProfitLossPointsGE)) |
| 4993 | + CheckOneCondition(sets.intDailyProfitLossPointsGE, sets.boolDailyProfitLossPointsGE, "Daily profit/loss greater or equal to " + IntegerToString(sets.intDailyProfitLossPointsGE)); |
| 4994 | + |
4973 | 4995 | // Daily profit/loss less or equal to <value> points. |
4974 | | - if ((!DisableDailyProfitLossPointsLE) && (daily_profit_loss_points <= sets.intDailyProfitLossPointsLE)) |
4975 | | - CheckOneCondition(sets.intDailyProfitLossPointsLE, sets.boolDailyProfitLossPointsLE, "Daily profit/loss less or equal to " + IntegerToString(sets.intDailyProfitLossPointsLE)); |
4976 | | - |
| 4996 | + if ((!DisableDailyProfitLossPointsLE) && (daily_profit_loss_points <= sets.intDailyProfitLossPointsLE)) |
| 4997 | + CheckOneCondition(sets.intDailyProfitLossPointsLE, sets.boolDailyProfitLossPointsLE, "Daily profit/loss less or equal to " + IntegerToString(sets.intDailyProfitLossPointsLE)); |
| 4998 | + |
4977 | 4999 | // Daily profit/loss greater or equal to <value> %. |
4978 | | - if ((!DisableDailyProfitLossPercGE) && (daily_profit_loss_perc >= sets.doubleDailyProfitLossPercGE)) |
4979 | | - CheckOneCondition(sets.doubleDailyProfitLossPercGE, sets.boolDailyProfitLossPercGE, "Daily profit/loss greater or equal to " + DoubleToString(sets.doubleDailyProfitLossPercGE, 2) + "% of balance"); |
4980 | | - |
| 5000 | + if ((!DisableDailyProfitLossPercGE) && (daily_profit_loss_perc >= sets.doubleDailyProfitLossPercGE)) |
| 5001 | + CheckOneCondition(sets.doubleDailyProfitLossPercGE, sets.boolDailyProfitLossPercGE, "Daily profit/loss greater or equal to " + DoubleToString(sets.doubleDailyProfitLossPercGE, 2) + "% of balance"); |
| 5002 | + |
4981 | 5003 | // Daily profit/loss less or equal to <value> %. |
4982 | | - if ((!DisableDailyProfitLossPercLE) && (daily_profit_loss_perc <= sets.doubleDailyProfitLossPercLE)) |
4983 | | - CheckOneCondition(sets.doubleDailyProfitLossPercLE, sets.boolDailyProfitLossPercLE, "Daily profit/loss less or equal to " + DoubleToString(sets.doubleDailyProfitLossPercLE, 2) + "% of balance"); |
| 5004 | + if ((!DisableDailyProfitLossPercLE) && (daily_profit_loss_perc <= sets.doubleDailyProfitLossPercLE)) |
| 5005 | + CheckOneCondition(sets.doubleDailyProfitLossPercLE, sets.boolDailyProfitLossPercLE, "Daily profit/loss less or equal to " + DoubleToString(sets.doubleDailyProfitLossPercLE, 2) + "% of balance"); |
| 5006 | + } |
4984 | 5007 |
|
4985 | 5008 | // Number of positions is greater or equal to <value>. |
4986 | 5009 | if ((!DisableNumberOfPositionsGE) && (market >= sets.intNumberOfPositionsGE)) |
@@ -5153,7 +5176,7 @@ void CAccountProtector::Logging(string message) |
5153 | 5176 | } |
5154 | 5177 | else Alert("Unexpected error accessing file: ", filename, "."); |
5155 | 5178 | } |
5156 | | - if (!SilentLogging) Print(message); |
| 5179 | + if ((!Silent) && (!SilentLogging)) Print(message); |
5157 | 5180 | } |
5158 | 5181 |
|
5159 | 5182 | // Creates array of Magic numbers and updates its counter. |
|
0 commit comments