diff --git a/Documents/Indicators/x-saherelm.x121.xhk.mq5 b/Documents/Indicators/x-saherelm.x121.xhk.mq5 index c6a7cfe5..8e23e94d 100644 --- a/Documents/Indicators/x-saherelm.x121.xhk.mq5 +++ b/Documents/Indicators/x-saherelm.x121.xhk.mq5 @@ -136,6 +136,7 @@ int limit; int maxLength; // +int firstBarIndex; // // Event Handlers ... @@ -418,9 +419,21 @@ void CalculateBuffers( int barsLimit = startCalculationForLastBars > 0 ? startCalculationForLastBars : 0; + if (barsLimit == 0) + { + // + barsLimit = ratesTotal; + firstBarIndex = barsLimit - 1; + } + else + { + // + firstBarIndex = startCalculationForLastBars; + } + // + // bool canCalculate = true; bool canCalculate = - barsLimit == 0 || bar_index <= barsLimit; if (canCalculate) { @@ -487,6 +500,13 @@ void CalculateXHK( const double &low[] // ) { + // + int lastBarIndex = bar_index + 1; + bool isFirstBar = + startCalculationForLastBars > 0 + ? bar_index == startCalculationForLastBars + : bar_index == firstBarIndex; + // double mPrevHKOpenValue; double mPrevHKCloseValue; diff --git a/Indicators/x-saherelm.x121.xczone.mq5 b/Indicators/x-saherelm.x121.xczone.mq5 index 1d805194..0e465bc2 100644 --- a/Indicators/x-saherelm.x121.xczone.mq5 +++ b/Indicators/x-saherelm.x121.xczone.mq5 @@ -203,6 +203,7 @@ input int rsiLength = 14; // RSI Length input double rsiOBLevel = 70.0; // RSI OB Level input double rsiOSLevel = 30.0; // RSI OS Level input ENUM_APPLIED_PRICE rsiAppliedTo = PRICE_CLOSE; // RSI Applied To +input int atrLength = 14; // ATR Length input int barsSignalLength = 14; // Bars Signal Length input ENUM_X_MA_METHOD barsSignalMethod = X_MA_MODE_EMA; // Bars Signalling Method input int deltaSignalLength = 14; // Delta Signal Length @@ -215,7 +216,7 @@ input ENUM_X_MA_METHOD hkSignalBarMethod = X_MA_MODE_EMA; // Hiken Ashi Sign // // Validating ... input group "Validating"; -input bool forceHasSwing = true; // Force Blocks to Have Swing +input bool forceHasSwing = true; // Force Blocks to Have Swing input bool forceHasFLiquidity = false; // Force Blocks to Have Following Liquidity input bool forceHasRLiquidity = false; // Force Blocks to Have Reversal Liquidity input bool validateGapSequence = false; // Validate Block's Gap Sequence @@ -237,10 +238,10 @@ input bool filterBasedOnHKSignalBar = false; // Filter Based on Hiken Ashi Signa // // Alert ... input group "Alerts"; -input string alertPrefix = ""; // Alert Prefix +input string alertPrefix = ""; // Alert Prefix input bool _logAlerts = true; // Log Alerts -input bool _pushAlerts = false; // Push Alerts -input bool _mailAlerts = false; // Mail Alerts +input bool _pushAlerts = false; // Push Alerts +input bool _mailAlerts = false; // Mail Alerts input bool _terminalAlerts = true; // Terminal Alerts // @@ -264,6 +265,7 @@ input int startCalculationForLastBars = 1000; // Calculate Last n Bars input int sarArrowCode = 159; // Sar Arrow Code input int rrZoneLength = 7; // Draw Risk Reward Ratio till n Bars input int maxSignalR2R = 4; // Max Allowed Signal R2R +input double slAtrMultiplier = 0; // Additional SL ATR Multiplier // // Styling ... @@ -310,7 +312,7 @@ input ENUM_LINE_STYLE fvgStyle = STYLE_SOLID; // FVG Style #property indicator_chart_window // -#property indicator_buffers 31 +#property indicator_buffers 32 #property indicator_plots 5 // @@ -516,6 +518,11 @@ double bullishVolumeSignalBuffer[]; #define bearishVolumeSignalBufferIndex mLastBufferIndex + 12 double bearishVolumeSignalBuffer[]; +// +// ATR ... +#define atrBufferIndex mLastBufferIndex + 13 +double atrBuffer[]; + // // Variables, Properties and etc ... @@ -544,6 +551,7 @@ XCPOIDrawer *drawer; bool mEnableAlerts = false; XCBarAnalyser *barAnalyser; int mTrendPeriodLength = 0; +int atrHandler = INVALID_HANDLE; int sarHandler = INVALID_HANDLE; int rsiHandler = INVALID_HANDLE; int adxHandler = INVALID_HANDLE; @@ -614,6 +622,19 @@ int OnInit() mTrendPeriodLength = PeriodSeconds(mTrendPeriod) / cPeriodSeconds; } + // + // ATR ... + atrHandler = iATR( + _Symbol, + _Period, + atrLength // + ); + bool isInited = atrHandler != INVALID_HANDLE; + if (!isInited) + { + return INIT_FAILED; + } + // // SAR ... sarHandler = iSAR( @@ -622,7 +643,7 @@ int OnInit() sarStep, sarMax // ); - bool isInited = sarHandler != INVALID_HANDLE; + isInited = sarHandler != INVALID_HANDLE; if (!isInited) { return INIT_FAILED; @@ -742,6 +763,7 @@ void OnDeinit(const int reason) ShowBars(); // + IndicatorRelease(atrHandler); IndicatorRelease(sarHandler); IndicatorRelease(rsiHandler); IndicatorRelease(adxHandler); @@ -791,6 +813,10 @@ int OnCalculate( // // Validate Calculated Bars ... + // + // ATR ... + int atrCalculatedBars = BarsCalculated(atrHandler); + // // SAR ... int sarCalculatedBars = BarsCalculated(sarHandler); @@ -809,6 +835,9 @@ int OnCalculate( // bool isPassedRequiredCalculatedBars = + // + // ATR ... + atrCalculatedBars >= maxLength && // // SAR ... sarCalculatedBars >= maxLength && @@ -836,6 +865,10 @@ int OnCalculate( // // Buffers Copy ... + // + // ATR ... + int copiedAtrs = CopyBuffer(atrHandler, MAIN_LINE, 0, limit, atrBuffer); + // // SAR ... int copiedSars = CopyBuffer(sarHandler, MAIN_LINE, 0, limit, sarBuffer); @@ -863,6 +896,9 @@ int OnCalculate( // // Validate Copied Items ... bool isPassedRequiredCopiedItems = + // + // ATR ... + copiedAtrs >= limit && // // SAR ... copiedSars >= limit && @@ -920,6 +956,7 @@ bool ValidateInputs() sarMax > 0 && sarStep > 0 && adxLength > 0 && + atrLength > 0 && rsiLength > 0 && rsiOBLevel > 0 && rsiOSLevel > 0 && @@ -1159,6 +1196,11 @@ void DefineBuffers() // ArraySetAsSeries(bearishVolumeSignalBuffer, true); SetIndexBuffer(bearishVolumeSignalBufferIndex, bearishVolumeSignalBuffer, INDICATOR_CALCULATIONS); + + // + // ATR ... + ArraySetAsSeries(atrBuffer, true); + SetIndexBuffer(atrBufferIndex, atrBuffer, INDICATOR_CALCULATIONS); } /** @@ -2339,10 +2381,17 @@ void DetectTriggerBlock( targets // ); } + + // + double iATR = atrBuffer[bar_index]; + double slAdditional = slAtrMultiplier * iATR; + + // has = ToSignal( tb.trigger, tb.signal, - targets // + targets, + slAdditional // ); } diff --git a/Indicators/x-saherelm.x121.xdelta.mq5 b/Indicators/x-saherelm.x121.xdelta.mq5 deleted file mode 100644 index 9cadee69..00000000 --- a/Indicators/x-saherelm.x121.xdelta.mq5 +++ /dev/null @@ -1,550 +0,0 @@ -/////////////////////////////////////////////////////// -// -// SaherElm IT Center MQL5 Ocillator -// ------------------------------------------------- -// Name: X121 XDelta -// Description: Detect Comulative Volumes Delta ... -// -// -// 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 XDelta Ocillator" -#property strict - -// -// Definitions ... - -// -#define ShortName "X121_XDELTA" - -// -// Imports ... -#include "../Libraries/x-saherelm.common.lib.mq5" - -// -// Inputs ... - -// -// Makret ... -input group "Market"; -input int maLength = 14; // Moving Averge Length -input ENUM_X_MA_METHOD maMethod = X_MA_MODE_EMA; // Moving Averge Method - -// -// Presentation ... -input group "Presentation"; -input int startCalculationForLastBars = 1000; // Calculate Last n Bars -input bool showDeltaLine = true; // Show Delta Line -input bool showDeltaHistogram = true; // Show Delta Histogram -input bool showDeltaMa = true; // Show Delta Moving Average - -// -// Buffers Props ... - -// -#property indicator_separate_window - -// -#property indicator_buffers 6 -#property indicator_plots 3 - -// -#define hideColorIDX 0 -#define bullishColorIDX 1 -#define bearishColorIDX 2 - -// -// Buffers ... - -// -#define deltaLineBufferIndex 0 -double deltaLineBuffer[]; - -#property indicator_label1 "Delta Line" -#property indicator_type1 DRAW_LINE -#property indicator_color1 clrYellow -#property indicator_style1 STYLE_SOLID -#property indicator_width1 1 - -// -#define deltaHistogramBufferIndex 1 -double deltaHistogramBuffer[]; - -// -#define deltaHistogramColorBufferIndex 2 -double deltaHistogramColorBuffer[]; - -#property indicator_label2 "Delta Histogram" -#property indicator_type2 DRAW_COLOR_HISTOGRAM -#property indicator_color2 CLR_NONE, clrGreen, clrDarkRed -#property indicator_style2 STYLE_DOT -#property indicator_width2 1 - -// -#define deltaMaBufferIndex 3 -double deltaMaBuffer[]; - -#property indicator_label3 "Delta MA" -#property indicator_type3 DRAW_LINE -#property indicator_color3 clrOrchid -#property indicator_style3 STYLE_SOLID -#property indicator_width3 1 - -// -// Data Buffers ... - -#define mLastBufferIndex 3 - -// -#define rawDeltaBufferIndex mLastBufferIndex + 1 -double rawDeltaBuffer[]; - -// -#define deltaStateBufferIndex mLastBufferIndex + 2 -double deltaStateBuffer[]; - -// -// Variables, Properties and etc ... - -// -// this counts Available Bars ... -int limit; - -// -int maxLength; - -// -int firstBarIndex; - -// -// Event Handlers ... - -/** - * Initialize Indicator ... - * - * @return ( int ) - */ -int OnInit() -{ - // - // Validate Inputs ... - if (!ValidateInputs()) - { - return INIT_PARAMETERS_INCORRECT; - } - - // - // Initialize Indicator Handlers ... - - // - // 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(); - IndicatorSetInteger(INDICATOR_DIGITS, 2); - - // - // 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 -} - -/** - * 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); - - // - 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; -} - -// -// Functions ... - -/** - * Validate Input Args for Initialization ... - * - * @return ( bool ) - */ -bool ValidateInputs() -{ - // - bool result = false; - - // - result = maLength > 0 && - IsValid(maMethod); - - // - return result; -} - -/** - * Extract Max Length of Inputs ... - * - * @return ( int ) - */ -int ExtractMaxLengthOfInputs() -{ - // - int result = 0; - - // - result = MathMax(0, maLength); - - // - return result; -} - -/** - * Set Indicator Short Name and also we can define Buffers Labels ... - */ -void SetIndicatorName() -{ - IndicatorSetString(INDICATOR_SHORTNAME, ShortName); -} - -/** - * Define Required Buffers ... - */ -void DefineBuffers() -{ - // - // Plot Buffers ... - - // - // Delta Line ... - ENUM_DRAW_TYPE deltaLineDrawType = showDeltaLine ? DRAW_LINE : DRAW_NONE; - - // - ArraySetAsSeries(deltaLineBuffer, true); - SetIndexBuffer(deltaLineBufferIndex, deltaLineBuffer, INDICATOR_DATA); - PlotIndexSetDouble(deltaLineBufferIndex, PLOT_EMPTY_VALUE, 0.0); - PlotIndexSetInteger(deltaLineBufferIndex, PLOT_SHOW_DATA, showDeltaLine); - PlotIndexSetInteger(deltaLineBufferIndex, PLOT_DRAW_TYPE, deltaLineDrawType); - - // - // Delta Histogram ... - ENUM_DRAW_TYPE deltaHistogramDrawType = showDeltaHistogram ? DRAW_COLOR_HISTOGRAM : DRAW_NONE; - - // - ArraySetAsSeries(deltaHistogramBuffer, true); - SetIndexBuffer(deltaHistogramBufferIndex, deltaHistogramBuffer, INDICATOR_DATA); - PlotIndexSetDouble(deltaHistogramBufferIndex, PLOT_EMPTY_VALUE, 0.0); - PlotIndexSetInteger(deltaHistogramBufferIndex, PLOT_SHOW_DATA, showDeltaHistogram); - PlotIndexSetInteger(deltaHistogramBufferIndex, PLOT_DRAW_TYPE, deltaHistogramDrawType); - - // - ArraySetAsSeries(deltaHistogramColorBuffer, true); - SetIndexBuffer(deltaHistogramColorBufferIndex, deltaHistogramColorBuffer, INDICATOR_COLOR_INDEX); - - // - // Delta Ma ... - ENUM_DRAW_TYPE deltaMaDrawType = showDeltaMa ? DRAW_LINE : DRAW_NONE; - - // - ArraySetAsSeries(deltaMaBuffer, true); - SetIndexBuffer(deltaMaBufferIndex, deltaMaBuffer, INDICATOR_DATA); - PlotIndexSetDouble(deltaMaBufferIndex, PLOT_EMPTY_VALUE, EMPTY_VALUE); - PlotIndexSetInteger(deltaMaBufferIndex, PLOT_SHOW_DATA, showDeltaMa); - PlotIndexSetInteger(deltaMaBufferIndex, PLOT_DRAW_TYPE, deltaMaDrawType); - - // - // Data Buffers ... - - // - ArraySetAsSeries(rawDeltaBuffer, true); - SetIndexBuffer(rawDeltaBufferIndex, rawDeltaBuffer, INDICATOR_CALCULATIONS); - - // - ArraySetAsSeries(deltaStateBuffer, true); - SetIndexBuffer(deltaStateBufferIndex, deltaStateBuffer, INDICATOR_CALCULATIONS); -} - -/** - * 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; - if (barsLimit == 0) - { - // - barsLimit = ratesTotal; - firstBarIndex = barsLimit - 1; - } - else - { - // - firstBarIndex = startCalculationForLastBars; - } - - // - // bool canCalculate = true; - bool canCalculate = - bar_index <= barsLimit; - if (canCalculate) - { - // - CalculateValues( - bar_index, - prevCalculated, - ratesTotal, - open, - high, - close, - low, - tickVolume // - ); - } - else - { - FillBuffersZero(bar_index); - } -} - -/** - * Fill All Bufers to Zero Vlue for Specified Bar Index ... - * - * @param barIndex: Integer ... - */ -void FillBuffersZero(int barIndex) -{ - // - rawDeltaBuffer[barIndex] = 0.0; - deltaLineBuffer[barIndex] = 0.0; - deltaStateBuffer[barIndex] = 0.0; - deltaHistogramBuffer[barIndex] = 0.0; - deltaMaBuffer[barIndex] = 0.0; - deltaHistogramColorBuffer[barIndex] = hideColorIDX; -} - -/** - * Calculate Vales ... - * - * @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 CalculateValues( - 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[] // -) -{ - // - // Check Prev Bar ... - int lastBarIndex = bar_index + 1; - bool isFirstBar = - startCalculationForLastBars > 0 - ? bar_index == startCalculationForLastBars - : bar_index == firstBarIndex; - - // - XOHCL bar; - bool has = bar.Init( - _Symbol, - _Period, - bar_index // - ); - if (!has) - { - return; - } - - // - double iDelta = 0.0; - - // - // Detecting Delta Volume ... - if (bar.IsBullish()) - { - iDelta = (double)bar.volume; - } - else if (bar.IsBearish()) - { - iDelta = -(double)bar.volume; - } - - // - // Setting iDelta Value ... - if (isFirstBar) - { - // - rawDeltaBuffer[bar_index] = iDelta; - deltaLineBuffer[bar_index] = iDelta; - deltaHistogramBuffer[bar_index] = iDelta; - } - else - { - // - iDelta += deltaLineBuffer[bar_index + 1]; - - // - rawDeltaBuffer[bar_index] = iDelta; - deltaLineBuffer[bar_index] = iDelta; - deltaHistogramBuffer[bar_index] = iDelta; - } - - // - // Setting iDelta State ... - double iState = iDelta > 0 - ? bullishColorIDX - : iDelta < 0 - ? bearishColorIDX - : hideColorIDX; - // - // Setting iDelta Color ... - double iDeltaColor = iDelta > 0 - ? bullishColorIDX - : iDelta < 0 - ? bearishColorIDX - : hideColorIDX; - - // - deltaStateBuffer[bar_index] = iState; - deltaHistogramColorBuffer[bar_index] = iDeltaColor; - - // - // Calculate Moving Averages ... - int deltaMAsCount = iMAOnBuffer( - ratesTotal, - prevCalculated, - bar_index, - maLength, - rawDeltaBuffer, - deltaMaBuffer, - maMethod // - ); - - // - bar.Clean(); -} - -// diff --git a/Indicators/x-saherelm.x121.xhk.mq5 b/Indicators/x-saherelm.x121.xhk.mq5 deleted file mode 100644 index 8e23e94d..00000000 --- a/Indicators/x-saherelm.x121.xhk.mq5 +++ /dev/null @@ -1,636 +0,0 @@ -/////////////////////////////////////////////////////// -// -// SaherElm IT Center MQL5 Indicator -// ------------------------------------------------- -// Name: X121 XHK -// Description: XHK ... -// -// -// 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 XHK Indicator" -#property strict - -// -// Definitions ... - -// -#define ShortName "X121 XHK" - -// -// Includes Common Library ... -#include "../Libraries/x-saherelm.common.lib.mq5" - -// -// Inputs ... - -// -// Market ... -input group "Market"; -input int xhkSmoothingLength = 14; // Length -input ENUM_X_MA_METHOD xhkSmoothingMode = X_MA_MODE_EMA; // Smoothing Method -input bool xhkIgnoreSmoothingShadows = false; // Ignore Smoothed Bar Shadows - -// -input group "Presentation"; - -// -input int startCalculationForLastBars = 1000; // Calculate Last n Bars - -// -input bool drawRawXHKCandles = true; // Draw Raw Candle -input bool drawSmoothedXHKCandles = true; // Draw Smoothed Candle - -// -// Buffers ... - -// -#property indicator_chart_window - -// -#property indicator_buffers 10 -#property indicator_plots 2 - -// -#define hideColorIDX 0 -#define bullishColorIDX 1 -#define bearishColorIDX 2 -#define neuturalColorIDX 3 - -// -// PLOTTED Buffers ... - -// -#define openHKBufferIndex 0 -double openHKBuffer[]; - -// -#define highHKBufferIndex 1 -double highHKBuffer[]; - -// -#define lowHKBufferIndex 2 -double lowHKBuffer[]; - -// -#define closeHKBufferIndex 3 -double closeHKBuffer[]; - -// -#define rawHKColorBufferIndex 4 -double rawHKColorBuffer[]; - -// -#define rawHKBufferIndex 0 -#property indicator_label1 "X121 XHK Open;X121 XHK High;X121 XHK Low;X121 XHK Close" -#property indicator_type1 DRAW_COLOR_CANDLES -#property indicator_color1 CLR_NONE, clrDodgerBlue, clrDarkGoldenrod - -// -// Open ... -#define openBufferIndex 5 -double openBuffer[]; - -// -// High ... -#define highBufferIndex 6 -double highBuffer[]; - -// -// Low ... -#define lowBufferIndex 7 -double lowBuffer[]; - -// -// Close ... -#define closeBufferIndex 8 -double closeBuffer[]; - -// -// Candle Color ... -#define candleColorBufferIndex 9 -double candleColorBuffer[]; - -// -#define candlesBufferIndex 1 -#property indicator_label2 "X121 XHKSM Open;X121 XHKSM High;X121 XHKSM Low;X121 XHKSM Close" -#property indicator_type2 DRAW_COLOR_CANDLES -#property indicator_color2 CLR_NONE, clrAqua, clrMagenta - -// -// Variables, Properties and etc ... - -// -// this counts Available Bars ... -int limit; - -// -int maxLength; - -// -int firstBarIndex; - -// -// Event Handlers ... - -/** - * Initialize Indicator ... - * - * @return ( int ) - */ -int OnInit() -{ - // - // Validate Inputs ... - if (!ValidateInputs()) - { - return INIT_PARAMETERS_INCORRECT; - } - - // - // Initialize Indicator Handlers ... - - // - // 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 - - // -} - -/** - * 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); - - // - // Fill All Buffers by Zero ... - if (prev_calculated == 0) - { - } - - // - 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; -} - -// -// Functions ... - -/** - * Validate Input Args for Initialization ... - * - * @return ( bool ) - */ -bool ValidateInputs() -{ - // - bool result = - // - true - // - ; - - // - return result; -} - -/** - * Extract Max Length of Inputs ... - * - * @return ( int ) - */ -int ExtractMaxLengthOfInputs() -{ - // - int result = 0; - - // - result = MathMax(0, xhkSmoothingLength); - - // - return result; -} - -/** - * Define Required Buffers ... - */ -void DefineBuffers() -{ - - // - // RAW ... - - // - // Raw Candles Color ... - ArraySetAsSeries(rawHKColorBuffer, true); - SetIndexBuffer(rawHKColorBufferIndex, rawHKColorBuffer, INDICATOR_COLOR_INDEX); - - // - // Set EMPTY_VALUE on Candles Buffer ... - PlotIndexSetDouble(rawHKBufferIndex, PLOT_EMPTY_VALUE, 0.0); - PlotIndexSetInteger(rawHKBufferIndex, PLOT_SHOW_DATA, false); - - // - // Open ... - ArraySetAsSeries(openHKBuffer, true); - SetIndexBuffer(openHKBufferIndex, openHKBuffer, INDICATOR_DATA); - - // - // High ... - ArraySetAsSeries(highHKBuffer, true); - SetIndexBuffer(highHKBufferIndex, highHKBuffer, INDICATOR_DATA); - - // - // Close ... - ArraySetAsSeries(closeHKBuffer, true); - SetIndexBuffer(closeHKBufferIndex, closeHKBuffer, INDICATOR_DATA); - - // - // Low ... - ArraySetAsSeries(lowHKBuffer, true); - SetIndexBuffer(lowHKBufferIndex, lowHKBuffer, INDICATOR_DATA); - - // - // SMOOTHED ... - - // - // Candles Color ... - ArraySetAsSeries(candleColorBuffer, true); - SetIndexBuffer(candleColorBufferIndex, candleColorBuffer, INDICATOR_COLOR_INDEX); - - // - // Set EMPTY_VALUE on Candles Buffer ... - PlotIndexSetDouble(candlesBufferIndex, PLOT_EMPTY_VALUE, 0.0); - PlotIndexSetInteger(candlesBufferIndex, PLOT_SHOW_DATA, false); - - // - // Open ... - ArraySetAsSeries(openBuffer, true); - SetIndexBuffer(openBufferIndex, openBuffer, INDICATOR_DATA); - - // - // High ... - ArraySetAsSeries(highBuffer, true); - SetIndexBuffer(highBufferIndex, highBuffer, INDICATOR_DATA); - - // - // Close ... - ArraySetAsSeries(closeBuffer, true); - SetIndexBuffer(closeBufferIndex, closeBuffer, INDICATOR_DATA); - - // - // Low ... - ArraySetAsSeries(lowBuffer, true); - SetIndexBuffer(lowBufferIndex, lowBuffer, INDICATOR_DATA); -} - -/** - * 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; - if (barsLimit == 0) - { - // - barsLimit = ratesTotal; - firstBarIndex = barsLimit - 1; - } - else - { - // - firstBarIndex = startCalculationForLastBars; - } - - // - // bool canCalculate = true; - bool canCalculate = - bar_index <= barsLimit; - if (canCalculate) - { - // - CalculateXHK( - bar_index, - prevCalculated, - ratesTotal, - open, - high, - close, - low // - ); - } - else - { - FillBuffersZero(bar_index); - } -} - -// -// Custom ... - -/** - * Fill All Bufers to Zero Vlue for Specified Bar Index ... - * - * @param barIndex: Integer ... - */ -void FillBuffersZero(int barIndex) -{ - // - lowHKBuffer[barIndex] = 0.0; - openHKBuffer[barIndex] = 0.0; - highHKBuffer[barIndex] = 0.0; - closeHKBuffer[barIndex] = 0.0; - rawHKColorBuffer[barIndex] = hideColorIDX; - - // - lowBuffer[barIndex] = 0.0; - openBuffer[barIndex] = 0.0; - highBuffer[barIndex] = 0.0; - closeBuffer[barIndex] = 0.0; - candleColorBuffer[barIndex] = hideColorIDX; -} - -/** - * Calculate ATR Zones ... - * - * @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 CalculateXHK( - int bar_index, // Selected Bar Index - const int prevCalculated, - const int ratesTotal, - const double &open[], - const double &high[], - const double &close[], - const double &low[] // -) -{ - // - int lastBarIndex = bar_index + 1; - bool isFirstBar = - startCalculationForLastBars > 0 - ? bar_index == startCalculationForLastBars - : bar_index == firstBarIndex; - - // - double mPrevHKOpenValue; - double mPrevHKCloseValue; - - // - if (ArraySize(open) <= bar_index + 1) - { - // - mPrevHKOpenValue = 0; - mPrevHKCloseValue = 0; - } - else - { - // - mPrevHKOpenValue = openHKBuffer[bar_index + 1]; - mPrevHKCloseValue = closeHKBuffer[bar_index + 1]; - } - - // - double mHKOpenValue = (mPrevHKOpenValue + mPrevHKCloseValue) / 2; - double mHKCloseValue = (open[bar_index] + high[bar_index] + close[bar_index] + low[bar_index]) / 4; - double mHKHighValue = MathMax(high[bar_index], MathMax(mHKOpenValue, mHKCloseValue)); - double mHKLowValue = MathMin(low[bar_index], MathMin(mHKOpenValue, mHKCloseValue)); - - // - double candleColorValue = - mHKOpenValue < mHKCloseValue ? bullishColorIDX : bearishColorIDX; - - // - // Raw HK Buffers ... - openHKBuffer[bar_index] = mHKOpenValue; - highHKBuffer[bar_index] = mHKHighValue; - lowHKBuffer[bar_index] = mHKLowValue; - closeHKBuffer[bar_index] = mHKCloseValue; - rawHKColorBuffer[bar_index] = drawRawXHKCandles ? candleColorValue : hideColorIDX; - - // - // Apply Smoothing ... - bool canSmooth = xhkSmoothingLength > 0 && - xhkSmoothingMode != X_MA_MODE_NONE; - if (!canSmooth) - { - // - double min = MathMin(mHKOpenValue, mHKCloseValue); - double max = MathMax(mHKOpenValue, mHKCloseValue); - - // - openBuffer[bar_index] = mHKOpenValue; - highBuffer[bar_index] = xhkIgnoreSmoothingShadows ? max : mHKHighValue; - lowBuffer[bar_index] = xhkIgnoreSmoothingShadows ? min : mHKLowValue; - closeBuffer[bar_index] = mHKCloseValue; - candleColorBuffer[bar_index] = drawSmoothedXHKCandles ? candleColorValue : hideColorIDX; - } - else - { - // - // Open ... - iMAOnBuffer( - ratesTotal, - prevCalculated, - bar_index, - xhkSmoothingLength, - openHKBuffer, - openBuffer, - xhkSmoothingMode // - ); - - // - // Close ... - iMAOnBuffer( - ratesTotal, - prevCalculated, - bar_index, - xhkSmoothingLength, - closeHKBuffer, - closeBuffer, - xhkSmoothingMode // - ); - - // - // Ignoring Shadows ... - if (!xhkIgnoreSmoothingShadows) - { - // - // High ... - iMAOnBuffer( - ratesTotal, - prevCalculated, - bar_index, - xhkSmoothingLength, - highHKBuffer, - highBuffer, - xhkSmoothingMode // - ); - - // - // Low ... - iMAOnBuffer( - ratesTotal, - prevCalculated, - bar_index, - xhkSmoothingLength, - lowHKBuffer, - lowBuffer, - xhkSmoothingMode // - ); - } - else - { - // - double min = MathMin(closeBuffer[bar_index], openBuffer[bar_index]); - double max = MathMin(closeBuffer[bar_index], openBuffer[bar_index]); - - // - lowBuffer[bar_index] = min; - highBuffer[bar_index] = max; - } - - // - // Calculate Smoothed Color ... - candleColorValue = - openBuffer[bar_index] < closeBuffer[bar_index] ? bullishColorIDX : bearishColorIDX; - candleColorBuffer[bar_index] = drawSmoothedXHKCandles ? candleColorValue : hideColorIDX; - } -} - -// \ No newline at end of file diff --git a/Indicators/x-saherelm.x121.xvolume.mq5 b/Indicators/x-saherelm.x121.xvolume.mq5 deleted file mode 100644 index 657cb9a7..00000000 --- a/Indicators/x-saherelm.x121.xvolume.mq5 +++ /dev/null @@ -1,567 +0,0 @@ -/////////////////////////////////////////////////////// -// -// SaherElm IT Center MQL5 Ocillator -// ------------------------------------------------- -// Name: X121 XVolume -// Description: Detect Bullish/Bearish Volumes ... -// -// -// 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 XVolume Ocillator" -#property strict - -// -// Definitions ... - -// -#define ShortName "X121_XVOLUME" - -// -// Imports ... -#include "../Libraries/x-saherelm.common.lib.mq5" - -// -// Inputs ... - -// -// Makret ... -input group "Market"; -input int maLength = 14; // Moving Averge Length -input ENUM_X_MA_METHOD maMethod = X_MA_MODE_EMA; // Moving Averge Method - -// -// Presentation ... -input group "Presentation"; -input int startCalculationForLastBars = 1000; // Calculate Last n Bars -input bool showBullishVolume = true; // Show Bullish Volume -input bool showBullishVolumeMa = true; // Show Bullish Volume Moving Average -input bool showBearishVolume = true; // Show Bearish Volume -input bool showBearishVolumeMa = true; // Show Bearish Volume Moving Average - -// -// Buffers Props ... - -// -#property indicator_separate_window - -// -#property indicator_buffers 6 -#property indicator_plots 4 - -// -// Buffers ... - -// -#define bullishVolumeBufferIndex 0 -double bullishVolumeBuffer[]; - -#property indicator_label1 "Bullish Volume" -#property indicator_type1 DRAW_HISTOGRAM -#property indicator_color1 clrLime -#property indicator_style1 STYLE_DOT -#property indicator_width1 1 - -// -#define bullishVolumeMaBufferIndex 1 -double bullishVolumeMaBuffer[]; - -#property indicator_label2 "Bullish Volume MA" -#property indicator_type2 DRAW_LINE -#property indicator_color2 clrLime -#property indicator_style2 STYLE_SOLID -#property indicator_width2 1 - -// -#define bearishVolumeBufferIndex 2 -double bearishVolumeBuffer[]; - -#property indicator_label3 "Bearish Volume" -#property indicator_type3 DRAW_HISTOGRAM -#property indicator_color3 clrRed -#property indicator_style3 STYLE_DOT -#property indicator_width3 1 - -// -#define bearishVolumeMaBufferIndex 3 -double bearishVolumeMaBuffer[]; - -#property indicator_label4 "Bearish Volume MA" -#property indicator_type4 DRAW_LINE -#property indicator_color4 clrRed -#property indicator_style4 STYLE_SOLID -#property indicator_width4 1 - -// -// Data Buffers ... - -#define mLastBufferIndex 3 - -// -#define rawBullishVolumeBufferIndex mLastBufferIndex + 1 -double rawBullishVolumeBuffer[]; - -// -#define rawBearishVolumeBufferIndex mLastBufferIndex + 2 -double rawBearishVolumeBuffer[]; - -// -// Variables, Properties and etc ... - -// -// this counts Available Bars ... -int limit; - -// -int maxLength; - -// -int firstBarIndex; - -// -// Event Handlers ... - -/** - * Initialize Indicator ... - * - * @return ( int ) - */ -int OnInit() -{ - // - // Validate Inputs ... - if (!ValidateInputs()) - { - return INIT_PARAMETERS_INCORRECT; - } - - // - // Initialize Indicator Handlers ... - - // - // 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(); - IndicatorSetInteger(INDICATOR_DIGITS, 2); - - // - // 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 -} - -/** - * 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); - - // - 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; -} - -// -// Functions ... - -/** - * Validate Input Args for Initialization ... - * - * @return ( bool ) - */ -bool ValidateInputs() -{ - // - bool result = false; - - // - result = maLength > 0 && - IsValid(maMethod); - - // - return result; -} - -/** - * Extract Max Length of Inputs ... - * - * @return ( int ) - */ -int ExtractMaxLengthOfInputs() -{ - // - int result = 0; - - // - result = MathMax(0, maLength); - - // - return result; -} - -/** - * Set Indicator Short Name and also we can define Buffers Labels ... - */ -void SetIndicatorName() -{ - IndicatorSetString(INDICATOR_SHORTNAME, ShortName); -} - -/** - * Define Required Buffers ... - */ -void DefineBuffers() -{ - // - // Plot Buffers ... - - // - // Bullish ... - - // - // Volume ... - ENUM_DRAW_TYPE bullishVolumeDrawType = showBullishVolume ? DRAW_HISTOGRAM : DRAW_NONE; - - // - ArraySetAsSeries(bullishVolumeBuffer, true); - SetIndexBuffer(bullishVolumeBufferIndex, bullishVolumeBuffer, INDICATOR_DATA); - PlotIndexSetDouble(bullishVolumeBufferIndex, PLOT_EMPTY_VALUE, EMPTY_VALUE); - PlotIndexSetInteger(bullishVolumeBufferIndex, PLOT_SHOW_DATA, showBullishVolume); - PlotIndexSetInteger(bullishVolumeBufferIndex, PLOT_DRAW_TYPE, bullishVolumeDrawType); - - // - // Average ... - ENUM_DRAW_TYPE bullishVolumeMaDrawType = showBullishVolumeMa ? DRAW_LINE : DRAW_NONE; - - // - ArraySetAsSeries(bullishVolumeMaBuffer, true); - SetIndexBuffer(bullishVolumeMaBufferIndex, bullishVolumeMaBuffer, INDICATOR_DATA); - PlotIndexSetDouble(bullishVolumeMaBufferIndex, PLOT_EMPTY_VALUE, 0.0); - PlotIndexSetInteger(bullishVolumeMaBufferIndex, PLOT_SHOW_DATA, showBullishVolumeMa); - PlotIndexSetInteger(bullishVolumeMaBufferIndex, PLOT_DRAW_TYPE, bullishVolumeMaDrawType); - - // - // Bearish ... - - // - // Volume ... - ENUM_DRAW_TYPE bearishVolumeDrawType = showBearishVolume ? DRAW_HISTOGRAM : DRAW_NONE; - - // - ArraySetAsSeries(bearishVolumeBuffer, true); - SetIndexBuffer(bearishVolumeBufferIndex, bearishVolumeBuffer, INDICATOR_DATA); - PlotIndexSetDouble(bearishVolumeBufferIndex, PLOT_EMPTY_VALUE, EMPTY_VALUE); - PlotIndexSetInteger(bearishVolumeBufferIndex, PLOT_SHOW_DATA, showBearishVolume); - PlotIndexSetInteger(bearishVolumeBufferIndex, PLOT_DRAW_TYPE, bearishVolumeDrawType); - - // - // Average ... - ENUM_DRAW_TYPE bearishVolumeMaDrawType = showBearishVolumeMa ? DRAW_LINE : DRAW_NONE; - - // - ArraySetAsSeries(bearishVolumeMaBuffer, true); - SetIndexBuffer(bearishVolumeMaBufferIndex, bearishVolumeMaBuffer, INDICATOR_DATA); - PlotIndexSetDouble(bearishVolumeMaBufferIndex, PLOT_EMPTY_VALUE, 0.0); - PlotIndexSetInteger(bearishVolumeMaBufferIndex, PLOT_SHOW_DATA, showBearishVolumeMa); - PlotIndexSetInteger(bearishVolumeMaBufferIndex, PLOT_DRAW_TYPE, bearishVolumeMaDrawType); - - // - // Data Buffers ... - - // - ArraySetAsSeries(rawBullishVolumeBuffer, true); - SetIndexBuffer(rawBullishVolumeBufferIndex, rawBullishVolumeBuffer, INDICATOR_CALCULATIONS); - - // - ArraySetAsSeries(rawBearishVolumeBuffer, true); - SetIndexBuffer(rawBearishVolumeBufferIndex, rawBearishVolumeBuffer, INDICATOR_CALCULATIONS); -} - -/** - * 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; - if (barsLimit == 0) - { - // - barsLimit = ratesTotal; - firstBarIndex = barsLimit - 1; - } - else - { - // - firstBarIndex = startCalculationForLastBars; - } - - // - // bool canCalculate = true; - bool canCalculate = - bar_index <= barsLimit; - if (canCalculate) - { - // - CalculateValues( - bar_index, - prevCalculated, - ratesTotal, - open, - high, - close, - low, - tickVolume // - ); - } - else - { - FillBuffersZero(bar_index); - } -} - -/** - * Fill All Bufers to Zero Vlue for Specified Bar Index ... - * - * @param barIndex: Integer ... - */ -void FillBuffersZero(int barIndex) -{ - // - bullishVolumeBuffer[barIndex] = EMPTY_VALUE; - bullishVolumeMaBuffer[barIndex] = EMPTY_VALUE; - rawBullishVolumeBuffer[barIndex] = 0; - - // - bearishVolumeBuffer[barIndex] = EMPTY_VALUE; - bearishVolumeMaBuffer[barIndex] = EMPTY_VALUE; - rawBearishVolumeBuffer[barIndex] = 0; -} - -/** - * Calculate Vales ... - * - * @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 CalculateValues( - 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[] // -) -{ - // - // Check Prev Bar ... - // - int lastBarIndex = bar_index + 1; - bool isFirstBar = - startCalculationForLastBars > 0 - ? bar_index == startCalculationForLastBars - : bar_index == firstBarIndex; - - // - XOHCL bar; - bool has = bar.Init( - _Symbol, - _Period, - bar_index // - ); - if (!has) - { - return; - } - - // - double iBullishVolume = 0; - double iBearishVolume = 0; - - // - if (bar.IsBullish()) - { - // - iBullishVolume = (double)bar.volume; - iBearishVolume = 0; - } - else if (bar.IsBearish()) - { - // - iBearishVolume = (double)bar.volume; - iBullishVolume = 0; - } - else - { - // - double volume = ((double)bar.volume) / 2.0; - iBullishVolume = volume; - iBearishVolume = volume; - } - - // - bullishVolumeBuffer[bar_index] = - iBullishVolume == 0 - ? EMPTY_VALUE - : iBullishVolume; - rawBullishVolumeBuffer[bar_index] = iBullishVolume; - - // - bearishVolumeBuffer[bar_index] = - iBearishVolume == 0 - ? EMPTY_VALUE - : iBearishVolume; - rawBearishVolumeBuffer[bar_index] = iBearishVolume; - - // - // Calculate Moving Averages ... - - // - int bullishMAsCount = iMAOnBuffer( - ratesTotal, - prevCalculated, - bar_index, - maLength, - rawBullishVolumeBuffer, - bullishVolumeMaBuffer, - maMethod // - ); - - // - int bearishMAsCount = iMAOnBuffer( - ratesTotal, - prevCalculated, - bar_index, - maLength, - rawBearishVolumeBuffer, - bearishVolumeMaBuffer, - maMethod // - ); - - // - bar.Clean(); -} - -// diff --git a/Indicators/x-saherelm.x121.xvwap.mq5 b/Indicators/x-saherelm.x121.xvwap.mq5 deleted file mode 100644 index ca2a2c46..00000000 --- a/Indicators/x-saherelm.x121.xvwap.mq5 +++ /dev/null @@ -1,712 +0,0 @@ -/////////////////////////////////////////////////////// -// -// SaherElm IT Center MQL5 Indicator -// ------------------------------------------------- -// Name: X121 XVWAP -// Description: XVWAP ... -// -// -// 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 XVWAP Indicator" -#property strict - -// -// Definitions ... - -// -#define ShortName "X121 XVWAP" - -// -// Includes Common Library ... -#include "../Libraries/x-saherelm.common.lib.mq5" - -// -// Inputs ... - -// -// Market ... -input group "Market"; -input int vwapFastLength = 20; // Fast Length -input int vwapMidLength = 40; // Mid Length -input int vwapSlowLength = 60; // Slow Length -input ENUM_APPLIED_PRICE vwapAppliedTo = PRICE_CLOSE; // Applied To - -// -// Presentation ... -input group "Presentation"; - -// -input int startCalculationForLastBars = 1000; // Calculate Last n Bars - -// -input bool showVWapFast = true; // Show VWap Fast -input bool showVWapMedium = true; // Show VWap Medium -input bool showVWapSlow = true; // Show VWap Slow - -// -// Buffers ... - -// -#define hideColorIDX 0 -#define bullishColorIDX 1 -#define bearishColorIDX 2 -#define neuturalColorIDX 3 - -// -#property indicator_chart_window - -// -#property indicator_buffers 11 -#property indicator_plots 3 - -// -// Plot Buffers ... - -// -// FAST ... -#define vwapFastBufferIndex 0 -double vwapFastBuffer[]; - -#define vwapFastColorBufferIndex 1 -double vwapFastColorBuffer[]; - -// -#define vwapFastPlotBufferIndex 0 -#property indicator_label1 "X121 VWF" -#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 vwapMidBufferIndex 2 -double vwapMidBuffer[]; - -#define vwapMidColorBufferIndex 3 -double vwapMidColorBuffer[]; - -// -#define vwapMidPlotBufferIndex 1 -#property indicator_label2 "X121 VWM" -#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 vwapSlowBufferIndex 4 -double vwapSlowBuffer[]; - -#define vwapSlowColorBufferIndex 5 -double vwapSlowColorBuffer[]; - -// -#define vwapSlowPlotBufferIndex 2 -#property indicator_label3 "X121 VWS" -#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 ... - -// -int mLastBufferIndex = 5; - -// -// Volume ... -#define vwapVolumeBufferIndex mLastBufferIndex + 1 -double vwapVolumeBuffer[]; - -// -// Price ... -#define vwapPriceBufferIndex mLastBufferIndex + 2 -double vwapPriceBuffer[]; - -// -// Fast State ... -#define vwapFastStateBufferIndex mLastBufferIndex + 3 -double vwapFastStateBuffer[]; - -// -// Mid State ... -#define vwapMidStateBufferIndex mLastBufferIndex + 4 -double vwapMidStateBuffer[]; - -// -// Slow State ... -#define vwapSlowStateBufferIndex mLastBufferIndex + 5 -double vwapSlowStateBuffer[]; - -// -// Variables, Properties and etc ... - -// -// this counts Available Bars ... -int limit; - -// -int maxLength; - -// -// Event Handlers ... - -/** - * Initialize Indicator ... - * - * @return ( int ) - */ -int OnInit() -{ - // - // Validate Inputs ... - if (!ValidateInputs()) - { - return INIT_PARAMETERS_INCORRECT; - } - - // - // Initialize Indicator Handlers ... - - // - // 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 -} - -/** - * 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 ... - - // - limit = (prev_calculated > rates_total || prev_calculated <= 0) - ? rates_total - : (rates_total - prev_calculated) + 1; - - // - // Buffers Copy ... - - // - // 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 = - // - vwapFastLength > 2 && - vwapMidLength > vwapFastLength && - vwapSlowLength > vwapMidLength - // - ; - - // - return result; -} - -/** - * Extract Max Length of Inputs ... - * - * @return ( int ) - */ -int ExtractMaxLengthOfInputs() -{ - // - int result = 0; - - // - // XVWAP ... - result = MathMax(result, vwapFastLength); - result = MathMax(result, vwapMidLength); - result = MathMax(result, vwapSlowLength); - - // - return result; -} - -/** - * Define Required Buffers ... - */ -void DefineBuffers() -{ - // - // Plot Buffers ... - - // - // Fast ... - ArraySetAsSeries(vwapFastBuffer, true); - ArraySetAsSeries(vwapFastColorBuffer, true); - SetIndexBuffer(vwapFastBufferIndex, vwapFastBuffer, INDICATOR_DATA); - SetIndexBuffer(vwapFastColorBufferIndex, vwapFastColorBuffer, INDICATOR_COLOR_INDEX); - - // - // Mid ... - ArraySetAsSeries(vwapMidBuffer, true); - ArraySetAsSeries(vwapMidColorBuffer, true); - SetIndexBuffer(vwapMidBufferIndex, vwapMidBuffer, INDICATOR_DATA); - SetIndexBuffer(vwapMidColorBufferIndex, vwapMidColorBuffer, INDICATOR_COLOR_INDEX); - - // - // Slow ... - ArraySetAsSeries(vwapSlowBuffer, true); - ArraySetAsSeries(vwapSlowColorBuffer, true); - SetIndexBuffer(vwapSlowBufferIndex, vwapSlowBuffer, INDICATOR_DATA); - SetIndexBuffer(vwapSlowColorBufferIndex, vwapSlowColorBuffer, INDICATOR_COLOR_INDEX); - - // - // Data Buffers ... - - // - // Volumes ... - ArraySetAsSeries(vwapVolumeBuffer, true); - SetIndexBuffer(vwapVolumeBufferIndex, vwapVolumeBuffer, INDICATOR_CALCULATIONS); - - // - // Price ... - ArraySetAsSeries(vwapPriceBuffer, true); - SetIndexBuffer(vwapPriceBufferIndex, vwapPriceBuffer, INDICATOR_CALCULATIONS); - - // - // Fast State ... - ArraySetAsSeries(vwapFastStateBuffer, true); - SetIndexBuffer(vwapFastStateBufferIndex, vwapFastStateBuffer, INDICATOR_CALCULATIONS); - - // - // Mid State ... - ArraySetAsSeries(vwapMidStateBuffer, true); - SetIndexBuffer(vwapMidStateBufferIndex, vwapMidStateBuffer, INDICATOR_CALCULATIONS); - - // - // Slow State ... - ArraySetAsSeries(vwapSlowStateBuffer, true); - SetIndexBuffer(vwapSlowStateBufferIndex, vwapSlowStateBuffer, 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 Required VWAP Data Buffers ... - if (ratesTotal - bar_index <= maxLength) - { - // - CalculateVWAPDataBuffers( - bar_index, - prevCalculated, - ratesTotal, - open, - high, - close, - low, - tickVolume // - ); - - // - // Prevent Moving Forward ... - } - else - { - // - CalculateVWAPDataBuffers( - bar_index, - prevCalculated, - ratesTotal, - open, - high, - close, - low, - tickVolume // - ); - - // - CalculateVWAPS( - bar_index, - prevCalculated, - ratesTotal, - open, - high, - close, - low, - tickVolume // - ); - } - } - else - { - FillBuffersZero(bar_index); - } -} - -// -// Custom ... - -/** - * Fill All Bufers to Zero Vlue for Specified Bar Index ... - * - * @param barIndex: Integer ... - */ -void FillBuffersZero(int barIndex) -{ - // - vwapFastBuffer[barIndex] = EMPTY_VALUE; - vwapMidBuffer[barIndex] = EMPTY_VALUE; - vwapSlowBuffer[barIndex] = EMPTY_VALUE; - vwapVolumeBuffer[barIndex] = EMPTY_VALUE; - vwapPriceBuffer[barIndex] = EMPTY_VALUE; - - // - vwapFastColorBuffer[barIndex] = hideColorIDX; - vwapMidColorBuffer[barIndex] = hideColorIDX; - vwapSlowColorBuffer[barIndex] = hideColorIDX; - vwapFastStateBuffer[barIndex] = hideColorIDX; - vwapMidStateBuffer[barIndex] = hideColorIDX; - vwapSlowStateBuffer[barIndex] = hideColorIDX; -} - -/** - * Calculate VWAP Value for Specified Bar ... - * - * @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 ... - * @param _length: Integer, Specified VWAP Length ... - * @param _show: Boolean, Specified Show Buffer or not ... - * @param _buffer: Double Array Reference, Points to Buffer ... - * @param _colorBuffer: Double Array Reference, Points to Color Buffer ... - * @param _stateBuffer: Double Array Reference, Points to State Buffer ... - */ -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 += vwapPriceBuffer[x + bar_index]; - vSum += vwapVolumeBuffer[x + bar_index]; - mSum += vwapPriceBuffer[x + bar_index] * vwapVolumeBuffer[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; - } -} - -/** - * Calculate VWAP Required Data 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 CalculateVWAPDataBuffers( - 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[] // -) -{ - // - ENUM_X_PRICE mPType = ToXPrice(vwapAppliedTo); - double price = GetAppliedPrice( - mPType, - open, - high, - low, - close, - bar_index // - ); - vwapPriceBuffer[bar_index] = price; - vwapVolumeBuffer[bar_index] = (double)tickVolume[bar_index]; -} - -/** - * Calculate Different VWaps ... - * - * @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 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, - // - vwapFastLength, - showVWapFast, - vwapFastBuffer, - vwapFastColorBuffer, - vwapFastStateBuffer // - ); - - // - // Mid ... - CalculateVWAP( - bar_index, - prevCalculated, - ratesTotal, - open, - high, - close, - low, - tickVolume, - // - vwapMidLength, - showVWapMedium, - vwapMidBuffer, - vwapMidColorBuffer, - vwapMidStateBuffer // - ); - - // - // Fast ... - CalculateVWAP( - bar_index, - prevCalculated, - ratesTotal, - open, - high, - close, - low, - tickVolume, - // - vwapSlowLength, - showVWapSlow, - vwapSlowBuffer, - vwapSlowColorBuffer, - vwapSlowStateBuffer // - ); -} - -// \ No newline at end of file