///////////////////////////////////////////////////////////////////////////// // // SaherElm IT Center MQL5 XTRNDHLP Indicator Helper for Signal Providers // -------------------------------------------------------------------------- // // 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 // // START Includes ... // // // Includes ... #include "../../Libraries/x-saherelm.alert.lib.mq5" #include "../../Libraries/x-saherelm.models.lib.mq5" #include "../../Libraries/x-saherelm.common.lib.mq5" // // END Includes ... // // // START Constants/Enums ... // // // XPERIOD Calculation Method ... // USED For Multiple Time Frames ... enum ENUM_X_PERIOD_METHOD { // X_PERIOD_NOTHING, // X_PERIOD_AUTO, // X_PERIOD_MANUALLY }; // // XPeaksAndVales Exported Buffers ... enum ENUM_X_XTRND_BUFFERS { // X_TRND_UP_LINE = 0, X_TRND_UP_STATE_LINE = 1, // X_TRND_DOWN_LINE = 2, X_TRND_DOWN_STATE_LINE = 3, // // Nearest ... // CYCLE 1 ... X_TRND_L1_UP_LINE = 4, X_TRND_L1_UP_STATE_LINE = 5, // X_TRND_L1_DOWN_LINE = 6, X_TRND_L1_DOWN_STATE_LINE = 7, // // Mediest ... // CYCLE 2 ... X_TRND_L2_UP_LINE = 8, X_TRND_L2_UP_STATE_LINE = 9, // X_TRND_L2_DOWN_LINE = 10, X_TRND_L2_DOWN_STATE_LINE = 11, // // Farest ... // CYCLE 3 ... X_TRND_L3_UP_LINE = 12, X_TRND_L3_UP_STATE_LINE = 13, // X_TRND_L3_DOWN_LINE = 14, X_TRND_L3_DOWN_STATE_LINE = 15, }; // // Trend State at unique Bar ... enum ENUM_X_XTRND_STATE { X_TRND_STATE_RANGE = 0, X_TRND_STATE_CONSOLIDATION = 1, X_TRND_STATE_UP = 2, X_TRND_STATE_DOWN = 3, }; // // END Constants/Enums ... // // double trndUpBuffer[]; double trndUpStateBuffer[]; double trndDownBuffer[]; double trndDownStateBuffer[]; // // Nearest ... // CYCLE 1 ... double trndl1UpBuffer[]; double trndl1UpStateBuffer[]; double trndl1DownBuffer[]; double trndl1DownStateBuffer[]; // // Mediest ... // CYCLE 2 ... double trndl2UpBuffer[]; double trndl2UpStateBuffer[]; double trndl2DownBuffer[]; double trndl2DownStateBuffer[]; // // Farest ... // CYCLE 3 ... double trndl3UpBuffer[]; double trndl3UpStateBuffer[]; double trndl3DownBuffer[]; double trndl3DownStateBuffer[]; // int mtrndHandler = INVALID_HANDLE; // // START Definitions ... // // // END Definitions ... // // // START Handler Functions ... // // bool XTRNDHLPOnInit() { // bool result = false; // ArraySetAsSeries(trndUpBuffer, true); ArraySetAsSeries(trndUpStateBuffer, true); ArraySetAsSeries(trndDownBuffer, true); ArraySetAsSeries(trndDownStateBuffer, true); // // Nearest ... // CYCLE 1 ... ArraySetAsSeries(trndl1UpBuffer, true); ArraySetAsSeries(trndl1UpStateBuffer, true); ArraySetAsSeries(trndl1DownBuffer, true); ArraySetAsSeries(trndl1DownStateBuffer, true); // // Mediest ... // CYCLE 2 ... ArraySetAsSeries(trndl2UpBuffer, true); ArraySetAsSeries(trndl2UpStateBuffer, true); ArraySetAsSeries(trndl2DownBuffer, true); ArraySetAsSeries(trndl2DownStateBuffer, true); // // Farest ... // CYCLE 3 ... ArraySetAsSeries(trndl3UpBuffer, true); ArraySetAsSeries(trndl3UpStateBuffer, true); ArraySetAsSeries(trndl3DownBuffer, true); ArraySetAsSeries(trndl3DownStateBuffer, true); // // Initial Handler ... mtrndHandler = iCustom( _Symbol, _Period, "x-saherelm.x121.xtrnd", // // Inputs ... // // MARKET ... "", 2, 2, // // CALCULATIONS ... "", MODE_HIGH, MODE_LOW, // // CYCLES ... "", // // LEVEL 1 ... "", X_PERIOD_AUTO, _Period, // // LEVEL 2 ... "", X_PERIOD_AUTO, _Period, // // LEVEL 3 ... "", X_PERIOD_AUTO, _Period, // // PRESENTATION ... "", true, true, true, true, true // ); result = mtrndHandler != INVALID_HANDLE; // return result; } // void XTRNDHLPOnDeInit() { // // Release Indicators ... IndicatorRelease(mtrndHandler); } // void XTRNDHLPReadBuffers() { // // UP ... CopyBuffer( mtrndHandler, X_TRND_UP_LINE, 0, 52, trndUpBuffer); // // UP STATE ... CopyBuffer( mtrndHandler, X_TRND_UP_STATE_LINE, 0, 52, trndUpStateBuffer); // // DOWN ... CopyBuffer( mtrndHandler, X_TRND_DOWN_LINE, 0, 52, trndDownBuffer); // // DOWN STATE ... CopyBuffer( mtrndHandler, X_TRND_DOWN_STATE_LINE, 0, 52, trndDownStateBuffer); // // CYCLES ... // // Nearest ... // CYCLE 1 ... // // UP ... CopyBuffer( mtrndHandler, X_TRND_L1_UP_LINE, 0, 52, trndl1UpBuffer); // // UP STATE ... CopyBuffer( mtrndHandler, X_TRND_L1_UP_STATE_LINE, 0, 52, trndl1UpStateBuffer); // // DOWN ... CopyBuffer( mtrndHandler, X_TRND_L1_DOWN_LINE, 0, 52, trndl1DownBuffer); // // DOWN STATE ... CopyBuffer( mtrndHandler, X_TRND_L1_DOWN_STATE_LINE, 0, 52, trndl1DownStateBuffer); // // Mediest ... // CYCLE 2 ... // // UP ... CopyBuffer( mtrndHandler, X_TRND_L2_UP_LINE, 0, 52, trndl2UpBuffer); // // UP STATE ... CopyBuffer( mtrndHandler, X_TRND_L2_UP_STATE_LINE, 0, 52, trndl2UpStateBuffer); // // DOWN ... CopyBuffer( mtrndHandler, X_TRND_L2_DOWN_LINE, 0, 52, trndl2DownBuffer); // // DOWN STATE ... CopyBuffer( mtrndHandler, X_TRND_L2_DOWN_STATE_LINE, 0, 52, trndl2DownStateBuffer); // // Farest ... // CYCLE 3 ... // // UP ... CopyBuffer( mtrndHandler, X_TRND_L3_UP_LINE, 0, 52, trndl3UpBuffer); // // UP STATE ... CopyBuffer( mtrndHandler, X_TRND_L3_UP_STATE_LINE, 0, 52, trndl3UpStateBuffer); // // DOWN ... CopyBuffer( mtrndHandler, X_TRND_L3_DOWN_LINE, 0, 52, trndl3DownBuffer); // // DOWN STATE ... CopyBuffer( mtrndHandler, X_TRND_L3_DOWN_STATE_LINE, 0, 52, trndl3DownStateBuffer); } // // END Handler Functions ... // // // START Helper Functions ... // // // START Strongly Trend Detects ... // // // Detect Strongly Bullish Trend ... bool XTRNDHLPIsTrendStronglyBullish( int bar_index // Bar Index ... ) { // bool result = false; // if (!XTRNDHLPIsValidArraySize(bar_index)) { return result; } // result = // XTRNDHLPIsTrendBullish(bar_index) && ( // (XTRNDHLPIsL1TrendBullish(bar_index) && XTRNDHLPIsL2TrendBullish(bar_index)) // || // (XTRNDHLPIsL1TrendBullish(bar_index) && XTRNDHLPIsL3TrendBullish(bar_index)) // || // (XTRNDHLPIsL2TrendBullish(bar_index) && XTRNDHLPIsL3TrendBullish(bar_index)) // ) // ; // return result; } // // Detect Strongly Trends Switched to Bullish ... bool XTRNDHLPIsTrendStronglySwitchedBullish( int bar_index // Bar Index ... ) { // bool result = false; // if (!XTRNDHLPIsValidArraySize(bar_index + 1)) { return result; } // result = // XTRNDHLPIsTrendStronglyBullish(bar_index) && !XTRNDHLPIsTrendStronglyBullish(bar_index + 1) // ; // return result; } // // Detect Strongly Bearish Trend ... bool XTRNDHLPIsTrendStronglyBearish( int bar_index // Bar Index ... ) { // bool result = false; // if (!XTRNDHLPIsValidArraySize(bar_index)) { return result; } // result = // XTRNDHLPIsTrendBearish(bar_index) && ( // (XTRNDHLPIsL1TrendBearish(bar_index) && XTRNDHLPIsL2TrendBearish(bar_index)) // || // (XTRNDHLPIsL1TrendBearish(bar_index) && XTRNDHLPIsL3TrendBearish(bar_index)) // || // (XTRNDHLPIsL2TrendBearish(bar_index) && XTRNDHLPIsL3TrendBearish(bar_index)) // ) // ; // return result; } // // Detect Strongly Trends Switched to Bearish ... bool XTRNDHLPIsTrendStronglySwitchedBearish( int bar_index // Bar Index ... ) { // bool result = false; // if (!XTRNDHLPIsValidArraySize(bar_index + 1)) { return result; } // result = // XTRNDHLPIsTrendStronglyBearish(bar_index) && !XTRNDHLPIsTrendStronglyBearish(bar_index + 1) // ; // return result; } // // Detect Strongly Consolidation/Range Trend ... bool XTRNDHLPIsTrendStronglyConsolidation( int bar_index // Bar Index ... ) { // bool result = false; // if (!XTRNDHLPIsValidArraySize(bar_index)) { return result; } // result = // XTRNDHLPIsTrendConsolidation(bar_index) && ( // (XTRNDHLPIsL1TrendConsolidation(bar_index) && XTRNDHLPIsL2TrendConsolidation(bar_index)) // || // (XTRNDHLPIsL1TrendConsolidation(bar_index) && XTRNDHLPIsL3TrendConsolidation(bar_index)) // || // (XTRNDHLPIsL2TrendConsolidation(bar_index) && XTRNDHLPIsL3TrendConsolidation(bar_index)) // ) // ; // return result; } // // Detect Strongly Trends Switched to Consolidation/Range ... bool XTRNDHLPIsTrendStronglySwitchedConsolidation( int bar_index // Bar Index ... ) { // bool result = false; // if (!XTRNDHLPIsValidArraySize(bar_index + 1)) { return result; } // result = // XTRNDHLPIsTrendStronglyConsolidation(bar_index) && !XTRNDHLPIsTrendStronglyConsolidation(bar_index + 1) // ; // return result; } // // END Strongly Trend Detects ... // // // Detect Trend Up ... bool XTRNDHLPIsTrendBullish( int bar_index // Bar Index ... ) { // bool result = false; // if (!XTRNDHLPIsValidArraySize(bar_index)) { return result; } // result = // trndUpStateBuffer[bar_index] == X_TRND_STATE_UP && trndDownStateBuffer[bar_index] == X_TRND_STATE_UP // ; // return result; } // // Detect Trend Switched Up ... bool XTRNDHLPIsTrendSwitchedBullish( int bar_index // Bar Index ... ) { // bool result = false; // if (!XTRNDHLPIsValidArraySize(bar_index + 1)) { return result; } // result = // XTRNDHLPIsTrendBullish(bar_index) && !XTRNDHLPIsTrendBullish(bar_index + 1) // ; // return result; } // // Detect Trend Down ... bool XTRNDHLPIsTrendBearish( int bar_index // Bar Index ... ) { // bool result = false; // if (!XTRNDHLPIsValidArraySize(bar_index)) { return result; } // result = // trndUpStateBuffer[bar_index] == X_TRND_STATE_DOWN && trndDownStateBuffer[bar_index] == X_TRND_STATE_DOWN // ; // return result; } // // Detect Trend Switched Down ... bool XTRNDHLPIsTrendSwitchedBearish( int bar_index // Bar Index ... ) { // bool result = false; // if (!XTRNDHLPIsValidArraySize(bar_index + 1)) { return result; } // result = // XTRNDHLPIsTrendBearish(bar_index) && !XTRNDHLPIsTrendBearish(bar_index + 1) // ; // return result; } // // Detect Trend Consolidation ... bool XTRNDHLPIsTrendConsolidation( int bar_index // Bar Index ... ) { // bool result = false; // if (!XTRNDHLPIsValidArraySize(bar_index)) { return result; } // result = // (trndUpStateBuffer[bar_index] == X_TRND_STATE_RANGE || trndUpStateBuffer[bar_index] == X_TRND_STATE_CONSOLIDATION) // && // (trndDownStateBuffer[bar_index] == X_TRND_STATE_RANGE || trndDownStateBuffer[bar_index] == X_TRND_STATE_CONSOLIDATION) // ; // return result; } // // Detect Trend Switched Consolidation ... bool XTRNDHLPIsTrendSwitchedConsolidation( int bar_index // Bar Index ... ) { // bool result = false; // if (!XTRNDHLPIsValidArraySize(bar_index + 1)) { return result; } // result = // XTRNDHLPIsTrendConsolidation(bar_index) && !XTRNDHLPIsTrendConsolidation(bar_index + 1) // ; // return result; } // // Detect Previous Trend Switched To Bullish ... int XTRNDHLPFindNextTrendSwitchedBullish( int bar_index // Bar Index ... ) { // int result = -1; // int size = ArraySize(trndDownStateBuffer); if (size <= 0) { return result; } // for (int i = bar_index + 1; i < size + bar_index; i++) { // bool isTrendSwitched = XTRNDHLPIsTrendSwitchedBullish(i); // if (isTrendSwitched && i != bar_index) { // result = i; break; } } // return result; } // // Detect Previous Trend Switched To Bearish ... int XTRNDHLPFindNextTrendSwitchedBearish( int bar_index // Bar Index ... ) { // int result = -1; // int size = ArraySize(trndDownStateBuffer); if (size <= 0) { return result; } // for (int i = bar_index + 1; i < size + bar_index; i++) { // bool isTrendSwitched = XTRNDHLPIsTrendSwitchedBearish(i); // if (isTrendSwitched && i != bar_index) { // result = i; break; } } // return result; } // // CYCLE 1 ... // // Detect Trend Up ... bool XTRNDHLPIsL1TrendBullish( int bar_index // Bar Index ... ) { // bool result = false; // if (!XTRNDHLPIsValidArraySize(bar_index)) { return result; } // result = // trndl1UpStateBuffer[bar_index] == X_TRND_STATE_UP && trndl1DownStateBuffer[bar_index] == X_TRND_STATE_UP // ; // return result; } // // Detect Trend Switched Up ... bool XTRNDHLPIsL1TrendSwitchedBullish( int bar_index // Bar Index ... ) { // bool result = false; // if (!XTRNDHLPIsValidArraySize(bar_index + 1)) { return result; } // result = // XTRNDHLPIsL1TrendBullish(bar_index) && !XTRNDHLPIsL1TrendBullish(bar_index + 1) // ; // return result; } // // Detect Trend Down ... bool XTRNDHLPIsL1TrendBearish( int bar_index // Bar Index ... ) { // bool result = false; // if (!XTRNDHLPIsValidArraySize(bar_index)) { return result; } // result = // trndl1UpStateBuffer[bar_index] == X_TRND_STATE_DOWN && trndl1DownStateBuffer[bar_index] == X_TRND_STATE_DOWN // ; // return result; } // // Detect Trend Switched Down ... bool XTRNDHLPIsL1TrendSwitchedBearish( int bar_index // Bar Index ... ) { // bool result = false; // if (!XTRNDHLPIsValidArraySize(bar_index + 1)) { return result; } // result = // XTRNDHLPIsL1TrendBearish(bar_index) && !XTRNDHLPIsL1TrendBearish(bar_index + 1) // ; // return result; } // // Detect Trend Consolidation ... bool XTRNDHLPIsL1TrendConsolidation( int bar_index // Bar Index ... ) { // bool result = false; // if (!XTRNDHLPIsValidArraySize(bar_index)) { return result; } // result = // (trndl1UpStateBuffer[bar_index] == X_TRND_STATE_RANGE || trndl1UpStateBuffer[bar_index] == X_TRND_STATE_CONSOLIDATION) // && // (trndl1DownStateBuffer[bar_index] == X_TRND_STATE_RANGE || trndl1DownStateBuffer[bar_index] == X_TRND_STATE_CONSOLIDATION) // ; // return result; } // // Detect Trend Switched Consolidation ... bool XTRNDHLPIsL1TrendSwitchedConsolidation( int bar_index // Bar Index ... ) { // bool result = false; // if (!XTRNDHLPIsValidArraySize(bar_index + 1)) { return result; } // result = // XTRNDHLPIsL1TrendConsolidation(bar_index) && !XTRNDHLPIsL1TrendConsolidation(bar_index + 1) // ; // return result; } // // CYCLE 2 ... // // Detect Trend Up ... bool XTRNDHLPIsL2TrendBullish( int bar_index // Bar Index ... ) { // bool result = false; // if (!XTRNDHLPIsValidArraySize(bar_index)) { return result; } // result = // trndl2UpStateBuffer[bar_index] == X_TRND_STATE_UP && trndl2DownStateBuffer[bar_index] == X_TRND_STATE_UP // ; // return result; } // // Detect Trend Switched Up ... bool XTRNDHLPIsL2TrendSwitchedBullish( int bar_index // Bar Index ... ) { // bool result = false; // if (!XTRNDHLPIsValidArraySize(bar_index + 1)) { return result; } // result = // XTRNDHLPIsL2TrendBullish(bar_index) && !XTRNDHLPIsL2TrendBullish(bar_index + 1) // ; // return result; } // // Detect Trend Down ... bool XTRNDHLPIsL2TrendBearish( int bar_index // Bar Index ... ) { // bool result = false; // if (!XTRNDHLPIsValidArraySize(bar_index)) { return result; } // result = // trndl2UpStateBuffer[bar_index] == X_TRND_STATE_DOWN && trndl2DownStateBuffer[bar_index] == X_TRND_STATE_DOWN // ; // return result; } // // Detect Trend Switched Down ... bool XTRNDHLPIsL2TrendSwitchedBearish( int bar_index // Bar Index ... ) { // bool result = false; // if (!XTRNDHLPIsValidArraySize(bar_index + 1)) { return result; } // result = // XTRNDHLPIsL2TrendBearish(bar_index) && !XTRNDHLPIsL2TrendBearish(bar_index + 1) // ; // return result; } // // Detect Trend Consolidation ... bool XTRNDHLPIsL2TrendConsolidation( int bar_index // Bar Index ... ) { // bool result = false; // if (!XTRNDHLPIsValidArraySize(bar_index)) { return result; } // result = // (trndl2UpStateBuffer[bar_index] == X_TRND_STATE_RANGE || trndl2UpStateBuffer[bar_index] == X_TRND_STATE_CONSOLIDATION) // && // (trndl2DownStateBuffer[bar_index] == X_TRND_STATE_RANGE || trndl2DownStateBuffer[bar_index] == X_TRND_STATE_CONSOLIDATION) // ; // return result; } // // Detect Trend Switched Consolidation ... bool XTRNDHLPIsL2TrendSwitchedConsolidation( int bar_index // Bar Index ... ) { // bool result = false; // if (!XTRNDHLPIsValidArraySize(bar_index + 1)) { return result; } // result = // XTRNDHLPIsL2TrendConsolidation(bar_index) && !XTRNDHLPIsL2TrendConsolidation(bar_index + 1) // ; // return result; } // // CYCLE 3 ... // // Detect Trend Up ... bool XTRNDHLPIsL3TrendBullish( int bar_index // Bar Index ... ) { // bool result = false; // if (!XTRNDHLPIsValidArraySize(bar_index)) { return result; } // result = // trndl3UpStateBuffer[bar_index] == X_TRND_STATE_UP && trndl3DownStateBuffer[bar_index] == X_TRND_STATE_UP // ; // return result; } // // Detect Trend Switched Up ... bool XTRNDHLPIsL3TrendSwitchedBullish( int bar_index // Bar Index ... ) { // bool result = false; // if (!XTRNDHLPIsValidArraySize(bar_index + 1)) { return result; } // result = // XTRNDHLPIsL3TrendBullish(bar_index) && !XTRNDHLPIsL3TrendBullish(bar_index + 1) // ; // return result; } // // Detect Trend Down ... bool XTRNDHLPIsL3TrendBearish( int bar_index // Bar Index ... ) { // bool result = false; // if (!XTRNDHLPIsValidArraySize(bar_index)) { return result; } // result = // trndl3UpStateBuffer[bar_index] == X_TRND_STATE_DOWN && trndl3DownStateBuffer[bar_index] == X_TRND_STATE_DOWN // ; // return result; } // // Detect Trend Switched Down ... bool XTRNDHLPIsL3TrendSwitchedBearish( int bar_index // Bar Index ... ) { // bool result = false; // if (!XTRNDHLPIsValidArraySize(bar_index + 1)) { return result; } // result = // XTRNDHLPIsL3TrendBearish(bar_index) && !XTRNDHLPIsL3TrendBearish(bar_index + 1) // ; // return result; } // // Detect Trend Consolidation ... bool XTRNDHLPIsL3TrendConsolidation( int bar_index // Bar Index ... ) { // bool result = false; // if (!XTRNDHLPIsValidArraySize(bar_index)) { return result; } // result = // (trndl3UpStateBuffer[bar_index] == X_TRND_STATE_RANGE || trndl3UpStateBuffer[bar_index] == X_TRND_STATE_CONSOLIDATION) // && // (trndl3DownStateBuffer[bar_index] == X_TRND_STATE_RANGE || trndl3DownStateBuffer[bar_index] == X_TRND_STATE_CONSOLIDATION) // ; // return result; } // // Detect Trend Switched Consolidation ... bool XTRNDHLPIsL3TrendSwitchedConsolidation( int bar_index // Bar Index ... ) { // bool result = false; // if (!XTRNDHLPIsValidArraySize(bar_index + 1)) { return result; } // result = // XTRNDHLPIsL3TrendConsolidation(bar_index) && !XTRNDHLPIsL3TrendConsolidation(bar_index + 1) // ; // return result; } // bool XTRNDHLPIsValidArraySize(int bar_index) { // bool result = false; // if (bar_index < 0) { return result; } // int rCount = ArraySize(trndUpBuffer); result = rCount > bar_index + 1; // return result; } // // END Helper Functions ... //