Files
MQL5Data/Indicators/x-saherelm.xatsl.mq5
T
2024-06-22 15:42:08 +03:30

333 lines
7.7 KiB
Plaintext

///////////////////////////////////////////////////////
// SaherElm IT Center MQL5 Indicator
// -------------------------------------------
// Name: XATSL
// Description: ATR SL ...
//
// 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 XATSL Indicator"
#property strict
//
#define ShortName "XATSL"
//
// Imports ...
#include "../Libraries/x-saherelm.common.lib.mq5"
//
// Inputs ...
//
input group "Market";
input int length = 14; // Length
input double coeff = 2.0; // Coefficient
//
input group "Presentation";
input bool show = true; // Show
//
// Buffers ...
//
#property indicator_chart_window
//
#property indicator_buffers 7
#property indicator_plots 2
//
#define mainBufferIndex 0
double mainBuffer[];
//
#define mainBufferColorIndex 1
double mainBufferColor[];
//
#define hideColorIDX 0
#define bullColorIDX 1
#define bearColorIDX 2
//
#property indicator_label1 "XATSL"
#property indicator_type1 DRAW_COLOR_LINE
#property indicator_color1 CLR_NONE, clrGreen, clrDarkRed
#property indicator_style1 STYLE_DOT
#property indicator_width1 1
//
#define hlBufferIndex 2
double hlBuffer[];
//
#define wmaBufferIndex 3
double wmaBuffer[];
//
#define tmpBufferIndex 4
double tmpBuffer[];
//
#define diffBufferIndex 5
double diffBuffer[];
//
// Variables ...
int maPeriod;
double k;
//
// Event Handlers ...
//
// Initialization ...
int OnInit()
{
//
// Validate Inputs ...
if (!ValidateInputs())
{
return INIT_PARAMETERS_INCORRECT;
}
//
maPeriod = int(length < 1 ? 1 : length);
double n1 = 2.0 * double(maPeriod - 1);
k = 2.0 / (n1 + 1);
//
// 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
}
//
// 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(high, true);
ArraySetAsSeries(low, true);
ArraySetAsSeries(close, true);
//
if (rates_total < maPeriod)
{
return 0;
}
//
int limit = rates_total - prev_calculated;
if (limit > 1)
{
//
limit = rates_total - 2;
//
ArrayInitialize(mainBuffer, EMPTY_VALUE);
ArrayInitialize(hlBuffer, 0);
ArrayInitialize(diffBuffer, 0);
ArrayInitialize(wmaBuffer, 0);
ArrayInitialize(tmpBuffer, 0);
}
//
for (int i = limit; i >= 0 && !IsStopped(); i--)
{
//
hlBuffer[i] = high[i] - low[i];
//
double href = 0;
double lref = 0;
//
double sma = MAOnArray(hlBuffer, 0, maPeriod, 0, MODE_SMA, i);
double hiLo = fmin(hlBuffer[i], sma);
//
href = (low[i] <= high[i + 1] ? high[i] - close[i + 1] : (hlBuffer[i] - close[i + 1] + high[i + 1]) / 2);
lref = (high[i] >= low[i + 1] ? close[i + 1] - low[i] : (close[i + 1] - low[i + 1] + hlBuffer[i]) / 2);
//
diffBuffer[i] = fmax(hiLo, fmax(href, lref));
}
//
for (int i = limit; i >= 0 && !IsStopped(); i--)
{
//
if (i == rates_total - 2)
{
wmaBuffer[i] = MAOnArray(diffBuffer, 0, maPeriod, 0, MODE_EMA, i);
}
else
{
wmaBuffer[i] = (diffBuffer[i] - wmaBuffer[i + 1]) * k + wmaBuffer[i + 1];
}
}
//
for (int i = limit; i >= 0 && !IsStopped(); i--)
{
//
double loss = wmaBuffer[i] * coeff;
if (close[i] > tmpBuffer[i + 1] && close[i + 1] > tmpBuffer[i + 1])
{
//
tmpBuffer[i] = fmax(tmpBuffer[i + 1], close[i] - loss);
//
mainBuffer[i] = tmpBuffer[i];
}
else
{
//
if (close[i] < tmpBuffer[i + 1] && close[i + 1] < tmpBuffer[i + 1])
{
//
tmpBuffer[i] = fmin(tmpBuffer[i + 1], close[i] + loss);
//
mainBuffer[i] = tmpBuffer[i];
}
else
{
//
if (close[i] > tmpBuffer[i + 1])
{
//
tmpBuffer[i] = close[i] - loss;
//
mainBuffer[i] = tmpBuffer[i];
}
else
{
//
tmpBuffer[i] = close[i] + loss;
//
mainBuffer[i] = tmpBuffer[i];
}
}
}
//
// Define Color Buffer ...
double clrValue = !show
? hideColorIDX
: mainBuffer[i] > close[i]
? bearColorIDX
: bullColorIDX;
mainBufferColor[i] = clrValue;
}
//
return rates_total;
}
//
// Custom Functions ...
//
// Validate Input Args for Initialization ...
bool ValidateInputs()
{
//
bool result = false;
//
result = length >= 2 &&
coeff > 0;
//
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()
{
//
// Main ...
ENUM_DRAW_TYPE mainDrawType = show ? DRAW_COLOR_LINE : DRAW_NONE;
ArraySetAsSeries(mainBuffer, true);
ArraySetAsSeries(mainBufferColor, true);
PlotIndexSetInteger(mainBufferIndex, PLOT_SHOW_DATA, true);
SetIndexBuffer(mainBufferIndex, mainBuffer, INDICATOR_DATA);
PlotIndexSetInteger(mainBufferIndex, PLOT_DRAW_TYPE, mainDrawType);
SetIndexBuffer(mainBufferColorIndex, mainBufferColor, INDICATOR_COLOR_INDEX);
//
ArraySetAsSeries(hlBuffer, true);
SetIndexBuffer(hlBufferIndex, hlBuffer, INDICATOR_CALCULATIONS);
//
ArraySetAsSeries(wmaBuffer, true);
SetIndexBuffer(wmaBufferIndex, wmaBuffer, INDICATOR_CALCULATIONS);
//
ArraySetAsSeries(tmpBuffer, true);
SetIndexBuffer(tmpBufferIndex, tmpBuffer, INDICATOR_CALCULATIONS);
//
ArraySetAsSeries(diffBuffer, true);
SetIndexBuffer(diffBufferIndex, diffBuffer, INDICATOR_CALCULATIONS);
}
//