Files
xMQL5/MQLTestWorkspace/XBEEA/14030224/Indicators/x-saherelm.x121.xzg.mq5
T
2024-05-13 18:21:02 +03:30

758 lines
15 KiB
Plaintext

///////////////////////////////////////////////////////
//
// SaherElm IT Center MQL5 Indicator
// ---------------------------------------------
// Name: X121_XZG
// Description: Zogzag Implementation ...
//
//
// 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_XZG Indicator"
#property strict
//
#define ShortName "X121_XZG"
//
// DEFINITIONS ...
enum ENUM_X_ZG_SEARCH_MODE
{
X_ZG_SEARCH_EXTREMUM = 0, // searching for the first extremum
X_ZG_SEARCH_PEAK = 1, // searching for the next ZigZag peak
X_ZG_SEARCH_VALE = -1 // searching for the next ZigZag vale
};
//
// How to Calculate Up and Down Price ...
enum ENUM_X_ZG_PRICE_MODE
{
//
X_ZG_PRICE_HIGH_LOW_MODE = 1,
X_ZG_PRICE_OPEN_CLOSE_MODE = 2,
};
//
// INPUT ...
//
input int depth = 12; // Depth
input int deviation = 5; // Deviation
input int backStep = 3; // Back Step
input ENUM_X_ZG_PRICE_MODE mode = X_ZG_PRICE_HIGH_LOW_MODE; // Mode
//
// BUFFERS ...
//
#property indicator_chart_window
//
#property indicator_buffers 5
#property indicator_plots 3
//
// ZigZag ...
//
#define mainBufferIndex 0
#define mainBufferPlotIndex 0
double mainBuffer[];
//
#property indicator_label1 "X121_XZG Main"
#property indicator_type1 DRAW_SECTION
#property indicator_color1 clrYellow
#property indicator_width1 2
//
// Highs ...
//
// ZigZag Pointer or Line Indicator ...
#define arrowBufferIndex 1
#define arrowBufferPlotIndex 1
double arrowBuffer[];
//
#define arrowColorBufferIndex 2
double arrowColorBuffer[];
//
#property indicator_label2 "X121_XZG PV"
#property indicator_type2 DRAW_COLOR_ARROW
#property indicator_color2 clrAqua, clrMagenta
#property indicator_width2 1
//
#define highsBufferIndex 3
double highsBuffer[];
//
// Lows ...
//
#define lowsBufferIndex 4
double lowsBuffer[];
//
// VARIABLES ...
//
int mRecalc = 3; // Number of last extremes for recalculation
//
// EVENT Handlers ...
//
// Initialization ...
int OnInit()
{
//
// Validate Inputs ...
if (!ValidateInputs())
{
return INIT_PARAMETERS_INCORRECT;
}
//
// logTag = ShortName;
//
// Define Index Buffers ...
DefineBuffers();
//
// Set Indicator ShortName ...
SetIndicatorName();
//
// Init Succeed ...
return INIT_SUCCEEDED;
}
//
// DeInitialization ...
void OnDeinit(const int reason)
{
//
// REASON_PROGRAM 0 The EA has stopped working calling the ExpertRemove() function
// REASON_REMOVE 1 Program removed from a chart
// REASON_RECOMPILE 2 Program recompiled
// REASON_CHARTCHANGE 3 A symbol or a chart period is changed
// REASON_CHARTCLOSE 4 Chart closed
// REASON_PARAMETERS 5 Inputs changed by a user
// REASON_ACCOUNT 6 Another account has been activated or reconnection to the trade server has occurred due to changes in the account settings
// REASON_TEMPLATE 7 Another chart template applied
// REASON_INITFAILED 8 The OnInit() handler returned a non-zero value
// REASON_CLOSE 9 Terminal closed
}
//
// Calculating what we want ...
int OnCalculate(
//
// 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[] //
)
{
//
double mZGHigh[];
double mZGLow[];
if (mode == X_ZG_PRICE_OPEN_CLOSE_MODE)
{
//
ArrayCopy(
mZGHigh,
open
//
);
//
ArrayCopy(
mZGLow,
close
//
);
}
else
{
//
ArrayCopy(
mZGHigh,
high
//
);
//
ArrayCopy(
mZGLow,
low
//
);
}
//
int zigZagResult = CalcukateZigZag(
prev_calculated,
rates_total,
mZGHigh,
mZGLow
//
);
//
if (zigZagResult != rates_total)
{
return zigZagResult;
}
//
return rates_total;
}
//
// CUSTOM Functions ...
//
// Validate Input Args for Initialization ...
bool ValidateInputs()
{
//
bool result = false;
//
result =
//
depth > 0 &&
deviation > 0 &&
backStep > 0
//
;
//
return result;
}
//
// Define Indexes and Styles ...
void DefineBuffers()
{
//
// Main ...
//
SetIndexBuffer(mainBufferIndex, mainBuffer, INDICATOR_DATA);
//
PlotIndexSetInteger(mainBufferPlotIndex, PLOT_SHOW_DATA, true);
PlotIndexSetInteger(mainBufferPlotIndex, PLOT_DRAW_BEGIN, 0);
PlotIndexSetDouble(mainBufferPlotIndex, PLOT_EMPTY_VALUE, 0);
//
// Arrow ...
//
SetIndexBuffer(arrowBufferIndex, arrowBuffer, INDICATOR_DATA);
//
PlotIndexSetInteger(arrowBufferPlotIndex, PLOT_SHOW_DATA, true);
PlotIndexSetInteger(arrowBufferPlotIndex, PLOT_DRAW_BEGIN, 0);
PlotIndexSetInteger(arrowBufferPlotIndex, PLOT_ARROW, 159);
PlotIndexSetInteger(arrowBufferPlotIndex, PLOT_ARROW_SHIFT, 0);
PlotIndexSetDouble(arrowBufferPlotIndex, PLOT_EMPTY_VALUE, 0);
//
// Arrow Color ...
SetIndexBuffer(arrowColorBufferIndex, arrowColorBuffer, INDICATOR_COLOR_INDEX);
//
// Highs ...
SetIndexBuffer(highsBufferIndex, highsBuffer, INDICATOR_CALCULATIONS);
//
// Lows ...
SetIndexBuffer(lowsBufferIndex, lowsBuffer, INDICATOR_CALCULATIONS);
}
//
// Set Indicator Short Name and also we can define Buffers Labels ...
void SetIndicatorName()
{
IndicatorSetString(INDICATOR_SHORTNAME, ShortName);
}
//
// Calculating Zig Zag ...
int CalcukateZigZag(
const int prev_calculated,
const int rates_total,
double &high[],
double &low[]
//
)
{
//
if (rates_total < 100)
{
return 0;
}
//
// Define Variables ...
int i = 0;
//
int start = 0;
int extreme_counter = 0;
int extreme_search = X_ZG_SEARCH_EXTREMUM;
int shift = 0;
int back = 0;
int last_high_pos = 0;
int last_low_pos = 0;
//
double val = 0;
double res = 0;
double curlow = 0;
double curhigh = 0;
double last_high = 0;
double last_low = 0;
//
double colorIDX = 0;
double lastZigZagValue = 0;
//
// Initializing ...
if (prev_calculated == 0)
{
//
ArrayInitialize(mainBuffer, 0);
ArrayInitialize(highsBuffer, 0);
ArrayInitialize(lowsBuffer, 0);
//
start = depth;
}
//
// Already Calculated Before ...
if (prev_calculated > 0)
{
//
i = rates_total - 1;
//
// Searching for the third X_ZG_SEARCH_EXTREMUM from the last UnComplete Bar ...
while (extreme_counter < mRecalc && i > rates_total - 100)
{
//
res = mainBuffer[i];
if (res != 0)
{
extreme_counter++;
}
//
i--;
}
//
i++;
start = i;
//
// What type of X_ZG_SEARCH_EXTREMUM we Search for ...
if (lowsBuffer[i] != 0)
{
//
curlow = lowsBuffer[i];
extreme_search = X_ZG_SEARCH_PEAK;
}
else
{
//
curhigh = highsBuffer[i];
extreme_search = X_ZG_SEARCH_VALE;
}
//
// Clear Values ...
for (i = start + 1; i < rates_total && !IsStopped(); i++)
{
//
mainBuffer[i] = 0;
lowsBuffer[i] = 0;
highsBuffer[i] = 0;
}
}
//
// Search for High and Low Extremes ...
for (shift = start; shift < rates_total && !IsStopped(); shift++)
{
//
// Low ...
val = low[Lowest(low, depth, shift)];
if (val == last_low)
{
val = 0;
}
else
{
//
last_low = val;
//
if ((low[shift] - val) > deviation * _Point)
{
val = 0;
}
else
{
//
for (back = 1; back <= backStep; back++)
{
//
res = lowsBuffer[shift - back];
if ((res != 0) && (res > val))
{
lowsBuffer[shift - back] = 0;
}
}
}
}
//
if (low[shift] == val)
{
lowsBuffer[shift] = val;
}
else
{
lowsBuffer[shift] = 0;
}
//
// High ...
val = high[Highest(high, depth, shift)];
if (val == last_high)
{
val = 0;
}
else
{
//
last_high = val;
//
if ((val - high[shift]) > deviation * _Point)
{
val = 0;
}
else
{
//
for (back = 1; back <= backStep; back++)
{
//
res = highsBuffer[shift - back];
if ((res != 0) && (res < val))
{
highsBuffer[shift - back] = 0;
}
}
}
}
//
if (high[shift] == val)
{
highsBuffer[shift] = val;
}
else
{
highsBuffer[shift] = 0;
}
}
//
// Set Last Value ...
if (extreme_search == 0) // Undefined Values ...
{
//
last_low = 0;
last_high = 0;
}
else
{
//
last_low = curlow;
last_high = curhigh;
}
//
// Final Selection of Extreme Points for ZigZag ...
for (shift = start; shift < rates_total && !IsStopped(); shift++)
{
//
res = 0;
//
switch (extreme_search)
{
//
case X_ZG_SEARCH_EXTREMUM:
//
if (last_low == 0 && last_high == 0)
{
//
if (highsBuffer[shift] != 0)
{
//
last_high_pos = shift;
last_high = high[shift];
//
extreme_search = X_ZG_SEARCH_VALE;
//
mainBuffer[shift] = last_high;
//
res = 1;
}
//
if (lowsBuffer[shift] != 0)
{
//
last_low_pos = shift;
last_low = low[shift];
//
extreme_search = X_ZG_SEARCH_PEAK;
//
mainBuffer[shift] = last_low;
//
res = 1;
}
}
break;
//
case X_ZG_SEARCH_PEAK:
//
if (lowsBuffer[shift] != 0 && lowsBuffer[shift] < last_low && highsBuffer[shift] == 0)
{
//
mainBuffer[last_low_pos] = 0;
//
last_low_pos = shift;
last_low = lowsBuffer[shift];
//
mainBuffer[shift] = last_low;
//
res = 1;
}
//
if (highsBuffer[shift] != 0 && lowsBuffer[shift] == 0)
{
//
last_high_pos = shift;
last_high = highsBuffer[shift];
//
mainBuffer[shift] = last_high;
//
extreme_search = X_ZG_SEARCH_VALE;
//
res = 1;
}
break;
//
case X_ZG_SEARCH_VALE:
//
if (highsBuffer[shift] != 0 && highsBuffer[shift] > last_high && lowsBuffer[shift] == 0)
{
//
mainBuffer[last_high_pos] = 0;
//
last_high_pos = shift;
last_high = highsBuffer[shift];
//
mainBuffer[shift] = last_high;
}
//
if (lowsBuffer[shift] != 0 && highsBuffer[shift] == 0)
{
//
last_low_pos = shift;
last_low = lowsBuffer[shift];
//
mainBuffer[shift] = last_low;
//
extreme_search = X_ZG_SEARCH_PEAK;
}
break;
}
//
double iZigZagForColor = lastZigZagValue;
double iZigZag = mainBuffer[shift];
if (iZigZag == 0 && lastZigZagValue != 0)
{
arrowBuffer[shift] = lastZigZagValue;
}
else if (iZigZag != 0)
{
//
lastZigZagValue = iZigZag;
arrowBuffer[shift] = lastZigZagValue;
}
//
colorIDX =
lastZigZagValue > iZigZagForColor
? 0
: lastZigZagValue < iZigZagForColor
? 1
: colorIDX;
arrowColorBuffer[shift] = colorIDX;
}
//
return rates_total;
}
//
// TOOLS ...
//
// Search for the index of the highest bar ...
int Highest(
const double &mArray[],
const int mDepth,
const int mStart //
)
{
//
int result = 0;
//
if (mStart < 0)
{
return result;
}
//
double max = mArray[mStart];
result = mStart;
//
// Start searching ...
for (int i = mStart - 1; i > mStart - mDepth && i >= 0; i--)
{
//
if (mArray[i] > max)
{
//
result = i;
max = mArray[i];
}
}
//
// Return index of the highest bar
return result;
}
//
// Search for the index of the lowest bar ...
int Lowest(
const double &mArray[],
const int mDepth,
const int mStart //
)
{
//
int result = 0;
//
if (mStart < 0)
{
return result;
}
//
double min = mArray[mStart];
result = mStart;
//
// Start searching ...
for (int i = mStart - 1; i > mStart - mDepth && i >= 0; i--)
{
//
if (mArray[i] < min)
{
//
result = i;
min = mArray[i];
}
}
//
// Return index of the lowest bar
return result;
}