Files
MQL5Data/Indicators/x-saherelm.x121.xmas.mq5
T
2025-03-12 13:33:44 +03:30

575 lines
12 KiB
Plaintext

///////////////////////////////////////////////////////
//
// SaherElm IT Center MQL5 Indicator
// -------------------------------------------------
// Name: X121 XMAS
// Description: XMAS ...
//
//
// 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 X121 XMAS Indicator"
#property strict
//
// Definitions ...
//
#define ShortName "X121_XMAS"
//
// Imports ...
#include "../Libraries/x-saherelm.common.lib.mq5"
//
// Inputs ...
//
// Market ...
input group "Market";
input int length = 14; // MA Length
input ENUM_MA_METHOD method = MODE_EMA; // MA Method
input ENUM_APPLIED_PRICE upperAppliedTo = PRICE_HIGH; // Upper Applied To
input ENUM_APPLIED_PRICE midAppliedTo = PRICE_MEDIAN; // Mid Applied To
input ENUM_APPLIED_PRICE lowerAppliedTo = PRICE_LOW; // Lower Applied To
//
// Presentation ...
input group "Presentation";
//
input int startCalculationForLastBars = 1000; // Calculate Last n Bars
input bool showUpper = true; // Show Upper
input bool showMid = true; // Show Mid
input bool showLower = true; // Show Lower
//
// Buffers ...
//
#define hideColorIDX 0
#define bullishColorIDX 1
#define bearishColorIDX 2
#define neuturalColorIDX 3
//
#property indicator_chart_window
//
#property indicator_buffers 3
#property indicator_plots 3
//
// Plot Buffers ...
//
// UPPER ...
#define upperBufferIndex 0
double upperBuffer[];
//
#property indicator_label1 "X121 MASU"
#property indicator_type1 DRAW_LINE
#property indicator_color1 clrAqua
#property indicator_style1 STYLE_SOLID
#property indicator_width1 1
//
// MID ...
#define midBufferIndex 1
double midBuffer[];
//
#property indicator_label2 "X121 MASM"
#property indicator_type2 DRAW_LINE
#property indicator_color2 clrLightBlue
#property indicator_style2 STYLE_SOLID
#property indicator_width2 1
//
// LOWER ...
#define lowerBufferIndex 2
double lowerBuffer[];
//
#property indicator_label3 "X121 MASL"
#property indicator_type3 DRAW_LINE
#property indicator_color3 clrMagenta
#property indicator_style3 STYLE_SOLID
#property indicator_width3 1
//
// Data Buffers ...
//
int mLastBufferIndex = 2;
//
// Variables, Properties and etc ...
//
// this counts Available Bars ...
int limit;
//
int maxLength;
//
int upperHandler = INVALID_HANDLE;
int midHandler = INVALID_HANDLE;
int lowerHandler = INVALID_HANDLE;
//
// Event Handlers ...
/**
* Initialize Indicator ...
*
* @return ( int )
*/
int OnInit()
{
//
// Validate Inputs ...
if (!ValidateInputs())
{
return INIT_PARAMETERS_INCORRECT;
}
//
// Initialize Indicator Handlers ...
//
// Upper ...
upperHandler = iMA(
_Symbol,
_Period,
length,
0,
method,
upperAppliedTo //
);
bool isInited = upperHandler != INVALID_HANDLE;
if (!isInited)
{
return INIT_FAILED;
}
//
// Mid ...
midHandler = iMA(
_Symbol,
_Period,
length,
0,
method,
midAppliedTo //
);
isInited = midHandler != INVALID_HANDLE;
if (!isInited)
{
return INIT_FAILED;
}
//
// Lower ...
lowerHandler = iMA(
_Symbol,
_Period,
length,
0,
method,
lowerAppliedTo //
);
isInited = lowerHandler != 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
// untill we pass the biggest input length, here we get max Input length
// and then wait until pass it ...
maxLength = ExtractMaxLengthOfInputs();
//
// Define Index Buffers ...
DefineBuffers();
//
// Set Indicator ShortName ...
SetIndicatorName();
IndicatorSetInteger(INDICATOR_DIGITS, 2);
//
// 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
//
IndicatorRelease(upperHandler);
IndicatorRelease(midHandler);
IndicatorRelease(lowerHandler);
}
/**
* 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 ...
//
// Upper ...
int upperCalculatedBars = BarsCalculated(upperHandler);
//
// Mid ...
int midCalculatedBars = BarsCalculated(midHandler);
//
// Lower ...
int lowerCalculatedBars = BarsCalculated(lowerHandler);
//
bool isPassedRequiredCalculatedBars =
//
// Upper ...
upperCalculatedBars >= maxLength &&
//
// Mid ...
midCalculatedBars >= maxLength &&
//
// Lower ...
lowerCalculatedBars >= maxLength
//
;
if (!isPassedRequiredCalculatedBars)
{
return prev_calculated;
}
//
limit = (prev_calculated > rates_total || prev_calculated <= 0)
? rates_total
: (rates_total - prev_calculated) + 1;
//
// Buffers Copy ...
//
// Upper ...
int copiedUppers = CopyBuffer(upperHandler, 0, 0, limit, upperBuffer);
//
// Mid ...
int copiedMids = CopyBuffer(midHandler, 0, 0, limit, midBuffer);
//
// Lower ...
int copiedLowers = CopyBuffer(lowerHandler, 0, 0, limit, lowerBuffer);
//
// Validate Copied Items ...
bool isPassedRequiredCopiedItems =
//
// Upper ...
copiedUppers > 0 &&
//
// Mid ...
copiedMids > 0 &&
//
// Lower ...
copiedLowers > 0
//
;
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;
}
//
// Functions ...
/**
* Validate Input Args for Initialization ...
*
* @return ( bool )
*/
bool ValidateInputs()
{
//
bool result = false;
//
result = length > 0;
//
return result;
}
/**
* Extract Max Length of Inputs ...
*
* @return ( int )
*/
int ExtractMaxLengthOfInputs()
{
//
int result = 0;
//
result = MathMax(result, length);
//
return result;
}
/**
* Set Indicator Short Name and also we can define Buffers Labels ...
*/
void SetIndicatorName()
{
IndicatorSetString(INDICATOR_SHORTNAME, ShortName);
}
/**
* Define Required Buffers ...
*/
void DefineBuffers()
{
//
// Plot Buffers ...
//
// Upper ...
//
ENUM_DRAW_TYPE upperDrawType = showUpper ? DRAW_LINE : DRAW_NONE;
//
ArraySetAsSeries(upperBuffer, true);
SetIndexBuffer(upperBufferIndex, upperBuffer, INDICATOR_DATA);
//
PlotIndexSetInteger(upperBufferIndex, PLOT_SHOW_DATA, showUpper);
PlotIndexSetInteger(upperBufferIndex, PLOT_DRAW_TYPE, upperDrawType);
//
PlotIndexSetDouble(upperBufferIndex, PLOT_EMPTY_VALUE, 0);
//
// Mid ...
//
ENUM_DRAW_TYPE midDrawType = showMid ? DRAW_LINE : DRAW_NONE;
ArraySetAsSeries(midBuffer, true);
SetIndexBuffer(midBufferIndex, midBuffer, INDICATOR_DATA);
//
PlotIndexSetInteger(midBufferIndex, PLOT_SHOW_DATA, showMid);
PlotIndexSetInteger(midBufferIndex, PLOT_DRAW_TYPE, midDrawType);
//
PlotIndexSetDouble(midBufferIndex, PLOT_EMPTY_VALUE, 0);
//
// Lower ...
//
ENUM_DRAW_TYPE lowerDrawType = showLower ? DRAW_LINE : DRAW_NONE;
ArraySetAsSeries(lowerBuffer, true);
SetIndexBuffer(lowerBufferIndex, lowerBuffer, INDICATOR_DATA);
//
PlotIndexSetInteger(lowerBufferIndex, PLOT_SHOW_DATA, showLower);
PlotIndexSetInteger(lowerBufferIndex, PLOT_DRAW_TYPE, lowerDrawType);
//
PlotIndexSetDouble(lowerBufferIndex, PLOT_EMPTY_VALUE, 0);
}
/**
* Calculate Custom Buffers ...
*
* @param bar_index: 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 bar_index, // Selected Bar Index
const int prevCalculated,
const int ratesTotal,
const double &open[],
const double &high[],
const double &close[],
const double &low[],
const long &tickVolume[] //
)
{
//
int barsLimit = startCalculationForLastBars > 0
? startCalculationForLastBars
: 0;
//
// bool canCalculate = true;
bool canCalculate =
barsLimit == 0 ||
bar_index <= barsLimit;
if (canCalculate)
{
//
CalculateValues(
bar_index,
prevCalculated,
ratesTotal,
open,
high,
close,
low,
tickVolume //
);
}
else
{
FillBuffersZero(bar_index);
}
}
/**
* Fill All Bufers to Zero Vlue for Specified Bar Index ...
*
* @param barIndex: Integer ...
*/
void FillBuffersZero(int barIndex)
{
//
upperBuffer[barIndex] = 0;
midBuffer[barIndex] = 0;
lowerBuffer[barIndex] = 0;
}
/**
* Calculate Vales ...
*
* @param bar_index: 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 CalculateValues(
int bar_index, // Selected Bar Index
const int prevCalculated,
const int ratesTotal,
const double &open[],
const double &high[],
const double &close[],
const double &low[],
const long &tickVolume[] //
)
{
//
// Implement Colorify Mechanism ...
}
//