From 493820dd419773dbba7130c6f0c81051695e104b Mon Sep 17 00:00:00 2001 From: Hadi Khazaee Asl Date: Sun, 30 Jun 2024 13:11:44 +0330 Subject: [PATCH] add guard for protecting Margin ... --- Classes/x-saherelm.x121.setup.xea.mq5 | 38 ++++++++++++++++++++++++++- Experts/x-saherelm.x121.ea.mq5 | 4 +++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/Classes/x-saherelm.x121.setup.xea.mq5 b/Classes/x-saherelm.x121.setup.xea.mq5 index e9db38f6..4b593cad 100644 --- a/Classes/x-saherelm.x121.setup.xea.mq5 +++ b/Classes/x-saherelm.x121.setup.xea.mq5 @@ -5289,7 +5289,43 @@ protected: // // Additional Works on Force States ... - // TODO: Complete this ... + + // + double drawdown = GetDrawdownPercent(); + double margin = mTrader.mAccount.GetMargin(); + double callMargin = mTrader.mAccount.GetMarginCall(); + double stopoutMargin = mTrader.mAccount.GetMarginStopOut(); + + // + double workingMargin = MathMax(callMargin, stopoutMargin) * 0.75; + + // + // Checking Call Margin ... + if (drawdown > workingMargin) + { + // + int maxInDPIDX = FindMaxDrawdownIndex( + positions // + ); + if (IsValidIndex(maxInDPIDX)) + { + // + // We Close Max In Drawdown Position to Guard Account ... + const comment = XEQMSupportToken + " Close Max Due Guard ..."; + bool isClosed = Close( + positions[maxInDPIDX].ticket, + comment // + ); + if (isClosed) + { + // + string message = XEQMSupportToken + " Close Position (" + ToString(positions[maxInDPIDX].ticket) + ") Due Guard Account ..."; + + // + Alert(message); + } + } + } } // diff --git a/Experts/x-saherelm.x121.ea.mq5 b/Experts/x-saherelm.x121.ea.mq5 index 36be3ad1..bb50e21e 100644 --- a/Experts/x-saherelm.x121.ea.mq5 +++ b/Experts/x-saherelm.x121.ea.mq5 @@ -154,6 +154,10 @@ int OnInit() return INIT_PARAMETERS_INCORRECT; } + // + double call = x121EA.mTrader.mAccount.GetMarginCall(); + double stopout = x121EA.mTrader.mAccount.GetMarginStopOut(); + // // Init Succeed ... return INIT_SUCCEEDED;