Files
2024-05-17 04:54:01 +03:30

470 lines
11 KiB
Plaintext

///////////////////////////////////////////////////////
//
// SaherElm IT Center MQL5 X121_XMC MA Cross Indicator
// ---------------------------------------------------
// Name: X121_XMC
// Description: Moving Average Crosses
// Market Analysor ...
//
//
// 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_XMC Indicator"
#property strict
//
// START Constants ...
//
//
// Indicator Short Name ...
#define ShortName "X121_XMC"
//
// END Constants ...
//
//
// START Inputs ...
//
//
input group "Fast";
input int fastMaLength = 9; // Length
input int fastMaShift = 0; // Shift
input ENUM_MA_METHOD fastMaMethod = MODE_EMA; // Method
input ENUM_APPLIED_PRICE fastMaAppliedTo = PRICE_CLOSE; // Applied To
//
input group "Slow";
input int slowMaLength = 18; // Length
input int slowMaShift = 0; // Shift
input ENUM_MA_METHOD slowMaMethod = MODE_EMA; // Method
input ENUM_APPLIED_PRICE slowMaAppliedTo = PRICE_CLOSE; // Applied To
//
input group "Verifier";
input int verifierMaLength = 50; // Length
input int verifierMaShift = 0; // Shift
input ENUM_MA_METHOD verifierMaMethod = MODE_EMA; // Method
input ENUM_APPLIED_PRICE verifierMaAppliedTo = PRICE_CLOSE; // Applied To
//
input group "Presentation";
input bool showFastMa = true; // Show Fast
input bool showSlowMa = true; // Show Slow
input bool showVerifierMa = true; // Show Verifier
//
// END Inputs ...
//
//
// START Includes and Imports ...
//
//
// Includes Logging Library ...
#include "../Libraries/x-saherelm.log.lib.mq5"
//
// Includes Common Library ...
#include "../Libraries/x-saherelm.common.lib.mq5"
//
// END Includes and Imports ...
//
//
// START Buffers ...
//
#property indicator_chart_window
//
#property indicator_buffers 3
#property indicator_plots 3
//
// PLOTTED Buffers ...
//
// MA ...
//
// FAST ...
#define fastMaBufferIndex 0
double fastMaBuffer[];
//
#property indicator_label1 "X121_XMC MA F"
#property indicator_type1 DRAW_LINE
#property indicator_color1 clrYellow
#property indicator_style1 STYLE_SOLID
#property indicator_width1 1
//
// SLOW ...
#define slowMaBufferIndex 1
double slowMaBuffer[];
//
#property indicator_label2 "X121_XMC MA S"
#property indicator_type2 DRAW_LINE
#property indicator_color2 clrDarkOrange
#property indicator_style2 STYLE_SOLID
#property indicator_width2 1
//
// VERIFIER ...
#define verifierMaBufferIndex 2
double verifierMaBuffer[];
//
#property indicator_label3 "X121_XMC MA V"
#property indicator_type3 DRAW_LINE
#property indicator_color3 clrDarkBlue
#property indicator_style3 STYLE_SOLID
#property indicator_width3 2
//
// END Buffers ...
//
//
// START Global Definitions: Variables, Properties and etc ...
//
//
int maxLength;
//
int fastMaHandler = INVALID_HANDLE;
int slowMaHandler = INVALID_HANDLE;
int verifierMaHandler = INVALID_HANDLE;
//
// END Global Definitions: Variables, Properties and etc ...
//
//
// START Event Handlers ...
//
//
// Initialization ...
int OnInit()
{
//
// Validate Inputs ...
if (!ValidateInputs())
{
return INIT_PARAMETERS_INCORRECT;
}
//
logTag = ShortName;
//
// 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();
//
// MA Initialization ...
//
// FAST ...
fastMaHandler = iMA(
_Symbol,
_Period,
fastMaLength,
fastMaShift,
fastMaMethod,
fastMaAppliedTo);
if (fastMaHandler == INVALID_HANDLE)
{
//
string msg = "Error in Initilizing Indicator ...";
LogMessage(msg);
//
return INIT_FAILED;
}
//
// SLOW ...
slowMaHandler = iMA(
_Symbol,
_Period,
slowMaLength,
slowMaShift,
slowMaMethod,
slowMaAppliedTo);
if (slowMaHandler == INVALID_HANDLE)
{
//
string msg = "Error in Initilizing Indicator ...";
LogMessage(msg);
//
return INIT_FAILED;
}
//
// VERIFIER ...
verifierMaHandler = iMA(
_Symbol,
_Period,
verifierMaLength,
verifierMaShift,
verifierMaMethod,
verifierMaAppliedTo);
if (verifierMaHandler == INVALID_HANDLE)
{
//
string msg = "Error in Initilizing Indicator ...";
LogMessage(msg);
//
return INIT_FAILED;
}
//
// 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 ...
IndicatorRelease(fastMaHandler);
IndicatorRelease(slowMaHandler);
IndicatorRelease(verifierMaHandler);
}
//
// 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;
//
// Check Calculated Bars ...
int fastMaCalculatedBars = BarsCalculated(fastMaHandler);
int slowMaCalculatedBars = BarsCalculated(slowMaHandler);
int verifierMaCalculatedBars = BarsCalculated(verifierMaHandler);
if (fastMaCalculatedBars < maxLength ||
slowMaCalculatedBars < maxLength ||
verifierMaCalculatedBars < maxLength)
{
return prev_calculated;
}
//
limit = (prev_calculated > rates_total || prev_calculated <= 0) ? rates_total : (rates_total - prev_calculated) + 1;
//
// Buffers Copy ...
int copiedFastMas = CopyBuffer(fastMaHandler, 0, 0, limit, fastMaBuffer);
int copiedSlowMas = CopyBuffer(slowMaHandler, 0, 0, limit, slowMaBuffer);
int copiedVerifierMas = CopyBuffer(verifierMaHandler, 0, 0, limit, verifierMaBuffer);
if (copiedFastMas <= 0 ||
copiedSlowMas <= 0 ||
copiedVerifierMas <= 0
)
{
return prev_calculated;
}
//
// Main Loop ...
for (int i = limit - 1; i >= 0 && !IsStopped(); i--)
{
CalculateBuffers(i);
}
//
return rates_total;
}
//
// END Event Handlers ...
//
//
// START Functions ...
//
//
// Validate Input Args for Initialization ...
bool ValidateInputs()
{
//
bool result =
//
// Validate Ma ...
(fastMaLength > 2 &&
slowMaLength > fastMaLength &&
verifierMaLength > slowMaLength)
//
;
//
return result;
}
//
// Retrieve all Exists Input Max Length ...
// use for Start Of Drawing ...
int ExtractMaxLengthOfInputs()
{
//
int result = 0;
//
// Ma ...
result = MathMax(result, fastMaLength);
result = MathMax(result, slowMaLength);
result = MathMax(result, verifierMaLength);
//
return result;
}
//
// Define Indexes and Styles ...
void DefineBuffers()
{
//
// MA ...
//
// FAST ...
ENUM_DRAW_TYPE fastMaDrawType = showFastMa ? DRAW_LINE : DRAW_NONE;
ArraySetAsSeries(fastMaBuffer, true);
SetIndexBuffer(fastMaBufferIndex, fastMaBuffer, INDICATOR_DATA);
PlotIndexSetInteger(fastMaBufferIndex, PLOT_DRAW_BEGIN, maxLength);
PlotIndexSetInteger(fastMaBufferIndex, PLOT_SHOW_DATA, false);
PlotIndexSetInteger(fastMaBufferIndex, PLOT_DRAW_TYPE, fastMaDrawType);
//
// SLOW ...
ENUM_DRAW_TYPE slowMaDrawType = showSlowMa ? DRAW_LINE : DRAW_NONE;
ArraySetAsSeries(slowMaBuffer, true);
SetIndexBuffer(slowMaBufferIndex, slowMaBuffer, INDICATOR_DATA);
PlotIndexSetInteger(slowMaBufferIndex, PLOT_DRAW_BEGIN, maxLength);
PlotIndexSetInteger(slowMaBufferIndex, PLOT_SHOW_DATA, false);
PlotIndexSetInteger(slowMaBufferIndex, PLOT_DRAW_TYPE, slowMaDrawType);
//
// VERIFIER ...
ENUM_DRAW_TYPE verifierMaDrawType = showVerifierMa ? DRAW_LINE : DRAW_NONE;
ArraySetAsSeries(verifierMaBuffer, true);
SetIndexBuffer(verifierMaBufferIndex, verifierMaBuffer, INDICATOR_DATA);
PlotIndexSetInteger(verifierMaBufferIndex, PLOT_DRAW_BEGIN, maxLength);
PlotIndexSetInteger(verifierMaBufferIndex, PLOT_SHOW_DATA, false);
PlotIndexSetInteger(verifierMaBufferIndex, PLOT_DRAW_TYPE, verifierMaDrawType);
}
//
// Set Indicator Short Name and also we can define Buffers Labels ...
void SetIndicatorName()
{
//
string indicatorShortName = "";
StringConcatenate(indicatorShortName, ShortName, "");
//
IndicatorSetString(INDICATOR_SHORTNAME, indicatorShortName);
}
//
// Calculate Buffers ...
void CalculateBuffers(
int bar_index // Selected Bar Index
)
{
}
//
// END Functions ...
//