Files
MQL5Data/Indicators/x-saherelm.x121.xich.mq5
T
2025-03-26 05:25:00 +03:30

626 lines
16 KiB
Plaintext

///////////////////////////////////////////////////////
//
// SaherElm IT Center MQL5 Indicator
// -------------------------------------------------
// Name: X121 XWZ
// Description: XWZ ...
//
//
// 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 XWZ Indicator"
#property strict
//
// Definitions ...
//
#define ShortName "X121_XICH"
//
// Imports ...
#include "../Libraries/x-saherelm.common.lib.mq5"
//
// Inputs ...
//
// Market ...
input group "Market";
input ENUM_X_PRICE chikouSpanMode = X_PRICE_CLOSE; // ChikouSpan Mode
input int tenkanSenLength = 9; // TenkanSen Length
input ENUM_X_BOUNDARY_PRICE tenkanSenMode = X_BOUNDARY_PRICE_HIGH_LOW; // TencanSen Mode
input int kijunSenLength = 26; // KijunSen Length
input ENUM_X_BOUNDARY_PRICE kijunSenMode = X_BOUNDARY_PRICE_HIGH_LOW; // KijunSen Mode
input int senkouSpanBLength = 52; // SenkouSpan B Length
input ENUM_X_BOUNDARY_PRICE senkouSpanBMode = X_BOUNDARY_PRICE_HIGH_LOW; // SenkouSpan B Mode
//
// Presentation ...
input group "Presentation";
//
input int startCalculationForLastBars = 1000; // Calculate Last n Bars
input bool shiftKumo = true; // Shift Kumo
input bool showTenkanSen = true; // Show TenkanSen
input bool showKijunSen = true; // Show KijunSen
input bool showChikouSpan = true; // Show ChikouSpan
input bool showSenkouSpanA = true; // Show Senkou Span A
input bool showSenkouSpanB = true; // Show Senkou Span B
input bool showKumo = true; // Show Kumo
//
// Buffers ...
//
#define hideColorIDX 0
#define bullishColorIDX 1
#define bearishColorIDX 2
#define neuturalColorIDX 3
//
#property indicator_chart_window
//
#property indicator_buffers 8
#property indicator_plots 6
//
// Plot Buffers ...
//
#define tenkanSenBufferIndex 0
double tenkanSenBuffer[];
//
#property indicator_label1 "XICH TK"
#property indicator_type1 DRAW_LINE
#property indicator_color1 clrBrown
#property indicator_style1 STYLE_SOLID
#property indicator_width1 1
//
#define kijunSenBufferIndex 1
double kijunSenBuffer[];
//
#property indicator_label2 "XICH KJ"
#property indicator_type2 DRAW_LINE
#property indicator_color2 clrDodgerBlue
#property indicator_style2 STYLE_SOLID
#property indicator_width2 1
//
#define chikouSpanBufferIndex 2
double chikouSpanBuffer[];
//
#property indicator_label3 "XICH CS"
#property indicator_type3 DRAW_LINE
#property indicator_color3 clrDarkGreen
#property indicator_style3 STYLE_SOLID
#property indicator_width3 1
//
#define senkouSpanABufferIndex 3
double senkouSpanABuffer[];
//
#property indicator_label4 "XICH SSA"
#property indicator_type4 DRAW_LINE
#property indicator_color4 clrLightGray
#property indicator_style4 STYLE_DASHDOTDOT
#property indicator_width4 1
//
#define senkouSpanBBufferIndex 4
double senkouSpanBBuffer[];
//
#property indicator_label5 "XICH SSB"
#property indicator_type5 DRAW_LINE
#property indicator_color5 clrLightGray
#property indicator_style5 STYLE_DASHDOTDOT
#property indicator_width5 1
//
#define senkouABufferIndex 5
double senkouABuffer[];
//
#define senkouBBufferIndex 6
double senkouBBuffer[];
//
#define kumoBufferIndex 5
//
#property indicator_label6 "XICH Kumo"
#property indicator_type6 DRAW_FILLING
#property indicator_color6 clrAqua, clrMagenta
#property indicator_style6 STYLE_SOLID
#property indicator_width6 1
//
// Data Buffers ...
//
int mLastBufferIndex = 6;
//
// Variables, Properties and etc ...
//
// this counts Available Bars ...
int limit;
//
int maxLength;
//
// Event Handlers ...
/**
* Initialize Indicator ...
*
* @return ( int )
*/
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();
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
}
/**
* 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);
//
limit = (prev_calculated > rates_total || prev_calculated <= 0)
? rates_total
: (rates_total - prev_calculated) + 1;
//
// 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 =
//
kijunSenLength > 0 &&
tenkanSenLength > 0 &&
senkouSpanBLength > 0 &&
IsValid(kijunSenMode) &&
IsValid(tenkanSenMode) &&
IsValid(chikouSpanMode) &&
IsValid(senkouSpanBMode)
//
;
//
return result;
}
/**
* Extract Max Length of Inputs ...
*
* @return ( int )
*/
int ExtractMaxLengthOfInputs()
{
//
int result = 0;
//
result = MathMax(kijunSenLength, tenkanSenLength);
result = MathMax(result, senkouSpanBLength);
//
return result;
}
/**
* Set Indicator Short Name and also we can define Buffers Labels ...
*/
void SetIndicatorName()
{
IndicatorSetString(INDICATOR_SHORTNAME, ShortName);
}
/**
* Define Required Buffers ...
*/
void DefineBuffers()
{
//
ENUM_DRAW_TYPE tenkanSenDrawType = showTenkanSen ? DRAW_LINE : DRAW_NONE;
//
ArraySetAsSeries(tenkanSenBuffer, true);
SetIndexBuffer(tenkanSenBufferIndex, tenkanSenBuffer, INDICATOR_DATA);
PlotIndexSetInteger(tenkanSenBufferIndex, PLOT_DRAW_BEGIN, maxLength);
PlotIndexSetInteger(tenkanSenBufferIndex, PLOT_SHOW_DATA, showTenkanSen);
PlotIndexSetInteger(tenkanSenBufferIndex, PLOT_DRAW_TYPE, tenkanSenDrawType);
//
ENUM_DRAW_TYPE kijunSenDrawType = showKijunSen ? DRAW_LINE : DRAW_NONE;
//
ArraySetAsSeries(kijunSenBuffer, true);
SetIndexBuffer(kijunSenBufferIndex, kijunSenBuffer, INDICATOR_DATA);
PlotIndexSetInteger(kijunSenBufferIndex, PLOT_DRAW_BEGIN, maxLength);
PlotIndexSetInteger(kijunSenBufferIndex, PLOT_SHOW_DATA, showKijunSen);
PlotIndexSetInteger(kijunSenBufferIndex, PLOT_DRAW_TYPE, kijunSenDrawType);
//
ENUM_DRAW_TYPE chikouSpanDrawType = showChikouSpan ? DRAW_LINE : DRAW_NONE;
//
ArraySetAsSeries(chikouSpanBuffer, true);
SetIndexBuffer(chikouSpanBufferIndex, chikouSpanBuffer, INDICATOR_DATA);
PlotIndexSetInteger(chikouSpanBufferIndex, PLOT_DRAW_BEGIN, maxLength);
PlotIndexSetInteger(chikouSpanBufferIndex, PLOT_SHIFT, -kijunSenLength);
PlotIndexSetInteger(chikouSpanBufferIndex, PLOT_SHOW_DATA, showChikouSpan);
PlotIndexSetInteger(chikouSpanBufferIndex, PLOT_DRAW_TYPE, chikouSpanDrawType);
//
int shiftSize = shiftKumo ? kijunSenLength : 0;
//
ENUM_DRAW_TYPE senkouSpanADrawType = showSenkouSpanA ? DRAW_LINE : DRAW_NONE;
//
ArraySetAsSeries(senkouSpanABuffer, true);
SetIndexBuffer(senkouSpanABufferIndex, senkouSpanABuffer, INDICATOR_DATA);
PlotIndexSetInteger(senkouSpanABufferIndex, PLOT_SHIFT, shiftSize);
PlotIndexSetInteger(senkouSpanABufferIndex, PLOT_SHOW_DATA, showSenkouSpanA);
PlotIndexSetInteger(senkouSpanABufferIndex, PLOT_DRAW_TYPE, senkouSpanADrawType);
//
ENUM_DRAW_TYPE senkouSpanBDrawType = showSenkouSpanB ? DRAW_LINE : DRAW_NONE;
//
ArraySetAsSeries(senkouSpanBBuffer, true);
SetIndexBuffer(senkouSpanBBufferIndex, senkouSpanBBuffer, INDICATOR_DATA);
PlotIndexSetInteger(senkouSpanBBufferIndex, PLOT_SHIFT, shiftSize);
PlotIndexSetInteger(senkouSpanBBufferIndex, PLOT_SHOW_DATA, showSenkouSpanB);
PlotIndexSetInteger(senkouSpanBBufferIndex, PLOT_DRAW_TYPE, senkouSpanBDrawType);
//
ArraySetAsSeries(senkouABuffer, true);
SetIndexBuffer(senkouABufferIndex, senkouABuffer, INDICATOR_DATA);
PlotIndexSetInteger(senkouABufferIndex, PLOT_DRAW_BEGIN, maxLength);
//
ArraySetAsSeries(senkouBBuffer, true);
SetIndexBuffer(senkouBBufferIndex, senkouBBuffer, INDICATOR_DATA);
PlotIndexSetInteger(senkouBBufferIndex, PLOT_DRAW_BEGIN, maxLength);
//
ENUM_DRAW_TYPE kumoDrawType = showKumo ? DRAW_FILLING : DRAW_NONE;
//
PlotIndexSetInteger(kumoBufferIndex, PLOT_SHIFT, shiftSize);
PlotIndexSetInteger(kumoBufferIndex, PLOT_SHOW_DATA, false);
PlotIndexSetInteger(kumoBufferIndex, PLOT_DRAW_TYPE, kumoDrawType);
}
/**
* 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)
{
//
senkouABuffer[barIndex] = 0;
senkouBBuffer[barIndex] = 0;
kijunSenBuffer[barIndex] = 0;
tenkanSenBuffer[barIndex] = 0;
chikouSpanBuffer[barIndex] = 0;
senkouSpanABuffer[barIndex] = 0;
senkouSpanBBuffer[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[] //
)
{
//
double uppers[];
double lowers[];
//
double uppersMax = 0;
double uppersMin = 0;
//
double lowersMax = 0;
double lowersMin = 0;
//
// Calculate ChikouSpan ...
double iChikouSpan = GetAppliedPrice(
_Symbol,
_Period,
bar_index,
chikouSpanMode //
);
//
// Calculate TenkanSen ...
GetPriceBoundary(
uppers,
lowers,
_Symbol,
_Period,
tenkanSenMode,
tenkanSenLength,
bar_index //
);
//
uppersMax = uppers[ArrayMaximum(uppers)];
uppersMin = uppers[ArrayMinimum(uppers)];
//
lowersMax = lowers[ArrayMaximum(lowers)];
lowersMin = lowers[ArrayMinimum(lowers)];
//
double iTenkanSen = (uppersMax + lowersMin) / 2;
//
// Calculate KijunSen ...
GetPriceBoundary(
uppers,
lowers,
_Symbol,
_Period,
kijunSenMode,
kijunSenLength,
bar_index //
);
//
uppersMax = uppers[ArrayMaximum(uppers)];
uppersMin = uppers[ArrayMinimum(uppers)];
//
lowersMax = lowers[ArrayMaximum(lowers)];
lowersMin = lowers[ArrayMinimum(lowers)];
//
double iKijunSen = (uppersMax + lowersMin) / 2;
//
// Calculate SenkouSpanB ...
GetPriceBoundary(
uppers,
lowers,
_Symbol,
_Period,
senkouSpanBMode,
senkouSpanBLength,
bar_index //
);
//
uppersMax = uppers[ArrayMaximum(uppers)];
uppersMin = uppers[ArrayMinimum(uppers)];
//
lowersMax = lowers[ArrayMaximum(lowers)];
lowersMin = lowers[ArrayMinimum(lowers)];
//
double iSenkouSpanB = (uppersMax + lowersMin) / 2;
//
// Calculate SenkouSpanA ...
double iSenkouSpanA = (iTenkanSen + iKijunSen) / 2;
//
// Setting Buffers ...
kijunSenBuffer[bar_index] = iKijunSen;
senkouABuffer[bar_index] = iSenkouSpanA;
senkouBBuffer[bar_index] = iSenkouSpanB;
tenkanSenBuffer[bar_index] = iTenkanSen;
chikouSpanBuffer[bar_index] = iChikouSpan;
senkouSpanABuffer[bar_index] = iSenkouSpanA;
senkouSpanBBuffer[bar_index] = iSenkouSpanB;
}
//