Files
xMQL5/BKPS/14030823/BKPs/14030802-X121/Indicators/x-saherelm.xchvlm.mq5
T
2024-11-15 10:50:11 +03:30

814 lines
17 KiB
Plaintext

///////////////////////////////////////////////////////
// SaherElm IT Center MQL5 Indicator
// -------------------------------------------
// Name: XCHVLM
// Description: XCHVLM Cycles ...
//
// 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 XCHVLM Indicator"
#property strict
//
#define ShortName "XCHVLM"
//
// Includes Common Library ...
#include <Math/Stat/Normal.mqh>
#include "../Libraries/x-saherelm.common.lib.mq5"
//
// Inputs ...
//
input group "Market";
input int length = 14; // Length
//
input group "Cycles";
//
input group "Short";
input ENUM_X_PERIOD_METHOD scMethod = X_PERIOD_AUTO; // How to Find Period
input ENUM_TIMEFRAMES scPeriod = NULL; // Time Period
//
input group "Medium";
input ENUM_X_PERIOD_METHOD mcMethod = X_PERIOD_AUTO; // How to Find Period
input ENUM_TIMEFRAMES mcPeriod = NULL; // Time Period
//
input group "Long";
input ENUM_X_PERIOD_METHOD lcMethod = X_PERIOD_AUTO; // How to Find Period
input ENUM_TIMEFRAMES lcPeriod = NULL; // Time Period
//
input group "Hind";
input ENUM_X_PERIOD_METHOD hcMethod = X_PERIOD_AUTO; // How to Find Period
input ENUM_TIMEFRAMES hcPeriod = NULL; // Time Period
//
// Presentation ...
input group "Presentation";
//
input bool showCurrent = true; // Show Current Cycle
input bool showShort = true; // Show Short Cycle
input bool showMedium = true; // Show Medium Cycle
input bool showLong = true; // Show Long Cycle
input bool showHind = true; // Show Hind Cycle
//
// Buffers ...
//
#define hideColorIDX 0
#define bullishColorIDX 1
#define bearishColorIDX 2
//
#property indicator_chart_window
//
#property indicator_buffers 10
#property indicator_plots 0
//
// Current ...
//
#define cBufferIndex 0
double cBuffer[];
//
#define cColorBufferIndex 1
double cColorBuffer[];
//
#define cPlotBufferIndex 0
#property indicator_label1 "XCHVLM C"
#property indicator_style1 STYLE_SOLID
#property indicator_type1 DRAW_COLOR_HISTOGRAM
#property indicator_color1 CLR_NONE, clrLime, clrRed
//
// Short ...
//
#define sBufferIndex 2
double sBuffer[];
//
#define sColorBufferIndex 3
double sColorBuffer[];
//
#define sPlotBufferIndex 1
#property indicator_label2 "XCHVLM S"
#property indicator_style2 STYLE_SOLID
#property indicator_type2 DRAW_COLOR_HISTOGRAM
#property indicator_color2 CLR_NONE, clrLime, clrRed
//
// Medium ...
//
#define mBufferIndex 4
double mBuffer[];
//
#define mColorBufferIndex 5
double mColorBuffer[];
//
#define mPlotBufferIndex 2
#property indicator_label3 "XCHVLM M"
#property indicator_style3 STYLE_SOLID
#property indicator_type3 DRAW_COLOR_HISTOGRAM
#property indicator_color3 CLR_NONE, clrLime, clrRed
//
// Long ...
//
#define lBufferIndex 6
double lBuffer[];
//
#define lColorBufferIndex 7
double lColorBuffer[];
//
#define lPlotBufferIndex 3
#property indicator_label4 "XCHVLM L"
#property indicator_style4 STYLE_SOLID
#property indicator_type4 DRAW_COLOR_HISTOGRAM
#property indicator_color4 CLR_NONE, clrLime, clrRed
//
// Hind ...
//
#define hBufferIndex 8
double hBuffer[];
//
#define hColorBufferIndex 9
double hColorBuffer[];
//
#define hPlotBufferIndex 4
#property indicator_label5 "XCHVLM H"
#property indicator_style5 STYLE_SOLID
#property indicator_type5 DRAW_COLOR_HISTOGRAM
#property indicator_color5 CLR_NONE, clrLime, clrRed
//
// Variables, Properties and etc ...
//
int maxLength;
//
// Current ...
int mCMarketLength = length;
double cVolumes[];
double cDistances[];
//
// Short ...
int mSCycleLength = 0;
int mSMarketLength = 0;
ENUM_TIMEFRAMES mSCPeriod = NULL;
double sVolumes[];
double sDistances[];
//
// Medium ...
int mMCycleLength = 0;
int mMMarketLength = 0;
ENUM_TIMEFRAMES mMCPeriod = NULL;
double mVolumes[];
double mDistances[];
//
// Long ...
int mLCycleLength = 0;
int mLMarketLength = 0;
ENUM_TIMEFRAMES mLCPeriod = NULL;
double lVolumes[];
double lDistances[];
//
// Hind ...
int mHCycleLength = 0;
int mHMarketLength = 0;
ENUM_TIMEFRAMES mHCPeriod = NULL;
double hVolumes[];
double hDistances[];
//
// Event Handlers ...
//
// Initialization ...
int OnInit()
{
//
// Validate Inputs ...
if (!ValidateInputs())
{
return INIT_PARAMETERS_INCORRECT;
}
//
if (!InitMarketCycles())
{
return INIT_PARAMETERS_INCORRECT;
}
//
// because in some cases we may have more than one input for
// calculation and we must prevent any calculation
// untill we pass the biggest input length, here we get max Input length
// and then wait until pass it ...
maxLength = ExtractMaxLengthOfInputs();
//
// Current ...
ArrayResize(cVolumes, maxLength, 0);
ArrayResize(cDistances, maxLength, 0);
//
// Short ...
ArrayResize(sVolumes, maxLength, 0);
ArrayResize(sDistances, maxLength, 0);
//
// Medium ...
ArrayResize(mVolumes, maxLength, 0);
ArrayResize(mDistances, maxLength, 0);
//
// Long ...
ArrayResize(lVolumes, maxLength, 0);
ArrayResize(lDistances, maxLength, 0);
//
// Hind ...
ArrayResize(hVolumes, maxLength, 0);
ArrayResize(hDistances, maxLength, 0);
//
// 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
//
Clean(cVolumes);
Clean(cDistances);
//
Clean(sVolumes);
Clean(sDistances);
//
Clean(mVolumes);
Clean(mDistances);
//
Clean(lVolumes);
Clean(lDistances);
//
Clean(hVolumes);
Clean(hDistances);
}
//
// Calculating what we want ...
int OnCalculate(
const int rates_total, // Total Bars on Chart ...
const int prev_calculated, // Total Calculated Bars on Charts ...
const datetime &time[], // History of Open Time ...
const double &open[], // History of Open Price ...
const double &high[], // History of High Price ...
const double &low[], // History of Low Price ...
const double &close[], // History of Close Price ...
const long &tick_volume[], // History of Tick Volumes on Bar ...
const long &volume[], // History of Trade Volumes ...
const int &spread[] // History of Spread Price ...
)
{
//
static int i = maxLength * 2;
for (i; i < rates_total; i++)
{
//
CalculateBuffers(
i,
prev_calculated,
rates_total,
//
open,
high,
close,
low,
tick_volume //
);
}
//
i = rates_total - 1;
//
return rates_total;
}
//
// Functions ...
//
// Validate Input Args for Initialization ...
bool ValidateInputs()
{
//
bool result = false;
//
result =
//
length >= 2 &&
//
(IsValid(scMethod, scPeriod) &&
IsValid(mcMethod, mcPeriod) &&
IsValid(lcMethod, lcPeriod) &&
IsValid(hcMethod, hcPeriod))
//
;
//
return result;
}
//
// Initialize Market Cycles ...
bool InitMarketCycles()
{
//
bool result = false;
//
// Current Cycle Initialization ...
//
int cPeriodSeconds = PeriodSeconds(_Period);
//
// Short Cycle Initialization ...
//
// Find Cycle Period ...
if (scMethod == X_PERIOD_AUTO)
{
//
// Select Period ...
mSCPeriod = GetCyclePeriod(
X_MARKET_CYCLE_SHORT,
_Period //
);
}
else
{
mSCPeriod = scPeriod;
}
//
result = IsValid(mSCPeriod);
if (!result)
{
return result;
}
//
mSCycleLength = PeriodSeconds(mSCPeriod) / cPeriodSeconds;
mSMarketLength = mSCycleLength * mCMarketLength;
result = mSCycleLength > 0;
if (!result)
{
return result;
}
//
// Medium Cycle Initialization ...
//
// Find Cycle Period ...
if (mcMethod == X_PERIOD_AUTO)
{
//
// Select Period ...
mMCPeriod = GetCyclePeriod(
X_MARKET_CYCLE_MEDIUM,
_Period //
);
}
else
{
mMCPeriod = mcPeriod;
}
//
result = IsValid(mMCPeriod);
if (!result)
{
return result;
}
//
mMCycleLength = PeriodSeconds(mMCPeriod) / cPeriodSeconds;
mMMarketLength = mMCycleLength * mCMarketLength;
result = mMCycleLength > 0;
if (!result)
{
return result;
}
//
// Long Cycle Initialization ...
//
// Find Cycle Period ...
if (lcMethod == X_PERIOD_AUTO)
{
//
// Select Period ...
mLCPeriod = GetCyclePeriod(
X_MARKET_CYCLE_LONG,
_Period //
);
}
else
{
mLCPeriod = lcPeriod;
}
//
result = IsValid(mLCPeriod);
if (!result)
{
return result;
}
//
mLCycleLength = PeriodSeconds(mLCPeriod) / cPeriodSeconds;
mLMarketLength = mLCycleLength * mCMarketLength;
result = mLCycleLength > 0;
if (!result)
{
return result;
}
//
// Hind Cycle Initialization ...
//
// Find Cycle Period ...
if (hcMethod == X_PERIOD_AUTO)
{
//
// Select Period ...
mHCPeriod = GetCyclePeriod(
X_MARKET_CYCLE_HIND,
_Period //
);
}
else
{
mHCPeriod = hcPeriod;
}
//
result = IsValid(mHCPeriod);
if (!result)
{
return result;
}
//
mHCycleLength = PeriodSeconds(mHCPeriod) / cPeriodSeconds;
mHMarketLength = mHCycleLength * mCMarketLength;
result = mHCycleLength > 0;
if (!result)
{
return result;
}
//
return result;
}
//
// Retrieve all Exists Input Max Length ...
// use for Start Of Drawing ...
int ExtractMaxLengthOfInputs()
{
//
int result = 0;
//
result = MathMax(mCMarketLength, mSMarketLength);
result = MathMax(result, mMMarketLength);
result = MathMax(result, mLMarketLength);
result = MathMax(result, mHMarketLength);
//
return result;
}
//
// Define Indexes and Styles ...
void DefineBuffers()
{
//
// Current ...
//
// ENUM_DRAW_TYPE cDrawType = showCurrent ? DRAW_COLOR_HISTOGRAM : DRAW_NONE;
// PlotIndexSetInteger(cPlotBufferIndex, PLOT_DRAW_TYPE, cDrawType);
// PlotIndexSetInteger(cPlotBufferIndex, PLOT_SHOW_DATA, showCurrent);
// SetIndexBuffer(cColorBufferIndex, cColorBuffer, INDICATOR_COLOR_INDEX);
SetIndexBuffer(cBufferIndex, cBuffer, INDICATOR_CALCULATIONS);
SetIndexBuffer(cColorBufferIndex, cColorBuffer, INDICATOR_CALCULATIONS);
//
// Short ...
//
// ENUM_DRAW_TYPE sDrawType = showShort ? DRAW_COLOR_HISTOGRAM : DRAW_NONE;
// PlotIndexSetInteger(sPlotBufferIndex, PLOT_DRAW_TYPE, sDrawType);
// PlotIndexSetInteger(sPlotBufferIndex, PLOT_SHOW_DATA, showShort);
// SetIndexBuffer(sColorBufferIndex, sColorBuffer, INDICATOR_COLOR_INDEX);
SetIndexBuffer(sBufferIndex, sBuffer, INDICATOR_CALCULATIONS);
SetIndexBuffer(sColorBufferIndex, sColorBuffer, INDICATOR_CALCULATIONS);
//
// Medium ...
//
// ENUM_DRAW_TYPE mDrawType = showMedium ? DRAW_COLOR_HISTOGRAM : DRAW_NONE;
// PlotIndexSetInteger(mPlotBufferIndex, PLOT_DRAW_TYPE, mDrawType);
// PlotIndexSetInteger(mPlotBufferIndex, PLOT_SHOW_DATA, showMedium);
// SetIndexBuffer(mColorBufferIndex, mColorBuffer, INDICATOR_COLOR_INDEX);
SetIndexBuffer(mBufferIndex, mBuffer, INDICATOR_CALCULATIONS);
SetIndexBuffer(mColorBufferIndex, mColorBuffer, INDICATOR_CALCULATIONS);
//
// Long ...
//
// ENUM_DRAW_TYPE lDrawType = showLong ? DRAW_COLOR_HISTOGRAM : DRAW_NONE;
// PlotIndexSetInteger(lPlotBufferIndex, PLOT_DRAW_TYPE, lDrawType);
// PlotIndexSetInteger(lPlotBufferIndex, PLOT_SHOW_DATA, showLong);
// SetIndexBuffer(lColorBufferIndex, lColorBuffer, INDICATOR_COLOR_INDEX);
SetIndexBuffer(lBufferIndex, lBuffer, INDICATOR_CALCULATIONS);
SetIndexBuffer(lColorBufferIndex, lColorBuffer, INDICATOR_CALCULATIONS);
//
// Hind ...
//
// ENUM_DRAW_TYPE hDrawType = showHind ? DRAW_COLOR_HISTOGRAM : DRAW_NONE;
// PlotIndexSetInteger(hPlotBufferIndex, PLOT_DRAW_TYPE, hDrawType);
// PlotIndexSetInteger(hPlotBufferIndex, PLOT_SHOW_DATA, showHind);
// SetIndexBuffer(hColorBufferIndex, hColorBuffer, INDICATOR_COLOR_INDEX);
SetIndexBuffer(hBufferIndex, hBuffer, INDICATOR_CALCULATIONS);
SetIndexBuffer(hColorBufferIndex, hColorBuffer, INDICATOR_CALCULATIONS);
}
//
// Set Indicator Short Name and also we can define Buffers Labels ...
void SetIndicatorName()
{
IndicatorSetString(INDICATOR_SHORTNAME, ShortName);
}
//
// Calculations ...
//
// Calculate Buffers ...
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[] //
)
{
//
CalculateCycles(
bar_index,
prevCalculated,
ratesTotal,
open,
high,
close,
low,
tickVolume //
);
}
//
void CalculateCycles(
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[] //
)
{
//
// CURRENT ...
CalculateCycle(
bar_index,
prevCalculated,
ratesTotal,
open,
high,
close,
low,
tickVolume,
//
mCMarketLength,
showCurrent,
cVolumes,
cDistances,
cBuffer,
cColorBuffer //
);
//
// SHORT ...
CalculateCycle(
bar_index,
prevCalculated,
ratesTotal,
open,
high,
close,
low,
tickVolume,
//
mSMarketLength,
showShort,
sVolumes,
sDistances,
sBuffer,
sColorBuffer //
);
//
// MEDIUM ...
CalculateCycle(
bar_index,
prevCalculated,
ratesTotal,
open,
high,
close,
low,
tickVolume,
//
mMMarketLength,
showMedium,
mVolumes,
mDistances,
mBuffer,
mColorBuffer //
);
//
// LONG ...
CalculateCycle(
bar_index,
prevCalculated,
ratesTotal,
open,
high,
close,
low,
tickVolume,
//
mLMarketLength,
showLong,
lVolumes,
lDistances,
lBuffer,
lColorBuffer //
);
//
// HIND ...
CalculateCycle(
bar_index,
prevCalculated,
ratesTotal,
open,
high,
close,
low,
tickVolume,
//
mHMarketLength,
showHind,
hVolumes,
hDistances,
hBuffer,
hColorBuffer //
);
}
//
void CalculateCycle(
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 &_volumes[],
double &_distances[],
double &_buffer[],
double &_colorBuffer[] //
)
{
//
for (int x = 0; x < _length; x++)
{
//
_volumes[x] = (double)tickVolume[bar_index - x];
_distances[x] =
(close[bar_index - x] > open[bar_index - x])
? high[bar_index - x] - low[bar_index - x]
: low[bar_index - x] - high[bar_index - x];
}
//
double iValue = MathMean(_volumes) * MathMean(_distances);
//
_buffer[bar_index] = iValue;
_colorBuffer[bar_index] = iValue > 0
? _show
? bullishColorIDX
: hideColorIDX
: _show
? bearishColorIDX
: hideColorIDX;
}
//