diff --git a/Classes/x-saherelm.xtrade.class.mq5 b/Classes/x-saherelm.xtrade.class.mq5 index 1163fcc5..eba5d75c 100644 --- a/Classes/x-saherelm.xtrade.class.mq5 +++ b/Classes/x-saherelm.xtrade.class.mq5 @@ -4177,6 +4177,59 @@ public: mTrailBasedOnProfit = value; } + // + // Hedging ... + + // + bool AllowHedging() + { + return mAllowHedging; + } + + // + void AllowHedging(bool value) + { + mAllowHedging = value; + } + + // + double HedgeVolumeFactor() + { + return mHedgeVolumeFactor; + } + + // + void HedgeVolumeFactor(double value) + { + // + if (value < 0) + { + value = 0; + } + + // + mHedgeVolumeFactor = value; + } + + // + double HedgeMinProfitPerVolumeFactor() + { + return mHedgeMinProfitPerVolumeFactor; + } + + // + void HedgeMinProfitPerVolumeFactor(double value) + { + // + if (value < 0) + { + value = 0; + } + + // + mHedgeMinProfitPerVolumeFactor = value; + } + // // Tools ... @@ -4386,11 +4439,57 @@ public: // // Do Hedging ... + if (mAllowHedging) + { + // + // Check Values Validation ... + if (count > 1 && + mHedgeVolumeFactor > 0 && + mHedgeMinProfitPerVolumeFactor > 0) + { + // + double profits = 0; + double commissions = 0; + double requiredProfitsForHedge = 0; + CalculateHedgeData( + profits, + commissions, + requiredProfitsForHedge // + ); + + // + bool canHedge = profits >= requiredProfitsForHedge; + if (canHedge) + { + // + string comment = "Hedging ..."; + + // + XPosition positions[]; + int positionsCount = mTrader.GetPositions(positions); + if (IsValidSize(positionsCount)) + { + // + int closeds = mTrader.Close( + positions, + comment // + ); + if (IsValidSize(closeds)) + { + // + string msg = ToString(positionsCount) + " Closed Due Hedge Action in: " + ToString(profits) + " ..."; + Alert(msg); + } + } + } + } + } // // Do Force Closing ... // + // Loop through Data ... for (int i = 0; i < count; i++) { // @@ -4614,6 +4713,12 @@ protected: double mTrailStartInPoint; // Trail Starts in Points of Profit double mTrailStepInPoint; // Profit Must Grows Point to do Trailling + // + // Hedging ... + bool mAllowHedging; // Hedge Trades + double mHedgeVolumeFactor; // Hedging Volume Factor + double mHedgeMinProfitPerVolumeFactor; // Hedging Min Required Profit Per Volume Factor + // XTradeData mData[]; // Hold Trade Data @@ -4821,6 +4926,57 @@ private: // Save Signal ... mCollector.SaveSignal(item); } + + // + // Calculate Required Data for Hedging ... + void CalculateHedgeData( + double &profits, + double &commissions, + double &requiredProfitsForHedge // + ) + { + // + // Reset Values ... + profits = 0; + commissions = 0; + requiredProfitsForHedge = 0; + + // + // Validate Configurations ... + if (mHedgeVolumeFactor <= 0 || + mHedgeMinProfitPerVolumeFactor <= 0) + { + return; + } + + // + // Check Positions Exists For Hedging ... + int count = Count(); + if (!IsValidSize(count)) + { + return; + } + + // + double swaps = 0; + double volumes = 0; + for (int i = 0; i < count; i++) + { + // + swaps += mData[i].swap; + profits += mData[i].profit; + volumes += mData[i].volume; + commissions += mData[i].commission; + } + + // + // Calculate required Profits for Hedging ... + requiredProfitsForHedge = ((volumes / mHedgeVolumeFactor) * mHedgeMinProfitPerVolumeFactor); + requiredProfitsForHedge += -1 * (swaps); + requiredProfitsForHedge -= commissions; + } + + // }; // \ No newline at end of file diff --git a/Experts/x-test-strategies.ea.mq5 b/Experts/x-test-strategies.ea.mq5 index 3e0dec13..22194b77 100644 --- a/Experts/x-test-strategies.ea.mq5 +++ b/Experts/x-test-strategies.ea.mq5 @@ -71,6 +71,10 @@ XSCBaseStrategy *strategies[]; // Trader Instance Class ... XSCTrade *eaTrader; +// +// Alert Class ... +XSCAlert *eaAlert; + // // Trade Handler Instance Class ... XSCXTradeHandler *eaTradeHandler; @@ -128,6 +132,7 @@ void OnDeinit(const int reason) delete mCCHelper; // + delete eaAlert; delete eaTrader; delete eaTradeHandler; @@ -213,6 +218,11 @@ bool InitialEA() return result; } + // + // Create Instance of Alert Class ... + eaAlert = new XSCAlert(); + eaAlert.SetPrefix(ShortName); + // // Create Trader Instance ... eaTrader = new XSCTrade( @@ -411,7 +421,7 @@ void OnSignalRecieved(XSignal &signal) " on: " + signal.symbol + " in: " + ToString(signal.period) + " Executed Successfully ..."; - Print(msg); + eaAlert.Alert(msg); } }