394 lines
9.0 KiB
Plaintext
394 lines
9.0 KiB
Plaintext
///////////////////////////////////////////////////////
|
|
// SaherElm IT Center MQL5 Indicator
|
|
// -------------------------------------------
|
|
// Name: X3MA
|
|
// Description: X3MA Trend Detector ...
|
|
//
|
|
// 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 X3MA Indicator"
|
|
#property strict
|
|
|
|
//
|
|
#define ShortName "X3MA"
|
|
|
|
//
|
|
// Includes Common Library ...
|
|
#include "../Libraries/x-saherelm.common.lib.mq5"
|
|
|
|
//
|
|
// Inputs ...
|
|
|
|
//
|
|
// Market ...
|
|
input group "Market";
|
|
input int fastLength = 50; // Fast Length
|
|
input int midLength = 100; // Mid Length
|
|
input int slowLength = 200; // Slow Length
|
|
input int shiftLength = 0; // Shift
|
|
input ENUM_MA_METHOD method = MODE_SMA; // Mode
|
|
input ENUM_APPLIED_PRICE appliedTo = PRICE_CLOSE; // Applied To
|
|
|
|
//
|
|
// Presentation ...
|
|
input group "Presentation";
|
|
|
|
//
|
|
// Parts ...
|
|
input bool showFast = true; // Show Fast
|
|
input bool showMid = true; // Show Mid
|
|
input bool showSlow = true; // Show Slow
|
|
|
|
//
|
|
// Buffers ...
|
|
|
|
//
|
|
#property indicator_chart_window
|
|
|
|
//
|
|
#property indicator_buffers 3
|
|
#property indicator_plots 3
|
|
|
|
//
|
|
// Current ...
|
|
|
|
//
|
|
// Fast ...
|
|
#define fastBufferIndex 0
|
|
double fastBuffer[];
|
|
|
|
//
|
|
#property indicator_label1 "X3MA F"
|
|
#property indicator_type1 DRAW_LINE
|
|
#property indicator_color1 clrAqua
|
|
#property indicator_style1 STYLE_DOT
|
|
#property indicator_width1 1
|
|
|
|
//
|
|
// Mid ...
|
|
#define midBufferIndex 1
|
|
double midBuffer[];
|
|
|
|
//
|
|
#property indicator_label2 "X3MA M"
|
|
#property indicator_type2 DRAW_LINE
|
|
#property indicator_color2 clrOrange
|
|
#property indicator_style2 STYLE_DOT
|
|
#property indicator_width2 1
|
|
|
|
//
|
|
// Slow ...
|
|
#define slowBufferIndex 2
|
|
double slowBuffer[];
|
|
|
|
//
|
|
#property indicator_label3 "X3MA S"
|
|
#property indicator_type3 DRAW_LINE
|
|
#property indicator_color3 clrMagenta
|
|
#property indicator_style3 STYLE_DOT
|
|
#property indicator_width3 1
|
|
|
|
//
|
|
// Variables ...
|
|
|
|
//
|
|
int maxLength;
|
|
|
|
//
|
|
// Handlers ...
|
|
int fastHandler = INVALID_HANDLE;
|
|
int midHandler = INVALID_HANDLE;
|
|
int slowHandler = INVALID_HANDLE;
|
|
|
|
//
|
|
// EVENT Handlers ...
|
|
|
|
//
|
|
// Initialization ...
|
|
int OnInit()
|
|
{
|
|
//
|
|
// Validate Inputs ...
|
|
if (!ValidateInputs())
|
|
{
|
|
return INIT_PARAMETERS_INCORRECT;
|
|
}
|
|
|
|
//
|
|
if (!InitHandlers())
|
|
{
|
|
return INIT_PARAMETERS_INCORRECT;
|
|
}
|
|
|
|
//
|
|
// 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();
|
|
|
|
//
|
|
// 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
|
|
|
|
//
|
|
// Release Handlers ...
|
|
IndicatorRelease(fastHandler);
|
|
IndicatorRelease(midHandler);
|
|
IndicatorRelease(slowHandler);
|
|
}
|
|
|
|
//
|
|
// Calculating what we want ...
|
|
int OnCalculate(
|
|
const int rates_total, // Total Bars on Chart ...
|
|
const int prev_calculated, // Total Calculated Bars on Charts ...
|
|
const datetime &time[], // History of Open Time ...
|
|
const double &open[], // History of Open Price ...
|
|
const double &high[], // History of High Price ...
|
|
const double &low[], // History of Low Price ...
|
|
const double &close[], // History of Close Price ...
|
|
const long &tick_volume[], // History of Tick Volumes on Bar ...
|
|
const long &volume[], // History of Trade Volumes ...
|
|
const int &spread[] // History of Spread Price ...
|
|
)
|
|
{
|
|
//
|
|
ArraySetAsSeries(low, true);
|
|
ArraySetAsSeries(open, true);
|
|
ArraySetAsSeries(high, true);
|
|
ArraySetAsSeries(close, true);
|
|
|
|
//
|
|
// this counts Available Bars ...
|
|
int limit;
|
|
|
|
//
|
|
// Validate Calculated Bars ...
|
|
bool isPassedRequiredCalculatedBars =
|
|
//
|
|
BarsCalculated(fastHandler) >= maxLength &&
|
|
BarsCalculated(midHandler) >= maxLength &&
|
|
BarsCalculated(slowHandler) >= maxLength
|
|
//
|
|
;
|
|
if (!isPassedRequiredCalculatedBars)
|
|
{
|
|
return prev_calculated;
|
|
}
|
|
|
|
//
|
|
limit =
|
|
(prev_calculated > rates_total || prev_calculated <= 0)
|
|
? rates_total
|
|
: (rates_total - prev_calculated) + 1;
|
|
|
|
//
|
|
// Buffers Copy ...
|
|
|
|
//
|
|
int copiedFasts = CopyBuffer(fastHandler, 0, 0, limit, fastBuffer);
|
|
int copiedMids = CopyBuffer(midHandler, 0, 0, limit, midBuffer);
|
|
int copiedSlows = CopyBuffer(slowHandler, 0, 0, limit, slowBuffer);
|
|
|
|
//
|
|
// Validate Copied Items ...
|
|
bool isPassedRequiredCopiedItems =
|
|
//
|
|
copiedFasts > 0 &&
|
|
copiedMids > 0 &&
|
|
copiedSlows > 0
|
|
//
|
|
;
|
|
if (!isPassedRequiredCopiedItems)
|
|
{
|
|
return prev_calculated;
|
|
}
|
|
|
|
//
|
|
// Main Loop ...
|
|
for (int i = limit - 1; i >= 0 && !IsStopped(); i--)
|
|
{
|
|
//
|
|
}
|
|
|
|
//
|
|
return rates_total;
|
|
}
|
|
|
|
//
|
|
// CUSTOM Functions ...
|
|
|
|
//
|
|
// Validate Input Args for Initialization ...
|
|
bool ValidateInputs()
|
|
{
|
|
//
|
|
bool result =
|
|
//
|
|
fastLength > 2 &&
|
|
midLength > fastLength &&
|
|
slowLength > midLength &&
|
|
//
|
|
shiftLength >= 0
|
|
//
|
|
;
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
//
|
|
// Retrieve all Exists Input Max Length ...
|
|
// use for Start Of Drawing ...
|
|
int ExtractMaxLengthOfInputs()
|
|
{
|
|
//
|
|
int result = 0;
|
|
|
|
//
|
|
// Current ...
|
|
result = MathMax(fastLength, midLength);
|
|
result = MathMax(result, slowLength);
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
//
|
|
// Set Indicator Short Name and also we can define Buffers Labels ...
|
|
void SetIndicatorName()
|
|
{
|
|
//
|
|
IndicatorSetInteger(INDICATOR_DIGITS, _Digits);
|
|
IndicatorSetString(INDICATOR_SHORTNAME, ShortName);
|
|
}
|
|
|
|
//
|
|
// Define Indexes and Styles ...
|
|
void DefineBuffers()
|
|
{
|
|
//
|
|
// Fast ...
|
|
bool canShowFast = showFast;
|
|
ENUM_DRAW_TYPE fastDrawType = canShowFast ? DRAW_LINE : DRAW_NONE;
|
|
ArraySetAsSeries(fastBuffer, true);
|
|
SetIndexBuffer(fastBufferIndex, fastBuffer, INDICATOR_DATA);
|
|
PlotIndexSetDouble(fastBufferIndex, PLOT_EMPTY_VALUE, 0);
|
|
PlotIndexSetInteger(fastBufferIndex, PLOT_DRAW_BEGIN, maxLength);
|
|
PlotIndexSetInteger(fastBufferIndex, PLOT_SHOW_DATA, canShowFast);
|
|
PlotIndexSetInteger(fastBufferIndex, PLOT_DRAW_TYPE, fastDrawType);
|
|
|
|
//
|
|
// Mid ...
|
|
bool canShowMid = showMid;
|
|
ENUM_DRAW_TYPE midDrawType = canShowMid ? DRAW_LINE : DRAW_NONE;
|
|
ArraySetAsSeries(midBuffer, true);
|
|
SetIndexBuffer(midBufferIndex, midBuffer, INDICATOR_DATA);
|
|
PlotIndexSetDouble(midBufferIndex, PLOT_EMPTY_VALUE, 0);
|
|
PlotIndexSetInteger(midBufferIndex, PLOT_DRAW_BEGIN, maxLength);
|
|
PlotIndexSetInteger(midBufferIndex, PLOT_SHOW_DATA, canShowMid);
|
|
PlotIndexSetInteger(midBufferIndex, PLOT_DRAW_TYPE, midDrawType);
|
|
|
|
//
|
|
// Slow ...
|
|
bool canShowSlow = showSlow;
|
|
ENUM_DRAW_TYPE slowDrawType = canShowSlow ? DRAW_LINE : DRAW_NONE;
|
|
ArraySetAsSeries(slowBuffer, true);
|
|
SetIndexBuffer(slowBufferIndex, slowBuffer, INDICATOR_DATA);
|
|
PlotIndexSetDouble(slowBufferIndex, PLOT_EMPTY_VALUE, 0);
|
|
PlotIndexSetInteger(slowBufferIndex, PLOT_DRAW_BEGIN, maxLength);
|
|
PlotIndexSetInteger(slowBufferIndex, PLOT_SHOW_DATA, canShowSlow);
|
|
PlotIndexSetInteger(slowBufferIndex, PLOT_DRAW_TYPE, slowDrawType);
|
|
}
|
|
|
|
//
|
|
// Initial Indicator Handlers ...
|
|
bool InitHandlers()
|
|
{
|
|
//
|
|
bool result = false;
|
|
|
|
//
|
|
// Initialize Handlers ...
|
|
|
|
//
|
|
// Fast ...
|
|
fastHandler = iMA(
|
|
_Symbol,
|
|
_Period,
|
|
fastLength,
|
|
shiftLength,
|
|
method,
|
|
appliedTo //
|
|
);
|
|
|
|
//
|
|
// Mid ...
|
|
midHandler = iMA(
|
|
_Symbol,
|
|
_Period,
|
|
midLength,
|
|
shiftLength,
|
|
method,
|
|
appliedTo //
|
|
);
|
|
|
|
//
|
|
// Slow ...
|
|
slowHandler = iMA(
|
|
_Symbol,
|
|
_Period,
|
|
slowLength,
|
|
shiftLength,
|
|
method,
|
|
appliedTo //
|
|
);
|
|
|
|
//
|
|
result =
|
|
//
|
|
fastHandler != INVALID_HANDLE &&
|
|
midHandler != INVALID_HANDLE &&
|
|
slowHandler != INVALID_HANDLE
|
|
//
|
|
;
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
// |