/////////////////////////////////////////////////////// // // SaherElm IT Center MQL5 XATR Indicator // --------------------------------------------- // Name: XATR // Description: Moving Average Indicator // Market Analysor ... // // // 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 XATR Indicator" #property strict // // START Constants ... // #define ShortName "XATR" // // END Constants ... // // // START Inputs ... // input int length = 14; // Length input double multiplier = 1.6; // Multiplier input ENUM_APPLIED_PRICE longAppliedTo = PRICE_CLOSE; // Long ATR Source input ENUM_APPLIED_PRICE shortAppliedTo = PRICE_OPEN; // Short ATR Source // // END Inputs ... // // // Includes Logging Library ... #include "../Libraries/x-saherelm.log.lib.mq5"; // // Includes Common Library ... #include "../Libraries/x-saherelm.common.lib.mq5"; // // START Buffers ... // #property indicator_chart_window // #property indicator_buffers 3 #property indicator_plots 2 // // LONG Buffer ... #define longSLBufferIndex 0 // double longSLBuffer[]; // #property indicator_label1 "XATR Long SL" #property indicator_type1 DRAW_LINE #property indicator_color1 clrAqua #property indicator_style1 STYLE_DOT #property indicator_width1 1 // // SHORT Buffer ... #define shortSLBufferIndex 1 // double shortSLBuffer[]; // #property indicator_label2 "XATR Short SL" #property indicator_type2 DRAW_LINE #property indicator_color2 clrFuchsia #property indicator_style2 STYLE_DOT #property indicator_width2 1 // // ATR Buffer ... #define atrBufferIndex 2 // double atrBuffer[]; // // END Buffers ... // // // START Global Definitions: Variables, Properties and etc ... // int atrHandler = INVALID_HANDLE; // // END Global Definitions: Variables, Properties and etc ... // // // START Event Handlers ... // // // Initialization ... int OnInit() { // // Validate Inputs ... if (!ValidateInputs()) { return INIT_PARAMETERS_INCORRECT; } // logTag = ShortName; // // ATR Handler ... atrHandler = iATR( _Symbol, _Period, length); if (atrHandler == INVALID_HANDLE) { // string message = "Error Initializing ATR Indicator ..."; LogMessage(message); // return INIT_FAILED; } // // Define Index Buffers ... DefineBuffers(); // // Set Indicator ShortName ... SetIndicatorName(); // // Init Succeed ... return INIT_SUCCEEDED; } // // DeInitialization ... 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 // // Here we can handle De Initialization Reasons ... } // // Calculations ... // // Calculating what we want ... int OnCalculate( // // total Candles on chart ... const int rates_total, // // total calculated Candles on charts ... const int prev_calculated, // // history of Candles Open Time ... const datetime &time[], // // history of Candles Open Price ... const double &open[], // // history of Candles High Price ... const double &high[], // // history of Candles Low Price ... const double &low[], // // history of Candles Close Price ... const double &close[], // // history of Tick Volumes on Candle ... const long &tick_volume[], // // history of Trade Volumes ... const long &volume[], // // history of Candles Spread Price ... 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); // // this counts Available Bars ... int limit; // // 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 ... int maxLength = MathMax(0, length); // // Check Calculated Bars ... int atrCalculatedBars = BarsCalculated(atrHandler); if (atrCalculatedBars < rates_total) { return prev_calculated; } // limit = (prev_calculated > rates_total || prev_calculated <= 0) ? rates_total : (rates_total - prev_calculated) + 1; // // Buffers Copy ... int atrCopiedItems = CopyBuffer(atrHandler, 0, 0, limit, atrBuffer); if (atrCopiedItems <= 0) { return prev_calculated; } // // Main Loop ... for (int i = limit - 1; i >= 0 && !IsStopped(); i--) { // CalculateBuffers(i); } // return rates_total; } // // END Event Handlers ... // // // START Functions ... // // // Validate Input Args for Initialization ... bool ValidateInputs() { // bool result = false; // // Validate Args ... if (length >= 2) { result = true; } // return result; } // // Define Indexes and Styles ... void DefineBuffers() { // // LONG SL Buffers ... string longSLBufferLabel = ShortName + " L SL " + "(" + (string)length + ")"; ArraySetAsSeries(longSLBuffer, true); SetIndexBuffer(longSLBufferIndex, longSLBuffer, INDICATOR_DATA); PlotIndexSetInteger(longSLBufferIndex, PLOT_DRAW_BEGIN, length); PlotIndexSetString(longSLBufferIndex, PLOT_LABEL, longSLBufferLabel); // // SHORT SL Buffers ... string shortSLBufferLabel = ShortName + " S SL " + "(" + (string)length + ")"; ArraySetAsSeries(shortSLBuffer, true); SetIndexBuffer(shortSLBufferIndex, shortSLBuffer, INDICATOR_DATA); PlotIndexSetInteger(shortSLBufferIndex, PLOT_DRAW_BEGIN, length); PlotIndexSetString(shortSLBufferIndex, PLOT_LABEL, shortSLBufferLabel); // // ATR Buffer ... ArraySetAsSeries(atrBuffer, true); SetIndexBuffer(atrBufferIndex, atrBuffer, INDICATOR_CALCULATIONS); } // // Set Indicator Short Name and also we can define Buffers Labels ... void SetIndicatorName() { // string indicatorShortName = ""; StringConcatenate(indicatorShortName, ShortName, ""); // IndicatorSetString(INDICATOR_SHORTNAME, indicatorShortName); } // // Calculate Buffers ... void CalculateBuffers( int bar_index // Selected Bar Index ) { // // ATR ... double atrValue = atrBuffer[bar_index]; // // LOMNG Buffer ... double longPrice = GetAppliedPrice( bar_index, longAppliedTo); double longSLValue = longPrice - (atrValue * multiplier); // longSLBuffer[bar_index] = longSLValue; // // SHORT Buffer ... double shortPrice = GetAppliedPrice( bar_index, shortAppliedTo); double shortSLValue = shortPrice + (atrValue * multiplier); // shortSLBuffer[bar_index] = shortSLValue; } // // END Functions ... //