Files
MQL5Data/Indicators/x-saherelm.xfima.mq5
T
2025-10-29 07:07:22 +03:30

3232 lines
74 KiB
Plaintext

///////////////////////////////////////////////////////
//
// SaherElm IT Center MQL5 Indicator
// -------------------------------------------------
// Name: XFIMA
// 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 ...
//
// Definitions ...
enum ENUM_X_ZG_SEARCH_MODE
{
X_ZG_SEARCH_EXTREMUM = 0, // searching for the first extremum
X_ZG_SEARCH_PEAK = 1, // searching for the next ZigZag peak
X_ZG_SEARCH_VALE = -1 // searching for the next ZigZag vale
};
//
#define ShortName "XFIMA"
//
// Imports ...
#include "../Classes/x-saherelm.x-poi.drawer.class.mq5"
#include "../Libraries/x-saherelm.common.lib.mq5"
//
// Inputs ...
//
input group "Calculation";
//
// Short ...
input ENUM_X_PERIOD_METHOD scMethod = X_PERIOD_AUTO; // How to Find Short Period
input ENUM_TIMEFRAMES scPeriod = NULL; // Short Period
//
// Medium ...
input ENUM_X_PERIOD_METHOD mcMethod = X_PERIOD_AUTO; // How to Find Medium Period
input ENUM_TIMEFRAMES mcPeriod = NULL; // Medium Period
//
// Long ...
input ENUM_X_PERIOD_METHOD lcMethod = X_PERIOD_AUTO; // How to Find Long Period
input ENUM_TIMEFRAMES lcPeriod = NULL; // Long Period
//
// Hind ...
input ENUM_X_PERIOD_METHOD hcMethod = X_PERIOD_AUTO; // How to Find Hind Period
input ENUM_TIMEFRAMES hcPeriod = NULL; // Hind Period
//
// PV ...
input ENUM_SERIESMODE upperMode = MODE_HIGH; // Peak Method
input ENUM_SERIESMODE lowerMode = MODE_LOW; // Vale Method
//
// Others ...
input ENUM_X_FIBO_LEVELS fiboUpperZoneLevel = X_FIBO_LEVEL_382; // Upper Zone Level
input ENUM_X_FIBO_LEVELS fiboMiddleZoneLevel = X_FIBO_LEVEL_500; // Upper Zone Level
input ENUM_X_FIBO_LEVELS fiboLowerZoneLevel = X_FIBO_LEVEL_618; // Lower Zone Level
input double sarStep = 0.02; // SAR Step
input double sarMax = 0.2; // SAR Maximum
input ENUM_MA_METHOD maMethod = MODE_EMA; // MA Method
input ENUM_APPLIED_PRICE maAppliedTo = PRICE_CLOSE; // MA Applied To
input int maFastLength = 7; // Fast MA Length
input int maSlowLength = 21; // Slow MA Length
input int maBandLength = 21; // MA Band Length
input int zigzagDepth = 12; // ZigZag Depth
input int zigzagDeviation = 5; // ZigZag Deviation
input int zigzagBackStep = 3; // ZigZag Back Step
input ENUM_X_PRICE zigzagUppersMode = X_PRICE_HIGH; // ZigZag High Detect Mode
input ENUM_X_PRICE zigzagLowersMode = X_PRICE_LOW; // ZigZag Low Detect Mode
input bool hkIgnoreShadows = true; // HK Remove noises
input int hkSmoothingLength = 21; // HK Smoothing Length
input ENUM_X_MA_METHOD hkSmoothingMode = X_MA_MODE_EMA; // HK Smoothing Method
input int atrLength = 14; // ATR Length
input double atrMultiplier = 1.5; // ATR Band Multiplier
input int atrSmoothingLength = 21; // ATR Smoothing Length
input ENUM_X_PRICE atrUpperPriceType = X_PRICE_HIGH; // ATR Upper Zone Applied To
input ENUM_X_PRICE atrLowerPriceType = X_PRICE_LOW; // ATR Lower Zone Applied To
input ENUM_X_MA_METHOD atrSmoothingMode = X_MA_MODE_EMA; // ATR Smoothing Method
//
// Presentation ...
input group "Presentation";
input bool showPV = true; // Show PV
input bool showHK = true; // Show HK
input bool showSAR = true; // Show Sar
input bool showMAFast = true; // Show MA Fast
input bool showMASlow = true; // Show MA Slow
input bool showMABand = true; // Show MA Band
input bool showHKBand = true; // Show HK Band
input bool showATRBand = true; // Show ATR Band
input bool showZigZag = true; // Show ZigZag
input bool showCyclesMa = true; // Show Cycles MA
input bool showFiboZone = true; // Show Fibo Zone
//
input int startCalculationForLastBars = 1500; // Calculate Last n Bars
//
input int sarArrowCode = 159; // Parabolic Sar Arrow Code
input int peakArrowCode = 159; // Peaks Arrow Code
input int valeArrowCode = 159; // Vales 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 57
#property indicator_plots 22
//
// Plot Buffers ...
//
// Fast MA ...
//
#define maFastBufferIndex 0
#define maFastBufferPlotIndex 0
double maFastBuffer[];
//
#define maFastColorBufferIndex 1
double maFastColorBuffer[];
//
#property indicator_label1 "FastMA"
#property indicator_type1 DRAW_COLOR_LINE
#property indicator_color1 CLR_NONE, clrLime, clrLime, clrLime // clrYellow, clrOrangeRed, clrGray
#property indicator_width1 2
//
// Slow MA ...
//
#define maSlowBufferIndex 2
#define maSlowBufferPlotIndex 1
double maSlowBuffer[];
//
#define maSlowColorBufferIndex 3
double maSlowColorBuffer[];
//
#property indicator_label2 "SlowMA"
#property indicator_type2 DRAW_COLOR_LINE
#property indicator_color2 CLR_NONE, clrRed, clrRed, clrRed // clrLime, clrRed, clrGray
#property indicator_width2 2
//
// SAR ...
//
#define sarBufferIndex 4
#define sarBufferPlotIndex 2
double sarBuffer[];
//
#define sarColorBufferIndex 5
double sarColorBuffer[];
//
#property indicator_label3 "SAR"
#property indicator_type3 DRAW_COLOR_ARROW
#property indicator_color3 CLR_NONE, clrLime, clrRed, clrGray
#property indicator_width3 2
//
// PEAKS ...
//
#define peakBufferIndex 6
#define peakBufferPlotIndex 3
double peakBuffer[];
//
#property indicator_label4 "PEAK"
#property indicator_type4 DRAW_ARROW
#property indicator_color4 clrMagenta
#property indicator_width4 3
//
// VALES ...
//
#define valeBufferIndex 7
#define valeBufferPlotIndex 4
double valeBuffer[];
//
#property indicator_label5 "VALE"
#property indicator_type5 DRAW_ARROW
#property indicator_color5 clrAqua
#property indicator_width5 3
//
// FIBO Upper ...
//
#define fiboUpperBufferIndex 8
#define fiboUpperBufferPlotIndex 5
double fiboUpperBuffer[];
//
#property indicator_label6 "FIBUP"
#property indicator_type6 DRAW_LINE
#property indicator_color6 clrAqua
#property indicator_width6 2
//
// FIBO Middle ...
//
#define fiboMiddleBufferIndex 9
#define fiboMiddleBufferPlotIndex 6
double fiboMiddleBuffer[];
//
#property indicator_label7 "FIBMID"
#property indicator_type7 DRAW_LINE
#property indicator_color7 clrBrown
#property indicator_width7 2
//
// FIBO Lower ...
//
#define fiboLowerBufferIndex 10
#define fiboLowerBufferPlotIndex 7
double fiboLowerBuffer[];
//
#property indicator_label8 "FIBLO"
#property indicator_type8 DRAW_LINE
#property indicator_color8 clrMagenta
#property indicator_width8 2
//
// ZigZag ...
//
#define zigzagBufferIndex 11
#define zigzagBufferPlotIndex 8
double zigzagBuffer[];
//
#property indicator_label9 "XZG"
#property indicator_type9 DRAW_SECTION
#property indicator_color9 clrYellow
#property indicator_width9 2
//
// MA Upper ...
//
#define maBandUpperBufferIndex 12
double maBandUpperBuffer[];
//
#define maBandUpperBufferPlotIndex 9
#property indicator_label10 "UpperMA"
#property indicator_type10 DRAW_LINE
#property indicator_color10 clrLime
#property indicator_width10 2
//
// MA Lower ...
//
#define maBandLowerBufferIndex 13
double maBandLowerBuffer[];
//
#define maBandLowerBufferPlotIndex 10
#property indicator_label11 "LowerMA"
#property indicator_type11 DRAW_LINE
#property indicator_color11 clrRed
#property indicator_width11 2
//
// XHK ...
//
#define hkOpenBufferIndex 14
double hkOpenBuffer[];
//
#define hkHighBufferIndex 15
double hkHighBuffer[];
//
#define hkLowBufferIndex 16
double hkLowBuffer[];
//
#define hkCloseBufferIndex 17
double hkCloseBuffer[];
//
#define hkColorBufferIndex 18
double hkColorBuffer[];
//
#define hkBufferPlotIndex 11
#property indicator_label12 "XHK Open;XHK High;XHK Low;XHK Close"
#property indicator_type12 DRAW_COLOR_CANDLES
#property indicator_color12 CLR_NONE, clrAqua, clrMagenta, clrGray
//
#define hkBandUpperBufferIndex 19
double hkBandUpperBuffer[];
//
#define hkBandUpperBufferPlotIndex 12
#property indicator_label13 "UpperHK"
#property indicator_type13 DRAW_LINE
#property indicator_color13 clrAqua
#property indicator_width13 2
//
#define hkBandLowerBufferIndex 20
double hkBandLowerBuffer[];
//
#define hkBandLowerBufferPlotIndex 13
#property indicator_label14 "LowerHK"
#property indicator_type14 DRAW_LINE
#property indicator_color14 clrMagenta
#property indicator_width14 2
//
// PB Upper ...
//
#define pbBandUpperBufferIndex 21
double pbBandUpperBuffer[];
//
#define pbBandUpperBufferPlotIndex 14
#property indicator_label15 "UpperPB"
#property indicator_type15 DRAW_LINE
#property indicator_color15 clrRoyalBlue
#property indicator_width15 2
//
// PB Lower ...
//
#define pbBandLowerBufferIndex 22
double pbBandLowerBuffer[];
//
#define pbBandLowerBufferPlotIndex 15
#property indicator_label16 "LowerPB"
#property indicator_type16 DRAW_LINE
#property indicator_color16 clrRoyalBlue
#property indicator_width16 2
//
// ATR Upper ...
//
#define atrBandUpperBufferIndex 23
double atrBandUpperBuffer[];
//
#define atrBandUpperBufferPlotIndex 16
#property indicator_label17 "UpperATR"
#property indicator_type17 DRAW_LINE
#property indicator_color17 clrOrange
#property indicator_width17 2
//
// ATR Lower ...
//
#define atrBandLowerBufferIndex 24
double atrBandLowerBuffer[];
//
#define atrBandLowerBufferPlotIndex 17
#property indicator_label18 "LowerATR"
#property indicator_type18 DRAW_LINE
#property indicator_color18 clrOrange
#property indicator_width18 2
//
#define scMaBufferIndex 25
double scMaBuffer[];
//
#define scMaBufferPlotIndex 18
#property indicator_label19 "SCMA"
#property indicator_type19 DRAW_LINE
#property indicator_color19 clrChocolate
#property indicator_style19 STYLE_DASHDOTDOT
#property indicator_width19 2
//
#define mcMaBufferIndex 26
double mcMaBuffer[];
//
#define mcMaBufferPlotIndex 19
#property indicator_label20 "MCMA"
#property indicator_type20 DRAW_LINE
#property indicator_color20 clrChocolate
#property indicator_style20 STYLE_DASHDOTDOT
#property indicator_width20 2
//
#define lcMaBufferIndex 27
double lcMaBuffer[];
//
#define lcMaBufferPlotIndex 20
#property indicator_label21 "LCMA"
#property indicator_type21 DRAW_LINE
#property indicator_color21 clrChocolate
#property indicator_style21 STYLE_DASHDOTDOT
#property indicator_width21 2
//
#define hcMaBufferIndex 28
double hcMaBuffer[];
//
#define hcMaBufferPlotIndex 21
#property indicator_label22 "HCMA"
#property indicator_type22 DRAW_LINE
#property indicator_color22 clrChocolate
#property indicator_style22 STYLE_DASHDOTDOT
#property indicator_width22 2
//
// Data Buffers ...
//
#define mLastBufferIndex 28
//
// MA ...
//
#define maFastStateBufferIndex mLastBufferIndex + 1
double maFastStateBuffer[];
//
#define maSlowStateBufferIndex mLastBufferIndex + 2
double maSlowStateBuffer[];
//
// SAR ...
//
#define sarStateBufferIndex mLastBufferIndex + 3
double sarStateBuffer[];
//
// CYCLES ...
//
// SC ...
//
#define sHHBufferIndex mLastBufferIndex + 4
double sHHBuffer[];
//
#define sLLBufferIndex mLastBufferIndex + 5
double sLLBuffer[];
//
// MC ...
//
#define mHHBufferIndex mLastBufferIndex + 6
double mHHBuffer[];
//
#define mLLBufferIndex mLastBufferIndex + 7
double mLLBuffer[];
//
// LC ...
//
#define lHHBufferIndex mLastBufferIndex + 8
double lHHBuffer[];
//
#define lLLBufferIndex mLastBufferIndex + 9
double lLLBuffer[];
//
// HC ...
//
#define hHHBufferIndex mLastBufferIndex + 10
double hHHBuffer[];
//
#define hLLBufferIndex mLastBufferIndex + 11
double hLLBuffer[];
//
// ZigZag ...
//
#define highsBufferIndex mLastBufferIndex + 12
double highsBuffer[];
//
#define highsTimeBufferIndex mLastBufferIndex + 13
double highsTimeBuffer[];
//
#define lowsBufferIndex mLastBufferIndex + 14
double lowsBuffer[];
//
#define lowsTimeBufferIndex mLastBufferIndex + 15
double lowsTimeBuffer[];
//
// XHK Data ...
//
#define hkRawHighBufferIndex mLastBufferIndex + 16
double hkRawHighBuffer[];
//
#define hkRawOpenBufferIndex mLastBufferIndex + 17
double hkRawOpenBuffer[];
//
#define hkRawLowBufferIndex mLastBufferIndex + 18
double hkRawLowBuffer[];
//
#define hkRawCloseBufferIndex mLastBufferIndex + 19
double hkRawCloseBuffer[];
//
// XPB Data ...
//
#define pbBandRawUpperBufferIndex mLastBufferIndex + 20
double pbBandRawUpperBuffer[];
//
#define pbBandRawLowerBufferIndex mLastBufferIndex + 21
double pbBandRawLowerBuffer[];
//
// XATR Data ...
//
#define atrBufferIndex mLastBufferIndex + 22
double atrBuffer[];
//
#define atrBandRawUpperBufferIndex mLastBufferIndex + 23
double atrBandRawUpperBuffer[];
//
#define atrBandRawLowerBufferIndex mLastBufferIndex + 24
double atrBandRawLowerBuffer[];
//
// XCMA Data ...
//
#define scMaStateBufferIndex mLastBufferIndex + 25
double scMaStateBuffer[];
//
#define mcMaStateBufferIndex mLastBufferIndex + 26
double mcMaStateBuffer[];
//
#define lcMaStateBufferIndex mLastBufferIndex + 27
double lcMaStateBuffer[];
//
#define hcMaStateBufferIndex mLastBufferIndex + 28
double hcMaStateBuffer[];
//
// Variables, Properties and etc ...
//
// XMarketCycle sc;
int mSCLength = 0;
int mSCMALength = 0;
ENUM_TIMEFRAMES mSCPeriod = NULL;
//
// XMarketCycle mc;
int mMCLength = 0;
int mMCMALength = 0;
ENUM_TIMEFRAMES mMCPeriod = NULL;
//
// XMarketCycle lc;
int mLCLength = 0;
int mLCMALength = 0;
ENUM_TIMEFRAMES mLCPeriod = NULL;
//
// XMarketCycle hc;
int mHCLength = 0;
int mHCMALength = 0;
ENUM_TIMEFRAMES mHCPeriod = NULL;
//
int sarHandler = INVALID_HANDLE;
int atrHandler = INVALID_HANDLE;
int scMaHandler = INVALID_HANDLE;
int mcMaHandler = INVALID_HANDLE;
int lcMaHandler = INVALID_HANDLE;
int hcMaHandler = INVALID_HANDLE;
int maFastHandler = INVALID_HANDLE;
int maSlowHandler = INVALID_HANDLE;
int maUpperHandler = INVALID_HANDLE;
int maLowerHandler = INVALID_HANDLE;
//
int limit;
//
int maxLength;
//
int firstBarIndex;
//
double zigzagUppers[];
double zigzagLowers[];
int zigzagLowersCount = 0;
int zigzagUppersCount = 0;
int zigzagRecalc = 3; // Number of last extremes for recalculation
//
double lastZigZagPeak = 0;
datetime lastZigZagPeakTime = NULL;
//
double lastZigZagVale = 0;
datetime lastZigZagValeTime = NULL;
//
ENUM_X_MA_METHOD iPBMethod = X_MA_MODE_NONE;
//
// Event Handlers ...
/**
* Initialize Indicator ...
*
* @return ( int )
*/
int OnInit()
{
//
bool has = false;
//
// Validate Inputs ...
has = ValidateInputs();
if (!has)
{
return INIT_PARAMETERS_INCORRECT;
}
//
// Prepare Price Buffer Smoothing Method ...
iPBMethod = ToXMaMethod(maMethod);
//
has = InitCycles();
if (!has)
{
return INIT_FAILED;
}
//
// 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 ...
IndicatorRelease(sarHandler);
IndicatorRelease(atrHandler);
IndicatorRelease(scMaHandler);
IndicatorRelease(mcMaHandler);
IndicatorRelease(lcMaHandler);
IndicatorRelease(hcMaHandler);
IndicatorRelease(maFastHandler);
IndicatorRelease(maSlowHandler);
IndicatorRelease(maUpperHandler);
IndicatorRelease(maLowerHandler);
}
/**
* 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[] //
)
{
//
// Do ZigZag Calculations Before ArraySet As Series ...
int calculatedZigZags = CalculateZigZag(
0,
prev_calculated,
rates_total,
open,
high,
close,
low,
tick_volume //
);
if (calculatedZigZags != rates_total)
{
return prev_calculated;
}
//
// 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 ...
//
int scCalculatedBars = BarsCalculated(scMaHandler);
int mcCalculatedBars = BarsCalculated(mcMaHandler);
int lcCalculatedBars = BarsCalculated(lcMaHandler);
int hcCalculatedBars = BarsCalculated(hcMaHandler);
//
// MA ...
int maFastCalculatedBars = BarsCalculated(maFastHandler);
int maSlowCalculatedBars = BarsCalculated(maSlowHandler);
int maUpperCalculatedBars = BarsCalculated(maUpperHandler);
int maLowerCalculatedBars = BarsCalculated(maLowerHandler);
//
// SAR ...
int sarCalculatedBars = BarsCalculated(sarHandler);
//
// ATR ...
int atrCalculatedBars = BarsCalculated(atrHandler);
//
bool isPassedRequiredCalculatedBars =
//
// Cycles MA Handlers ...
scCalculatedBars >= maxLength &&
mcCalculatedBars >= maxLength &&
lcCalculatedBars >= maxLength &&
hcCalculatedBars >= maxLength &&
//
// MA ...
maFastCalculatedBars >= maxLength &&
maSlowCalculatedBars >= maxLength &&
maUpperCalculatedBars >= maxLength &&
maLowerCalculatedBars >= maxLength &&
//
// SAR ...
sarCalculatedBars >= maxLength &&
//
// ATR ...
atrCalculatedBars >= maxLength //
;
if (!isPassedRequiredCalculatedBars)
{
return prev_calculated;
}
//
limit = (prev_calculated > rates_total || prev_calculated <= 0)
? rates_total
: (rates_total - prev_calculated) + 1;
//
// Buffers Copy ...
//
int copiedSCs = CopyBuffer(scMaHandler, MAIN_LINE, 0, limit, scMaBuffer);
int copiedMCs = CopyBuffer(mcMaHandler, MAIN_LINE, 0, limit, mcMaBuffer);
int copiedLCs = CopyBuffer(lcMaHandler, MAIN_LINE, 0, limit, lcMaBuffer);
int copiedHCs = CopyBuffer(hcMaHandler, MAIN_LINE, 0, limit, hcMaBuffer);
//
// MA ...
int copiedMAFasts = CopyBuffer(maFastHandler, MAIN_LINE, 0, limit, maFastBuffer);
int copiedMASlows = CopyBuffer(maSlowHandler, MAIN_LINE, 0, limit, maSlowBuffer);
int copiedMAUppers = CopyBuffer(maUpperHandler, MAIN_LINE, 0, limit, maBandUpperBuffer);
int copiedMALowers = CopyBuffer(maLowerHandler, MAIN_LINE, 0, limit, maBandLowerBuffer);
//
// SAR ...
int copiedSARs = CopyBuffer(sarHandler, MAIN_LINE, 0, limit, sarBuffer);
//
// ATR ...
int copiedATRs = CopyBuffer(atrHandler, MAIN_LINE, 0, limit, atrBuffer);
//
// Validate Copied Items ...
bool isPassedRequiredCopiedItems =
//
// Cycles MA ...
copiedSCs >= limit &&
copiedMCs >= limit &&
copiedLCs >= limit &&
copiedHCs >= limit &&
//
// MA ...
copiedMAFasts >= limit &&
copiedMASlows >= limit &&
copiedMAUppers >= limit &&
copiedMALowers >= limit &&
//
// SAR ...
copiedSARs >= limit &&
//
// ATR ...
copiedATRs >= limit
//
;
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;
}
//
// 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 =
//
// MA ...
maFastLength >= 2 &&
maSlowLength >= 2 &&
maBandLength >= 2 &&
maSlowLength > maFastLength &&
//
// SAR ...
sarMax > 0 &&
sarStep > 0 &&
sarMax > sarStep &&
//
// ATR ...
atrLength > 0 &&
atrMultiplier >= 1 &&
IsXValid(atrUpperPriceType) &&
IsXValid(atrLowerPriceType) &&
//
// CYCLES ...
(IsValid(scMethod, scPeriod) &&
IsValid(mcMethod, mcPeriod) &&
IsValid(lcMethod, lcPeriod) &&
IsValid(hcMethod, hcPeriod)) &&
//
// ZIGZAG ...
zigzagDepth > 0 &&
zigzagDeviation > 0 &&
zigzagBackStep > 0 &&
//
// Validate Upper Mode Detection ...
IsXValid(zigzagUppersMode) &&
//
// Validate Lower Mode Detection ...
IsXValid(zigzagLowersMode)
//
;
//
return result;
}
/**
* Extract Max Length of Inputs ...
*
* @return ( int )
*/
int ExtractMaxLengthOfInputs()
{
//
int result = 0;
//
result = MathMax(mSCLength, mMCLength);
result = MathMax(result, mLCLength);
result = MathMax(result, mHCLength);
result = MathMax(result, maFastLength);
result = MathMax(result, maSlowLength);
result = MathMax(result, maBandLength);
result = MathMax(result, atrLength);
//
return result;
}
/**
* Define Required Buffers ...
*/
void DefineBuffers()
{
//
// Fast MA ...
//
ArraySetAsSeries(maFastBuffer, true);
SetIndexBuffer(maFastBufferIndex, maFastBuffer, INDICATOR_DATA);
//
PlotIndexSetInteger(maFastBufferPlotIndex, PLOT_SHOW_DATA, showMAFast);
PlotIndexSetDouble(maFastBufferPlotIndex, PLOT_EMPTY_VALUE, emptyValue);
//
ArraySetAsSeries(maFastColorBuffer, true);
SetIndexBuffer(maFastColorBufferIndex, maFastColorBuffer, INDICATOR_COLOR_INDEX);
//
// Slow MA ...
//
ArraySetAsSeries(maSlowBuffer, true);
SetIndexBuffer(maSlowBufferIndex, maSlowBuffer, INDICATOR_DATA);
//
PlotIndexSetInteger(maSlowBufferPlotIndex, PLOT_SHOW_DATA, showMASlow);
PlotIndexSetDouble(maSlowBufferPlotIndex, PLOT_EMPTY_VALUE, emptyValue);
//
ArraySetAsSeries(maSlowColorBuffer, true);
SetIndexBuffer(maSlowColorBufferIndex, maSlowColorBuffer, INDICATOR_COLOR_INDEX);
//
// SAR ...
//
ArraySetAsSeries(sarBuffer, true);
SetIndexBuffer(sarBufferIndex, sarBuffer, INDICATOR_DATA);
//
PlotIndexSetInteger(sarBufferPlotIndex, PLOT_SHOW_DATA, showSAR);
PlotIndexSetDouble(sarBufferPlotIndex, PLOT_EMPTY_VALUE, emptyValue);
PlotIndexSetInteger(sarBufferPlotIndex, PLOT_ARROW, sarArrowCode);
//
ArraySetAsSeries(sarColorBuffer, true);
SetIndexBuffer(sarColorBufferIndex, sarColorBuffer, INDICATOR_COLOR_INDEX);
//
// PEAK / VALE ...
//
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, emptyValue);
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, emptyValue);
PlotIndexSetInteger(valeBufferPlotIndex, PLOT_ARROW, valeArrowCode);
//
// FIBO Zone ...
//
ENUM_DRAW_TYPE fiboZoneDrawType = showFiboZone ? DRAW_LINE : DRAW_NONE;
//
// Upper ...
ArraySetAsSeries(fiboUpperBuffer, true);
SetIndexBuffer(fiboUpperBufferIndex, fiboUpperBuffer, INDICATOR_DATA);
//
PlotIndexSetInteger(fiboUpperBufferPlotIndex, PLOT_SHOW_DATA, showFiboZone);
PlotIndexSetInteger(fiboUpperBufferPlotIndex, PLOT_DRAW_TYPE, fiboZoneDrawType);
PlotIndexSetDouble(fiboUpperBufferPlotIndex, PLOT_EMPTY_VALUE, emptyValue);
//
// Middle ...
ArraySetAsSeries(fiboMiddleBuffer, true);
SetIndexBuffer(fiboMiddleBufferIndex, fiboMiddleBuffer, INDICATOR_DATA);
//
PlotIndexSetInteger(fiboMiddleBufferPlotIndex, PLOT_SHOW_DATA, showFiboZone);
PlotIndexSetInteger(fiboMiddleBufferPlotIndex, PLOT_DRAW_TYPE, fiboZoneDrawType);
PlotIndexSetDouble(fiboMiddleBufferPlotIndex, PLOT_EMPTY_VALUE, emptyValue);
//
// Lower ...
ArraySetAsSeries(fiboLowerBuffer, true);
SetIndexBuffer(fiboLowerBufferIndex, fiboLowerBuffer, INDICATOR_DATA);
//
PlotIndexSetInteger(fiboLowerBufferPlotIndex, PLOT_SHOW_DATA, showFiboZone);
PlotIndexSetInteger(fiboLowerBufferPlotIndex, PLOT_DRAW_TYPE, fiboZoneDrawType);
PlotIndexSetDouble(fiboLowerBufferPlotIndex, PLOT_EMPTY_VALUE, emptyValue);
//
// ZigZag ...
//
ENUM_DRAW_TYPE zigzagDrawType = showZigZag ? DRAW_SECTION : DRAW_NONE;
//
SetIndexBuffer(zigzagBufferIndex, zigzagBuffer, INDICATOR_DATA);
//
PlotIndexSetInteger(zigzagBufferPlotIndex, PLOT_SHOW_DATA, showZigZag);
PlotIndexSetInteger(zigzagBufferPlotIndex, PLOT_DRAW_TYPE, zigzagDrawType);
PlotIndexSetInteger(zigzagBufferPlotIndex, PLOT_DRAW_BEGIN, 0);
PlotIndexSetDouble(zigzagBufferPlotIndex, PLOT_EMPTY_VALUE, 0);
//
// MA Band ...
//
ENUM_DRAW_TYPE maBandDrawType = showMABand ? DRAW_LINE : DRAW_NONE;
//
// Upper Band ...
//
ArraySetAsSeries(maBandUpperBuffer, true);
SetIndexBuffer(maBandUpperBufferIndex, maBandUpperBuffer, INDICATOR_DATA);
//
PlotIndexSetInteger(maBandUpperBufferPlotIndex, PLOT_SHOW_DATA, showMABand);
PlotIndexSetInteger(maBandUpperBufferPlotIndex, PLOT_DRAW_TYPE, maBandDrawType);
PlotIndexSetInteger(maBandUpperBufferPlotIndex, PLOT_DRAW_BEGIN, 0);
PlotIndexSetDouble(maBandUpperBufferPlotIndex, PLOT_EMPTY_VALUE, 0);
//
// Lower Band ...
//
ArraySetAsSeries(maBandLowerBuffer, true);
SetIndexBuffer(maBandLowerBufferIndex, maBandLowerBuffer, INDICATOR_DATA);
//
PlotIndexSetInteger(maBandLowerBufferPlotIndex, PLOT_SHOW_DATA, showMABand);
PlotIndexSetInteger(maBandLowerBufferPlotIndex, PLOT_DRAW_TYPE, maBandDrawType);
PlotIndexSetInteger(maBandLowerBufferPlotIndex, PLOT_DRAW_BEGIN, 0);
PlotIndexSetDouble(maBandLowerBufferPlotIndex, PLOT_EMPTY_VALUE, 0);
//
// XPB Band ...
//
// Upper Band ...
//
ArraySetAsSeries(pbBandUpperBuffer, true);
SetIndexBuffer(pbBandUpperBufferIndex, pbBandUpperBuffer, INDICATOR_DATA);
//
PlotIndexSetInteger(pbBandUpperBufferPlotIndex, PLOT_SHOW_DATA, showMABand);
PlotIndexSetInteger(pbBandUpperBufferPlotIndex, PLOT_DRAW_TYPE, maBandDrawType);
PlotIndexSetDouble(pbBandUpperBufferPlotIndex, PLOT_EMPTY_VALUE, emptyValue);
//
// Lower Band ...
//
ArraySetAsSeries(pbBandLowerBuffer, true);
SetIndexBuffer(pbBandLowerBufferIndex, pbBandLowerBuffer, INDICATOR_DATA);
//
PlotIndexSetInteger(pbBandLowerBufferPlotIndex, PLOT_SHOW_DATA, showMABand);
PlotIndexSetInteger(pbBandLowerBufferPlotIndex, PLOT_DRAW_TYPE, maBandDrawType);
PlotIndexSetDouble(pbBandLowerBufferPlotIndex, PLOT_EMPTY_VALUE, emptyValue);
//
// XHK ...
//
// XHK Color ...
ArraySetAsSeries(hkColorBuffer, true);
SetIndexBuffer(hkColorBufferIndex, hkColorBuffer, INDICATOR_COLOR_INDEX);
//
// Set EMPTY_VALUE on XHK Bar Buffer ...
PlotIndexSetDouble(hkBufferPlotIndex, PLOT_EMPTY_VALUE, emptyValue);
PlotIndexSetInteger(hkBufferPlotIndex, PLOT_SHOW_DATA, false);
//
// Open ...
ArraySetAsSeries(hkOpenBuffer, true);
SetIndexBuffer(hkOpenBufferIndex, hkOpenBuffer, INDICATOR_DATA);
//
// High ...
ArraySetAsSeries(hkHighBuffer, true);
SetIndexBuffer(hkHighBufferIndex, hkHighBuffer, INDICATOR_DATA);
//
// Low ...
ArraySetAsSeries(hkLowBuffer, true);
SetIndexBuffer(hkLowBufferIndex, hkLowBuffer, INDICATOR_DATA);
//
// Close ...
ArraySetAsSeries(hkCloseBuffer, true);
SetIndexBuffer(hkCloseBufferIndex, hkCloseBuffer, INDICATOR_DATA);
//
// XHK Band ...
//
ENUM_DRAW_TYPE hkBandDrawType = showHKBand ? DRAW_LINE : DRAW_NONE;
//
// Upper ...
ArraySetAsSeries(hkBandUpperBuffer, true);
SetIndexBuffer(hkBandUpperBufferIndex, hkBandUpperBuffer, INDICATOR_DATA);
//
PlotIndexSetInteger(hkBandUpperBufferPlotIndex, PLOT_SHOW_DATA, showHKBand);
PlotIndexSetInteger(hkBandUpperBufferPlotIndex, PLOT_DRAW_TYPE, hkBandDrawType);
PlotIndexSetDouble(hkBandUpperBufferPlotIndex, PLOT_EMPTY_VALUE, emptyValue);
//
// Lower ...
ArraySetAsSeries(hkBandLowerBuffer, true);
SetIndexBuffer(hkBandLowerBufferIndex, hkBandLowerBuffer, INDICATOR_DATA);
//
PlotIndexSetInteger(hkBandLowerBufferPlotIndex, PLOT_SHOW_DATA, showHKBand);
PlotIndexSetInteger(hkBandLowerBufferPlotIndex, PLOT_DRAW_TYPE, hkBandDrawType);
PlotIndexSetDouble(hkBandLowerBufferPlotIndex, PLOT_EMPTY_VALUE, emptyValue);
//
// XATR Band ...
//
ENUM_DRAW_TYPE atrBandDrawType = showATRBand ? DRAW_LINE : DRAW_NONE;
//
// Upper ...
ArraySetAsSeries(atrBandUpperBuffer, true);
SetIndexBuffer(atrBandUpperBufferIndex, atrBandUpperBuffer, INDICATOR_DATA);
//
PlotIndexSetInteger(atrBandUpperBufferPlotIndex, PLOT_SHOW_DATA, showATRBand);
PlotIndexSetInteger(atrBandUpperBufferPlotIndex, PLOT_DRAW_TYPE, atrBandDrawType);
PlotIndexSetDouble(atrBandUpperBufferPlotIndex, PLOT_EMPTY_VALUE, emptyValue);
//
// Lower ...
ArraySetAsSeries(atrBandLowerBuffer, true);
SetIndexBuffer(atrBandLowerBufferIndex, atrBandLowerBuffer, INDICATOR_DATA);
//
PlotIndexSetInteger(atrBandLowerBufferPlotIndex, PLOT_SHOW_DATA, showATRBand);
PlotIndexSetInteger(atrBandLowerBufferPlotIndex, PLOT_DRAW_TYPE, atrBandDrawType);
PlotIndexSetDouble(atrBandLowerBufferPlotIndex, PLOT_EMPTY_VALUE, emptyValue);
//
// Data Buffers ...
//
ArraySetAsSeries(maFastStateBuffer, true);
SetIndexBuffer(maFastStateBufferIndex, maFastStateBuffer, INDICATOR_CALCULATIONS);
//
ArraySetAsSeries(maSlowStateBuffer, true);
SetIndexBuffer(maSlowStateBufferIndex, maSlowStateBuffer, INDICATOR_CALCULATIONS);
//
ArraySetAsSeries(sarStateBuffer, true);
SetIndexBuffer(sarStateBufferIndex, sarStateBuffer, INDICATOR_CALCULATIONS);
//
// CYCLES ...
//
// SC ...
//
ArraySetAsSeries(sHHBuffer, true);
SetIndexBuffer(sHHBufferIndex, sHHBuffer, INDICATOR_CALCULATIONS);
//
ArraySetAsSeries(sLLBuffer, true);
SetIndexBuffer(sLLBufferIndex, sLLBuffer, INDICATOR_CALCULATIONS);
//
// MC ...
//
ArraySetAsSeries(mHHBuffer, true);
SetIndexBuffer(mHHBufferIndex, mHHBuffer, INDICATOR_CALCULATIONS);
//
ArraySetAsSeries(mLLBuffer, true);
SetIndexBuffer(mLLBufferIndex, mLLBuffer, INDICATOR_CALCULATIONS);
//
// LC ...
//
ArraySetAsSeries(lHHBuffer, true);
SetIndexBuffer(lHHBufferIndex, lHHBuffer, INDICATOR_CALCULATIONS);
//
ArraySetAsSeries(lLLBuffer, true);
SetIndexBuffer(lLLBufferIndex, lLLBuffer, INDICATOR_CALCULATIONS);
//
// HC ...
//
ArraySetAsSeries(hHHBuffer, true);
SetIndexBuffer(hHHBufferIndex, hHHBuffer, INDICATOR_CALCULATIONS);
//
ArraySetAsSeries(hLLBuffer, true);
SetIndexBuffer(hLLBufferIndex, hLLBuffer, INDICATOR_CALCULATIONS);
//
// Highs ...
SetIndexBuffer(highsBufferIndex, highsBuffer, INDICATOR_CALCULATIONS);
SetIndexBuffer(highsTimeBufferIndex, highsTimeBuffer, INDICATOR_CALCULATIONS);
//
// Lows ...
SetIndexBuffer(lowsBufferIndex, lowsBuffer, INDICATOR_CALCULATIONS);
SetIndexBuffer(lowsTimeBufferIndex, lowsTimeBuffer, INDICATOR_CALCULATIONS);
//
// XHK Data ...
//
ArraySetAsSeries(hkRawHighBuffer, true);
SetIndexBuffer(hkRawHighBufferIndex, hkRawHighBuffer, INDICATOR_CALCULATIONS);
//
ArraySetAsSeries(hkRawOpenBuffer, true);
SetIndexBuffer(hkRawOpenBufferIndex, hkRawOpenBuffer, INDICATOR_CALCULATIONS);
//
ArraySetAsSeries(hkRawLowBuffer, true);
SetIndexBuffer(hkRawLowBufferIndex, hkRawLowBuffer, INDICATOR_CALCULATIONS);
//
ArraySetAsSeries(hkRawCloseBuffer, true);
SetIndexBuffer(hkRawCloseBufferIndex, hkRawCloseBuffer, INDICATOR_CALCULATIONS);
//
// XPB Data ...
//
ArraySetAsSeries(pbBandRawUpperBuffer, true);
SetIndexBuffer(pbBandRawUpperBufferIndex, pbBandRawUpperBuffer, INDICATOR_CALCULATIONS);
//
ArraySetAsSeries(pbBandRawLowerBuffer, true);
SetIndexBuffer(pbBandRawLowerBufferIndex, pbBandRawLowerBuffer, INDICATOR_CALCULATIONS);
//
// XATR Data ...
//
ArraySetAsSeries(atrBuffer, true);
SetIndexBuffer(atrBufferIndex, atrBuffer, INDICATOR_CALCULATIONS);
//
ArraySetAsSeries(atrBandRawUpperBuffer, true);
SetIndexBuffer(atrBandRawUpperBufferIndex, atrBandRawUpperBuffer, INDICATOR_CALCULATIONS);
//
ArraySetAsSeries(atrBandRawLowerBuffer, true);
SetIndexBuffer(atrBandRawLowerBufferIndex, atrBandRawLowerBuffer, INDICATOR_CALCULATIONS);
//
// MA State ...
//
// Draw Type ...
ENUM_DRAW_TYPE cyclesMADrawType = showCyclesMa ? DRAW_LINE : DRAW_NONE;
//
// SC ...
//
ArraySetAsSeries(scMaBuffer, true);
SetIndexBuffer(scMaBufferIndex, scMaBuffer, INDICATOR_DATA);
//
PlotIndexSetInteger(scMaBufferPlotIndex, PLOT_SHOW_DATA, showCyclesMa);
PlotIndexSetDouble(scMaBufferPlotIndex, PLOT_EMPTY_VALUE, emptyValue);
PlotIndexSetInteger(scMaBufferPlotIndex, PLOT_DRAW_TYPE, cyclesMADrawType);
//
ArraySetAsSeries(scMaStateBuffer, true);
SetIndexBuffer(scMaStateBufferIndex, scMaStateBuffer, INDICATOR_CALCULATIONS);
//
// SC ...
//
ArraySetAsSeries(mcMaBuffer, true);
SetIndexBuffer(mcMaBufferIndex, mcMaBuffer, INDICATOR_DATA);
//
PlotIndexSetInteger(mcMaBufferPlotIndex, PLOT_SHOW_DATA, showCyclesMa);
PlotIndexSetDouble(mcMaBufferPlotIndex, PLOT_EMPTY_VALUE, emptyValue);
PlotIndexSetInteger(mcMaBufferPlotIndex, PLOT_DRAW_TYPE, cyclesMADrawType);
//
ArraySetAsSeries(scMaStateBuffer, true);
SetIndexBuffer(mcMaStateBufferIndex, mcMaStateBuffer, INDICATOR_CALCULATIONS);
//
// LC ...
//
ArraySetAsSeries(lcMaBuffer, true);
SetIndexBuffer(lcMaBufferIndex, lcMaBuffer, INDICATOR_DATA);
//
PlotIndexSetInteger(lcMaBufferPlotIndex, PLOT_SHOW_DATA, showCyclesMa);
PlotIndexSetDouble(lcMaBufferPlotIndex, PLOT_EMPTY_VALUE, emptyValue);
PlotIndexSetInteger(lcMaBufferPlotIndex, PLOT_DRAW_TYPE, cyclesMADrawType);
//
ArraySetAsSeries(lcMaStateBuffer, true);
SetIndexBuffer(lcMaStateBufferIndex, lcMaStateBuffer, INDICATOR_CALCULATIONS);
//
// HC ...
//
ArraySetAsSeries(hcMaBuffer, true);
SetIndexBuffer(hcMaBufferIndex, hcMaBuffer, INDICATOR_DATA);
//
PlotIndexSetInteger(hcMaBufferPlotIndex, PLOT_SHOW_DATA, showCyclesMa);
PlotIndexSetDouble(hcMaBufferPlotIndex, PLOT_EMPTY_VALUE, emptyValue);
PlotIndexSetInteger(hcMaBufferPlotIndex, PLOT_DRAW_TYPE, cyclesMADrawType);
//
ArraySetAsSeries(hcMaStateBuffer, true);
SetIndexBuffer(hcMaStateBufferIndex, hcMaStateBuffer, INDICATOR_CALCULATIONS);
//
}
/**
* Initial Market Cycles ...
*
* @return ( bool )
*/
bool InitCycles()
{
//
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;
if (result)
{
//
// Initialized Cycles MA Ribons Length ...
mSCMALength = maBandLength * mSCLength;
mMCMALength = maBandLength * mMCLength;
mLCMALength = maBandLength * mLCLength;
mHCMALength = maBandLength * mHCLength;
//
result = mSCMALength > 0 &&
mMCMALength > mSCMALength &&
mLCMALength > mMCMALength &&
mHCMALength > mLCMALength;
}
//
return result;
}
/**
* Initial all Indicator Requirements ...
*
* @return ( false )
*
*/
bool InitRequirements()
{
//
bool result = false;
//
// Handlers ...
//
// Fast MA ...
maFastHandler = iMA(
_Symbol,
_Period,
maFastLength,
0,
maMethod,
maAppliedTo //
);
result = maFastHandler != INVALID_HANDLE;
if (!result)
{
return result;
}
//
// Slow MA ...
maSlowHandler = iMA(
_Symbol,
_Period,
maSlowLength,
0,
maMethod,
maAppliedTo //
);
result = maSlowHandler != INVALID_HANDLE;
if (!result)
{
return result;
}
//
// MA Band Upper ...
maUpperHandler = iMA(
_Symbol,
_Period,
maBandLength,
0,
maMethod,
PRICE_HIGH //
);
result = maUpperHandler != INVALID_HANDLE;
if (!result)
{
return result;
}
//
// MA Band Lower ...
maLowerHandler = iMA(
_Symbol,
_Period,
maBandLength,
0,
maMethod,
PRICE_LOW //
);
result = maLowerHandler != INVALID_HANDLE;
if (!result)
{
return result;
}
//
// SAR ...
sarHandler = iSAR(
_Symbol,
_Period,
sarStep,
sarMax //
);
result = sarHandler != INVALID_HANDLE;
if (!result)
{
return result;
}
//
// ATR ...
atrHandler = iATR(
_Symbol,
_Period,
atrLength //
);
result = atrHandler != INVALID_HANDLE;
if (!result)
{
return result;
}
//
// Cycles MA Handler ...
//
// SC ...
scMaHandler = iMA(
_Symbol,
_Period,
mSCMALength,
0,
maMethod,
maAppliedTo //
);
result = scMaHandler != INVALID_HANDLE;
if (!result)
{
return result;
}
//
// MC ...
mcMaHandler = iMA(
_Symbol,
_Period,
mMCMALength,
0,
maMethod,
maAppliedTo //
);
result = mcMaHandler != INVALID_HANDLE;
if (!result)
{
return result;
}
//
// LC ...
lcMaHandler = iMA(
_Symbol,
_Period,
mLCMALength,
0,
maMethod,
maAppliedTo //
);
result = lcMaHandler != INVALID_HANDLE;
if (!result)
{
return result;
}
//
// HC ...
hcMaHandler = iMA(
_Symbol,
_Period,
mHCMALength,
0,
maMethod,
maAppliedTo //
);
result = hcMaHandler != INVALID_HANDLE;
if (!result)
{
return result;
}
//
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 ...
}
/**
* 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[] //
)
{
//
int lastBarIndex = barIndex + 1;
int maxBarIndex = startCalculationForLastBars > 0
? startCalculationForLastBars
: ratesTotal;
bool isFirstBar =
startCalculationForLastBars > 0
? barIndex == startCalculationForLastBars
: barIndex == firstBarIndex;
//
// Common Requirements ...
//
// CYCLES Ranges ...
CalculateCycleRanges(
barIndex,
prevCalculated,
ratesTotal,
open,
high,
close,
low,
tickVolume //
);
//
// PV ...
CalculatePV(
barIndex,
prevCalculated,
ratesTotal,
open,
high,
close,
low,
tickVolume //
);
//
// XHK ...
CalculateHKBar(
barIndex,
prevCalculated,
ratesTotal,
open,
high,
close,
low,
tickVolume //
);
//
// XATR ...
CalculateATR(
barIndex,
prevCalculated,
ratesTotal,
open,
high,
close,
low,
tickVolume //
);
//
// Cycles MA Calculation ...
CalculateCyclesMA(
barIndex,
prevCalculated,
ratesTotal,
open,
high,
close,
low,
tickVolume //
);
//
// Fast MA ...
double iFastMa = maFastBuffer[barIndex];
double maFastState = close[barIndex] > iFastMa
? bullishState
: close[barIndex] < iFastMa
? bearishState
: neuturalState;
double maFastColor = close[barIndex] > iFastMa
? bullishColorIDX
: close[barIndex] < iFastMa
? bearishColorIDX
: neuturalColorIDX;
maFastStateBuffer[barIndex] = maFastState;
maFastColorBuffer[barIndex] = showMAFast
? maFastColor
: hideColorIDX;
//
// Slow MA ...
double iSlowMa = maSlowBuffer[barIndex];
double maSlowState = close[barIndex] > iSlowMa
? bullishState
: close[barIndex] < iSlowMa
? bearishState
: neuturalState;
double maSlowColor = close[barIndex] > iSlowMa
? bullishColorIDX
: close[barIndex] < iSlowMa
? bearishColorIDX
: neuturalColorIDX;
maSlowStateBuffer[barIndex] = maSlowState;
maSlowColorBuffer[barIndex] = showMASlow
? maSlowColor
: hideColorIDX;
//
// SAR ...
double iSar = sarBuffer[barIndex];
double sarState = low[barIndex] > iSar
? bullishState
: high[barIndex] < iSar
? bearishState
: neuturalState;
double sarColor = low[barIndex] > iSar
? bullishColorIDX
: high[barIndex] < iSar
? bearishColorIDX
: neuturalColorIDX;
sarStateBuffer[barIndex] = sarState;
sarColorBuffer[barIndex] = showSAR
? sarColor
: hideColorIDX;
//
// FIBO Zone ...
//
double iPeak = peakBuffer[barIndex];
double iVale = valeBuffer[barIndex];
//
double iFiboUp = GetFibonacciLevel(
iPeak,
iVale,
fiboUpperZoneLevel,
X_DIRECTION_BULLISH //
);
//
double iFiboMid = GetFibonacciLevel(
iPeak,
iVale,
fiboMiddleZoneLevel,
X_DIRECTION_BULLISH //
);
//
double iFiboLo = GetFibonacciLevel(
iPeak,
iVale,
fiboLowerZoneLevel,
X_DIRECTION_BULLISH //
);
//
fiboUpperBuffer[barIndex] = iFiboUp;
fiboMiddleBuffer[barIndex] = iFiboMid;
fiboLowerBuffer[barIndex] = iFiboLo;
//
// XPB ...
//
double iPBUpper = MathMax(close[barIndex], open[barIndex]);
double iPBLower = MathMin(close[barIndex], open[barIndex]);
//
pbBandUpperBuffer[barIndex] = iPBUpper;
pbBandRawUpperBuffer[barIndex] = iPBUpper;
//
pbBandLowerBuffer[barIndex] = iPBLower;
pbBandRawLowerBuffer[barIndex] = iPBLower;
//
if (IsXValid(iPBMethod) && maBandLength > 0)
{
//
// XPB Upper ...
iMAOnBuffer(
ratesTotal,
prevCalculated,
barIndex,
maBandLength,
pbBandRawUpperBuffer,
pbBandUpperBuffer,
iPBMethod //
);
//
// XPB Lower ...
iMAOnBuffer(
ratesTotal,
prevCalculated,
barIndex,
maBandLength,
pbBandRawLowerBuffer,
pbBandLowerBuffer,
iPBMethod //
);
}
}
//
void CalculatePV(
int barIndex,
const int prevCalculated,
const int ratesTotal,
const double &open[],
const double &high[],
const double &close[],
const double &low[],
const long &tickVolume[] //
)
{
//
int lastBarIndex = barIndex + 1;
bool isFirstBar =
startCalculationForLastBars > 0
? barIndex == startCalculationForLastBars
: barIndex == firstBarIndex;
//
double iSHH = sHHBuffer[barIndex];
double iSLL = sLLBuffer[barIndex];
//
double iMHH = mHHBuffer[barIndex];
double iMLL = mLLBuffer[barIndex];
//
double iLHH = lHHBuffer[barIndex];
double iLLL = lLLBuffer[barIndex];
//
double iHHH = hHHBuffer[barIndex];
double iHLL = hLLBuffer[barIndex];
//
// Calculate VALE ...
bool isVale = iSLL == iMLL &&
iMLL == iLLL &&
iLLL == iHLL;
double iPVale =
isFirstBar
? emptyValue
: valeBuffer[lastBarIndex];
double iVale =
isVale
? iSLL
: iPVale;
valeBuffer[barIndex] = iVale;
//
// Calculate PEAK ...
bool isPeak = iSHH == iMHH &&
iMHH == iLHH &&
iLHH == iHHH;
double iPPeak =
isFirstBar
? emptyValue
: peakBuffer[lastBarIndex];
double iPeak =
isPeak
? iSHH
: iPPeak;
peakBuffer[barIndex] = iPeak;
}
/**
* 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 ...
*
* @return ( int )
*/
int CalculateZigZag(
int barIndex,
const int prevCalculated,
const int ratesTotal,
const double &open[],
const double &high[],
const double &close[],
const double &low[],
const long &tickVolume[] //
)
{
//
int result = 0;
//
// Define Variables ...
int i = 0;
//
int start = 0;
int extreme_counter = 0;
int extreme_search = X_ZG_SEARCH_EXTREMUM;
int shift = 0;
int back = 0;
int last_high_pos = 0;
int last_low_pos = 0;
//
double val = 0;
double res = 0;
double curlow = 0;
double curhigh = 0;
double last_high = 0;
double last_low = 0;
//
// Initializing ...
if (prevCalculated == 0)
{
//
ArrayInitialize(lowsBuffer, 0);
ArrayInitialize(highsBuffer, 0);
ArrayInitialize(zigzagBuffer, 0);
ArrayInitialize(lowsTimeBuffer, 0);
ArrayInitialize(highsTimeBuffer, 0);
//
start = zigzagDepth;
}
//
// Preparing ZigZag Calculation High and Lows ...
//
if (zigzagLowersCount != zigzagUppersCount || zigzagLowersCount != ratesTotal)
{
//
// Filling Upper ...
zigzagUppersCount = FillPrice(
zigzagUppersMode,
zigzagUppers,
open,
high,
close,
low //
);
//
// Filling Lower ...
zigzagLowersCount = FillPrice(
zigzagLowersMode,
zigzagLowers,
open,
high,
close,
low //
);
}
//
// Already Calculated Before ...
if (prevCalculated > 0)
{
//
i = ratesTotal - 1;
//
// Searching for the third X_ZG_SEARCH_EXTREMUM from the last UnComplete Bar ...
while (extreme_counter < zigzagRecalc && i > ratesTotal - 100)
{
//
res = zigzagBuffer[i];
if (res != 0)
{
extreme_counter++;
}
//
i--;
}
//
i++;
start = i;
//
// What type of X_ZG_SEARCH_EXTREMUM we Search for ...
if (lowsBuffer[i] != 0)
{
//
curlow = lowsBuffer[i];
extreme_search = X_ZG_SEARCH_PEAK;
}
else
{
//
curhigh = highsBuffer[i];
extreme_search = X_ZG_SEARCH_VALE;
}
//
// Clear Values ...
for (i = start + 1; i < ratesTotal && !IsStopped(); i++)
{
//
lowsBuffer[i] = 0;
highsBuffer[i] = 0;
zigzagBuffer[i] = 0;
lowsTimeBuffer[i] = 0;
highsTimeBuffer[i] = 0;
}
}
//
// Search for High and Low Extremes ...
for (shift = start; shift < ratesTotal && !IsStopped(); shift++)
{
//
// Low ...
val = zigzagLowers[Lowest(zigzagLowers, zigzagDepth, shift)];
if (val == last_low)
{
val = 0;
}
else
{
//
last_low = val;
//
if ((zigzagLowers[shift] - val) > zigzagDeviation * _Point)
{
val = 0;
}
else
{
//
for (back = 1; back <= zigzagBackStep; back++)
{
//
res = lowsBuffer[shift - back];
if ((res != 0) && (res > val))
{
//
lowsBuffer[shift - back] = 0;
lowsTimeBuffer[shift - back] = 0;
}
}
}
}
//
if (zigzagLowers[shift] == val)
{
//
lowsBuffer[shift] = val;
lowsTimeBuffer[shift] = GetZigZagBarTime(shift);
}
else
{
//
lowsBuffer[shift] = 0;
lowsTimeBuffer[shift] = 0;
}
//
// High ...
val = zigzagUppers[Highest(zigzagUppers, zigzagDepth, shift)];
if (val == last_high)
{
val = 0;
}
else
{
//
last_high = val;
//
if ((val - zigzagUppers[shift]) > zigzagDeviation * _Point)
{
val = 0;
}
else
{
//
for (back = 1; back <= zigzagBackStep; back++)
{
//
res = highsBuffer[shift - back];
if ((res != 0) && (res < val))
{
//
highsBuffer[shift - back] = 0;
highsTimeBuffer[shift - back] = 0;
}
}
}
}
//
if (zigzagUppers[shift] == val)
{
//
highsBuffer[shift] = val;
highsTimeBuffer[shift] = GetZigZagBarTime(shift);
}
else
{
highsBuffer[shift] = 0;
highsTimeBuffer[shift] = 0;
}
}
//
// Set Last Value ...
if (extreme_search == 0) // Undefined Values ...
{
//
last_low = 0;
last_high = 0;
}
else
{
//
last_low = curlow;
last_high = curhigh;
}
//
// Final Selection of Extreme Points for ZigZag ...
for (shift = start; shift < ratesTotal && !IsStopped(); shift++)
{
//
res = 0;
//
switch (extreme_search)
{
//
case X_ZG_SEARCH_EXTREMUM:
//
if (last_low == 0 && last_high == 0)
{
//
if (highsBuffer[shift] != 0)
{
//
last_high_pos = shift;
last_high = zigzagUppers[shift];
//
extreme_search = X_ZG_SEARCH_VALE;
//
zigzagBuffer[shift] = last_high;
//
res = 1;
}
//
if (lowsBuffer[shift] != 0)
{
//
last_low_pos = shift;
last_low = zigzagLowers[shift];
//
extreme_search = X_ZG_SEARCH_PEAK;
//
zigzagBuffer[shift] = last_low;
//
res = 1;
}
}
break;
//
case X_ZG_SEARCH_PEAK:
//
if (lowsBuffer[shift] != 0 && lowsBuffer[shift] < last_low && highsBuffer[shift] == 0)
{
//
zigzagBuffer[last_low_pos] = 0;
//
last_low_pos = shift;
last_low = lowsBuffer[shift];
//
zigzagBuffer[shift] = last_low;
//
res = 1;
}
//
if (highsBuffer[shift] != 0 && lowsBuffer[shift] == 0)
{
//
last_high_pos = shift;
last_high = highsBuffer[shift];
//
zigzagBuffer[shift] = last_high;
//
extreme_search = X_ZG_SEARCH_VALE;
//
res = 1;
}
break;
//
case X_ZG_SEARCH_VALE:
//
if (highsBuffer[shift] != 0 && highsBuffer[shift] > last_high && lowsBuffer[shift] == 0)
{
//
zigzagBuffer[last_high_pos] = 0;
//
last_high_pos = shift;
last_high = highsBuffer[shift];
//
zigzagBuffer[shift] = last_high;
}
//
if (lowsBuffer[shift] != 0 && highsBuffer[shift] == 0)
{
//
last_low_pos = shift;
last_low = lowsBuffer[shift];
//
zigzagBuffer[shift] = last_low;
//
extreme_search = X_ZG_SEARCH_PEAK;
}
break;
}
}
//
result = ratesTotal;
//
return result;
}
//
void CalculateHKBar(
int barIndex,
const int prevCalculated,
const int ratesTotal,
const double &open[],
const double &high[],
const double &close[],
const double &low[],
const long &tickVolume[] //
)
{
//
int lastBarIndex = barIndex + 1;
bool isFirstBar =
startCalculationForLastBars > 0
? barIndex == startCalculationForLastBars
: barIndex == firstBarIndex;
//
double lastHKOpen =
isFirstBar
? emptyValue
: hkOpenBuffer[lastBarIndex];
double lastHKClose =
isFirstBar
? emptyValue
: hkCloseBuffer[lastBarIndex];
//
double iHKOpenValue = (lastHKOpen + lastHKClose) / 2;
double iHKCloseValue = (open[barIndex] + high[barIndex] + close[barIndex] + low[barIndex]) / 4;
double iHKHighValue = MathMax(high[barIndex], MathMax(iHKOpenValue, iHKCloseValue));
double iHKLowValue = MathMin(low[barIndex], MathMin(iHKOpenValue, iHKCloseValue));
//
bool isBullish = iHKOpenValue < iHKCloseValue;
bool isBearish = iHKOpenValue > iHKCloseValue;
//
if (hkIgnoreShadows)
{
//
iHKHighValue = isBullish
? iHKCloseValue
: iHKOpenValue;
//
iHKLowValue = isBullish
? iHKOpenValue
: iHKCloseValue;
}
//
// XHK Buffers ...
hkOpenBuffer[barIndex] = iHKOpenValue;
hkHighBuffer[barIndex] = iHKHighValue;
hkLowBuffer[barIndex] = iHKLowValue;
hkCloseBuffer[barIndex] = iHKCloseValue;
//
// XHK Raw Bufferss ...
hkRawOpenBuffer[barIndex] = iHKOpenValue;
hkRawHighBuffer[barIndex] = iHKHighValue;
hkRawLowBuffer[barIndex] = iHKLowValue;
hkRawCloseBuffer[barIndex] = iHKCloseValue;
//
// Smoothing ...
if (IsXValid(hkSmoothingMode) && hkSmoothingLength > 0)
{
//
// XHK Signal ...
//
// High ...
iMAOnBuffer(
ratesTotal,
prevCalculated,
barIndex,
hkSmoothingLength,
hkRawHighBuffer,
hkHighBuffer,
hkSmoothingMode //
);
//
// Open ...
iMAOnBuffer(
ratesTotal,
prevCalculated,
barIndex,
hkSmoothingLength,
hkRawOpenBuffer,
hkOpenBuffer,
hkSmoothingMode //
);
//
// Close ...
iMAOnBuffer(
ratesTotal,
prevCalculated,
barIndex,
hkSmoothingLength,
hkRawLowBuffer,
hkLowBuffer,
hkSmoothingMode //
);
//
// Close ...
iMAOnBuffer(
ratesTotal,
prevCalculated,
barIndex,
hkSmoothingLength,
hkRawCloseBuffer,
hkCloseBuffer,
hkSmoothingMode //
);
}
//
hkBandUpperBuffer[barIndex] = hkHighBuffer[barIndex];
hkBandLowerBuffer[barIndex] = hkLowBuffer[barIndex];
//
// Calculate HK Color ...
double iHKColorValue =
isBullish
? bullishColorIDX
: isBearish
? bearishColorIDX
: neuturalColorIDX;
hkColorBuffer[barIndex] =
showHK
? iHKColorValue
: hideColorIDX;
}
//
void CalculateATR(
int barIndex,
const int prevCalculated,
const int ratesTotal,
const double &open[],
const double &high[],
const double &close[],
const double &low[],
const long &tickVolume[] //
)
{
//
int lastBarIndex = barIndex + 1;
bool isFirstBar =
startCalculationForLastBars > 0
? barIndex == startCalculationForLastBars
: barIndex == firstBarIndex;
//
double iAtr = atrBuffer[barIndex];
double iAppliedAtr = atrMultiplier <= 0
? iAtr
: atrMultiplier * iAtr;
double iUpperPrice = GetAppliedPrice(
atrUpperPriceType,
open,
high,
low,
close,
barIndex //
);
double iLowerPrice = GetAppliedPrice(
atrLowerPriceType,
open,
high,
low,
close,
barIndex //
);
double iPriceMid = iLowerPrice + ((iUpperPrice - iLowerPrice) / 2);
//
double iUpperAtr = iPriceMid + iAppliedAtr;
double iLowerAtr = iPriceMid - iAppliedAtr;
//
atrBandRawUpperBuffer[barIndex] = iUpperAtr;
atrBandRawLowerBuffer[barIndex] = iLowerAtr;
//
// Check Can Smooth Atr ...
if (IsXValid(atrSmoothingMode) &&
atrSmoothingLength > 0)
{
//
// Upper ...
iMAOnBuffer(
ratesTotal,
prevCalculated,
barIndex,
atrSmoothingLength,
atrBandRawUpperBuffer,
atrBandUpperBuffer,
atrSmoothingMode //
);
//
// Lower ...
iMAOnBuffer(
ratesTotal,
prevCalculated,
barIndex,
atrSmoothingLength,
atrBandRawLowerBuffer,
atrBandLowerBuffer,
atrSmoothingMode //
);
}
else
{
//
atrBandUpperBuffer[barIndex] = iUpperAtr;
atrBandLowerBuffer[barIndex] = iLowerAtr;
}
}
//
void CalculateCyclesMA(
int barIndex,
const int prevCalculated,
const int ratesTotal,
const double &open[],
const double &high[],
const double &close[],
const double &low[],
const long &tickVolume[] //
)
{
//
int lastBarIndex = barIndex + 1;
bool isFirstBar =
startCalculationForLastBars > 0
? barIndex == startCalculationForLastBars
: barIndex == firstBarIndex;
//
// Retrieve Indexed Bar Price for MA States Checking ...
ENUM_X_PRICE iPriceType = X_PRICE_CLOSE;
double iPrice = GetAppliedPrice(
iPriceType,
open,
high,
low,
close,
barIndex //
);
//
// SC ...
double iSCMa = scMaBuffer[barIndex];
double iSCMaColor = iPrice > iSCMa
? bullishColorIDX
: iPrice < iSCMa
? bearishColorIDX
: neuturalColorIDX;
double iSCMaState = iPrice > iSCMa
? bullishState
: iPrice < iSCMa
? bearishState
: neuturalState;
scMaStateBuffer[barIndex] = iSCMaState;
//
// MC ...
double iMCMa = mcMaBuffer[barIndex];
double iMCMaColor = iPrice > iMCMa
? bullishColorIDX
: iPrice < iMCMa
? bearishColorIDX
: neuturalColorIDX;
double iMCMaState = iPrice > iMCMa
? bullishState
: iPrice < iMCMa
? bearishState
: neuturalState;
mcMaStateBuffer[barIndex] = iMCMaState;
//
// LC ...
double iLCMa = lcMaBuffer[barIndex];
double iLCMaColor = iPrice > iLCMa
? bullishColorIDX
: iPrice < iLCMa
? bearishColorIDX
: neuturalColorIDX;
double iLCMaState = iPrice > iLCMa
? bullishState
: iPrice < iLCMa
? bearishState
: neuturalState;
lcMaStateBuffer[barIndex] = iLCMaState;
//
// HC ...
double iHCMa = hcMaBuffer[barIndex];
double iHCMaColor = iPrice > iHCMa
? bullishColorIDX
: iPrice < iHCMa
? bearishColorIDX
: neuturalColorIDX;
double iHCMaState = iPrice > iHCMa
? bullishState
: iPrice < iHCMa
? bearishState
: neuturalState;
hcMaStateBuffer[barIndex] = iHCMaState;
}
//
void CalculateCycleRanges(
int barIndex,
const int prevCalculated,
const int ratesTotal,
const double &open[],
const double &high[],
const double &close[],
const double &low[],
const long &tickVolume[] //
)
{
//
// 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 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,
upperMode //
);
hhBuffer[barIndex] = hhValue;
//
// Find Lowest Low ...
double llValue = bar.FindLowest(
mLength,
lowerMode //
);
llBuffer[barIndex] = llValue;
//
bar.Clean();
}
//
//
//
//
// Get Specified Bar Time as Double ...
double GetZigZagBarTime(int barIndex)
{
//
if (barIndex <= 0)
{
barIndex = 0;
}
//
datetime time = iTime(_Symbol, _Period, barIndex);
//
double result = (double)((int)time);
//
return result;
}
//
// Filling Required Prices Based on MODE ...
int FillPrice(
ENUM_X_PRICE mode,
double &dest[],
const double &open[],
const double &high[],
const double &close[],
const double &low[] //
)
{
//
int result = 0;
//
SpecifiedClean(dest);
//
if (!IsXValid(mode))
{
return result;
}
//
switch (mode)
{
//
case X_PRICE_HIGH:
//
result = ArrayCopy(
dest,
high //
);
break;
//
case X_PRICE_LOW:
//
result = ArrayCopy(
dest,
low //
);
break;
//
case X_PRICE_OPEN:
//
result = ArrayCopy(
dest,
open //
);
break;
//
case X_PRICE_CLOSE:
//
result = ArrayCopy(
dest,
close //
);
break;
//
default:
//
result = ArrayCopy(
dest,
close //
);
break;
}
//
return result;
}
//
// Search for the index of the highest bar ...
int Highest(
const double &mArray[],
const int mDepth,
const int mStart //
)
{
//
int result = 0;
//
if (mStart < 0)
{
return result;
}
//
double max = mArray[mStart];
result = mStart;
//
// Start searching ...
for (int i = mStart - 1; i > mStart - mDepth && i >= 0; i--)
{
//
if (mArray[i] > max)
{
//
result = i;
max = mArray[i];
}
}
//
// Return index of the highest bar
return result;
}
//
// Search for the index of the lowest bar ...
int Lowest(
const double &mArray[],
const int mDepth,
const int mStart //
)
{
//
int result = 0;
//
if (mStart < 0)
{
return result;
}
//
double min = mArray[mStart];
result = mStart;
//
// Start searching ...
for (int i = mStart - 1; i > mStart - mDepth && i >= 0; i--)
{
//
if (mArray[i] < min)
{
//
result = i;
min = mArray[i];
}
}
//
// Return index of the lowest bar
return result;
}
//