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

776 lines
15 KiB
Plaintext

///////////////////////////////////////////////////////
//
// SaherElm IT Center MQL5 Indicator
// ---------------------------------------------
// Name: X121_XCHNL
// Description: XCHL Channel Indicator ...
//
//
// 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_XCHNL Indicator"
#property strict
//
// START Constants ...
//
//
#define ShortName "X121_XCHNL"
//
// END Constants ...
//
//
// START Includes and Imports ...
//
//
// Includes Common Library ...
#include "../Libraries/x-saherelm.common.lib.mq5"
//
// Includes Draw Library ...
#include "../Libraries/x-saherelm.draw.lib.mq5"
//
// END Includes and Imports ...
//
//
// START XSCHULL Class Definition ...
//
//
// END XSCHULL Class Definition ...
//
//
// 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 showTrend = false; // Show Trend
input bool showSRs = false; // Show Support and Resistance
//
// END Inputs ...
//
//
// START Buffers ...
//
//
// PEKSANDVALES ...
#property indicator_chart_window
//
#property indicator_buffers 8
#property indicator_plots 0
#define sHHBufferIndex 0
#define sLLBufferIndex 1
#define mHHBufferIndex 2
#define mLLBufferIndex 3
#define lHHBufferIndex 4
#define lLLBufferIndex 5
#define hHHBufferIndex 6
#define hLLBufferIndex 7
double sHHBuffer[];
double sLLBuffer[];
double mHHBuffer[];
double mLLBuffer[];
double lHHBuffer[];
double lLLBuffer[];
double hHHBuffer[];
double hLLBuffer[];
//
// END Buffers ...
//
//
// START Global Definitions: Variables, Properties and etc ...
//
//
int maxLength;
//
// END Global Definitions: Variables, Properties and etc ...
//
//
// START Event Handlers ...
//
//
// Initialization ...
int OnInit()
{
//
// Validate Inputs ...
if (!ValidateInputs())
{
return INIT_PARAMETERS_INCORRECT;
}
//
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();
//
// Make an Instance of Hull Class ...
//
// 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 mPeriod 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);
}
//
// 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[])
{
//
// this counts Available Bars ...
int limit;
//
limit = prev_calculated - 1;
if (limit < 0)
{
limit = 0;
}
//
// Main Loop ...
for (int i = limit; i < rates_total && !IsStopped(); i++)
{
CalculateBuffers(i);
}
//
return rates_total;
}
//
// END Event Handlers ...
//
//
// START Functions ...
//
//
// Validate Input Args for Initialization ...
bool ValidateInputs()
{
//
bool result = true;
//
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;
}
//
// Set Indicator Short Name and also we can define Buffers Labels ...
void SetIndicatorName()
{
//
IndicatorSetString(INDICATOR_SHORTNAME, ShortName);
}
//
// Define Indexes and Styles ...
void DefineBuffers()
{
//
ArraySetAsSeries(sHHBuffer, true);
SetIndexBuffer(sHHBufferIndex, sHHBuffer, INDICATOR_CALCULATIONS);
//
ArraySetAsSeries(sLLBuffer, true);
SetIndexBuffer(sLLBufferIndex, sLLBuffer, INDICATOR_CALCULATIONS);
//
ArraySetAsSeries(mHHBuffer, true);
SetIndexBuffer(mHHBufferIndex, mHHBuffer, INDICATOR_CALCULATIONS);
//
ArraySetAsSeries(mLLBuffer, true);
SetIndexBuffer(mLLBufferIndex, mLLBuffer, INDICATOR_CALCULATIONS);
//
ArraySetAsSeries(lHHBuffer, true);
SetIndexBuffer(lHHBufferIndex, lHHBuffer, INDICATOR_CALCULATIONS);
//
ArraySetAsSeries(lLLBuffer, true);
SetIndexBuffer(lLLBufferIndex, lLLBuffer, INDICATOR_CALCULATIONS);
//
ArraySetAsSeries(hHHBuffer, true);
SetIndexBuffer(hHHBufferIndex, hHHBuffer, INDICATOR_CALCULATIONS);
//
ArraySetAsSeries(hLLBuffer, true);
SetIndexBuffer(hLLBufferIndex, hLLBuffer, INDICATOR_CALCULATIONS);
}
//
// Calculate Buffers ...
void CalculateBuffers(
int bar_index // Selected Bar Index
)
{
//
// Calculate ...
//
// 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;
//
// 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);
}