/////////////////////////////////////////////////////// // // SaherElm IT Center MQL5 Indicator // ------------------------------------------------- // Name: X121 XSTR // Description: XSTR ... // // // 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 XSTR Indicator" #property strict // // Definitions ... // #define ShortName "X121 XSTR" // // Includes Common Library ... #include "../Libraries/x-saherelm.common.lib.mq5" // // Inputs ... // // Market ... input group "Market"; input int strLength = 14; // Length input double strMultiplier = 3; // Multiplier input ENUM_APPLIED_PRICE strAppliedTo = PRICE_MEDIAN; // Applied To // input group "Presentation"; // input bool showStr = true; // Show Str input bool showStrUpper = true; // Show Str Upper input bool showStrLower = true; // Show Str Lower // // Buffers ... // #define hideColorIDX 0 #define bullishColorIDX 1 #define bearishColorIDX 2 #define neuturalColorIDX 3 // #property indicator_chart_window // #property indicator_buffers 7 #property indicator_plots 3 // // STR ... #define strBufferIndex 0 double strBuffer[]; #define strColorBufferIndex 1 double strColorBuffer[]; // #define strPlotBufferIndex 0 #property indicator_label1 "X121 STR" #property indicator_type1 DRAW_COLOR_LINE #property indicator_color1 CLR_NONE, clrLime, clrRed, clrGray #property indicator_style1 STYLE_SOLID #property indicator_width1 2 // // Up ... #define strUpBufferIndex 2 double strUpBuffer[]; // #define strUpPlotBufferIndex 1 #property indicator_label2 "X121 STR U" #property indicator_type2 DRAW_LINE #property indicator_color2 C'255,106,0' #property indicator_style2 STYLE_SOLID #property indicator_width2 1 // // Down ... #define strDownBufferIndex 3 double strDownBuffer[]; // #define strDownPlotBufferIndex 2 #property indicator_label3 "X121 STR D" #property indicator_type3 DRAW_LINE #property indicator_color3 C'255,106,0' #property indicator_style3 STYLE_SOLID #property indicator_width3 1 // // Data Buffers ... // int mLastBufferIndex = 3; // // STR ... // // Atr ... #define atrBufferIndex mLastBufferIndex + 1 double atrBuffer[]; // // Price ... #define strPriceBufferIndex mLastBufferIndex + 2 double strPriceBuffer[]; // // Trend ... #define strStateBufferIndex mLastBufferIndex + 3 double strStateBuffer[]; // // Variables, Properties and etc ... // // this counts Available Bars ... int limit; // int maxLength; // // ATR Handler ... int atrHandler = INVALID_HANDLE; // bool isStrTrendChanged; bool isStrStartBearishTrend; bool isStrStartBullishTrend; // int changeOfTrend; int startBearishTrend; int startBullishTrend; // // Event Handlers ... /** * Initialize Indicator ... * * @return ( int ) */ int OnInit() { // // Validate Inputs ... if (!ValidateInputs()) { return INIT_PARAMETERS_INCORRECT; } // // Initialize Indicator Handlers ... // // ATR ... atrHandler = iATR( _Symbol, _Period, strLength // ); bool 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(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 ... // // Validate Calculated Bars ... // // ATR ... int atrCalculatedBars = BarsCalculated(atrHandler); // bool isPassedRequiredCalculatedBars = // // ATR ... atrCalculatedBars >= 0 // ; if (!isPassedRequiredCalculatedBars) { return prev_calculated; } // // // checking for the limit start of calculation of an indicator ... limit = (prev_calculated > rates_total || prev_calculated <= 0) ? maxLength : prev_calculated - 1; // // Buffers Copy ... // // ATR ... int copiedAtrs = CopyBuffer(atrHandler, 0, 0, rates_total, atrBuffer); // // Validate Copied Items ... bool isPassedRequiredCopiedItems = // // ATR ... copiedAtrs >= 0 // ; if (!isPassedRequiredCopiedItems) { return prev_calculated; } // // Main Loop ... for (int i = limit; i < rates_total && !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 = // // STR ... strLength > 0 && strMultiplier > 0 // ; // return result; } /** * Extract Max Length of Inputs ... * * @return ( int ) */ int ExtractMaxLengthOfInputs() { // int result = 0; // result = MathMax(1, strLength); // return result; } /** * Define Required Buffers ... */ void DefineBuffers() { // // STR ... SetIndexBuffer(strBufferIndex, strBuffer, INDICATOR_DATA); SetIndexBuffer(strColorBufferIndex, strColorBuffer, INDICATOR_COLOR_INDEX); // ENUM_DRAW_TYPE strUpDrawType = showStrUpper ? DRAW_LINE : DRAW_NONE; SetIndexBuffer(strUpBufferIndex, strUpBuffer, INDICATOR_DATA); PlotIndexSetDouble(strUpPlotBufferIndex, PLOT_EMPTY_VALUE, EMPTY_VALUE); PlotIndexSetInteger(strUpPlotBufferIndex, PLOT_SHOW_DATA, showStrUpper); PlotIndexSetInteger(strUpPlotBufferIndex, PLOT_DRAW_TYPE, strUpDrawType); // ENUM_DRAW_TYPE strDownDrawType = showStrLower ? DRAW_LINE : DRAW_NONE; SetIndexBuffer(strDownBufferIndex, strDownBuffer, INDICATOR_DATA); PlotIndexSetDouble(strDownPlotBufferIndex, PLOT_EMPTY_VALUE, EMPTY_VALUE); PlotIndexSetInteger(strDownPlotBufferIndex, PLOT_SHOW_DATA, showStrLower); PlotIndexSetInteger(strDownPlotBufferIndex, PLOT_DRAW_TYPE, strDownDrawType); // // Data Buffers ... // // STR ... // SetIndexBuffer(strPriceBufferIndex, strPriceBuffer, INDICATOR_CALCULATIONS); SetIndexBuffer(strStateBufferIndex, strStateBuffer, 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[] // ) { // CalculateStr( bar_index, prevCalculated, ratesTotal, open, high, close, low // ); } // // Custom ... /** * Fill All Bufers to Zero Vlue for Specified Bar Index ... * * @param barIndex: Integer ... */ void FillBuffersZero(int barIndex) { // // STR ... strBuffer[barIndex] = 0; strUpBuffer[barIndex] = 0; strDownBuffer[barIndex] = 0; strPriceBuffer[barIndex] = 0; strColorBuffer[barIndex] = hideColorIDX; strStateBuffer[barIndex] = hideColorIDX; } /** * Calculate STR ... * * @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 CalculateStr( int bar_index, // Selected Bar Index const int prevCalculated, const int ratesTotal, const double &open[], const double &high[], const double &close[], const double &low[] // ) { // // Calculated Price ... ENUM_X_PRICE mPTYpe = ToXPrice(strAppliedTo); double price = GetAppliedPrice( mPTYpe, open, high, low, close, bar_index // ); strPriceBuffer[bar_index] = price; // double atr = atrBuffer[bar_index]; // // Up ... strUpBuffer[bar_index] = price + (strMultiplier * atr); // // Down ... strDownBuffer[bar_index] = price - (strMultiplier * atr); // if (close[bar_index] > strUpBuffer[bar_index - 1]) { // strStateBuffer[bar_index] = 1; if (strStateBuffer[bar_index - 1] == -1) { changeOfTrend = 1; } } else if (close[bar_index] < strDownBuffer[bar_index - 1]) { // strStateBuffer[bar_index] = -1; if (strStateBuffer[bar_index - 1] == 1) { changeOfTrend = 1; } } else if (strStateBuffer[bar_index - 1] == 1) { // strStateBuffer[bar_index] = 1; changeOfTrend = 0; } else if (strStateBuffer[bar_index - 1] == -1) { // strStateBuffer[bar_index] = -1; changeOfTrend = 0; } // // Down Trend Starting ... if (strStateBuffer[bar_index] < 0 && strStateBuffer[bar_index - 1] > 0) { startBearishTrend = 1; } else { startBearishTrend = 0; } // // Up Trend Starting ... if (strStateBuffer[bar_index] > 0 && strStateBuffer[bar_index - 1] < 0) { startBullishTrend = 1; } else { startBullishTrend = 0; } // if (strStateBuffer[bar_index] > 0 && strDownBuffer[bar_index] < strDownBuffer[bar_index - 1]) { strDownBuffer[bar_index] = strDownBuffer[bar_index - 1]; } // if (strStateBuffer[bar_index] < 0 && strUpBuffer[bar_index] > strUpBuffer[bar_index - 1]) { strUpBuffer[bar_index] = strUpBuffer[bar_index - 1]; } // if (startBearishTrend == 1) { strUpBuffer[bar_index] = strPriceBuffer[bar_index] + (strMultiplier * atr); } // if (startBullishTrend == 1) { strDownBuffer[bar_index] = strPriceBuffer[bar_index] - (strMultiplier * atr); } // // Draw the indicator ... // double colorIDX = hideColorIDX; strColorBuffer[bar_index] = colorIDX; // if (strStateBuffer[bar_index] == 1) { // strBuffer[bar_index] = strDownBuffer[bar_index]; if (changeOfTrend == 1) { strBuffer[bar_index - 1] = strBuffer[bar_index - 2]; changeOfTrend = 0; } // colorIDX = bullishColorIDX; } else if (strStateBuffer[bar_index] == -1) { // strBuffer[bar_index] = strUpBuffer[bar_index]; if (changeOfTrend == 1) { // strBuffer[bar_index - 1] = strBuffer[bar_index - 2]; changeOfTrend = 0; } // colorIDX = bearishColorIDX; } // if (showStr) { strColorBuffer[bar_index] = colorIDX; } } //