/////////////////////////////////////////////////////// // // SaherElm IT Center MQL5 Indicator // ------------------------------------------------- // Name: X121 XPV // Description: XPV ... // // // 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 XPV Indicator" #property strict // // Definitions ... // #define ShortName "X121 XPV" // // Includes Common Library ... #include "../Libraries/x-saherelm.common.lib.mq5" // // Inputs ... // // Market ... input group "Market"; // 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 // input group "Boundary Detection"; input ENUM_SERIESMODE hhMode = MODE_HIGH; // Highest High Calculation Method input ENUM_SERIESMODE llMode = MODE_LOW; // Lowest Low Calculation Method // input group "Sar Detection"; input double sarStep = 0.02; // Step input double sarMax = 0.2; // Maximum // // Presentation ... input group "Presentation"; // input int startCalculationForLastBars = 1000; // Calculate Last n Bars input int sarArrowCode = 159; // Parabolic Sar Arrow Code input int peaksArrowCode = 159; // Peaks Arrow Code input int valesArrowCode = 159; // Vales Arrow Code // input bool showSar = true; // Show Parabolic Sar input bool showPeaks = true; // Show Peaks input bool showVales = true; // Show Vales // // Buffers ... // #define hideColorIDX 0 #define bullishColorIDX 1 #define bearishColorIDX 2 #define neuturalColorIDX 3 // #property indicator_chart_window // #property indicator_buffers 13 #property indicator_plots 3 // // Plot Buffers ... // // PEAKS ... // #define peaksBufferIndex 0 double peaksBuffer[]; // #property indicator_label1 "X121 PEAKS" #property indicator_type1 DRAW_ARROW #property indicator_color1 clrAqua #property indicator_width1 1 // // VALES ... // #define valesBufferIndex 1 double valesBuffer[]; // #property indicator_label2 "X121 VALES" #property indicator_type2 DRAW_ARROW #property indicator_color2 clrMagenta #property indicator_width2 1 // #define sarBufferIndex 2 double sarBuffer[]; // #property indicator_label3 "X121 SAR" #property indicator_type3 DRAW_ARROW #property indicator_color3 clrYellow #property indicator_width3 1 // // Data Buffers ... // int mLastBufferIndex = 2; // // CURRENT ... // #define cHHBufferIndex mLastBufferIndex + 1 double cHHBuffer[]; // #define cLLBufferIndex mLastBufferIndex + 2 double cLLBuffer[]; // // SHORT ... // #define sHHBufferIndex mLastBufferIndex + 3 double sHHBuffer[]; // #define sLLBufferIndex mLastBufferIndex + 4 double sLLBuffer[]; // // MEDIUM ... // #define mHHBufferIndex mLastBufferIndex + 5 double mHHBuffer[]; // #define mLLBufferIndex mLastBufferIndex + 6 double mLLBuffer[]; // // LONG ... // #define lHHBufferIndex mLastBufferIndex + 7 double lHHBuffer[]; // #define lLLBufferIndex mLastBufferIndex + 8 double lLLBuffer[]; // // HIND ... // #define hHHBufferIndex mLastBufferIndex + 9 double hHHBuffer[]; // #define hLLBufferIndex mLastBufferIndex + 10 double hLLBuffer[]; // // Variables, Properties and etc ... // // this counts Available Bars ... int limit; // int maxLength; // // SAR Handler ... int sarHandler = INVALID_HANDLE; // // XMarketCycle sc; int mSCLength = 0; ENUM_TIMEFRAMES mSCPeriod = NULL; // // XMarketCycle mc; int mMCLength = 0; ENUM_TIMEFRAMES mMCPeriod = NULL; // // XMarketCycle lc; int mLCLength = 0; ENUM_TIMEFRAMES mLCPeriod = NULL; // // XMarketCycle hc; int mHCLength = 0; ENUM_TIMEFRAMES mHCPeriod = NULL; // // Event Handlers ... /** * Initialize Indicator ... * * @return ( int ) */ int OnInit() { // // Validate Inputs ... if (!ValidateInputs()) { return INIT_PARAMETERS_INCORRECT; } // // Initialize Indicator Handlers ... // // SAR ... sarHandler = iSAR( _Symbol, _Period, sarStep, sarMax // ); bool isInited = sarHandler != INVALID_HANDLE; if (!isInited) { return INIT_FAILED; } // 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; } /** * 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(sarHandler); } /** * 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); // // Validate Calculated Bars ... // // SAR ... int sarCalculatedBars = BarsCalculated(sarHandler); // bool isPassedRequiredCalculatedBars = // // SAR ... sarCalculatedBars >= maxLength // ; if (!isPassedRequiredCalculatedBars) { return prev_calculated; } // limit = (prev_calculated > rates_total || prev_calculated <= 0) ? rates_total : (rates_total - prev_calculated) + 1; // // Buffers Copy ... // // SAR ... int copiedSars = CopyBuffer(sarHandler, 0, 0, limit, sarBuffer); // // Validate Copied Items ... bool isPassedRequiredCopiedItems = // // SAR ... copiedSars > 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 = false; // result = // // SAR ... sarMax > 0 && sarStep > 0 && sarMax > sarStep && // // XPV ... (IsValid(scMethod, scPeriod) && IsValid(mcMethod, mcPeriod) && IsValid(lcMethod, lcPeriod) && IsValid(hcMethod, hcPeriod)) // ; // return result; } /** * Extract Max Length of Inputs ... * * @return ( int ) */ int ExtractMaxLengthOfInputs() { // int result = 0; // return result; } /** * Define Required Buffers ... */ void DefineBuffers() { // // Plot Buffers ... // // PEAKS ... // ENUM_DRAW_TYPE peaksDrawType = showPeaks ? DRAW_ARROW : DRAW_NONE; // ArraySetAsSeries(peaksBuffer, true); SetIndexBuffer(peaksBufferIndex, peaksBuffer, INDICATOR_DATA); // PlotIndexSetInteger(peaksBufferIndex, PLOT_SHOW_DATA, showPeaks); PlotIndexSetInteger(peaksBufferIndex, PLOT_DRAW_TYPE, peaksDrawType); // PlotIndexSetDouble(peaksBufferIndex, PLOT_EMPTY_VALUE, 0); PlotIndexSetInteger(peaksBufferIndex, PLOT_ARROW, peaksArrowCode); // // VALES ... // ENUM_DRAW_TYPE valesDrawType = showVales ? DRAW_ARROW : DRAW_NONE; // ArraySetAsSeries(valesBuffer, true); SetIndexBuffer(valesBufferIndex, valesBuffer, INDICATOR_DATA); // PlotIndexSetInteger(valesBufferIndex, PLOT_SHOW_DATA, showVales); PlotIndexSetInteger(valesBufferIndex, PLOT_DRAW_TYPE, valesDrawType); // PlotIndexSetDouble(valesBufferIndex, PLOT_EMPTY_VALUE, 0); PlotIndexSetInteger(valesBufferIndex, PLOT_ARROW, valesArrowCode); // // SAR ... // ENUM_DRAW_TYPE sarDrawType = showSar ? DRAW_ARROW : DRAW_NONE; // ArraySetAsSeries(sarBuffer, true); SetIndexBuffer(sarBufferIndex, sarBuffer, INDICATOR_DATA); // PlotIndexSetInteger(sarBufferIndex, PLOT_SHOW_DATA, showSar); PlotIndexSetInteger(sarBufferIndex, PLOT_DRAW_TYPE, sarDrawType); // PlotIndexSetDouble(sarBufferIndex, PLOT_EMPTY_VALUE, 0); PlotIndexSetInteger(sarBufferIndex, PLOT_ARROW, sarArrowCode); // // Data Buffers ... // // CURRENT ... // ArraySetAsSeries(cHHBuffer, true); SetIndexBuffer(cHHBufferIndex, cHHBuffer, INDICATOR_CALCULATIONS); // ArraySetAsSeries(cLLBuffer, true); SetIndexBuffer(cLLBufferIndex, cLLBuffer, INDICATOR_CALCULATIONS); // // SHORT ... // ArraySetAsSeries(sHHBuffer, true); SetIndexBuffer(sHHBufferIndex, sHHBuffer, INDICATOR_CALCULATIONS); // ArraySetAsSeries(sLLBuffer, true); SetIndexBuffer(sLLBufferIndex, sLLBuffer, INDICATOR_CALCULATIONS); // // MEDIUM ... // ArraySetAsSeries(mHHBuffer, true); SetIndexBuffer(mHHBufferIndex, mHHBuffer, INDICATOR_CALCULATIONS); // ArraySetAsSeries(mLLBuffer, true); SetIndexBuffer(mLLBufferIndex, mLLBuffer, INDICATOR_CALCULATIONS); // // LONG ... // ArraySetAsSeries(lHHBuffer, true); SetIndexBuffer(lHHBufferIndex, lHHBuffer, INDICATOR_CALCULATIONS); // ArraySetAsSeries(lLLBuffer, true); SetIndexBuffer(lLLBufferIndex, lLLBuffer, INDICATOR_CALCULATIONS); // // HIND ... // ArraySetAsSeries(hHHBuffer, true); SetIndexBuffer(hHHBufferIndex, hHHBuffer, INDICATOR_CALCULATIONS); // ArraySetAsSeries(hLLBuffer, true); SetIndexBuffer(hLLBufferIndex, hLLBuffer, 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) { // // Calculate Cycles ... CalculateCycles(bar_index); // // Calculate Peaks and Vales ... CalculatePeaksAndVales(bar_index); } else { FillBuffersZero(bar_index); } } // // Custom ... /** * Initial Market Cycles ... * * @return ( bool ) */ bool InitMarketCycles() { // bool result = false; // int cPeriodSeconds = PeriodSeconds(_Period); // // Find Cycle Period ... if (scMethod == X_PERIOD_AUTO) { // // Select Period ... mSCPeriod = GetCyclePeriod( X_MARKET_CYCLE_SHORT, _Period // ); } else { mSCPeriod = scPeriod; } // if (IsValid(mSCPeriod)) { mSCLength = PeriodSeconds(mSCPeriod) / cPeriodSeconds; } // // Find Cycle Period ... if (mcMethod == X_PERIOD_AUTO) { // // Select Period ... mMCPeriod = GetCyclePeriod( X_MARKET_CYCLE_MEDIUM, _Period // ); } else { mMCPeriod = mcPeriod; } // if (IsValid(mMCPeriod)) { mMCLength = PeriodSeconds(mMCPeriod) / cPeriodSeconds; } // // Find Cycle Period ... if (lcMethod == X_PERIOD_AUTO) { // // Select Period ... mLCPeriod = GetCyclePeriod( X_MARKET_CYCLE_LONG, _Period // ); } else { mLCPeriod = lcPeriod; } // if (IsValid(mLCPeriod)) { mLCLength = PeriodSeconds(mLCPeriod) / cPeriodSeconds; } // // Find Cycle Period ... if (hcMethod == X_PERIOD_AUTO) { // // Select Period ... mHCPeriod = GetCyclePeriod( X_MARKET_CYCLE_HIND, _Period // ); } else { mHCPeriod = hcPeriod; } // if (IsValid(mHCPeriod)) { mHCLength = PeriodSeconds(mHCPeriod) / cPeriodSeconds; } // result = mSCLength > 0 && mMCLength > mSCLength && mLCLength > mMCLength && mHCLength > mLCLength; // return result; } /** * Fill All Bufers to Zero Vlue for Specified Bar Index ... * * @param barIndex: Integer ... */ void FillBuffersZero(int barIndex) { // // CURRENT ... cHHBuffer[barIndex] = EMPTY_VALUE; cLLBuffer[barIndex] = EMPTY_VALUE; // // SHORT ... sHHBuffer[barIndex] = EMPTY_VALUE; sLLBuffer[barIndex] = EMPTY_VALUE; // // MEDIUM ... mHHBuffer[barIndex] = EMPTY_VALUE; mLLBuffer[barIndex] = EMPTY_VALUE; // // LONG ... lHHBuffer[barIndex] = EMPTY_VALUE; lLLBuffer[barIndex] = EMPTY_VALUE; // // HIND ... hHHBuffer[barIndex] = EMPTY_VALUE; hLLBuffer[barIndex] = EMPTY_VALUE; // // PEAKS ... peaksBuffer[barIndex] = EMPTY_VALUE; // // VALES ... valesBuffer[barIndex] = EMPTY_VALUE; // // SARS ... sarBuffer[barIndex] = EMPTY_VALUE; } /** * Calculate Specified Market Cycle Info ... * * @param barIndex: Integer, Bar Index ... * @param cycle: ENUM_X_MARKET_CYCLES member, Specified Cycle ... * @param hhBuffer: Highest High Buffer Reference ... * @param llBuffer: Lowest Low Buffer Reference ... */ void CalculateCycle( int barIndex, ENUM_X_MARKET_CYCLES cycle, double &hhBuffer[], double &llBuffer[] // ) { // XOHCL bar; bool isBarInited = bar.Init( _Symbol, _Period, barIndex // ); if (!isBarInited) { return; } // int mLength = 0; switch (cycle) { // case X_MARKET_CYCLE_SHORT: mLength = mSCLength; break; // case X_MARKET_CYCLE_MEDIUM: mLength = mMCLength; break; // case X_MARKET_CYCLE_LONG: mLength = mLCLength; break; // case X_MARKET_CYCLE_HIND: mLength = mHCLength; break; } // if (mLength == 0) { return; } // // Find Highest High ... double hhValue = bar.FindHighest( mLength, hhMode // ); hhBuffer[barIndex] = hhValue; // // Find Lowest Low ... double llValue = bar.FindLowest( mLength, llMode // ); llBuffer[barIndex] = llValue; } /** * Claculate Cycles ... * * @param barIndex: Integer, Bar Index ... */ void CalculateCycles(int barIndex) { // // SHORT ... CalculateCycle( barIndex, X_MARKET_CYCLE_SHORT, sHHBuffer, sLLBuffer // ); // // MEDIUM ... CalculateCycle( barIndex, X_MARKET_CYCLE_MEDIUM, mHHBuffer, mLLBuffer // ); // // LONG ... CalculateCycle( barIndex, X_MARKET_CYCLE_LONG, lHHBuffer, lLLBuffer // ); // // HIND ... CalculateCycle( barIndex, X_MARKET_CYCLE_HIND, hHHBuffer, hLLBuffer // ); } /** * Calculate Peaks and Vales ... * * @param barIndex: Integer, Bar Index ... */ void CalculatePeaksAndVales(int barIndex) { // int lastIndex = barIndex + 1; int barsCount = iBars(_Symbol, _Period); // // PEAKS ... double lastPeak = lastIndex < barsCount ? peaksBuffer[lastIndex] : 0; // double isHH = sHHBuffer[barIndex]; double imHH = mHHBuffer[barIndex]; double ilHH = lHHBuffer[barIndex]; double ihHH = hHHBuffer[barIndex]; // double iHHs[4] = { isHH, imHH, ilHH, ihHH // }; double rValue = GetAverage(iHHs); bool isPeak = rValue == isHH && isHH == imHH && imHH == ilHH && ilHH == ihHH; double iPeak = isPeak ? rValue : lastPeak; peaksBuffer[barIndex] = iPeak; // // VALES ... double lastVale = lastIndex < barsCount ? valesBuffer[lastIndex] : 0; // double isLL = sLLBuffer[barIndex]; double imLL = mLLBuffer[barIndex]; double ilLL = lLLBuffer[barIndex]; double ihLL = hLLBuffer[barIndex]; // double iLLs[4] = { isLL, imLL, ilLL, ihLL // }; double sValue = GetAverage(iLLs); bool isVale = sValue == isLL && isLL == imLL && imLL == ilLL && ilLL == ihLL; double iVale = isVale ? sValue : lastVale; valesBuffer[barIndex] = iVale; } //