diff --git a/Classes/x-saherelm.x121.setup.xea.mq5 b/Classes/x-saherelm.x121.setup.xea.mq5 index eb7d2cf9..f2a52f2f 100644 --- a/Classes/x-saherelm.x121.setup.xea.mq5 +++ b/Classes/x-saherelm.x121.setup.xea.mq5 @@ -88,9 +88,6 @@ struct XSignalInfo bearishScore = 0; signal.Clean(); conditions.Clean(); - - // - ResetGrid(); } // @@ -323,21 +320,6 @@ struct XSignalInfo return result; } - // - // Grid ... - - // - double gVolume; // Next Grid Volume - double gEntry; // Next Grid Entry - - // - void ResetGrid() - { - // - gEntry = 0; - gVolume = 0; - } - // }; @@ -558,6 +540,10 @@ public: { // Clean(mSignalInfos); + + // + // Disbale Using Signal Collector ... + UseSignalCollector(false); mSignalCollector = new XSignalInfoCollector(); } @@ -565,20 +551,6 @@ public: // Deconstructor ... ~XSCX121SetupEA() { - // - int count = CountSignalInfos(); - if (IsValidSize(count)) - { - // - for (int i = 0; i < count; i++) - { - mSignalCollector.Save(mSignalInfos[i]); - } - - // - Clean(mSignalInfos); - } - // delete mSignalCollector; @@ -867,6 +839,18 @@ public: mGridVolumeMultiplier = value; } + // + bool UseSignalCollector() + { + return mUseSignalCollector; + } + + // + void UseSignalCollector(bool value) + { + mUseSignalCollector = value; + } + // // Overrides ... @@ -1416,13 +1400,17 @@ public: ); // - AddNewSignal( - cPusher, - cSignal, - mBullishScore, - mBearishScore, - cConditions // - ); + if (UseSignalCollector()) + { + // + AddNewSignal( + cPusher, + cSignal, + mBullishScore, + mBearishScore, + cConditions // + ); + } } } } @@ -1510,13 +1498,17 @@ public: ); // - AddNewSignal( - sPusher, - sSignal, - mBullishScore, - mBearishScore, - sConditions // - ); + if (UseSignalCollector()) + { + // + AddNewSignal( + sPusher, + sSignal, + mBullishScore, + mBearishScore, + sConditions // + ); + } } } } @@ -1604,13 +1596,17 @@ public: ); // - AddNewSignal( - mPusher, - mSignal, - mBullishScore, - mBearishScore, - mConditions // - ); + if (UseSignalCollector()) + { + // + AddNewSignal( + mPusher, + mSignal, + mBullishScore, + mBearishScore, + mConditions // + ); + } } } } @@ -1698,13 +1694,17 @@ public: ); // - AddNewSignal( - lPusher, - lSignal, - mBullishScore, - mBearishScore, - lConditions // - ); + if (UseSignalCollector()) + { + // + AddNewSignal( + lPusher, + lSignal, + mBullishScore, + mBearishScore, + lConditions // + ); + } } } } @@ -1791,13 +1791,17 @@ public: ); // - AddNewSignal( - hPusher, - hSignal, - mBullishScore, - mBearishScore, - hConditions // - ); + if (UseSignalCollector()) + { + // + AddNewSignal( + hPusher, + hSignal, + mBullishScore, + mBearishScore, + hConditions // + ); + } } } } @@ -1963,13 +1967,17 @@ public: void OnDealsChangedHandler(int count) override { // - XDeal deal; - bool hasDeal = mTrader.GetLastDeal(deal); - if (hasDeal) + if (UseSignalCollector()) { // - // Update Signal Info State ... - UpdateSignalState(deal); + XDeal deal; + bool hasDeal = mTrader.GetLastDeal(deal); + if (hasDeal) + { + // + // Update Signal Info State ... + UpdateSignalState(deal); + } } } @@ -1981,19 +1989,10 @@ public: ) override { // - UpdateSignalState(ticket, position, comment); - } - - // - void OnGoingToProfit(XProfitTrack &track) override - { - // RemoveEQMOrders(); - } - - // - void OnGoingToDrawdown(XProfitTrack &track) override - { - // PlaceEQMOrders(); + if (UseSignalCollector()) + { + UpdateSignalState(ticket, position, comment); + } } // @@ -2065,6 +2064,7 @@ private: // XSignalInfo mSignalInfos[]; XSignalInfoCollector *mSignalCollector; + bool mUseSignalCollector; // Enable or Disable Signal Collector // int CountSignalInfos() @@ -2198,6 +2198,12 @@ private: X121SetupConditions &conditions // ) { + // + if (!UseSignalCollector()) + { + return; + } + // XSignalInfo info; bool isFilled = info.Fill( @@ -2222,6 +2228,12 @@ private: // void UpdateSignalState(XDeal &deal) { + // + if (!UseSignalCollector()) + { + return; + } + // if (!deal.IsValid()) { @@ -2243,30 +2255,6 @@ private: mSignalInfos[infoIDX].ticket = deal.positionId; mSignalInfos[infoIDX].commission = deal.commission; mSignalInfos[infoIDX].signal.positionId = deal.positionId; - - // - bool isLong = IsLong(mSignalInfos[infoIDX].signal.type); - double sEntry = mSignalInfos[infoIDX].signal.entry; - - // - // Setting Grid Data ... - if (mUseGrid) - { - // - mSignalInfos[infoIDX].ResetGrid(); - - // - double gDistancePrice = PointToPrice( - mGridDistance, - mSignalInfos[infoIDX].signal.symbol // - ); - - // - mSignalInfos[infoIDX].gVolume = mSignalInfos[infoIDX].volume * mGridVolumeMultiplier; - mSignalInfos[infoIDX].gEntry = isLong - ? sEntry - gDistancePrice - : sEntry + gDistancePrice; - } } else if (deal.entry == DEAL_ENTRY_OUT) { @@ -2309,6 +2297,12 @@ private: const string message // ) { + // + if (!UseSignalCollector()) + { + return; + } + // int infoIDX = FindSignalInfoIndex(ticket); if (!IsValidIndex(infoIDX)) @@ -2632,141 +2626,141 @@ private: void DoEQMGrid() { // - if (!mUseGrid) + if (!UseGrid()) { return; } - // - int infosCount = CountSignalInfos(); - if (!IsValidSize(infosCount)) - { - return; - } + // // + // int infosCount = CountSignalInfos(); + // if (!IsValidSize(infosCount)) + // { + // return; + // } - // - for (int i = 0; i < infosCount; i++) - { - // - // Check Signal Executed or not ... - if (mSignalInfos[i].ticket <= 0) - { - continue; - } + // // + // for (int i = 0; i < infosCount; i++) + // { + // // + // // Check Signal Executed or not ... + // if (mSignalInfos[i].ticket <= 0) + // { + // continue; + // } - // - // Check Specific Position not a Support Position ... - XPosition position; - bool hasPosition = mTrader.GetPosition( - mSignalInfos[i].ticket, - position // - ); - if (!hasPosition) - { - continue; - } + // // + // // Check Specific Position not a Support Position ... + // XPosition position; + // bool hasPosition = mTrader.GetPosition( + // mSignalInfos[i].ticket, + // position // + // ); + // if (!hasPosition) + // { + // continue; + // } - // - // Check Position not a Support or EQM Support ... - ulong supParent = 0; - supParent = ExtractSupportedTicket(position.comment); - if (supParent > 0) - { - continue; - } - supParent = ExtractEQMSupportedTicket(position.comment); - if (supParent > 0) - { - continue; - } + // // + // // Check Position not a Support or EQM Support ... + // ulong supParent = 0; + // supParent = ExtractSupportedTicket(position.comment); + // if (supParent > 0) + // { + // continue; + // } + // supParent = ExtractEQMSupportedTicket(position.comment); + // if (supParent > 0) + // { + // continue; + // } - // - // Check Grid Data ... - if (mSignalInfos[i].gEntry <= 0 || mSignalInfos[i].gVolume <= 0) - { - continue; - } + // // + // // Check Grid Data ... + // if (mSignalInfos[i].gEntry <= 0 || mSignalInfos[i].gVolume <= 0) + // { + // continue; + // } - // - // Retrieve Type of Position ... - bool isLong = IsLong(mSignalInfos[i].signal.type); + // // + // // Retrieve Type of Position ... + // bool isLong = IsLong(mSignalInfos[i].signal.type); - // - // Retrieve Cureent Entry Price based on Signal ... - double cEntry = GetEntry( - mSignalInfos[i].symbol, - mSignalInfos[i].signal.type // - ); + // // + // // Retrieve Cureent Entry Price based on Signal ... + // double cEntry = GetEntry( + // mSignalInfos[i].symbol, + // mSignalInfos[i].signal.type // + // ); - // - // Check Price Distance passed or not ... - bool isPriceDistancePassedForGrid = - isLong - ? cEntry <= mSignalInfos[i].gEntry - : cEntry >= mSignalInfos[i].gEntry; - if (!isPriceDistancePassedForGrid) - { - continue; - } + // // + // // Check Price Distance passed or not ... + // bool isPriceDistancePassedForGrid = + // isLong + // ? cEntry <= mSignalInfos[i].gEntry + // : cEntry >= mSignalInfos[i].gEntry; + // if (!isPriceDistancePassedForGrid) + // { + // continue; + // } - // - // Prepare Grid Signal ... - XSignal gSignal; - bool isPrepared = gSignal.Prepare( - mSignalInfos[i].signal.symbol, - mSignalInfos[i].signal.provider, - mSignalInfos[i].signal.period, - mSignalInfos[i].signal.type, - X_ORDER_MODE_MARKET, - mSignalInfos[i].gEntry, - mSignalInfos[i].gVolume, - 0, // SL ... - 0 // TP ... - ); - if (!isPrepared) - { - continue; - } + // // + // // Prepare Grid Signal ... + // XSignal gSignal; + // bool isPrepared = gSignal.Prepare( + // mSignalInfos[i].signal.symbol, + // mSignalInfos[i].signal.provider, + // mSignalInfos[i].signal.period, + // mSignalInfos[i].signal.type, + // X_ORDER_MODE_MARKET, + // mSignalInfos[i].gEntry, + // mSignalInfos[i].gVolume, + // 0, // SL ... + // 0 // TP ... + // ); + // if (!isPrepared) + // { + // continue; + // } - // - // Add Support Tag ... - string comment = GenerateSupportTag(mSignalInfos[i].ticket); - gSignal.comment = comment; + // // + // // Add Support Tag ... + // string comment = GenerateSupportTag(mSignalInfos[i].ticket); + // gSignal.comment = comment; - // - // Try to Execute Grid Signal ... - ENUM_X_SIGNAL_EXECUTION_RESULT gState; - int isExecuted = mTrader.ExecuteSignal( - gSignal, - gState, - ORDER_TIME_GTC, - NULL, - false // Ignore Policies ... - ); - if (isExecuted) - { - // - // Update Grid State of Signal Info ... + // // + // // Try to Execute Grid Signal ... + // ENUM_X_SIGNAL_EXECUTION_RESULT gState; + // int isExecuted = mTrader.ExecuteSignal( + // gSignal, + // gState, + // ORDER_TIME_GTC, + // NULL, + // false // Ignore Policies ... + // ); + // if (isExecuted) + // { + // // + // // Update Grid State of Signal Info ... - // - double gDistancePrice = PointToPrice( - mGridDistance, - mSignalInfos[i].signal.symbol // - ); + // // + // double gDistancePrice = PointToPrice( + // mGridDistance, + // mSignalInfos[i].signal.symbol // + // ); - // - mSignalInfos[i].gVolume *= mGridVolumeMultiplier; - mSignalInfos[i].gEntry = isLong - ? mSignalInfos[i].gEntry - gDistancePrice - : mSignalInfos[i].gEntry + gDistancePrice; + // // + // mSignalInfos[i].gVolume *= mGridVolumeMultiplier; + // mSignalInfos[i].gEntry = isLong + // ? mSignalInfos[i].gEntry - gDistancePrice + // : mSignalInfos[i].gEntry + gDistancePrice; - // - string message = XEQMSupportToken + " Execute Grid ..."; + // // + // string message = XEQMSupportToken + " Execute Grid ..."; - // - Alert(message); - } - } + // // + // Alert(message); + // } + // } } // diff --git a/Classes/x-saherelm.xea.class.mq5 b/Classes/x-saherelm.xea.class.mq5 index 3a8e7d50..9d8e8614 100644 --- a/Classes/x-saherelm.xea.class.mq5 +++ b/Classes/x-saherelm.xea.class.mq5 @@ -21,7 +21,6 @@ // // Imports ... -#include #include "../Classes/x-saherelm.xalert.class.mq5" #include "../Classes/x-saherelm.xtrade.class.mq5" @@ -29,1660 +28,6 @@ // Define On Signal Event Handler Type ... typedef void (*TOnSignal)(XSignal &signal); -// -// Tokens ... -string XINProfitToken = "INP"; -string XINDrawdownToken = "IND"; -string XProfitsToken = "PFS"; -string XTimesToken = "TMS"; -string XPricessToken = "PCS"; - -// -enum ENUM_X_PROFIT_TRACK_FIELDS -{ - X_PROFIT_TRACK_PROFIT, - X_PROFIT_TRACK_LONGS_PROFIT, - X_PROFIT_TRACK_SHORTS_PROFIT, -}; - -// -// Model Account Profits ... -struct XProfitTrack -{ - // - int longs; // Number of Long ... - int shorts; // Number of Short ... - - // - double profit; // Profit of All ... - double longsProfit; // Profits of Longs ... - double shortsProfit; // Profits of Shorts ... - - // - double profitFrom; // Profit of All ... - double longsProfitFrom; // Profits of Longs ... - double shortsProfitFrom; // Profits of Shorts ... - - // - double volume; // Volume ... - double longsVolume; // Volume of Longs ... - double shortsVolume; // Volume of Shorts ... - - // - datetime time; // Check Time ... - - // - XProfitTrack() - { - Clean(); - } - - // - // Tools ... - - // - // Initialization ... - bool Init(XSCTrade *mTrader) - { - // - bool result = false; - - // - XPosition positions[]; - int positionsCount = mTrader.GetPositions(positions); - result = positionsCount > 0; - if (!result) - { - return result; - } - - // - for (int i = 0; i < positionsCount; i++) - { - // - XPosition iPosition = positions[i]; - - // - bool isLong = IsLong(iPosition.type); - - // - if (isLong) - { - // - longs++; - longsVolume += iPosition.volume; - longsProfit += iPosition.profit; - } - else - { - // - shorts++; - shortsVolume += iPosition.volume; - shortsProfit += iPosition.profit; - } - - // - profit += iPosition.profit; - volume += iPosition.volume; - } - - // - result = IsValid(); - if (result) - { - time = TimeCurrent(); - } - - // - return result; - } - - // - double SelectField(ENUM_X_PROFIT_TRACK_FIELDS field) - { - // - double result = profit; - - // - switch (field) - { - // - case X_PROFIT_TRACK_LONGS_PROFIT: - result = longsProfit; - break; - - // - case X_PROFIT_TRACK_SHORTS_PROFIT: - result = shortsProfit; - break; - - // - default: - case X_PROFIT_TRACK_PROFIT: - result = profit; - break; - } - - // - return result; - } - - // - // Cleanup ... - void Clean() - { - // - longs = 0; - shorts = 0; - profit = 0; - volume = 0; - profitFrom = 0; - longsVolume = 0; - longsProfit = 0; - shortsVolume = 0; - shortsProfit = 0; - longsProfitFrom = 0; - shortsProfitFrom = 0; - - // - time = NULL; - } - - // - // Validate ... - bool IsValid() - { - // - bool result = false; - - // - result = - // - (longs > 0 || shorts > 0) - // - && - // - volume > 0 - // - ; - - // - return result; - } - - // -}; - -// -// Track Specific Position ... -struct XPositionTrack -{ - // - ulong ticket; // Position Ticket - string symbol; // Position Symbol - ENUM_POSITION_TYPE type; // Position Type - ENUM_TIMEFRAMES period; // Position Period - - // - double price[]; // Check Price ... - double profit[]; // Check Profit ... - datetime time[]; // Check Time ... - - // - double tp; // Current TP - double sl; // Current SL - double volume; // Current Volume - double entry; // Position Entry - datetime openAt; // Position Open Time - - // - int inProfitCount; // Number of Cross in DrawDown - int inDrawdownCount; // Number of Crossed in Profit - - // - // Constructor ... - XPositionTrack() - { - Clean(); - } - - // - // Tools ... - - // - // Initialization ... - bool Init( - XPosition &position // Source Position - ) - { - // - bool result = false; - - // - Clean(); - - // - result = position.IsValid(); - if (!result) - { - return result; - } - - // - ticket = position.ticket; - symbol = position.symbol; - period = position.period; - type = position.type; - tp = position.tp; - sl = position.sl; - entry = position.entry; - volume = position.volume; - openAt = position.openAt; - - // - datetime cTime = TimeCurrent(); - Add( - cTime, - time // - ); - - // - Add( - position.profit, - profit // - ); - - // - Add( - position.price, - price // - ); - - // - result = IsValid(); - - // - return result; - } - - // - bool Update( - XPosition &position // Source Position - ) - { - // - bool result = false; - - // - // Check Current Model is Valid or not ... - result = IsValid(); - if (!result) - { - return result; - } - - // - // Check Position is a Valid Model ... - result = position.IsValid(); - if (!result) - { - return result; - } - - // - // Check Current Model belongs to Given Position ... - result = - // - entry == position.entry && - openAt == position.openAt && - symbol == position.symbol && - period == position.period && - type == position.type && - ticket == position.ticket - // - ; - if (!result) - { - return result; - } - - // - tp = position.tp; - sl = position.sl; - volume = position.volume; - - // - datetime cTime = TimeCurrent(); - - // - SetArrayStates(false); - - // - Add( - cTime, - time // - ); - - // - Add( - position.profit, - profit // - ); - - // - Add( - position.price, - price // - ); - - // - SetArrayStates(true); - - // - result = IsValid(); - if (!result) - { - return result; - } - - // - // Fill In Profit or Drawdown Counters ... - - // - bool isCrossInProfit = IsCrossedInProfit(); - if (isCrossInProfit) - { - inProfitCount++; - } - - // - bool isCrossedInDrawdown = IsCrossedInDrawdown(); - if (isCrossedInDrawdown) - { - inDrawdownCount++; - } - - // - return result; - } - - // - // Cleanup ... - void - Clean() - { - // - ticket = 0; - type = NULL; - symbol = NULL; - period = NULL; - - // - tp = 0; - sl = 0; - volume = 0; - entry = 0; - openAt = NULL; - - // - inProfitCount = 0; - inDrawdownCount = 0; - - // - Clean(price); - Clean(profit); - Clean(time); - } - - // - // Validate ... - bool IsValid() - { - // - bool result = false; - - // - result = - // - ticket > 0 && - type != NULL && - period != NULL && - volume > 0 && - entry > 0 && - openAt > 0 - // - ; - - // - return result; - } - - // - void SetArrayStates(bool state) - { - // - ArraySetAsSeries(price, state); - ArraySetAsSeries(profit, state); - ArraySetAsSeries(time, state); - } - - // - bool IsCrossedInProfit() - { - // - bool result = false; - - // - result = IsValid(); - if (!result) - { - return result; - } - - // - result = ArraySize(time) > 1; - if (!result) - { - return result; - } - - // - result = - // - profit[0] > 0 && - profit[1] < 0 - // - ; - - // - return result; - } - - // - bool IsCrossedInDrawdown() - { - // - bool result = false; - - // - result = IsValid(); - if (!result) - { - return result; - } - - // - result = ArraySize(time) > 1; - if (!result) - { - return result; - } - - // - result = - // - profit[0] < 0 && - profit[1] > 0 - // - ; - - // - return result; - } - - // - // Collector Tools ... - - // - string GetToken() - { - return GetToken(this); - } - - // - bool IsModelValid() - { - return IsValid(); - } - - // - bool IsSameAs(XPositionTrack &track) - { - // - bool result = false; - - // - result = - // - track.IsValid() && - type == track.type && - ticket == track.ticket && - symbol == track.symbol && - period == track.period && - openAt == track.openAt - // - ; - - // - return result; - } - - // - int FindIndex( - const XPositionTrack &values[] // Collection - ) - { - // - return FindIndex( - this, - values - // - ); - } - - // - // Converts a Model to String Representation ... - string ToModelString() - { - // - string result = NULL; - - // - if (!IsValid()) - { - return result; - } - - // - // For Modeling a PositionTrack Object ... - // - tp - // - sl - // - type - // - entry - // - ticket - // - symbol - // - period - // - openAt - // - volume - // - inProfitCount - // - inDrawdownCount - // - time[] - // - price[] - // - profit[] - - // - // Ticket ... - string ticketStr = Surround(XIDToken, ticket); - - // - // Symbol ... - string SymbolStr = Surround(XSymbolToken, symbol); - - // - // Type ... - string typeStr = Surround(XTypeToken, type); - - // - // Period ... - int iPeriodInt = (int)period; - string periodStr = Surround(XPeriodToken, iPeriodInt); - - // - // TP ... - string tpStr = Surround(XTPToken, tp); - - // - // SL ... - string slStr = Surround(XSLToken, sl); - - // - // Volume ... - string volumeStr = Surround(XVolumeToken, volume); - - // - // Entry ... - string entryStr = Surround(XEntryToken, entry); - - // - // OpenAt ... - string timeStr = Surround(XTimeToken, openAt); - - // - // InProfitCount ... - string inProfitCountStr = Surround(XINProfitToken, inProfitCount); - - // - // InDrawdownCount ... - string inDrawdownCountStr = Surround(XINDrawdownToken, inDrawdownCount); - - // - // Time(s) ... - string timesContentStr = ToString(time); - string timesStr = Surround(XTimesToken, timesContentStr); - - // - // Price(s) ... - string pricesContentStr = ToString(price); - string pricesStr = Surround(XPricessToken, pricesContentStr); - - // - // Profit(s) ... - string profitsContentStr = ToString(profit); - string profitsStr = Surround(XProfitsToken, profitsContentStr); - - // - string token = GetToken(); - - // - result = - // - token + - "[" + - // - ticketStr + - SymbolStr + - typeStr + - periodStr + - tpStr + - slStr + - volumeStr + - entryStr + - timeStr + - inProfitCountStr + - inDrawdownCountStr + - timesStr + - pricesStr + - profitsStr + - // - "]" - // - ; - - // - return result; - } - - // - // Pres and Fill Model based on it's String Representation ... - bool ParseModel(string value) - { - // - bool result = false; - - // - Clean(); - - // - result = IsValid(value); - if (!result) - { - return result; - } - - // - // For Modeling a PositionTrack Object ... - // - tp - // - sl - // - type - // - entry - // - ticket - // - symbol - // - period - // - openAt - // - volume - // - inProfitCount - // - inDrawdownCount - // - time[] - // - price[] - // - profit[] - - // - // TP ... - tp = ParseDoubleSurrounded(value, XTPToken); - - // - // SL ... - tp = ParseDoubleSurrounded(value, XSLToken); - - // - // Type ... - type = ((ENUM_POSITION_TYPE)ParseIntSurrounded(value, XTypeToken)); - - // - // Entry ... - entry = ParseDoubleSurrounded(value, XEntryToken); - - // - // Ticket ... - ticket = ParseLongSurrounded(value, XIDToken); - - // - // Symbol ... - symbol = ParseStringSurrounded(value, XSymbolToken); - - // - // Period ... - period = ((ENUM_TIMEFRAMES)ParseIntSurrounded(value, XPeriodToken)); - - // - // Open At ... - openAt = ParseTimeSurrounded(value, XTimeToken); - - // - // Volume ... - volume = ParseDoubleSurrounded(value, XVolumeToken); - - // - // InProfitCount ... - inProfitCount = ParseIntSurrounded(value, XINProfitToken); - - // - // InDrawdownCount ... - inDrawdownCount = ParseIntSurrounded(value, XINDrawdownToken); - - // - // Time(s) ... - ParseTimeArraySurrounded( - time, - value, - XTimesToken - // - ); - - // - // Price(s) ... - ParseDoubleArraySurrounded( - price, - value, - XPricessToken - // - ); - - // - // Profit(s) ... - ParseDoubleArraySurrounded( - profit, - value, - XProfitsToken - // - ); - - // - result = IsValid(); - - // - return result; - } -}; - -// -// Type Definitions for XPosition Tracker Events ... -typedef void (*TOnPositionState)(const XPositionTrack &track); - -// -class XSCPositionHolder -{ - // - // Public ... -public: - // - // Props ... - - // - // Constructor(s) ... - - // - XSCPositionHolder() - { - Clear(); - } - - // - // Tools ... - - // - // Cleanup ... - void Clear() - { - Clean(mItems); - } - - // - bool AddItem(XPosition &item) - { - // - bool result = false; - - // - result = item.IsValid(); - if (!result) - { - return result; - } - - // - result = !HasItem(item); - if (!result) - { - return result; - } - - // - AddRef( - item, - mItems // - ); - - // - return result; - } - - // - int AddItems(XPosition &items[]) - { - // - int result = 0; - - // - int itemsCount = ArraySize(items); - if (itemsCount <= 0) - { - return result; - } - - // - for (int i = 0; i < itemsCount; i++) - { - // - XPosition iItem = items[i]; - - // - bool isAdded = AddItem(iItem); - if (isAdded) - { - result++; - } - } - - // - return result; - } - - // - bool AddOrUpdateItem(XPosition &item) - { - // - bool result = false; - - // - result = item.IsValid(); - if (!result) - { - return result; - } - - // - bool hasItem = HasItem(item); - if (!hasItem) - { - result = AddItem(item); - } - else - { - // - result = RemoveItem(item); - if (result) - { - result = AddItem(item); - } - } - - // - return result; - } - - // - int AddOrUpdateItem(XPosition &items[]) - { - // - int result = 0; - - // - int itemsCount = ArraySize(items); - if (itemsCount <= 0) - { - return result; - } - - // - for (int i = 0; i < itemsCount; i++) - { - // - XPosition iItem = items[i]; - - // - bool isAddOrUpdated = AddOrUpdateItem(iItem); - if (isAddOrUpdated) - { - result++; - } - } - - // - return result; - } - - // - bool HasItem(XPosition &item) - { - // - bool result = false; - - // - int itemIDX = FindItem(item); - result = itemIDX >= 0; - - // - return result; - } - - // - int FindItem(XPosition &item) - { - // - int result = -1; - - // - if (!item.IsValid()) - { - return result; - } - - // - int itemsCount = CountItems(); - if (itemsCount <= 0) - { - return result; - } - - // - for (int i = 0; i < itemsCount; i++) - { - // - XPosition iItem = mItems[i]; - - // - bool isSame = IsItemSameAs(item, iItem); - if (isSame) - { - // - result = i; - break; - } - } - - // - return result; - } - - // - bool GetByIndex( - int index, - XPosition &item // - ) - { - // - bool result = false; - - // - NormalizeIndex( - index, - mItems // - ); - - // - item = mItems[index]; - - // - result = item.IsValid(); - - // - return result; - } - - // - bool RemoveItem(XPosition &item) - { - // - bool result = false; - - // - result = HasItem(item); - if (!result) - { - return result; - } - - // - int itemIDX = FindItem(item); - result = ArrayRemove( - mItems, - itemIDX, - 1 // - ); - - // - return result; - } - - // - int RemoveItems(XPosition &items[]) - { - // - int result = 0; - - // - int itemsCount = ArraySize(items); - if (itemsCount <= 0) - { - return result; - } - - // - for (int i = 0; i < itemsCount; i++) - { - // - XPosition iItem = items[i]; - - // - bool isRemoved = RemoveItem(iItem); - if (isRemoved) - { - result++; - } - } - - // - return result; - } - - // - int CountItems() - { - return ArraySize(mItems); - } - - // - bool IsItemSameAs( - XPosition &source, - XPosition &dest // Compare Source with this ... - ) - { - // - bool result = false; - - // - result = - // - dest.IsValid() && - source.IsValid() && - source.symbol == dest.symbol && - source.period == dest.period && - source.ticket == dest.ticket && - source.openAt == dest.openAt && - source.provider == dest.provider - // - ; - - // - return result; - } - - // - // Custom Functions ... - - // - bool GetMaxInDrawdown( - XPosition &item // Result - ) - { - // - bool result = false; - - // - item.Clean(); - - // - int itemsCount = CountItems(); - result = IsValidSize(itemsCount); - if (!result) - { - return result; - } - - // - int maxIDX = -1; - for (int i = 0; i < itemsCount; i++) - { - // - XPosition iItem = mItems[i]; - - // - bool canSelect = - // - iItem.profit < 0 - // - && - // - ( - // - (maxIDX == -1 && !item.IsValid()) - ? true - : item.profit < iItem.profit - // - ) - // - ; - if (canSelect) - { - // - maxIDX = i; - item = iItem; - } - } - - // - result = maxIDX > -1 && item.IsValid(); - - // - return result; - } - - // - bool GetMaxInProfit( - XPosition &item // Result - ) - { - // - bool result = false; - - // - item.Clean(); - - // - int itemsCount = CountItems(); - result = IsValidSize(itemsCount); - if (!result) - { - return result; - } - - // - int maxIDX = -1; - for (int i = 0; i < itemsCount; i++) - { - // - XPosition iItem = mItems[i]; - - // - bool canSelect = - // - iItem.profit > 0 - // - && - // - ( - // - (maxIDX == -1 && !item.IsValid()) - ? true - : item.profit < iItem.profit - // - ) - // - ; - if (canSelect) - { - // - maxIDX = i; - item = iItem; - } - } - - // - result = maxIDX > -1 && item.IsValid(); - - // - return result; - } - - // - bool GetFarestEntry( - XPosition &item, // Result - string symbol = NULL, - string provider = NULL, - ENUM_TIMEFRAMES period = NULL, - ENUM_X_POSITION_TYPE type = NULL // - ) - { - // - bool result = false; - - // - item.Clean(); - - // - int itemsCount = CountItems(); - result = IsValidSize(itemsCount); - if (!result) - { - return result; - } - - // - for (int i = 0; i < itemsCount; i++) - { - // - XPosition iItem = mItems[i]; - - // - bool isFiltersPassed = - // - iItem.IsFiltersPassed( - symbol, - provider, - period, - type // - ) - // - ; - - // - if (!isFiltersPassed) - { - continue; - } - - // - // Do Additional Filtering ... - bool isValidEntry = - // - !item.IsValid() - ? true - : item.entry < iItem.entry - // - ; - if (isValidEntry) - { - // - item = iItem; - } - } - - // - result = item.IsValid(); - - // - return result; - } - - // - bool GetNearestEntry( - XPosition &item, // Result - string symbol = NULL, - string provider = NULL, - ENUM_TIMEFRAMES period = NULL, - ENUM_X_POSITION_TYPE type = NULL // - ) - { - // - bool result = false; - - // - item.Clean(); - - // - int itemsCount = CountItems(); - result = IsValidSize(itemsCount); - if (!result) - { - return result; - } - - // - for (int i = 0; i < itemsCount; i++) - { - // - XPosition iItem = mItems[i]; - - // - bool isFiltersPassed = - // - iItem.IsFiltersPassed( - symbol, - provider, - period, - type // - ) - // - ; - - // - if (!isFiltersPassed) - { - continue; - } - - // - // Do Additional Filtering ... - bool isValidEntry = - // - !item.IsValid() - ? true - : item.entry > iItem.entry - // - ; - if (isValidEntry) - { - // - item = iItem; - } - } - - // - result = item.IsValid(); - - // - return result; - } - - // - bool GetFarestSL( - XPosition &item, // Result - string symbol = NULL, - string provider = NULL, - ENUM_TIMEFRAMES period = NULL, - ENUM_X_POSITION_TYPE type = NULL // - ) - { - // - bool result = false; - - // - item.Clean(); - - // - int itemsCount = CountItems(); - result = IsValidSize(itemsCount); - if (!result) - { - return result; - } - - // - for (int i = 0; i < itemsCount; i++) - { - // - XPosition iItem = mItems[i]; - - // - bool isFiltersPassed = - // - iItem.IsFiltersPassed( - symbol, - provider, - period, - type // - ) - // - ; - - // - if (!isFiltersPassed) - { - continue; - } - - // - // Do Additional Filtering ... - bool isValidEntry = - // - !item.IsValid() - ? true - : item.sl < iItem.sl - // - ; - if (isValidEntry) - { - // - item = iItem; - } - } - - // - result = item.IsValid(); - - // - return result; - } - - // - bool GetNearestSL( - XPosition &item, // Result - string symbol = NULL, - string provider = NULL, - ENUM_TIMEFRAMES period = NULL, - ENUM_X_POSITION_TYPE type = NULL // - ) - { - // - bool result = false; - - // - item.Clean(); - - // - int itemsCount = CountItems(); - result = IsValidSize(itemsCount); - if (!result) - { - return result; - } - - // - for (int i = 0; i < itemsCount; i++) - { - // - XPosition iItem = mItems[i]; - - // - bool isFiltersPassed = - // - iItem.IsFiltersPassed( - symbol, - provider, - period, - type // - ) - // - ; - - // - if (!isFiltersPassed) - { - continue; - } - - // - // Do Additional Filtering ... - bool isValidEntry = - // - !item.IsValid() - ? true - : item.sl > iItem.sl - // - ; - if (isValidEntry) - { - // - item = iItem; - } - } - - // - result = item.IsValid(); - - // - return result; - } - - // - bool GetFarestTP( - XPosition &item, // Result - string symbol = NULL, - string provider = NULL, - ENUM_TIMEFRAMES period = NULL, - ENUM_X_POSITION_TYPE type = NULL // - ) - { - // - bool result = false; - - // - item.Clean(); - - // - int itemsCount = CountItems(); - result = IsValidSize(itemsCount); - if (!result) - { - return result; - } - - // - for (int i = 0; i < itemsCount; i++) - { - // - XPosition iItem = mItems[i]; - - // - bool isFiltersPassed = - // - iItem.IsFiltersPassed( - symbol, - provider, - period, - type // - ) - // - ; - - // - if (!isFiltersPassed) - { - continue; - } - - // - // Do Additional Filtering ... - bool isValidEntry = - // - !item.IsValid() - ? true - : item.tp < iItem.tp - // - ; - if (isValidEntry) - { - // - item = iItem; - } - } - - // - result = item.IsValid(); - - // - return result; - } - - // - bool GetNearestTP( - XPosition &item, // Result - string symbol = NULL, - string provider = NULL, - ENUM_TIMEFRAMES period = NULL, - ENUM_X_POSITION_TYPE type = NULL // - ) - { - // - bool result = false; - - // - item.Clean(); - - // - int itemsCount = CountItems(); - result = IsValidSize(itemsCount); - if (!result) - { - return result; - } - - // - for (int i = 0; i < itemsCount; i++) - { - // - XPosition iItem = mItems[i]; - - // - bool isFiltersPassed = - // - iItem.IsFiltersPassed( - symbol, - provider, - period, - type // - ) - // - ; - - // - if (!isFiltersPassed) - { - continue; - } - - // - // Do Additional Filtering ... - bool isValidEntry = - // - !item.IsValid() - ? true - : item.tp > iItem.tp - // - ; - if (isValidEntry) - { - // - item = iItem; - } - } - - // - result = item.IsValid(); - - // - return result; - } - - // - // Private ... -private: - // - // Props ... - XPosition mItems[]; -}; - // // Class Definition ... @@ -1957,9 +302,6 @@ public: // Check Trade Events ... OnTrade(); - // - HandleProfitManageMent(); - // SyncSupports(); @@ -2286,20 +628,6 @@ public: } } - // - virtual void OnGoingToProfit(XProfitTrack &track) {} - virtual void OnLongsGoingToProfit(XProfitTrack &track) {} - virtual void OnShortsGoingToProfit(XProfitTrack &track) {} - - // - virtual void OnGoingToDrawdown(XProfitTrack &track) {} - virtual void OnLongsGoingToDrawdown(XProfitTrack &track) {} - virtual void OnShortsGoingToDrawdown(XProfitTrack &track) {} - - // - virtual void OnPositionGoingInProfit(XPositionTrack &track) {} - virtual void OnPositionGoingToDrawdown(XPositionTrack &track) {} - // // Protected ... protected: @@ -2569,395 +897,6 @@ protected: } } - // - // Profit Tracker ... - - // - XProfitTrack mProfitTracks[]; - - // - bool GetProfitTrack( - XProfitTrack &track, - int index = 0 // - ) - { - // - bool result = false; - - // - if (index < 0) - { - index = 0; - } - - // - int tracksCount = CountProfitTracks(); - result = tracksCount > 0 && index < tracksCount; - if (!result) - { - return result; - } - - // - track.Clean(); - - // - track = mProfitTracks[index]; - - // - result = track.IsValid(); - - // - return result; - } - - // - int CountProfitTracks() - { - return ArraySize(mProfitTracks); - } - - // - void AddProfitTrack() - { - // - XProfitTrack track; - bool isValid = track.Init(mTrader); - if (!isValid) - { - return; - } - - // - ArraySetAsSeries(mProfitTracks, false); - - // - AddRef( - track, - mProfitTracks // - ); - - // - ArraySetAsSeries(mProfitTracks, true); - } - - // - // Extract Specified Field Vales of XProfit Track Struct ... - int ExtractProfitTrackField( - double &result[], - ENUM_X_PROFIT_TRACK_FIELDS field, - int start = 0, - int count = 10 // - ) - { - // - Clean(result); - - // - if (start < 0) - { - start = 0; - } - - // - int end = start + count; - - // - int tracksCount = CountProfitTracks(); - if (end > tracksCount) - { - end = tracksCount; - } - - // - for (int i = start; i < end; i++) - { - // - XProfitTrack iTrack; - bool isValid = GetProfitTrack( - iTrack, - i // - ); - - // - if (!isValid) - { - continue; - } - - // - double iValue = iTrack.SelectField(field); - Add( - iValue, - result // - ); - } - - // - int mResult = ArraySize(result); - - // - return mResult; - } - - // - double AverageProfitTrackField( - ENUM_X_PROFIT_TRACK_FIELDS field, - int start = 0, - int count = 10 // - ) - { - // - double result = EMPTY_VALUE; - - // - double items[]; - int itemsCount = ExtractProfitTrackField( - items, - field, - start, - count // - ); - if (itemsCount <= 0) - { - return result; - } - - // - result = GetAverage(items); - - // - return result; - } - - // - bool IsProfitTrackFieldIncreasing( - ENUM_X_PROFIT_TRACK_FIELDS field, - int start = 0, - int count = 10 // - ) - { - // - bool result = false; - - // - double items[]; - int itemsCount = ExtractProfitTrackField( - items, - field, - start, - count // - ); - if (itemsCount <= 0) - { - return result; - } - - // - int from = itemsCount - 1; - int to = 0; - - // - result = IsIncreasing( - from, - to, - items // - ); - - // - return result; - } - - // - bool IsProfitTrackFieldDecreasing( - ENUM_X_PROFIT_TRACK_FIELDS field, - int start = 0, - int count = 10 // - ) - { - // - bool result = false; - - // - double items[]; - int itemsCount = ExtractProfitTrackField( - items, - field, - start, - count // - ); - if (itemsCount <= 0) - { - return result; - } - - // - int from = itemsCount - 1; - int to = 0; - - // - result = IsDecreasing( - from, - to, - items // - ); - - // - return result; - } - - // - void HandleProfitManageMent() - { - // - // Check Tracking Interval ... - - // - static datetime lastProfitTracked = NULL; - datetime cTime = TimeCurrent(); - - // - int profitManagementInterval = PeriodSeconds(PERIOD_M5); - int timeDiff = (int)cTime - (int)lastProfitTracked; - - // - bool canManage = - // - lastProfitTracked == NULL - ? true - : timeDiff >= profitManagementInterval - // - ; - if (!canManage) - { - return; - } - - // - XPosition positions[]; - int positionsCount = mTrader.GetPositions(positions); - if (!IsValidSize(positionsCount)) - { - // - Clean(mProfitTracks); - return; - } - - // - lastProfitTracked = cTime; - - // - // Do Track ... - - // - AddProfitTrack(); - - // - int profitsCount = CountProfitTracks(); - if (profitsCount <= 1) - { - return; - } - - // - XProfitTrack cT; - bool isValid = GetProfitTrack( - cT, - 0 // - ); - if (!isValid) - { - return; - } - - // - XProfitTrack pT; - isValid = GetProfitTrack( - pT, - 1 // - ); - if (!isValid) - { - return; - } - - // - cT.profitFrom = pT.profit; - cT.longsProfitFrom = pT.longsProfit; - cT.shortsProfitFrom = pT.shortsProfit; - - // - bool isGoningToProfit = - // - cT.profit > 0 && - pT.profit <= 0 - // - ; - if (isGoningToProfit) - { - // - OnGoingToProfit(cT); - } - - // - bool isLongsGoningToProfit = - // - cT.longsProfit > 0 && - pT.longsProfit <= 0 - // - ; - if (isLongsGoningToProfit) - { - OnLongsGoingToProfit(cT); - } - - // - bool isShortsGoningToProfit = - // - cT.shortsProfit > 0 && - pT.shortsProfit <= 0 - // - ; - if (isShortsGoningToProfit) - { - OnShortsGoingToProfit(cT); - } - - // - bool isGoningToDrawdown = - // - cT.profit < 0 && - pT.profit >= 0 - // - ; - if (isGoningToDrawdown) - { - OnGoingToDrawdown(cT); - } - - bool isLongsGoningToDrawdown = - // - cT.longsProfit < 0 && - pT.longsProfit >= 0 - // - ; - if (isLongsGoningToDrawdown) - { - OnLongsGoingToDrawdown(cT); - } - - bool isShortsGoningToDrawdown = - // - cT.shortsProfit < 0 && - pT.shortsProfit >= 0 - // - ; - if (isShortsGoningToDrawdown) - { - OnShortsGoingToDrawdown(cT); - } - } - // // Search Positions and Remove // non Exists Positions Support Orders ... @@ -3606,87 +1545,6 @@ protected: } } - // - // Position Filter ... - - // - int FilterSymbolBasedPositions( - string &symbols[], // Hold Symbols ... - XSCPositionHolder *&holders[] // Holder Class Instances ... - ) - { - // - int result = 0; - - // - XPosition positions[]; - int positionsCount = mTrader.GetPositions( - positions // - ); - if (!IsValidSize(positionsCount)) - { - return result; - } - - // - // Create a HashMap for Holding Positions ... - CHashMap symbolMap; - - // - // Loop Through Positions ... - for (int i = 0; i < positionsCount; i++) - { - // - XPosition iPosition = positions[i]; - - // - bool hasIKey = symbolMap.ContainsKey(iPosition.symbol); - - // - // Empty Class Pointer ... - XSCPositionHolder *mHolder = NULL; - - // - // Add new to Map ... - if (!hasIKey) - { - // - mHolder = new XSCPositionHolder(); - if (symbolMap.TrySetValue(iPosition.symbol, mHolder)) - { - mHolder.AddOrUpdateItem(iPosition); - } - } - // - // Update Exists in Map ... - else - { - // - if (symbolMap.TryGetValue(iPosition.symbol, mHolder)) - { - mHolder.AddOrUpdateItem(iPosition); - } - } - } - - // - // Now we Have a Map which Filled Based on Separate Symbols and InDrawDown Positions ... - int mapCount = symbolMap.Count(); - if (!IsValidSize(mapCount)) - { - return result; - } - - // - result = symbolMap.CopyTo( - symbols, - holders // - ); - - // - return result; - } - // // Private ... private: