/////////////////////////////////////////////////////// // // 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 2023, SaherElm IT Center" #property link "https://www.saherelm.ir" #property version "1.00" #property strict // // Imports ... #include "../Classes/x-saherelm.base.class.mq5" #include "../Libraries/x-saherelm.common.lib.mq5" #include // // Definitions ... /** * Represent a Pivot ... **/ struct XPivot { // // Props ... double value; // Value datetime time; // Time string symbol; // Symbol ENUM_X_DIRECTION dir; // Direction ENUM_TIMEFRAMES period; // Period ENUM_X_PIVOT_TYPE type; // Type // string prefix; // Object Prefix // // Constructor ... XPivot() { Clean(); } // // Init ... /** * Initialize Structure ... * * @param _value: double, value of point ... * @param _time: datetime, time of point ... * @param _symbol: string, Symbol ... * @param _dir: ENUM_X_DIRECTION, direction of point ... * @param _period: ENUM_TIMEFRAMES, period ... * @param _type: ENUM_X_PIVOT_TYPE, point type ... * * @return ( bool ) */ bool Init( double _value, datetime _time, string _symbol, ENUM_X_DIRECTION _dir, ENUM_TIMEFRAMES _period, ENUM_X_PIVOT_TYPE _type // ) { // bool result = false; // dir = _dir; time = _time; type = _type; value = _value; symbol = _symbol; period = _period; // result = IsValid(); if (!result) { Clean(); } // return result; } // // Tools ... /** * Cleanup Model ... */ void Clean() { // value = 0; time = NULL; symbol = NULL; period = NULL; prefix = NULL; dir = X_DIRECTION_NONE; type = X_PIVOT_TYPE_NONE; // ZeroMemory(this); } /** * Validate Model ... * * @return ( bool ) */ bool IsValid() { // bool result = false; // result = value > 0 && IsXValid(type) && HasDirection(dir) && IsSpecifiedValid(time) && IsSpecifiedValid(symbol) && IsSpecifiedValid(period); // return result; } /** * Check Model is Peak or not ... * * @return ( bool ) */ bool IsPeak() { // bool result = false; // result = IsValid() && IsXPeak(type); // return result; } /** * Check Model is Vale or not ... * * @return ( bool ) */ bool IsVale() { // bool result = false; // result = IsValid() && IsXVale(type); // return result; } /** * Check Point is Bullish or not ... * * @return ( bool ) */ bool IsBullish() { // bool result = false; // result = IsValid() && IsSpecifiedBullish(dir); // return result; } /** * Check Point is Bearish or not ... * * @return ( bool ) */ bool IsBearish() { // bool result = false; // result = IsValid() && IsSpecifiedBearish(dir); // return result; } /** * Check two Struct are Same or not ... * * @param item: XPivot ... * * @return ( bool ) */ bool IsSameAs(XPivot &item) { // bool result = false; // result = IsValid() && item.IsValid() && dir == item.dir && type == item.type && time == item.time && value == item.value && symbol == item.symbol && period == item.period; // return result; } /** * Calculate Pivot Age ... * * @return ( int ) */ int GetAge() { // int result = -1; // if (!IsValid()) { return result; } // result = GetBarIndex( symbol, period, time // ); // return result; } /** * Get Pivot Time Bar ... * * @param bar: XOHCL, reference to hold result ... * * @return ( bool ) */ bool GetBar(XOHCL &bar) { // bool result = false; // // Prepare ... bar.Clean(); // // Validate ... result = IsValid(); if (!result) { return result; } // // Initial Bar ... result = bar.Init( symbol, period, time // ); // if (!result) { bar.Clean(); } // return result; } /** * Check to model Has Same Symbol ... * * @param item: XPivot, dest model for Checking ... * * @return ( bool ) */ bool IsSameSymbolAs(XPivot &item) { // bool result = false; // result = IsValid() && item.IsValid() && symbol == item.symbol; // return result; } /** * Check to model Has Same Period ... * * @param item: XPivot, dest model for Checking ... * * @return ( bool ) */ bool IsSamePeriodAs(XPivot &item) { // bool result = false; // result = IsValid() && item.IsValid() && period == item.period; // return result; } /** * Check to model Has Same Symbol/Period ... * * @param item: XPivot, dest model for Checking ... * * @return ( bool ) */ bool IsSameMarketAs(XPivot &item) { // bool result = false; // result = IsValid() && item.IsValid() && IsSameSymbolAs(item) && IsSamePeriodAs(item); // return result; } /** * Check to model Has Same Type ... * * @param item: XPivot, dest model for Checking ... * * @return ( bool ) */ bool IsSameTypeAs(XPivot &item) { // bool result = false; // result = IsValid() && item.IsValid() && type == item.type; // return result; } /** * Check to model Has Same Direction ... * * @param item: XPivot, dest model for Checking ... * * @return ( bool ) */ bool IsSameDirectionAs(XPivot &item) { // bool result = false; // result = IsValid() && item.IsValid() && dir == item.dir; // return result; } // // Logging Tools ... /** * Extract Specified Token of Struct ... * * @return ( string ) */ string GetTag() { // string result = NULL; // string token = GetToken(this); // result = (IsSpecifiedValid(prefix) ? prefix + "_" : "") + token; // return result; } /** * Represent an String Unique Identifier for Chart Objects ... * * @return ( string ) */ string GetObjectID() { // string result = ""; // if (!IsValid()) { return result; } // result = GetTag() + "_" + symbol + "_" + ToXString(period) + "_" + ToXString(dir) + "_" + ToXString(type) + "_" + ToXString(TimeToSeconds(time)); // return result; } /** * Represent a Point as String ... * * @param spacer: string, How to Represent Spaces ... * * @return ( string ) */ string GetMessage(string spacer = NULL) { // string result = ""; // // Normalize Spacer ... if (!IsSpecifiedValid(spacer)) { spacer = " "; } // // Validate ... if (!IsValid()) { return result; } // result = ToXString(dir) + spacer + GetTag() + ":" + spacer + "Symbol: " + symbol + spacer + "Period: " + ToXString(period) + spacer + "Type: " + ToXString(type) + spacer + "Direction: " + ToXString(dir) + spacer + "Value: " + ToXString(value) + spacer + "Time: " + ToXString(time) + spacer; // return result; } }; /** * Model a Box ... **/ struct XBoxZone { // // Props ... string symbol; ENUM_TIMEFRAMES period; // double upper; double lower; // datetime at; datetime from; datetime to; // string type; ENUM_X_DIRECTION dir; // // Special Props ... datetime breakAt; datetime tests[]; datetime acteds[]; datetime breaks[]; datetime insides[]; datetime fakeBreaks[]; // // Constructor ... XBoxZone() { Clean(); } // // Tools ... /** * Cleanup Model ... */ void Clean() { // upper = 0; lower = 0; to = NULL; at = NULL; from = NULL; symbol = NULL; period = NULL; dir = X_DIRECTION_NONE; // SpecialClean(); // ZeroMemory(this); } /** * Clean Special Props ... */ void SpecialClean() { // breakAt = NULL; XClean(tests); XClean(acteds); XClean(breaks); XClean(insides); XClean(fakeBreaks); } /** * Validate Model ... * * @return ( bool ) */ bool IsValid() { // bool result = false; // result = // upper > 0 && lower > 0 && from < to && IsSpecifiedValid(to) && upper >= lower && IsSpecifiedValid(from) && IsSpecifiedValid(symbol) && IsSpecifiedValid(period) // ; // return result; } /** * Check Box is Bullish or not ... * * @return ( bool ) */ bool IsBullish() { return IsValid() && IsSpecifiedBullish(dir); } /** * Check Box is Bearish or not ... * * @return ( bool ) */ bool IsBearish() { return IsValid() && IsSpecifiedBearish(dir); } /** * Clone Box by Provided Source ... * * @return ( bool ) */ bool Clone(XBoxZone &source) { // bool result = false; // Clean(); result = source.IsValid(); if (!result) { return result; } // // Filling Props ... to = source.to; dir = source.dir; from = source.from; type = source.type; upper = source.upper; lower = source.lower; symbol = source.symbol; period = source.period; // result = IsValid(); if (!result) { Clean(); } // return result; } /** * Check Box is Same as Other ... * * @param item: XBoxZone instance Resource ... * * @return ( bool ) */ bool IsSameAs(XBoxZone &item) { // return IsValid() && item.IsValid() && dir == item.dir && from == item.from && type == item.type && lower == item.lower && upper == item.upper && symbol == item.symbol && period == item.period; } /** * Retrieve Box Age ... * * @param forPeriod: ENUM_TIMEFRAMES member ... * * @return ( int ) */ int GetAge( ENUM_TIMEFRAMES forPeriod = NULL // ) { // int result = 0; // if (!IsValid()) { return result; } // if (!IsSpecifiedValid(forPeriod)) { forPeriod = period; } // result = iBarShift( symbol, forPeriod, from // ); // return result; } /** * Calculate Box Middle Price ... * * @return ( double ) */ double GetMid() { // double result = 0; // if (!IsValid()) { return result; } // double diffAvg = (upper - lower) / 2; result = lower + diffAvg; result = NormalizePrice(result, symbol); // return result; } /** * Calculate Box Leg N Price ... * N default is 2 * * @param leg: int ... * * @return ( double ) */ double GetLeg(int leg = 2) { // double result = 0; // leg = NormalizeInt(leg, 2); // if (!IsValid()) { return result; } // double range = (upper - lower) * leg; result = IsBullish() ? upper + range : lower - range; result = NormalizePrice(result, symbol); // return result; } /** * Calculate Box Range ... * * @return ( double ) */ double GetRange() { // double result = 0; // if (!IsValid()) { return result; } // result = (upper - lower); result = NormalizePrice(result, symbol); // return result; } /** * Retrieve Box Applied Price ... * * @return ( double ) */ double GetDirectionalPrice() { // double result = 0; // if (!IsValid()) { return result; } // result = IsBullish() ? upper : lower; // return result; } /** * Retrieve Box Breaked Price ... * * @return ( double ) */ double GetInDirectionalPrice() { // double result = 0; // if (!IsValid()) { return result; } // result = IsBullish() ? lower : upper; // return result; } /** * To Bar Index ... * * @return ( int ) */ int ToIndex( ENUM_TIMEFRAMES forPeriod = NULL // ) { // int result = -1; // if (!IsValid()) { return result; } // if (!IsSpecifiedValid(forPeriod)) { forPeriod = period; } // result = GetBarIndex( symbol, forPeriod, to // ); // return result; } /** * Retrieve To Bar ... * * @return ( bool ) */ bool ToBar(XOHCL &bar) { // bool result = false; // bar.Clean(); // int idx = ToIndex(); // result = IsValid() && IsValidIndex(idx); if (!result) { return result; } // result = bar.Init( symbol, period, idx // ); // return result; } /** * At Bar Index ... * * @return ( int ) */ int AtIndex( ENUM_TIMEFRAMES forPeriod = NULL // ) { // int result = -1; // if (!IsValid() || !IsXValid(at)) { return result; } // if (!IsSpecifiedValid(forPeriod)) { forPeriod = period; } // result = GetBarIndex( symbol, forPeriod, at // ); // return result; } /** * Retrieve At Bar ... * * @return ( bool ) */ bool AtBar(XOHCL &bar) { // bool result = false; // bar.Clean(); // int idx = AtIndex(); // result = IsValid() && IsValidIndex(idx); if (!result) { return result; } // result = bar.Init( symbol, period, idx // ); // return result; } /** * From Bar Index ... * * @return ( int ) */ int FromIndex( ENUM_TIMEFRAMES forPeriod = NULL // ) { // int result = -1; // if (!IsValid()) { return result; } // if (!IsSpecifiedValid(forPeriod)) { forPeriod = period; } // result = GetBarIndex( symbol, forPeriod, from // ); // return result; } /** * Retrieve From Bar ... * * @return ( bool ) */ bool FromBar(XOHCL &bar) { // bool result = false; // bar.Clean(); // int idx = FromIndex(); // result = IsValid() && IsValidIndex(idx); if (!result) { return result; } // result = bar.Init( symbol, period, idx // ); // return result; } /** * Retrieve Unique Identifier ... * * @return ( string ) */ string GetTag() { // int digits = GetDigits(symbol); double nUpper = NormalizeDouble(NormalizePrice(upper, symbol), digits); double nLower = NormalizeDouble(NormalizePrice(lower, symbol), digits); // string typeStr = IsSpecifiedValid(type) ? type : "XBOX"; string result = // typeStr + "_" + ToString(dir) + "_" + symbol + "_" + ToXString(period) + "_" + ToXString(nUpper) + "_" + ToXString(nLower) // ; // return result; } /** * Retrieve Unique Middle Identifier ... * * @return ( string ) */ string GetMiddleTag() { // int digits = GetDigits(symbol); double nMid = NormalizeDouble(NormalizePrice(GetMid(), symbol), digits); // string typeStr = IsSpecifiedValid(type) ? type : "XBOX"; string result = // typeStr + "_" + "Mid_" + ToString(dir) + "_" + symbol + "_" + ToXString(period) + "_" + ToXString(nMid) // ; // return result; } }; /** * Model a Direction Shift ... **/ struct XDirectionShift { // // Props ... string type; datetime at; double after; double before; string symbol; ENUM_TIMEFRAMES period; ENUM_X_DIRECTION shiftTo; // // Constructor ... XDirectionShift() { Clean(); } // // Tools ... // // Cleanup ... void Clean() { // at = NULL; after = 0; before = 0; type = NULL; symbol = NULL; period = NULL; shiftTo = X_DIRECTION_NONE; // ZeroMemory(this); } // // Validate ... bool IsValid() { // bool result = false; // result = after > 0 && IsSpecifiedValid(at) && IsSpecifiedValid(type) && after != before && IsSpecifiedValid(symbol) && IsSpecifiedValid(period) && HasDirection(shiftTo); // return result; } // // Get Bar Index of Direction Shift ... int Index() { // int result = -1; // // Validate ... if (!IsValid()) { return result; } // // Retrieve Bar Index ... result = GetBarIndex( symbol, period, at // ); // return result; } // // Get At Bar ... bool AtBar(XOHCL &bar) { // bool result = false; // // Normalize ... bar.Clean(); // // Validate ... result = IsValid(); if (!result) { return result; } // int idx = Index(); if (IsValidIndex(idx)) { // result = bar.Init( symbol, period, idx // ); } // result = bar.IsValid(); // return result; } // // Check Same ... bool IsSameAs(XDirectionShift &item) { // bool result = false; // result = IsValid() && item.IsValid(); if (!result) { return result; } // result = // at == item.at && type == item.type && after == item.after && before == item.before && symbol == item.symbol && period == item.period && shiftTo == item.shiftTo // ; // return result; } }; //