Files
MQL5Data/Indicators/x-saherelm.x121.xki.mq5
T
2026-01-08 18:44:19 +03:30

1790 lines
44 KiB
Plaintext

///////////////////////////////////////////////////////
//
// SaherElm IT Center MQL5 Indicator
// -------------------------------------------------
// Name: XKI
// Description: a Kijunsen Based Indicator ...
//
//
// 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 XKI Indicator"
#property icon "../Images/SaherElm_Logo_Color.ico"
#property strict
//
// Definitions ...
//
#define ShortName "XKI"
//
// Imports ...
#include "../Libraries/x-saherelm.common.lib.mq5"
// #region Inputs ...
//
input group "Calculation";
input int kiFastLength = 26; // KI Fast Length
input int kiSlowLength = 104; // KI Slow Length
input int kiSuperSlowLength = 208; // KI Super Slow Length
input int maFastLength = 26; // MA Fast Length
input int maSlowLength = 104; // MA Slow Length
input int maSuperSlowLength = 208; // MA Super Slow Length
input ENUM_MA_METHOD maMethod = MODE_EMA; // MA Mode
input ENUM_APPLIED_PRICE maAppliedTo = PRICE_CLOSE; // MA Applied To
input int rsiLength = 14; // RSI Length
input ENUM_APPLIED_PRICE rsiAppliedTo = PRICE_CLOSE; // RSI Applied To
input double rsiBullishConfirmation = 55; // RSI Bullish Confirmation Level
input double rsiBearishConfirmation = 45; // RSI Bearish Confirmation Level
input int rsiSmoothingLength = 14; // RSI Smoothing Length
input ENUM_X_MA_METHOD rsiSmoothingMode = X_MA_MODE_EMA; // RSI Smoothing Method
input int kiWaveSmoothingLength = 14; // RSI KI Wave Smoothing Length
input ENUM_X_MA_METHOD kiWaveSmoothingMode = X_MA_MODE_EMA; // RSI KI Wave Smoothing Method
input double rsiMultiplier = 0.5; // RSI Multiplier
input int atrLength = 14; // ATR Length
input double atrMultiplier = 2; // ATR Multiplier
input int atrSmoothingLength = 14; // ATR Smoothing Length
input ENUM_X_MA_METHOD atrSmoothingMode = X_MA_MODE_EMA; // ATR Smoothing Method
input ENUM_X_PRICE atrUpperPriceType = X_PRICE_HIGH; // ATR Upper Price Type
input ENUM_X_PRICE atrLowerPriceType = X_PRICE_LOW; // ATR Lower Price Type
//
// Presentation ...
input group "Presentation";
input bool showPV = true; // Show PV
input bool showKIFast = true; // Show KI Fast
input bool showKISlow = true; // Show KI Slow
input bool showKISuperSlow = true; // Show KI Super Slow
input bool showMaFast = true; // Show Ma Fast
input bool showMaSlow = true; // Show MA Slow
input bool showMaSuperSlow = true; // Show MA Super Slow
input bool showKIWave = true; // Show KI Wave
input bool showKIUpper = true; // Show KI Upper
input bool showKILower = true; // Show KI Lower
//
input int startCalculationForLastBars = 1500; // Calculate Last n Bars
//
input int peakArrowCode = 159; // Peaks Arrow Code
input int valeArrowCode = 159; // Vales Arrow Code
// #endregion
// #region 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
// #endregion
// #region Buffers ...
//
#property indicator_chart_window
//
#property indicator_buffers 26
#property indicator_plots 11
// #region Plot Buffers ...
// #region KI ...
//
#define kiBufferIndex 0
#define kiBufferPlotIndex 0
double kiBuffer[];
//
#define kiColorBufferIndex 1
double kiColorBuffer[];
//
#property indicator_label1 "KI Fast"
#property indicator_type1 DRAW_COLOR_LINE
#property indicator_color1 CLR_NONE, clrGreen, clrRed, clrGray
#property indicator_style1 STYLE_SOLID
#property indicator_width1 2
// #endregion
// #region KI Wave ...
//
#define kiWaveBufferIndex 2
#define kiWaveBufferPlotIndex 1
double kiWaveBuffer[];
//
#define kiWaveColorBufferIndex 3
double kiWaveColorBuffer[];
//
#property indicator_label2 "KI Wave"
#property indicator_type2 DRAW_COLOR_LINE
#property indicator_color2 CLR_NONE, clrGreen, clrRed, clrGray
#property indicator_style2 STYLE_DOT
#property indicator_width2 1
// #endregion
// #region KI Upper ...
//
#define kiUpperBufferIndex 4
#define kiUpperBufferPlotIndex 2
double kiUpperBuffer[];
//
#property indicator_label3 "KI Upper"
#property indicator_type3 DRAW_LINE
#property indicator_color3 clrAqua
#property indicator_style3 STYLE_DASH
#property indicator_width3 1
// #endregion
// #region KI Lower ...
//
#define kiLowerBufferIndex 5
#define kiLowerBufferPlotIndex 3
double kiLowerBuffer[];
//
#property indicator_label4 "KI Lower"
#property indicator_type4 DRAW_LINE
#property indicator_color4 clrMagenta
#property indicator_style4 STYLE_DASH
#property indicator_width4 1
// #endregion
// #region MA Fast ...
//
#define maFastBufferIndex 6
#define maFastBufferPlotIndex 4
double maFastBuffer[];
//
#property indicator_label5 "MA Fast"
#property indicator_type5 DRAW_LINE
#property indicator_color5 clrLime
#property indicator_style5 STYLE_DOT
#property indicator_width5 1
// #endregion
// #region MA Slow ...
//
#define maSlowBufferIndex 7
#define maSlowBufferPlotIndex 5
double maSlowBuffer[];
//
#property indicator_label6 "MA Slow"
#property indicator_type6 DRAW_LINE
#property indicator_color6 clrRed
#property indicator_style6 STYLE_DOT
#property indicator_width6 1
// #endregion
// #region KI Slow ...
//
#define kiSlowBufferIndex 8
#define kiSlowBufferPlotIndex 6
double kiSlowBuffer[];
//
#define kiSlowColorBufferIndex 9
double kiSlowColorBuffer[];
//
#property indicator_label7 "KI Slow"
#property indicator_type7 DRAW_COLOR_LINE
#property indicator_color7 CLR_NONE, clrGreen, clrRed, clrGray
#property indicator_style7 STYLE_SOLID
#property indicator_width7 2
// #endregion
// #region KI Super Slow ...
//
#define kiSuperSlowBufferIndex 10
#define kiSuperSlowBufferPlotIndex 7
double kiSuperSlowBuffer[];
//
#define kiSuperSlowColorBufferIndex 11
double kiSuperSlowColorBuffer[];
//
#property indicator_label8 "KI Super Slow"
#property indicator_type8 DRAW_COLOR_LINE
#property indicator_color8 CLR_NONE, clrAqua, clrMagenta, clrGray
#property indicator_style8 STYLE_SOLID
#property indicator_width8 2
// #endregion
// #region MA Super Slow ...
//
#define maSuperSlowBufferIndex 12
#define maSuperSlowBufferPlotIndex 8
double maSuperSlowBuffer[];
//
#property indicator_label9 "MA Super Slow"
#property indicator_type9 DRAW_LINE
#property indicator_color9 clrMagenta
#property indicator_style9 STYLE_DOT
#property indicator_width9 1
// #endregion
// #region PV ...
//
// PEAKS ...
//
#define peakBufferIndex 13
#define peakBufferPlotIndex 9
double peakBuffer[];
//
#property indicator_label10 "PEAK"
#property indicator_type10 DRAW_ARROW
#property indicator_color10 clrMagenta
#property indicator_width10 3
//
// VALES ...
//
#define valeBufferIndex 14
#define valeBufferPlotIndex 10
double valeBuffer[];
//
#property indicator_label11 "VALE"
#property indicator_type11 DRAW_ARROW
#property indicator_color11 clrAqua
#property indicator_width11 3
// #endregion
// #endregion
//
#define mLastBufferIndex 14
// #region Data Buffers ...
//
#define kiStateBufferIndex mLastBufferIndex + 1
double kiStateBuffer[];
//
#define atrBufferIndex mLastBufferIndex + 2
double atrBuffer[];
//
#define atrRawUpperBufferIndex mLastBufferIndex + 3
double atrRawUpperBuffer[];
//
#define atrRawLowerBufferIndex mLastBufferIndex + 4
double atrRawLowerBuffer[];
//
#define rsiBufferIndex mLastBufferIndex + 5
double rsiBuffer[];
//
#define kiWaveRawBufferIndex mLastBufferIndex + 6
double kiWaveRawBuffer[];
//
#define kiWaveStateBufferIndex mLastBufferIndex + 7
double kiWaveStateBuffer[];
//
#define kiSlowStateBufferIndex mLastBufferIndex + 8
double kiSlowStateBuffer[];
//
#define atrSmoothedBufferIndex mLastBufferIndex + 9
double atrSmoothedBuffer[];
//
#define kiSuperSlowStateBufferIndex mLastBufferIndex + 10
double kiSuperSlowStateBuffer[];
//
#define rsiSmoothedBufferIndex mLastBufferIndex + 11
double rsiSmoothedBuffer[];
// #endregion
// #endregion
// #region Variables, Properties and etc ...
//
int limit;
//
int maxLength;
//
int firstBarIndex;
//
// Handlers ...
int atrHandler = INVALID_HANDLE;
int rsiHandler = INVALID_HANDLE;
int maFastHandler = INVALID_HANDLE;
int maSlowHandler = INVALID_HANDLE;
int maSuperSlowHandler = INVALID_HANDLE;
// #endregion
// #region 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 ...
//
// ATR ...
if (atrHandler != INVALID_HANDLE)
{
IndicatorRelease(atrHandler);
}
//
// RSI ...
if (rsiHandler != INVALID_HANDLE)
{
IndicatorRelease(rsiHandler);
}
//
// MA Fast ...
if (maFastHandler != INVALID_HANDLE)
{
IndicatorRelease(maFastHandler);
}
//
// MA Slow ...
if (maSlowHandler != INVALID_HANDLE)
{
IndicatorRelease(maSlowHandler);
}
//
// MA Super Slow ...
if (maSuperSlowHandler != INVALID_HANDLE)
{
IndicatorRelease(maSuperSlowHandler);
}
}
/**
* 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 ...
//
// ATR ...
int atrCalculatedBars = BarsCalculated(atrHandler);
//
// RSI ...
int rsiCalculatedBars = BarsCalculated(rsiHandler);
//
// MA ...
int maFastCalculatedBars = BarsCalculated(maFastHandler);
int maSlowCalculatedBars = BarsCalculated(maSlowHandler);
int maSuperSlowCalculatedBars = BarsCalculated(maSuperSlowHandler);
//
bool isPassedRequiredCalculatedBars =
//
// ATR ...
atrCalculatedBars >= maxLength &&
//
// RSI ...
rsiCalculatedBars >= maxLength &&
//
// MA ...
maFastCalculatedBars >= maxLength &&
maSlowCalculatedBars >= maxLength &&
maSuperSlowCalculatedBars >= maxLength
//
;
if (!isPassedRequiredCalculatedBars)
{
return prev_calculated;
}
//
limit = (prev_calculated > rates_total || prev_calculated <= 0)
? rates_total
: (rates_total - prev_calculated) + 1;
//
// Buffers Copy ...
//
// ATR ...
int copiedATRs = CopyBuffer(atrHandler, MAIN_LINE, 0, limit, atrBuffer);
//
// RSI ...
int copiedRSIs = CopyBuffer(rsiHandler, MAIN_LINE, 0, limit, rsiBuffer);
//
// MA ...
int copiedMAFasts = CopyBuffer(maFastHandler, MAIN_LINE, 0, limit, maFastBuffer);
int copiedMASlows = CopyBuffer(maSlowHandler, MAIN_LINE, 0, limit, maSlowBuffer);
int copiedMASuperSlows = CopyBuffer(maSuperSlowHandler, MAIN_LINE, 0, limit, maSuperSlowBuffer);
//
// Validate Copied Items ...
bool isPassedRequiredCopiedItems =
//
// ATR ...
copiedATRs >= limit &&
//
// RSI ...
copiedRSIs >= limit &&
//
// MA ...
copiedMAFasts >= limit &&
copiedMASlows >= limit &&
copiedMASuperSlows >= 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;
}
// #endregion
// #region 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 =
//
// KI ...
kiFastLength >= 5 &&
kiSlowLength > kiFastLength &&
kiSuperSlowLength > kiSlowLength &&
//
// MA ...
maFastLength >= 2 &&
maSlowLength > maFastLength &&
maSuperSlowLength > maSlowLength &&
//
// ATR ...
atrLength >= 5 &&
//
// RSI ...
rsiLength >= 5 &&
rsiBullishConfirmation > 0 &&
rsiBullishConfirmation < 100 &&
rsiBearishConfirmation > 0 &&
rsiBearishConfirmation < 100
//
;
//
return result;
}
/**
* Extract Max Length of Inputs ...
*
* @return ( int )
*/
int ExtractMaxLengthOfInputs()
{
//
int result = 0;
//
result = MathMax(kiFastLength, atrLength);
result = MathMax(result, rsiLength);
result = MathMax(result, kiSlowLength);
result = MathMax(result, maFastLength);
result = MathMax(result, maSlowLength);
result = MathMax(result, maSuperSlowLength);
result = MathMax(result, kiSuperSlowLength);
//
if (rsiSmoothingLength > 0)
{
result = MathMax(result, rsiSmoothingLength);
}
if (atrSmoothingLength > 0)
{
result = MathMax(result, atrSmoothingLength);
}
if (kiWaveSmoothingLength > 0)
{
result = MathMax(result, kiWaveSmoothingLength);
}
//
return result;
}
/**
* Define Required Buffers ...
*/
void DefineBuffers()
{
// #region KI ...
//
ArraySetAsSeries(kiBuffer, true);
SetIndexBuffer(kiBufferIndex, kiBuffer, INDICATOR_DATA);
//
PlotIndexSetInteger(kiBufferPlotIndex, PLOT_SHOW_DATA, showKIFast);
PlotIndexSetDouble(kiBufferPlotIndex, PLOT_EMPTY_VALUE, emptyValue);
//
ArraySetAsSeries(kiColorBuffer, true);
SetIndexBuffer(kiColorBufferIndex, kiColorBuffer, INDICATOR_COLOR_INDEX);
//
ArraySetAsSeries(kiStateBuffer, true);
SetIndexBuffer(kiStateBufferIndex, kiStateBuffer, INDICATOR_CALCULATIONS);
// #endregion
// #region KI Slow ...
//
ArraySetAsSeries(kiSlowBuffer, true);
SetIndexBuffer(kiSlowBufferIndex, kiSlowBuffer, INDICATOR_DATA);
//
PlotIndexSetInteger(kiSlowBufferPlotIndex, PLOT_SHOW_DATA, showKISlow);
PlotIndexSetDouble(kiSlowBufferPlotIndex, PLOT_EMPTY_VALUE, emptyValue);
//
ArraySetAsSeries(kiSlowColorBuffer, true);
SetIndexBuffer(kiSlowColorBufferIndex, kiSlowColorBuffer, INDICATOR_COLOR_INDEX);
//
ArraySetAsSeries(kiSlowStateBuffer, true);
SetIndexBuffer(kiSlowStateBufferIndex, kiSlowStateBuffer, INDICATOR_CALCULATIONS);
// #endregion
// #region KI Super Slow ...
//
ArraySetAsSeries(kiSuperSlowBuffer, true);
SetIndexBuffer(kiSuperSlowBufferIndex, kiSuperSlowBuffer, INDICATOR_DATA);
//
PlotIndexSetInteger(kiSuperSlowBufferPlotIndex, PLOT_SHOW_DATA, showKISuperSlow);
PlotIndexSetDouble(kiSuperSlowBufferPlotIndex, PLOT_EMPTY_VALUE, emptyValue);
//
ArraySetAsSeries(kiSuperSlowColorBuffer, true);
SetIndexBuffer(kiSuperSlowColorBufferIndex, kiSuperSlowColorBuffer, INDICATOR_COLOR_INDEX);
//
ArraySetAsSeries(kiSuperSlowStateBuffer, true);
SetIndexBuffer(kiSuperSlowStateBufferIndex, kiSuperSlowStateBuffer, INDICATOR_CALCULATIONS);
// #endregion
// #region KI Upper ...
//
ENUM_DRAW_TYPE kiUpperDrawType = showKIUpper ? DRAW_LINE : DRAW_NONE;
//
// Upper ...
ArraySetAsSeries(kiUpperBuffer, true);
SetIndexBuffer(kiUpperBufferIndex, kiUpperBuffer, INDICATOR_DATA);
//
PlotIndexSetInteger(kiUpperBufferPlotIndex, PLOT_SHOW_DATA, showKIUpper);
PlotIndexSetInteger(kiUpperBufferPlotIndex, PLOT_DRAW_TYPE, kiUpperDrawType);
PlotIndexSetDouble(kiUpperBufferPlotIndex, PLOT_EMPTY_VALUE, emptyValue);
// #endregion
// #region KI Lower ...
//
ENUM_DRAW_TYPE kiLowerDrawType = showKILower ? DRAW_LINE : DRAW_NONE;
//
// Upper ...
ArraySetAsSeries(kiLowerBuffer, true);
SetIndexBuffer(kiLowerBufferIndex, kiLowerBuffer, INDICATOR_DATA);
//
PlotIndexSetInteger(kiLowerBufferPlotIndex, PLOT_SHOW_DATA, showKILower);
PlotIndexSetInteger(kiLowerBufferPlotIndex, PLOT_DRAW_TYPE, kiLowerDrawType);
PlotIndexSetDouble(kiLowerBufferPlotIndex, PLOT_EMPTY_VALUE, emptyValue);
// #endregion
// #region KI Wave ...
//
ArraySetAsSeries(kiWaveBuffer, true);
SetIndexBuffer(kiWaveBufferIndex, kiWaveBuffer, INDICATOR_DATA);
//
PlotIndexSetInteger(kiWaveBufferPlotIndex, PLOT_SHOW_DATA, showKIWave);
PlotIndexSetDouble(kiWaveBufferPlotIndex, PLOT_EMPTY_VALUE, emptyValue);
//
ArraySetAsSeries(kiWaveColorBuffer, true);
SetIndexBuffer(kiWaveColorBufferIndex, kiWaveColorBuffer, INDICATOR_COLOR_INDEX);
//
ArraySetAsSeries(rsiBuffer, true);
SetIndexBuffer(rsiBufferIndex, rsiBuffer, INDICATOR_CALCULATIONS);
//
ArraySetAsSeries(rsiSmoothedBuffer, true);
SetIndexBuffer(rsiSmoothedBufferIndex, rsiSmoothedBuffer, INDICATOR_CALCULATIONS);
//
ArraySetAsSeries(kiWaveRawBuffer, true);
SetIndexBuffer(kiWaveRawBufferIndex, kiWaveRawBuffer, INDICATOR_CALCULATIONS);
//
ArraySetAsSeries(kiWaveStateBuffer, true);
SetIndexBuffer(kiWaveStateBufferIndex, kiWaveStateBuffer, INDICATOR_CALCULATIONS);
// #endregion
// #region PV ...
//
// 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);
// #endregion
// #region ATR ...
//
ArraySetAsSeries(atrBuffer, true);
SetIndexBuffer(atrBufferIndex, atrBuffer, INDICATOR_CALCULATIONS);
//
ArraySetAsSeries(atrSmoothedBuffer, true);
SetIndexBuffer(atrSmoothedBufferIndex, atrSmoothedBuffer, INDICATOR_CALCULATIONS);
//
ArraySetAsSeries(atrRawUpperBuffer, true);
SetIndexBuffer(atrRawUpperBufferIndex, atrRawUpperBuffer, INDICATOR_CALCULATIONS);
//
ArraySetAsSeries(atrRawLowerBuffer, true);
SetIndexBuffer(atrRawLowerBufferIndex, atrRawLowerBuffer, INDICATOR_CALCULATIONS);
// #endregion
// #region MA ...
// #region Fast ...
//
ENUM_DRAW_TYPE maFastDrawType = showMaFast ? DRAW_LINE : DRAW_NONE;
//
// Fast ...
ArraySetAsSeries(maFastBuffer, true);
SetIndexBuffer(maFastBufferIndex, maFastBuffer, INDICATOR_DATA);
//
PlotIndexSetInteger(maFastBufferPlotIndex, PLOT_SHOW_DATA, showMaFast);
PlotIndexSetInteger(maFastBufferPlotIndex, PLOT_DRAW_TYPE, maFastDrawType);
PlotIndexSetDouble(maFastBufferPlotIndex, PLOT_EMPTY_VALUE, emptyValue);
// #endregion
// #region Slow ...
//
ENUM_DRAW_TYPE maSlowDrawType = showMaSlow ? DRAW_LINE : DRAW_NONE;
//
// Slow ...
ArraySetAsSeries(maSlowBuffer, true);
SetIndexBuffer(maSlowBufferIndex, maSlowBuffer, INDICATOR_DATA);
//
PlotIndexSetInteger(maSlowBufferPlotIndex, PLOT_SHOW_DATA, showMaSlow);
PlotIndexSetInteger(maSlowBufferPlotIndex, PLOT_DRAW_TYPE, maSlowDrawType);
PlotIndexSetDouble(maSlowBufferPlotIndex, PLOT_EMPTY_VALUE, emptyValue);
// #endregion
// #region Super Slow ...
//
ENUM_DRAW_TYPE maSuperSlowDrawType = showMaSuperSlow ? DRAW_LINE : DRAW_NONE;
//
// Slow ...
ArraySetAsSeries(maSuperSlowBuffer, true);
SetIndexBuffer(maSuperSlowBufferIndex, maSuperSlowBuffer, INDICATOR_DATA);
//
PlotIndexSetInteger(maSuperSlowBufferPlotIndex, PLOT_SHOW_DATA, showMaSuperSlow);
PlotIndexSetInteger(maSuperSlowBufferPlotIndex, PLOT_DRAW_TYPE, maSuperSlowDrawType);
PlotIndexSetDouble(maSuperSlowBufferPlotIndex, PLOT_EMPTY_VALUE, emptyValue);
// #endregion
// #endregion
}
/**
* Initial all Indicator Requirements ...
*
* @return ( false )
*
*/
bool InitRequirements()
{
//
bool result = false;
//
// Handlers ...
//
// ATR ...
atrHandler = iATR(
_Symbol,
_Period,
atrLength //
);
result = atrHandler != INVALID_HANDLE;
if (!result)
{
return result;
}
//
// RSI ...
rsiHandler = iRSI(
_Symbol,
_Period,
rsiLength,
rsiAppliedTo //
);
result = rsiHandler != INVALID_HANDLE;
if (!result)
{
return result;
}
//
// MA Fast ...
maFastHandler = iMA(
_Symbol,
_Period,
maFastLength,
0,
maMethod,
maAppliedTo //
);
result = maFastHandler != INVALID_HANDLE;
if (!result)
{
return result;
}
//
// MA Slow ...
maSlowHandler = iMA(
_Symbol,
_Period,
maSlowLength,
0,
maMethod,
maAppliedTo //
);
result = maSlowHandler != INVALID_HANDLE;
if (!result)
{
return result;
}
//
// MA Super Slow ...
maSuperSlowHandler = iMA(
_Symbol,
_Period,
maSuperSlowLength,
0,
maMethod,
maAppliedTo //
);
result = maSuperSlowHandler != 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 ...
// #region PV ...
CalculatePV(
barIndex,
prevCalculated,
ratesTotal,
open,
high,
close,
low,
tickVolume //
);
// #endregion
// #region KI ...
CalculateKI(
barIndex,
prevCalculated,
ratesTotal,
open,
high,
close,
low,
tickVolume //
);
// #endregion
// #region KI Slow ...
CalculateKISlow(
barIndex,
prevCalculated,
ratesTotal,
open,
high,
close,
low,
tickVolume //
);
// #endregion
// #region KI Super Slow ...
CalculateKISuperSlow(
barIndex,
prevCalculated,
ratesTotal,
open,
high,
close,
low,
tickVolume //
);
// #endregion
// #region ATR (Ki Upper/Lower) ...
CalculateATR(
barIndex,
prevCalculated,
ratesTotal,
open,
high,
close,
low,
tickVolume //
);
// #endregion
// #region RSI (Ki Wave) ...
CalculateRSI(
barIndex,
prevCalculated,
ratesTotal,
open,
high,
close,
low,
tickVolume //
);
// #endregion
}
/**
* Calculate KI Bufer ...
*
* @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 CalculateKI(
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;
//
XOHCL iBar;
bool isInited = iBar.Init(
_Symbol,
_Period,
barIndex //
);
//
double iClose = close[barIndex];
//
// Calculate KI / Color ...
double lastKI =
isFirstBar
? emptyValue
: kiBuffer[lastBarIndex];
double iKI = lastKI;
if (isInited)
{
//
double iLLKI = iBar.FindLowest(kiFastLength, MODE_LOW);
double iHHKI = iBar.FindHighest(kiFastLength, MODE_HIGH);
//
iKI = (iHHKI + iLLKI) / 2;
if (iLLKI == 0 || iHHKI == 0)
{
iKI = lastKI;
}
}
else
{
iKI = lastKI;
}
kiBuffer[barIndex] = iKI;
//
double iKIState = neuturalState;
double iKIColor = neuturalColorIDX;
if (iClose > iKI)
{
//
iKIState = bullishState;
iKIColor =
!showKIFast
? hideColorIDX
: bullishColorIDX;
}
else if (iClose < iKI)
{
//
iKIState = bearishState;
iKIColor =
!showKIFast
? hideColorIDX
: bearishColorIDX;
}
kiStateBuffer[barIndex] = iKIState;
kiColorBuffer[barIndex] = iKIColor;
//
iBar.Clean();
}
/**
* Calculate KI Slow Bufer ...
*
* @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 CalculateKISlow(
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;
//
XOHCL iBar;
bool isInited = iBar.Init(
_Symbol,
_Period,
barIndex //
);
//
double iClose = close[barIndex];
//
// Calculate KI / Color ...
double lastKI =
isFirstBar
? emptyValue
: kiSlowBuffer[lastBarIndex];
double iKI = lastKI;
if (isInited)
{
//
double iLLKI = iBar.FindLowest(kiSlowLength, MODE_LOW);
double iHHKI = iBar.FindHighest(kiSlowLength, MODE_HIGH);
//
iKI = (iHHKI + iLLKI) / 2;
if (iLLKI == 0 || iHHKI == 0)
{
iKI = lastKI;
}
}
else
{
iKI = lastKI;
}
kiSlowBuffer[barIndex] = iKI;
//
double iKIState = neuturalState;
double iKIColor = neuturalColorIDX;
if (iClose > iKI)
{
//
iKIState = bullishState;
iKIColor =
!showKISlow
? hideColorIDX
: bullishColorIDX;
}
else if (iClose < iKI)
{
//
iKIState = bearishState;
iKIColor =
!showKISlow
? hideColorIDX
: bearishColorIDX;
}
kiSlowStateBuffer[barIndex] = iKIState;
kiSlowColorBuffer[barIndex] = iKIColor;
//
iBar.Clean();
}
/**
* Calculate KI Super Slow Bufer ...
*
* @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 CalculateKISuperSlow(
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;
//
XOHCL iBar;
bool isInited = iBar.Init(
_Symbol,
_Period,
barIndex //
);
//
double iClose = close[barIndex];
//
// Calculate KI / Color ...
double lastKI =
isFirstBar
? emptyValue
: kiSuperSlowBuffer[lastBarIndex];
double iKI = lastKI;
if (isInited)
{
//
double iLLKI = iBar.FindLowest(kiSuperSlowLength, MODE_LOW);
double iHHKI = iBar.FindHighest(kiSuperSlowLength, MODE_HIGH);
//
iKI = (iHHKI + iLLKI) / 2;
if (iLLKI == 0 || iHHKI == 0)
{
iKI = lastKI;
}
}
else
{
iKI = lastKI;
}
kiSuperSlowBuffer[barIndex] = iKI;
//
double iKIState = neuturalState;
double iKIColor = neuturalColorIDX;
if (iClose > iKI)
{
//
iKIState = bullishState;
iKIColor =
!showKISuperSlow
? hideColorIDX
: bullishColorIDX;
}
else if (iClose < iKI)
{
//
iKIState = bearishState;
iKIColor =
!showKISuperSlow
? hideColorIDX
: bearishColorIDX;
}
kiSuperSlowStateBuffer[barIndex] = iKIState;
kiSuperSlowColorBuffer[barIndex] = iKIColor;
//
iBar.Clean();
}
/**
* Calculate PV Bufer ...
*
* @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 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;
//
XOHCL iBar;
bool isInited = iBar.Init(
_Symbol,
_Period,
barIndex //
);
//
// Calculate Peak ...
double lastPeak =
isFirstBar
? emptyValue
: peakBuffer[lastBarIndex];
double lastVale =
isFirstBar
? emptyValue
: valeBuffer[lastBarIndex];
double iPeak = lastPeak;
double iVale = lastVale;
if (isInited)
{
//
int length = maxLength;
//
double iLL = iBar.FindLowest(length, MODE_LOW);
double iHH = iBar.FindHighest(length, MODE_HIGH);
//
double iLL2 = iBar.FindLowest(length / 2, MODE_LOW);
double iHH2 = iBar.FindHighest(length / 2, MODE_HIGH);
//
iPeak = iHH == iHH2 ? iHH : lastPeak;
iVale = iLL == iLL2 ? iLL : lastVale;
//
lastPeak = iPeak;
lastVale = iVale;
}
else
{
//
iPeak = lastPeak;
iVale = lastVale;
}
peakBuffer[barIndex] = iPeak;
valeBuffer[barIndex] = iVale;
//
iBar.Clean();
}
/**
* Calculate ATR and Ki Upper/Lower Buffers ...
*
* @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 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 iKI = kiBuffer[barIndex];
double iAtr = atrBuffer[barIndex];
double iAppliedAtr = atrMultiplier <= 0
? iAtr
: atrMultiplier * iAtr;
//
double iPriceMid = 0;
if (IsXValid(atrUpperPriceType) &&
IsXValid(atrLowerPriceType))
{
//
double iUpperPrice = GetAppliedPrice(
atrUpperPriceType,
open,
high,
low,
close,
barIndex //
);
double iLowerPrice = GetAppliedPrice(
atrLowerPriceType,
open,
high,
low,
close,
barIndex //
);
iPriceMid = ((iUpperPrice - iLowerPrice) / 2);
}
//
double iUpperValue = iKI + iPriceMid + iAppliedAtr;
double iLowerValue = iKI - iPriceMid - iAppliedAtr;
//
atrRawUpperBuffer[barIndex] = iUpperValue;
atrRawLowerBuffer[barIndex] = iLowerValue;
//
// Check Can Smooth Atr ...
if (IsXValid(atrSmoothingMode) &&
atrSmoothingLength > 0)
{
//
// Smoothed ATR ...
iMAOnBuffer(
ratesTotal,
prevCalculated,
barIndex,
atrSmoothingLength,
atrBuffer,
atrSmoothedBuffer,
atrSmoothingMode //
);
//
// Upper ...
iMAOnBuffer(
ratesTotal,
prevCalculated,
barIndex,
atrSmoothingLength,
atrRawUpperBuffer,
kiUpperBuffer,
atrSmoothingMode //
);
//
// Lower ...
iMAOnBuffer(
ratesTotal,
prevCalculated,
barIndex,
atrSmoothingLength,
atrRawLowerBuffer,
kiLowerBuffer,
atrSmoothingMode //
);
}
else
{
//
atrSmoothedBuffer[barIndex] = iAtr;
kiUpperBuffer[barIndex] = iUpperValue;
kiLowerBuffer[barIndex] = iLowerValue;
}
}
/**
* Calculate RSI and Ki Wave Buffers ...
*
* @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 CalculateRSI(
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 iKI = kiBuffer[barIndex];
double iRsi = rsiBuffer[barIndex];
double iAppliedRsi = rsiMultiplier <= 0
? iRsi
: rsiMultiplier * iRsi;
//
double iValue = iKI + iAppliedRsi;
//
double iState =
iRsi >= rsiBullishConfirmation
? bullishState
: iRsi <= rsiBearishConfirmation
? bearishState
: neuturalState;
double iColor =
iRsi >= rsiBullishConfirmation
? bullishColorIDX
: iRsi <= rsiBearishConfirmation
? bearishColorIDX
: neuturalColorIDX;
if (!showKIWave)
{
iColor = hideColorIDX;
}
//
kiWaveRawBuffer[barIndex] = iValue;
kiWaveColorBuffer[barIndex] = iColor;
kiWaveStateBuffer[barIndex] = iState;
//
// Check Can Smooth KI Wave ...
if (IsXValid(kiWaveSmoothingMode) &&
kiWaveSmoothingLength > 0)
{
//
// Ki Wave ...
iMAOnBuffer(
ratesTotal,
prevCalculated,
barIndex,
kiWaveSmoothingLength,
kiWaveRawBuffer,
kiWaveBuffer,
kiWaveSmoothingMode //
);
}
else
{
//
kiWaveBuffer[barIndex] = iValue;
}
//
// Check Can Smooth RSI ...
if (IsXValid(rsiSmoothingMode) &&
rsiSmoothingLength > 0)
{
//
// Ki Wave ...
iMAOnBuffer(
ratesTotal,
prevCalculated,
barIndex,
rsiSmoothingLength,
rsiBuffer,
rsiSmoothedBuffer,
rsiSmoothingMode //
);
}
else
{
//
rsiSmoothedBuffer[barIndex] = iRsi;
}
}
// #endregion