Files
xMQL5/BKPS/14041018/Documents/BKP/test-xpv.ind.mq5
T
2026-01-08 11:39:42 +03:30

765 lines
17 KiB
Plaintext

///////////////////////////////////////////////////////
//
// SaherElm IT Center MQL5 Indicator
// -------------------------------------------------
// Name: XFIMAPV
// Description: Integrates All Requirements inside
// this indicator for analyse Markets Based on OM1 ...
//
//
// 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 XFIMA Indicator"
#property icon "../Images/SaherElm_Logo_Color.ico"
#property strict
//
// Definitions ...
//
#define ShortName "XFIMA"
//
// Imports ...
#include "../Libraries/x-saherelm.common.lib.mq5"
//
// Inputs ...
//
// Calculation ...
input group "Calculation";
input int pvValidationLength = 7; // Peaks and Vales Validation Length
input int pvValidationCount = 2; // Recent Peak or Vale must under/over latests
input ENUM_X_PRICE peakPriceType = X_PRICE_HIGH; // Peak Calculated Price
input ENUM_X_PRICE valePriceType = X_PRICE_LOW; // Vale Calculated Price
//
// Presentation ...
input group "Presentation";
input bool showPV = true; // Show Peaks and Vales
input bool showHL = true; // Show Highs and Lows
input int startCalculationForLastBars = 1500; // Calculate Last n Bars
input int peakArrowCode = 159; // Peaks Arrow Code
input int highArrowCode = 159; // Highs Arrow Code
input int valeArrowCode = 159; // Vales Arrow Code
input int lowArrowCode = 159; // Lows Arrow Code
//
// Non Inputs ...
//
#define hideColorIDX 0
#define bullishColorIDX 1
#define bearishColorIDX 2
#define neuturalColorIDX 3
//
#define bullishState 1
#define neuturalState 0
#define bearishState -1
//
#define emptyValue 0.0
//
// Buffers ...
//
#property indicator_chart_window
//
#property indicator_buffers 6
#property indicator_plots 4
//
// Plot Buffers ...
//
// PEAKS ...
//
#define peakBufferIndex 0
double peakBuffer[];
//
#define peakBufferPlotIndex 0
#property indicator_label1 "PEAK"
#property indicator_type1 DRAW_ARROW
#property indicator_color1 clrMagenta
#property indicator_width1 3
//
// VALES ...
//
#define valeBufferIndex 1
double valeBuffer[];
//
#define valeBufferPlotIndex 1
#property indicator_label2 "VALE"
#property indicator_type2 DRAW_ARROW
#property indicator_color2 clrAqua
#property indicator_width2 3
//
// HIGHS ...
//
#define highBufferIndex 2
double highBuffer[];
//
#define highBufferPlotIndex 2
#property indicator_label3 "HIGH"
#property indicator_type3 DRAW_ARROW
#property indicator_color3 clrOrangeRed
#property indicator_width3 3
//
// LOWS ...
//
#define lowBufferIndex 3
double lowBuffer[];
//
#define lowBufferPlotIndex 3
#property indicator_label4 "LOW"
#property indicator_type4 DRAW_ARROW
#property indicator_color4 clrYellow
#property indicator_width4 3
//
// Data Buffers ...
//
#define mLastBufferIndex 3
//
#define highRawBufferIndex mLastBufferIndex + 1
double highRawBuffer[];
//
#define lowRawBufferIndex mLastBufferIndex + 2
double lowRawBuffer[];
//
// Variables ...
//
int limit;
//
int maxLength;
//
int firstBarIndex;
//
// Event Handlers ...
/**
* Initialize Indicator ...
*
* @return ( int )
*/
int OnInit()
{
//
bool has = false;
//
// Validate Inputs ...
has = ValidateInputs();
if (!has)
{
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();
//
// Initial Requirements ...
has = InitRequirements();
if (!has)
{
return INIT_FAILED;
}
//
// Define Index Buffers ...
DefineBuffers();
//
// Set Indicator ShortName ...
SetIndicatorName();
//
// Init Succeed ...
return INIT_SUCCEEDED;
//
}
/**
* De Initialize Indicator ...
*
* @param reason: Integer, De Initialization Reason ...
*/
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
//
// De Initialize all Handlers and etc ...
}
/**
* Calculate Bars ...
*
* @param rates_total: Integer, Total Bars on Chart ...
* @param prev_calculated: Integer, Total Calculated Bars on Charts ...
* @param time: DateTime Array, History of Open Time ...
* @param open: Double Array, History of Open Prices ...
* @param high: Double Array, History of High Prices ...
* @param low: Double Array, History of Low Prices ...
* @param close: Double Array, History of Close Prices ...
* @param tick_volume: Long, History of Tick Volumes on Bar ...
* @param volume: Long, History of Trade Volumes ...
* @param spread: Double, History of Spread Price ...
*
* @return ( int )
*/
int OnCalculate(
const int rates_total,
const int prev_calculated,
const datetime &time[],
const double &open[],
const double &high[],
const double &low[],
const double &close[],
const long &tick_volume[],
const long &volume[],
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);
//
// Validate Calculated Bars ...
//
limit = (prev_calculated > rates_total || prev_calculated <= 0)
? rates_total
: (rates_total - prev_calculated) + 1;
//
// Buffers Copy ...
//
// Main Loop ...
for (int i = limit - 1; i >= 0 && !IsStopped(); i--)
{
//
CalculateBuffers(
i,
prev_calculated,
rates_total,
//
open,
high,
close,
low,
tick_volume //
);
}
//
return rates_total;
}
//
// Custom Functions ...
/**
* Set Indicator Short Name and also we can define Buffers Labels ...
*/
void SetIndicatorName()
{
//
IndicatorSetInteger(INDICATOR_DIGITS, _Digits);
IndicatorSetString(INDICATOR_SHORTNAME, ShortName);
}
/**
* Validate Input Args for Initialization ...
*
* @return ( bool )
*/
bool ValidateInputs()
{
//
bool result = false;
//
result =
//
IsXValid(peakPriceType) &&
IsXValid(valePriceType) &&
NotEmptyZero(pvValidationLength)
//
;
//
return result;
}
/**
* Extract Max Length of Inputs ...
*
* @return ( int )
*/
int ExtractMaxLengthOfInputs()
{
//
int result = 0;
//
result = MathMax(result, pvValidationLength);
//
return result;
}
/**
* Define Required Buffers ...
*/
void DefineBuffers()
{
//
// Plot Buffers ...
//
// PV ...
//
ENUM_DRAW_TYPE pvDrawType = showPV ? DRAW_ARROW : DRAW_NONE;
//
// PEAK ...
//
ArraySetAsSeries(peakBuffer, true);
SetIndexBuffer(peakBufferIndex, peakBuffer, INDICATOR_DATA);
//
PlotIndexSetInteger(peakBufferPlotIndex, PLOT_SHOW_DATA, showPV);
PlotIndexSetInteger(peakBufferPlotIndex, PLOT_DRAW_TYPE, pvDrawType);
//
PlotIndexSetDouble(peakBufferPlotIndex, PLOT_EMPTY_VALUE, EMPTY_VALUE);
PlotIndexSetInteger(peakBufferPlotIndex, PLOT_ARROW, peakArrowCode);
//
// VALE ...
//
ArraySetAsSeries(valeBuffer, true);
SetIndexBuffer(valeBufferIndex, valeBuffer, INDICATOR_DATA);
//
PlotIndexSetInteger(valeBufferPlotIndex, PLOT_SHOW_DATA, showPV);
PlotIndexSetInteger(valeBufferPlotIndex, PLOT_DRAW_TYPE, pvDrawType);
//
PlotIndexSetDouble(valeBufferPlotIndex, PLOT_EMPTY_VALUE, EMPTY_VALUE);
PlotIndexSetInteger(valeBufferPlotIndex, PLOT_ARROW, valeArrowCode);
//
// HL ...
//
ENUM_DRAW_TYPE hlDrawType = showHL ? DRAW_ARROW : DRAW_NONE;
//
// HIGH ...
//
ArraySetAsSeries(highBuffer, true);
SetIndexBuffer(highBufferIndex, highBuffer, INDICATOR_DATA);
//
PlotIndexSetInteger(highBufferPlotIndex, PLOT_SHOW_DATA, showHL);
PlotIndexSetInteger(highBufferPlotIndex, PLOT_DRAW_TYPE, hlDrawType);
//
PlotIndexSetDouble(highBufferPlotIndex, PLOT_EMPTY_VALUE, EMPTY_VALUE);
PlotIndexSetInteger(highBufferPlotIndex, PLOT_ARROW, highArrowCode);
//
// LOW ...
//
ArraySetAsSeries(lowBuffer, true);
SetIndexBuffer(lowBufferIndex, lowBuffer, INDICATOR_DATA);
//
PlotIndexSetInteger(lowBufferPlotIndex, PLOT_SHOW_DATA, showHL);
PlotIndexSetInteger(lowBufferPlotIndex, PLOT_DRAW_TYPE, hlDrawType);
//
PlotIndexSetDouble(lowBufferPlotIndex, PLOT_EMPTY_VALUE, EMPTY_VALUE);
PlotIndexSetInteger(lowBufferPlotIndex, PLOT_ARROW, lowArrowCode);
//
// Data Buffers ...
//
ArraySetAsSeries(lowRawBuffer, true);
SetIndexBuffer(lowRawBufferIndex, lowRawBuffer, INDICATOR_CALCULATIONS);
//
ArraySetAsSeries(highRawBuffer, true);
SetIndexBuffer(highRawBufferIndex, highRawBuffer, INDICATOR_CALCULATIONS);
}
/**
* Initial all Indicator Requirements ...
*
* @return ( false )
*
*/
bool InitRequirements()
{
//
bool result = true;
//
// Handlers ...
//
return result;
}
/**
* Calculate Custom Buffers ...
*
* @param barIndex: Integer, Represent Current Bar ...
* @param prevCalculated: Integer, Represent Previous Calculated Bars ...
* @param ratesTotal: Integer, Represents All Available Bars ...
* @param open: Double Array, History of Open Prices ...
* @param high: Double Array, History of High Prices ...
* @param close: Double Array, History of Close Prices ...
* @param low: Double Array, History of Low Prices ...
* @param tickVolume: Long, History of Tick Volumes on Bar ...
*/
void CalculateBuffers(
int barIndex,
const int prevCalculated,
const int ratesTotal,
const double &open[],
const double &high[],
const double &close[],
const double &low[],
const long &tickVolume[] //
)
{
//
// Calculate Bars Limit and First Bar Index ...
int barsLimit = startCalculationForLastBars > 0
? startCalculationForLastBars
: 0;
if (barsLimit == 0)
{
//
barsLimit = ratesTotal;
firstBarIndex = barsLimit - 1;
}
else
{
firstBarIndex = startCalculationForLastBars;
}
//
// bool canCalculate = true;
bool canCalculate =
barIndex <= barsLimit;
if (canCalculate)
{
//
// Calculate Values ...
CalculateValues(
barIndex,
prevCalculated,
ratesTotal,
open,
high,
close,
low,
tickVolume //
);
}
else
{
FillBuffersZero(barIndex);
}
}
/**
* Fill All Bufers to Zero Vlue for Specified Bar Index ...
*
* @param barIndex: Integer ...
*/
void FillBuffersZero(int barIndex)
{
//
// TODO: Fill Buffers as Empty ...
lowBuffer[barIndex] = emptyValue;
highBuffer[barIndex] = emptyValue;
peakBuffer[barIndex] = emptyValue;
valeBuffer[barIndex] = emptyValue;
highRawBuffer[barIndex] = emptyValue;
lowRawBuffer[barIndex] = emptyValue;
}
/**
* Calculate Values ...
*
* @param barIndex: int, Specified Bar Index ...
* @param prevCalculated: int, Provides Previous Calculated Bars ...
* @param ratesTotal: int, Provides All Availabled Bars ...
* @param open: double Collection, Provides Open Prices Time Series ...
* @param high: double Collection, Provides High Prices Time Series ...
* @param close: double Collection, Provides Close Prices Time Series ...
* @param low: double Collection, Provides Low Prices Time Series ...
* @param tickVolume: long Collection, Provides Ticks Volume Prices Time Series ...
*/
void CalculateValues(
int barIndex,
const int prevCalculated,
const int ratesTotal,
const double &open[],
const double &high[],
const double &close[],
const double &low[],
const long &tickVolume[] //
)
{
//
// Common Requirements ...
//
FillBuffersZero(barIndex);
//
int lastBarIndex = barIndex + 1;
int maxBarIndex = startCalculationForLastBars > 0
? startCalculationForLastBars
: ratesTotal;
//
bool isFirstBar =
startCalculationForLastBars > 0
? barIndex == startCalculationForLastBars
: barIndex == firstBarIndex;
//
// Calculate Highs and Lows ...
XOHCL iBar;
bool has = iBar.Init(
_Symbol,
_Period,
barIndex //
);
if (!has)
{
return;
}
//
int lowestIDX = -1;
double iRawLow = iBar.FindLowest(
lowestIDX,
pvValidationLength,
valePriceType //
);
//
int highestIDX = -1;
double iRawHigh = iBar.FindHighest(
highestIDX,
pvValidationLength,
peakPriceType //
);
//
lowRawBuffer[barIndex] = iRawLow;
highRawBuffer[barIndex] = iRawHigh;
//
lowBuffer[barIndex] = iRawLow;
highBuffer[barIndex] = iRawHigh;
//
if (barIndex < firstBarIndex - (pvValidationLength + 1))
{
//
double tmp[];
ArrayCopy(
tmp,
lowRawBuffer,
0,
barIndex,
pvValidationLength //
);
double minLow = GetMin(tmp);
bool isLow = iRawLow < minLow;
lowBuffer[barIndex] =
isLow
? iRawLow
: lowBuffer[lastBarIndex];
//
XClean(tmp);
ArrayCopy(
tmp,
highRawBuffer,
0,
barIndex,
pvValidationLength //
);
double maxHigh = GetMax(tmp);
bool isHigh = iRawHigh > maxHigh;
highBuffer[barIndex] =
isHigh
? iRawHigh
: highBuffer[lastBarIndex];
//
// Cleanup Resources ...
XClean(tmp);
}
// //
// double iLow = emptyValue;
// // IsLow(iLow, barIndex);
// double iPLow = isFirstBar
// ? iLow
// : lowRawBuffer[lastBarIndex];
// //
// double iHigh = emptyValue;
// IsHigh(iHigh, barIndex);
// double iPHigh = isFirstBar
// ? iHigh
// : highRawBuffer[lastBarIndex];
// //
// // Clculate Values ...
// //
// lowRawBuffer[barIndex] = iLow;
// highRawBuffer[barIndex] = iHigh;
// //
// lowBuffer[barIndex] = NotEmptyZero(iLow)
// ? iLow
// : iPLow;
// highBuffer[barIndex] = NotEmptyZero(iHigh)
// ? iHigh
// : iPHigh;
// //
// double iP2Low =
// barIndex < maxBarIndex - 2
// ? lowBuffer[barIndex + 2]
// : emptyValue;
// double iP2High =
// barIndex < maxBarIndex - 2
// ? highBuffer[barIndex + 2]
// : emptyValue;
// //
// double mLowValues[];
// double iVale = emptyValue;
// if (NotEmptyZero(iPLow))
// {
// Add(iPLow, mLowValues);
// }
// if (NotEmptyZero(iP2Low))
// {
// Add(iP2Low, mLowValues);
// }
// bool isVale = iLow <= GetMin(mLowValues);
// iVale = isVale
// ? iLow
// : isFirstBar
// ? emptyValue
// : valeBuffer[lastBarIndex];
// valeBuffer[barIndex] = iVale;
// //
// double mHighValues[];
// double iPeak = emptyValue;
// if (NotEmptyZero(iPHigh))
// {
// Add(iPHigh, mHighValues);
// }
// if (NotEmptyZero(iP2High))
// {
// Add(iP2High, mHighValues);
// }
// bool isPeak = iHigh >= GetMax(mHighValues);
// iPeak = isPeak
// ? iHigh
// : isFirstBar
// ? emptyValue
// : peakBuffer[lastBarIndex];
// peakBuffer[barIndex] = iPeak;
// //
// bool has =
// NotEmptyZero(iLow) ||
// NotEmptyZero(iPLow) ||
// NotEmptyZero(iHigh) ||
// NotEmptyZero(iPHigh);
// if (has)
// {
// }
}
//