/////////////////////////////////////////////////////// // SaherElm IT Center MQL5 Indicator // ------------------------------------------- // Name: X3VWAP // Description: X3VWAP Trend Detector ... // // 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 X3VWAP Indicator" #property strict // #define ShortName "X3VWAP" // // Includes Common Library ... #include "../Libraries/x-saherelm.common.lib.mq5" // // Inputs ... // // Market ... input group "Market"; input int fastLength = 50; // Fast Length input int midLength = 100; // Mid Length input int slowLength = 200; // Slow Length input ENUM_APPLIED_PRICE appliedTo = PRICE_CLOSE; // Applied To // // Presentation ... input group "Presentation"; input bool showFast = true; // Show Fast input bool showMid = true; // Show Mid input bool showSlow = true; // Show Slow // // Buffers ... // #property indicator_chart_window // #property indicator_buffers 11 #property indicator_plots 3 // #define hideColorIDX 0 #define bullishColorIDX 1 #define bearishColorIDX 2 #define neuturalColorIDX 3 // // Current ... // // Fast ... #define fastBufferIndex 0 double fastBuffer[]; #define fastColorBufferIndex 1 double fastColorBuffer[]; // #define fastPlotBufferIndex 0 #property indicator_label1 "X3VWAP F" #property indicator_type1 DRAW_COLOR_LINE #property indicator_color1 CLR_NONE, clrAqua, clrMagenta, clrGray #property indicator_style1 STYLE_SOLID #property indicator_width1 2 // // Mid ... #define midBufferIndex 2 double midBuffer[]; #define midColorBufferIndex 3 double midColorBuffer[]; // #define midPlotBufferIndex 1 #property indicator_label2 "X3VWAP M" #property indicator_type2 DRAW_COLOR_LINE #property indicator_color2 CLR_NONE, clrGreen, clrRed, clrGray #property indicator_style2 STYLE_SOLID #property indicator_width2 2 // // Slow ... #define slowBufferIndex 4 double slowBuffer[]; #define slowColorBufferIndex 5 double slowColorBuffer[]; // #define slowPlotBufferIndex 2 #property indicator_label3 "X3VWAP S" #property indicator_type3 DRAW_COLOR_LINE #property indicator_color3 CLR_NONE, clrSteelBlue, clrDarkRed, clrGray #property indicator_style3 STYLE_SOLID #property indicator_width3 2 // // Data Buffers ... // // Volumes ... #define volumeBufferIndex 6 double volumeBuffer[]; // // Price ... #define priceBufferIndex 7 double priceBuffer[]; // // Fast State ... #define fastStateBufferIndex 8 double fastStateBuffer[]; // // Mid State ... #define midStateBufferIndex 9 double midStateBuffer[]; // // Slow State ... #define slowStateBufferIndex 10 double slowStateBuffer[]; // // Variables ... // int maxLength; // // EVENT Handlers ... // // Initialization ... int OnInit() { // // Validate Inputs ... if (!ValidateInputs()) { return INIT_PARAMETERS_INCORRECT; } // // 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; } // // 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 } // // Calculating what we want ... int OnCalculate( const int rates_total, // Total Bars on Chart ... const int prev_calculated, // Total Calculated Bars on Charts ... const datetime &time[], // History of Open Time ... const double &open[], // History of Open Price ... const double &high[], // History of High Price ... const double &low[], // History of Low Price ... const double &close[], // History of Close Price ... const long &tick_volume[], // History of Tick Volumes on Bar ... const long &volume[], // History of Trade Volumes ... const int &spread[] // History of Spread Price ... ) { // ArraySetAsSeries(low, true); ArraySetAsSeries(open, true); ArraySetAsSeries(high, true); ArraySetAsSeries(close, true); ArraySetAsSeries(tick_volume, true); // // this counts Available Bars ... int limit; // limit = (prev_calculated > rates_total || prev_calculated <= 0) ? rates_total : (rates_total - prev_calculated) + 1; // // 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; } // // CUSTOM Functions ... // // Validate Input Args for Initialization ... bool ValidateInputs() { // bool result = // fastLength > 2 && midLength > fastLength && slowLength > midLength // ; // return result; } // // Retrieve all Exists Input Max Length ... // use for Start Of Drawing ... int ExtractMaxLengthOfInputs() { // int result = 0; // // Current ... result = MathMax(fastLength, midLength); result = MathMax(result, slowLength); // return result; } // // Set Indicator Short Name and also we can define Buffers Labels ... void SetIndicatorName() { // IndicatorSetInteger(INDICATOR_DIGITS, _Digits); IndicatorSetString(INDICATOR_SHORTNAME, ShortName); } // // Define Indexes and Styles ... void DefineBuffers() { // // Fast ... ArraySetAsSeries(fastBuffer, true); ArraySetAsSeries(fastColorBuffer, true); SetIndexBuffer(fastBufferIndex, fastBuffer, INDICATOR_DATA); SetIndexBuffer(fastColorBufferIndex, fastColorBuffer, INDICATOR_COLOR_INDEX); // // Mid ... ArraySetAsSeries(midBuffer, true); ArraySetAsSeries(midColorBuffer, true); SetIndexBuffer(midBufferIndex, midBuffer, INDICATOR_DATA); SetIndexBuffer(midColorBufferIndex, midColorBuffer, INDICATOR_COLOR_INDEX); // // Slow ... ArraySetAsSeries(slowBuffer, true); ArraySetAsSeries(slowColorBuffer, true); SetIndexBuffer(slowBufferIndex, slowBuffer, INDICATOR_DATA); SetIndexBuffer(slowColorBufferIndex, slowColorBuffer, INDICATOR_COLOR_INDEX); // // Data Buffers ... // // Volumes ... ArraySetAsSeries(volumeBuffer, true); SetIndexBuffer(volumeBufferIndex, volumeBuffer, INDICATOR_CALCULATIONS); // // Price ... ArraySetAsSeries(priceBuffer, true); SetIndexBuffer(priceBufferIndex, priceBuffer, INDICATOR_CALCULATIONS); // // Fast State ... ArraySetAsSeries(fastStateBuffer, true); SetIndexBuffer(fastStateBufferIndex, fastStateBuffer, INDICATOR_CALCULATIONS); // // Mid State ... ArraySetAsSeries(midStateBuffer, true); SetIndexBuffer(midStateBufferIndex, midStateBuffer, INDICATOR_CALCULATIONS); // // Slow State ... ArraySetAsSeries(slowStateBuffer, true); SetIndexBuffer(slowStateBufferIndex, slowStateBuffer, INDICATOR_CALCULATIONS); } // // Calculate Buffers ... 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[] // ) { // // Calculate Volumes and Price ... // if (ratesTotal - bar_index <= maxLength) { // CalculateDataBuffers( bar_index, prevCalculated, ratesTotal, open, high, close, low, tickVolume // ); return; } // CalculateDataBuffers( bar_index, prevCalculated, ratesTotal, open, high, close, low, tickVolume // ); // CalculateVWAPS( bar_index, prevCalculated, ratesTotal, open, high, close, low, tickVolume // ); } // // Calculate Required Data Buffers ... void CalculateDataBuffers( 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 price = GetAppliedPrice( appliedTo, open, high, low, close, bar_index // ); priceBuffer[bar_index] = price; volumeBuffer[bar_index] = (double)tickVolume[bar_index]; } // // Calculate Different VWaps ... void CalculateVWAPS( 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[] // ) { // // Fast ... CalculateVWAP( bar_index, prevCalculated, ratesTotal, open, high, close, low, tickVolume, // fastLength, showFast, fastBuffer, fastColorBuffer, fastStateBuffer // ); // // Mid ... CalculateVWAP( bar_index, prevCalculated, ratesTotal, open, high, close, low, tickVolume, // midLength, showMid, midBuffer, midColorBuffer, midStateBuffer // ); // // Fast ... CalculateVWAP( bar_index, prevCalculated, ratesTotal, open, high, close, low, tickVolume, // slowLength, showSlow, slowBuffer, slowColorBuffer, slowStateBuffer // ); } // // Calculate VWAP ... void CalculateVWAP( 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 _length, // Calculation Length bool _show, double &_buffer[], double &_colorBuffer[], double &_stateBuffer[] // ) { // double vSum = 0; double pSum = 0; double mSum = 0; for (int x = 0; x < _length; x++) { // pSum += priceBuffer[x + bar_index]; vSum += volumeBuffer[x + bar_index]; mSum += priceBuffer[x + bar_index] * volumeBuffer[x + bar_index]; } // double iValue = mSum / vSum; iValue = NormalizeDouble(iValue, _Digits); // _buffer[bar_index] = iValue; // bool isBullish = low[bar_index] > iValue; bool isBearish = high[bar_index] < iValue; // double iColor = isBullish ? bullishColorIDX : isBearish ? bearishColorIDX : neuturalColorIDX; // _colorBuffer[bar_index] = hideColorIDX; _stateBuffer[bar_index] = iColor; if (_show) { _colorBuffer[bar_index] = iColor; } } //