diff --git a/Classes/x-saherelm.x121.xtrade.handler.class.mq5 b/Classes/x-saherelm.x121.xtrade.handler.class.mq5 index 4f7853c0..39c45286 100644 Binary files a/Classes/x-saherelm.x121.xtrade.handler.class.mq5 and b/Classes/x-saherelm.x121.xtrade.handler.class.mq5 differ 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