Files
xMQL5/MQLTestWorkspace/BKPS/BKP/x-saherelm.x121.xtrnd.single.cycle.mq5
T

460 lines
9.7 KiB
Plaintext

///////////////////////////////////////////////////////////////
//
// SaherElm IT Center MQL5 X121_XTRND Trends Indicator
// -----------------------------------------------------------
// Name: X121_XTRND
// Description: Market Trend Detector ...
// Market Analysor ...
//
//
// Maintainer:
// ------------
// Hadi Khazaee Asl (hadi_khazaee_asl@yahoo.com)
//
//////////////////////////////////////////////////////
//
// Global Properties ...
#property copyright "Copyright 2023, SaherElm IT Center"
#property link "https://saherelm.ir"
#property version "1.00"
#property description "SaherElm X121_XTRND Indicator"
#property strict
//
// START Constants ...
//
//
// Indicator Short Name ...
#define ShortName "X121_XTRND"
//
// END Constants ...
//
//
// START Inputs ...
//
//
input group "Makret";
input int cycleLength = 2; // Cycle Length to Check
input int numberOfVerifications = 2; // Number of Verifications
//
input group "Calculation";
input ENUM_SERIESMODE hhMode = MODE_HIGH; // Detect Up
input ENUM_SERIESMODE llMode = MODE_LOW; // Detect Low
//
input group "Presentation";
input bool showConsolidations = false; // Show Consolidations
input bool showTrend = true; // Show Trend
//
// END Inputs ...
//
//
// START Includes and Imports ...
//
//
// Includes Logging Library ...
#include "../Libraries/x-saherelm.log.lib.mq5"
//
// Includes Common Library ...
#include "../Libraries/x-saherelm.common.lib.mq5"
//
// END Includes and Imports ...
//
//
// START Buffers ...
//
//
#property indicator_chart_window
//
#property indicator_buffers 4
#property indicator_plots 4
//
// Higher High ...
#define hhBufferIndex 0
#define hhPlotBufferIndex 0
double hhBuffer[];
#define hhColorBufferIndex 1
double hhColorBuffer[];
//
#property indicator_label1 "X121_XTRND U"
#property indicator_type1 DRAW_COLOR_LINE
#property indicator_color1 CLR_NONE, clrLightGray, clrAqua, clrMagenta
#property indicator_style1 STYLE_DOT
#property indicator_width1 1
//
// Lower Low ...
#define llBufferIndex 2
#define llPlotBufferIndex 1
double llBuffer[];
#define llColorBufferIndex 3
double llColorBuffer[];
//
#property indicator_label2 "X121_XTRND D"
#property indicator_type2 DRAW_COLOR_LINE
#property indicator_color2 CLR_NONE, clrLightGray, clrAqua, clrMagenta
#property indicator_style2 STYLE_DOT
#property indicator_width2 1
//
// 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;
}
//
logTag = ShortName;
//
// because in some cases we may have more than one input for
// calculation and we must prevent any calculation
// untill we pass the biggest input length, here we get max Input length
// and then wait until pass it ...
maxLength = ExtractMaxLengthOfInputs();
//
// Define Index Buffers ...
DefineBuffers();
//
// Set Indicator ShortName ...
SetIndicatorName();
//
// Init Succeed ...
return INIT_SUCCEEDED;
}
//
// DeInitialization ...
void OnDeinit(const int reason)
{
//
// REASON_PROGRAM 0 The EA has stopped working calling the ExpertRemove() function
// REASON_REMOVE 1 Program removed from a chart
// REASON_RECOMPILE 2 Program recompiled
// REASON_CHARTCHANGE 3 A symbol or a chart period is changed
// REASON_CHARTCLOSE 4 Chart closed
// REASON_PARAMETERS 5 Inputs changed by a user
// REASON_ACCOUNT 6 Another account has been activated or reconnection to the trade server has occurred due to changes in the account settings
// REASON_TEMPLATE 7 Another chart template applied
// REASON_INITFAILED 8 The OnInit() handler returned a non-zero value
// REASON_CLOSE 9 Terminal closed
}
//
// Calculations ...
//
// Calculating what we want ...
int OnCalculate(
//
// total Candles on chart ...
const int rates_total,
//
// total calculated Candles on charts ...
const int prev_calculated,
//
// history of Candles Open Time ...
const datetime &time[],
//
// history of Candles Open Price ...
const double &open[],
//
// history of Candles High Price ...
const double &high[],
//
// history of Candles Low Price ...
const double &low[],
//
// history of Candles Close Price ...
const double &close[],
//
// history of Tick Volumes on Candle ...
const long &tick_volume[],
//
// history of Trade Volumes ...
const long &volume[],
//
// history of Candles Spread Price ...
const int &spread[])
{
//
// Prepare Buffers ...
ArraySetAsSeries(time, true);
ArraySetAsSeries(open, true);
ArraySetAsSeries(high, true);
ArraySetAsSeries(low, true);
ArraySetAsSeries(close, true);
ArraySetAsSeries(tick_volume, true);
ArraySetAsSeries(volume, true);
ArraySetAsSeries(spread, true);
//
// this counts Available Bars ...
int limit;
//
limit = (prev_calculated > rates_total || prev_calculated <= 0) ? rates_total : (rates_total - prev_calculated) + 1;
//
// Main Loop ...
for (int i = limit - 1; i >= 0 && !IsStopped(); i--)
{
CalculateBuffers(i);
}
//
return rates_total;
}
//
// END Event Handlers ...
//
//
// START Functions ...
//
//
// Validate Input Args for Initialization ...
bool ValidateInputs()
{
//
bool result =
//
(cycleLength > 0 &&
numberOfVerifications > 0)
//
;
//
return result;
}
//
// Retrieve all Exists Input Max Length ...
// use for Start Of Drawing ...
int ExtractMaxLengthOfInputs()
{
//
int result = 0;
//
// Peaks and Vales ...
result = MathMax(
cycleLength,
numberOfVerifications);
//
return result;
}
//
// Define Indexes and Styles ...
void DefineBuffers()
{
//
ENUM_DRAW_TYPE drawType = showTrend ? DRAW_COLOR_LINE : DRAW_NONE;
//
// HH Buffer ...
//
// SERIES ...
ArraySetAsSeries(hhBuffer, true);
SetIndexBuffer(hhBufferIndex, hhBuffer, INDICATOR_DATA);
//
// PLOT Config ...
PlotIndexSetInteger(hhPlotBufferIndex, PLOT_SHOW_DATA, showTrend);
PlotIndexSetInteger(hhPlotBufferIndex, PLOT_DRAW_BEGIN, maxLength);
PlotIndexSetInteger(hhPlotBufferIndex, PLOT_DRAW_TYPE, drawType);
//
// COLOR Buffer ...
ArraySetAsSeries(hhColorBuffer, true);
SetIndexBuffer(hhColorBufferIndex, hhColorBuffer, INDICATOR_COLOR_INDEX);
//
// LL Buffer ...
//
// SERIES ...
ArraySetAsSeries(llBuffer, true);
SetIndexBuffer(llBufferIndex, llBuffer, INDICATOR_DATA);
//
// PLOT Config ...
PlotIndexSetInteger(llPlotBufferIndex, PLOT_SHOW_DATA, showTrend);
PlotIndexSetInteger(llPlotBufferIndex, PLOT_DRAW_TYPE, drawType);
PlotIndexSetInteger(llPlotBufferIndex, PLOT_DRAW_BEGIN, maxLength);
//
// COLOR Buffer ...
ArraySetAsSeries(llColorBuffer, true);
SetIndexBuffer(llColorBufferIndex, llColorBuffer, INDICATOR_COLOR_INDEX);
}
//
// Set Indicator Short Name and also we can define Buffers Labels ...
void SetIndicatorName()
{
//
string indicatorShortName = "";
StringConcatenate(indicatorShortName, ShortName, "");
//
IndicatorSetString(INDICATOR_SHORTNAME, indicatorShortName);
}
//
// Calculate Buffers ...
void CalculateBuffers(
int bar_index // Selected Bar Index
)
{
//
// HIGHER Highs ...
double hh = GetHighestHigh(
_Symbol,
_Period,
hhMode,
cycleLength,
bar_index);
//
hhBuffer[bar_index] = hh;
//
// LOWER Lows ...
double ll = GetLowestLow(
_Symbol,
_Period,
llMode,
cycleLength,
bar_index);
//
llBuffer[bar_index] = ll;
//
// Set Default Color ...
// in Consolidations use index 0 for CLR_NONE ...
double hhColorIDX = showConsolidations ? 1 : 0;
double llColorIDX = showConsolidations ? 1 : 0;
//
// Calculate Cycle Changes ...
//
int cycleCLength = cycleLength * numberOfVerifications;
bool isCycleCLengthPassed = MathAbs(ArraySize(hhBuffer) - bar_index) > cycleCLength + 1;
//
if (isCycleCLengthPassed)
{
//
bool isLowestLowUp = true; // ll > llBuffer[bar_index + 1];
bool isLowestLowDown = true; // ll < llBuffer[bar_index + 1];
bool isHighsestHighUp = true; // hh > hhBuffer[bar_index + 1];
bool isHighsestHighDown = true; // hh < hhBuffer[bar_index + 1];
for (int i = bar_index; i < bar_index + cycleCLength; i++)
{
//
double iHH = hhBuffer[i];
double iLL = llBuffer[i];
//
if (hh < iHH)
{
isHighsestHighUp = false;
}
else if (hh > iHH)
{
isHighsestHighDown = false;
}
//
if (ll < iLL)
{
isLowestLowUp = false;
}
else if (ll > iLL)
{
isLowestLowDown = false;
}
}
//
// Detect Trends and Change Colors ...
//
if (isLowestLowUp &&
isHighsestHighUp)
{
//
hhColorIDX = 2;
llColorIDX = 2;
}
//
if (isLowestLowDown &&
isHighsestHighDown)
{
//
hhColorIDX = 3;
llColorIDX = 3;
}
}
//
hhColorBuffer[bar_index] = hhColorIDX;
llColorBuffer[bar_index] = llColorIDX;
}
//
// END Functions ...
//