Files
xMQL5/BKPS/XAU5-14021118-Full/Indicators/x-saherelm.xtd.oscillator.mq5
T
2024-02-07 14:46:54 +03:30

466 lines
11 KiB
Plaintext

///////////////////////////////////////////////////////
//
// SaherElm IT Center MQL5 Trend Detector Oscillator
// --------------------------------------------------
// Name: XTD
// Description: trend detecting in both sides ...
//
//
// 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 XTD Oscillator"
#property strict
//
// START Constants ...
//
#define ShortName "XTD"
//
// XTD Oscillator States ...
enum ENUM_X_XTD_STATES
{
X_XTD_BULLISH_CROSSED_OVER_BEARISH = 2,
X_XTD_BULLISH_OVER_BEARISH = 1,
X_XTD_NEUTURAL = 0,
X_XTD_BULLISH_UNDER_BEARISH = -1,
X_XTD_BULLISH_CROSSED_UNDER_BEARISH = -2
};
//
// END Constants ...
//
//
// Include Models Library ...
#include "../Libraries/x-saherelm.models.lib.mq5"
//
// START Inputs ...
//
input int length = 14; // Market Length
input bool drawCrosses = true; // Draw Cross Arrows
//
// Bullish ...
input uchar bullishArrowCode = 228; // Cross Over Arrow Code
input color bullishArrowColor = clrAqua; // Cross Over Arrow Color
//
// Bearish ...
input uchar bearishArrowCode = 230; // Cross Under Arrow Code
input color bearishArrowColor = clrMagenta; // Cross Under Arrow Color
//
// END Inputs ...
//
//
// Includes Logging Library ...
#include "../Libraries/x-saherelm.log.lib.mq5";
//
// Includes Common Library ...
#include "../Libraries/x-saherelm.common.lib.mq5";
//
// Includes Draw Library ...
#include "../Libraries/x-saherelm.draw.lib.mq5";
//
// START Buffers ...
//
#property indicator_separate_window
//
#property indicator_buffers 3
#property indicator_plots 3
//
#define bullishBufferIndex 0
#define bearishBufferIndex 1
#define signalBufferIndex 2
//
double bullishBuffer[];
double bearishBuffer[];
double signalBuffer[];
//
#property indicator_label1 "XBullPower"
#property indicator_type1 DRAW_LINE
#property indicator_color1 clrLime
#property indicator_style1 STYLE_SOLID
#property indicator_width1 1
//
#property indicator_label2 "XBearPower"
#property indicator_type2 DRAW_LINE
#property indicator_color2 clrRed
#property indicator_style2 STYLE_SOLID
#property indicator_width2 1
//
#property indicator_label3 "XSignal"
#property indicator_type3 DRAW_LINE
#property indicator_color3 clrGold
#property indicator_style3 STYLE_DOT
#property indicator_width3 1
//
// END Buffers ...
//
//
// START Global Definitions: Variables, Properties and etc ...
//
//
int xtdBullishCandleCount = 0;
int xtdBearishCandleCount = 0;
//
// END Global Definitions: Variables, Properties and etc ...
//
//
// START Event Handlers ...
//
//
// Initialization ...
int OnInit()
{
//
// Validate Inputs ...
if (!ValidateInputs())
{
return INIT_PARAMETERS_INCORRECT;
}
//
logTag = ShortName;
drawPrefix = ShortName;
//
// Define Index Buffers ...
DefineBuffers();
//
// Set Indicator ShortName ...
SetIndicatorName();
//
// Init Succeed ...
return INIT_SUCCEEDED;
}
//
// DeInitialization ...
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
//
// Here we can handle De Initialization Reasons ...
//
// De Init Draw Library ...
OnDeinitDrawLibrary();
}
//
// Calculations ...
//
// Calculating what we want ...
int OnCalculate(
//
// total Candles on chart ...
const int rates_total,
//
// total calculated Candles on charts ...
const int prev_calculated,
//
// history of Candles Open Time ...
const datetime &time[],
//
// history of Candles Open Price ...
const double &open[],
//
// history of Candles High Price ...
const double &high[],
//
// history of Candles Low Price ...
const double &low[],
//
// history of Candles Close Price ...
const double &close[],
//
// history of Tick Volumes on Candle ...
const long &tick_volume[],
//
// history of Trade Volumes ...
const long &volume[],
//
// history of Candles Spread Price ...
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);
//
// 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, length);
//
limit = prev_calculated < maxLength ? rates_total - (maxLength + 1) : rates_total - prev_calculated;
//
// Main Loop ...
for (int i = limit; i >= 0 && !IsStopped(); i--)
{
//
CalculateBuffers(i, close);
}
//
return rates_total;
}
//
// END Event Handlers ...
//
//
// START Functions ...
//
//
// Validate Input Args for Initialization ...
bool ValidateInputs()
{
//
bool result = false;
//
// Validate Args ...
if (length >= 5)
{
result = true;
}
//
return result;
}
//
// Define Indexes and Styles ...
void DefineBuffers()
{
//
// Bullish Buffer ...
string bullishBufferLabel = ShortName + "BullPow " + "(" + (string)length + ")";
ArraySetAsSeries(bullishBuffer, true);
SetIndexBuffer(bullishBufferIndex, bullishBuffer, INDICATOR_DATA);
PlotIndexSetInteger(bullishBufferIndex, PLOT_DRAW_BEGIN, length);
PlotIndexSetString(bullishBufferIndex, PLOT_LABEL, bullishBufferLabel);
//
// Bearish Buffer ...
string bearishBufferLabel = ShortName + "BearPow " + "(" + (string)length + ")";
ArraySetAsSeries(bearishBuffer, true);
SetIndexBuffer(bearishBufferIndex, bearishBuffer, INDICATOR_DATA);
PlotIndexSetInteger(bearishBufferIndex, PLOT_DRAW_BEGIN, length);
PlotIndexSetString(bearishBufferIndex, PLOT_LABEL, bearishBufferLabel);
//
// Signal Buffer ...
string signalBufferLabel = ShortName + "Signal " + "(" + (string)length + ")";
ArraySetAsSeries(signalBuffer, true);
SetIndexBuffer(signalBufferIndex, signalBuffer, INDICATOR_DATA);
PlotIndexSetInteger(signalBufferIndex, PLOT_DRAW_BEGIN, length);
PlotIndexSetString(signalBufferIndex, PLOT_LABEL, signalBufferLabel);
}
//
// Set Indicator Short Name and also we can define Buffers Labels ...
void SetIndicatorName()
{
//
string indicatorShortName = "";
StringConcatenate(indicatorShortName, ShortName, " ", "( ", length, " )");
//
IndicatorSetString(INDICATOR_SHORTNAME, indicatorShortName);
}
//
// Calculate Buffers ...
void CalculateBuffers(
int bar_index,
const double &close[])
{
//
// Reset Global Counters ...
xtdBullishCandleCount = 0;
xtdBearishCandleCount = 0;
//
// Loop through Closes ...
for (int i = bar_index; i <= bar_index + length; i++)
{
for (int j = i + 1; j <= bar_index + length; j++)
{
//
if (close[i] > close[j])
{
xtdBullishCandleCount++;
}
//
if (close[i] < close[j])
{
xtdBearishCandleCount++;
}
}
}
//
// Bullish Power Buffer ...
bullishBuffer[bar_index] = MathAbs(xtdBullishCandleCount);
//
// Bearish Power Buffer ...
bearishBuffer[bar_index] = MathAbs(xtdBearishCandleCount);
//
// Signal Buffer ...
signalBuffer[bar_index] = bullishBuffer[bar_index] - bearishBuffer[bar_index];
//
// Check and Draw Crosses if it's Specified ...
if (drawCrosses)
{
DrawCrosses(bar_index);
}
}
//
//
double GetState(
int bar_index // Specified Bar Index
)
{
//
// Check Cross Points ...
// bullishCrossedOverBearish = 2;
// bullishOverBearish = 1;
// bullishCrossedUnderBearish = -2;
// bullishUnderBearish = -1;
// OtherWise = 0;
bool isBullishCrossedOverBearish = IsCrossOver(
bullishBuffer,
bearishBuffer,
bar_index);
bool isBullishCrossedUnderBearish = IsCrossUnder(
bullishBuffer,
bearishBuffer,
bar_index);
bool isBullishOverBearish = IsOver(
bullishBuffer,
bearishBuffer,
bar_index);
bool isBullishUnderBearish = IsUnder(
bullishBuffer,
bearishBuffer,
bar_index);
//
double result = X_XTD_NEUTURAL;
if (isBullishCrossedOverBearish)
{
result = X_XTD_BULLISH_CROSSED_OVER_BEARISH;
}
else if (isBullishCrossedUnderBearish)
{
result = X_XTD_BULLISH_CROSSED_UNDER_BEARISH;
}
else if (isBullishOverBearish)
{
result = X_XTD_BULLISH_OVER_BEARISH;
}
else if (isBullishUnderBearish)
{
result = X_XTD_BULLISH_UNDER_BEARISH;
}
else
{
result = X_XTD_NEUTURAL;
}
//
return result;
}
//
// Draw Crosses ...
void DrawCrosses(
int bar_index // the bars which required to draw cross arrow on it ...
)
{
//
double stateValue = GetState(bar_index);
//
bool isOver = stateValue == 2;
double low = iLow(_Symbol, _Period, bar_index);
double high = iHigh(_Symbol, _Period, bar_index);
//
double price = isOver ? low : high;
datetime time = iTime(_Symbol, _Period, bar_index);
ENUM_ARROW_ANCHOR anchor = isOver ? ANCHOR_TOP : ANCHOR_BOTTOM;
uchar arrowCode = isOver ? bullishArrowCode : bearishArrowCode;
color arrowColor = isOver ? bullishArrowColor : bearishArrowColor;
string crossName = "Trend " + (isOver ? "Up" : "Down") + "_" + (string)time + "_" + (string)price;
//
DrawArrow(
0,
crossName,
0,
time,
price,
arrowCode,
anchor,
arrowColor);
}
//
// END Functions ...
//