/////////////////////////////////////////////////////// // // SaherElm IT Center MQL5 Seriallize Library // --------------------------------------- // Name: XPOILib // Description: All models related to POIs ... // // // Maintainer: // ------------ // Hadi Khazaee Asl (hadi_khazaee_asl@yahoo.com) // ////////////////////////////////////////////////////// // // Global Properties ... #property library #property copyright "Copyright `023, SaherElm IT Center" #property link "https://www.saherelm.ir" #property version "1.00" #property strict // // Imports ... #include "../Libraries/x-saherelm.x-poi.lib.mq5" // // Extensions ... /** * Get Oldest Box Index ... * * @param source: XBoxZone instance Reference Collection ... * * @return ( int ) */ int GetOldest( XBoxZone &source[] // ) { // int result = -1; // int count = ArraySize(source); if (!IsValidSize(count)) { return result; } // XBoxZone selected; for (int i = 0; i < count; i++) { // XBoxZone iBox = source[i]; // bool canSet = !selected.IsValid() ? true : selected.from > iBox.from; if (canSet) { // result = i; selected = iBox; } // iBox.Clean(); } // selected.Clean(); // return result; } int GetOldest( XOHCL &bar, XBoxZone &source[] // ) { // int result = -1; // if (!bar.IsValid()) { return result; } // int count = ArraySize(source); if (!IsValidSize(count)) { return result; } // XBoxZone selected; for (int i = 0; i < count; i++) { // XBoxZone iBox = source[i]; // bool isBoxValidForBar = IsBoxPlaceValidForBar(bar, iBox); if (!isBoxValidForBar) { // iBox.Clean(); // continue; } // bool canSet = !selected.IsValid() ? true : selected.from > iBox.from; if (canSet) { // result = i; selected = iBox; } // iBox.Clean(); } // selected.Clean(); // return result; } /** * Get Youngest Box Index ... * * @param source: XBoxZone instance Reference Collection ... * * @return ( int ) */ int GetYoungest( XBoxZone &source[] // ) { // int result = -1; // int count = ArraySize(source); if (!IsValidSize(count)) { return result; } // XBoxZone selected; for (int i = 0; i < count; i++) { // XBoxZone iBox = source[i]; // bool canSet = !selected.IsValid() ? true : selected.from < iBox.from; if (canSet) { // result = i; selected = iBox; } // iBox.Clean(); } // selected.Clean(); // return result; } int GetYoungest( XOHCL &bar, XBoxZone &source[] // ) { // int result = -1; // if (!bar.IsValid()) { return result; } // int count = ArraySize(source); if (!IsValidSize(count)) { return result; } // XBoxZone selected; for (int i = 0; i < count; i++) { // XBoxZone iBox = source[i]; // bool isBoxValidForBar = IsBoxPlaceValidForBar(bar, iBox); if (!isBoxValidForBar) { // iBox.Clean(); // continue; } // bool canSet = !selected.IsValid() ? true : selected.from < iBox.from; if (canSet) { // result = i; selected = iBox; } // iBox.Clean(); } // selected.Clean(); // return result; } /** * Get Highest Box Index ... * * @param source: XBoxZone instance Reference Collection ... * * @return ( int ) */ int GetHighest( XBoxZone &source[] // ) { // int result = -1; // int count = ArraySize(source); if (!IsValidSize(count)) { return result; } // XBoxZone selected; for (int i = 0; i < count; i++) { // XBoxZone iBox = source[i]; // bool canSet = !selected.IsValid() ? true : selected.upper < iBox.upper; if (canSet) { // result = i; selected = iBox; } // iBox.Clean(); } // selected.Clean(); // return result; } int GetHighest( XOHCL &bar, XBoxZone &source[] // ) { // int result = -1; // if (!bar.IsValid()) { return result; } // int count = ArraySize(source); if (!IsValidSize(count)) { return result; } // XBoxZone selected; for (int i = 0; i < count; i++) { // XBoxZone iBox = source[i]; // bool isBoxValidForBar = IsBoxPlaceValidForBar(bar, iBox); if (!isBoxValidForBar) { // iBox.Clean(); // continue; } // bool canSet = !selected.IsValid() ? true : selected.upper < iBox.upper; if (canSet) { // result = i; selected = iBox; } // iBox.Clean(); } // selected.Clean(); // return result; } /** * Get Lowest Box Index ... * * @param source: XBoxZone instance Reference Collection ... * * @return ( int ) */ int GetLowest( XBoxZone &source[] // ) { // int result = -1; // int count = ArraySize(source); if (!IsValidSize(count)) { return result; } // XBoxZone selected; for (int i = 0; i < count; i++) { // XBoxZone iBox = source[i]; // bool canSet = !selected.IsValid() ? true : selected.lower > iBox.lower; if (canSet) { // result = i; selected = iBox; } // iBox.Clean(); } // selected.Clean(); // return result; } int GetLowest( XOHCL &bar, XBoxZone &source[] // ) { // int result = -1; // if (!bar.IsValid()) { return result; } // int count = ArraySize(source); if (!IsValidSize(count)) { return result; } // XBoxZone selected; for (int i = 0; i < count; i++) { // XBoxZone iBox = source[i]; // bool isBoxValidForBar = IsBoxPlaceValidForBar(bar, iBox); if (!isBoxValidForBar) { // iBox.Clean(); // continue; } // bool canSet = !selected.IsValid() ? true : selected.lower > iBox.lower; if (canSet) { // result = i; selected = iBox; } // iBox.Clean(); } // selected.Clean(); // return result; } /** * Validate Boxes ... * @param box: XBoxZone instance reference for Validate ... * @param barIndex: int, Bar Index ... * @param barsLength: int, Bars Length for Validation ... * * @return ( bool ) */ bool IsBoxValid( XBoxZone &box, int barIndex = 0, int barsLength = 3 // ) { // bool result = false; // // Normalize Bar Index ... barIndex = NormalizeInt(barIndex, 0); barsLength = NormalizeInt(barsLength, 1); // // Ceck Input Box is Valid ... result = box.IsValid(); if (!result) { return result; } // // Check Input Box is Breaked or not ... // Breaktion Found using Counting Over/Under Close Bars ... bool isBullish = box.IsBullish(); double appliedPrice = box.GetInDirectionalPrice(); result = NotEmptyZero(appliedPrice); if (!result) { return result; } // int toIDX = box.ToIndex(); // XOHCL iBar; int edgeBreakes = 0; for (int i = barIndex; i <= toIDX; i++) { // // Initialize Indexed Bar ... result = iBar.Init( box.symbol, box.period, i // ); // // Checking Price ... result = // // Checking Bar Validation ... result && // // Checking Price Breaked or not ... (isBullish ? iBar.close < box.lower : iBar.close > box.upper); if (result) { edgeBreakes++; } else { edgeBreakes = 0; } // result = edgeBreakes >= barsLength; if (result) { break; } // iBar.Clean(); } // iBar.Clean(); // // return true; // result = edgeBreakes < barsLength; // return result; } /** * Find Specified Box Index inside a Collection of Boxes ... * * @param box: XBoxZone instance Reference ... * @param boxes: XBoxZone instance Reference Collection ... * * @return ( int ) */ bool FindBoxIndex( int &index, XBoxZone &box, XBoxZone &boxes[] // ) { // bool result = false; // index = -1; // int count = ArraySize(boxes); result = box.IsValid() && IsValidSize(count); if (!result) { return result; } // for (int i = 0; i < count; i++) { // XBoxZone iBox = boxes[i]; // bool isSame = box.IsSameAs(iBox); if (isSame) { // index = i; break; } } // result = IsValidIndex(index); // return result; } /** * Remove Specified Box from Collection ... */ bool RemoveBox( XBoxZone &box, XBoxZone &boxes[] // ) { // bool result = false; // result = box.IsValid() && HasChild(boxes); if (!result) { return result; } // int idx = -1; result = FindBoxIndex( idx, box, boxes // ); if (!result) { return result; } // result = ArrayRemove( boxes, idx, 1 // ); // return result; } /** * Remove Specified Box from Collection ... */ int RemoveBoxes( XBoxZone &items[], XBoxZone &sources[] // ) { // int result = 0; // bool has = HasChild(items) && HasChild(sources); if (!has) { return result; } // int count = ArraySize(items); for (int i = 0; i < count; i++) { // int idx = -1; has = FindBoxIndex( idx, items[i], sources // ); if (has) { // has = ArrayRemove( sources, idx, 1 // ); // if (has) { result++; } } } // return result; } /** * Add items reference into source Collection only if it's not Exists ...alignas * * @param item: XBoxZone instance Reference, Specified Item to Add ... * @param sources: XBoxZone instance Reference Collection, Source Items Collection ... * * @return ( bool ) */ bool AddBoxIfNotExists( XBoxZone &item, XBoxZone &sources[] // ) { // bool result = false; // // Validate Args ... result = item.IsValid(); if (!result) { return result; } // // Check Item Exists or not ... int idx = -1; bool isExists = FindBoxIndex( idx, item, sources // ); result = !isExists; if (!result) { return result; } // AddRef( item, sources //; ); // return result; } /** * Add items Collection reference Members into source Collection only if it's not Exists ...alignas * * @param items: XBoxZone instance Reference Collection, Specified Items to Add ... * @param sources: XBoxZone instance Reference Collection, Source Items Collection ... * * @return ( int ) */ int AddBoxIfNotExists( XBoxZone &items[], XBoxZone &sources[] // ) { // int result = 0; // bool has = HasChild(items); if (!has) { return result; } // int before = ArraySize(sources); // int count = ArraySize(items); for (int i = 0; i < count; i++) { // has = AddBoxIfNotExists( items[i], sources // ); } // int after = ArraySize(sources); // result = after > before; // return result; } // void SortBoxes( XBoxZone &items[], int maxAllowed = 0 // ) { // int idx = -1; bool has = false; // has = HasChild(items); if (!has) { return; } // XBoxZone tmp[]; Copy( items, tmp // ); Clean(items); // while (HasChild(tmp)) { // idx = GetOldest(tmp); has = IsValidIndex(idx); if (!has) { break; } // XBoxZone iBox = tmp[idx]; ArrayRemove( tmp, idx, 1 // ); // AddRef( iBox, items // ); } // if (maxAllowed > 0) { // CleanupArray( items, maxAllowed // ); } // Clean(tmp); } /** * Check a BaxHas Valid Place for Specified Bar ... * * @param bar: XOHCL instance reference, Specified Bar ... * #param box: XBoxZone instance reference, Specified Box ... * * @return ( bool ) */ bool IsBoxPlaceValidForBar( XOHCL &bar, XBoxZone &box // ) { // bool result = false; // result = bar.IsValid() && box.IsValid(); if (!result) { return result; } // result = box.IsBullish() ? bar.low > box.upper : bar.high < box.upper; // return result; } // void ValidateBoxes( XBoxZone &boxes[], int barIndex = 0, int barsLength = 3, bool forceSorting = false // ) { // bool has = false; // // Normalize Args ... barIndex = NormalizeInt(barIndex, 0); barsLength = NormalizeInt(barsLength, 3); // // Validate Args ... has = HasChild(boxes); if (!has) { return; } // // Copy Boxes to tmp ... XBoxZone tmp[]; Copy( boxes, tmp // ); Clean(boxes); // // Looping Through Exists Boxes ... while (HasChild(tmp)) { // // Select Indexed Box ... XBoxZone iBox = tmp[0]; // // Remove Indexed Box ... ArrayRemove( tmp, 0, 1 // ); // // Check Box Validating ... has = IsBoxValid( iBox, barIndex, barsLength // ); if (has) { // AddRef( iBox, boxes // ); } } // // Sorting ... if (forceSorting) { // // Check Has Result or not ... has = HasChild(boxes); if (has) { // // Sorting ... SortBoxes(boxes); } } // } //