/////////////////////////////////////////////////////// // // 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 ... // // Market ... input group "Market"; input int fastMALength = 20; // Fast MA Length input int midMALength = 50; // Mid MA Length input int slowMALength = 200; // Slow MA Length input ENUM_MA_METHOD maMethod = MODE_EMA; // MA Method input ENUM_APPLIED_PRICE maAppliedTo = PRICE_CLOSE; // Applied To // // Presentation ... input group "Presentation"; // input int startCalculationForLastBars = 1000; // Calculate Last n Bars input bool showMaFast = true; // Show Fast MA input bool showMaMid = true; // Show Mid MA input bool showMaSlow = true; // Show Slow MA // // Buffers ... // #define hideColorIDX 0 #define bullishColorIDX 1 #define bearishColorIDX 2 #define neuturalColorIDX 3 // #property indicator_chart_window // #property indicator_buffers 9 #property indicator_plots 3 // // Plot Buffers ... // // FAST ... #define maFastBufferIndex 0 double maFastBuffer[]; #define maFastColorBufferIndex 1 double maFastColorBuffer[]; // #define maFastPlotBufferIndex 0 #property indicator_label1 "X121 MAF" #property indicator_type1 DRAW_COLOR_LINE #property indicator_color1 CLR_NONE, clrAqua, clrMagenta, clrGray #property indicator_style1 STYLE_SOLID #property indicator_width1 2 // // MID ... #define maMidBufferIndex 2 double maMidBuffer[]; #define maMidColorBufferIndex 3 double maMidColorBuffer[]; // #define maMidPlotBufferIndex 1 #property indicator_label2 "X121 MAM" #property indicator_type2 DRAW_COLOR_LINE #property indicator_color2 CLR_NONE, clrGreen, clrRed, clrGray #property indicator_style2 STYLE_SOLID #property indicator_width2 2 // // SLOW ... #define maSlowBufferIndex 4 double maSlowBuffer[]; #define maSlowColorBufferIndex 5 double maSlowColorBuffer[]; // #define maSlowPlotBufferIndex 2 #property indicator_label3 "X121 MAS" #property indicator_type3 DRAW_COLOR_LINE #property indicator_color3 CLR_NONE, clrSteelBlue, clrDarkRed, clrGray #property indicator_style3 STYLE_SOLID #property indicator_width3 2 // // Data Buffers ... // int mLastBufferIndex = 5; // // Fast State ... #define maFastStateBufferIndex mLastBufferIndex + 1 double maFastStateBuffer[]; // // Mid State ... #define maMidStateBufferIndex mLastBufferIndex + 2 double maMidStateBuffer[]; // // Slow State ... #define maSlowStateBufferIndex mLastBufferIndex + 3 double maSlowStateBuffer[]; // // Variables, Properties and etc ... // // this counts Available Bars ... int limit; // int maxLength; // int maFastHandler = INVALID_HANDLE; int maMidHandler = INVALID_HANDLE; int maSlowHandler = INVALID_HANDLE; // // Event Handlers ... /** * Initialize Indicator ... * * @return ( int ) */ int OnInit() { // // Validate Inputs ... if (!ValidateInputs()) { return INIT_PARAMETERS_INCORRECT; } // // Initialize Indicator Handlers ... // // Fast MA ... maFastHandler = iMA( _Symbol, _Period, fastMALength, 0, maMethod, maAppliedTo // ); bool isInited = maFastHandler != INVALID_HANDLE; if (!isInited) { return INIT_FAILED; } // // Mid MA ... maMidHandler = iMA( _Symbol, _Period, midMALength, 0, maMethod, maAppliedTo // ); isInited = maMidHandler != INVALID_HANDLE; if (!isInited) { return INIT_FAILED; } // // Slow MA ... maSlowHandler = iMA( _Symbol, _Period, slowMALength, 0, maMethod, maAppliedTo // ); isInited = maSlowHandler != INVALID_HANDLE; if (!isInited) { 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 // IndicatorRelease(maFastHandler); IndicatorRelease(maMidHandler); IndicatorRelease(maSlowHandler); } /** * 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 ... // // Fast MA ... int maFastCalculatedBars = BarsCalculated(maFastHandler); // // Mid MA ... int maMidCalculatedBars = BarsCalculated(maMidHandler); // // Slow MA ... int maSlowCalculatedBars = BarsCalculated(maSlowHandler); // bool isPassedRequiredCalculatedBars = // // Fast MA ... maFastCalculatedBars >= maxLength && // // Slow MA ... maMidCalculatedBars >= maxLength && // // Slow MA ... maSlowCalculatedBars >= maxLength // ; if (!isPassedRequiredCalculatedBars) { return prev_calculated; } // limit = (prev_calculated > rates_total || prev_calculated <= 0) ? rates_total : (rates_total - prev_calculated) + 1; // // Buffers Copy ... // // Fast MA ... int copiedFastMas = CopyBuffer(maFastHandler, 0, 0, limit, maFastBuffer); // // Mid MA ... int copiedMidMas = CopyBuffer(maMidHandler, 0, 0, limit, maMidBuffer); // // Slow MA ... int copiedSlowMas = CopyBuffer(maSlowHandler, 0, 0, limit, maSlowBuffer); // // Validate Copied Items ... bool isPassedRequiredCopiedItems = // // Fast MA ... copiedFastMas > 0 && // // Mid MA ... copiedMidMas > 0 && // // Slow MA ... copiedSlowMas > 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 = fastMALength > 0 && midMALength > 0 && slowMALength > 0 && midMALength > fastMALength && slowMALength > midMALength; // return result; } /** * Extract Max Length of Inputs ... * * @return ( int ) */ int ExtractMaxLengthOfInputs() { // int result = 0; // result = MathMax(fastMALength, midMALength); result = MathMax(result, slowMALength); // return result; } /** * Set Indicator Short Name and also we can define Buffers Labels ... */ void SetIndicatorName() { IndicatorSetString(INDICATOR_SHORTNAME, ShortName); } /** * Define Required Buffers ... */ void DefineBuffers() { // // Plot Buffers ... // // Fast ... ArraySetAsSeries(maFastBuffer, true); ArraySetAsSeries(maFastColorBuffer, true); SetIndexBuffer(maFastBufferIndex, maFastBuffer, INDICATOR_DATA); SetIndexBuffer(maFastColorBufferIndex, maFastColorBuffer, INDICATOR_COLOR_INDEX); // // Mid ... ArraySetAsSeries(maMidBuffer, true); ArraySetAsSeries(maMidColorBuffer, true); SetIndexBuffer(maMidBufferIndex, maMidBuffer, INDICATOR_DATA); SetIndexBuffer(maMidColorBufferIndex, maMidColorBuffer, INDICATOR_COLOR_INDEX); // // Slow ... ArraySetAsSeries(maSlowBuffer, true); ArraySetAsSeries(maSlowColorBuffer, true); SetIndexBuffer(maSlowBufferIndex, maSlowBuffer, INDICATOR_DATA); SetIndexBuffer(maSlowColorBufferIndex, maSlowColorBuffer, INDICATOR_COLOR_INDEX); // // Data Buffers ... // // Fast State ... ArraySetAsSeries(maFastStateBuffer, true); SetIndexBuffer(maFastStateBufferIndex, maFastStateBuffer, INDICATOR_CALCULATIONS); // // Mid State ... ArraySetAsSeries(maMidStateBuffer, true); SetIndexBuffer(maMidStateBufferIndex, maMidStateBuffer, INDICATOR_CALCULATIONS); // // Slow State ... ArraySetAsSeries(maSlowStateBuffer, true); SetIndexBuffer(maSlowStateBufferIndex, maSlowStateBuffer, 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) { // maMidBuffer[barIndex] = 0; maFastBuffer[barIndex] = 0; maSlowBuffer[barIndex] = 0; // maFastColorBuffer[barIndex] = hideColorIDX; maMidColorBuffer[barIndex] = hideColorIDX; maSlowColorBuffer[barIndex] = hideColorIDX; maFastStateBuffer[barIndex] = hideColorIDX; maMidStateBuffer[barIndex] = hideColorIDX; maSlowStateBuffer[barIndex] = hideColorIDX; } /** * 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 ... // // Calculate Applied Price ... ENUM_X_PRICE mPType = ToXPrice(maAppliedTo); double iPrice = GetAppliedPrice( mPType, open, high, low, close, bar_index // ); // double iFast = maFastBuffer[bar_index]; double iMid = maMidBuffer[bar_index]; double iSlow = maSlowBuffer[bar_index]; // double iFastState = iPrice > iFast ? bullishColorIDX : iPrice < iFast ? bearishColorIDX : neuturalColorIDX; double iMidState = iPrice > iMid ? bullishColorIDX : iPrice < iMid ? bearishColorIDX : neuturalColorIDX; double iSlowState = iPrice > iSlow ? bullishColorIDX : iPrice < iSlow ? bearishColorIDX : neuturalColorIDX; // maFastColorBuffer[bar_index] = showMaFast ? iFastState : hideColorIDX; maFastStateBuffer[bar_index] = iFastState; // maMidColorBuffer[bar_index] = showMaMid ? iMidState : hideColorIDX; maMidStateBuffer[bar_index] = iMidState; // maSlowColorBuffer[bar_index] = showMaSlow ? iSlowState : hideColorIDX; maSlowStateBuffer[bar_index] = iSlowState; } //