complete XHK Indicator and it's related Helper and also Attached it to x121 Helper ...

This commit is contained in:
2025-01-25 21:42:37 +03:30
parent 6959bc83db
commit 8e502953a3
4 changed files with 1923 additions and 261 deletions
+226 -258
View File
@@ -35,19 +35,9 @@
//
// Market ...
input group "Market";
//
input group "RSI Detection";
input int rsiLength = 14; // Length
input ENUM_APPLIED_PRICE rsiAppliedTo = PRICE_CLOSE; // Applied To
//
input group "ATR Detection";
input int atrLength = 14; // Length
input double atrMultiplier = 1; // Multiplier
input ENUM_APPLIED_PRICE atrUpperAppliedTo = PRICE_HIGH; // Upper Zone Applied To
input ENUM_APPLIED_PRICE atrLowerAppliedTo = PRICE_LOW; // Lower Zone Applied To
input ENUM_X_MA_METHOD atrSmoothingMode = X_MA_MODE_EMA; // Smoothing Method
input int xhkSmoothingLength = 14; // Length
input ENUM_X_MA_METHOD xhkSmoothingMode = X_MA_MODE_EMA; // Smoothing Method
input bool xhkIgnoreSmoothingShadows = false; // Ignore Smoothed Bar Shadows
//
input group "Presentation";
@@ -56,12 +46,19 @@ input group "Presentation";
input int startCalculationForLastBars = 1000; // Calculate Last n Bars
//
input bool showAtrUpper = true; // Show Upper Zone
input bool showAtrLower = true; // Show Lower Zone
input bool drawRawXHKCandles = true; // Draw Raw Candle
input bool drawSmoothedXHKCandles = true; // Draw Smoothed Candle
//
// Buffers ...
//
#property indicator_chart_window
//
#property indicator_buffers 10
#property indicator_plots 2
//
#define hideColorIDX 0
#define bullishColorIDX 1
@@ -69,63 +66,64 @@ input bool showAtrLower = true; // Show Lower Zone
#define neuturalColorIDX 3
//
#property indicator_chart_window
// PLOTTED Buffers ...
//
#property indicator_buffers 6
#property indicator_plots 2
#define openHKBufferIndex 0
double openHKBuffer[];
//
// ATR ...
#define highHKBufferIndex 1
double highHKBuffer[];
//
// Upper ...
#define lowHKBufferIndex 2
double lowHKBuffer[];
//
#define atrUpperBufferIndex 0
double atrUpperBuffer[];
#property indicator_label1 "X121 ATRU"
#property indicator_type1 DRAW_LINE
#property indicator_color1 clrYellow
#property indicator_style1 STYLE_SOLID
#property indicator_width1 2
#define closeHKBufferIndex 3
double closeHKBuffer[];
//
// Lower ...
#define rawHKColorBufferIndex 4
double rawHKColorBuffer[];
//
#define atrLowerBufferIndex 1
double atrLowerBuffer[];
#property indicator_label2 "X121 ATRL"
#property indicator_type2 DRAW_LINE
#property indicator_color2 clrYellow
#property indicator_style2 STYLE_SOLID
#property indicator_width2 2
#define rawHKBufferIndex 0
#property indicator_label1 "X121 XHK Open;X121 XHK High;X121 XHK Low;X121 XHK Close"
#property indicator_type1 DRAW_COLOR_CANDLES
#property indicator_color1 CLR_NONE, clrDodgerBlue, clrDarkGoldenrod
//
// Data Buffers ...
// Open ...
#define openBufferIndex 5
double openBuffer[];
//
int mLastBufferIndex = 1;
// High ...
#define highBufferIndex 6
double highBuffer[];
//
// ATR ...
#define atrBufferIndex mLastBufferIndex + 1
double atrBuffer[];
#define atrUpperRawBufferIndex mLastBufferIndex + 2
double atrUpperRawBuffer[];
#define atrLowerRawBufferIndex mLastBufferIndex + 3
double atrLowerRawBuffer[];
// Low ...
#define lowBufferIndex 7
double lowBuffer[];
//
// RSI ...
#define rsiBufferIndex mLastBufferIndex + 4
double rsiBuffer[];
// Close ...
#define closeBufferIndex 8
double closeBuffer[];
//
// Candle Color ...
#define candleColorBufferIndex 9
double candleColorBuffer[];
//
#define candlesBufferIndex 1
#property indicator_label2 "X121 XHKSM Open;X121 XHKSM High;X121 XHKSM Low;X121 XHKSM Close"
#property indicator_type2 DRAW_COLOR_CANDLES
#property indicator_color2 CLR_NONE, clrAqua, clrMagenta
//
// Variables, Properties and etc ...
@@ -138,17 +136,6 @@ int limit;
int maxLength;
//
// RSI Handler ...
int rsiHandler = INVALID_HANDLE;
//
// ATR Handler ...
int atrHandler = INVALID_HANDLE;
//
bool isStrTrendChanged;
bool isStrStartBearishTrend;
bool isStrStartBullishTrend;
//
// Event Handlers ...
@@ -170,33 +157,6 @@ int OnInit()
//
// Initialize Indicator Handlers ...
//
// RSI ...
rsiHandler = iRSI(
_Symbol,
_Period,
rsiLength,
rsiAppliedTo //
);
bool isInited = rsiHandler != INVALID_HANDLE;
if (!isInited)
{
return INIT_FAILED;
}
//
// ATR ...
atrHandler = iATR(
_Symbol,
_Period,
atrLength //
);
isInited = atrHandler != INVALID_HANDLE;
if (!isInited)
{
return INIT_FAILED;
}
//
// because in some cases we may have more than one input for
// calculation and we must prevent any calculation
@@ -237,8 +197,6 @@ void OnDeinit(const int reason)
// REASON_CLOSE 9 Terminal closed
//
IndicatorRelease(rsiHandler);
IndicatorRelease(atrHandler);
}
/**
@@ -287,65 +245,11 @@ int OnCalculate(
{
}
//
// Validate Calculated Bars ...
//
// RSI ...
int rsiCalculatedBars = BarsCalculated(rsiHandler);
//
// ATR ...
int atrCalculatedBars = BarsCalculated(atrHandler);
//
bool isPassedRequiredCalculatedBars =
//
// RSI ...
rsiCalculatedBars >= 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 ...
//
// RSI ...
int copiedRsis = CopyBuffer(rsiHandler, 0, 0, limit, rsiBuffer);
//
// ATR ...
int copiedAtrs = CopyBuffer(atrHandler, 0, 0, limit, atrBuffer);
//
// Validate Copied Items ...
bool isPassedRequiredCopiedItems =
//
//
// RSI ...
copiedRsis > 0 &&
//
// ATR ...
copiedAtrs > 0
//
;
if (!isPassedRequiredCopiedItems)
{
return prev_calculated;
}
//
// Main Loop ...
for (int i = limit - 1; i >= 0 && !IsStopped(); i--)
@@ -381,12 +285,7 @@ bool ValidateInputs()
//
bool result =
//
// RSI ...
rsiLength > 0 &&
//
// ATR ...
atrLength > 0 &&
atrMultiplier > 0
true
//
;
@@ -405,7 +304,7 @@ int ExtractMaxLengthOfInputs()
int result = 0;
//
result = MathMax(rsiLength, atrLength);
result = MathMax(0, xhkSmoothingLength);
//
return result;
@@ -416,53 +315,72 @@ int ExtractMaxLengthOfInputs()
*/
void DefineBuffers()
{
//
// ATR ...
//
// UPPER ...
bool canShowAtrUpper = showAtrUpper;
ENUM_DRAW_TYPE atrUpperDrawType = canShowAtrUpper ? DRAW_LINE : DRAW_NONE;
ArraySetAsSeries(atrUpperBuffer, true);
SetIndexBuffer(atrUpperBufferIndex, atrUpperBuffer, INDICATOR_DATA);
PlotIndexSetDouble(atrUpperBufferIndex, PLOT_EMPTY_VALUE, EMPTY_VALUE);
PlotIndexSetInteger(atrUpperBufferIndex, PLOT_SHOW_DATA, canShowAtrUpper);
PlotIndexSetInteger(atrUpperBufferIndex, PLOT_DRAW_TYPE, atrUpperDrawType);
// RAW ...
//
// LOWER ...
bool canShowAtrLower = showAtrLower;
ENUM_DRAW_TYPE atrLowerDrawType = canShowAtrLower ? DRAW_LINE : DRAW_NONE;
ArraySetAsSeries(atrLowerBuffer, true);
SetIndexBuffer(atrLowerBufferIndex, atrLowerBuffer, INDICATOR_DATA);
PlotIndexSetDouble(atrLowerBufferIndex, PLOT_EMPTY_VALUE, EMPTY_VALUE);
PlotIndexSetInteger(atrLowerBufferIndex, PLOT_SHOW_DATA, canShowAtrLower);
PlotIndexSetInteger(atrLowerBufferIndex, PLOT_DRAW_TYPE, atrLowerDrawType);
// Raw Candles Color ...
ArraySetAsSeries(rawHKColorBuffer, true);
SetIndexBuffer(rawHKColorBufferIndex, rawHKColorBuffer, INDICATOR_COLOR_INDEX);
//
// Data Buffers ...
// Set EMPTY_VALUE on Candles Buffer ...
PlotIndexSetDouble(rawHKBufferIndex, PLOT_EMPTY_VALUE, 0.0);
PlotIndexSetInteger(rawHKBufferIndex, PLOT_SHOW_DATA, false);
//
// ATR ...
// Open ...
ArraySetAsSeries(openHKBuffer, true);
SetIndexBuffer(openHKBufferIndex, openHKBuffer, INDICATOR_DATA);
//
ArraySetAsSeries(atrBuffer, true);
SetIndexBuffer(atrBufferIndex, atrBuffer, INDICATOR_CALCULATIONS);
// High ...
ArraySetAsSeries(highHKBuffer, true);
SetIndexBuffer(highHKBufferIndex, highHKBuffer, INDICATOR_DATA);
//
ArraySetAsSeries(atrUpperRawBuffer, true);
SetIndexBuffer(atrUpperRawBufferIndex, atrUpperRawBuffer, INDICATOR_CALCULATIONS);
// Close ...
ArraySetAsSeries(closeHKBuffer, true);
SetIndexBuffer(closeHKBufferIndex, closeHKBuffer, INDICATOR_DATA);
//
ArraySetAsSeries(atrLowerRawBuffer, true);
SetIndexBuffer(atrLowerRawBufferIndex, atrLowerRawBuffer, INDICATOR_CALCULATIONS);
// Low ...
ArraySetAsSeries(lowHKBuffer, true);
SetIndexBuffer(lowHKBufferIndex, lowHKBuffer, INDICATOR_DATA);
//
// RSI ...
// SMOOTHED ...
//
ArraySetAsSeries(rsiBuffer, true);
SetIndexBuffer(rsiBufferIndex, rsiBuffer, INDICATOR_CALCULATIONS);
// Candles Color ...
ArraySetAsSeries(candleColorBuffer, true);
SetIndexBuffer(candleColorBufferIndex, candleColorBuffer, INDICATOR_COLOR_INDEX);
//
// Set EMPTY_VALUE on Candles Buffer ...
PlotIndexSetDouble(candlesBufferIndex, PLOT_EMPTY_VALUE, 0.0);
PlotIndexSetInteger(candlesBufferIndex, PLOT_SHOW_DATA, false);
//
// Open ...
ArraySetAsSeries(openBuffer, true);
SetIndexBuffer(openBufferIndex, openBuffer, INDICATOR_DATA);
//
// High ...
ArraySetAsSeries(highBuffer, true);
SetIndexBuffer(highBufferIndex, highBuffer, INDICATOR_DATA);
//
// Close ...
ArraySetAsSeries(closeBuffer, true);
SetIndexBuffer(closeBufferIndex, closeBuffer, INDICATOR_DATA);
//
// Low ...
ArraySetAsSeries(lowBuffer, true);
SetIndexBuffer(lowBufferIndex, lowBuffer, INDICATOR_DATA);
}
/**
@@ -501,14 +419,13 @@ void CalculateBuffers(
? startCalculationForLastBars
: 0;
//
// bool canCalculate = true;
bool canCalculate =
barsLimit == 0 ||
bar_index <= barsLimit;
if (canCalculate)
{
//
CalculateAtrZones(
CalculateXHK(
bar_index,
prevCalculated,
ratesTotal,
@@ -535,16 +452,18 @@ void CalculateBuffers(
void FillBuffersZero(int barIndex)
{
//
// RSIS ...
rsiBuffer[barIndex] = 0;
lowHKBuffer[barIndex] = 0.0;
openHKBuffer[barIndex] = 0.0;
highHKBuffer[barIndex] = 0.0;
closeHKBuffer[barIndex] = 0.0;
rawHKColorBuffer[barIndex] = hideColorIDX;
//
// ATRS ...
atrBuffer[barIndex] = 0;
atrUpperBuffer[barIndex] = 0;
atrUpperRawBuffer[barIndex] = 0;
atrUpperBuffer[barIndex] = 0;
atrLowerRawBuffer[barIndex] = 0;
lowBuffer[barIndex] = 0.0;
openBuffer[barIndex] = 0.0;
highBuffer[barIndex] = 0.0;
closeBuffer[barIndex] = 0.0;
candleColorBuffer[barIndex] = hideColorIDX;
}
/**
@@ -558,7 +477,7 @@ void FillBuffersZero(int barIndex)
* @param close: Double Array, History of Close Prices ...
* @param low: Double Array, History of Low Prices ...
*/
void CalculateAtrZones(
void CalculateXHK(
int bar_index, // Selected Bar Index
const int prevCalculated,
const int ratesTotal,
@@ -569,79 +488,128 @@ void CalculateAtrZones(
)
{
//
// Select ATr Value ...
double atrValue = atrBuffer[bar_index];
double appliedAtrMultiplierValue = atrValue * atrMultiplier;
double mPrevHKOpenValue;
double mPrevHKCloseValue;
//
// Select Upper Price ...
double upperPrice = GetAppliedPrice(
atrUpperAppliedTo,
open,
high,
low,
close,
bar_index //
);
//
// Select Lower Price ...
double lowerPrice = GetAppliedPrice(
atrLowerAppliedTo,
open,
high,
low,
close,
bar_index //
);
//
// Calculate Raw Atrs ...
//
// Upper ...
double atrUpperRawValue = upperPrice + appliedAtrMultiplierValue;
atrUpperRawBuffer[bar_index] = atrUpperRawValue;
//
// Lower ...
double atrLowerRawValue = lowerPrice - appliedAtrMultiplierValue;
atrLowerRawBuffer[bar_index] = atrLowerRawValue;
//
bool canSmooth = atrSmoothingMode != X_MA_MODE_NONE;
if (canSmooth)
if (ArraySize(open) <= bar_index + 1)
{
//
// Upper ...
iMAOnBuffer(
ratesTotal,
prevCalculated,
bar_index,
atrLength,
atrUpperRawBuffer,
atrUpperBuffer,
atrSmoothingMode //
);
//
// Lower ...
iMAOnBuffer(
ratesTotal,
prevCalculated,
bar_index,
atrLength,
atrLowerRawBuffer,
atrLowerBuffer,
atrSmoothingMode //
);
mPrevHKOpenValue = 0;
mPrevHKCloseValue = 0;
}
else
{
//
// Use Raw Values ...
atrUpperBuffer[bar_index] = atrUpperRawValue;
atrLowerBuffer[bar_index] = atrLowerRawValue;
mPrevHKOpenValue = openHKBuffer[bar_index + 1];
mPrevHKCloseValue = closeHKBuffer[bar_index + 1];
}
//
double mHKOpenValue = (mPrevHKOpenValue + mPrevHKCloseValue) / 2;
double mHKCloseValue = (open[bar_index] + high[bar_index] + close[bar_index] + low[bar_index]) / 4;
double mHKHighValue = MathMax(high[bar_index], MathMax(mHKOpenValue, mHKCloseValue));
double mHKLowValue = MathMin(low[bar_index], MathMin(mHKOpenValue, mHKCloseValue));
//
double candleColorValue =
mHKOpenValue < mHKCloseValue ? bullishColorIDX : bearishColorIDX;
//
// Raw HK Buffers ...
openHKBuffer[bar_index] = mHKOpenValue;
highHKBuffer[bar_index] = mHKHighValue;
lowHKBuffer[bar_index] = mHKLowValue;
closeHKBuffer[bar_index] = mHKCloseValue;
rawHKColorBuffer[bar_index] = drawRawXHKCandles ? candleColorValue : hideColorIDX;
//
// Apply Smoothing ...
bool canSmooth = xhkSmoothingLength > 0 &&
xhkSmoothingMode != X_MA_MODE_NONE;
if (!canSmooth)
{
//
double min = MathMin(mHKOpenValue, mHKCloseValue);
double max = MathMax(mHKOpenValue, mHKCloseValue);
//
openBuffer[bar_index] = mHKOpenValue;
highBuffer[bar_index] = xhkIgnoreSmoothingShadows ? max : mHKHighValue;
lowBuffer[bar_index] = xhkIgnoreSmoothingShadows ? min : mHKLowValue;
closeBuffer[bar_index] = mHKCloseValue;
candleColorBuffer[bar_index] = drawSmoothedXHKCandles ? candleColorValue : hideColorIDX;
}
else
{
//
// Open ...
iMAOnBuffer(
ratesTotal,
prevCalculated,
bar_index,
xhkSmoothingLength,
openHKBuffer,
openBuffer,
xhkSmoothingMode //
);
//
// Close ...
iMAOnBuffer(
ratesTotal,
prevCalculated,
bar_index,
xhkSmoothingLength,
closeHKBuffer,
closeBuffer,
xhkSmoothingMode //
);
//
// Ignoring Shadows ...
if (!xhkIgnoreSmoothingShadows)
{
//
// High ...
iMAOnBuffer(
ratesTotal,
prevCalculated,
bar_index,
xhkSmoothingLength,
highHKBuffer,
highBuffer,
xhkSmoothingMode //
);
//
// Low ...
iMAOnBuffer(
ratesTotal,
prevCalculated,
bar_index,
xhkSmoothingLength,
lowHKBuffer,
lowBuffer,
xhkSmoothingMode //
);
}
else
{
//
double min = MathMin(closeBuffer[bar_index], openBuffer[bar_index]);
double max = MathMin(closeBuffer[bar_index], openBuffer[bar_index]);
//
lowBuffer[bar_index] = min;
highBuffer[bar_index] = max;
}
//
// Calculate Smoothed Color ...
candleColorValue =
openBuffer[bar_index] < closeBuffer[bar_index] ? bullishColorIDX : bearishColorIDX;
candleColorBuffer[bar_index] = drawSmoothedXHKCandles ? candleColorValue : hideColorIDX;
}
}