Files
MQL5Data/Indicators/x-saherelm.x121.xpv.mq5
T
2025-04-04 08:36:41 +03:30

1606 lines
36 KiB
Plaintext

///////////////////////////////////////////////////////
//
// SaherElm IT Center MQL5 Indicator
// -------------------------------------------------
// Name: X121 XPV
// Description: XPV ...
//
//
// 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 XPV Indicator"
#property strict
//
// Definitions ...
//
#define ShortName "X121 XPV"
//
// Includes Common Library ...
#include "../Libraries/x-saherelm.common.lib.mq5"
//
// Inputs ...
//
// Market ...
input group "Market";
//
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
//
input group "Boundary Detection";
input ENUM_SERIESMODE hhMode = MODE_HIGH; // Highest High Calculation Method
input ENUM_SERIESMODE llMode = MODE_LOW; // Lowest Low Calculation Method
input ENUM_X_FIBO_LEVELS goldenZoneLevel = X_FIBO_LEVEL_236; // Golden Zone Detection
//
input group "Sar Detection";
input double sarStep = 0.02; // Step
input double sarMax = 0.2; // Maximum
//
input group "Swing Detection";
input int swingLength = 5;
//
// Presentation ...
input group "Presentation";
//
input int startCalculationForLastBars = 1000; // Calculate Last n Bars
input int sarArrowCode = 159; // Parabolic Sar Arrow Code
input int peaksArrowCode = 159; // Peaks Arrow Code
input int valesArrowCode = 159; // Vales Arrow Code
input int trendArrowCode = 117; // Trend Arrow Code
input int swingLowsArrowCode = 159; // Swing Lows Arrow Code
input int swingHighsArrowCode = 159; // Swing Highs Arrow Code
input int supportsArrowCode = 159; // Supports Arrow Code
input int resistancesArrowCode = 159; // Resistances Arrow Code
//
input bool showSar = true; // Show Parabolic Sar
input bool showPeaks = true; // Show Peaks
input bool showVales = true; // Show Vales
input bool showTrend = true; // Show Trend
input bool showSwingLows = true; // Show Swing Lows
input bool showSwingHighs = true; // Show Swing Highs
input bool showGoldenZones = true; // Show Golden Zones
input bool showSupports = true; // Show Supports
input bool showResistances = true; // Show Resistances
//
// Buffers ...
//
#define hideColorIDX 0
#define bullishColorIDX 1
#define bearishColorIDX 2
#define neuturalColorIDX 3
//
#define emptyValue 0.0
//
#property indicator_chart_window
//
#property indicator_buffers 24
#property indicator_plots 10
//
// Plot Buffers ...
//
// PEAKS ...
//
#define peaksBufferIndex 0
double peaksBuffer[];
//
#property indicator_label1 "X121 PEAKS"
#property indicator_type1 DRAW_ARROW
#property indicator_color1 clrMagenta
#property indicator_width1 1
//
// VALES ...
//
#define valesBufferIndex 1
double valesBuffer[];
//
#property indicator_label2 "X121 VALES"
#property indicator_type2 DRAW_ARROW
#property indicator_color2 clrAqua
#property indicator_width2 1
//
#define sarBufferIndex 2
double sarBuffer[];
//
#property indicator_label3 "X121 SAR"
#property indicator_type3 DRAW_ARROW
#property indicator_color3 clrYellow
#property indicator_width3 1
//
// Swing Lows ...
//
#define swingLowsBufferIndex 3
double swingLowsBuffer[];
//
#property indicator_label4 "X121 SWL"
#property indicator_type4 DRAW_ARROW
#property indicator_color4 clrDodgerBlue
#property indicator_width4 1
//
// Swng Highs ...
//
#define swingHighsBufferIndex 4
double swingHighsBuffer[];
//
#property indicator_label5 "X121 SWH"
#property indicator_type5 DRAW_ARROW
#property indicator_color5 clrSaddleBrown
#property indicator_width5 1
//
#define peaksGoldenZoneBufferIndex 5
double peaksGoldenZoneBuffer[];
//
#property indicator_label6 "X121 PEAKGZ"
#property indicator_type6 DRAW_LINE
#property indicator_color6 clrGold
#property indicator_style6 STYLE_DOT
#property indicator_width6 1
//
#define valesGoldenZoneBufferIndex 6
double valesGoldenZoneBuffer[];
//
#property indicator_label7 "X121 VALEGZ"
#property indicator_type7 DRAW_LINE
#property indicator_color7 clrGold
#property indicator_style7 STYLE_DOT
#property indicator_width7 1
//
#define supportsBufferIndex 7
double supportsBuffer[];
//
#property indicator_label8 "X121 SUPRT"
#property indicator_type8 DRAW_ARROW
#property indicator_color8 clrLime
#property indicator_width8 1
//
#define resistancesBufferIndex 8
double resistancesBuffer[];
//
#property indicator_label9 "X121 RESIS"
#property indicator_type9 DRAW_ARROW
#property indicator_color9 clrRed
#property indicator_width9 1
//
// Trend ...
#define trendBufferIndex 9
double trendBuffer[];
#define trendColorBufferIndex 10
double trendColorBuffer[];
//
#property indicator_label10 "X121 TRND"
#property indicator_type10 DRAW_COLOR_ARROW
#property indicator_color10 CLR_NONE, clrAqua, clrMagenta, clrGray
#property indicator_width10 2
//
// Data Buffers ...
//
int mLastBufferIndex = 10;
//
// CURRENT ...
//
#define cHHBufferIndex mLastBufferIndex + 1
double cHHBuffer[];
//
#define cLLBufferIndex mLastBufferIndex + 2
double cLLBuffer[];
//
// SHORT ...
//
#define sHHBufferIndex mLastBufferIndex + 3
double sHHBuffer[];
//
#define sLLBufferIndex mLastBufferIndex + 4
double sLLBuffer[];
//
// MEDIUM ...
//
#define mHHBufferIndex mLastBufferIndex + 5
double mHHBuffer[];
//
#define mLLBufferIndex mLastBufferIndex + 6
double mLLBuffer[];
//
// LONG ...
//
#define lHHBufferIndex mLastBufferIndex + 7
double lHHBuffer[];
//
#define lLLBufferIndex mLastBufferIndex + 8
double lLLBuffer[];
//
// HIND ...
//
#define hHHBufferIndex mLastBufferIndex + 9
double hHHBuffer[];
//
#define hLLBufferIndex mLastBufferIndex + 10
double hLLBuffer[];
//
// Support and Resistances ...
//
#define fractalsUpperBufferIndex mLastBufferIndex + 11
double fractalsUpperBuffer[];
//
#define fractalsLowerBufferIndex mLastBufferIndex + 12
double fractalsLowerBuffer[];
//
#define trendStateBufferIndex mLastBufferIndex + 13
double trendStateBuffer[];
//
// Variables, Properties and etc ...
//
// this counts Available Bars ...
int limit;
//
int maxLength;
//
// SAR Handler ...
int sarHandler = INVALID_HANDLE;
//
// FRACTALS Handler ...
int fractalHandler = INVALID_HANDLE;
//
// XMarketCycle sc;
int mSCLength = 0;
ENUM_TIMEFRAMES mSCPeriod = NULL;
//
// XMarketCycle mc;
int mMCLength = 0;
ENUM_TIMEFRAMES mMCPeriod = NULL;
//
// XMarketCycle lc;
int mLCLength = 0;
ENUM_TIMEFRAMES mLCPeriod = NULL;
//
// XMarketCycle hc;
int mHCLength = 0;
ENUM_TIMEFRAMES mHCPeriod = NULL;
//
// Event Handlers ...
/**
* Initialize Indicator ...
*
* @return ( int )
*/
int OnInit()
{
//
// Validate Inputs ...
if (!ValidateInputs())
{
return INIT_PARAMETERS_INCORRECT;
}
//
// Initialize Indicator Handlers ...
//
// SAR ...
sarHandler = iSAR(
_Symbol,
_Period,
sarStep,
sarMax //
);
bool isInited = sarHandler != INVALID_HANDLE;
if (!isInited)
{
return INIT_FAILED;
}
//
// Fractal ...
fractalHandler = iFractals(
_Symbol,
_Period //
);
isInited = fractalHandler != INVALID_HANDLE;
if (!isInited)
{
return INIT_FAILED;
}
//
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();
//
// 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
//
IndicatorRelease(sarHandler);
IndicatorRelease(fractalHandler);
}
/**
* 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 ...
//
// SAR ...
int sarCalculatedBars = BarsCalculated(sarHandler);
//
// FRACTALS ...
int fractalsCalculatedBars = BarsCalculated(fractalHandler);
//
bool isPassedRequiredCalculatedBars =
//
// SAR ...
sarCalculatedBars >= maxLength &&
//
// FRACTALS ...
fractalsCalculatedBars >= maxLength
//
;
if (!isPassedRequiredCalculatedBars)
{
return prev_calculated;
}
//
limit = (prev_calculated > rates_total || prev_calculated <= 0)
? rates_total
: (rates_total - prev_calculated) + 1;
//
// Buffers Copy ...
//
// SAR ...
int copiedSars = CopyBuffer(sarHandler, 0, 0, limit, sarBuffer);
//
// FRACTALS ...
int copiedFractalsUppers = CopyBuffer(fractalHandler, UPPER_LINE, 0, limit + 1, fractalsUpperBuffer);
int copiedFractalsLowers = CopyBuffer(fractalHandler, LOWER_LINE, 0, limit + 1, fractalsLowerBuffer);
//
// Validate Copied Items ...
bool isPassedRequiredCopiedItems =
//
// SAR ...
copiedSars > 0 &&
//
// FRACTALS ...
copiedFractalsUppers > 0 &&
copiedFractalsLowers > 0
//
;
if (!isPassedRequiredCopiedItems)
{
return prev_calculated;
}
//
// 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;
}
//
// Functions ...
/**
* Validate Input Args for Initialization ...
*
* @return ( bool )
*/
bool ValidateInputs()
{
//
bool result = false;
//
result =
//
// SAR ...
sarMax > 0 &&
sarStep > 0 &&
swingLength > 0 &&
sarMax > sarStep &&
//
// XPV ...
(IsValid(scMethod, scPeriod) &&
IsValid(mcMethod, mcPeriod) &&
IsValid(lcMethod, lcPeriod) &&
IsValid(hcMethod, hcPeriod))
//
;
//
return result;
}
/**
* Extract Max Length of Inputs ...
*
* @return ( int )
*/
int ExtractMaxLengthOfInputs()
{
//
int result = 0;
//
result = MathMax(0, swingLength);
//
return result;
}
/**
* Define Required Buffers ...
*/
void DefineBuffers()
{
//
// Plot Buffers ...
//
// PEAKS ...
//
ENUM_DRAW_TYPE peaksDrawType = showPeaks ? DRAW_ARROW : DRAW_NONE;
//
ArraySetAsSeries(peaksBuffer, true);
SetIndexBuffer(peaksBufferIndex, peaksBuffer, INDICATOR_DATA);
//
PlotIndexSetInteger(peaksBufferIndex, PLOT_SHOW_DATA, showPeaks);
PlotIndexSetInteger(peaksBufferIndex, PLOT_DRAW_TYPE, peaksDrawType);
//
PlotIndexSetDouble(peaksBufferIndex, PLOT_EMPTY_VALUE, EMPTY_VALUE);
PlotIndexSetInteger(peaksBufferIndex, PLOT_ARROW, peaksArrowCode);
//
// VALES ...
//
ENUM_DRAW_TYPE valesDrawType = showVales ? DRAW_ARROW : DRAW_NONE;
//
ArraySetAsSeries(valesBuffer, true);
SetIndexBuffer(valesBufferIndex, valesBuffer, INDICATOR_DATA);
//
PlotIndexSetInteger(valesBufferIndex, PLOT_SHOW_DATA, showVales);
PlotIndexSetInteger(valesBufferIndex, PLOT_DRAW_TYPE, valesDrawType);
//
PlotIndexSetDouble(valesBufferIndex, PLOT_EMPTY_VALUE, EMPTY_VALUE);
PlotIndexSetInteger(valesBufferIndex, PLOT_ARROW, valesArrowCode);
//
// SAR ...
//
ENUM_DRAW_TYPE sarDrawType = showSar ? DRAW_ARROW : DRAW_NONE;
//
ArraySetAsSeries(sarBuffer, true);
SetIndexBuffer(sarBufferIndex, sarBuffer, INDICATOR_DATA);
//
PlotIndexSetInteger(sarBufferIndex, PLOT_SHOW_DATA, showSar);
PlotIndexSetInteger(sarBufferIndex, PLOT_DRAW_TYPE, sarDrawType);
//
PlotIndexSetDouble(sarBufferIndex, PLOT_EMPTY_VALUE, EMPTY_VALUE);
PlotIndexSetInteger(sarBufferIndex, PLOT_ARROW, sarArrowCode);
//
// Swing Lows ...
//
ENUM_DRAW_TYPE swingLowsDrawType = showSwingLows ? DRAW_ARROW : DRAW_NONE;
//
ArraySetAsSeries(swingLowsBuffer, true);
SetIndexBuffer(swingLowsBufferIndex, swingLowsBuffer, INDICATOR_DATA);
//
PlotIndexSetInteger(swingLowsBufferIndex, PLOT_SHOW_DATA, showSwingLows);
PlotIndexSetInteger(swingLowsBufferIndex, PLOT_DRAW_TYPE, swingLowsDrawType);
//
PlotIndexSetDouble(swingLowsBufferIndex, PLOT_EMPTY_VALUE, emptyValue);
PlotIndexSetInteger(swingLowsBufferIndex, PLOT_ARROW, swingLowsArrowCode);
//
// Swing Highs ...
//
ENUM_DRAW_TYPE swingHighsDrawType = showSwingHighs ? DRAW_ARROW : DRAW_NONE;
//
ArraySetAsSeries(swingHighsBuffer, true);
SetIndexBuffer(swingHighsBufferIndex, swingHighsBuffer, INDICATOR_DATA);
//
PlotIndexSetInteger(swingHighsBufferIndex, PLOT_SHOW_DATA, showSwingHighs);
PlotIndexSetInteger(swingHighsBufferIndex, PLOT_DRAW_TYPE, swingHighsDrawType);
//
PlotIndexSetDouble(swingHighsBufferIndex, PLOT_EMPTY_VALUE, emptyValue);
PlotIndexSetInteger(swingHighsBufferIndex, PLOT_ARROW, swingHighsArrowCode);
//
// Support ...
//
ENUM_DRAW_TYPE supportsDrawType = showSupports ? DRAW_ARROW : DRAW_NONE;
//
ArraySetAsSeries(supportsBuffer, true);
SetIndexBuffer(supportsBufferIndex, supportsBuffer, INDICATOR_DATA);
//
PlotIndexSetInteger(supportsBufferIndex, PLOT_SHOW_DATA, showSupports);
PlotIndexSetInteger(supportsBufferIndex, PLOT_DRAW_TYPE, supportsDrawType);
//
PlotIndexSetDouble(supportsBufferIndex, PLOT_EMPTY_VALUE, EMPTY_VALUE);
PlotIndexSetInteger(supportsBufferIndex, PLOT_ARROW, supportsArrowCode);
//
// Resistances...
//
ENUM_DRAW_TYPE resistancesDrawType = showResistances ? DRAW_ARROW : DRAW_NONE;
//
ArraySetAsSeries(resistancesBuffer, true);
SetIndexBuffer(resistancesBufferIndex, resistancesBuffer, INDICATOR_DATA);
//
PlotIndexSetInteger(resistancesBufferIndex, PLOT_SHOW_DATA, showResistances);
PlotIndexSetInteger(resistancesBufferIndex, PLOT_DRAW_TYPE, resistancesDrawType);
//
PlotIndexSetDouble(resistancesBufferIndex, PLOT_EMPTY_VALUE, EMPTY_VALUE);
PlotIndexSetInteger(resistancesBufferIndex, PLOT_ARROW, resistancesArrowCode);
//
// Golden Zones ...
//
ENUM_DRAW_TYPE goldenZonesDrawType = showGoldenZones ? DRAW_LINE : DRAW_NONE;
//
// PEAK Golden Zone ...
//
ArraySetAsSeries(peaksGoldenZoneBuffer, true);
SetIndexBuffer(peaksGoldenZoneBufferIndex, peaksGoldenZoneBuffer, INDICATOR_DATA);
//
PlotIndexSetInteger(peaksGoldenZoneBufferIndex, PLOT_SHOW_DATA, showGoldenZones);
PlotIndexSetInteger(peaksGoldenZoneBufferIndex, PLOT_DRAW_TYPE, goldenZonesDrawType);
//
// VALE Golden Zone ...
//
ArraySetAsSeries(valesGoldenZoneBuffer, true);
SetIndexBuffer(valesGoldenZoneBufferIndex, valesGoldenZoneBuffer, INDICATOR_DATA);
//
PlotIndexSetInteger(valesGoldenZoneBufferIndex, PLOT_SHOW_DATA, showGoldenZones);
PlotIndexSetInteger(valesGoldenZoneBufferIndex, PLOT_DRAW_TYPE, goldenZonesDrawType);
//
// Trends ...
//
ENUM_DRAW_TYPE trendDrawType = showTrend ? DRAW_COLOR_ARROW : DRAW_NONE;
//
ArraySetAsSeries(trendBuffer, true);
SetIndexBuffer(trendBufferIndex, trendBuffer, INDICATOR_DATA);
//
PlotIndexSetInteger(trendBufferIndex, PLOT_SHOW_DATA, showTrend);
PlotIndexSetInteger(trendBufferIndex, PLOT_DRAW_TYPE, trendDrawType);
//
PlotIndexSetDouble(trendBufferIndex, PLOT_EMPTY_VALUE, emptyValue);
PlotIndexSetInteger(trendBufferIndex, PLOT_ARROW, trendArrowCode);
//
ArraySetAsSeries(trendColorBuffer, true);
SetIndexBuffer(trendColorBufferIndex, trendColorBuffer, INDICATOR_COLOR_INDEX);
//
// Data Buffers ...
//
// CURRENT ...
//
ArraySetAsSeries(cHHBuffer, true);
SetIndexBuffer(cHHBufferIndex, cHHBuffer, INDICATOR_CALCULATIONS);
//
ArraySetAsSeries(cLLBuffer, true);
SetIndexBuffer(cLLBufferIndex, cLLBuffer, INDICATOR_CALCULATIONS);
//
// SHORT ...
//
ArraySetAsSeries(sHHBuffer, true);
SetIndexBuffer(sHHBufferIndex, sHHBuffer, INDICATOR_CALCULATIONS);
//
ArraySetAsSeries(sLLBuffer, true);
SetIndexBuffer(sLLBufferIndex, sLLBuffer, INDICATOR_CALCULATIONS);
//
// MEDIUM ...
//
ArraySetAsSeries(mHHBuffer, true);
SetIndexBuffer(mHHBufferIndex, mHHBuffer, INDICATOR_CALCULATIONS);
//
ArraySetAsSeries(mLLBuffer, true);
SetIndexBuffer(mLLBufferIndex, mLLBuffer, INDICATOR_CALCULATIONS);
//
// LONG ...
//
ArraySetAsSeries(lHHBuffer, true);
SetIndexBuffer(lHHBufferIndex, lHHBuffer, INDICATOR_CALCULATIONS);
//
ArraySetAsSeries(lLLBuffer, true);
SetIndexBuffer(lLLBufferIndex, lLLBuffer, INDICATOR_CALCULATIONS);
//
// HIND ...
//
ArraySetAsSeries(hHHBuffer, true);
SetIndexBuffer(hHHBufferIndex, hHHBuffer, INDICATOR_CALCULATIONS);
//
ArraySetAsSeries(hLLBuffer, true);
SetIndexBuffer(hLLBufferIndex, hLLBuffer, INDICATOR_CALCULATIONS);
//
// FRACTALS ...
//
ArraySetAsSeries(fractalsUpperBuffer, true);
SetIndexBuffer(fractalsUpperBufferIndex, fractalsUpperBuffer, INDICATOR_CALCULATIONS);
//
ArraySetAsSeries(fractalsLowerBuffer, true);
SetIndexBuffer(fractalsLowerBufferIndex, fractalsLowerBuffer, INDICATOR_CALCULATIONS);
//
ArraySetAsSeries(trendStateBuffer, true);
SetIndexBuffer(trendStateBufferIndex, trendStateBuffer, INDICATOR_CALCULATIONS);
}
/**
* Set Indicator Short Name and also we can define Buffers Labels ...
*/
void SetIndicatorName()
{
//
IndicatorSetInteger(INDICATOR_DIGITS, 2);
IndicatorSetString(INDICATOR_SHORTNAME, ShortName);
}
/**
* Calculate Custom Buffers ...
*
* @param bar_index: 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 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 barsLimit = startCalculationForLastBars > 0
? startCalculationForLastBars
: 0;
//
// bool canCalculate = true;
bool canCalculate =
barsLimit == 0 ||
bar_index <= barsLimit;
if (canCalculate)
{
//
// Calculate Cycles ...
CalculateCycles(bar_index);
//
// Calculate Peaks and Vales ...
CalculatePeaksAndVales(bar_index);
//
// Calculate Support nad Resistance ...
CalculateSupportAndResistance(
bar_index,
prevCalculated,
ratesTotal,
open,
high,
close,
low,
tickVolume //
);
//
// Calcultae Swing High and Low and Trends ...
CalculateSwingsAndTrend(
bar_index,
prevCalculated,
ratesTotal,
open,
high,
close,
low,
tickVolume //
);
}
else
{
FillBuffersZero(bar_index);
}
}
//
// Custom ...
/**
* Initial Market Cycles ...
*
* @return ( bool )
*/
bool InitMarketCycles()
{
//
bool result = false;
//
int cPeriodSeconds = PeriodSeconds(_Period);
//
// Find Cycle Period ...
if (scMethod == X_PERIOD_AUTO)
{
//
// Select Period ...
mSCPeriod = GetCyclePeriod(
X_MARKET_CYCLE_SHORT,
_Period //
);
}
else
{
mSCPeriod = scPeriod;
}
//
if (IsValid(mSCPeriod))
{
mSCLength = PeriodSeconds(mSCPeriod) / cPeriodSeconds;
}
//
// Find Cycle Period ...
if (mcMethod == X_PERIOD_AUTO)
{
//
// Select Period ...
mMCPeriod = GetCyclePeriod(
X_MARKET_CYCLE_MEDIUM,
_Period //
);
}
else
{
mMCPeriod = mcPeriod;
}
//
if (IsValid(mMCPeriod))
{
mMCLength = PeriodSeconds(mMCPeriod) / cPeriodSeconds;
}
//
// Find Cycle Period ...
if (lcMethod == X_PERIOD_AUTO)
{
//
// Select Period ...
mLCPeriod = GetCyclePeriod(
X_MARKET_CYCLE_LONG,
_Period //
);
}
else
{
mLCPeriod = lcPeriod;
}
//
if (IsValid(mLCPeriod))
{
mLCLength = PeriodSeconds(mLCPeriod) / cPeriodSeconds;
}
//
// Find Cycle Period ...
if (hcMethod == X_PERIOD_AUTO)
{
//
// Select Period ...
mHCPeriod = GetCyclePeriod(
X_MARKET_CYCLE_HIND,
_Period //
);
}
else
{
mHCPeriod = hcPeriod;
}
//
if (IsValid(mHCPeriod))
{
mHCLength = PeriodSeconds(mHCPeriod) / cPeriodSeconds;
}
//
result = mSCLength > 0 &&
mMCLength > mSCLength &&
mLCLength > mMCLength &&
mHCLength > mLCLength;
//
return result;
}
/**
* Fill All Bufers to Zero Vlue for Specified Bar Index ...
*
* @param barIndex: Integer ...
*/
void FillBuffersZero(int barIndex)
{
//
// CURRENT ...
cHHBuffer[barIndex] = EMPTY_VALUE;
cLLBuffer[barIndex] = EMPTY_VALUE;
//
// SHORT ...
sHHBuffer[barIndex] = EMPTY_VALUE;
sLLBuffer[barIndex] = EMPTY_VALUE;
//
// MEDIUM ...
mHHBuffer[barIndex] = EMPTY_VALUE;
mLLBuffer[barIndex] = EMPTY_VALUE;
//
// LONG ...
lHHBuffer[barIndex] = EMPTY_VALUE;
lLLBuffer[barIndex] = EMPTY_VALUE;
//
// HIND ...
hHHBuffer[barIndex] = EMPTY_VALUE;
hLLBuffer[barIndex] = EMPTY_VALUE;
//
// PEAKS ...
peaksBuffer[barIndex] = EMPTY_VALUE;
//
// VALES ...
valesBuffer[barIndex] = EMPTY_VALUE;
//
// SARS ...
sarBuffer[barIndex] = EMPTY_VALUE;
//
// Support and Resistances ...
supportsBuffer[barIndex] = EMPTY_VALUE;
resistancesBuffer[barIndex] = EMPTY_VALUE;
//
trendBuffer[barIndex] = emptyValue;
swingLowsBuffer[barIndex] = emptyValue;
swingHighsBuffer[barIndex] = emptyValue;
trendStateBuffer[barIndex] = emptyValue;
trendColorBuffer[barIndex] = hideColorIDX;
//
// Fractals ...
fractalsUpperBuffer[barIndex] = EMPTY_VALUE;
fractalsLowerBuffer[barIndex] = EMPTY_VALUE;
}
/**
* Calculate Specified Market Cycle Info ...
*
* @param barIndex: Integer, Bar Index ...
* @param cycle: ENUM_X_MARKET_CYCLES member, Specified Cycle ...
* @param hhBuffer: Highest High Buffer Reference ...
* @param llBuffer: Lowest Low Buffer Reference ...
*/
void CalculateCycle(
int barIndex,
ENUM_X_MARKET_CYCLES cycle,
double &hhBuffer[],
double &llBuffer[] //
)
{
//
XOHCL bar;
bool isBarInited = bar.Init(
_Symbol,
_Period,
barIndex
//
);
if (!isBarInited)
{
return;
}
//
int mLength = 0;
switch (cycle)
{
//
case X_MARKET_CYCLE_SHORT:
mLength = mSCLength;
break;
//
case X_MARKET_CYCLE_MEDIUM:
mLength = mMCLength;
break;
//
case X_MARKET_CYCLE_LONG:
mLength = mLCLength;
break;
//
case X_MARKET_CYCLE_HIND:
mLength = mHCLength;
break;
}
//
if (mLength == 0)
{
return;
}
//
// Find Highest High ...
double hhValue = bar.FindHighest(
mLength,
hhMode
//
);
hhBuffer[barIndex] = hhValue;
//
// Find Lowest Low ...
double llValue = bar.FindLowest(
mLength,
llMode
//
);
llBuffer[barIndex] = llValue;
}
/**
* Claculate Cycles ...
*
* @param barIndex: Integer, Bar Index ...
*/
void CalculateCycles(int barIndex)
{
//
// SHORT ...
CalculateCycle(
barIndex,
X_MARKET_CYCLE_SHORT,
sHHBuffer,
sLLBuffer //
);
//
// MEDIUM ...
CalculateCycle(
barIndex,
X_MARKET_CYCLE_MEDIUM,
mHHBuffer,
mLLBuffer //
);
//
// LONG ...
CalculateCycle(
barIndex,
X_MARKET_CYCLE_LONG,
lHHBuffer,
lLLBuffer //
);
//
// HIND ...
CalculateCycle(
barIndex,
X_MARKET_CYCLE_HIND,
hHHBuffer,
hLLBuffer //
);
}
/**
* Calculate Peaks and Vales ...
*
* @param barIndex: Integer, Bar Index ...
*/
void CalculatePeaksAndVales(int barIndex)
{
//
int lastIndex = barIndex + 1;
int barsCount = iBars(_Symbol, _Period);
//
// PEAKS ...
double lastPeak =
lastIndex < barsCount
? peaksBuffer[lastIndex]
: 0;
//
double isHH = sHHBuffer[barIndex];
double imHH = mHHBuffer[barIndex];
double ilHH = lHHBuffer[barIndex];
double ihHH = hHHBuffer[barIndex];
//
double iHHs[4] = {
isHH,
imHH,
ilHH,
ihHH //
};
double rValue = GetAverage(iHHs);
bool isPeak = rValue == isHH &&
isHH == imHH &&
imHH == ilHH &&
ilHH == ihHH;
double iPeak =
isPeak
? rValue
: lastPeak;
peaksBuffer[barIndex] = iPeak;
//
// VALES ...
double lastVale =
lastIndex < barsCount
? valesBuffer[lastIndex]
: 0;
//
double isLL = sLLBuffer[barIndex];
double imLL = mLLBuffer[barIndex];
double ilLL = lLLBuffer[barIndex];
double ihLL = hLLBuffer[barIndex];
//
double iLLs[4] = {
isLL,
imLL,
ilLL,
ihLL //
};
double sValue = GetAverage(iLLs);
bool isVale = sValue == isLL &&
isLL == imLL &&
imLL == ilLL &&
ilLL == ihLL;
double iVale =
isVale
? sValue
: lastVale;
valesBuffer[barIndex] = iVale;
//
// Peaks Golden Zone ...
double iPeakGoldenZone = GetFibonacciLevel(
iPeak,
iVale,
goldenZoneLevel,
X_DIRECTION_BULLISH //
);
peaksGoldenZoneBuffer[barIndex] = iPeakGoldenZone;
//
// Vales Golden Zone ...
double iValeGoldenZone = GetFibonacciLevel(
iPeak,
iVale,
goldenZoneLevel,
X_DIRECTION_BEARISH //
);
valesGoldenZoneBuffer[barIndex] = iValeGoldenZone;
//
}
/**
* Calculate Support nad Resistances ...
*
* @param bar_index: 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 CalculateSupportAndResistance(
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[] //
)
{
//
double iUpperFractalsPrev = fractalsUpperBuffer[bar_index + 1];
double iLowerFractalsPrev = fractalsLowerBuffer[bar_index + 1];
//
double iHigh = high[bar_index];
double iLow = low[bar_index];
//
double iSupport = iLowerFractalsPrev != EMPTY_VALUE
? iLow
: supportsBuffer[bar_index + 1];
double iResistance = iUpperFractalsPrev != EMPTY_VALUE
? iHigh
: resistancesBuffer[bar_index + 1];
//
supportsBuffer[bar_index] = iSupport;
resistancesBuffer[bar_index] = iResistance;
}
/**
* Calculate Swing High and Low and Trend ...
*
* @param bar_index: 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 CalculateSwingsAndTrend(
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 lastBarIndex = bar_index + 1;
bool isFirstBar =
startCalculationForLastBars > 0
? bar_index == startCalculationForLastBars
: bar_index == ratesTotal;
//
double iLow = low[bar_index];
double iHigh = high[bar_index];
double iOpen = open[bar_index];
double iClose = close[bar_index];
//
// Swing Low ...
double lastSwingLow = isFirstBar
? emptyValue
: swingLowsBuffer[lastBarIndex];
bool isSwingLow = IsSwingLow(high, low, bar_index, swingLength);
double iSwingLow =
!isSwingLow
? lastSwingLow
: iLow;
swingLowsBuffer[bar_index] = iSwingLow;
//
// Swing High ...
double lastSwingHigh = isFirstBar
? emptyValue
: swingHighsBuffer[lastBarIndex];
bool isSwingHigh = IsSwingHigh(high, low, bar_index, swingLength);
double iSwingHigh =
!isSwingHigh
? lastSwingHigh
: iHigh;
swingHighsBuffer[bar_index] = iSwingHigh;
//
// Calculate Trend ...
//
double lastTrend = isFirstBar
? emptyValue
: trendBuffer[lastBarIndex];
double iTrend =
isSwingLow &&
iSwingLow > lastSwingLow
? iSwingLow
: isSwingHigh &&
iSwingHigh < lastSwingHigh
? iSwingHigh
: lastTrend;
trendBuffer[bar_index] = iTrend;
//
double lastTrendState = isFirstBar
? emptyValue
: trendStateBuffer[lastBarIndex];
double iTrendState =
isSwingLow &&
iSwingLow > lastSwingLow
? 1
: isSwingHigh &&
iSwingHigh < lastSwingHigh
? -1
: lastTrendState;
//
bool isTrendBraked = false;
if (!isTrendBraked)
{
//
bool isBullishState = iTrendState > 0;
bool isBearishState = iTrendState < 0;
//
isTrendBraked = isBullishState
? iClose < iTrend
: isBearishState
? iClose > iTrend
: false;
if (isTrendBraked)
{
//
if (isBullishState)
{
iTrendState = -1;
}
//
if (isBearishState)
{
iTrendState = 1;
}
}
}
trendStateBuffer[bar_index] = iTrendState;
//
double iTrendColor = iTrendState == 0
? neuturalColorIDX
: iTrendState > 0
? bullishColorIDX
: iTrendState < 0
? bearishColorIDX
: hideColorIDX;
trendColorBuffer[bar_index] = iTrendColor;
}
//
// Tools ...
//
bool IsSwingLow(
const double &high[],
const double &low[],
int index,
int length //
)
{
//
bool result = false;
//
result = index - length > 0;
if (!result)
{
return result;
}
//
for (int i = 1; i <= length; i++)
{
//
result = low[index] < low[index - 1] &&
low[index] < low[index + i];
if (!result)
{
break;
}
}
//
return result;
}
//
bool IsSwingHigh(
const double &high[],
const double &low[],
int index,
int length //
)
{
//
bool result = false;
//
result = index - length > 0;
if (!result)
{
return result;
}
//
for (int i = 1; i <= length; i++)
{
//
result = high[index] > high[index - 1] &&
high[index] > high[index + i];
if (!result)
{
break;
}
}
//
return result;
}
//