349 lines
7.1 KiB
Plaintext
349 lines
7.1 KiB
Plaintext
//////////////////////////////////////////////////////
|
|
//
|
|
// SaherElm IT Center MQL5 Indicator
|
|
// ---------------------------------------------
|
|
// Name: X121_XDEMA
|
|
// Description: Super Trend 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 X121_XAMA Indicator"
|
|
#property strict
|
|
|
|
//
|
|
// START Constants ...
|
|
//
|
|
|
|
#define ShortName "X121_XDEMA"
|
|
|
|
//
|
|
// END Constants ...
|
|
//
|
|
|
|
//
|
|
// START Inputs ...
|
|
//
|
|
|
|
//
|
|
// Market ...
|
|
input group "Market";
|
|
input int length = 200; // Length
|
|
input int shift = 0; // Shift
|
|
|
|
//
|
|
// Presentation ...
|
|
input group "Presentation";
|
|
input bool showDema = true; // Show Dema
|
|
input bool showEMA = false; // Show EMA
|
|
input bool showEMAOfEMA = false; // Show EMA of EMA
|
|
|
|
//
|
|
// END Inputs ...
|
|
//
|
|
|
|
//
|
|
// Includes Common Library ...
|
|
#include <MovingAverages.mqh>
|
|
#include "../Libraries/x-saherelm.common.lib.mq5"
|
|
|
|
//
|
|
// START Buffers ...
|
|
//
|
|
|
|
//
|
|
#property indicator_chart_window
|
|
|
|
//
|
|
#property indicator_buffers 6
|
|
#property indicator_plots 6
|
|
|
|
//
|
|
#define demaBufferIndex 0
|
|
double demaBuffer[];
|
|
|
|
//
|
|
#define demaColorBufferIndex 1
|
|
double demaColorBuffer[];
|
|
|
|
//
|
|
#property indicator_label1 "X121_XDEMA DEMA"
|
|
#property indicator_type1 DRAW_COLOR_LINE
|
|
#property indicator_color1 clrDodgerBlue, CLR_NONE
|
|
#property indicator_style1 STYLE_SOLID
|
|
#property indicator_width1 1
|
|
|
|
//
|
|
#define emaBufferIndex 2
|
|
double emaBuffer[];
|
|
|
|
//
|
|
#define emaColorBufferIndex 3
|
|
double emaColorBuffer[];
|
|
|
|
//
|
|
#property indicator_label2 "X121_XDEMA EMA"
|
|
#property indicator_type2 DRAW_COLOR_LINE
|
|
#property indicator_color2 clrBlue, CLR_NONE
|
|
#property indicator_style2 STYLE_DOT
|
|
#property indicator_width2 1
|
|
|
|
//
|
|
#define emaOfEMABufferIndex 4
|
|
double emaOfEMABuffer[];
|
|
|
|
//
|
|
#define emaOfEMAColorBufferIndex 5
|
|
double emaOfEMAColorBuffer[];
|
|
|
|
//
|
|
#property indicator_label3 "X121_XDEMA EMA of EMA"
|
|
#property indicator_type3 DRAW_COLOR_LINE
|
|
#property indicator_color3 clrOrange, CLR_NONE
|
|
#property indicator_style3 STYLE_DOT
|
|
#property indicator_width3 1
|
|
|
|
//
|
|
// END Buffers ...
|
|
//
|
|
|
|
//
|
|
// START Global Definitions: Variables, Properties and etc ...
|
|
//
|
|
|
|
//
|
|
int maxLength;
|
|
|
|
//
|
|
// END Global Definitions: Variables, Properties and etc ...
|
|
//
|
|
|
|
//
|
|
// START Event Handlers ...
|
|
//
|
|
|
|
//
|
|
// Initialization ...
|
|
int OnInit()
|
|
{
|
|
//
|
|
// Validate Inputs ...
|
|
if (!ValidateInputs())
|
|
{
|
|
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
|
|
}
|
|
|
|
//
|
|
// Do Calculations ...
|
|
int OnCalculate(const int rates_total,
|
|
const int prev_calculated,
|
|
const int begin,
|
|
const double &price[])
|
|
{
|
|
//
|
|
ArraySetAsSeries(price, true);
|
|
|
|
//
|
|
// check for data ...
|
|
if (rates_total < 2 * length - 2)
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
//
|
|
int limit;
|
|
|
|
//
|
|
limit = (prev_calculated > rates_total || prev_calculated <= 0) ? rates_total : (rates_total - prev_calculated) + 1;
|
|
|
|
//
|
|
// calculate EMA ...
|
|
ExponentialMAOnBuffer(rates_total, prev_calculated, 0, length, price, emaBuffer);
|
|
|
|
//
|
|
// calculate EMA on EMA array ...
|
|
ExponentialMAOnBuffer(rates_total, prev_calculated, length - 1, length, emaBuffer, emaOfEMABuffer);
|
|
|
|
//
|
|
// Main Loop ...
|
|
for (int i = limit - 1; i >= 0 && !IsStopped(); i--)
|
|
{
|
|
//
|
|
// calculate DEMA ...
|
|
demaBuffer[i] = 2 * emaBuffer[i] - emaOfEMABuffer[i];
|
|
|
|
//
|
|
double showColorIDX = 0;
|
|
double hideColorIDX = 1;
|
|
|
|
//
|
|
demaColorBuffer[i] = hideColorIDX;
|
|
emaColorBuffer[i] = hideColorIDX;
|
|
emaOfEMAColorBuffer[i] = hideColorIDX;
|
|
|
|
//
|
|
if (showDema)
|
|
{
|
|
demaColorBuffer[i] = showColorIDX;
|
|
}
|
|
|
|
//
|
|
if (showEMA)
|
|
{
|
|
emaColorBuffer[i] = showColorIDX;
|
|
}
|
|
|
|
//
|
|
if (showEMAOfEMA)
|
|
{
|
|
emaOfEMAColorBuffer[i] = showColorIDX;
|
|
}
|
|
}
|
|
|
|
//
|
|
return rates_total;
|
|
}
|
|
|
|
//
|
|
// END Event Handlers ...
|
|
//
|
|
|
|
//
|
|
// START Functions ...
|
|
//
|
|
|
|
//
|
|
// Validate Input Args for Initialization ...
|
|
bool ValidateInputs()
|
|
{
|
|
//
|
|
bool result =
|
|
//
|
|
(length > 2 &&
|
|
shift >= 0)
|
|
//
|
|
;
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
//
|
|
// Retrieve all Exists Input Max Length ...
|
|
// use for Start Of Drawing ...
|
|
int ExtractMaxLengthOfInputs()
|
|
{
|
|
//
|
|
int result = 0;
|
|
|
|
//
|
|
// Peaks and Vales ...
|
|
result = length;
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
//
|
|
// Set Indicator Short Name and also we can define Buffers Labels ...
|
|
void SetIndicatorName()
|
|
{
|
|
IndicatorSetString(INDICATOR_SHORTNAME, ShortName);
|
|
}
|
|
|
|
//
|
|
// Define Indexes and Styles ...
|
|
void DefineBuffers()
|
|
{
|
|
//
|
|
// DEMA ...
|
|
|
|
//
|
|
// Buffer ...
|
|
ArraySetAsSeries(demaBuffer, true);
|
|
SetIndexBuffer(demaBufferIndex, demaBuffer, INDICATOR_DATA);
|
|
PlotIndexSetInteger(demaBufferIndex, PLOT_SHOW_DATA, showDema);
|
|
|
|
//
|
|
// Color Buffer ...
|
|
ArraySetAsSeries(demaColorBuffer, true);
|
|
SetIndexBuffer(demaColorBufferIndex, demaColorBuffer, INDICATOR_COLOR_INDEX);
|
|
|
|
//
|
|
// EMA ...
|
|
|
|
//
|
|
// Buffer ...
|
|
ArraySetAsSeries(emaBuffer, true);
|
|
SetIndexBuffer(emaBufferIndex, emaBuffer, INDICATOR_DATA);
|
|
PlotIndexSetInteger(emaBufferIndex, PLOT_SHOW_DATA, showEMA);
|
|
|
|
//
|
|
// Color Buffer ...
|
|
ArraySetAsSeries(emaColorBuffer, true);
|
|
SetIndexBuffer(emaColorBufferIndex, emaColorBuffer, INDICATOR_COLOR_INDEX);
|
|
|
|
//
|
|
// EMA of EMA ...
|
|
|
|
//
|
|
// Buffer ...
|
|
ArraySetAsSeries(emaOfEMABuffer, true);
|
|
SetIndexBuffer(emaOfEMABufferIndex, emaOfEMABuffer, INDICATOR_DATA);
|
|
PlotIndexSetInteger(emaOfEMABufferIndex, PLOT_SHOW_DATA, showEMAOfEMA);
|
|
|
|
//
|
|
// Color Buffer ...
|
|
ArraySetAsSeries(emaOfEMAColorBuffer, true);
|
|
SetIndexBuffer(emaOfEMAColorBufferIndex, emaOfEMAColorBuffer, INDICATOR_COLOR_INDEX);
|
|
}
|
|
|
|
//
|
|
// END Functions ...
|
|
//
|