/////////////////////////////////////////////////////// // // SaherElm IT Center MQL5 Indicator // ------------------------------------------------- // Name: X121 XATR // Description: XATR ... // // // 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 XATR Indicator" #property strict // // Definitions ... // #define ShortName "X121 XATR" // // Includes Common Library ... #include "../Libraries/x-saherelm.common.lib.mq5" // // Inputs ... // // Market ... input group "Market"; // input group "RSI Detection"; input int rsiLength = 14; // Length input ENUM_APPLIED_PRICE rsiAppliedTo = PRICE_CLOSE; // Applied To // input group "ATR Detection"; input int atrLength = 14; // Length input double atrMultiplier = 1; // Multiplier input ENUM_APPLIED_PRICE atrUpperAppliedTo = PRICE_HIGH; // Upper Zone Applied To input ENUM_APPLIED_PRICE atrLowerAppliedTo = PRICE_LOW; // Lower Zone Applied To input ENUM_X_MA_METHOD atrSmoothingMode = X_MA_MODE_EMA; // Smoothing Method // input group "Presentation"; // input int startCalculationForLastBars = 1000; // Calculate Last n Bars // input bool showAtrUpper = true; // Show Upper Zone input bool showAtrLower = true; // Show Lower Zone // // Buffers ... // #define hideColorIDX 0 #define bullishColorIDX 1 #define bearishColorIDX 2 #define neuturalColorIDX 3 // #property indicator_chart_window // #property indicator_buffers 6 #property indicator_plots 2 // // ATR ... // // Upper ... // #define atrUpperBufferIndex 0 double atrUpperBuffer[]; #property indicator_label1 "X121 ATRU" #property indicator_type1 DRAW_LINE #property indicator_color1 clrYellow #property indicator_style1 STYLE_SOLID #property indicator_width1 2 // // Lower ... // #define atrLowerBufferIndex 1 double atrLowerBuffer[]; #property indicator_label2 "X121 ATRL" #property indicator_type2 DRAW_LINE #property indicator_color2 clrYellow #property indicator_style2 STYLE_SOLID #property indicator_width2 2 // // Data Buffers ... // int mLastBufferIndex = 1; // // ATR ... #define atrBufferIndex mLastBufferIndex + 1 double atrBuffer[]; #define atrUpperRawBufferIndex mLastBufferIndex + 2 double atrUpperRawBuffer[]; #define atrLowerRawBufferIndex mLastBufferIndex + 3 double atrLowerRawBuffer[]; // // RSI ... #define rsiBufferIndex mLastBufferIndex + 4 double rsiBuffer[]; // // Variables, Properties and etc ... // // this counts Available Bars ... int limit; // int maxLength; // // RSI Handler ... int rsiHandler = INVALID_HANDLE; // // ATR Handler ... int atrHandler = INVALID_HANDLE; // bool isStrTrendChanged; bool isStrStartBearishTrend; bool isStrStartBullishTrend; // // Event Handlers ... /** * Initialize Indicator ... * * @return ( int ) */ int OnInit() { // // Validate Inputs ... if (!ValidateInputs()) { return INIT_PARAMETERS_INCORRECT; } // // Initialize Indicator Handlers ... // // RSI ... rsiHandler = iRSI( _Symbol, _Period, rsiLength, rsiAppliedTo // ); bool isInited = rsiHandler != INVALID_HANDLE; if (!isInited) { return INIT_FAILED; } // // ATR ... atrHandler = iATR( _Symbol, _Period, atrLength // ); isInited = atrHandler != 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(rsiHandler); IndicatorRelease(atrHandler); } /** * 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); // // Fill All Buffers by Zero ... if (prev_calculated == 0) { } // // Validate Calculated Bars ... // // RSI ... int rsiCalculatedBars = BarsCalculated(rsiHandler); // // ATR ... int atrCalculatedBars = BarsCalculated(atrHandler); // bool isPassedRequiredCalculatedBars = // // RSI ... rsiCalculatedBars >= maxLength && // // ATR ... atrCalculatedBars >= maxLength // ; if (!isPassedRequiredCalculatedBars) { return prev_calculated; } // limit = (prev_calculated > rates_total || prev_calculated <= 0) ? rates_total : (rates_total - prev_calculated) + 1; // // Buffers Copy ... // // RSI ... int copiedRsis = CopyBuffer(rsiHandler, 0, 0, limit, rsiBuffer); // // ATR ... int copiedAtrs = CopyBuffer(atrHandler, 0, 0, limit, atrBuffer); // // Validate Copied Items ... bool isPassedRequiredCopiedItems = // // // RSI ... copiedRsis > 0 && // // ATR ... copiedAtrs > 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 = // // RSI ... rsiLength > 0 && // // ATR ... atrLength > 0 && atrMultiplier > 0 // ; // return result; } /** * Extract Max Length of Inputs ... * * @return ( int ) */ int ExtractMaxLengthOfInputs() { // int result = 0; // result = MathMax(rsiLength, atrLength); // return result; } /** * Define Required Buffers ... */ void DefineBuffers() { // // ATR ... // // UPPER ... bool canShowAtrUpper = showAtrUpper; ENUM_DRAW_TYPE atrUpperDrawType = canShowAtrUpper ? DRAW_LINE : DRAW_NONE; ArraySetAsSeries(atrUpperBuffer, true); SetIndexBuffer(atrUpperBufferIndex, atrUpperBuffer, INDICATOR_DATA); PlotIndexSetDouble(atrUpperBufferIndex, PLOT_EMPTY_VALUE, EMPTY_VALUE); PlotIndexSetInteger(atrUpperBufferIndex, PLOT_SHOW_DATA, canShowAtrUpper); PlotIndexSetInteger(atrUpperBufferIndex, PLOT_DRAW_TYPE, atrUpperDrawType); // // LOWER ... bool canShowAtrLower = showAtrLower; ENUM_DRAW_TYPE atrLowerDrawType = canShowAtrLower ? DRAW_LINE : DRAW_NONE; ArraySetAsSeries(atrLowerBuffer, true); SetIndexBuffer(atrLowerBufferIndex, atrLowerBuffer, INDICATOR_DATA); PlotIndexSetDouble(atrLowerBufferIndex, PLOT_EMPTY_VALUE, EMPTY_VALUE); PlotIndexSetInteger(atrLowerBufferIndex, PLOT_SHOW_DATA, canShowAtrLower); PlotIndexSetInteger(atrLowerBufferIndex, PLOT_DRAW_TYPE, atrLowerDrawType); // // Data Buffers ... // // ATR ... // ArraySetAsSeries(atrBuffer, true); SetIndexBuffer(atrBufferIndex, atrBuffer, INDICATOR_CALCULATIONS); // ArraySetAsSeries(atrUpperRawBuffer, true); SetIndexBuffer(atrUpperRawBufferIndex, atrUpperRawBuffer, INDICATOR_CALCULATIONS); // ArraySetAsSeries(atrLowerRawBuffer, true); SetIndexBuffer(atrLowerRawBufferIndex, atrLowerRawBuffer, INDICATOR_CALCULATIONS); // // RSI ... // ArraySetAsSeries(rsiBuffer, true); SetIndexBuffer(rsiBufferIndex, rsiBuffer, INDICATOR_CALCULATIONS); } /** * Set Indicator Short Name and also we can define Buffers Labels ... */ void SetIndicatorName() { 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) { // CalculateAtrZones( bar_index, prevCalculated, ratesTotal, open, high, close, low // ); } else { FillBuffersZero(bar_index); } } // // Custom ... /** * Fill All Bufers to Zero Vlue for Specified Bar Index ... * * @param barIndex: Integer ... */ void FillBuffersZero(int barIndex) { // // RSIS ... rsiBuffer[barIndex] = 0; // // ATRS ... atrBuffer[barIndex] = 0; atrUpperBuffer[barIndex] = 0; atrUpperRawBuffer[barIndex] = 0; atrUpperBuffer[barIndex] = 0; atrLowerRawBuffer[barIndex] = 0; } /** * Calculate ATR Zones ... * * @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 ... */ void CalculateAtrZones( int bar_index, // Selected Bar Index const int prevCalculated, const int ratesTotal, const double &open[], const double &high[], const double &close[], const double &low[] // ) { // // Select ATr Value ... double atrValue = atrBuffer[bar_index]; double appliedAtrMultiplierValue = atrValue * atrMultiplier; // // Select Upper Price ... double upperPrice = GetAppliedPrice( atrUpperAppliedTo, open, high, low, close, bar_index // ); // // Select Lower Price ... double lowerPrice = GetAppliedPrice( atrLowerAppliedTo, open, high, low, close, bar_index // ); // // Calculate Raw Atrs ... // // Upper ... double atrUpperRawValue = upperPrice + appliedAtrMultiplierValue; atrUpperRawBuffer[bar_index] = atrUpperRawValue; // // Lower ... double atrLowerRawValue = lowerPrice - appliedAtrMultiplierValue; atrLowerRawBuffer[bar_index] = atrLowerRawValue; // bool canSmooth = atrSmoothingMode != X_MA_MODE_NONE; if (canSmooth) { // // Upper ... iMAOnBuffer( ratesTotal, prevCalculated, bar_index, atrLength, atrUpperRawBuffer, atrUpperBuffer, atrSmoothingMode // ); // // Lower ... iMAOnBuffer( ratesTotal, prevCalculated, bar_index, atrLength, atrLowerRawBuffer, atrLowerBuffer, atrSmoothingMode // ); } else { // // Use Raw Values ... atrUpperBuffer[bar_index] = atrUpperRawValue; atrLowerBuffer[bar_index] = atrLowerRawValue; } } //