Files
xMQL4/xIndicators/x-saherelm.xrange.mq4
T
2024-01-25 04:03:36 +03:30

294 lines
6.0 KiB
Plaintext

///////////////////////////////////////////////////////
//
// SaherElm IT Center XRange Indicator
// ---------------------------------------------
// saherelm implementation of above 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 XRange Indicator"
#property strict
//
// START Inputs ...
//
input int marketLength = 14;
//
// END Inputs ...
//
//
// START Global Definitions: Variables, Properties and etc ...
//
#property indicator_chart_window
//
#property indicator_buffers 3
//
#property indicator_plots 3
//
// Start Define Indicator Buffer Styles ...
//
//
// Highest High Buffer ...
#property indicator_type1 DRAW_LINE
#property indicator_color1 clrGreen
#property indicator_style1 STYLE_DOT
#property indicator_width1 1
//
// Lowest Low Buffer ...
#property indicator_type2 DRAW_LINE
#property indicator_color2 clrRed
#property indicator_style2 STYLE_DOT
#property indicator_width2 1
//
// Middle Buffer ...
#property indicator_type3 DRAW_LINE
#property indicator_color3 clrYellow
#property indicator_style3 STYLE_DASH
#property indicator_width3 1
//
// End Define Indicator Buffer Styles ...
//
//
// Buffers ...
//
#define highestHighBufferIndex 0
#define lowestLowBufferIndex 1
#define middleBufferIndex 2
//
double highestHighBuffer[];
double lowestLowBuffer[];
double middleBuffer[];
//
datetime startTime;
//
// END Global Definitions: Variables, Properties and etc ...
//
//
// Start Event Handlers ...
//
//
// Initialization ...
int OnInit() {
//
// Initialize what we want ...
if (
marketLength <= 0
) {
return INIT_PARAMETERS_INCORRECT;
}
//
string hhLbl = StringConcatenate(
"XR HH(", marketLength, ")"
);
SetIndexBuffer(highestHighBufferIndex, highestHighBuffer);
SetIndexLabel(highestHighBufferIndex, hhLbl);
//
string llLbl = StringConcatenate(
"XR LL(", marketLength, ")"
);
SetIndexBuffer(lowestLowBufferIndex, lowestLowBuffer);
SetIndexLabel(lowestLowBufferIndex, llLbl);
//
string midLbl = StringConcatenate(
"XR Mid(", marketLength, ")"
);
SetIndexBuffer(middleBufferIndex, middleBuffer);
SetIndexLabel(middleBufferIndex, midLbl);
//
return INIT_SUCCEEDED;
}
//
// Calculating what we want ...
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[]
) {
//
// this counts Available Bars ...
int limit;
//
// 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 ...
int maxLength = MathMax(0, marketLength);
//
// input variable, we return 0 means nothing passed ...
if (rates_total < maxLength) {
return 0;
}
//
// found which candles calculated before ...
limit = (prev_calculated == 0) ? rates_total - maxLength - 1 : rates_total - prev_calculated + 1;
//
// this is the main loop of calculations, for each bar index ...
for (int i = limit - 1; i >= 0; i--) {
//
// Start Calculation here ...
// i is bar_index ...
//
// Calculations ...
CalculateBuffers(i);
}
//
return rates_total;
}
//
// END Event Handlers ...
//
//
// START Functions ...
//
//
// Calculations ...
void CalculateBuffers(
const int &bar_index
) {
//
// Retrieve Bar Time ...
datetime barTime = iTime(
_Symbol,
_Period,
bar_index
);
//
if (startTime == 0) {
//
startTime = barTime;
SetIndicatorBuffersNone(bar_index);
return;
}
//
int startBarIndex = iBarShift(
_Symbol,
_Period,
startTime
);
//
int startDistance = startBarIndex - bar_index;
if (startDistance < marketLength) {
//
SetIndicatorBuffersNone(bar_index);
return;
}
//
// Highest High ...
int hhIdx = iHighest(
_Symbol,
_Period,
MODE_HIGH,
marketLength,
bar_index
);
double hh = iHigh(
_Symbol,
_Period,
hhIdx
);
highestHighBuffer[bar_index] = hh;
//
// Lowest Low ...
int llIdx = iLowest(
_Symbol,
_Period,
MODE_LOW,
marketLength,
bar_index
);
double ll = iLow(
_Symbol,
_Period,
llIdx
);
lowestLowBuffer[bar_index] = ll;
//
// Middle Buffer ...
double mid = (hh + ll) / 2;
middleBuffer[bar_index] = mid;
//
// Reset Start Time ...
startTime = 0;
}
//
void SetIndicatorBuffersNone(
const int bar_index
) {
//
int lastBarIndex = bar_index + 1;
//
double lastHighestHigh;
double lastLowestLow;
//
// Highest High ...
if (ArraySize(highestHighBuffer) > lastBarIndex) {
lastHighestHigh = highestHighBuffer[lastBarIndex];
} else {
lastHighestHigh = 0;
}
//
// Lowest Low ...
if (ArraySize(lowestLowBuffer) > lastBarIndex) {
lastLowestLow = lowestLowBuffer[lastBarIndex];
} else {
lastLowestLow = 0;
}
//
highestHighBuffer[bar_index] = lastHighestHigh;
lowestLowBuffer[bar_index] = lastLowestLow;
//
double mid = (lastHighestHigh + lastLowestLow) / 2;
middleBuffer[bar_index] = mid;
}
//
// END Functions ...
//