From 59eb5e86a2479fce58b9178440ba3a30d5a6a10c Mon Sep 17 00:00:00 2001 From: Hadi Khazaee Asl Date: Tue, 1 Oct 2024 18:41:03 +0330 Subject: [PATCH] Last Apply Props and Fix on Protection Handlers ... --- .../x-saherelm.x121.xtrade.handler.class.mq5 | Bin 160344 -> 162340 bytes Experts/x-saherelm.x121.ea.mq5 | 204 ++++++++++++++---- 2 files changed, 164 insertions(+), 40 deletions(-) diff --git a/Classes/x-saherelm.x121.xtrade.handler.class.mq5 b/Classes/x-saherelm.x121.xtrade.handler.class.mq5 index 4f7853c03010e6cfd6040f705cfe630d412398e6..39c45286c99018797ba6e89fecf49ebe53082d5e 100644 GIT binary patch delta 613 zcmaJ;OD{uV5S{td)!raYZ(SlDUxd;U(S}%P7am1I6VH7~*m<+C5s#&fWF*sw3em() zu(=!3)=I=ig!ln|0Ev}|xNS)+s*6d!%$zyjnRA|BY_WISSw)-@AuD|yIQe=4oxC@R zE}B2qsX1Dl?vVOg%w1Ys7IVG&lL6P7qRfhBpP zFi3Cv?R0pg6lEXtLIHNc5|+>lb7s~OGLFr`G;fE5o9%rW2^6PLhW0S=ciegOuz{MP{v1Cv-E9JdhM S0bW$MCNTn#BDdaD0!AeuAQ#I3 diff --git a/Experts/x-saherelm.x121.ea.mq5 b/Experts/x-saherelm.x121.ea.mq5 index ce50beb7..2898f34a 100644 --- a/Experts/x-saherelm.x121.ea.mq5 +++ b/Experts/x-saherelm.x121.ea.mq5 @@ -126,6 +126,8 @@ input group "In Drawdown Positions Protecting"; input group "Protect"; input bool eaAllowProtect = true; // Allow Proptecting input int eaProtectionDelay = 300; // Delay Between two Protection in Secconds +input int eaMaxAllowedProtection = 1; // Max Allowed Protection +input bool eaUseEntryAsProtectionSL = true; // Use Position Entry For SL of Protect input bool eaProtectOnConditions = true; // Do Protection only when Conditions happens input bool eaAllowDirectionProtct = false; // Allow also Directional Protect input double eaProtectionStartDistance = 100; // Protection Start Drawdown in Point @@ -219,8 +221,10 @@ XCEdit eaStaticVolumeTb; XCLabel eaStaticVolumeLbl; XCButton eaSetStaticVolumeBtn; XCButton eaResetStaticVolumeBtn; -bool eaAppliedStaticVolume = false; -bool eEnableDynamicVolumeVolume = false; +double eaAppliedStaticVolume = eaVolume; +bool eEnableDynamicVolumeVolume = eaUseDynamicVolume && + eaDynamicVolumeStep > 0 && + eaDynamicVolumeBalanceFactor > 0; // // Event Handlers ... @@ -593,6 +597,8 @@ bool InitEA() // Protect ... eaTradeHandler.AllowProtect(eaAllowProtect); eaTradeHandler.ProtectionDelay(eaProtectionDelay); + eaTradeHandler.MaxAllowedProtection(eaMaxAllowedProtection); + eaTradeHandler.UseEntryAsProtectionSL(eaUseEntryAsProtectionSL); eaTradeHandler.ProtectOnConditions(eaProtectOnConditions); eaTradeHandler.AllowDirectionProtct(eaAllowDirectionProtct); eaTradeHandler.ProtectionStartDistance(eaProtectionStartDistance); @@ -1129,36 +1135,8 @@ void OnSignalRecieved( } // - // Prepare Signal Dynamic Volume - // if Provided ... - if (eaUseDynamicVolume && - eaDynamicVolumeStep > 0 && - eaDynamicVolumeBalanceFactor > 0) - { - // - double dVolume = eaTrader.GetDynamicVolume( - signal.symbol, - eaDynamicVolumeBalanceFactor, - eaDynamicVolumeStep // - ); - - // - if (dVolume > 0) - { - signal.volume = dVolume; - } - else - { - signal.volume = eaVolume; - } - - // - eaTradeHandler.UpdateSignal(signal); - } - else - { - signal.volume = eaVolume; - } + // Apply Volume On Signal ... + ApplyVolumeOnSignal(signal); // // Chekc Signalling Enable or not ... @@ -1320,6 +1298,46 @@ void RegisterStrategy(XSCBaseStrategy *strategy) strategies[ArraySize(strategies) - 1] = strategy; } +/** + * Apply Volume Based On Configurations on Signal ... + * + * @param signal: XSignal instance + */ +void ApplyVolumeOnSignal(XSignal &signal) +{ + // + // Prepare Signal Dynamic Volume + // if Provided ... + if (eEnableDynamicVolumeVolume && + eaDynamicVolumeStep > 0 && + eaDynamicVolumeBalanceFactor > 0) + { + // + double dVolume = eaTrader.GetDynamicVolume( + signal.symbol, + eaDynamicVolumeBalanceFactor, + eaDynamicVolumeStep // + ); + + // + if (dVolume > 0) + { + signal.volume = dVolume; + } + else + { + signal.volume = eaAppliedStaticVolume; + } + } + else + { + signal.volume = eaAppliedStaticVolume; + } + + // + eaTradeHandler.UpdateSignal(signal); +} + // // GUI Parts ... @@ -1914,9 +1932,9 @@ bool InitVolumeGUI() string eaStaticVolumeTbName = eaVolumePrefix + "STATIC_VOLUME_tb"; result = eaStaticVolumeTb.Create( eaStaticVolumeTbName, - 150, - 250, 160, + 250, + 150, 25 // ); if (!result) @@ -1972,6 +1990,59 @@ bool InitVolumeGUI() */ void UpdateVolumeGUI() { + // + string tbVolume = eaStaticVolumeTb.Text(); + + // + // Set Current Volume if it's Empty ... + if (!IsValid(tbVolume)) + { + eaStaticVolumeTb.Text(ToString(eaVolume)); + } + + // + bool isDefault = ToString(tbVolume) == ToString(eaVolume); + bool isCurrent = ToString(tbVolume) == ToString(eaAppliedStaticVolume); + + // + // Set Static Volume Color ... + color eaSetStaticVolumeBtnColor = (isDefault || + isCurrent) + ? clrGray + : clrGreen; + eaSetStaticVolumeBtn.Color(eaSetStaticVolumeBtnColor); + + // + // Reset Static Volume Color ... + color eaResetStaticVolumeBtnColor = isDefault + ? clrGray + : clrRed; + eaResetStaticVolumeBtn.Color(eaResetStaticVolumeBtnColor); + + // + // Dynamic Volume State ... + + // + // Check Required Props ... + bool isDynamicVolumeActivedProps = + eaDynamicVolumeStep > 0 && + eaDynamicVolumeBalanceFactor > 0; + + // + color eaDynamicVolumeBtnColor = + !isDynamicVolumeActivedProps + ? clrGray + : eEnableDynamicVolumeVolume + ? clrRed + : clrGreen; + string eaDynamicVolumeBtnText = + !isDynamicVolumeActivedProps + ? "Disabled" + : eEnableDynamicVolumeVolume + ? "Deactive Dynamic Volume" + : "Active Dynamic Volume"; + eaDynamicVolumeBtn.Text(eaDynamicVolumeBtnText); + eaDynamicVolumeBtn.Color(eaDynamicVolumeBtnColor); } /** @@ -1990,29 +2061,82 @@ void DestroyVolumeGUI() eaVolumePanel.Destroy(); } +/** + * Toggle Dynamic Volume Usage ... + */ void HandleChangeDynamicVolumeState() { // - UpdateVolumeOnTradeHandler(); + // Check Required Props ... + bool isDynamicVolumeActivedProps = + eaDynamicVolumeStep > 0 && + eaDynamicVolumeBalanceFactor > 0; + + // + if (!isDynamicVolumeActivedProps) + { + return; + } + + // + eEnableDynamicVolumeVolume = !eEnableDynamicVolumeVolume; } +/** + * Change Static Volume ... + */ void HandleChangeStaticVolume() { + // + string tbVolume = eaStaticVolumeTb.Text(); // - UpdateVolumeOnTradeHandler(); + // Set Current Volume if it's Empty ... + if (!IsValid(tbVolume)) + { + return; + } + + // + bool isCurrent = ToString(tbVolume) == ToString(eaAppliedStaticVolume); + if (isCurrent) + { + return; + } + + // + eaAppliedStaticVolume = (double)tbVolume; + if (eaAppliedStaticVolume <= 0) + { + eaAppliedStaticVolume = eaVolume; + } } +/** + * Set Default Static Volume Value ... + */ void HandleResetStaticVolume() { + // + string tbVolume = eaStaticVolumeTb.Text(); // - UpdateVolumeOnTradeHandler(); -} + // Set Current Volume if it's Empty ... + if (!IsValid(tbVolume)) + { + return; + } -void UpdateVolumeOnTradeHandler() -{ + // + bool isDefault = ToString(tbVolume) == ToString(eaVolume); + if (isDefault) + { + return; + } + + // + eaStaticVolumeTb.Text(ToString(eaVolume)); } // \ No newline at end of file