/////////////////////////////////////////////////////// // SaherElm IT Center MQL5 Indicator // ------------------------------------------- // Name: XCHVWAP // Description: XCHVWAP 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 XCHVWAP Indicator" #property strict // #define ShortName "XCHVWAP" // // Includes Common Library ... #include "../Libraries/x-saherelm.common.lib.mq5" // // Inputs ... // // Market ... input group "Market"; input ENUM_APPLIED_PRICE appliedTo = PRICE_CLOSE; // Applied To // input group "Cycles"; // input group "Short"; input ENUM_X_PERIOD_METHOD scMethod = X_PERIOD_AUTO; // How to Find Period input ENUM_TIMEFRAMES scPeriod = NULL; // Time Period // input group "Medium"; input ENUM_X_PERIOD_METHOD mcMethod = X_PERIOD_AUTO; // How to Find Period input ENUM_TIMEFRAMES mcPeriod = NULL; // Time Period // input group "Long"; input ENUM_X_PERIOD_METHOD lcMethod = X_PERIOD_AUTO; // How to Find Period input ENUM_TIMEFRAMES lcPeriod = NULL; // Time Period // input group "Hind"; input ENUM_X_PERIOD_METHOD hcMethod = X_PERIOD_AUTO; // How to Find Period input ENUM_TIMEFRAMES hcPeriod = NULL; // Time Period // // Presentation ... input group "Presentation"; input bool showShort = true; // Show Short Cycle input bool showMedium = true; // Show Medium Cycle input bool showLong = true; // Show Long Cycle input bool showHind = true; // Show Hind Cycle // // Buffers ... // #property indicator_chart_window // #property indicator_buffers 14 #property indicator_plots 8 // #define hideColorIDX 0 #define bullishColorIDX 1 #define bearishColorIDX 2 #define neuturalColorIDX 3 // // Short ... // #define sBufferIndex 0 double sBuffer[]; // #define sColorBufferIndex 1 double sColorBuffer[]; // #define sPlotBufferIndex 0 #property indicator_label1 "XCHVWAP S" #property indicator_type1 DRAW_COLOR_LINE #property indicator_color1 CLR_NONE, clrGreen, clrRed, clrGray #property indicator_style1 STYLE_DOT #property indicator_width1 1 // // Medium ... // #define mBufferIndex 2 double mBuffer[]; // #define mColorBufferIndex 3 double mColorBuffer[]; // #define mPlotBufferIndex 1 #property indicator_label2 "XCHVWAP M" #property indicator_type2 DRAW_COLOR_LINE #property indicator_color2 CLR_NONE, clrGreen, clrRed, clrGray #property indicator_style2 STYLE_DOT #property indicator_width2 1 // // Long ... // #define lBufferIndex 4 double lBuffer[]; // #define lColorBufferIndex 5 double lColorBuffer[]; // #define lPlotBufferIndex 2 #property indicator_label3 "XCHVWAP L" #property indicator_type3 DRAW_COLOR_LINE #property indicator_color3 CLR_NONE, clrGreen, clrRed, clrGray #property indicator_style3 STYLE_DOT #property indicator_width3 1 // // Hind ... // #define hBufferIndex 6 double hBuffer[]; // #define hColorBufferIndex 7 double hColorBuffer[]; // #define hPlotBufferIndex 3 #property indicator_label4 "XCHVWAP H" #property indicator_type4 DRAW_COLOR_LINE #property indicator_color4 CLR_NONE, clrGreen, clrRed, clrGray #property indicator_style4 STYLE_DOT #property indicator_width4 1 // // Data Buffers ... // // Volumes ... #define volumeBufferIndex 8 double volumeBuffer[]; // // Price ... #define priceBufferIndex 9 double priceBuffer[]; // // SHORT ... #define sStateBufferIndex 10 double sStateBuffer[]; // // MEDIUM ... #define mStateBufferIndex 11 double mStateBuffer[]; // // LONG ... #define lStateBufferIndex 12 double lStateBuffer[]; // // HIND ... #define hStateBufferIndex 13 double hStateBuffer[]; // // Variables ... // int maxLength; // // Short ... int mSCycleLength = 0; ENUM_TIMEFRAMES mSCPeriod = NULL; // // Medium ... int mMCycleLength = 0; ENUM_TIMEFRAMES mMCPeriod = NULL; // // Long ... int mLCycleLength = 0; ENUM_TIMEFRAMES mLCPeriod = NULL; // // Hind ... int mHCycleLength = 0; ENUM_TIMEFRAMES mHCPeriod = NULL; // // EVENT Handlers ... // // Initialization ... int OnInit() { // // Validate Inputs ... if (!ValidateInputs()) { return INIT_PARAMETERS_INCORRECT; } // if (!InitMarketCycles()) { 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 = // (IsValid(scMethod, scPeriod) && IsValid(mcMethod, mcPeriod) && IsValid(lcMethod, lcPeriod) && IsValid(hcMethod, hcPeriod)) // ; // return result; } // // Initialize Market Cycles ... bool InitMarketCycles() { // bool result = false; // // Current Cycle Initialization ... // int cPeriodSeconds = PeriodSeconds(_Period); // // Short Cycle Initialization ... // // Find Cycle Period ... if (scMethod == X_PERIOD_AUTO) { // // Select Period ... mSCPeriod = GetCyclePeriod( X_MARKET_CYCLE_SHORT, _Period // ); } else { mSCPeriod = scPeriod; } // result = IsValid(mSCPeriod); if (!result) { return result; } // mSCycleLength = PeriodSeconds(mSCPeriod) / cPeriodSeconds; result = mSCycleLength > 0; if (!result) { return result; } // // Medium Cycle Initialization ... // // Find Cycle Period ... if (mcMethod == X_PERIOD_AUTO) { // // Select Period ... mMCPeriod = GetCyclePeriod( X_MARKET_CYCLE_MEDIUM, _Period // ); } else { mMCPeriod = mcPeriod; } // result = IsValid(mMCPeriod); if (!result) { return result; } // mMCycleLength = PeriodSeconds(mMCPeriod) / cPeriodSeconds; result = mMCycleLength > 0; if (!result) { return result; } // // Long Cycle Initialization ... // // Find Cycle Period ... if (lcMethod == X_PERIOD_AUTO) { // // Select Period ... mLCPeriod = GetCyclePeriod( X_MARKET_CYCLE_LONG, _Period // ); } else { mLCPeriod = lcPeriod; } // result = IsValid(mLCPeriod); if (!result) { return result; } // mLCycleLength = PeriodSeconds(mLCPeriod) / cPeriodSeconds; result = mLCycleLength > 0; if (!result) { return result; } // // Hind Cycle Initialization ... // // Find Cycle Period ... if (hcMethod == X_PERIOD_AUTO) { // // Select Period ... mHCPeriod = GetCyclePeriod( X_MARKET_CYCLE_HIND, _Period // ); } else { mHCPeriod = hcPeriod; } // result = IsValid(mHCPeriod); if (!result) { return result; } // mHCycleLength = PeriodSeconds(mHCPeriod) / cPeriodSeconds; result = mHCycleLength > 0; if (!result) { return result; } // return result; } // // Set Indicator Short Name and also we can define Buffers Labels ... void SetIndicatorName() { IndicatorSetString(INDICATOR_SHORTNAME, ShortName); } // // Define Indexes and Styles ... void DefineBuffers() { // // Short ... // ArraySetAsSeries(sBuffer, true); ArraySetAsSeries(sColorBuffer, true); SetIndexBuffer(sBufferIndex, sBuffer, INDICATOR_DATA); SetIndexBuffer(sColorBufferIndex, sColorBuffer, INDICATOR_COLOR_INDEX); // // Medium ... // ArraySetAsSeries(mBuffer, true); ArraySetAsSeries(mColorBuffer, true); SetIndexBuffer(mBufferIndex, mBuffer, INDICATOR_DATA); SetIndexBuffer(mColorBufferIndex, mColorBuffer, INDICATOR_COLOR_INDEX); // // Long ... // ArraySetAsSeries(lBuffer, true); ArraySetAsSeries(lColorBuffer, true); SetIndexBuffer(lBufferIndex, lBuffer, INDICATOR_DATA); SetIndexBuffer(lColorBufferIndex, lColorBuffer, INDICATOR_COLOR_INDEX); // // Hind ... // ArraySetAsSeries(hBuffer, true); ArraySetAsSeries(hColorBuffer, true); SetIndexBuffer(hBufferIndex, hBuffer, INDICATOR_DATA); SetIndexBuffer(hColorBufferIndex, hColorBuffer, INDICATOR_COLOR_INDEX); // // Data Buffers ... // // Volumes ... ArraySetAsSeries(volumeBuffer, true); SetIndexBuffer(volumeBufferIndex, volumeBuffer, INDICATOR_CALCULATIONS); // // Price ... ArraySetAsSeries(priceBuffer, true); SetIndexBuffer(priceBufferIndex, priceBuffer, INDICATOR_CALCULATIONS); // // Short State ... ArraySetAsSeries(sStateBuffer, true); SetIndexBuffer(sStateBufferIndex, sStateBuffer, INDICATOR_CALCULATIONS); // // Medium State ... ArraySetAsSeries(mStateBuffer, true); SetIndexBuffer(mStateBufferIndex, mStateBuffer, INDICATOR_CALCULATIONS); // // Long State ... ArraySetAsSeries(lStateBuffer, true); SetIndexBuffer(lStateBufferIndex, lStateBuffer, INDICATOR_CALCULATIONS); // // Hind State ... ArraySetAsSeries(hStateBuffer, true); SetIndexBuffer(hStateBufferIndex, hStateBuffer, INDICATOR_CALCULATIONS); } // // Retrieve all Exists Input Max Length ... // use for Start Of Drawing ... int ExtractMaxLengthOfInputs() { // int result = 0; // result = MathMax(mSCycleLength, mMCycleLength); result = MathMax(result, mLCycleLength); result = MathMax(result, mHCycleLength); // return result; } // // 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 // ); // CalculateCycles( bar_index, prevCalculated, ratesTotal, open, high, close, low, tickVolume // ); } // 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]; } // void CalculateCycles( 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[] // ) { // // SHORT ... CalculateCycle( bar_index, prevCalculated, ratesTotal, open, high, close, low, tickVolume, // mSCycleLength, showShort, sBuffer, sColorBuffer, sStateBuffer // ); // // MEDIUM ... CalculateCycle( bar_index, prevCalculated, ratesTotal, open, high, close, low, tickVolume, // mMCycleLength, showMedium, mBuffer, mColorBuffer, mStateBuffer // ); // // LONG ... CalculateCycle( bar_index, prevCalculated, ratesTotal, open, high, close, low, tickVolume, // mLCycleLength, showLong, lBuffer, lColorBuffer, lStateBuffer // ); // // HIND ... CalculateCycle( bar_index, prevCalculated, ratesTotal, open, high, close, low, tickVolume, // mHCycleLength, showHind, hBuffer, hColorBuffer, hStateBuffer // ); } // void CalculateCycle( 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; } } //