574 lines
12 KiB
Plaintext
574 lines
12 KiB
Plaintext
///////////////////////////////////////////////////////
|
|
//
|
|
// SaherElm IT Center MQL5 Indicator
|
|
// -------------------------------------------------
|
|
// Name: X121 XSWLH
|
|
// Description: XSWLH ...
|
|
//
|
|
//
|
|
// 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 XSWLH Indicator"
|
|
#property strict
|
|
|
|
//
|
|
// Definitions ...
|
|
|
|
//
|
|
#define ShortName "X121 XSWLH"
|
|
|
|
//
|
|
// Includes Common Library ...
|
|
#include "../Libraries/x-saherelm.common.lib.mq5"
|
|
|
|
//
|
|
// Inputs ...
|
|
|
|
//
|
|
input int swingLength; // Swing Length
|
|
|
|
//
|
|
input int startCalculationForLastBars = 1000; // Calculate Last n Bars
|
|
|
|
//
|
|
input bool showSwingLow; // Show Swing Low
|
|
input bool showSwingHigh; // Show Swing High
|
|
|
|
//
|
|
input int swingLowArrowCode = 225; // Swing Low Arrow Code
|
|
input int swingHighArrowCode = 226; // Swing High Arrow Code
|
|
|
|
//
|
|
#define emptyValue 0.0
|
|
|
|
//
|
|
// Buffers ...
|
|
|
|
//
|
|
#property indicator_chart_window
|
|
|
|
//
|
|
#property indicator_buffers 2
|
|
#property indicator_plots 2
|
|
|
|
//
|
|
// SWING LOW ...
|
|
|
|
//
|
|
#define swingLowBufferIndex 0
|
|
double swingLowBuffer[];
|
|
|
|
//
|
|
#property indicator_label1 "SWL"
|
|
#property indicator_type1 DRAW_ARROW
|
|
#property indicator_color1 clrGreen
|
|
#property indicator_width1 1
|
|
|
|
//
|
|
// SWING HIGH ...
|
|
|
|
//
|
|
#define swingHighBufferIndex 1
|
|
double swingHighBuffer[];
|
|
|
|
//
|
|
#property indicator_label2 "SWH"
|
|
#property indicator_type2 DRAW_ARROW
|
|
#property indicator_color2 clrRed
|
|
#property indicator_width2 1
|
|
|
|
//
|
|
// Data Buffers ...
|
|
|
|
//
|
|
#define lastBufferIndex 1
|
|
|
|
//
|
|
// Variables, Properties and etc ...
|
|
|
|
//
|
|
// this counts Available Bars ...
|
|
int limit;
|
|
|
|
//
|
|
int firstBarIndex;
|
|
|
|
//
|
|
int maxLength;
|
|
|
|
//
|
|
// Event Handlers ...
|
|
|
|
/**
|
|
* Initialize Indicator ...
|
|
*
|
|
* @return ( int )
|
|
*/
|
|
int OnInit()
|
|
{
|
|
//
|
|
// Validate Inputs ...
|
|
if (!ValidateInputs())
|
|
{
|
|
return INIT_PARAMETERS_INCORRECT;
|
|
}
|
|
|
|
//
|
|
// Initialize Indicator Handlers ...
|
|
|
|
//
|
|
// 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;
|
|
}
|
|
|
|
/**
|
|
* 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 =
|
|
//
|
|
swingLength > 0
|
|
//
|
|
;
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* Extract Max Length of Inputs ...
|
|
*
|
|
* @return ( int )
|
|
*/
|
|
int ExtractMaxLengthOfInputs()
|
|
{
|
|
//
|
|
int result = 0;
|
|
|
|
//
|
|
result = MathMax(result, swingLength);
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* Define Required Buffers ...
|
|
*/
|
|
void DefineBuffers()
|
|
{
|
|
//
|
|
// Plot Buffers ...
|
|
|
|
//
|
|
// SWING Low ...
|
|
|
|
//
|
|
ENUM_DRAW_TYPE swingLowDrawType = showSwingLow ? DRAW_ARROW : DRAW_NONE;
|
|
|
|
//
|
|
ArraySetAsSeries(swingLowBuffer, true);
|
|
SetIndexBuffer(swingLowBufferIndex, swingLowBuffer, INDICATOR_DATA);
|
|
|
|
//
|
|
PlotIndexSetInteger(swingLowBufferIndex, PLOT_SHOW_DATA, showSwingLow);
|
|
PlotIndexSetInteger(swingLowBufferIndex, PLOT_DRAW_TYPE, swingLowDrawType);
|
|
|
|
//
|
|
PlotIndexSetDouble(swingLowBufferIndex, PLOT_EMPTY_VALUE, emptyValue);
|
|
PlotIndexSetInteger(swingLowBufferIndex, PLOT_ARROW, swingLowArrowCode);
|
|
|
|
//
|
|
// SWING High ...
|
|
|
|
//
|
|
ENUM_DRAW_TYPE swingHighDrawType = showSwingHigh ? DRAW_ARROW : DRAW_NONE;
|
|
|
|
//
|
|
ArraySetAsSeries(swingHighBuffer, true);
|
|
SetIndexBuffer(swingHighBufferIndex, swingHighBuffer, INDICATOR_DATA);
|
|
|
|
//
|
|
PlotIndexSetInteger(swingHighBufferIndex, PLOT_SHOW_DATA, showSwingHigh);
|
|
PlotIndexSetInteger(swingHighBufferIndex, PLOT_DRAW_TYPE, swingHighDrawType);
|
|
|
|
//
|
|
PlotIndexSetDouble(swingHighBufferIndex, PLOT_EMPTY_VALUE, emptyValue);
|
|
PlotIndexSetInteger(swingHighBufferIndex, PLOT_ARROW, swingHighArrowCode);
|
|
}
|
|
|
|
/**
|
|
* Set Indicator Short Name and also we can define Buffers Labels ...
|
|
*/
|
|
void SetIndicatorName()
|
|
{
|
|
//
|
|
IndicatorSetInteger(INDICATOR_DIGITS, _Digits);
|
|
IndicatorSetString(INDICATOR_SHORTNAME, ShortName);
|
|
}
|
|
|
|
/**
|
|
* Fill All Bufers to Zero Vlue for Specified Bar Index ...
|
|
*
|
|
* @param barIndex: Integer ...
|
|
*/
|
|
void FillBuffersZero(int barIndex)
|
|
{
|
|
//
|
|
swingLowBuffer[barIndex] = emptyValue;
|
|
swingHighBuffer[barIndex] = emptyValue;
|
|
}
|
|
|
|
/**
|
|
* 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;
|
|
if (barsLimit == 0)
|
|
{
|
|
//
|
|
barsLimit = ratesTotal;
|
|
firstBarIndex = barsLimit - 1;
|
|
}
|
|
else
|
|
{
|
|
//
|
|
firstBarIndex = startCalculationForLastBars;
|
|
}
|
|
|
|
//
|
|
// bool canCalculate = true;
|
|
bool canCalculate =
|
|
bar_index <= barsLimit;
|
|
if (canCalculate)
|
|
{
|
|
//
|
|
// Calculate Support nad Resistance ...
|
|
CalculateValues(
|
|
bar_index,
|
|
prevCalculated,
|
|
ratesTotal,
|
|
open,
|
|
high,
|
|
close,
|
|
low,
|
|
tickVolume //
|
|
);
|
|
}
|
|
else
|
|
{
|
|
FillBuffersZero(bar_index);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Calculate Values ...
|
|
*
|
|
* @param bar_index: int, Specified Bar Index ...
|
|
* @param prevCalculated: int, Provides Previous Calculated Bars ...
|
|
* @param ratesTotal: int, Provides All Availabled Bars ...
|
|
* @param open: double Collection, Provides Open Prices Time Series ...
|
|
* @param high: double Collection, Provides High Prices Time Series ...
|
|
* @param close: double Collection, Provides Close Prices Time Series ...
|
|
* @param low: double Collection, Provides Low Prices Time Series ...
|
|
* @param tickVolume: long Collection, Provides Ticks Volume Prices Time Series ...
|
|
*/
|
|
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[] //
|
|
)
|
|
{
|
|
//
|
|
// Check Prev Bar ...
|
|
//
|
|
int lastBarIndex = bar_index + 1;
|
|
bool isFirstBar =
|
|
startCalculationForLastBars > 0
|
|
? bar_index == startCalculationForLastBars
|
|
: bar_index == firstBarIndex;
|
|
|
|
//
|
|
// Reading Last Values ...
|
|
|
|
//
|
|
double iSWLPrev =
|
|
isFirstBar
|
|
? emptyValue
|
|
: swingLowBuffer[lastBarIndex];
|
|
|
|
//
|
|
double iSWHPrev =
|
|
isFirstBar
|
|
? emptyValue
|
|
: swingHighBuffer[lastBarIndex];
|
|
|
|
//
|
|
// Checking Swings ...
|
|
bool isSwingLow = IsSwingLow(high, low, bar_index, swingLength);
|
|
bool isSwingHigh = IsSwingHigh(high, low, bar_index, swingLength);
|
|
bool hasSwing = isSwingLow ||
|
|
isSwingHigh;
|
|
if (!hasSwing)
|
|
{
|
|
//
|
|
// FillBuffersZero(bar_index);
|
|
|
|
//
|
|
swingLowBuffer[bar_index] = iSWLPrev;
|
|
swingHighBuffer[bar_index] = iSWHPrev;
|
|
return;
|
|
}
|
|
|
|
//
|
|
double iSWL = low[bar_index];
|
|
double iSWH = high[bar_index];
|
|
|
|
//
|
|
// Act Based on Swing High or Swing Low Detection ...
|
|
if (isSwingLow && !isSwingHigh)
|
|
{
|
|
//
|
|
// Detect Swing High based On swing Low ...
|
|
iSWH = iSWHPrev;
|
|
}
|
|
else if (isSwingHigh && !isSwingLow)
|
|
{
|
|
//
|
|
// Detect Swing Low Based On Swing High ...
|
|
iSWL = iSWLPrev;
|
|
}
|
|
|
|
//
|
|
if (iSWL > iSWLPrev && iSWH == iSWHPrev)
|
|
{
|
|
iSWL = iSWLPrev;
|
|
}
|
|
|
|
//
|
|
if (iSWH < iSWHPrev && iSWL == iSWLPrev)
|
|
{
|
|
iSWH = iSWHPrev;
|
|
}
|
|
|
|
//
|
|
swingLowBuffer[bar_index] = iSWL;
|
|
swingHighBuffer[bar_index] = iSWH;
|
|
}
|
|
|
|
//
|
|
// Helpers ...
|
|
|
|
//
|
|
bool IsSwingLow(
|
|
const double &high[],
|
|
const double &low[],
|
|
int index,
|
|
int length //
|
|
)
|
|
{
|
|
//
|
|
bool result = false;
|
|
|
|
//
|
|
result = index + 1 + length <= firstBarIndex;
|
|
if (!result)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
for (int i = index + 1; i <= index + length; i++)
|
|
{
|
|
//
|
|
result = low[index] < low[i];
|
|
if (!result)
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
//
|
|
bool IsSwingHigh(
|
|
const double &high[],
|
|
const double &low[],
|
|
int index,
|
|
int length //
|
|
)
|
|
{
|
|
//
|
|
bool result = false;
|
|
|
|
//
|
|
result = index + 1 + length <= firstBarIndex;
|
|
if (!result)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
for (int i = index + 1; i <= index + length; i++)
|
|
{
|
|
//
|
|
result = high[index] > high[i];
|
|
if (!result)
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
|
|
//
|
|
return result;
|
|
}
|