/////////////////////////////////////////////////////// // // SaherElm IT Center MQL5 Indicator // ------------------------------------------------- // Name: X121 XWZ // Description: XWZ ... // // // Maintainer: // ------------ // Hadi Khazaee Asl (hadi_khazaee_asl@yahoo.com) // ////////////////////////////////////////////////////// // // Global Properties ... #property copyright "Copyright 2023, SaherElm IT Center" #property link "https://saherelm.ir" #property version "1.00" #property description "SaherElm X121 XWZ Indicator" #property strict // // Definitions ... // #define ShortName "X121_X3MA" // // Imports ... #include "../Libraries/x-saherelm.common.lib.mq5" // // Inputs ... // input group "Accumulation/Distribution"; input ENUM_APPLIED_VOLUME adAppliedTo = VOLUME_TICK; // Applied To // input group "ADX"; input int adxLength = 14; // Length // input group "ATR"; input int atrLength = 14; // Length // input group "CCI"; input int cciLength = 14; // Length input ENUM_APPLIED_PRICE cciAppliedTo = PRICE_TYPICAL; // Applied To // input group "RSI"; input int rsiLength = 14; // Length input ENUM_APPLIED_PRICE rsiAppliedTo = PRICE_CLOSE; // Applied To // input group "MFI"; input int mfiLength = 14; // Length input ENUM_APPLIED_VOLUME mfiAppliedTo = VOLUME_TICK; // Applied To // input group "RVI"; input int rviLength = 14; // Length // input group "MACD"; input int macdFastLength = 12; // Fast EMA Length input int macdSlowLength = 26; // Slow EMA Length input int macdSignalLength = 9; // Signal Length input ENUM_APPLIED_PRICE macdAppliedTo = PRICE_CLOSE; // Applied To // input group "Momentum"; input int momentumLength = 14; // Length input ENUM_APPLIED_PRICE momentumAppliedTo = PRICE_CLOSE; // Applied To // input group "Stochastic"; input int stochasticKLength = 5; // K Length (Bars for Calculation) input int stochasticDLength = 3; // D Length (first Smoothing) input int stochasticSmoothingLength = 3; // Smoothing Length input ENUM_MA_METHOD stochasticSmoothingMethod = MODE_SMA; // Smoothing Method input ENUM_STO_PRICE stochasticAppliedTo = STO_LOWHIGH; // Applied To // // Presentation ... input group "Presentation"; // input int startCalculationForLastBars = 1000; // Calculate Last n Bars // // Buffers ... // #define emptyValue 0.0 // #property indicator_chart_window // #property indicator_buffers 16 #property indicator_plots 0 // // Plot Buffers ... // // Data Buffers ... // int mLastBufferIndex = -1; // // AC ... #define acBufferIndex mLastBufferIndex + 1 double acBuffer[]; // // AD ... #define adBufferIndex mLastBufferIndex + 2 double adBuffer[]; // // ADX ... #define adxBufferIndex mLastBufferIndex + 3 double adxBuffer[]; // // ADX Plus ... #define adxpBufferIndex mLastBufferIndex + 4 double adxpBuffer[]; // // ADX Negative ... #define adxnBufferIndex mLastBufferIndex + 5 double adxnBuffer[]; // // ATR ... #define atrBufferIndex mLastBufferIndex + 6 double atrBuffer[]; // // CCI ... #define cciBufferIndex mLastBufferIndex + 7 double cciBuffer[]; // // RSI ... #define rsiBufferIndex mLastBufferIndex + 8 double rsiBuffer[]; // // MFI ... #define mfiBufferIndex mLastBufferIndex + 9 double mfiBuffer[]; // // RVI ... #define rviBufferIndex mLastBufferIndex + 10 double rviBuffer[]; // // RVI Signal ... #define rviSignalBufferIndex mLastBufferIndex + 11 double rviSignalBuffer[]; // // MACD ... #define macdBufferIndex mLastBufferIndex + 12 double macdBuffer[]; // // MACD Signal ... #define macdSignalBufferIndex mLastBufferIndex + 13 double macdSignalBuffer[]; // // Momentum ... #define momentumBufferIndex mLastBufferIndex + 14 double momentumBuffer[]; // // Stochstic ... #define stochasticBufferIndex mLastBufferIndex + 15 double stochasticBuffer[]; // // Stochstic Signal ... #define stochasticSignalBufferIndex mLastBufferIndex + 16 double stochasticSignalBuffer[]; // // Variables, Properties and etc ... // // this counts Available Bars ... int limit; // int maxLength; // int acHandler = INVALID_HANDLE; int adHandler = INVALID_HANDLE; int adxHandler = INVALID_HANDLE; int atrHandler = INVALID_HANDLE; int cciHandler = INVALID_HANDLE; int rsiHandler = INVALID_HANDLE; int rviHandler = INVALID_HANDLE; int mfiHandler = INVALID_HANDLE; int macdHandler = INVALID_HANDLE; int momentumHandler = INVALID_HANDLE; int stochasticHandler = INVALID_HANDLE; // // Event Handlers ... /** * Initialize Indicator ... * * @return ( int ) */ int OnInit() { // // Validate Inputs ... if (!ValidateInputs()) { return INIT_PARAMETERS_INCORRECT; } // // Initialize Indicator Handlers ... if (!InitializeHandlres()) { return INIT_FAILED; } // // because in some cases we may have more than one input for // calculation and we must prevent any calculation // untill we pass the biggest input length, here we get max Input length // and then wait until pass it ... maxLength = ExtractMaxLengthOfInputs(); // // Define Index Buffers ... DefineBuffers(); // // Set Indicator ShortName ... SetIndicatorName(); IndicatorSetInteger(INDICATOR_DIGITS, 2); // // Init Succeed ... return INIT_SUCCEEDED; } /** * De Initialize Indicator ... * * @param reason: Integer, De Initialization Reason ... */ void OnDeinit(const int reason) { // // REASON_PROGRAM 0 The EA has stopped working calling the ExpertRemove() function // REASON_REMOVE 1 Program removed from a chart // REASON_RECOMPILE 2 Program recompiled // REASON_CHARTCHANGE 3 A symbol or a chart period is changed // REASON_CHARTCLOSE 4 Chart closed // REASON_PARAMETERS 5 Inputs changed by a user // REASON_ACCOUNT 6 Another account has been activated or reconnection to the trade server has occurred due to changes in the account settings // REASON_TEMPLATE 7 Another chart template applied // REASON_INITFAILED 8 The OnInit() handler returned a non-zero value // REASON_CLOSE 9 Terminal closed // DeInitializeHandlres(); } /** * Calculate Bars ... * * @param rates_total: Integer, Total Bars on Chart ... * @param prev_calculated: Integer, Total Calculated Bars on Charts ... * @param time: DateTime Array, History of Open Time ... * @param open: Double Array, History of Open Prices ... * @param high: Double Array, History of High Prices ... * @param low: Double Array, History of Low Prices ... * @param close: Double Array, History of Close Prices ... * @param tick_volume: Long, History of Tick Volumes on Bar ... * @param volume: Long, History of Trade Volumes ... * @param spread: Double, History of Spread Price ... * * @return ( int ) */ int OnCalculate( const int rates_total, const int prev_calculated, const datetime &time[], const double &open[], const double &high[], const double &low[], const double &close[], const long &tick_volume[], const long &volume[], const int &spread[] // ) { // // Prepare Buffers ... ArraySetAsSeries(time, true); ArraySetAsSeries(open, true); ArraySetAsSeries(high, true); ArraySetAsSeries(low, true); ArraySetAsSeries(close, true); ArraySetAsSeries(tick_volume, true); ArraySetAsSeries(volume, true); ArraySetAsSeries(spread, true); // // Validate Calculated Bars ... // // AC ... int acCalculatedBars = BarsCalculated(acHandler); // // AD ... int adCalculatedBars = BarsCalculated(adHandler); // // ADX ... int adxCalculatedBars = BarsCalculated(adHandler); // // ATR ... int atrCalculatedBars = BarsCalculated(atrHandler); // // CCI ... int cciCalculatedBars = BarsCalculated(cciHandler); // // RSI ... int rsiCalculatedBars = BarsCalculated(rsiHandler); // // MFI ... int mfiCalculatedBars = BarsCalculated(mfiHandler); // // RVI ... int rviCalculatedBars = BarsCalculated(rviHandler); // // MACD ... int macdCalculatedBars = BarsCalculated(macdHandler); // // Momentum ... int momentumCalculatedBars = BarsCalculated(momentumHandler); // // Stochastic ... int stochasticCalculatedBars = BarsCalculated(stochasticHandler); // bool isPassedRequiredCalculatedBars = // // AC ... acCalculatedBars >= maxLength && // // AD ... adCalculatedBars >= maxLength && // // ADX ... adxCalculatedBars >= maxLength && // // ATR ... atrCalculatedBars >= maxLength && // // CCI ... cciCalculatedBars >= maxLength && // // RSI ... rsiCalculatedBars >= maxLength && // // MFI ... mfiCalculatedBars >= maxLength && // // RVI ... rviCalculatedBars >= maxLength && // // MACD ... macdCalculatedBars >= maxLength && // // Momentum ... momentumCalculatedBars >= maxLength && // // Stochastic ... stochasticCalculatedBars >= maxLength // ; if (!isPassedRequiredCalculatedBars) { return prev_calculated; } // limit = (prev_calculated > rates_total || prev_calculated <= 0) ? rates_total : (rates_total - prev_calculated) + 1; // // Buffers Copy ... // // AC ... int copiedACs = CopyBuffer(acHandler, MAIN_LINE, 0, limit, acBuffer); // // AD ... int copiedADs = CopyBuffer(adHandler, MAIN_LINE, 0, limit, adBuffer); // // ADX ... int copiedADXs = CopyBuffer(adxHandler, MAIN_LINE, 0, limit, adxBuffer); // // ADX Plus ... int copiedADXPs = CopyBuffer(adxHandler, PLUSDI_LINE, 0, limit, adxpBuffer); // // ADX Negative ... int copiedADXNs = CopyBuffer(adxHandler, MINUSDI_LINE, 0, limit, adxnBuffer); // // ATR ... int copiedATRs = CopyBuffer(atrHandler, MAIN_LINE, 0, limit, atrBuffer); // // CCI ... int copiedCCIs = CopyBuffer(cciHandler, MAIN_LINE, 0, limit, cciBuffer); // // RSI ... int copiedRSIs = CopyBuffer(rsiHandler, MAIN_LINE, 0, limit, rsiBuffer); // // MFI ... int copiedMFIs = CopyBuffer(mfiHandler, MAIN_LINE, 0, limit, mfiBuffer); // // RVI ... int copiedRVIs = CopyBuffer(rviHandler, MAIN_LINE, 0, limit, rviBuffer); // // RVI ... int copiedRVISignals = CopyBuffer(rviHandler, SIGNAL_LINE, 0, limit, rviSignalBuffer); // // MACD ... int copiedMACDs = CopyBuffer(macdHandler, MAIN_LINE, 0, limit, macdBuffer); // // MACD Signal ... int copiedMACDSignals = CopyBuffer(macdHandler, SIGNAL_LINE, 0, limit, macdSignalBuffer); // // Momentum ... int copiedMomentums = CopyBuffer(cciHandler, MAIN_LINE, 0, limit, momentumBuffer); // // Stochastic ... int copiedStochastics = CopyBuffer(stochasticHandler, MAIN_LINE, 0, limit, stochasticBuffer); // // Stochastic Signal ... int copiedStochasticSignals = CopyBuffer(stochasticHandler, SIGNAL_LINE, 0, limit, stochasticSignalBuffer); // // Validate Copied Items ... bool isPassedRequiredCopiedItems = // // AC ... copiedACs > 0 && // // AD ... copiedADs > 0 && // // ADX ... copiedADXs > 0 && // // ADX Plus ... copiedADXPs > 0 && // // ADX Negative ... copiedADXNs > 0 && // // ATR ... copiedATRs > 0 && // // CCI ... copiedCCIs > 0 && // // RSI ... copiedRSIs > 0 && // // MFI ... copiedMFIs > 0 && // // RVI ... copiedRVIs > 0 && // // RVI Signal ... copiedRVISignals > 0 && // // MACD ... copiedMACDs > 0 && // // MACD Signal ... copiedMACDSignals > 0 && // // Momentum ... copiedMomentums > 0 && // // Stochastic ... copiedStochastics > 0 && // // Stochastic Signal ... copiedStochasticSignals > 0 // ; if (!isPassedRequiredCopiedItems) { return prev_calculated; } // // Main Loop ... for (int i = limit - 1; i >= 0 && !IsStopped(); i--) { // CalculateBuffers( // i, prev_calculated, rates_total, // open, high, close, low, tick_volume // ); } // return rates_total; } // // Functions ... /** * Validate Input Args for Initialization ... * * @return ( bool ) */ bool ValidateInputs() { // bool result = false; // result = // adxLength > 0 && atrLength > 0 && cciLength > 0 && rsiLength > 0 && mfiLength > 0 && rviLength > 0 && // macdFastLength > 0 && macdSlowLength > 0 && macdSignalLength > 0 && macdFastLength < macdSlowLength && // stochasticKLength > 0 && stochasticDLength > 0 && stochasticSmoothingLength > 0 && // momentumLength > 0 // ; // return result; } /** * Initialize All Indicators Handlers ... * * @return ( bool ) */ bool InitializeHandlres() { // bool result = false; // // AC ... acHandler = iAC(_Symbol, _Period); result = acHandler != INVALID_HANDLE; if (!result) { return result; } // // AD ... adHandler = iAD( _Symbol, _Period, adAppliedTo // ); result = adHandler != INVALID_HANDLE; if (!result) { return result; } // // ADX ... adxHandler = iADX( _Symbol, _Period, adxLength // ); result = adxHandler != INVALID_HANDLE; if (!result) { return result; } // // ATR ... atrHandler = iATR( _Symbol, _Period, atrLength // ); result = atrHandler != INVALID_HANDLE; if (!result) { return result; } // // CCI ... cciHandler = iCCI( _Symbol, _Period, cciLength, cciAppliedTo // ); result = cciHandler != INVALID_HANDLE; if (!result) { return result; } // // RSI ... rsiHandler = iRSI( _Symbol, _Period, rsiLength, rsiAppliedTo // ); result = rsiHandler != INVALID_HANDLE; if (!result) { return result; } // // MFI ... mfiHandler = iMFI( _Symbol, _Period, mfiLength, mfiAppliedTo // ); result = mfiHandler != INVALID_HANDLE; if (!result) { return result; } // // RVI ... rviHandler = iRVI( _Symbol, _Period, rviLength // ); result = rviHandler != INVALID_HANDLE; if (!result) { return result; } // // MACD ... macdHandler = iMACD( _Symbol, _Period, macdFastLength, macdSlowLength, macdSignalLength, macdAppliedTo // ); result = macdHandler != INVALID_HANDLE; if (!result) { return result; } // // Momentum ... momentumHandler = iMomentum( _Symbol, _Period, momentumLength, momentumAppliedTo // ); result = momentumHandler != INVALID_HANDLE; if (!result) { return result; } // // Stochastic ... stochasticHandler = iStochastic( _Symbol, _Period, stochasticKLength, stochasticDLength, stochasticSmoothingLength, stochasticSmoothingMethod, stochasticAppliedTo // ); result = stochasticHandler != INVALID_HANDLE; if (!result) { return result; } // return result; } /** * Release All Indicators ... */ void DeInitializeHandlres() { // IndicatorRelease(acHandler); IndicatorRelease(adHandler); IndicatorRelease(adxHandler); IndicatorRelease(atrHandler); IndicatorRelease(cciHandler); IndicatorRelease(rsiHandler); IndicatorRelease(mfiHandler); IndicatorRelease(rviHandler); IndicatorRelease(macdHandler); IndicatorRelease(momentumHandler); IndicatorRelease(stochasticHandler); } /** * Extract Max Length of Inputs ... * * @return ( int ) */ int ExtractMaxLengthOfInputs() { // int result = 0; // result = MathMax(adxLength, atrLength); result = MathMax(result, cciLength); result = MathMax(result, rsiLength); result = MathMax(result, mfiLength); result = MathMax(result, rviLength); result = MathMax(result, momentumLength); result = MathMax(result, macdFastLength); result = MathMax(result, macdSlowLength); result = MathMax(result, macdSignalLength); result = MathMax(result, stochasticKLength); result = MathMax(result, stochasticDLength); result = MathMax(result, stochasticSmoothingLength); // return result; } /** * Set Indicator Short Name and also we can define Buffers Labels ... */ void SetIndicatorName() { // IndicatorSetInteger(INDICATOR_DIGITS, _Digits); IndicatorSetString(INDICATOR_SHORTNAME, ShortName); } /** * Define Required Buffers ... */ void DefineBuffers() { // // Plot Buffers ... // // Data Buffers ... // // AC ... ArraySetAsSeries(acBuffer, true); SetIndexBuffer(acBufferIndex, acBuffer, INDICATOR_CALCULATIONS); // // AD ... ArraySetAsSeries(adBuffer, true); SetIndexBuffer(adBufferIndex, adBuffer, INDICATOR_CALCULATIONS); // // ADX ... ArraySetAsSeries(adxBuffer, true); SetIndexBuffer(adxBufferIndex, adxBuffer, INDICATOR_CALCULATIONS); // // ADX Plus ... ArraySetAsSeries(adxpBuffer, true); SetIndexBuffer(adxpBufferIndex, adxpBuffer, INDICATOR_CALCULATIONS); // // ADX Negative ... ArraySetAsSeries(adxnBuffer, true); SetIndexBuffer(adxnBufferIndex, adxnBuffer, INDICATOR_CALCULATIONS); // // ATR ... ArraySetAsSeries(atrBuffer, true); SetIndexBuffer(atrBufferIndex, atrBuffer, INDICATOR_CALCULATIONS); // // CCI ... ArraySetAsSeries(cciBuffer, true); SetIndexBuffer(cciBufferIndex, cciBuffer, INDICATOR_CALCULATIONS); // // RSI ... ArraySetAsSeries(rsiBuffer, true); SetIndexBuffer(rsiBufferIndex, rsiBuffer, INDICATOR_CALCULATIONS); // // MFI ... ArraySetAsSeries(mfiBuffer, true); SetIndexBuffer(mfiBufferIndex, mfiBuffer, INDICATOR_CALCULATIONS); // // RVI ... ArraySetAsSeries(rviBuffer, true); SetIndexBuffer(rviBufferIndex, rviBuffer, INDICATOR_CALCULATIONS); // // RVI Signal ... ArraySetAsSeries(rviSignalBuffer, true); SetIndexBuffer(rviSignalBufferIndex, rviSignalBuffer, INDICATOR_CALCULATIONS); // // MACD ... ArraySetAsSeries(macdBuffer, true); SetIndexBuffer(macdBufferIndex, macdBuffer, INDICATOR_CALCULATIONS); // // MACD Signal ... ArraySetAsSeries(macdSignalBuffer, true); SetIndexBuffer(macdSignalBufferIndex, macdSignalBuffer, INDICATOR_CALCULATIONS); // // Momentum ... ArraySetAsSeries(momentumBuffer, true); SetIndexBuffer(momentumBufferIndex, momentumBuffer, INDICATOR_CALCULATIONS); // // Stochastics ... ArraySetAsSeries(stochasticBuffer, true); SetIndexBuffer(stochasticBufferIndex, stochasticBuffer, INDICATOR_CALCULATIONS); // // Stochastics Signal ... ArraySetAsSeries(stochasticSignalBuffer, true); SetIndexBuffer(stochasticSignalBufferIndex, stochasticSignalBuffer, INDICATOR_CALCULATIONS); } /** * Calculate Custom Buffers ... * * @param bar_index: Integer, Represent Current Bar ... * @param prevCalculated: Integer, Represent Previous Calculated Bars ... * @param ratesTotal: Integer, Represents All Available Bars ... * @param open: Double Array, History of Open Prices ... * @param high: Double Array, History of High Prices ... * @param close: Double Array, History of Close Prices ... * @param low: Double Array, History of Low Prices ... * @param tickVolume: Long, History of Tick Volumes on Bar ... */ void CalculateBuffers( int bar_index, // Selected Bar Index const int prevCalculated, const int ratesTotal, const double &open[], const double &high[], const double &close[], const double &low[], const long &tickVolume[] // ) { // int barsLimit = startCalculationForLastBars > 0 ? startCalculationForLastBars : 0; // // bool canCalculate = true; bool canCalculate = barsLimit == 0 || bar_index <= barsLimit; if (canCalculate) { // CalculateValues( bar_index, prevCalculated, ratesTotal, open, high, close, low, tickVolume // ); } else { FillBuffersZero(bar_index); } } /** * Fill All Bufers to Zero Vlue for Specified Bar Index ... * * @param barIndex: Integer ... */ void FillBuffersZero(int barIndex) { // acBuffer[barIndex] = emptyValue; adBuffer[barIndex] = emptyValue; adxBuffer[barIndex] = emptyValue; adxpBuffer[barIndex] = emptyValue; adxnBuffer[barIndex] = emptyValue; atrBuffer[barIndex] = emptyValue; cciBuffer[barIndex] = emptyValue; rsiBuffer[barIndex] = emptyValue; mfiBuffer[barIndex] = emptyValue; rviBuffer[barIndex] = emptyValue; macdBuffer[barIndex] = emptyValue; momentumBuffer[barIndex] = emptyValue; rviSignalBuffer[barIndex] = emptyValue; macdSignalBuffer[barIndex] = emptyValue; stochasticBuffer[barIndex] = emptyValue; stochasticSignalBuffer[barIndex] = emptyValue; } /** * Calculate Vales ... * * @param bar_index: Integer, Represent Current Bar ... * @param prevCalculated: Integer, Represent Previous Calculated Bars ... * @param ratesTotal: Integer, Represents All Available Bars ... * @param open: Double Array, History of Open Prices ... * @param high: Double Array, History of High Prices ... * @param close: Double Array, History of Close Prices ... * @param low: Double Array, History of Low Prices ... * @param tickVolume: Long, History of Tick Volumes on Bar ... */ void CalculateValues( int bar_index, // Selected Bar Index const int prevCalculated, const int ratesTotal, const double &open[], const double &high[], const double &close[], const double &low[], const long &tickVolume[] // ) { // // Implement Colorify Mechanism ... } //