/////////////////////////////////////////////////////////////// // // SaherElm IT Center MQL5 X121_PV Peaks and Vales Indicator // ----------------------------------------------------------- // Name: X121_PV // Description: Market Peaks and Vales // 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 X121_PV Indicator" #property strict // // START Constants ... // // // Indicator Short Name ... #define ShortName "X121_PV" // // END Constants ... // // // START Inputs ... // // input group "Makret Cycles"; input int shortLength = 9; // Short Length input int mediumLength = 17; // Medium Length input int longLength = 26; // Long Length input int hindLength = 35; // Hind Length // input group "Trend"; input int numOfShoulder = 10; // Number of Shoulders for Swings // input group "Detection Modes"; input ENUM_SERIESMODE hhMode = MODE_HIGH; // Highest High Calculation Method input ENUM_SERIESMODE llMode = MODE_LOW; // Lowest Low Calculation Method // input group "Presentation"; input bool showPeaksAndVales = true; // Show Peaks and Vales input bool showLevels = false; // Show Levels input bool showTrend = false; // Show Trend input bool showSRs = false; // Show Support and Resistance // // END Inputs ... // // // START Includes and Imports ... // // // Includes Logging Library ... #include "../Libraries/x-saherelm.log.lib.mq5" // // Includes Draw Library ... #include "../Libraries/x-saherelm.draw.lib.mq5" // // Includes Common Library ... #include "../Libraries/x-saherelm.common.lib.mq5" // // END Includes and Imports ... // // // START Buffers ... // #property indicator_chart_window // #property indicator_buffers 14 #property indicator_plots 12 // // PEKSANDVALES ... // // PEAKS ... // #define peaksBufferIndex 0 double peaksBuffer[]; // #property indicator_label1 "X121_PV PEAKS" #property indicator_type1 DRAW_ARROW #property indicator_color1 clrAqua #property indicator_width1 1 // // VALES ... // #define valesBufferIndex 1 double valesBuffer[]; // #property indicator_label2 "X121_PV VALES" #property indicator_type2 DRAW_ARROW #property indicator_color2 clrMagenta #property indicator_width2 1 // #define rBufferIndex 2 double rBuffer[]; // #define rColorBufferIndex 3 double rColorBuffer[]; // #property indicator_label3 "X121_PV R" #property indicator_type3 DRAW_COLOR_LINE #property indicator_color3 clrAqua, CLR_NONE #property indicator_style3 STYLE_DOT #property indicator_width3 1 // #define sBufferIndex 4 #define sBufferPlotIndex 3 double sBuffer[]; // #define sColorBufferIndex 5 double sColorBuffer[]; // #property indicator_label4 "X121_PV S" #property indicator_type4 DRAW_COLOR_LINE #property indicator_color4 clrMagenta, CLR_NONE #property indicator_style4 STYLE_DOT #property indicator_width4 1 // #define sHHBufferIndex 6 #define sHHBufferPlotIndex 4 double sHHBuffer[]; // #property indicator_label5 "X121_PV SHH" #property indicator_type5 DRAW_LINE #property indicator_color5 clrLightGray #property indicator_style5 STYLE_DOT #property indicator_width5 1 #define sLLBufferIndex 7 #define sLLBufferPlotIndex 5 double sLLBuffer[]; // #property indicator_label6 "X121_PV SLL" #property indicator_type6 DRAW_LINE #property indicator_color6 clrLightGray #property indicator_style6 STYLE_DOT #property indicator_width6 1 // #define mHHBufferIndex 8 #define mHHBufferPlotIndex 6 double mHHBuffer[]; // #property indicator_label7 "X121_PV MHH" #property indicator_type7 DRAW_LINE #property indicator_color7 clrLightGray #property indicator_style7 STYLE_DOT #property indicator_width7 1 // #define mLLBufferIndex 9 #define mLLBufferPlotIndex 7 double mLLBuffer[]; // #property indicator_label8 "X121_PV MLL" #property indicator_type8 DRAW_LINE #property indicator_color8 clrLightGray #property indicator_style8 STYLE_DOT #property indicator_width8 1 // #define lHHBufferIndex 10 #define lHHBufferPlotIndex 8 double lHHBuffer[]; // #property indicator_label9 "X121_PV LHH" #property indicator_type9 DRAW_LINE #property indicator_color9 clrLightGray #property indicator_style9 STYLE_DOT #property indicator_width9 1 // #define lLLBufferIndex 11 #define lLLBufferPlotIndex 9 double lLLBuffer[]; // #property indicator_label10 "X121_PV LLL" #property indicator_type10 DRAW_LINE #property indicator_color10 clrLightGray #property indicator_style10 STYLE_DOT #property indicator_width10 1 // #define hHHBufferIndex 12 #define hHHBufferPlotIndex 10 double hHHBuffer[]; // #property indicator_label11 "X121_PV HHH" #property indicator_type11 DRAW_LINE #property indicator_color11 clrLightGray #property indicator_style11 STYLE_DOT #property indicator_width11 1 // #define hLLBufferIndex 13 #define hLLBufferPlotIndex 11 double hLLBuffer[]; // #property indicator_label12 "X121_PV HLL" #property indicator_type12 DRAW_LINE #property indicator_color12 clrLightGray #property indicator_style12 STYLE_DOT #property indicator_width12 1 // // END Buffers ... // // // START Global Definitions: Variables, Properties and etc ... // // int maxLength; // double lastPeak; double lastVale; // // END Global Definitions: Variables, Properties and etc ... // // // START Event Handlers ... // // // Initialization ... int OnInit() { // // Validate Inputs ... if (!ValidateInputs()) { return INIT_PARAMETERS_INCORRECT; } // logTag = ShortName; drawPrefix = ShortName; // // 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 // RemoveDraws(drawPrefix); } // // 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; // 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); } // return rates_total; } // // END Event Handlers ... // // // START Functions ... // // // Validate Input Args for Initialization ... bool ValidateInputs() { // bool result = // // Validate Peaks and Vales ... (shortLength > 2 && mediumLength > shortLength && longLength > mediumLength && hindLength > longLength) // ; // return result; } // // Retrieve all Exists Input Max Length ... // use for Start Of Drawing ... int ExtractMaxLengthOfInputs() { // int result = 0; // // Peaks and Vales ... result = MathMax(shortLength, mediumLength); result = MathMax(result, longLength); result = MathMax(result, hindLength); // return result; } // // Define Indexes and Styles ... void DefineBuffers() { // // RANGE ... // // PEAKS and VALES ... ENUM_DRAW_TYPE pvDrawType = showPeaksAndVales ? DRAW_ARROW : DRAW_NONE; // // PEAKS ... ArraySetAsSeries(peaksBuffer, true); SetIndexBuffer(peaksBufferIndex, peaksBuffer, INDICATOR_DATA); PlotIndexSetInteger(peaksBufferIndex, PLOT_ARROW, 225); // PlotIndexSetInteger(peaksBufferIndex, PLOT_ARROW_SHIFT, -20); PlotIndexSetDouble(peaksBufferIndex, PLOT_EMPTY_VALUE, 0); // PlotIndexSetInteger(peaksBufferIndex, PLOT_SHOW_DATA, showPeaksAndVales); PlotIndexSetInteger(peaksBufferIndex, PLOT_DRAW_TYPE, pvDrawType); // // VALES ... ArraySetAsSeries(valesBuffer, true); SetIndexBuffer(valesBufferIndex, valesBuffer, INDICATOR_DATA); PlotIndexSetInteger(valesBufferIndex, PLOT_ARROW, 225); // PlotIndexSetInteger(valesBufferIndex, PLOT_ARROW_SHIFT, 20); PlotIndexSetDouble(valesBufferIndex, PLOT_EMPTY_VALUE, 0); // PlotIndexSetInteger(valesBufferIndex, PLOT_SHOW_DATA, showPeaksAndVales); PlotIndexSetInteger(valesBufferIndex, PLOT_DRAW_TYPE, pvDrawType); // // SUPPORT and RESISTANCE ... // // // RESISTANCE ... ArraySetAsSeries(rBuffer, true); SetIndexBuffer(rBufferIndex, rBuffer, INDICATOR_DATA); PlotIndexSetInteger(rBufferIndex, PLOT_DRAW_BEGIN, maxLength); PlotIndexSetInteger(rBufferIndex, PLOT_SHOW_DATA, false); // // RESISTANCE Color ... ArraySetAsSeries(rColorBuffer, true); SetIndexBuffer(rColorBufferIndex, rColorBuffer, INDICATOR_COLOR_INDEX); // // SUPPORT ... ArraySetAsSeries(sBuffer, true); SetIndexBuffer(sBufferIndex, sBuffer, INDICATOR_DATA); PlotIndexSetInteger(sBufferIndex, PLOT_DRAW_BEGIN, maxLength); PlotIndexSetInteger(sBufferPlotIndex, PLOT_SHOW_DATA, false); // // SUPPORT Color ... ArraySetAsSeries(sColorBuffer, true); SetIndexBuffer(sColorBufferIndex, sColorBuffer, INDICATOR_COLOR_INDEX); // ENUM_DRAW_TYPE zoneDrawType = showLevels ? DRAW_LINE : DRAW_NONE; // // SHORT Buffers ... // // HIGH ... ArraySetAsSeries(sHHBuffer, true); SetIndexBuffer(sHHBufferIndex, sHHBuffer, INDICATOR_DATA); PlotIndexSetInteger(sHHBufferIndex, PLOT_DRAW_BEGIN, maxLength); // PlotIndexSetInteger(sHHBufferPlotIndex, PLOT_SHOW_DATA, showLevels); PlotIndexSetInteger(sHHBufferPlotIndex, PLOT_DRAW_TYPE, zoneDrawType); // // LOW ... ArraySetAsSeries(sLLBuffer, true); SetIndexBuffer(sLLBufferIndex, sLLBuffer, INDICATOR_DATA); PlotIndexSetInteger(sLLBufferIndex, PLOT_DRAW_BEGIN, maxLength); // PlotIndexSetInteger(sLLBufferPlotIndex, PLOT_SHOW_DATA, showLevels); PlotIndexSetInteger(sLLBufferPlotIndex, PLOT_DRAW_TYPE, zoneDrawType); // // MEDIUM Buffer ... // // HIGH ... ArraySetAsSeries(mHHBuffer, true); SetIndexBuffer(mHHBufferIndex, mHHBuffer, INDICATOR_DATA); PlotIndexSetInteger(mHHBufferIndex, PLOT_DRAW_BEGIN, maxLength); // PlotIndexSetInteger(mHHBufferPlotIndex, PLOT_SHOW_DATA, showLevels); PlotIndexSetInteger(mHHBufferPlotIndex, PLOT_DRAW_TYPE, zoneDrawType); // // LOW ... ArraySetAsSeries(mLLBuffer, true); SetIndexBuffer(mLLBufferIndex, mLLBuffer, INDICATOR_DATA); PlotIndexSetInteger(mLLBufferIndex, PLOT_DRAW_BEGIN, maxLength); // PlotIndexSetInteger(mLLBufferPlotIndex, PLOT_SHOW_DATA, showLevels); PlotIndexSetInteger(mLLBufferPlotIndex, PLOT_DRAW_TYPE, zoneDrawType); // // LONG Buffer ... // // HIGH ... ArraySetAsSeries(lHHBuffer, true); SetIndexBuffer(lHHBufferIndex, lHHBuffer, INDICATOR_DATA); PlotIndexSetInteger(lHHBufferIndex, PLOT_DRAW_BEGIN, maxLength); // PlotIndexSetInteger(lHHBufferPlotIndex, PLOT_SHOW_DATA, showLevels); PlotIndexSetInteger(lHHBufferPlotIndex, PLOT_DRAW_TYPE, zoneDrawType); // // LOW ... ArraySetAsSeries(lLLBuffer, true); SetIndexBuffer(lLLBufferIndex, lLLBuffer, INDICATOR_DATA); PlotIndexSetInteger(lLLBufferIndex, PLOT_DRAW_BEGIN, maxLength); // PlotIndexSetInteger(lLLBufferPlotIndex, PLOT_SHOW_DATA, showLevels); PlotIndexSetInteger(lLLBufferPlotIndex, PLOT_DRAW_TYPE, zoneDrawType); // // HIND Buffer ... // // HIGH ... ArraySetAsSeries(hHHBuffer, true); SetIndexBuffer(hHHBufferIndex, hHHBuffer, INDICATOR_DATA); PlotIndexSetInteger(hHHBufferIndex, PLOT_DRAW_BEGIN, maxLength); // PlotIndexSetInteger(hHHBufferPlotIndex, PLOT_SHOW_DATA, showLevels); PlotIndexSetInteger(hHHBufferPlotIndex, PLOT_DRAW_TYPE, zoneDrawType); // // LOW ... ArraySetAsSeries(hLLBuffer, true); SetIndexBuffer(hLLBufferIndex, hLLBuffer, INDICATOR_DATA); PlotIndexSetInteger(hLLBufferIndex, PLOT_DRAW_BEGIN, maxLength); // PlotIndexSetInteger(hLLBufferPlotIndex, PLOT_SHOW_DATA, showLevels); PlotIndexSetInteger(hLLBufferPlotIndex, PLOT_DRAW_TYPE, zoneDrawType); } // // 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 ) { // // Calculate ... // double rColorIDX = 1; double sColorIDX = 1; // // SHORT ... // // HIGH ... int sHHIndex = iHighest( _Symbol, _Period, hhMode, shortLength, bar_index); double sHHValue = GetAppliedPrice( sHHIndex, hhMode); // sHHBuffer[bar_index] = sHHValue; // // LOW ... int sLLIndex = iLowest( _Symbol, _Period, llMode, shortLength, bar_index); double sLLValue = GetAppliedPrice( sLLIndex, llMode); // sLLBuffer[bar_index] = sLLValue; // // MEDIUM ... // // HIGH ... int mHHIndex = iHighest( _Symbol, _Period, hhMode, mediumLength, bar_index); double mHHValue = GetAppliedPrice( mHHIndex, hhMode); // mHHBuffer[bar_index] = mHHValue; // // LOW ... int mLLIndex = iLowest( _Symbol, _Period, llMode, mediumLength, bar_index); double mLLValue = GetAppliedPrice( mLLIndex, llMode); // mLLBuffer[bar_index] = mLLValue; // // LONG ... // // HIGH ... int lHHIndex = iHighest( _Symbol, _Period, hhMode, longLength, bar_index); double lHHValue = GetAppliedPrice( lHHIndex, hhMode); // lHHBuffer[bar_index] = lHHValue; // // LOW ... int lLLIndex = iLowest( _Symbol, _Period, llMode, longLength, bar_index); double lLLValue = GetAppliedPrice( lLLIndex, llMode); // lLLBuffer[bar_index] = lLLValue; // // HIND ... // // HIGH ... int hHHIndex = iHighest( _Symbol, _Period, hhMode, hindLength, bar_index); double hHHValue = GetAppliedPrice( hHHIndex, hhMode); // hHHBuffer[bar_index] = hHHValue; // // LOW ... int hLLIndex = iLowest( _Symbol, _Period, llMode, hindLength, bar_index); double hLLValue = GetAppliedPrice( hLLIndex, llMode); // hLLBuffer[bar_index] = hLLValue; // // RESISTANCE SUPPORT ... // // RESISTANCE ... double rValue = (sHHValue + mHHValue + lHHValue + hHHValue) / 4; rBuffer[bar_index] = rValue; // // SUPPORT ... double sValue = (sLLValue + mLLValue + lLLValue + hLLValue) / 4; sBuffer[bar_index] = sValue; // // PEAKS and VALES ... // // PEAKS ... bool isPeak = rValue == hHHValue && hHHValue == lHHValue && lHHValue == mHHValue && mHHValue == sHHValue; // if (isPeak && lastPeak != rValue) { // if (showPeaksAndVales) { rColorIDX = 0; } // lastPeak = rValue; peaksBuffer[bar_index] = lastPeak; } else { peaksBuffer[bar_index] = lastPeak; } // // VALES ... bool isVale = sValue == hLLValue && hLLValue == lLLValue && lLLValue == mLLValue && mLLValue == sLLValue; // if (isVale && lastVale != sValue) { // if (showPeaksAndVales) { sColorIDX = 0; } // lastVale = sValue; valesBuffer[bar_index] = lastVale; } else { valesBuffer[bar_index] = lastVale; } // // Set Default Colors ... // rColorBuffer[bar_index] = rColorIDX; sColorBuffer[bar_index] = sColorIDX; // // Find Swings ... // // High ... int hSwing1 = FindSwing( _Symbol, _Period, MODE_HIGH, numOfShoulder, bar_index + 1); int hSwing2 = FindSwing( _Symbol, _Period, MODE_HIGH, numOfShoulder, hSwing1 + 1); // // High ... int hSwing3 = FindSwing( _Symbol, _Period, MODE_HIGH, numOfShoulder, hSwing2 + 1); // int hSwing4 = FindSwing( // _Symbol, // _Period, // MODE_HIGH, // numOfShoulder, // hSwing3 + 1); // // Low ... int lSwing1 = FindSwing( _Symbol, _Period, MODE_LOW, numOfShoulder, bar_index + 1); int lSwing2 = FindSwing( _Symbol, _Period, MODE_LOW, numOfShoulder, lSwing1 + 1); int lSwing3 = FindSwing( _Symbol, _Period, MODE_LOW, numOfShoulder, lSwing2 + 1); // int lSwing4 = FindSwing( // _Symbol, // _Period, // MODE_LOW, // numOfShoulder, // lSwing3 + 1); // if (bar_index < 50) { // bool isStrongResistance = // hHHBuffer[bar_index + 1] == hHHBuffer[bar_index + 2] && hHHBuffer[bar_index + 2] == hHHBuffer[bar_index + 3] && // lHHBuffer[bar_index + 1] == lHHBuffer[bar_index + 2] && lHHBuffer[bar_index + 2] == lHHBuffer[bar_index + 3] && // mHHBuffer[bar_index + 1] == mHHBuffer[bar_index + 2] && mHHBuffer[bar_index + 2] == mHHBuffer[bar_index + 3] && // sHHBuffer[bar_index + 1] == sHHBuffer[bar_index + 2] && sHHBuffer[bar_index + 2] == sHHBuffer[bar_index + 3] // ; // bool isStrongSupport = // hLLBuffer[bar_index + 1] == hLLBuffer[bar_index + 2] && hLLBuffer[bar_index + 2] == hLLBuffer[bar_index + 3] && // lLLBuffer[bar_index + 1] == lLLBuffer[bar_index + 2] && lLLBuffer[bar_index + 2] == lLLBuffer[bar_index + 3] && // mLLBuffer[bar_index + 1] == mLLBuffer[bar_index + 2] && mLLBuffer[bar_index + 2] == mLLBuffer[bar_index + 3] && // sLLBuffer[bar_index + 1] == sLLBuffer[bar_index + 2] && sLLBuffer[bar_index + 2] == sLLBuffer[bar_index + 3] // ; // if (isStrongResistance && showSRs) { // DrawSupportResistance( "R", clrRed, sHHBuffer[bar_index + 3], bar_index + 3); } // if (isStrongSupport && showSRs) { // DrawSupportResistance( "S", clrGreen, sLLBuffer[bar_index + 3], bar_index + 3); } } // if (showTrend && bar_index < 20) { // DrawSwing( "High1", MODE_HIGH, clrAqua, hSwing1, hSwing2); // DrawSwing( "Low1", MODE_LOW, clrAqua, lSwing1, lSwing2); // DrawSwing( "High2", MODE_HIGH, clrMagenta, hSwing2, hSwing3); // DrawSwing( "Low2", MODE_LOW, clrMagenta, lSwing2, lSwing3); } } // void DrawSwing( string prefix, // Prefix ... ENUM_SERIESMODE mode, // Swing Mode ... color swingColor, // Color ... int index1, // Index 1 ... int index2 // Index 2 ... ) { // if (mode != MODE_HIGH && mode != MODE_LOW) { return; } // XOHCL c1 = GetCandle( _Symbol, _Period, index1); // datetime time1 = GetCandleTime( _Symbol, _Period, index1); // XOHCL c2 = GetCandle( _Symbol, _Period, index2); // datetime time2 = GetCandleTime( _Symbol, _Period, index2); // double value1 = -1; double value2 = -1; // switch (mode) { // case MODE_HIGH: value1 = c1.high; value2 = c2.high; break; // case MODE_LOW: value1 = c1.low; value2 = c2.low; break; } // if (value1 == -1 || value2 == -1) { return; } // long chartId = ChartID(); string tlName = prefix + "_" + "TrendLine"; // // // if (IsDrawExists(tlName)) { // RemoveDraw(tlName); // } // DrawTrendLine( chartId, tlName, 0, time2, value2, time1, value1, swingColor, STYLE_DOT, 3, false, false, true); } // // Draw Support and Resistance ... void DrawSupportResistance( string prefix, // Prefix ... color lColor, // Color ... double price, // Support or Resistance price ... int bar_index // Start Bar Index ... ) { // datetime time1 = GetCandleTime( _Symbol, _Period, bar_index); // datetime time2 = GetCandleTime( _Symbol, _Period, bar_index - 1); // long chartId = ChartID(); string tlName = prefix + "_" + "TL" + "_" + (string)(NormalizePrice(price, _Symbol)); // DrawTrendLine( chartId, tlName, 0, time1, price, time2, price, lColor, STYLE_DOT, 3, false, false, true); } // // END Functions ... //