1254 lines
27 KiB
Plaintext
1254 lines
27 KiB
Plaintext
/////////////////////////////////////////////////////
|
|
//
|
|
// SaherElm IT Center MQL5 Indicator
|
|
// --------------------------------------------------
|
|
// Name: X121_XTRND
|
|
// Description: Market Trend Detector ...
|
|
//
|
|
//
|
|
// 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"
|
|
|
|
//
|
|
// XPERIOD Calculation Method ...
|
|
// USED For Multiple Time Frames ...
|
|
enum ENUM_X_PERIOD_METHOD
|
|
{
|
|
//
|
|
X_PERIOD_NOTHING,
|
|
//
|
|
X_PERIOD_AUTO,
|
|
//
|
|
X_PERIOD_MANUALLY
|
|
};
|
|
|
|
//
|
|
// 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 "Cycles";
|
|
|
|
//
|
|
input group "Level 1";
|
|
input ENUM_X_PERIOD_METHOD l1Method = X_PERIOD_AUTO; // How to Find Period
|
|
input ENUM_TIMEFRAMES l1Period = NULL; // Time Period
|
|
|
|
//
|
|
input group "Level 2";
|
|
input ENUM_X_PERIOD_METHOD l2Method = X_PERIOD_AUTO; // How to Find Period
|
|
input ENUM_TIMEFRAMES l2Period = NULL; // Time Period
|
|
|
|
//
|
|
input group "Level 3";
|
|
input ENUM_X_PERIOD_METHOD l3Method = X_PERIOD_AUTO; // How to Find Period
|
|
input ENUM_TIMEFRAMES l3Period = NULL; // Time Period
|
|
|
|
//
|
|
input group "Presentation";
|
|
input bool showConsolidations = false; // Show Consolidations
|
|
input bool showTrend = true; // Show Trend
|
|
input bool showL1Trend = false; // Show Cycle 1 Trend
|
|
input bool showL2Trend = false; // Show Cycle 2 Trend
|
|
input bool showL3Trend = false; // Show Cycle 3 Trend
|
|
|
|
//
|
|
// END Inputs ...
|
|
//
|
|
|
|
//
|
|
// START Includes and Imports ...
|
|
//
|
|
|
|
//
|
|
// Includes Common Library ...
|
|
#include "../Libraries/x-saherelm.common.lib.mq5"
|
|
|
|
//
|
|
// END Includes and Imports ...
|
|
//
|
|
|
|
//
|
|
// START Buffers ...
|
|
//
|
|
|
|
//
|
|
#property indicator_chart_window
|
|
|
|
//
|
|
#property indicator_buffers 24
|
|
#property indicator_plots 16
|
|
|
|
//
|
|
// 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
|
|
|
|
//
|
|
// CYCLE 1 ...
|
|
|
|
//
|
|
// Higher High ...
|
|
#define l1hhBufferIndex 4
|
|
#define l1hhPlotBufferIndex 2
|
|
double l1hhBuffer[];
|
|
|
|
#define l1hhColorBufferIndex 5
|
|
double l1hhColorBuffer[];
|
|
|
|
//
|
|
#property indicator_label3 "X121_XTRND C1 U"
|
|
#property indicator_type3 DRAW_COLOR_LINE
|
|
#property indicator_color3 CLR_NONE, clrLightGray, clrAqua, clrMagenta
|
|
#property indicator_style3 STYLE_DOT
|
|
#property indicator_width3 1
|
|
|
|
//
|
|
// Lower Low ...
|
|
#define l1llBufferIndex 6
|
|
#define l1llPlotBufferIndex 3
|
|
double l1llBuffer[];
|
|
|
|
#define l1llColorBufferIndex 7
|
|
double l1llColorBuffer[];
|
|
|
|
//
|
|
#property indicator_label4 "X121_XTRND C1 D"
|
|
#property indicator_type4 DRAW_COLOR_LINE
|
|
#property indicator_color4 CLR_NONE, clrLightGray, clrAqua, clrMagenta
|
|
#property indicator_style4 STYLE_DOT
|
|
#property indicator_width4 1
|
|
|
|
//
|
|
// CYCLE 2 ...
|
|
|
|
//
|
|
// Higher High ...
|
|
#define l2hhBufferIndex 8
|
|
#define l2hhPlotBufferIndex 4
|
|
double l2hhBuffer[];
|
|
|
|
#define l2hhColorBufferIndex 9
|
|
double l2hhColorBuffer[];
|
|
|
|
//
|
|
#property indicator_label5 "X121_XTRND C2 U"
|
|
#property indicator_type5 DRAW_COLOR_LINE
|
|
#property indicator_color5 CLR_NONE, clrLightGray, clrAqua, clrMagenta
|
|
#property indicator_style5 STYLE_DOT
|
|
#property indicator_width5 1
|
|
|
|
//
|
|
// Lower Low ...
|
|
#define l2llBufferIndex 10
|
|
#define l2llPlotBufferIndex 5
|
|
double l2llBuffer[];
|
|
|
|
#define l2llColorBufferIndex 11
|
|
double l2llColorBuffer[];
|
|
|
|
//
|
|
#property indicator_label6 "X121_XTRND C2 D"
|
|
#property indicator_type6 DRAW_COLOR_LINE
|
|
#property indicator_color6 CLR_NONE, clrLightGray, clrAqua, clrMagenta
|
|
#property indicator_style6 STYLE_DOT
|
|
#property indicator_width6 1
|
|
|
|
//
|
|
// CYCLE 3 ...
|
|
|
|
//
|
|
// Higher High...
|
|
#define l3hhBufferIndex 12
|
|
#define l3hhPlotBufferIndex 6
|
|
double l3hhBuffer[];
|
|
|
|
#define l3hhColorBufferIndex 13
|
|
double l3hhColorBuffer[];
|
|
|
|
//
|
|
#property indicator_label7 "X121_XTRND C3 U"
|
|
#property indicator_type7 DRAW_COLOR_LINE
|
|
#property indicator_color7 CLR_NONE, clrLightGray, clrAqua, clrMagenta
|
|
#property indicator_style7 STYLE_DOT
|
|
#property indicator_width7 1
|
|
|
|
//
|
|
// Lower Low ...
|
|
#define l3llBufferIndex 14
|
|
#define l3llPlotBufferIndex 7
|
|
double l3llBuffer[];
|
|
|
|
#define l3llColorBufferIndex 15
|
|
double l3llColorBuffer[];
|
|
|
|
//
|
|
#property indicator_label8 "X121_XTRND C3 D"
|
|
#property indicator_type8 DRAW_COLOR_LINE
|
|
#property indicator_color8 CLR_NONE, clrLightGray, clrAqua, clrMagenta
|
|
#property indicator_style8 STYLE_DOT
|
|
#property indicator_width8 1
|
|
|
|
//
|
|
// DATA Buffers ...
|
|
|
|
#define hhStateBufferIndex 16
|
|
#define llStateBufferIndex 17
|
|
double hhStateBuffer[];
|
|
double llStateBuffer[];
|
|
|
|
#define l1HHStateBufferIndex 18
|
|
#define l1LLStateBufferIndex 19
|
|
double l1HHStateBuffer[];
|
|
double l1LLStateBuffer[];
|
|
|
|
#define l2HHStateBufferIndex 20
|
|
#define l2LLStateBufferIndex 21
|
|
double l2HHStateBuffer[];
|
|
double l2LLStateBuffer[];
|
|
|
|
#define l3HHStateBufferIndex 22
|
|
#define l3LLStateBufferIndex 23
|
|
double l3HHStateBuffer[];
|
|
double l3LLStateBuffer[];
|
|
|
|
//
|
|
// END Buffers ...
|
|
//
|
|
|
|
//
|
|
// START Global Definitions: Variables, Properties and etc ...
|
|
//
|
|
|
|
//
|
|
int maxLength;
|
|
|
|
//
|
|
int currentPeriodSecconds = 0;
|
|
|
|
//
|
|
// CYCLE 1 ...
|
|
int mL1Candles;
|
|
int mL1PeriodSecconds = 0;
|
|
int mLastL1StartBarIndex = 0;
|
|
datetime mLastL1StartTime = NULL;
|
|
ENUM_TIMEFRAMES mL1Period = NULL;
|
|
|
|
//
|
|
// CYCLE 2 ...
|
|
int mL2Candles;
|
|
int mL2PeriodSecconds = 0;
|
|
int mLastL2StartBarIndex = 0;
|
|
datetime mLastL2StartTime = NULL;
|
|
ENUM_TIMEFRAMES mL2Period = NULL;
|
|
|
|
//
|
|
// CYCLE 3 ...
|
|
int mL3Candles;
|
|
int mL3PeriodSecconds = 0;
|
|
int mLastL3StartBarIndex = 0;
|
|
datetime mLastL3StartTime = NULL;
|
|
ENUM_TIMEFRAMES mL3Period = NULL;
|
|
|
|
//
|
|
// END Global Definitions: Variables, Properties and etc ...
|
|
//
|
|
|
|
//
|
|
// START Event Handlers ...
|
|
//
|
|
|
|
//
|
|
// Initialization ...
|
|
int OnInit()
|
|
{
|
|
//
|
|
// Validate Inputs ...
|
|
if (!ValidateInputs())
|
|
{
|
|
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();
|
|
|
|
//
|
|
// Define Index Buffers ...
|
|
DefineBuffers();
|
|
|
|
//
|
|
// Set Indicator ShortName ...
|
|
SetIndicatorName();
|
|
|
|
//
|
|
// Initial Periods ...
|
|
InitialCycles();
|
|
|
|
//
|
|
// 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[])
|
|
{
|
|
//
|
|
// 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;
|
|
}
|
|
|
|
//
|
|
// Initial Multi Time Frame Cycles Periods ...
|
|
void InitialCycles()
|
|
{
|
|
//
|
|
// Calculate Current Period Secconds ...
|
|
currentPeriodSecconds = PeriodSeconds(_Period);
|
|
|
|
//
|
|
// Nearest ...
|
|
// Calculate Cycle 1 Requirements ...
|
|
if (mL1Period == NULL)
|
|
{
|
|
//
|
|
if (l1Method == X_PERIOD_AUTO)
|
|
{
|
|
mL1Period = GetNearestTimeFrame(_Period);
|
|
}
|
|
else
|
|
{
|
|
mL1Period = l1Period;
|
|
}
|
|
|
|
//
|
|
mL1PeriodSecconds = PeriodSeconds(mL1Period);
|
|
|
|
//
|
|
mL1Candles = mL1PeriodSecconds / currentPeriodSecconds;
|
|
}
|
|
|
|
//
|
|
// Mediest ...
|
|
// Calculate Cycle 2 Requirements ...
|
|
if (mL2Period == NULL)
|
|
{
|
|
//
|
|
if (l2Method == X_PERIOD_AUTO)
|
|
{
|
|
mL2Period = GetMediestTimeFrame(_Period);
|
|
}
|
|
else
|
|
{
|
|
mL2Period = l2Period;
|
|
}
|
|
|
|
//
|
|
mL2PeriodSecconds = PeriodSeconds(mL2Period);
|
|
|
|
//
|
|
mL2Candles = mL2PeriodSecconds / currentPeriodSecconds;
|
|
}
|
|
|
|
//
|
|
// Farest ...
|
|
// Calculate Cycle 3 Requirements ...
|
|
if (mL3Period == NULL)
|
|
{
|
|
//
|
|
if (l3Method == X_PERIOD_AUTO)
|
|
{
|
|
mL3Period = GetFarestTimeFrame(_Period);
|
|
}
|
|
else
|
|
{
|
|
mL3Period = l3Period;
|
|
}
|
|
|
|
//
|
|
mL3PeriodSecconds = PeriodSeconds(mL3Period);
|
|
|
|
//
|
|
mL3Candles = mL3PeriodSecconds / currentPeriodSecconds;
|
|
}
|
|
}
|
|
|
|
//
|
|
// Define Indexes and Styles ...
|
|
void DefineBuffers()
|
|
{
|
|
//
|
|
// 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);
|
|
|
|
//
|
|
// 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_BEGIN, maxLength);
|
|
|
|
//
|
|
// COLOR Buffer ...
|
|
ArraySetAsSeries(llColorBuffer, true);
|
|
SetIndexBuffer(llColorBufferIndex, llColorBuffer, INDICATOR_COLOR_INDEX);
|
|
|
|
//
|
|
// CYCLES ...
|
|
|
|
//
|
|
// NEAREST ...
|
|
|
|
//
|
|
// HH Buffer ...
|
|
|
|
//
|
|
// SERIES ...
|
|
ArraySetAsSeries(l1hhBuffer, true);
|
|
SetIndexBuffer(l1hhBufferIndex, l1hhBuffer, INDICATOR_DATA);
|
|
|
|
//
|
|
// PLOT Config ...
|
|
PlotIndexSetInteger(l1hhPlotBufferIndex, PLOT_SHOW_DATA, showL1Trend);
|
|
PlotIndexSetInteger(l1hhPlotBufferIndex, PLOT_DRAW_BEGIN, maxLength);
|
|
|
|
//
|
|
// COLOR Buffer ...
|
|
ArraySetAsSeries(l1hhColorBuffer, true);
|
|
SetIndexBuffer(l1hhColorBufferIndex, l1hhColorBuffer, INDICATOR_COLOR_INDEX);
|
|
|
|
//
|
|
// LL Buffer ...
|
|
|
|
//
|
|
// SERIES ...
|
|
ArraySetAsSeries(l1llBuffer, true);
|
|
SetIndexBuffer(l1llBufferIndex, l1llBuffer, INDICATOR_DATA);
|
|
|
|
//
|
|
// PLOT Config ...
|
|
PlotIndexSetInteger(l1llPlotBufferIndex, PLOT_SHOW_DATA, showL1Trend);
|
|
PlotIndexSetInteger(l1llPlotBufferIndex, PLOT_DRAW_BEGIN, maxLength);
|
|
|
|
//
|
|
// COLOR Buffer ...
|
|
ArraySetAsSeries(l1llColorBuffer, true);
|
|
SetIndexBuffer(l1llColorBufferIndex, l1llColorBuffer, INDICATOR_COLOR_INDEX);
|
|
|
|
//
|
|
// MEDIEST ...
|
|
|
|
//
|
|
// HH Buffer ...
|
|
|
|
//
|
|
// SERIES ...
|
|
ArraySetAsSeries(l2hhBuffer, true);
|
|
SetIndexBuffer(l2hhBufferIndex, l2hhBuffer, INDICATOR_DATA);
|
|
|
|
//
|
|
// PLOT Config ...
|
|
PlotIndexSetInteger(l2hhPlotBufferIndex, PLOT_SHOW_DATA, showL2Trend);
|
|
PlotIndexSetInteger(l2hhPlotBufferIndex, PLOT_DRAW_BEGIN, maxLength);
|
|
|
|
//
|
|
// COLOR Buffer ...
|
|
ArraySetAsSeries(l2hhColorBuffer, true);
|
|
SetIndexBuffer(l2hhColorBufferIndex, l2hhColorBuffer, INDICATOR_COLOR_INDEX);
|
|
|
|
//
|
|
// LL Buffer ...
|
|
|
|
//
|
|
// SERIES ...
|
|
ArraySetAsSeries(l2llBuffer, true);
|
|
SetIndexBuffer(l2llBufferIndex, l2llBuffer, INDICATOR_DATA);
|
|
|
|
//
|
|
// PLOT Config ...
|
|
PlotIndexSetInteger(l2llPlotBufferIndex, PLOT_SHOW_DATA, showL2Trend);
|
|
PlotIndexSetInteger(l2llPlotBufferIndex, PLOT_DRAW_BEGIN, maxLength);
|
|
|
|
//
|
|
// COLOR Buffer ...
|
|
ArraySetAsSeries(l2llColorBuffer, true);
|
|
SetIndexBuffer(l2llColorBufferIndex, l2llColorBuffer, INDICATOR_COLOR_INDEX);
|
|
|
|
//
|
|
// FAREST ...
|
|
|
|
//
|
|
// HH Buffer ...
|
|
|
|
//
|
|
// SERIES ...
|
|
ArraySetAsSeries(l3hhBuffer, true);
|
|
SetIndexBuffer(l3hhBufferIndex, l3hhBuffer, INDICATOR_DATA);
|
|
|
|
//
|
|
// PLOT Config ...
|
|
PlotIndexSetInteger(l3hhPlotBufferIndex, PLOT_SHOW_DATA, showL3Trend);
|
|
PlotIndexSetInteger(l3hhPlotBufferIndex, PLOT_DRAW_BEGIN, maxLength);
|
|
|
|
//
|
|
// COLOR Buffer ...
|
|
ArraySetAsSeries(l3hhColorBuffer, true);
|
|
SetIndexBuffer(l3hhColorBufferIndex, l3hhColorBuffer, INDICATOR_COLOR_INDEX);
|
|
|
|
//
|
|
// LL Buffer ...
|
|
|
|
//
|
|
// SERIES ...
|
|
ArraySetAsSeries(l3llBuffer, true);
|
|
SetIndexBuffer(l3llBufferIndex, l3llBuffer, INDICATOR_DATA);
|
|
|
|
//
|
|
// PLOT Config ...
|
|
PlotIndexSetInteger(l3llPlotBufferIndex, PLOT_SHOW_DATA, showL3Trend);
|
|
PlotIndexSetInteger(l3llPlotBufferIndex, PLOT_DRAW_BEGIN, maxLength);
|
|
|
|
//
|
|
// COLOR Buffer ...
|
|
ArraySetAsSeries(l3llColorBuffer, true);
|
|
SetIndexBuffer(l3llColorBufferIndex, l3llColorBuffer, INDICATOR_COLOR_INDEX);
|
|
|
|
//
|
|
// DATA Buffers ...
|
|
ArraySetAsSeries(hhStateBuffer, true);
|
|
SetIndexBuffer(hhStateBufferIndex, hhStateBuffer, INDICATOR_CALCULATIONS);
|
|
|
|
ArraySetAsSeries(llStateBuffer, true);
|
|
SetIndexBuffer(llStateBufferIndex, llStateBuffer, INDICATOR_CALCULATIONS);
|
|
|
|
//
|
|
// LEVEL 1 ...
|
|
ArraySetAsSeries(l1HHStateBuffer, true);
|
|
SetIndexBuffer(l1HHStateBufferIndex, l1HHStateBuffer, INDICATOR_CALCULATIONS);
|
|
|
|
ArraySetAsSeries(l1LLStateBuffer, true);
|
|
SetIndexBuffer(l1LLStateBufferIndex, l1LLStateBuffer, INDICATOR_CALCULATIONS);
|
|
|
|
//
|
|
// LEVEL 2 ...
|
|
ArraySetAsSeries(l2HHStateBuffer, true);
|
|
SetIndexBuffer(l2HHStateBufferIndex, l2HHStateBuffer, INDICATOR_CALCULATIONS);
|
|
|
|
ArraySetAsSeries(l2LLStateBuffer, true);
|
|
SetIndexBuffer(l2LLStateBufferIndex, l2LLStateBuffer, INDICATOR_CALCULATIONS);
|
|
|
|
//
|
|
// LEVEL 3 ...
|
|
ArraySetAsSeries(l3HHStateBuffer, true);
|
|
SetIndexBuffer(l3HHStateBufferIndex, l3HHStateBuffer, INDICATOR_CALCULATIONS);
|
|
|
|
ArraySetAsSeries(l3LLStateBuffer, true);
|
|
SetIndexBuffer(l3LLStateBufferIndex, l3LLStateBuffer, INDICATOR_CALCULATIONS);
|
|
}
|
|
|
|
//
|
|
// Set Indicator Short Name and also we can define Buffers Labels ...
|
|
void SetIndicatorName()
|
|
{
|
|
//
|
|
IndicatorSetString(INDICATOR_SHORTNAME, ShortName);
|
|
}
|
|
|
|
//
|
|
// 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 && showTrend ? 1 : 0;
|
|
double llColorIDX = showConsolidations && showTrend ? 1 : 0;
|
|
hhColorBuffer[bar_index] = hhColorIDX;
|
|
llColorBuffer[bar_index] = llColorIDX;
|
|
|
|
//
|
|
// Calculate Cycle Changes ...
|
|
|
|
//
|
|
int cycleCLength = cycleLength * numberOfVerifications;
|
|
bool isCycleCLengthPassed = MathAbs(ArraySize(hhBuffer) - bar_index) > cycleCLength + 1;
|
|
|
|
//
|
|
if (isCycleCLengthPassed)
|
|
{
|
|
//
|
|
bool isLowestLowUp = true;
|
|
bool isLowestLowDown = true;
|
|
bool isHighsestHighUp = true;
|
|
bool isHighsestHighDown = true;
|
|
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;
|
|
}
|
|
}
|
|
|
|
//
|
|
if (showTrend)
|
|
{
|
|
//
|
|
hhColorBuffer[bar_index] = hhColorIDX;
|
|
llColorBuffer[bar_index] = llColorIDX;
|
|
}
|
|
|
|
//
|
|
hhColorIDX =
|
|
hhColorIDX == 0 || hhColorIDX == 1
|
|
? 0
|
|
: hhColorIDX - 1;
|
|
llColorIDX =
|
|
llColorIDX == 0 || llColorIDX == 1
|
|
? 0
|
|
: llColorIDX - 1;
|
|
|
|
//
|
|
hhStateBuffer[bar_index] = hhColorIDX;
|
|
llStateBuffer[bar_index] = llColorIDX;
|
|
|
|
//
|
|
// CYCLE 1 ...
|
|
|
|
//
|
|
int l1cycleLength = cycleLength * mL1Candles;
|
|
|
|
//
|
|
// HIGHER Highs ...
|
|
double l1hh = GetHighestHigh(
|
|
_Symbol,
|
|
_Period,
|
|
hhMode,
|
|
l1cycleLength,
|
|
bar_index);
|
|
|
|
//
|
|
l1hhBuffer[bar_index] = l1hh;
|
|
|
|
//
|
|
// LOWER Lows ...
|
|
double l1ll = GetLowestLow(
|
|
_Symbol,
|
|
_Period,
|
|
llMode,
|
|
l1cycleLength,
|
|
bar_index);
|
|
|
|
//
|
|
l1llBuffer[bar_index] = l1ll;
|
|
|
|
//
|
|
// Set Default Color ...
|
|
// in Consolidations use index 0 for CLR_NONE ...
|
|
double l1hhColorIDX = showConsolidations && showL1Trend ? 1 : 0;
|
|
double l1llColorIDX = showConsolidations && showL1Trend ? 1 : 0;
|
|
l1hhColorBuffer[bar_index] = l1hhColorIDX;
|
|
l1llColorBuffer[bar_index] = l1llColorIDX;
|
|
|
|
//
|
|
// Calculate Cycle Changes ...
|
|
|
|
//
|
|
int l1cycleCLength = l1cycleLength * numberOfVerifications;
|
|
bool isl1CycleCLengthPassed = MathAbs(ArraySize(l1hhBuffer) - bar_index) > l1cycleCLength + 1;
|
|
|
|
//
|
|
if (isl1CycleCLengthPassed)
|
|
{
|
|
//
|
|
bool isLowestLowUp = true;
|
|
bool isLowestLowDown = true;
|
|
bool isHighsestHighUp = true;
|
|
bool isHighsestHighDown = true;
|
|
for (int i = bar_index; i < bar_index + l1cycleCLength; i++)
|
|
{
|
|
//
|
|
double iHH = l1hhBuffer[i];
|
|
double iLL = l1llBuffer[i];
|
|
|
|
//
|
|
if (l1hh < iHH)
|
|
{
|
|
isHighsestHighUp = false;
|
|
}
|
|
else if (l1hh > iHH)
|
|
{
|
|
isHighsestHighDown = false;
|
|
}
|
|
|
|
//
|
|
if (l1ll < iLL)
|
|
{
|
|
isLowestLowUp = false;
|
|
}
|
|
else if (l1ll > iLL)
|
|
{
|
|
isLowestLowDown = false;
|
|
}
|
|
}
|
|
|
|
//
|
|
// Detect Trends and Change Colors ...
|
|
|
|
//
|
|
if (isLowestLowUp &&
|
|
isHighsestHighUp)
|
|
{
|
|
//
|
|
l1hhColorIDX = 2;
|
|
l1llColorIDX = 2;
|
|
}
|
|
|
|
//
|
|
if (isLowestLowDown &&
|
|
isHighsestHighDown)
|
|
{
|
|
//
|
|
l1hhColorIDX = 3;
|
|
l1llColorIDX = 3;
|
|
}
|
|
}
|
|
|
|
//
|
|
if (showL1Trend)
|
|
{
|
|
//
|
|
l1hhColorBuffer[bar_index] = l1hhColorIDX;
|
|
l1llColorBuffer[bar_index] = l1llColorIDX;
|
|
}
|
|
|
|
//
|
|
l1hhColorIDX =
|
|
l1hhColorIDX == 0 || l1hhColorIDX == 1
|
|
? 0
|
|
: l1hhColorIDX - 1;
|
|
l1llColorIDX =
|
|
l1llColorIDX == 0 || l1llColorIDX == 1
|
|
? 0
|
|
: l1llColorIDX - 1;
|
|
|
|
//
|
|
l1HHStateBuffer[bar_index] = l1hhColorIDX;
|
|
l1LLStateBuffer[bar_index] = l1llColorIDX;
|
|
|
|
|
|
//
|
|
// CYCLE 2 ...
|
|
|
|
//
|
|
int l2cycleLength = cycleLength * mL2Candles;
|
|
|
|
//
|
|
// HIGHER Highs ...
|
|
double l2hh = GetHighestHigh(
|
|
_Symbol,
|
|
_Period,
|
|
hhMode,
|
|
l2cycleLength,
|
|
bar_index);
|
|
|
|
//
|
|
l2hhBuffer[bar_index] = l2hh;
|
|
|
|
//
|
|
// LOWER Lows ...
|
|
double l2ll = GetLowestLow(
|
|
_Symbol,
|
|
_Period,
|
|
llMode,
|
|
l2cycleLength,
|
|
bar_index);
|
|
|
|
//
|
|
l2llBuffer[bar_index] = l2ll;
|
|
|
|
//
|
|
// Set Default Color ...
|
|
// in Consolidations use index 0 for CLR_NONE ...
|
|
double l2hhColorIDX = showConsolidations && showL2Trend ? 1 : 0;
|
|
double l2llColorIDX = showConsolidations && showL2Trend ? 1 : 0;
|
|
l2hhColorBuffer[bar_index] = l2hhColorIDX;
|
|
l2llColorBuffer[bar_index] = l2llColorIDX;
|
|
|
|
//
|
|
// Calculate Cycle Changes ...
|
|
|
|
//
|
|
int l2cycleCLength = l2cycleLength * numberOfVerifications;
|
|
bool isl2CycleCLengthPassed = MathAbs(ArraySize(l2hhBuffer) - bar_index) > l2cycleCLength + 1;
|
|
|
|
//
|
|
if (isl2CycleCLengthPassed)
|
|
{
|
|
//
|
|
bool isLowestLowUp = true;
|
|
bool isLowestLowDown = true;
|
|
bool isHighsestHighUp = true;
|
|
bool isHighsestHighDown = true;
|
|
for (int i = bar_index; i < bar_index + l2cycleCLength; i++)
|
|
{
|
|
//
|
|
double iHH = l2hhBuffer[i];
|
|
double iLL = l2llBuffer[i];
|
|
|
|
//
|
|
if (l2hh < iHH)
|
|
{
|
|
isHighsestHighUp = false;
|
|
}
|
|
else if (l2hh > iHH)
|
|
{
|
|
isHighsestHighDown = false;
|
|
}
|
|
|
|
//
|
|
if (l2ll < iLL)
|
|
{
|
|
isLowestLowUp = false;
|
|
}
|
|
else if (l2ll > iLL)
|
|
{
|
|
isLowestLowDown = false;
|
|
}
|
|
}
|
|
|
|
//
|
|
// Detect Trends and Change Colors ...
|
|
|
|
//
|
|
if (isLowestLowUp &&
|
|
isHighsestHighUp)
|
|
{
|
|
//
|
|
l2hhColorIDX = 2;
|
|
l2llColorIDX = 2;
|
|
}
|
|
|
|
//
|
|
if (isLowestLowDown &&
|
|
isHighsestHighDown)
|
|
{
|
|
//
|
|
l2hhColorIDX = 3;
|
|
l2llColorIDX = 3;
|
|
}
|
|
}
|
|
|
|
//
|
|
if (showL2Trend)
|
|
{
|
|
//
|
|
l2hhColorBuffer[bar_index] = l2hhColorIDX;
|
|
l2llColorBuffer[bar_index] = l2llColorIDX;
|
|
}
|
|
|
|
//
|
|
l2hhColorIDX =
|
|
l2hhColorIDX == 0 || l2hhColorIDX == 1
|
|
? 0
|
|
: l2hhColorIDX - 1;
|
|
l2llColorIDX =
|
|
l2llColorIDX == 0 || l2llColorIDX == 1
|
|
? 0
|
|
: l2llColorIDX - 1;
|
|
|
|
//
|
|
l2HHStateBuffer[bar_index] = l2hhColorIDX;
|
|
l2LLStateBuffer[bar_index] = l2llColorIDX;
|
|
|
|
//
|
|
// CYCLE 3 ...
|
|
|
|
//
|
|
int l3cycleLength = cycleLength * mL3Candles;
|
|
|
|
//
|
|
// HIGHER Highs ...
|
|
double l3hh = GetHighestHigh(
|
|
_Symbol,
|
|
_Period,
|
|
hhMode,
|
|
l3cycleLength,
|
|
bar_index);
|
|
|
|
//
|
|
l3hhBuffer[bar_index] = l3hh;
|
|
|
|
//
|
|
// LOWER Lows ...
|
|
double l3ll = GetLowestLow(
|
|
_Symbol,
|
|
_Period,
|
|
llMode,
|
|
l3cycleLength,
|
|
bar_index);
|
|
|
|
//
|
|
l3llBuffer[bar_index] = l3ll;
|
|
|
|
//
|
|
// Set Default Color ...
|
|
// in Consolidations use index 0 for CLR_NONE ...
|
|
double l3hhColorIDX = showConsolidations && showL3Trend ? 1 : 0;
|
|
double l3llColorIDX = showConsolidations && showL3Trend ? 1 : 0;
|
|
l3hhColorBuffer[bar_index] = l3hhColorIDX;
|
|
l3llColorBuffer[bar_index] = l3llColorIDX;
|
|
|
|
//
|
|
// Calculate Cycle Changes ...
|
|
|
|
//
|
|
int l3cycleCLength = l3cycleLength * numberOfVerifications;
|
|
bool isl3CycleCLengthPassed = MathAbs(ArraySize(l3hhBuffer) - bar_index) > l3cycleCLength + 1;
|
|
|
|
//
|
|
if (isl3CycleCLengthPassed)
|
|
{
|
|
//
|
|
bool isLowestLowUp = true;
|
|
bool isLowestLowDown = true;
|
|
bool isHighsestHighUp = true;
|
|
bool isHighsestHighDown = true;
|
|
for (int i = bar_index; i < bar_index + l3cycleCLength; i++)
|
|
{
|
|
//
|
|
double iHH = l3hhBuffer[i];
|
|
double iLL = l3llBuffer[i];
|
|
|
|
//
|
|
if (l3hh < iHH)
|
|
{
|
|
isHighsestHighUp = false;
|
|
}
|
|
else if (l3hh > iHH)
|
|
{
|
|
isHighsestHighDown = false;
|
|
}
|
|
|
|
//
|
|
if (l3ll < iLL)
|
|
{
|
|
isLowestLowUp = false;
|
|
}
|
|
else if (l3ll > iLL)
|
|
{
|
|
isLowestLowDown = false;
|
|
}
|
|
}
|
|
|
|
//
|
|
// Detect Trends and Change Colors ...
|
|
|
|
//
|
|
if (isLowestLowUp &&
|
|
isHighsestHighUp)
|
|
{
|
|
//
|
|
l3hhColorIDX = 2;
|
|
l3llColorIDX = 2;
|
|
}
|
|
|
|
//
|
|
if (isLowestLowDown &&
|
|
isHighsestHighDown)
|
|
{
|
|
//
|
|
l3hhColorIDX = 3;
|
|
l3llColorIDX = 3;
|
|
}
|
|
}
|
|
|
|
//
|
|
if (showL3Trend)
|
|
{
|
|
//
|
|
l3hhColorBuffer[bar_index] = l3hhColorIDX;
|
|
l3llColorBuffer[bar_index] = l3llColorIDX;
|
|
}
|
|
|
|
//
|
|
l3hhColorIDX =
|
|
l3hhColorIDX == 0 || l3hhColorIDX == 1
|
|
? 0
|
|
: l3hhColorIDX - 1;
|
|
l3llColorIDX =
|
|
l3llColorIDX == 0 || l3llColorIDX == 1
|
|
? 0
|
|
: l3llColorIDX - 1;
|
|
|
|
//
|
|
l3HHStateBuffer[bar_index] = l3hhColorIDX;
|
|
l3LLStateBuffer[bar_index] = l3llColorIDX;
|
|
}
|
|
|
|
//
|
|
// END Functions ...
|
|
//
|