/////////////////////////////////////////////////////// // // SaherElm IT Center MQL5 Indicator // ------------------------------------------------- // Name: X121 XTREND // Description: XTREND ... // // // 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 XTREND Indicator" #property strict // // Definitions ... // #define ShortName "X121 XTREND" // // Includes Common Library ... #include "../Libraries/x-saherelm.common.lib.mq5" // // Inputs ... // // Market ... input group "Market"; input ENUM_APPLIED_PRICE trendPriceType = PRICE_CLOSE; // Detect Trend Price Type input group "MA Detection"; input int maLength = 50; // MA Length input ENUM_MA_METHOD maMethod = MODE_EMA; // MA Method input ENUM_APPLIED_PRICE maAppliedTo = PRICE_CLOSE; // MA Applied To input group "ADX Detection"; input int adxLength = 14; // ADX Length input double adxThreshold = 25; // ADX Threshold for Strong Trends // // Presentation ... input group "Presentation"; // input int startCalculationForLastBars = 1000; // Calculate Last n Bars input int upTrendArrowCode = 233; // Up Trend Arrow Code input int downTrendArrowCode = 234; // Down Trend Arrow Code // input bool showOnlyTrendStart = true; // Show Only Trend Start input bool showUpTrend = true; // Show Up Trend input bool showDownTrend = true; // Show Down Trend // #property indicator_chart_window // #property indicator_buffers 8 #property indicator_plots 2 // // Plot Buffers ... // #define upTrendBufferIndex 0 double upTrendBuffer[]; // #property indicator_label1 "X121 XTREND Up" #property indicator_type1 DRAW_ARROW #property indicator_color1 clrAqua #property indicator_width1 1 // #define downTrendBufferIndex 1 double downTrendBuffer[]; // #property indicator_label2 "X121 XTREND Down" #property indicator_type2 DRAW_ARROW #property indicator_color2 clrMagenta #property indicator_width2 1 // // Data Buffers ... // int mLastBufferIndex = 1; // #define maBufferIndex mLastBufferIndex + 1 double maBuffer[]; // #define adxBufferIndex mLastBufferIndex + 2 double adxBuffer[]; // #define adxPBufferIndex mLastBufferIndex + 3 double adxPBuffer[]; // #define adxMBufferIndex mLastBufferIndex + 4 double adxMBuffer[]; // #define upTBufferIndex mLastBufferIndex + 5 double upTBuffer[]; // #define downTBufferIndex mLastBufferIndex + 6 double downTBuffer[]; // // Variables, Properties and etc ... // // this counts Available Bars ... int limit; // int maxLength; // double lastValue = 0; ENUM_X_DIRECTION lastTrendDir = X_DIRECTION_NONE; // // MA Handler ... int maHandler = INVALID_HANDLE; // // ADX Handler ... int adxHandler = INVALID_HANDLE; // // Event Handlers ... /** * Initialize Indicator ... * * @return ( int ) */ int OnInit() { // // Validate Inputs ... if (!ValidateInputs()) { return INIT_PARAMETERS_INCORRECT; } // // Initialize Indicator Handlers ... // // MA ... maHandler = iMA( _Symbol, _Period, maLength, 0, maMethod, maAppliedTo // ); bool isInited = maHandler != INVALID_HANDLE; if (!isInited) { return INIT_FAILED; } // // ADx ... adxHandler = iADX( _Symbol, _Period, adxLength // ); isInited = maHandler != 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(maHandler); IndicatorRelease(adxHandler); } /** * 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 ... // // MA ... int maCalculatedBars = BarsCalculated(maHandler); // // ADX ... int adxCalculatedBars = BarsCalculated(adxHandler); // bool isPassedRequiredCalculatedBars = // // MA ... maCalculatedBars >= maxLength && // // ADX ... adxCalculatedBars >= maxLength // ; if (!isPassedRequiredCalculatedBars) { return prev_calculated; } // limit = (prev_calculated > rates_total || prev_calculated <= 0) ? rates_total : (rates_total - prev_calculated) + 1; // // Buffers Copy ... // // MA ... int copiedMas = CopyBuffer(maHandler, 0, 0, limit, maBuffer); // // ADX ... int copiedAdxs = CopyBuffer(adxHandler, MAIN_LINE, 0, limit, adxBuffer); int copiedAdxPs = CopyBuffer(adxHandler, PLUSDI_LINE, 0, limit, adxPBuffer); int copiedAdxMs = CopyBuffer(adxHandler, MINUSDI_LINE, 0, limit, adxMBuffer); // // Validate Copied Items ... bool isPassedRequiredCopiedItems = // // MA ... copiedMas > 0 && // // ADX ... copiedAdxs > 0 && copiedAdxPs > 0 && copiedAdxMs > 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 = // // MA ... maLength > 0 && // // ADX ... adxLength > 0 && adxThreshold > 0 // ; // return result; } /** * Extract Max Length of Inputs ... * * @return ( int ) */ int ExtractMaxLengthOfInputs() { // int result = 0; // result = MathMax(maLength, adxLength); // 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 ... // // UP Trend ... // ENUM_DRAW_TYPE upTrendDrawType = showUpTrend ? DRAW_ARROW : DRAW_NONE; // ArraySetAsSeries(upTrendBuffer, true); SetIndexBuffer(upTrendBufferIndex, upTrendBuffer, INDICATOR_DATA); // PlotIndexSetInteger(upTrendBufferIndex, PLOT_SHOW_DATA, showUpTrend); PlotIndexSetInteger(upTrendBufferIndex, PLOT_DRAW_TYPE, upTrendDrawType); // PlotIndexSetDouble(upTrendBufferIndex, PLOT_EMPTY_VALUE, 0); PlotIndexSetInteger(upTrendBufferIndex, PLOT_ARROW, upTrendArrowCode); // // DOWN Trend ... // ENUM_DRAW_TYPE downTrendDrawType = showDownTrend ? DRAW_ARROW : DRAW_NONE; // ArraySetAsSeries(downTrendBuffer, true); SetIndexBuffer(downTrendBufferIndex, downTrendBuffer, INDICATOR_DATA); // PlotIndexSetInteger(downTrendBufferIndex, PLOT_SHOW_DATA, showDownTrend); PlotIndexSetInteger(downTrendBufferIndex, PLOT_DRAW_TYPE, downTrendDrawType); // PlotIndexSetDouble(downTrendBufferIndex, PLOT_EMPTY_VALUE, 0); PlotIndexSetInteger(downTrendBufferIndex, PLOT_ARROW, downTrendArrowCode); // // Data Buffers ... // ArraySetAsSeries(maBuffer, true); SetIndexBuffer(maBufferIndex, maBuffer, INDICATOR_CALCULATIONS); // ArraySetAsSeries(adxBuffer, true); SetIndexBuffer(adxBufferIndex, adxBuffer, INDICATOR_CALCULATIONS); // ArraySetAsSeries(adxPBuffer, true); SetIndexBuffer(adxPBufferIndex, adxPBuffer, INDICATOR_CALCULATIONS); // ArraySetAsSeries(adxMBuffer, true); SetIndexBuffer(adxMBufferIndex, adxMBuffer, INDICATOR_CALCULATIONS); // ArraySetAsSeries(upTBuffer, true); SetIndexBuffer(upTBufferIndex, upTBuffer, INDICATOR_CALCULATIONS); // ArraySetAsSeries(downTBuffer, true); SetIndexBuffer(downTBufferIndex, downTBuffer, 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) { // CalculateTrend( 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) { // upTrendBuffer[barIndex] = 0; downTrendBuffer[barIndex] = 0; // maBuffer[barIndex] = 0; adxBuffer[barIndex] = 0; adxPBuffer[barIndex] = 0; adxMBuffer[barIndex] = 0; } /** * Calculate Trends ... * * @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 CalculateTrend( 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[] // ) { // double iPrice = GetAppliedPrice( trendPriceType, open, high, low, close, bar_index // ); // double iMa = maBuffer[bar_index]; double iAdx = adxBuffer[bar_index]; double points = GetPoints(_Symbol); double points10 = 10 * points; // bool isUpTrend = iPrice > iMa && iAdx > adxThreshold; bool isDownTrend = iPrice < iMa && iAdx > adxThreshold; // if (isUpTrend) { // // double iLastValue = // prevCalculated <= 0 // ? 0 // : upTrendBuffer[bar_index + 1]; // if (iLastValue > 0 && // iValue > iLastValue) // { // iValue = iLastValue; // } // double selectedValue = low[bar_index] - points10; // double iValue = !IsBullish(lastTrendDir) ? selectedValue : !showOnlyTrendStart ? lastValue : 0; // upTrendBuffer[bar_index] = iValue; upTBuffer[bar_index] = selectedValue; // downTrendBuffer[bar_index] = 0; // lastValue = iValue; lastTrendDir = X_DIRECTION_BULLISH; } else if (isDownTrend) { // // double iLastValue = // prevCalculated <= 0 // ? 0 // : downTrendBuffer[bar_index + 1]; // if (iLastValue > 0 && // iValue < iLastValue) // { // iValue = iLastValue; // } // double selectedValue = high[bar_index] + points10; // double iValue = !IsBearish(lastTrendDir) ? selectedValue : !showOnlyTrendStart ? lastValue : 0; // upTrendBuffer[bar_index] = 0; downTrendBuffer[bar_index] = iValue; downTBuffer[bar_index] = selectedValue; // lastValue = iValue; lastTrendDir = X_DIRECTION_BEARISH; } else { // double iValue = IsBullish(lastTrendDir) ? showOnlyTrendStart ? 0 : lastValue : IsBearish(lastTrendDir) ? showOnlyTrendStart ? 0 : lastValue : 0; // upTrendBuffer[bar_index] = 0; downTrendBuffer[bar_index] = 0; // upTBuffer[bar_index] = 0; downTBuffer[bar_index] = 0; // lastValue = 0; lastTrendDir = X_DIRECTION_NONE; } }