From 43ea4edc42ba618cc4eb48f433298288f94674ba Mon Sep 17 00:00:00 2001 From: Hadi Khazaee Asl Date: Sun, 23 Jun 2024 17:11:07 +0330 Subject: [PATCH] last ... --- Classes/x-saherelm.x121.setup.cycle.class.mq5 | 35 +- Classes/x-saherelm.x121.setup.xea.mq5 | 788 +++++++++--------- Documents/BKP/usefull.mq5 | 245 ++++++ Experts/x-saherelm.x121.ea.mq5 | 88 +- 4 files changed, 735 insertions(+), 421 deletions(-) diff --git a/Classes/x-saherelm.x121.setup.cycle.class.mq5 b/Classes/x-saherelm.x121.setup.cycle.class.mq5 index d8ab3043..67aa77f1 100644 --- a/Classes/x-saherelm.x121.setup.cycle.class.mq5 +++ b/Classes/x-saherelm.x121.setup.cycle.class.mq5 @@ -404,6 +404,18 @@ public: return cCycle.GetPeriod(); } + // + bool ForceState() + { + return forceState; + } + + // + void ForceState(bool value) + { + forceState = value; + } + // bool CanProcessBar( ENUM_X_MARKET_CYCLES cycle = X_MARKET_CYCLE_UNKNOWN // @@ -776,6 +788,9 @@ private: // X121SetupInputs inputs; // Cycles Configurations + // + bool forceState; // Force State + // // Bar Trackers ... XBarTracker cBarTracker; @@ -794,7 +809,8 @@ private: // void FillNextBarTime( datetime &next, - XBarTracker &tracker) + XBarTracker &tracker // + ) { // next = iTime( @@ -803,23 +819,6 @@ private: 0) + PeriodSeconds(tracker.period); } - - // // - // bool ProcessCurrentMarket( - // XSignal &signal, - // X121SetupConditions &conditions, - // int &pushers, - // string &provider, - // int barIndex = 0, - // int loopback = 5 // - // ) - // { - // // - // bool result = false; - - // // - // return result; - // } }; // \ No newline at end of file diff --git a/Classes/x-saherelm.x121.setup.xea.mq5 b/Classes/x-saherelm.x121.setup.xea.mq5 index e963eb63..0c9ac0db 100644 --- a/Classes/x-saherelm.x121.setup.xea.mq5 +++ b/Classes/x-saherelm.x121.setup.xea.mq5 @@ -52,17 +52,6 @@ struct XTradeInfo string message; // Close Reason datetime endTime; // End Time - // - int bullishSigns; // Bullish Signs On Signal Time - int bearishSigns; // Bearish Signs On Signal Time - - // - double bullishScore; // Bullish Score On Signal Time - double bearishScore; // Bearish Score On Signal Time - - // - double volumeOSC; // Volume Weight On Signal Time - // X121SetupConditions cConditions; // Generated Current Conditions X121SetupConditions sConditions; // Generated Short Conditions @@ -100,12 +89,7 @@ struct XTradeInfo profit = 0; volume = 0; pushers = 0; - volumeOSC = 0; commission = 0; - bullishSigns = 0; - bearishSigns = 0; - bullishScore = 0; - bearishScore = 0; // cConditions.Clean(); @@ -124,11 +108,9 @@ struct XTradeInfo bool Fill( const XSignal &_signal, int _pushers, - double _volumeOSC, - int _bullishSigns, - int _bearishSigns, - double _bullishScore, - double _bearishScore, + bool _allowGrid, + bool _allowRecover, + bool _allowSupport, X121SetupConditions &_cConditions, X121SetupConditions &_sConditions, X121SetupConditions &_mConditions, @@ -171,17 +153,6 @@ struct XTradeInfo lConditions = _lConditions; hConditions = _hConditions; - // - volumeOSC = _volumeOSC; - - // - bullishSigns = _bullishSigns; - bearishSigns = _bearishSigns; - - // - bullishScore = _bullishScore; - bearishScore = _bearishScore; - // X121SignalGenerator generator; XSignal tSignal = _signal; @@ -191,11 +162,11 @@ struct XTradeInfo ); tSignal.Clean(); int pivotsCount = ArraySize(pivots); - if (!IsValidSize(pivotsCount)) - { - // - Print("Dangerous Position ..."); - } + + // + allowGrid = _allowGrid; + allowRecover = _allowRecover; + allowSupport = _allowSupport; // return result; @@ -394,12 +365,6 @@ struct XTradeInfo ToString("Time", time) + ToString("Pushers", pushers) + "-------------" + "\n" + - ToString("VolumeOSC", volumeOSC) + - ToString("BullishSigns", bullishSigns) + - ToString("BearishSigns", bearishSigns) + - ToString("BullishScore", bullishScore) + - ToString("BearishScore", bearishScore) + - "-------------" + "\n" + ToString("Volume", volume) + ToString("Profit", profit) + ToString("Commission", commission) + @@ -453,7 +418,9 @@ struct XTradeInfo // void ResetSupport() { + // Clean(pivots); + allowSupport = false; } // @@ -637,6 +604,9 @@ struct XTradeInfo gEntry = 0; gLevel = 0; gVolume = 0; + + // + allowGrid = false; } // @@ -787,7 +757,7 @@ struct XTradeInfo // Recovery Zone System ... // - bool allowRecovery; // Allow Recover + bool allowRecover; // Allow Recover ENUM_POSITION_TYPE rLastRecoveryType; // Last Executed Recovery Signal's Type double rLongEntry; // Represent Next Recovery Long Signal's Entry double rShortEntry; // Represent Next Recovery Short Signal's Entry @@ -806,6 +776,9 @@ struct XTradeInfo rZoneUpper = 0; rZoneLower = 0; rShortEntry = 0; + + // + allowRecover = false; } // @@ -1270,6 +1243,18 @@ public: mAllowTrailStop = value; } + // + bool DisableTrailingWhenOpposit() + { + return mDisableTrailingWhenOpposit; + } + + // + void DisableTrailingWhenOpposit(bool value) + { + mDisableTrailingWhenOpposit = value; + } + // double NextTrailPoint() { @@ -1830,11 +1815,9 @@ public: void Add( XSignal &signal, int pushers, - double volumeOSC, - int bullishSigns, - int bearishSigns, - double bullishScore, - double bearishScore, + bool allowGrid, + bool allowRecover, + bool allowSupport, X121SetupConditions &cConditions, X121SetupConditions &sConditions, X121SetupConditions &mConditions, @@ -1843,15 +1826,13 @@ public: ) { // - XTradeInfo signalInfo; - bool isFilled = signalInfo.Fill( + XTradeInfo info; + bool isFilled = info.Fill( signal, pushers, - volumeOSC, - bullishSigns, - bearishSigns, - bullishScore, - bearishScore, + allowGrid, + allowRecover, + allowSupport, cConditions, sConditions, mConditions, @@ -1865,7 +1846,7 @@ public: // AddRef( - signalInfo, + info, data // ); } @@ -2073,9 +2054,8 @@ public: // // Handle Do All Protecting ... int DoProtect( - XSignal &signals[], // if PRotecting Generate Some Support Signals, Holds them here - XSCX121SetupCycles *&setups[], // Number of Setups - bool forceState = false // + XSignal &signals[], // if PRotecting Generate Some Support Signals, Holds them here + XSCX121SetupCycles *&setups[] // Number of Setups ) { // @@ -2170,11 +2150,12 @@ protected: // // Trailling ... - bool mAllowTrailStop; // Enable Trailling Stop Losses - double mNextTrailPoint; // Next Trailling Step - double mFirstTrailPoint; // First Stop Trail - double mFirstTrailDistance; // Start Trailling Distance - double mNextTrailDistance; // Next Trailling Distance + bool mAllowTrailStop; // Enable Trailling Stop Losses + bool mDisableTrailingWhenOpposit; // Disable Trailling When has Opposite In Drawdown + double mNextTrailPoint; // Next Trailling Step + double mFirstTrailPoint; // First Stop Trail + double mFirstTrailDistance; // Start Trailling Distance + double mNextTrailDistance; // Next Trailling Distance // // Supporting ... @@ -2254,17 +2235,33 @@ protected: // Since Trailing Done based On In Profit Positions ... // First we Check if ... XPosition positions[]; - int positionsCount = mTrader.GetInProfitPositions(positions); + XPosition inDPositions[]; + XPosition inPPositions[]; + int positionsCount = mTrader.GetPositions(positions); if (!IsValidSize(positionsCount)) { return result; } // - for (int i = 0; i < positionsCount; i++) + int inDPositionsCount = ExtractInDrawdownPositions( + positions, + inDPositions // + ); + int inPPositionsCount = ExtractInProfitPositions( + positions, + inPPositions // + ); + if (!IsValidSize(inPPositionsCount)) + { + return result; + } + + // + for (int i = 0; i < inPPositionsCount; i++) { // - XPosition iPosition = positions[i]; + XPosition iPosition = inPPositions[i]; // // ignore Support Positions ... @@ -2363,6 +2360,38 @@ protected: continue; } + // + // Check Opposit Side Position ... + // if Exists, dont Trail ... + if (DisableTrailingWhenOpposit() && IsValidSize(inDPositionsCount)) + { + // + bool canTrail = true; + ENUM_POSITION_TYPE oppositType = GetOpposit(iPosition.type); + for (int j = 0; j < inDPositionsCount; j++) + { + // + XPosition inDPosition = inDPositions[j]; + + // + bool isOpposit = + inDPosition.symbol == iPosition.symbol && + inDPosition.type == oppositType; + if (isOpposit) + { + // + canTrail = false; + break; + } + } + + // + if (!canTrail) + { + continue; + } + } + // usedTrailDiff = isFirstTrail @@ -2999,6 +3028,18 @@ public: // mTrader.SetMaxAllowedPositions(value); } + // + bool UseMaxAllowedPerDirection() + { + return mUseMaxAllowedPerDirection; + } + + // + void UseMaxAllowedPerDirection(bool value) + { + mUseMaxAllowedPerDirection = value; + } + // bool IgnoreTP() { @@ -3110,6 +3151,18 @@ public: mTradeHandler.AllowTrailStop(value); } + // + bool DisableTrailingWhenOpposit() + { + return mTradeHandler.DisableTrailingWhenOpposit(); + } + + // + void DisableTrailingWhenOpposit(bool value) + { + mTradeHandler.DisableTrailingWhenOpposit(value); + } + // double NextTrailPoint() { @@ -3185,6 +3238,12 @@ public: mTradeHandler.SupportOnlyForces(value); } + // + bool EnableSupport() + { + return mTradeHandler.EnableSupport(); + } + // double SupportDistance() { @@ -3260,6 +3319,12 @@ public: mTradeHandler.GridOnlyForces(value); } + // + bool EnableGrid() + { + return mTradeHandler.EnableGrid(); + } + // int MaxNumberOfGrids() { @@ -3335,6 +3400,12 @@ public: mTradeHandler.RecoverOnlyForces(value); } + // + bool EnableRecovery() + { + return mTradeHandler.EnableRecovery(); + } + // int MaxNumberOfRecoveries() { @@ -3455,17 +3526,17 @@ public: mMaxPositionsForce = value; } - // - bool ForceState() - { - return mForceState; - } + // // + // bool ForceState() + // { + // return mForceState; + // } - // - void ForceState(bool value) - { - mForceState = value; - } + // // + // void ForceState(bool value) + // { + // mForceState = value; + // } // // Overrides ... @@ -3486,18 +3557,6 @@ public: // // Tools ... - // - double GetBullishScore() - { - return mBullishScore; - } - - // - double GetBearishScore() - { - return mBearishScore; - } - // // Check Provider for any Guards ... bool CheckForGuard(XGuard &guards[]) override @@ -3580,7 +3639,24 @@ public: X121SetupConditions hConditions; // - int maxAllowed = MaxAllowedTrades(); + bool canSupport = + // + EnableSupport() && + !SupportOnlyForces() + // + ; + bool canGrid = + // + EnableGrid() && + !GridOnlyForces() + // + ; + bool canRecover = + // + EnableRecovery() && + !RecoverOnlyForces() + // + ; // double volume = Volume(); @@ -3590,7 +3666,6 @@ public: bool ignoreTP = IgnoreTP(); bool allowLong = AllowLong(); bool allowShort = AllowShort(); - bool allowSupport = AllowSupport(); // for (int i = 0; i < count; i++) @@ -3613,7 +3688,7 @@ public: cPusher, cProvider, X_MARKET_CYCLE_UNKNOWN, - allowSupport, + canSupport, allowLong, allowShort, volume, @@ -3643,7 +3718,7 @@ public: sPusher, sProvider, X_MARKET_CYCLE_SHORT, - allowSupport, + canSupport, allowLong, allowShort, volume, @@ -3673,7 +3748,7 @@ public: mPusher, mProvider, X_MARKET_CYCLE_MEDIUM, - allowSupport, + canSupport, allowLong, allowShort, volume, @@ -3703,7 +3778,7 @@ public: lPusher, lProvider, X_MARKET_CYCLE_LONG, - allowSupport, + canSupport, allowLong, allowShort, volume, @@ -3733,7 +3808,7 @@ public: hPusher, hProvider, X_MARKET_CYCLE_HIND, - allowSupport, + canSupport, allowLong, allowShort, volume, @@ -3889,7 +3964,7 @@ public: ; // - bool forceState = ForceState(); + bool forceState = mSetups[i].ForceState(); // if (!forceState) @@ -3906,11 +3981,9 @@ public: mTradeHandler.Add( signal, pushers, - mVolumeOSC, - mBullishSigns, - mBearishSigns, - mBullishScore, - mBearishScore, + canGrid, + canRecover, + canSupport, cConditions, sConditions, mConditions, @@ -3918,6 +3991,45 @@ public: hConditions // ); } + else + { + // + // Clear Other Conditions ... + Clean(otherConditions); + + // + // Fill All Conditions to Other Conditions ... + AddRef( + cConditions, + otherConditions // + ); + AddRef( + sConditions, + otherConditions // + ); + AddRef( + mConditions, + otherConditions // + ); + AddRef( + lConditions, + otherConditions // + ); + AddRef( + hConditions, + otherConditions // + ); + + // + // If Recieved Any Unverified Signal + // we Passed it to EQM Manager ... + EQMHandleInvalidSignal( + signal, + pushers, + isValid, + otherConditions // + ); + } } } @@ -3959,6 +4071,7 @@ public: // int maxAllowed = MaxAllowedTrades(); + bool perDirection = UseMaxAllowedPerDirection(); // for (int i = 0; i < signalsCount; i++) @@ -4024,14 +4137,19 @@ public: if (maxAllowed > 0) { // + // Here we Get Opposite Signals of Same Symbol Valid ... + ENUM_X_POSITION_TYPES iType = + perDirection + ? ToPositionType(iSignal.type) + : X_POSITION_TYPE_ALL; XPosition iSymbolsPositions[]; int iSymbolsPositionsCount = mTrader.GetPositions( iSymbolsPositions, iSignal.symbol, - NULL, - NULL, - NULL, - true // + NULL, // Provider + NULL, // Period + iType, // Type + true // ); // @@ -4109,6 +4227,14 @@ public: // // Do all State Management here ... + // + // Check if there are open Positions + // and their doesn't have TP and SL and + // Youngest Age of Positions longest as enough + // Make State of Signalling to Force State for Recieving + // Signal and Clear their Tp and SL for allowing hedges ... + EQMHandleForceState(); + // DoHedge(); @@ -4329,8 +4455,8 @@ protected: int positionsCount = mTrader.GetPositions( positions, signal.symbol, - signal.provider, - signal.period, + NULL, // signal.provider, + NULL, // signal.period, ToPositionType(signal.type), true // Filter By Magic ... ); @@ -4368,12 +4494,13 @@ private: // // Risk Management ... - double mVolume; // Volume - double mTPPoint; // TP Point - double mSLPoint; // SL Point - int mMaxAllowedTrades; // Max Alloed Positions - bool mIgnoreTP; // Ignore Calculated TP - bool mIgnoreSL; // Ignore Calculated SL + double mVolume; // Volume + double mTPPoint; // TP Point + double mSLPoint; // SL Point + int mMaxAllowedTrades; // Max Alloed Positions + bool mUseMaxAllowedPerDirection; // Use Max Allowed Positions Per Direction + bool mIgnoreTP; // Ignore Calculated TP + bool mIgnoreSL; // Ignore Calculated SL // // Signalling ... @@ -4389,7 +4516,7 @@ private: // - Receive Signals with minimal Verifications; // - Increase Volume Size of in Profit Positions; // - etc ... - bool mForceState; // Determine in Force State or not + // bool mForceState; // Determine in Force State or not int mDelayBetweenTwoSignal; // Delay between two same signal int mMaxPositionsForce; // Max Number of Positions For Guard Handling double mMaxDrawdownForce; // Force Handling State on Drawdown for Guard @@ -4459,17 +4586,6 @@ private: SaveResults(false); } - // - int mBullishSigns; - double mBullishScore; - - // - int mBearishSigns; - double mBearishScore; - - // - double mVolumeOSC; - // XSCX121SetupCycles *mSetups[]; // Number of Setups @@ -4623,21 +4739,11 @@ private: // Do Gridding Mechanism ... void DoEQMProtect() { - // - // Check if there are open Positions - // and their doesn't have TP and SL and - // Youngest Age of Positions longest as enough - // Make State of Signalling to Force State for Recieving - // Signal and Clear their Tp and SL for allowing hedges ... - EQMHandleForceState(); - // XSignal supports[]; int supportsCount = mTradeHandler.DoProtect( supports, - mSetups, - ForceState() // - ); + mSetups); // if (IsValidSize(supportsCount)) @@ -4692,283 +4798,171 @@ private: int positionsCount = mTrader.GetPositions(positions); if (!IsValidSize(positionsCount)) { + // + int setupsCount = CountSetups(); + if (IsValidSize(setupsCount)) + { + // + for (int i = 0; i < setupsCount; i++) + { + mSetups[i].ForceState(false); + } + } + // // Disable Force State if there isn't any open Positions // and it's Enabled ... // this means Hedged positions ... - if (ForceState()) - { - ForceState(false); - } + // if (ForceState()) + // { + // ForceState(false); + // } // return; } - // // - // double profits = SpecifiedCalculatePositionsProfit(positions); + // + // Search For Force State Per Each Setup ... + // + int forceCount = 0; + int setupsCount = CountSetups(); + if (IsValidSize(setupsCount)) + { + // + int maxAllowed = MaxAllowedTrades(); + bool perDirection = UseMaxAllowedPerDirection(); - // // - // // Checking Force Sate Conditions ... - // if (ForceState()) + // + for (int i = 0; i < setupsCount; i++) + { + // + string iSymbol = mSetups[i].GetSymbol(); + ENUM_TIMEFRAMES iPeriod = mSetups[i].GetPeriod(); + + // + XPosition iPositions[]; + int iPositionsCount = mTrader.GetPositions( + iPositions, + iSymbol, + NULL, // All Providers ... + NULL, // All Periods ... + NULL, // All Types ... + true // Filter By Magic ... + ); + if (!IsValidSize(iPositionsCount)) + { + continue; + } + + // + // Counting Positions ... + int longs; + double longProfits; + double longVolumes; + + // + int shorts; + double shortProfits; + double shortVolumes; + + // + CountPositions( + iPositions, + // + longs, + longProfits, + longVolumes, + // + shorts, + shortProfits, + shortVolumes // + ); + + // + bool isMaxReached = + maxAllowed <= 0 + ? false + : !perDirection + ? longs + shorts >= maxAllowed + : longs >= maxAllowed && shorts >= maxAllowed; + double volumeSummary = longVolumes + shortVolumes; + double profitsSummary = longProfits + shortProfits; + + // + bool isIInForce = + // + maxAllowed <= 0 + ? profitsSummary < 0 && + longVolumes == shortVolumes + : isMaxReached && + profitsSummary < 0 && + longVolumes == shortVolumes + // + ; + if (isIInForce) + { + // + forceCount++; + mSetups[i].ForceState(true); + } + } + } + + // + // bool isForce = forceCount > 1; + // if (!ForceState() && isForce) // { // // - // // Check Force State Hedging ... - // bool isHedged = DoEQMForceClose(); - // if (isHedged) - // { - // // - // ForceState(false); - // return; - // } - - // // - // // Handle Act Based On Drawdow ... - // if (mMaxDrawdownForce > 0) - // { - // // - // double drawdown = CalculateMaxDrawdown(); - // if (drawdown > mMaxDrawdownForce) - // { - // // - // // Try to Hedge ... - // if (profits > 0) - // { - // // - // bool isHedged = DoEQMForceClose(profits); - // if (isHedged) - // { - // // - // ForceState(false); - // return; - // } - // } - - // // - // // Close Max In Drawdown Position ... - // int maxInDIDX = FindMaxDrawdownIndex(positions); - // if (IsValidIndex(maxInDIDX)) - // { - // // - // string comment = XEQMSupportToken + " Max In D ..."; - - // // - // bool isClosed = mTrader.Close( - // positions[maxInDIDX].ticket, - // comment // - // ); - // if (isClosed) - // { - // // - // string message = XEQMSupportToken + " Close " + ToString(positions[maxInDIDX].ticket) + " Due Max In Drawdown ..."; - - // // - // Alert(message); - // } - - // // - // return; - // } - - // // - // return; - // } - // } - - // // - // // Handle Act Based on Positions Count ... - // if (mMaxPositionsForce > 0 && positionsCount > mMaxPositionsForce) - // { - // // - // double requiredProfit = SpecifiedCalculateRequiredProfitForHedge( - // positions, - // MinRequiredProfitPerTrade(), - // MinRequiredProfitPerVolumeFactor() // - // ); - - // // - // double halfProfit = requiredProfit / 2; - // if (halfProfit > 0 && profits > halfProfit) - // { - // // - // bool isHedged = DoEQMForceClose(halfProfit); - // if (isHedged) - // { - // // - // ForceState(false); - // return; - // } - // } - - // // - // // Close Min In Profit Position ... - // int minInPIDX = FindMinProfitIndex(positions); - // if (IsValidIndex(minInPIDX)) - // { - // // - // string comment = XEQMSupportToken + " Min In P ..."; - - // // - // bool isClosed = mTrader.Close( - // positions[minInPIDX].ticket, - // comment // - // ); - // if (isClosed) - // { - // // - // string message = XEQMSupportToken + " Close " + ToString(positions[minInPIDX].ticket) + " Due Min In Profit ..."; - - // // - // Alert(message); - - // // - // return; - // } - // } - - // // - // // Close Max In Drawdown Position ... - // int maxInDIDX = FindMaxDrawdownIndex(positions); - // if (IsValidIndex(maxInDIDX)) - // { - // // - // string comment = XEQMSupportToken + " Max In D ..."; - - // // - // bool isClosed = mTrader.Close( - // positions[maxInDIDX].ticket, - // comment // - // ); - // if (isClosed) - // { - // // - // string message = XEQMSupportToken + "Close " + ToString(positions[maxInDIDX].ticket) + " Due Max In Drawdown ..."; - - // // - // Alert(message); - // } - - // // - // return; - // } - - // // - // return; - // } - + // ForceState(isForce); + // return; + // } + // else if (ForceState() && !isForce) + // { // // + // ForceState(isForce); // return; // } // // - // int longs = 0; - // double longVolumes = 0; - // double longProfits = 0; - - // // - // int shorts = 0; - // double shortVolumes = 0; - // double shortProfits = 0; - - // // - // // Retrieve Required Informations of Positions ... - // CountPositions( - // positions, - // longs, - // longProfits, - // longVolumes, - // shorts, - // shortProfits, - // shortVolumes // - // ); - - // // - // // Retrieve Youngest Age ... - // XPosition youngest; - // int youngestAge = GetYoungest( - // youngest, - // positions // - // ); - - // // - // // Now Comparing items for enabling force State ... - // bool isForceState = - // // - // youngestAge >= 20 && - // // - // (longProfits + shortProfits < 0) - // // - // ; - // if (isForceState) + // // Handle Force State ... + // if (!ForceState()) // { - // // - // ForceState(true); - - // // - // return; - // } - - // // - // XPosition supports[]; - // int supportsCount = ExtractSupports( - // positions, - // supports // - // ); - // if (!IsValidSize(supportsCount)) - // { - // return; - // } - - // // - // int maxSupports = mTradeHandler.EnableSupport() - // ? MaxNumberOfSupports() - // : 0; - // int maxGrids = mTradeHandler.EnableGrid() - // ? MaxNumberOfGrids() - // : 0; - // int maxRecoveries = mTradeHandler.EnableRecovery() - // ? MaxNumberOfRecoveries() - // : 0; - - // // - // // Supporting Force States ... - // bool isProtectionForce = - // supportsCount >= 1; - // if (isProtectionForce) - // { - // // - // ForceState(true); - - // // // return; // } // + // TODO ... + // Here i Can Check what to do In Force State ... } // // Handle any Invalid Signals ... void EQMHandleInvalidSignal( - XSignal &signal, // Invalid Signal - int pushers, // Pushers - double volumeOSC, // Volume Summary of Signal - double bullishScore, // Bullish Score Summary - int bullishSigns, // Number of Bullish Signs - double bearishScore, // Bearish Score Summary - int bearishSigns, // Number of Bearish Signs - X121SetupConditions &conditions[], // All Time Frames Conditions - bool isValid = false // IsValid Signal ... + XSignal &signal, // Invalid Signal + int pushers, // Pushers + bool isValid, // IsValid Signal ... + X121SetupConditions &conditions[] // All Time Frames Conditions ) { // // Check Force State ... - if (!ForceState()) - { + // if (!ForceState()) + // { + // return; + // } + + // + int setupIDX = FindSetupIndex(signal.symbol); + if (!IsValidIndex(setupIDX)) { return; } // - // TODO: Implement it ... + bool isForce = mSetups[setupIDX].ForceState(); + if (!isForce) { + return; + } // XSignal signals[]; @@ -4977,7 +4971,24 @@ private: if (isValid) { // - // bool canHandle = mMaxDrawdownForce <= 0 || mMaxDrawdownForce + bool canSupport = + // + EnableSupport() && + SupportOnlyForces() + // + ; + bool canGrid = + // + EnableGrid() && + GridOnlyForces() + // + ; + bool canRecover = + // + EnableRecovery() && + RecoverOnlyForces() + // + ; // // Check Delay Between Two Signals ... @@ -4991,6 +5002,21 @@ private: signal, signals // ); + + // + // Add new Signal Info ... + mTradeHandler.Add( + signal, + pushers, + canGrid, + canRecover, + canSupport, + conditions[0], + conditions[1], + conditions[2], + conditions[3], + conditions[4] // + ); } } diff --git a/Documents/BKP/usefull.mq5 b/Documents/BKP/usefull.mq5 index 8c6f64a5..b488cef3 100644 --- a/Documents/BKP/usefull.mq5 +++ b/Documents/BKP/usefull.mq5 @@ -3605,3 +3605,248 @@ // hConditions // // ); // } + + +//////////////////////////////////////////////////////////////////////////////// + + + // // + // double profits = SpecifiedCalculatePositionsProfit(positions); + + // // + // // Checking Force Sate Conditions ... + // if (ForceState()) + // { + // // + // // Check Force State Hedging ... + // bool isHedged = DoEQMForceClose(); + // if (isHedged) + // { + // // + // ForceState(false); + // return; + // } + + // // + // // Handle Act Based On Drawdow ... + // if (mMaxDrawdownForce > 0) + // { + // // + // double drawdown = CalculateMaxDrawdown(); + // if (drawdown > mMaxDrawdownForce) + // { + // // + // // Try to Hedge ... + // if (profits > 0) + // { + // // + // bool isHedged = DoEQMForceClose(profits); + // if (isHedged) + // { + // // + // ForceState(false); + // return; + // } + // } + + // // + // // Close Max In Drawdown Position ... + // int maxInDIDX = FindMaxDrawdownIndex(positions); + // if (IsValidIndex(maxInDIDX)) + // { + // // + // string comment = XEQMSupportToken + " Max In D ..."; + + // // + // bool isClosed = mTrader.Close( + // positions[maxInDIDX].ticket, + // comment // + // ); + // if (isClosed) + // { + // // + // string message = XEQMSupportToken + " Close " + ToString(positions[maxInDIDX].ticket) + " Due Max In Drawdown ..."; + + // // + // Alert(message); + // } + + // // + // return; + // } + + // // + // return; + // } + // } + + // // + // // Handle Act Based on Positions Count ... + // if (mMaxPositionsForce > 0 && positionsCount > mMaxPositionsForce) + // { + // // + // double requiredProfit = SpecifiedCalculateRequiredProfitForHedge( + // positions, + // MinRequiredProfitPerTrade(), + // MinRequiredProfitPerVolumeFactor() // + // ); + + // // + // double halfProfit = requiredProfit / 2; + // if (halfProfit > 0 && profits > halfProfit) + // { + // // + // bool isHedged = DoEQMForceClose(halfProfit); + // if (isHedged) + // { + // // + // ForceState(false); + // return; + // } + // } + + // // + // // Close Min In Profit Position ... + // int minInPIDX = FindMinProfitIndex(positions); + // if (IsValidIndex(minInPIDX)) + // { + // // + // string comment = XEQMSupportToken + " Min In P ..."; + + // // + // bool isClosed = mTrader.Close( + // positions[minInPIDX].ticket, + // comment // + // ); + // if (isClosed) + // { + // // + // string message = XEQMSupportToken + " Close " + ToString(positions[minInPIDX].ticket) + " Due Min In Profit ..."; + + // // + // Alert(message); + + // // + // return; + // } + // } + + // // + // // Close Max In Drawdown Position ... + // int maxInDIDX = FindMaxDrawdownIndex(positions); + // if (IsValidIndex(maxInDIDX)) + // { + // // + // string comment = XEQMSupportToken + " Max In D ..."; + + // // + // bool isClosed = mTrader.Close( + // positions[maxInDIDX].ticket, + // comment // + // ); + // if (isClosed) + // { + // // + // string message = XEQMSupportToken + "Close " + ToString(positions[maxInDIDX].ticket) + " Due Max In Drawdown ..."; + + // // + // Alert(message); + // } + + // // + // return; + // } + + // // + // return; + // } + + // // + // return; + // } + + // // + // int longs = 0; + // double longVolumes = 0; + // double longProfits = 0; + + // // + // int shorts = 0; + // double shortVolumes = 0; + // double shortProfits = 0; + + // // + // // Retrieve Required Informations of Positions ... + // CountPositions( + // positions, + // longs, + // longProfits, + // longVolumes, + // shorts, + // shortProfits, + // shortVolumes // + // ); + + // // + // // Retrieve Youngest Age ... + // XPosition youngest; + // int youngestAge = GetYoungest( + // youngest, + // positions // + // ); + + // // + // // Now Comparing items for enabling force State ... + // bool isForceState = + // // + // youngestAge >= 20 && + // // + // (longProfits + shortProfits < 0) + // // + // ; + // if (isForceState) + // { + // // + // ForceState(true); + + // // + // return; + // } + + // // + // XPosition supports[]; + // int supportsCount = ExtractSupports( + // positions, + // supports // + // ); + // if (!IsValidSize(supportsCount)) + // { + // return; + // } + + // // + // int maxSupports = mTradeHandler.EnableSupport() + // ? MaxNumberOfSupports() + // : 0; + // int maxGrids = mTradeHandler.EnableGrid() + // ? MaxNumberOfGrids() + // : 0; + // int maxRecoveries = mTradeHandler.EnableRecovery() + // ? MaxNumberOfRecoveries() + // : 0; + + // // + // // Supporting Force States ... + // bool isProtectionForce = + // supportsCount >= 1; + // if (isProtectionForce) + // { + // // + // ForceState(true); + + // // + // return; + // } + + +////////////////////////////////////////////////////////////////////////////////////////// \ No newline at end of file diff --git a/Experts/x-saherelm.x121.ea.mq5 b/Experts/x-saherelm.x121.ea.mq5 index b7a4f207..668d9eef 100644 --- a/Experts/x-saherelm.x121.ea.mq5 +++ b/Experts/x-saherelm.x121.ea.mq5 @@ -21,6 +21,8 @@ // // Imports ... +#include "../Helpers/x-saherelm.xct.helper.mq5" +#include "../Helpers/x-saherelm.xcc.helper.mq5" #include "../Classes/x-saherelm.x121.setup.xea.mq5" // @@ -42,24 +44,25 @@ input group "Signals"; //,USDCHFb,EURCHFb,USDJPYb,EURJPYb,GBPUSDb // EURUSDb,USDCHFb,EURCHFb,USDJPYb,EURJPYb,GBPUSDb // EURUSDb,USDCHFb,EURCHFb,USDJPYb,EURJPYb,GBPUSDb,XAUUSDb,NZDUSDb,AUDUSDb,USDCADb,BTCUSD,ETHUSD -input string x121EASymbols = "EURUSDb"; // Symbols -input bool x121EAUseAllSymbols = false; // Use All Available Symbols -input bool x121EAUseAllWatchListSymbols = false; // Use All WatchList Symbols -input bool x121EAAllowLong = true; // Allow Long Trades -input bool x121EAAllowShort = true; // Allow Short Trades -input int x121EADelayBetweenTwoSignal = 5; // Delay Between two Same Signals -input int x121EAReuiredSignalVerifications = 4; // Required Verifications for Signals -input bool x121EAGetVerificationFromOtherTimeFrames = true; // Get Verifications from Other Time Frames +input string x121EASymbols = "EURUSDb,USDCHFb,EURCHFb,USDJPYb,EURJPYb,GBPUSDb"; // Symbols +input bool x121EAUseAllSymbols = false; // Use All Available Symbols +input bool x121EAUseAllWatchListSymbols = false; // Use All WatchList Symbols +input bool x121EAAllowLong = true; // Allow Long Trades +input bool x121EAAllowShort = true; // Allow Short Trades +input int x121EADelayBetweenTwoSignal = 5; // Delay Between two Same Signals +input int x121EAReuiredSignalVerifications = 4; // Required Verifications for Signals +input bool x121EAGetVerificationFromOtherTimeFrames = true; // Get Verifications from Other Time Frames // // Risk Management ... input group "Risk Management"; -input double x121EAVolume = 0.02; // Static Volume -input double x121EATPPoint = 1000; // TP Point -input double x121EASLPoint = 1000; // SL Point -input int x121EAMaxAllowedTrades = 2; // Max Allowed Positions -input bool x121EAIgnoreSL = true; // Ignore Calculated SL -input bool x121EAIgnoreTP = false; // Ignore Calculated TP +input double x121EAVolume = 0.01; // Static Volume +input double x121EATPPoint = 1000; // TP Point +input double x121EASLPoint = 1000; // SL Point +input int x121EAMaxAllowedTrades = 1; // Max Allowed Positions Per Symbol +input bool x121EAUseMaxAllowedPerDirection = true; // Use Max Allowed Positions Per Direction +input bool x121EAIgnoreSL = true; // Ignore Calculated SL +input bool x121EAIgnoreTP = false; // Ignore Calculated TP // // Protection ... @@ -71,11 +74,12 @@ input bool x121EACloseOnOpposit = false; // Close all Positions on Opposit // input group "Trailling"; -input bool x121EAAllowTrailStop = true; // Enable Trailling Stop Losses -input double x121EAFirstTrailDistance = 100; // Start Trailling Distance -input double x121EAFirstTrailPoint = 60; // First Stop Trail -input double x121EANextTrailDistance = 50; // Next Trailling Distance -input double x121EANextTrailPoint = 25; // Next Trailling Step +input bool x121EAAllowTrailStop = true; // Enable Trailling Stop Losses +input bool x121EADisableTrailingWhenOpposit = true; // Disable Trailling When has Opposite In Drawdown +input double x121EAFirstTrailDistance = 100; // Start Trailling Distance +input double x121EAFirstTrailPoint = 60; // First Stop Trail +input double x121EANextTrailDistance = 50; // Next Trailling Distance +input double x121EANextTrailPoint = 25; // Next Trailling Step // input group "Supporting"; @@ -132,6 +136,10 @@ input bool x121EATerminalAlerts = false; // Terminal Alerts string x121EAUsedSymbols[]; XSCX121SetupEA *x121EA; +// +XSCXCTHelper *x121EACTHelper; +XSCXCCHelper *x121EACCHelper; + // // Event Handlers ... @@ -172,6 +180,10 @@ void OnDeinit(const int reason) // // De Initialize XSampleEA Providers ... delete x121EA; + + // + delete x121EACTHelper; + delete x121EACCHelper; } // @@ -194,6 +206,36 @@ bool InitialEA() // bool result = false; + // + // Bar Timer ... + XCTInputs ctInputs; + ctInputs.Default(); + x121EACTHelper = new XSCXCTHelper(); + result = x121EACTHelper.Init( + _Symbol, + _Period, + ctInputs // + ); + if (!result) + { + return result; + } + + // + // Bar Styles ... + XCCInputs ccInputs; + ccInputs.Default(); + x121EACCHelper = new XSCXCCHelper(); + result = x121EACCHelper.Init( + _Symbol, + _Period, + ccInputs // + ); + if (!result) + { + return result; + } + // // TesterHideIndicators(true); @@ -283,6 +325,7 @@ bool InitialEA() x121EA.IgnoreSL(x121EAIgnoreSL); x121EA.IgnoreTP(x121EAIgnoreTP); x121EA.MaxAllowedTrades(x121EAMaxAllowedTrades); + x121EA.UseMaxAllowedPerDirection(x121EAUseMaxAllowedPerDirection); // // Protection ... @@ -301,6 +344,7 @@ bool InitialEA() x121EA.FirstTrailPoint(x121EAFirstTrailPoint); x121EA.NextTrailDistance(x121EANextTrailDistance); x121EA.FirstTrailDistance(x121EAFirstTrailDistance); + x121EA.DisableTrailingWhenOpposit(x121EADisableTrailingWhenOpposit); // // Supporting ... @@ -489,7 +533,7 @@ void X121ReportState() // // Detect What we Want to Show as Comment ... - bool isInForceState = x121EA.ForceState(); + // bool isInForceState = x121EA.ForceState(); double bullishScore = 0; // x121EA.GetBullishScore(); double bearishScore = 0; // x121EA.GetBearishScore(); double drawDownPercent = x121EA.GetDrawdownPercent(); @@ -505,8 +549,8 @@ void X121ReportState() "------------" + "\n" : "") + "Drawdown: " + ToString(drawDownPercent) + "%" + "\n" + - "------------" + "\n" + - "Force State: " + ToString(isInForceState) + "\n" + + // "------------" + "\n" + + // "Force State: " + ToString(isInForceState) + "\n" + // "" //