/////////////////////////////////////////////////////// // // SaherElm IT Center MQL5 Indicator // ------------------------------------------------- // Name: X121 X3MA // Description: X3MA ... // // // 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 X3MA Indicator" #property strict // // Definitions ... // #define ShortName "X121 X3MA" // // Includes Common Library ... #include "../Libraries/x-saherelm.common.lib.mq5" // // Inputs ... // // Fast ... input group "Fast"; input int fastLength = 6; // Length input ENUM_MA_METHOD fastMethod = MODE_SMA; // Method input ENUM_APPLIED_PRICE fastAppliedTo = PRICE_CLOSE; // Applied To // // Medium ... input group "Medium"; input int mediumLength = 21; // Length input ENUM_MA_METHOD mediumMethod = MODE_SMA; // Method input ENUM_APPLIED_PRICE mediumAppliedTo = PRICE_CLOSE; // Applied To // // Slow ... input group "Slow"; input int slowLength = 34; // Length input ENUM_MA_METHOD slowMethod = MODE_SMA; // Method input ENUM_APPLIED_PRICE slowAppliedTo = PRICE_CLOSE; // Applied To // // Presentation ... input group "Presentation"; // input int startCalculationForLastBars = 1000; // Calculate Last n Bars // input bool applyColor = false; // input bool showFast = true; // Show Fast input bool showMedium = true; // Show Medium input bool showSlow = true; // Show Slow // // Buffers ... // #define hideColorIDX 0 #define bullishColorIDX 1 #define bearishColorIDX 2 #define neuturalColorIDX 3 // #define bullishState 1 #define neuturalState 0 #define bearishState -1 // #define emptyValue 0.0 // #property indicator_chart_window // #property indicator_buffers 9 #property indicator_plots 3 // // Plot Buffers ... // // FAST ... // #define fastBufferIndex 0 double fastBuffer[]; // #define fastColorBufferIndex 1 double fastColorBuffer[]; // #property indicator_label1 "X121 X3MA Fast" #property indicator_type1 DRAW_COLOR_LINE #property indicator_color1 CLR_NONE, clrYellow, clrChocolate, clrGray #property indicator_style1 STYLE_SOLID #property indicator_width1 2 // // MEDIUM ... // #define mediumBufferIndex 2 double mediumBuffer[]; // #define mediumColorBufferIndex 3 double mediumColorBuffer[]; // #property indicator_label2 "X121 X3MA Medium" #property indicator_type2 DRAW_COLOR_LINE #property indicator_color2 CLR_NONE, clrAqua, clrMagenta, clrGray #property indicator_style2 STYLE_DASHDOTDOT #property indicator_width2 2 // // SLOW ... // #define slowBufferIndex 4 double slowBuffer[]; // #define slowColorBufferIndex 5 double slowColorBuffer[]; // #property indicator_label3 "X121 X3MA Slow" #property indicator_type3 DRAW_COLOR_LINE #property indicator_color3 CLR_NONE, clrLime, clrRed, clrGray #property indicator_style3 STYLE_DASH #property indicator_width3 2 // // Data Buffers ... #define mLastBufferIndex 5 // #define fastStateBufferIndex mLastBufferIndex + 1 double fastStateBuffer[]; // #define mediumStateBufferIndex mLastBufferIndex + 2 double mediumStateBuffer[]; // #define slowStateBufferIndex mLastBufferIndex + 3 double slowStateBuffer[]; // // Variables, Properties and etc ... // // this counts Available Bars ... int limit; // int maxLength; // int fastHandler = INVALID_HANDLE; int mediumHandler = INVALID_HANDLE; int slowHandler = INVALID_HANDLE; // // Event Handlers ... /** * Initialize Indicator ... * * @return ( int ) */ int OnInit() { // // Validate Inputs ... if (!ValidateInputs()) { return INIT_PARAMETERS_INCORRECT; } // // Initialize Indicator Handlers ... // // FAST ... fastHandler = iMA( _Symbol, _Period, fastLength, 0, fastMethod, fastAppliedTo // ); bool isInited = fastHandler != INVALID_HANDLE; if (!isInited) { return INIT_FAILED; } // // MEDIUM ... mediumHandler = iMA( _Symbol, _Period, mediumLength, 0, mediumMethod, mediumAppliedTo // ); isInited = mediumHandler != INVALID_HANDLE; if (!isInited) { return INIT_FAILED; } // // SLOW ... slowHandler = iMA( _Symbol, _Period, slowLength, 0, slowMethod, slowAppliedTo // ); isInited = slowHandler != 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(); // // 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(fastHandler); IndicatorRelease(mediumHandler); IndicatorRelease(slowHandler); } /** * 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 ... int fastCalculatedBars = BarsCalculated(fastHandler); // // MEDIUM ... int mediumCalculatedBars = BarsCalculated(mediumHandler); // // SLOW ... int slowCalculatedBars = BarsCalculated(slowHandler); // bool isPassedRequiredCalculatedBars = // // FAST ... fastCalculatedBars >= maxLength && // // MEDIUM ... mediumCalculatedBars >= maxLength && // // SLOW ... slowCalculatedBars >= maxLength // ; if (!isPassedRequiredCalculatedBars) { return prev_calculated; } // limit = (prev_calculated > rates_total || prev_calculated <= 0) ? rates_total : (rates_total - prev_calculated) + 1; // // Buffers Copy ... // // FAST ... int copiedFasts = CopyBuffer(fastHandler, 0, 0, limit, fastBuffer); // // MEDIUM ... int copiedMediumss = CopyBuffer(mediumHandler, 0, 0, limit, mediumBuffer); // // SLOW ... int copiedSlows = CopyBuffer(slowHandler, 0, 0, limit, slowBuffer); // // Validate Copied Items ... bool isPassedRequiredCopiedItems = // // FAST ... copiedFasts >= limit && // // MEDIUM ... copiedMediumss >= limit && // // SLOW ... copiedSlows >= limit // ; 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 = // fastLength > 2 && mediumLength > 2 && slowLength > 2 && mediumLength > fastLength && mediumLength < slowLength // ; // return result; } /** * Extract Max Length of Inputs ... * * @return ( int ) */ int ExtractMaxLengthOfInputs() { // int result = 0; // result = MathMax(fastLength, mediumLength); result = MathMax(result, slowLength); // return result; } /** * Define Required Buffers ... */ void DefineBuffers() { // // Plot Buffers ... // // FAST ... // ArraySetAsSeries(fastBuffer, true); SetIndexBuffer(fastBufferIndex, fastBuffer, INDICATOR_DATA); // PlotIndexSetInteger(fastBufferIndex, PLOT_SHOW_DATA, showFast); // PlotIndexSetDouble(fastBufferIndex, PLOT_EMPTY_VALUE, emptyValue); // ArraySetAsSeries(fastColorBuffer, true); SetIndexBuffer(fastColorBufferIndex, fastColorBuffer, INDICATOR_COLOR_INDEX); // // MEDIUM ... // ArraySetAsSeries(mediumBuffer, true); SetIndexBuffer(mediumBufferIndex, mediumBuffer, INDICATOR_DATA); // PlotIndexSetInteger(mediumBufferIndex, PLOT_SHOW_DATA, showMedium); // PlotIndexSetDouble(mediumBufferIndex, PLOT_EMPTY_VALUE, emptyValue); // ArraySetAsSeries(mediumColorBuffer, true); SetIndexBuffer(mediumColorBufferIndex, mediumColorBuffer, INDICATOR_COLOR_INDEX); // // SLOW ... // ArraySetAsSeries(slowBuffer, true); SetIndexBuffer(slowBufferIndex, slowBuffer, INDICATOR_DATA); // PlotIndexSetInteger(slowBufferIndex, PLOT_SHOW_DATA, showSlow); // PlotIndexSetDouble(slowBufferIndex, PLOT_EMPTY_VALUE, emptyValue); // ArraySetAsSeries(slowColorBuffer, true); SetIndexBuffer(slowColorBufferIndex, slowColorBuffer, INDICATOR_COLOR_INDEX); // // Data Buffers ... // // FAST State ... ArraySetAsSeries(fastStateBuffer, true); SetIndexBuffer(fastStateBufferIndex, fastStateBuffer, INDICATOR_CALCULATIONS); // // MEDIUM State ... ArraySetAsSeries(mediumStateBuffer, true); SetIndexBuffer(mediumStateBufferIndex, mediumStateBuffer, INDICATOR_CALCULATIONS); // // SLOW State ... ArraySetAsSeries(slowStateBuffer, true); SetIndexBuffer(slowStateBufferIndex, slowStateBuffer, INDICATOR_CALCULATIONS); } /** * Set Indicator Short Name and also we can define Buffers Labels ... */ void SetIndicatorName() { // IndicatorSetInteger(INDICATOR_DIGITS, _Digits); IndicatorSetString(INDICATOR_SHORTNAME, ShortName); } /** * 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) { // // Calculate Values ... 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) { // // FAST ... fastBuffer[barIndex] = emptyValue; fastColorBuffer[barIndex] = hideColorIDX; fastStateBuffer[barIndex] = neuturalState; // // MEDIUM ... mediumBuffer[barIndex] = emptyValue; mediumColorBuffer[barIndex] = hideColorIDX; mediumStateBuffer[barIndex] = neuturalState; // // SLOW ... slowBuffer[barIndex] = emptyValue; slowColorBuffer[barIndex] = hideColorIDX; slowStateBuffer[barIndex] = neuturalState; } /** * Calculate Values ... * * @param bar_index: int, Specified Bar Index ... * @param prevCalculated: int, Provides Previous Calculated Bars ... * @param ratesTotal: int, Provides All Availabled Bars ... * @param open: double Collection, Provides Open Prices Time Series ... * @param high: double Collection, Provides High Prices Time Series ... * @param close: double Collection, Provides Close Prices Time Series ... * @param low: double Collection, Provides Low Prices Time Series ... * @param tickVolume: long Collection, Provides Ticks Volume Prices Time Series ... */ 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[] // ) { // // Check Prev Bar ... // int lastBarIndex = bar_index + 1; bool isFirstBar = startCalculationForLastBars > 0 ? bar_index == startCalculationForLastBars : bar_index == ratesTotal; // double iLow = low[bar_index]; double iHigh = high[bar_index]; double iClose = close[bar_index]; // // FAST ... // double iFast = fastBuffer[bar_index]; // double iFastState = iLow > iFast ? bullishState : iHigh < iFast ? bearishState : neuturalState; double iFastColor = hideColorIDX; if (showFast) { // if (applyColor) { // iFastColor = iFastState == bullishState ? bullishColorIDX : iFastState == bearishState ? bearishColorIDX : neuturalColorIDX; } else { iFastColor = bullishColorIDX; } } fastColorBuffer[bar_index] = iFastColor; fastStateBuffer[bar_index] = iFastState; // // MEDIUM ... // double iMedium = mediumBuffer[bar_index]; // double iMediumState = iLow > iMedium ? bullishState : iHigh < iMedium ? bearishState : neuturalState; double iMediumColor = hideColorIDX; if (showMedium) { // if (applyColor) { // iMediumColor = iMediumState == bullishState ? bullishColorIDX : iMediumState == bearishState ? bearishColorIDX : neuturalColorIDX; } else { iMediumColor = bullishColorIDX; } } mediumColorBuffer[bar_index] = iMediumColor; mediumStateBuffer[bar_index] = iMediumState; // // SLOW ... // double iSlow = slowBuffer[bar_index]; // double iSlowState = iLow > iSlow ? bullishState : iHigh < iSlow ? bearishState : neuturalState; double iSlowColor = hideColorIDX; if (showSlow) { // if (applyColor) { // iSlowColor = iSlowState == bullishState ? bullishColorIDX : iSlowState == bearishState ? bearishColorIDX : neuturalColorIDX; } else { iSlowColor = bullishColorIDX; } } slowColorBuffer[bar_index] = iSlowColor; slowStateBuffer[bar_index] = iSlowState; } //