Files
xMQL5/MQLTestWorkspace/BKPS/BKP/x-saherelm.x121.xpv.mq5
T

641 lines
13 KiB
Plaintext

///////////////////////////////////////////////////////////////
//
// SaherElm IT Center MQL5 X121_PV Peaks and Vales Indicator
// -----------------------------------------------------------
// Name: X121_PV
// Description: Market Peaks and Vales
// 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_PV Indicator"
#property strict
//
// START Constants ...
//
//
// Indicator Short Name ...
#define ShortName "X121_PV"
//
// END Constants ...
//
//
// 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 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 showPeaksAndVales = true; // Show Peaks and Vales
//
// 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 12
#property indicator_plots 4
//
// PEKSANDVALES ...
//
// PEAKS ...
//
#define peaksBufferIndex 0
double peaksBuffer[];
//
#property indicator_label1 "X121_PV PEAKS"
#property indicator_type1 DRAW_ARROW
#property indicator_color1 clrAqua
#property indicator_width1 3
//
// VALES ...
//
#define valesBufferIndex 1
double valesBuffer[];
//
#property indicator_label2 "X121_PV VALES"
#property indicator_type2 DRAW_ARROW
#property indicator_color2 clrMagenta
#property indicator_width2 3
//
#define rUpBufferIndex 2
#define rUpPlotBufferIndex 2
double rUpBuffer[];
//
#define rUpColorBufferIndex 3
double rUpColorBuffer[];
//
#property indicator_label3 "X121_PV R UP"
#property indicator_type3 DRAW_COLOR_LINE
#property indicator_color3 clrAqua, CLR_NONE
#property indicator_style3 STYLE_DOT
#property indicator_width3 1
//
#define rDownBufferIndex 4
#define rDownPlotBufferIndex 3
double rDownBuffer[];
//
#define rDownColorBufferIndex 5
double rDownColorBuffer[];
//
#property indicator_label4 "X121_PV R Down"
#property indicator_type4 DRAW_COLOR_LINE
#property indicator_color4 clrMagenta, CLR_NONE
#property indicator_style4 STYLE_DOT
#property indicator_width4 1
//
// DATA Buffers ...
//
#define sHHBufferIndex 6
double sHHBuffer[];
#define sLLBufferIndex 7
double sLLBuffer[];
//
#define mHHBufferIndex 8
double mHHBuffer[];
//
#define mLLBufferIndex 9
double mLLBuffer[];
//
#define lHHBufferIndex 10
double lHHBuffer[];
//
#define lLLBufferIndex 11
double lLLBuffer[];
//
// END Buffers ...
//
//
// START Global Definitions: Variables, Properties and etc ...
//
//
int maxLength;
//
double lastPeak;
double lastVale;
//
// 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 =
//
// Validate Peaks and Vales ...
(shortLength > 2 &&
mediumLength > shortLength &&
longLength > mediumLength)
//
;
//
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);
//
return result;
}
//
// Define Indexes and Styles ...
void DefineBuffers()
{
//
// RANGE ...
//
// PEAKS and VALES ...
ENUM_DRAW_TYPE pvDrawType = showPeaksAndVales ? DRAW_ARROW : DRAW_NONE;
//
// PEAKS ...
ArraySetAsSeries(peaksBuffer, true);
SetIndexBuffer(peaksBufferIndex, peaksBuffer, INDICATOR_DATA);
PlotIndexSetInteger(peaksBufferIndex, PLOT_ARROW, 225);
PlotIndexSetInteger(peaksBufferIndex, PLOT_ARROW_SHIFT, -20);
PlotIndexSetDouble(peaksBufferIndex, PLOT_EMPTY_VALUE, 0);
//
PlotIndexSetInteger(peaksBufferIndex, PLOT_SHOW_DATA, false);
PlotIndexSetInteger(peaksBufferIndex, PLOT_DRAW_TYPE, pvDrawType);
//
// VALES ...
ArraySetAsSeries(valesBuffer, true);
SetIndexBuffer(valesBufferIndex, valesBuffer, INDICATOR_DATA);
PlotIndexSetInteger(valesBufferIndex, PLOT_ARROW, 225);
PlotIndexSetInteger(valesBufferIndex, PLOT_ARROW_SHIFT, 20);
PlotIndexSetDouble(valesBufferIndex, PLOT_EMPTY_VALUE, 0);
//
PlotIndexSetInteger(valesBufferIndex, PLOT_SHOW_DATA, false);
PlotIndexSetInteger(valesBufferIndex, PLOT_DRAW_TYPE, pvDrawType);
//
// UP Buffer ...
ArraySetAsSeries(rUpBuffer, true);
SetIndexBuffer(rUpBufferIndex, rUpBuffer, INDICATOR_DATA);
PlotIndexSetInteger(rUpBufferIndex, PLOT_DRAW_BEGIN, maxLength);
PlotIndexSetInteger(rUpBufferIndex, PLOT_SHOW_DATA, false);
//
// UP Color Buffer ...
ArraySetAsSeries(rUpColorBuffer, true);
SetIndexBuffer(rUpColorBufferIndex, rUpColorBuffer, INDICATOR_COLOR_INDEX);
//
// DOWN Buffer ...
ArraySetAsSeries(rDownBuffer, true);
SetIndexBuffer(rDownBufferIndex, rDownBuffer, INDICATOR_DATA);
PlotIndexSetInteger(rDownBufferIndex, PLOT_DRAW_BEGIN, maxLength);
PlotIndexSetInteger(rDownPlotBufferIndex, PLOT_SHOW_DATA, false);
//
// DOWN Color Buffer ...
ArraySetAsSeries(rDownColorBuffer, true);
SetIndexBuffer(rDownColorBufferIndex, rDownColorBuffer, INDICATOR_COLOR_INDEX);
//
// DATA Buffers ...
//
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);
}
//
// 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
)
{
//
// Calculate ...
//
double rUpColorIDX = 1;
double rDownColorIDX = 1;
//
// PEAKS and VALES ...
//
// SHORT ...
//
// HH Buffer ...
int sHHIndex = iHighest(
_Symbol,
_Period,
hhMode,
shortLength,
bar_index);
double sHHValue = GetAppliedPrice(
sHHIndex,
hhMode);
//
sHHBuffer[bar_index] = sHHValue;
//
// LL Buffer ...
int sLLIndex = iLowest(
_Symbol,
_Period,
llMode,
shortLength,
bar_index);
double sLLValue = GetAppliedPrice(
sLLIndex,
llMode);
//
sLLBuffer[bar_index] = sLLValue;
//
// MEDIUM ...
//
// HH Buffer ...
int mHHIndex = iHighest(
_Symbol,
_Period,
hhMode,
mediumLength,
bar_index);
double mHHValue = GetAppliedPrice(
mHHIndex,
hhMode);
//
mHHBuffer[bar_index] = mHHValue;
//
// LL Buffer ...
int mLLIndex = iLowest(
_Symbol,
_Period,
llMode,
mediumLength,
bar_index);
double mLLValue = GetAppliedPrice(
mLLIndex,
llMode);
//
mLLBuffer[bar_index] = mLLValue;
//
// LONG ...
//
// HH Buffer ...
int lHHIndex = iHighest(
_Symbol,
_Period,
hhMode,
longLength,
bar_index);
double lHHValue = GetAppliedPrice(
lHHIndex,
hhMode);
//
lHHBuffer[bar_index] = lHHValue;
//
// LL Buffer ...
int lLLIndex = iLowest(
_Symbol,
_Period,
llMode,
longLength,
bar_index);
double lLLValue = GetAppliedPrice(
lLLIndex,
llMode);
//
lLLBuffer[bar_index] = lLLValue;
//
// RANGE ...
//
// UP ...
double rUpValue = (sHHValue + mHHValue + lHHValue) / 3;
rUpBuffer[bar_index] = rUpValue;
//
// DOWN ...
double rDownValue = (sLLValue + mLLValue + lLLValue) / 3;
rDownBuffer[bar_index] = rDownValue;
//
// Detecting Peaks and Vales ...
//
// PEAKS ...
bool isPeak = rUpValue == lHHValue &&
lHHValue == mHHValue &&
mHHValue == sHHValue;
//
if (isPeak && lastPeak != rUpValue)
{
//
if (showPeaksAndVales)
{
rUpColorIDX = 0;
}
//
lastPeak = rUpValue;
peaksBuffer[bar_index] = lastPeak;
}
else
{
peaksBuffer[bar_index] = 0;
}
//
// VALES ...
bool isVale = rDownValue == lLLValue &&
lLLValue == mLLValue &&
mLLValue == sLLValue;
if (isVale && lastVale != rDownValue)
{
//
if (showPeaksAndVales)
{
rDownColorIDX = 0;
}
//
lastVale = rDownValue;
valesBuffer[bar_index] = lastVale;
}
else
{
valesBuffer[bar_index] = 0;
}
//
// Set Default Colors ...
//
rUpColorBuffer[bar_index] = rUpColorIDX;
rDownColorBuffer[bar_index] = rDownColorIDX;
}
//
// END Functions ...
//