Initial Commit ...
This commit is contained in:
Binary file not shown.
@@ -0,0 +1,178 @@
|
||||
//+------------------------------------------------------------------+
|
||||
//| Learn 3.mq5 |
|
||||
//| Copyright 2016, Hudson Analytics |
|
||||
//| NurudeenAmedu |
|
||||
//+------------------------------------------------------------------+
|
||||
#property copyright "Copyright 2016, Hudson Analytics"
|
||||
#property link "NurudeenAmedu"
|
||||
#property version "1.00"
|
||||
#property indicator_separate_window
|
||||
#property indicator_buffers 3
|
||||
#property indicator_plots 2
|
||||
#property indicator_type1 DRAW_LINE
|
||||
#property indicator_color1 clrBlue
|
||||
#property indicator_type2 DRAW_LINE
|
||||
#property indicator_color2 clrRed
|
||||
|
||||
double ExtBuffer1[];
|
||||
double ExtBuffer2[];
|
||||
//+------------------------------------------------------------------+
|
||||
//| Custom indicator initialization function |
|
||||
//+------------------------------------------------------------------+
|
||||
int OnInit()
|
||||
{
|
||||
//--- indicator buffers mapping
|
||||
SetIndexBuffer(0, ExtBuffer1);
|
||||
SetIndexBuffer(1, ExtBuffer2);
|
||||
//---
|
||||
return (INIT_SUCCEEDED);
|
||||
}
|
||||
//+------------------------------------------------------------------+
|
||||
//| Custom indicator iteration function |
|
||||
//+------------------------------------------------------------------+
|
||||
int OnCalculate(const int rates_total, // 1001
|
||||
const int prev_calculated, // 1000
|
||||
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[])
|
||||
{
|
||||
//---
|
||||
int x, y, limit;
|
||||
int min_rates_total = 100;
|
||||
|
||||
ArraySetAsSeries(close, true);
|
||||
ArraySetAsSeries(ExtBuffer1, true);
|
||||
ArraySetAsSeries(ExtBuffer2, true);
|
||||
|
||||
//---- calculations of the necessary amount of data to be copied
|
||||
//---- and the 'limit' starting index for the bars recalculation loop
|
||||
if (prev_calculated > rates_total || prev_calculated <= 0) // checking for the first start of the indicator calculation
|
||||
{
|
||||
limit = rates_total - min_rates_total - 1; // calculated number of all bars
|
||||
}
|
||||
else
|
||||
{
|
||||
limit = rates_total - prev_calculated; // starting index for calculation of new bars
|
||||
}
|
||||
|
||||
for (int i = limit; i >= 0; i--)
|
||||
{
|
||||
x = 0;
|
||||
y = 0;
|
||||
|
||||
if (close[i] > close[i + 1])
|
||||
x++;
|
||||
if (close[i] < close[i + 1])
|
||||
y++;
|
||||
|
||||
if (close[i] > close[i + 2])
|
||||
x++;
|
||||
if (close[i] < close[i + 2])
|
||||
y++;
|
||||
|
||||
if (close[i] > close[i + 3])
|
||||
x++;
|
||||
if (close[i] < close[i + 3])
|
||||
y++;
|
||||
|
||||
if (close[i] > close[i + 4])
|
||||
x++;
|
||||
if (close[i] < close[i + 4])
|
||||
y++;
|
||||
|
||||
if (close[i] > close[i + 5])
|
||||
x++;
|
||||
if (close[i] < close[i + 5])
|
||||
y++;
|
||||
|
||||
if (close[i] > close[i + 6])
|
||||
x++;
|
||||
if (close[i] < close[i + 6])
|
||||
y++;
|
||||
|
||||
//////////////////////////
|
||||
if (close[i + 1] > close[i + 2])
|
||||
x++;
|
||||
if (close[i + 1] < close[i + 2])
|
||||
y++;
|
||||
|
||||
if (close[i + 1] > close[i + 3])
|
||||
x++;
|
||||
if (close[i + 1] < close[i + 3])
|
||||
y++;
|
||||
|
||||
if (close[i + 1] > close[i + 4])
|
||||
x++;
|
||||
if (close[i + 1] < close[i + 4])
|
||||
y++;
|
||||
|
||||
if (close[i + 1] > close[i + 5])
|
||||
x++;
|
||||
if (close[i + 1] < close[i + 5])
|
||||
y++;
|
||||
|
||||
if (close[i + 1] > close[i + 6])
|
||||
x++;
|
||||
if (close[i + 1] < close[i + 6])
|
||||
y++;
|
||||
|
||||
//////////////////////////////
|
||||
if (close[i + 2] > close[i + 3])
|
||||
x++;
|
||||
if (close[i + 2] < close[i + 3])
|
||||
y++;
|
||||
|
||||
if (close[i + 2] > close[i + 4])
|
||||
x++;
|
||||
if (close[i + 2] < close[i + 4])
|
||||
y++;
|
||||
|
||||
if (close[i + 2] > close[i + 5])
|
||||
x++;
|
||||
if (close[i + 2] < close[i + 5])
|
||||
y++;
|
||||
|
||||
if (close[i + 2] > close[i + 6])
|
||||
x++;
|
||||
if (close[i + 2] < close[i + 6])
|
||||
y++;
|
||||
|
||||
////////////////////////////
|
||||
if (close[i + 3] > close[i + 4])
|
||||
x++;
|
||||
if (close[i + 3] < close[i + 4])
|
||||
y++;
|
||||
|
||||
if (close[i + 3] > close[i + 5])
|
||||
x++;
|
||||
if (close[i + 3] < close[i + 5])
|
||||
y++;
|
||||
|
||||
if (close[i + 3] > close[i + 6])
|
||||
x++;
|
||||
if (close[i + 3] < close[i + 6])
|
||||
y++;
|
||||
|
||||
////////////////////////////
|
||||
if (close[i + 4] > close[i + 5])
|
||||
x++;
|
||||
if (close[i + 4] < close[i + 5])
|
||||
y++;
|
||||
|
||||
if (close[i + 4] > close[i + 6])
|
||||
x++;
|
||||
if (close[i + 4] < close[i + 6])
|
||||
y++;
|
||||
|
||||
ExtBuffer1[i] = MathAbs(x);
|
||||
ExtBuffer2[i] = MathAbs(y);
|
||||
}
|
||||
//--- return value of prev_calculated for next call
|
||||
return (rates_total);
|
||||
}
|
||||
//+------------------------------------------------------------------+
|
||||
@@ -0,0 +1,367 @@
|
||||
///////////////////////////////////////////////////////
|
||||
//
|
||||
// SaherElm IT Center MQL5 MA Candle Indicator
|
||||
// ---------------------------------------------
|
||||
// Name: XMACCandle
|
||||
// Description: Moving Average Indicator which
|
||||
// colorified Candle based on price state ...
|
||||
//
|
||||
//
|
||||
// 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 XMAC Indicator"
|
||||
#property strict
|
||||
|
||||
//
|
||||
// START Constants ...
|
||||
//
|
||||
#define ShortName "XMACC"
|
||||
//
|
||||
// END Constants ...
|
||||
//
|
||||
|
||||
//
|
||||
// START Inputs ...
|
||||
//
|
||||
input int maPeriod = 14; // Period
|
||||
input int maShift = 0; // Shift
|
||||
input ENUM_MA_METHOD maMethod = MODE_SMA; // Method
|
||||
input ENUM_APPLIED_PRICE maAppliedTo = PRICE_CLOSE; // Applied To
|
||||
//
|
||||
// END Inputs ...
|
||||
//
|
||||
|
||||
//
|
||||
// Includes Logging Library ...
|
||||
#include "../Libraries/x-saherelm.log.lib.mq5";
|
||||
|
||||
//
|
||||
// START Buffers ...
|
||||
//
|
||||
#property indicator_chart_window
|
||||
|
||||
//
|
||||
#property indicator_buffers 6
|
||||
#property indicator_plots 1
|
||||
|
||||
//
|
||||
// maBuffer ...
|
||||
#define openBufferIndex 0
|
||||
#define highBufferIndex 1
|
||||
#define lowBufferIndex 2
|
||||
#define closeBufferIndex 3
|
||||
#define colorsIndex 4
|
||||
#define maBufferIndex 5
|
||||
|
||||
double openBuffer[];
|
||||
double highBuffer[];
|
||||
double lowBuffer[];
|
||||
double closeBuffer[];
|
||||
double colors[];
|
||||
|
||||
#property indicator_label1 "Open; High; Low; Close;"
|
||||
#property indicator_type1 DRAW_COLOR_CANDLES
|
||||
#property indicator_color1 clrLime, clrGray, clrRed
|
||||
#property indicator_style1 STYLE_SOLID
|
||||
#property indicator_width1 1
|
||||
//
|
||||
// END Buffers ...
|
||||
//
|
||||
|
||||
//
|
||||
// START Global Definitions: Variables, Properties and etc ...
|
||||
//
|
||||
double maBuffer[];
|
||||
int maHandler = INVALID_HANDLE;
|
||||
//
|
||||
// END Global Definitions: Variables, Properties and etc ...
|
||||
//
|
||||
|
||||
//
|
||||
// START Event Handlers ...
|
||||
//
|
||||
//
|
||||
// Initialization ...
|
||||
int OnInit()
|
||||
{
|
||||
//
|
||||
// Validate Inputs ...
|
||||
if (!ValidateInputs())
|
||||
{
|
||||
return INIT_PARAMETERS_INCORRECT;
|
||||
}
|
||||
|
||||
//
|
||||
logTag = ShortName;
|
||||
|
||||
//
|
||||
// Initializing MA Handler ...
|
||||
maHandler = iMA(
|
||||
_Symbol,
|
||||
_Period,
|
||||
maPeriod,
|
||||
maShift,
|
||||
maMethod,
|
||||
maAppliedTo);
|
||||
if (maHandler == INVALID_HANDLE)
|
||||
{
|
||||
//
|
||||
string message = "Error Initializing Ma Handler ...";
|
||||
LogMessage(message);
|
||||
|
||||
//
|
||||
return INIT_FAILED;
|
||||
}
|
||||
|
||||
//
|
||||
// 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
|
||||
|
||||
//
|
||||
// Here we can handle De Initialization Reasons ...
|
||||
}
|
||||
|
||||
//
|
||||
// Calculations ...
|
||||
//
|
||||
// Calculating what we want ...
|
||||
int OnCalculate(
|
||||
//
|
||||
// total Candles on chart ...
|
||||
const int rates_total,
|
||||
//
|
||||
// total calculated Candles on charts ...
|
||||
const int prev_calculated,
|
||||
//
|
||||
// history of Candles Open Time ...
|
||||
const datetime &time[],
|
||||
//
|
||||
// history of Candles Open Price ...
|
||||
const double &open[],
|
||||
//
|
||||
// history of Candles High Price ...
|
||||
const double &high[],
|
||||
//
|
||||
// history of Candles Low Price ...
|
||||
const double &low[],
|
||||
//
|
||||
// history of Candles Close Price ...
|
||||
const double &close[],
|
||||
//
|
||||
// history of Tick Volumes on Candle ...
|
||||
const long &tick_volume[],
|
||||
//
|
||||
// history of Trade Volumes ...
|
||||
const long &volume[],
|
||||
//
|
||||
// history of Candles Spread Price ...
|
||||
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);
|
||||
|
||||
//
|
||||
// this counts Available Bars ...
|
||||
int limit;
|
||||
|
||||
//
|
||||
// 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 ...
|
||||
int maxLength = MathMax(0, maPeriod);
|
||||
|
||||
//
|
||||
// Check Calculated Bars ...
|
||||
int maCalculatedBars = BarsCalculated(maHandler);
|
||||
if (maCalculatedBars < maxLength)
|
||||
{
|
||||
return prev_calculated;
|
||||
}
|
||||
|
||||
//
|
||||
// Calculate Limit ...
|
||||
limit = prev_calculated < maxLength ? rates_total - (maxLength + 1) : rates_total - prev_calculated;
|
||||
|
||||
//
|
||||
// Prepare Copy Buffers ...
|
||||
bool isBuffersPrepared = PrepareBuffers(limit);
|
||||
if (!isBuffersPrepared) {
|
||||
return prev_calculated;
|
||||
}
|
||||
|
||||
//
|
||||
// Main Loop ...
|
||||
for (int i = limit; i >= 0 && !IsStopped(); i--)
|
||||
{
|
||||
//
|
||||
CalculateColorBuffer(i);
|
||||
}
|
||||
|
||||
//
|
||||
return rates_total;
|
||||
}
|
||||
//
|
||||
// END Event Handlers ...
|
||||
//
|
||||
|
||||
//
|
||||
// START Functions ...
|
||||
//
|
||||
//
|
||||
// Validate Input Args for Initialization ...
|
||||
bool ValidateInputs()
|
||||
{
|
||||
//
|
||||
bool result = false;
|
||||
|
||||
//
|
||||
// Validate Args ...
|
||||
if (maPeriod >= 2)
|
||||
{
|
||||
result = true;
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
// Define Indexes and Styles ...
|
||||
void DefineBuffers()
|
||||
{
|
||||
//
|
||||
// Open Buffer ...
|
||||
ArraySetAsSeries(openBuffer, true);
|
||||
SetIndexBuffer(openBufferIndex, openBuffer, INDICATOR_DATA);
|
||||
PlotIndexSetInteger(openBufferIndex, PLOT_DRAW_BEGIN, maPeriod);
|
||||
|
||||
//
|
||||
// High Buffer ...
|
||||
ArraySetAsSeries(highBuffer, true);
|
||||
SetIndexBuffer(highBufferIndex, highBuffer, INDICATOR_DATA);
|
||||
PlotIndexSetInteger(highBufferIndex, PLOT_DRAW_BEGIN, maPeriod);
|
||||
|
||||
//
|
||||
// Low Buffer ...
|
||||
ArraySetAsSeries(lowBuffer, true);
|
||||
SetIndexBuffer(lowBufferIndex, lowBuffer, INDICATOR_DATA);
|
||||
PlotIndexSetInteger(lowBufferIndex, PLOT_DRAW_BEGIN, maPeriod);
|
||||
|
||||
//
|
||||
// Close Buffer ...
|
||||
ArraySetAsSeries(closeBuffer, true);
|
||||
SetIndexBuffer(closeBufferIndex, closeBuffer, INDICATOR_DATA);
|
||||
PlotIndexSetInteger(closeBufferIndex, PLOT_DRAW_BEGIN, maPeriod);
|
||||
|
||||
//
|
||||
// Colors ...
|
||||
ArraySetAsSeries(colors, true);
|
||||
SetIndexBuffer(colorsIndex, colors, INDICATOR_COLOR_INDEX);
|
||||
|
||||
//
|
||||
// Ma Buffer ...
|
||||
ArraySetAsSeries(maBuffer, true);
|
||||
SetIndexBuffer(maBufferIndex, maBuffer, INDICATOR_CALCULATIONS);
|
||||
}
|
||||
|
||||
//
|
||||
// Set Indicator Short Name and also we can define Buffers Labels ...
|
||||
void SetIndicatorName()
|
||||
{
|
||||
//
|
||||
string indicatorShortName = "";
|
||||
StringConcatenate(indicatorShortName, ShortName, " ", "( ", maPeriod, " )");
|
||||
|
||||
//
|
||||
IndicatorSetString(INDICATOR_SHORTNAME, indicatorShortName);
|
||||
}
|
||||
|
||||
//
|
||||
// Prepare Buffers ...
|
||||
bool PrepareBuffers(int count) {
|
||||
//
|
||||
// Copy MaBuffer ...
|
||||
int maCopied = CopyBuffer(maHandler, 0, 0, count, maBuffer);
|
||||
if (maCopied <= maPeriod) {
|
||||
return false;
|
||||
}
|
||||
|
||||
//
|
||||
// Copy OHLC Buffers ...
|
||||
CopyOpen(_Symbol, _Period, 0, count, openBuffer);
|
||||
CopyHigh(_Symbol, _Period, 0, count, highBuffer);
|
||||
CopyLow(_Symbol, _Period, 0, count, lowBuffer);
|
||||
CopyClose(_Symbol, _Period, 0, count, closeBuffer);
|
||||
|
||||
//
|
||||
return true;
|
||||
}
|
||||
|
||||
//
|
||||
// Setting Ma Color ...
|
||||
void CalculateColorBuffer(int bar_index)
|
||||
{
|
||||
//
|
||||
double low = iLow(
|
||||
_Symbol,
|
||||
_Period,
|
||||
bar_index);
|
||||
|
||||
//
|
||||
double high = iHigh(
|
||||
_Symbol,
|
||||
_Period,
|
||||
bar_index);
|
||||
|
||||
//
|
||||
double clrValue =
|
||||
maBuffer[bar_index] < low ? 0 : maBuffer[bar_index] > high ? 2
|
||||
: 1;
|
||||
//
|
||||
colors[bar_index] = clrValue;
|
||||
}
|
||||
//
|
||||
// END Functions ...
|
||||
//
|
||||
@@ -0,0 +1,319 @@
|
||||
///////////////////////////////////////////////////////
|
||||
//
|
||||
// SaherElm IT Center MQL5 MA Colorify Indicator
|
||||
// ---------------------------------------------
|
||||
// Name: XMAC
|
||||
// Description: Moving Average Indicator which
|
||||
// colorified based on price state ...
|
||||
//
|
||||
//
|
||||
// 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 XMAC Indicator"
|
||||
#property strict
|
||||
|
||||
//
|
||||
// START Constants ...
|
||||
//
|
||||
#define ShortName "XMAC"
|
||||
//
|
||||
// END Constants ...
|
||||
//
|
||||
|
||||
//
|
||||
// START Inputs ...
|
||||
//
|
||||
input int maPeriod = 14; // Period
|
||||
input int maShift = 0; // Shift
|
||||
input ENUM_MA_METHOD maMethod = MODE_SMA; // Method
|
||||
input ENUM_APPLIED_PRICE maAppliedTo = PRICE_CLOSE; // Applied To
|
||||
//
|
||||
// END Inputs ...
|
||||
//
|
||||
|
||||
//
|
||||
// Includes Logging Library ...
|
||||
#include "../Libraries/x-saherelm.log.lib.mq5";
|
||||
|
||||
//
|
||||
// START Buffers ...
|
||||
//
|
||||
#property indicator_chart_window
|
||||
// #property indicator_separate_window
|
||||
|
||||
//
|
||||
#property indicator_buffers 2
|
||||
#property indicator_plots 1
|
||||
|
||||
//
|
||||
// maBuffer ...
|
||||
#define maBufferIndex 0
|
||||
#define maColorIndex 1
|
||||
|
||||
double maBuffer[];
|
||||
double maColor[];
|
||||
|
||||
#property indicator_label1 "XMAC"
|
||||
#property indicator_type1 DRAW_COLOR_LINE
|
||||
#property indicator_color1 clrLime, clrGray, clrRed
|
||||
#property indicator_style1 STYLE_SOLID
|
||||
#property indicator_width1 1
|
||||
//
|
||||
// END Buffers ...
|
||||
//
|
||||
|
||||
//
|
||||
// START Global Definitions: Variables, Properties and etc ...
|
||||
//
|
||||
int maHandler = INVALID_HANDLE;
|
||||
//
|
||||
// END Global Definitions: Variables, Properties and etc ...
|
||||
//
|
||||
|
||||
//
|
||||
// START Event Handlers ...
|
||||
//
|
||||
//
|
||||
// Initialization ...
|
||||
int OnInit()
|
||||
{
|
||||
//
|
||||
// Validate Inputs ...
|
||||
if (!ValidateInputs())
|
||||
{
|
||||
return INIT_PARAMETERS_INCORRECT;
|
||||
}
|
||||
|
||||
//
|
||||
logTag = ShortName;
|
||||
|
||||
//
|
||||
// Initializing MA Handler ...
|
||||
maHandler = iMA(
|
||||
_Symbol,
|
||||
_Period,
|
||||
maPeriod,
|
||||
maShift,
|
||||
maMethod,
|
||||
maAppliedTo);
|
||||
if (maHandler == INVALID_HANDLE)
|
||||
{
|
||||
//
|
||||
string message = "Error Initializing Ma Handler ...";
|
||||
LogMessage(message);
|
||||
|
||||
//
|
||||
return INIT_FAILED;
|
||||
}
|
||||
|
||||
//
|
||||
// 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
|
||||
|
||||
//
|
||||
// Here we can handle De Initialization Reasons ...
|
||||
}
|
||||
|
||||
//
|
||||
// Calculations ...
|
||||
//
|
||||
// Calculating what we want ...
|
||||
int OnCalculate(
|
||||
//
|
||||
// total Candles on chart ...
|
||||
const int rates_total,
|
||||
//
|
||||
// total calculated Candles on charts ...
|
||||
const int prev_calculated,
|
||||
//
|
||||
// history of Candles Open Time ...
|
||||
const datetime &time[],
|
||||
//
|
||||
// history of Candles Open Price ...
|
||||
const double &open[],
|
||||
//
|
||||
// history of Candles High Price ...
|
||||
const double &high[],
|
||||
//
|
||||
// history of Candles Low Price ...
|
||||
const double &low[],
|
||||
//
|
||||
// history of Candles Close Price ...
|
||||
const double &close[],
|
||||
//
|
||||
// history of Tick Volumes on Candle ...
|
||||
const long &tick_volume[],
|
||||
//
|
||||
// history of Trade Volumes ...
|
||||
const long &volume[],
|
||||
//
|
||||
// history of Candles Spread Price ...
|
||||
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);
|
||||
|
||||
//
|
||||
// this counts Available Bars ...
|
||||
int limit;
|
||||
|
||||
//
|
||||
// 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 ...
|
||||
int maxLength = MathMax(0, maPeriod);
|
||||
|
||||
//
|
||||
// Check Calculated Bars ...
|
||||
int maCalculatedBars = BarsCalculated(maHandler);
|
||||
if (maCalculatedBars < maxLength)
|
||||
{
|
||||
return prev_calculated;
|
||||
}
|
||||
|
||||
//
|
||||
limit = prev_calculated < maxLength ? rates_total - (maxLength + 1) : rates_total - prev_calculated;
|
||||
|
||||
//
|
||||
// Buffers Copy ...
|
||||
int copiedMas = CopyBuffer(maHandler, 0, 0, limit + 1, maBuffer);
|
||||
if (copiedMas <= 0)
|
||||
{
|
||||
return prev_calculated;
|
||||
}
|
||||
|
||||
//
|
||||
// Main Loop ...
|
||||
for (int i = limit; i >= 0 && !IsStopped(); i--)
|
||||
{
|
||||
//
|
||||
SetMaColor(i);
|
||||
}
|
||||
|
||||
//
|
||||
return rates_total;
|
||||
}
|
||||
//
|
||||
// END Event Handlers ...
|
||||
//
|
||||
|
||||
//
|
||||
// START Functions ...
|
||||
//
|
||||
//
|
||||
// Validate Input Args for Initialization ...
|
||||
bool ValidateInputs()
|
||||
{
|
||||
//
|
||||
bool result = false;
|
||||
|
||||
//
|
||||
// Validate Args ...
|
||||
if (maPeriod >= 2)
|
||||
{
|
||||
result = true;
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
// Define Indexes and Styles ...
|
||||
void DefineBuffers()
|
||||
{
|
||||
//
|
||||
string maBufferLabel = "MA " + "(" + (string)maPeriod + ")";
|
||||
|
||||
//
|
||||
// Ma Buffer ...
|
||||
ArraySetAsSeries(maBuffer, true);
|
||||
SetIndexBuffer(maBufferIndex, maBuffer, INDICATOR_DATA);
|
||||
PlotIndexSetString(maBufferIndex, PLOT_LABEL, maBufferLabel);
|
||||
|
||||
//
|
||||
// Ma Color ...
|
||||
ArraySetAsSeries(maColor, true);
|
||||
SetIndexBuffer(maColorIndex, maColor, INDICATOR_COLOR_INDEX);
|
||||
}
|
||||
|
||||
//
|
||||
// Set Indicator Short Name and also we can define Buffers Labels ...
|
||||
void SetIndicatorName()
|
||||
{
|
||||
//
|
||||
string indicatorShortName = "";
|
||||
StringConcatenate(indicatorShortName, ShortName, " ", "( ", maPeriod, " )");
|
||||
|
||||
//
|
||||
IndicatorSetString(INDICATOR_SHORTNAME, indicatorShortName);
|
||||
}
|
||||
|
||||
//
|
||||
// Setting Ma Color ...
|
||||
void SetMaColor(int bar_index)
|
||||
{
|
||||
//
|
||||
double low = iLow(
|
||||
_Symbol,
|
||||
_Period,
|
||||
bar_index);
|
||||
|
||||
//
|
||||
double high = iHigh(
|
||||
_Symbol,
|
||||
_Period,
|
||||
bar_index);
|
||||
|
||||
//
|
||||
double clrValue =
|
||||
maBuffer[bar_index] < low ? 0 : maBuffer[bar_index] > high ? 2
|
||||
: 1;
|
||||
//
|
||||
maColor[bar_index] = clrValue;
|
||||
}
|
||||
//
|
||||
// END Functions ...
|
||||
//
|
||||
@@ -0,0 +1,276 @@
|
||||
///////////////////////////////////////////////////////
|
||||
//
|
||||
// SaherElm IT Center MQL5 MA Candle Indicator
|
||||
// ---------------------------------------------
|
||||
// Name: XMA
|
||||
// Description: Moving Average 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 XMA Indicator"
|
||||
#property strict
|
||||
|
||||
//
|
||||
// START Constants ...
|
||||
//
|
||||
#define ShortName "XMA"
|
||||
//
|
||||
// END Constants ...
|
||||
//
|
||||
|
||||
//
|
||||
// START Inputs ...
|
||||
//
|
||||
input int maPeriod = 14; // Period
|
||||
input int maShift = 0; // Shift
|
||||
input ENUM_MA_METHOD maMethod = MODE_SMA; // Method
|
||||
input ENUM_APPLIED_PRICE maAppliedTo = PRICE_CLOSE; // Applied To
|
||||
//
|
||||
// END Inputs ...
|
||||
//
|
||||
|
||||
//
|
||||
// Includes Logging Library ...
|
||||
#include "../Libraries/x-saherelm.log.lib.mq5";
|
||||
|
||||
//
|
||||
// START Buffers ...
|
||||
//
|
||||
#property indicator_chart_window
|
||||
// #property indicator_separate_window
|
||||
|
||||
//
|
||||
#property indicator_buffers 1
|
||||
#property indicator_plots 1
|
||||
|
||||
//
|
||||
// maBuffer ...
|
||||
#define maBufferIndex 0
|
||||
|
||||
double maBuffer[];
|
||||
|
||||
#property indicator_label1 "XMA"
|
||||
#property indicator_type1 DRAW_LINE
|
||||
#property indicator_color1 clrBlue
|
||||
#property indicator_style1 STYLE_SOLID
|
||||
#property indicator_width1 1
|
||||
//
|
||||
// END Buffers ...
|
||||
//
|
||||
|
||||
//
|
||||
// START Global Definitions: Variables, Properties and etc ...
|
||||
//
|
||||
int maHandler = INVALID_HANDLE;
|
||||
//
|
||||
// END Global Definitions: Variables, Properties and etc ...
|
||||
//
|
||||
|
||||
//
|
||||
// START Event Handlers ...
|
||||
//
|
||||
//
|
||||
// Initialization ...
|
||||
int OnInit()
|
||||
{
|
||||
//
|
||||
// Validate Inputs ...
|
||||
if (!ValidateInputs())
|
||||
{
|
||||
return INIT_PARAMETERS_INCORRECT;
|
||||
}
|
||||
|
||||
//
|
||||
logTag = ShortName;
|
||||
|
||||
//
|
||||
// Initializing MA Handler ...
|
||||
maHandler = iMA(
|
||||
_Symbol,
|
||||
_Period,
|
||||
maPeriod,
|
||||
maShift,
|
||||
maMethod,
|
||||
maAppliedTo);
|
||||
if (maHandler == INVALID_HANDLE)
|
||||
{
|
||||
//
|
||||
string message = "Error Initializing Ma Handler ...";
|
||||
LogMessage(message);
|
||||
|
||||
//
|
||||
return INIT_FAILED;
|
||||
}
|
||||
|
||||
//
|
||||
// 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
|
||||
|
||||
//
|
||||
// Here we can handle De Initialization Reasons ...
|
||||
}
|
||||
|
||||
//
|
||||
// Calculations ...
|
||||
//
|
||||
// Calculating what we want ...
|
||||
int OnCalculate(
|
||||
//
|
||||
// total Candles on chart ...
|
||||
const int rates_total,
|
||||
//
|
||||
// total calculated Candles on charts ...
|
||||
const int prev_calculated,
|
||||
//
|
||||
// history of Candles Open Time ...
|
||||
const datetime &time[],
|
||||
//
|
||||
// history of Candles Open Price ...
|
||||
const double &open[],
|
||||
//
|
||||
// history of Candles High Price ...
|
||||
const double &high[],
|
||||
//
|
||||
// history of Candles Low Price ...
|
||||
const double &low[],
|
||||
//
|
||||
// history of Candles Close Price ...
|
||||
const double &close[],
|
||||
//
|
||||
// history of Tick Volumes on Candle ...
|
||||
const long &tick_volume[],
|
||||
//
|
||||
// history of Trade Volumes ...
|
||||
const long &volume[],
|
||||
//
|
||||
// history of Candles Spread Price ...
|
||||
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);
|
||||
|
||||
//
|
||||
// this counts Available Bars ...
|
||||
int limit;
|
||||
|
||||
//
|
||||
// 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 ...
|
||||
int maxLength = MathMax(0, maPeriod);
|
||||
|
||||
//
|
||||
// Check Calculated Bars ...
|
||||
if (BarsCalculated(maHandler) < rates_total)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
//
|
||||
limit = (prev_calculated > rates_total || prev_calculated <= 0) ? rates_total : (rates_total - prev_calculated) + 1;
|
||||
|
||||
//
|
||||
// Buffers Copy ...
|
||||
if (CopyBuffer(maHandler, 0, 0, limit, maBuffer) <= 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
//
|
||||
return rates_total;
|
||||
}
|
||||
//
|
||||
// END Event Handlers ...
|
||||
//
|
||||
|
||||
//
|
||||
// START Functions ...
|
||||
//
|
||||
//
|
||||
// Validate Input Args for Initialization ...
|
||||
bool ValidateInputs()
|
||||
{
|
||||
//
|
||||
bool result = false;
|
||||
|
||||
//
|
||||
// Validate Args ...
|
||||
if (maPeriod >= 2)
|
||||
{
|
||||
result = true;
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
// Define Indexes and Styles ...
|
||||
void DefineBuffers()
|
||||
{
|
||||
//
|
||||
string maBufferLabel = "MA " + "(" + (string) maPeriod + ")";
|
||||
|
||||
//
|
||||
// Ma Buffer ...
|
||||
ArraySetAsSeries(maBuffer, true);
|
||||
SetIndexBuffer(maBufferIndex, maBuffer, INDICATOR_DATA);
|
||||
PlotIndexSetString(maBufferIndex, PLOT_LABEL, maBufferLabel);
|
||||
}
|
||||
|
||||
//
|
||||
// Set Indicator Short Name and also we can define Buffers Labels ...
|
||||
void SetIndicatorName()
|
||||
{
|
||||
//
|
||||
string indicatorShortName = "";
|
||||
StringConcatenate(indicatorShortName, ShortName, " ", "(", maPeriod, ")");
|
||||
|
||||
//
|
||||
IndicatorSetString(INDICATOR_SHORTNAME, indicatorShortName);
|
||||
}
|
||||
//
|
||||
// END Functions ...
|
||||
//
|
||||
@@ -0,0 +1,260 @@
|
||||
//////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// SaherElm IT Center MQL5 Strong Body Candel Detector Indicator
|
||||
// -------------------------------------------------------------
|
||||
// Name: XSBCD
|
||||
// Description: detect Strong Body Candles ...
|
||||
//
|
||||
//
|
||||
// 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 XSBCD Indicator"
|
||||
#property strict
|
||||
|
||||
//
|
||||
// START Constants ...
|
||||
//
|
||||
#define ShortName "XSBCD"
|
||||
//
|
||||
// END Constants ...
|
||||
//
|
||||
|
||||
//
|
||||
// START Inputs ...
|
||||
//
|
||||
//
|
||||
// END Inputs ...
|
||||
//
|
||||
|
||||
//
|
||||
// Includes Logging Library ...
|
||||
#include "../Libraries/x-saherelm.log.lib.mq5";
|
||||
|
||||
//
|
||||
// Include Common and Models Library ...
|
||||
#include "../Libraries/x-saherelm.common.lib.mq5";
|
||||
|
||||
//
|
||||
// Include Draw Library ...
|
||||
#include "../Libraries/x-saherelm.draw.lib.mq5";
|
||||
|
||||
//
|
||||
// START Buffers ...
|
||||
//
|
||||
#property indicator_chart_window
|
||||
|
||||
//
|
||||
#property indicator_buffers 0
|
||||
#property indicator_plots 0
|
||||
//
|
||||
// END Buffers ...
|
||||
//
|
||||
|
||||
//
|
||||
// START Global Definitions: Variables, Properties and etc ...
|
||||
//
|
||||
//
|
||||
// END Global Definitions: Variables, Properties and etc ...
|
||||
//
|
||||
|
||||
//
|
||||
// START Event Handlers ...
|
||||
//
|
||||
//
|
||||
// Initialization ...
|
||||
int OnInit()
|
||||
{
|
||||
//
|
||||
// Validate Inputs ...
|
||||
if (!ValidateInputs())
|
||||
{
|
||||
return INIT_PARAMETERS_INCORRECT;
|
||||
}
|
||||
|
||||
//
|
||||
logTag = ShortName;
|
||||
drawPrefix = ShortName;
|
||||
|
||||
//
|
||||
// Define Index Buffers ...
|
||||
DefineBuffers();
|
||||
|
||||
//
|
||||
// Set Indicator ShortName ...
|
||||
SetIndicatorName();
|
||||
|
||||
//
|
||||
LogMessage("Initialized ...");
|
||||
|
||||
//
|
||||
// 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
|
||||
|
||||
//
|
||||
//
|
||||
LogMessage("De Init Indicator");
|
||||
|
||||
//
|
||||
// Here we can handle De Initialization Reasons ...
|
||||
OnDeinitDrawLibrary();
|
||||
}
|
||||
|
||||
//
|
||||
// Calculations ...
|
||||
//
|
||||
// Calculating what we want ...
|
||||
int OnCalculate(
|
||||
//
|
||||
// total Candles on chart ...
|
||||
const int rates_total,
|
||||
//
|
||||
// total calculated Candles on charts ...
|
||||
const int prev_calculated,
|
||||
//
|
||||
// history of Candles Open Time ...
|
||||
const datetime &time[],
|
||||
//
|
||||
// history of Candles Open Price ...
|
||||
const double &open[],
|
||||
//
|
||||
// history of Candles High Price ...
|
||||
const double &high[],
|
||||
//
|
||||
// history of Candles Low Price ...
|
||||
const double &low[],
|
||||
//
|
||||
// history of Candles Close Price ...
|
||||
const double &close[],
|
||||
//
|
||||
// history of Tick Volumes on Candle ...
|
||||
const long &tick_volume[],
|
||||
//
|
||||
// history of Trade Volumes ...
|
||||
const long &volume[],
|
||||
//
|
||||
// history of Candles Spread Price ...
|
||||
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);
|
||||
|
||||
//
|
||||
// this counts Available Bars ...
|
||||
int limit;
|
||||
|
||||
//
|
||||
// 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 ...
|
||||
int maxLength = MathMax(0, 1);
|
||||
|
||||
//
|
||||
limit = prev_calculated < maxLength ? rates_total - (maxLength + 1) : rates_total - prev_calculated;
|
||||
|
||||
//
|
||||
// Main Loop ...
|
||||
for (int i = limit; i >= 0 && !IsStopped(); i--)
|
||||
{
|
||||
//
|
||||
CalculateBuffers(i);
|
||||
}
|
||||
|
||||
//
|
||||
return rates_total;
|
||||
}
|
||||
//
|
||||
// END Event Handlers ...
|
||||
//
|
||||
|
||||
//
|
||||
// START Functions ...
|
||||
//
|
||||
//
|
||||
// Validate Input Args for Initialization ...
|
||||
bool ValidateInputs()
|
||||
{
|
||||
//
|
||||
bool result = true;
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
// Define Indexes and Styles ...
|
||||
void DefineBuffers()
|
||||
{
|
||||
}
|
||||
|
||||
//
|
||||
// Set Indicator Short Name and also we can define Buffers Labels ...
|
||||
void SetIndicatorName()
|
||||
{
|
||||
IndicatorSetString(INDICATOR_SHORTNAME, ShortName);
|
||||
}
|
||||
|
||||
//
|
||||
// Calculate Buffers ...
|
||||
void CalculateBuffers(int bar_index)
|
||||
{
|
||||
//
|
||||
XOHCL c = GetCandle(bar_index + 1);
|
||||
bool isStrong = HasStrongBody(c);
|
||||
|
||||
//
|
||||
if (!isStrong)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
//
|
||||
// Draw a Vertical Line on it ...
|
||||
datetime time = iTime(_Symbol, _Period, bar_index + 1);
|
||||
string name = "StrongBodyCandle" + (string)time;
|
||||
color clr = clrYellow;
|
||||
|
||||
//
|
||||
DrawVerticalLine(
|
||||
0,
|
||||
name,
|
||||
0,
|
||||
time,
|
||||
clr);
|
||||
}
|
||||
//
|
||||
// END Functions ...
|
||||
//
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,327 @@
|
||||
///////////////////////////////////////////////////////
|
||||
//
|
||||
// SaherElm IT Center MQL5 Trend Power Oscillator
|
||||
// --------------------------------------------------
|
||||
// Name: XTPW
|
||||
// Description: trend power detecting in both sides ...
|
||||
//
|
||||
//
|
||||
// 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 XTPW Oscillator"
|
||||
#property strict
|
||||
|
||||
//
|
||||
// START Constants ...
|
||||
//
|
||||
#define ShortName "XTPW"
|
||||
//
|
||||
// END Constants ...
|
||||
//
|
||||
|
||||
//
|
||||
// START Inputs ...
|
||||
//
|
||||
input int length = 14; // Market Length
|
||||
//
|
||||
// END Inputs ...
|
||||
//
|
||||
|
||||
//
|
||||
// Includes Logging Library ...
|
||||
#include "../Libraries/x-saherelm.log.lib.mq5";
|
||||
|
||||
//
|
||||
// Include Common and Models Library ...
|
||||
#include "../Libraries/x-saherelm.common.lib.mq5";
|
||||
|
||||
//
|
||||
// START Buffers ...
|
||||
//
|
||||
#property indicator_separate_window
|
||||
|
||||
//
|
||||
#property indicator_buffers 2
|
||||
#property indicator_plots 2
|
||||
|
||||
//
|
||||
#property indicator_minimum 0
|
||||
#property indicator_maximum 100
|
||||
|
||||
//
|
||||
#define bullishBufferIndex 0
|
||||
#define bearishBufferIndex 1
|
||||
|
||||
double bullishBuffer[];
|
||||
double bearishBuffer[];
|
||||
|
||||
#property indicator_label1 "XBullPower"
|
||||
#property indicator_type1 DRAW_HISTOGRAM
|
||||
#property indicator_color1 clrLime
|
||||
#property indicator_style1 STYLE_SOLID
|
||||
#property indicator_width1 1
|
||||
|
||||
#property indicator_label2 "XBearPower"
|
||||
#property indicator_type2 DRAW_HISTOGRAM
|
||||
#property indicator_color2 clrRed
|
||||
#property indicator_style2 STYLE_SOLID
|
||||
#property indicator_width2 1
|
||||
//
|
||||
// END Buffers ...
|
||||
//
|
||||
|
||||
//
|
||||
// START Global Definitions: Variables, Properties and etc ...
|
||||
//
|
||||
//
|
||||
// END Global Definitions: Variables, Properties and etc ...
|
||||
//
|
||||
|
||||
//
|
||||
// START Event Handlers ...
|
||||
//
|
||||
//
|
||||
// Initialization ...
|
||||
int OnInit()
|
||||
{
|
||||
//
|
||||
// Validate Inputs ...
|
||||
if (!ValidateInputs())
|
||||
{
|
||||
return INIT_PARAMETERS_INCORRECT;
|
||||
}
|
||||
|
||||
//
|
||||
logTag = ShortName;
|
||||
|
||||
//
|
||||
// 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
|
||||
|
||||
//
|
||||
// Here we can handle De Initialization Reasons ...
|
||||
}
|
||||
|
||||
//
|
||||
// Calculations ...
|
||||
//
|
||||
// Calculating what we want ...
|
||||
int OnCalculate(
|
||||
//
|
||||
// total Candles on chart ...
|
||||
const int rates_total,
|
||||
//
|
||||
// total calculated Candles on charts ...
|
||||
const int prev_calculated,
|
||||
//
|
||||
// history of Candles Open Time ...
|
||||
const datetime &time[],
|
||||
//
|
||||
// history of Candles Open Price ...
|
||||
const double &open[],
|
||||
//
|
||||
// history of Candles High Price ...
|
||||
const double &high[],
|
||||
//
|
||||
// history of Candles Low Price ...
|
||||
const double &low[],
|
||||
//
|
||||
// history of Candles Close Price ...
|
||||
const double &close[],
|
||||
//
|
||||
// history of Tick Volumes on Candle ...
|
||||
const long &tick_volume[],
|
||||
//
|
||||
// history of Trade Volumes ...
|
||||
const long &volume[],
|
||||
//
|
||||
// history of Candles Spread Price ...
|
||||
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);
|
||||
|
||||
//
|
||||
// this counts Available Bars ...
|
||||
int limit;
|
||||
|
||||
//
|
||||
// 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 ...
|
||||
int maxLength = MathMax(0, length);
|
||||
|
||||
//
|
||||
limit = prev_calculated < maxLength ? rates_total - (maxLength + 1) : rates_total - prev_calculated;
|
||||
|
||||
//
|
||||
// Main Loop ...
|
||||
for (int i = limit; i >= 0 && !IsStopped(); i--)
|
||||
{
|
||||
//
|
||||
CalculateBuffers(i);
|
||||
}
|
||||
|
||||
//
|
||||
return rates_total;
|
||||
}
|
||||
//
|
||||
// END Event Handlers ...
|
||||
//
|
||||
|
||||
//
|
||||
// START Functions ...
|
||||
//
|
||||
//
|
||||
// Validate Input Args for Initialization ...
|
||||
bool ValidateInputs()
|
||||
{
|
||||
//
|
||||
bool result = false;
|
||||
|
||||
//
|
||||
// Validate Args ...
|
||||
if (length >= 5)
|
||||
{
|
||||
result = true;
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
// Define Indexes and Styles ...
|
||||
void DefineBuffers()
|
||||
{
|
||||
//
|
||||
string bullishBufferLabel = "BullPow " + "(" + (string)length + ")";
|
||||
string bearishBufferLabel = "BearPow " + "(" + (string)length + ")";
|
||||
|
||||
//
|
||||
// Bullish Buffer ...
|
||||
ArraySetAsSeries(bullishBuffer, true);
|
||||
SetIndexBuffer(bullishBufferIndex, bullishBuffer, INDICATOR_DATA);
|
||||
PlotIndexSetInteger(bullishBufferIndex, PLOT_DRAW_BEGIN, length * 2);
|
||||
PlotIndexSetString(bullishBufferIndex, PLOT_LABEL, bullishBufferLabel);
|
||||
|
||||
//
|
||||
// Bearish Buffer ...
|
||||
ArraySetAsSeries(bearishBuffer, true);
|
||||
SetIndexBuffer(bearishBufferIndex, bearishBuffer, INDICATOR_DATA);
|
||||
PlotIndexSetInteger(bearishBufferIndex, PLOT_DRAW_BEGIN, length * 2);
|
||||
PlotIndexSetString(bearishBufferIndex, PLOT_LABEL, bearishBufferLabel);
|
||||
}
|
||||
|
||||
//
|
||||
// Set Indicator Short Name and also we can define Buffers Labels ...
|
||||
void SetIndicatorName()
|
||||
{
|
||||
//
|
||||
string indicatorShortName = "";
|
||||
StringConcatenate(indicatorShortName, ShortName, " ", "( ", length, " )");
|
||||
|
||||
//
|
||||
IndicatorSetString(INDICATOR_SHORTNAME, indicatorShortName);
|
||||
}
|
||||
|
||||
//
|
||||
// Calculate Buffers ...
|
||||
void CalculateBuffers(int bar_index)
|
||||
{
|
||||
//
|
||||
double bullPower = 0;
|
||||
double bearPower = 0;
|
||||
|
||||
//
|
||||
double upperRange = 100;
|
||||
double lowerRange = 0;
|
||||
|
||||
//
|
||||
for (int i = bar_index + 1; i < bar_index + length + 1; i++) {
|
||||
//
|
||||
XOHCL c = GetCandle(i);
|
||||
|
||||
//
|
||||
double cRange =
|
||||
MathAbs(c.open - c.close);
|
||||
|
||||
//
|
||||
bool isBullish = c.open < c.close;
|
||||
bool isBearish = c.open > c.close;
|
||||
|
||||
//
|
||||
if (isBullish) {
|
||||
bullPower += cRange;
|
||||
} else {
|
||||
bearPower += cRange;
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
bullishBuffer[bar_index] = bullPower;
|
||||
bearishBuffer[bar_index] = bearPower;
|
||||
|
||||
// //
|
||||
// double maxBullRange = bullishBuffer[ArrayMaximum(bullishBuffer)];
|
||||
// double minBullRange = bullishBuffer[ArrayMinimum(bullishBuffer)];
|
||||
|
||||
// //
|
||||
// double maxBearRange = bearishBuffer[ArrayMaximum(bearishBuffer)];
|
||||
// double minBearRange = bearishBuffer[ArrayMinimum(bearishBuffer)];
|
||||
|
||||
// //
|
||||
// // Bullish Buffer ...
|
||||
// double normalBullPower = (((bullPower - minBullRange) * (upperRange - lowerRange)) / (maxBullRange - minBullRange)) + lowerRange;
|
||||
// bullishBuffer[bar_index] = normalBullPower;
|
||||
|
||||
// //
|
||||
// // Bearish Buffer ...
|
||||
// double normalBearPower = (((bearPower - minBearRange) * (upperRange - lowerRange)) / (maxBearRange - minBearRange)) + lowerRange;
|
||||
// bearishBuffer[bar_index] = normalBearPower;
|
||||
}
|
||||
//
|
||||
// END Functions ...
|
||||
//
|
||||
@@ -0,0 +1,476 @@
|
||||
/////////////////////////////////////////////////////////////
|
||||
//
|
||||
// SaherElm IT Center MQL5 Cycle Channel Oscillator
|
||||
// ---------------------------------------------------------
|
||||
// Name: XCCH
|
||||
// Description: Cycle Channel Oscillator ...
|
||||
//
|
||||
//
|
||||
// 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 XCCH Oscillator"
|
||||
#property strict
|
||||
|
||||
//
|
||||
// START Constants ...
|
||||
//
|
||||
#define ShortName "XCCH"
|
||||
|
||||
//
|
||||
// Including Models Library ...
|
||||
#include "..\Libraries\x-saherelm.models.lib.mq5"
|
||||
//
|
||||
// END Constants ...
|
||||
//
|
||||
|
||||
//
|
||||
// START Inputs ...
|
||||
//
|
||||
//
|
||||
input int fastLength = 10; // Fast Length
|
||||
input double fastMultiplier = 1.0; // Fast Multiplier
|
||||
|
||||
//
|
||||
input int slowLength = 30; // Slow Length
|
||||
input double slowMultiplier = 3.0; // Slow Multiplier
|
||||
|
||||
//
|
||||
input ENUM_APPLIED_PRICE appliedTo = PRICE_CLOSE; // Source
|
||||
//
|
||||
// END Inputs ...
|
||||
//
|
||||
|
||||
//
|
||||
// Includes Logging Library ...
|
||||
#include "../Libraries/x-saherelm.log.lib.mq5";
|
||||
|
||||
//
|
||||
// Includes Common Library ...
|
||||
#include "../Libraries/x-saherelm.common.lib.mq5";
|
||||
|
||||
//
|
||||
// START Buffers ...
|
||||
//
|
||||
#property indicator_separate_window
|
||||
|
||||
//
|
||||
#property indicator_buffers 6
|
||||
#property indicator_plots 2
|
||||
|
||||
//
|
||||
#define fastBufferIndex 0
|
||||
#define slowBufferIndex 1
|
||||
#define fastRMABufferIndex 2
|
||||
#define slowRMABufferIndex 3
|
||||
#define fastATRBufferIndex 4
|
||||
#define slowATRBufferIndex 5
|
||||
|
||||
//
|
||||
double fastBuffer[];
|
||||
double slowBuffer[];
|
||||
double fastRMABuffer[];
|
||||
double slowRMABuffer[];
|
||||
double fastATRBuffer[];
|
||||
double slowATRBuffer[];
|
||||
|
||||
//
|
||||
#property indicator_label1 "XCCH Fast"
|
||||
#property indicator_type1 DRAW_LINE
|
||||
#property indicator_color1 clrAqua
|
||||
#property indicator_style1 STYLE_DOT
|
||||
#property indicator_width1 1
|
||||
|
||||
#property indicator_label2 "XCCH Slow"
|
||||
#property indicator_type2 DRAW_LINE
|
||||
#property indicator_color2 clrFuchsia
|
||||
#property indicator_style2 STYLE_DOT
|
||||
#property indicator_width2 1
|
||||
//
|
||||
// END Buffers ...
|
||||
//
|
||||
|
||||
//
|
||||
// START Global Definitions: Variables, Properties and etc ...
|
||||
//
|
||||
//
|
||||
int fastRMAHandler = INVALID_HANDLE;
|
||||
int slowRMAHandler = INVALID_HANDLE;
|
||||
int fastATRHandler = INVALID_HANDLE;
|
||||
int slowATRHandler = INVALID_HANDLE;
|
||||
//
|
||||
// END Global Definitions: Variables, Properties and etc ...
|
||||
//
|
||||
|
||||
//
|
||||
// START Event Handlers ...
|
||||
//
|
||||
//
|
||||
// Initialization ...
|
||||
int OnInit()
|
||||
{
|
||||
//
|
||||
// Validate Inputs ...
|
||||
if (!ValidateInputs())
|
||||
{
|
||||
return INIT_PARAMETERS_INCORRECT;
|
||||
}
|
||||
|
||||
//
|
||||
logTag = ShortName;
|
||||
|
||||
//
|
||||
// Define Index Buffers ...
|
||||
DefineBuffers();
|
||||
|
||||
//
|
||||
// Set Indicator ShortName ...
|
||||
SetIndicatorName();
|
||||
|
||||
//
|
||||
// Resetting Last Error ...
|
||||
ResetLastError();
|
||||
|
||||
//
|
||||
// Initialized Requirements ...
|
||||
//
|
||||
// Fast ATR Handler ...
|
||||
fastATRHandler = iATR(
|
||||
_Symbol,
|
||||
_Period,
|
||||
fastLength / 2);
|
||||
if (fastATRHandler == INVALID_HANDLE)
|
||||
{
|
||||
//
|
||||
LogMessage("ATR Indicator initialization failed: " + (string)GetLastError());
|
||||
return INIT_FAILED;
|
||||
}
|
||||
|
||||
//
|
||||
// Slow ATR Handler ...
|
||||
slowATRHandler = iATR(
|
||||
_Symbol,
|
||||
_Period,
|
||||
slowLength / 2);
|
||||
if (slowATRHandler == INVALID_HANDLE)
|
||||
{
|
||||
//
|
||||
LogMessage("ATR Indicator initialization failed: " + (string)GetLastError());
|
||||
return INIT_FAILED;
|
||||
}
|
||||
|
||||
//
|
||||
// Fast RMA ...
|
||||
fastRMAHandler = iCustom(
|
||||
_Symbol,
|
||||
_Period,
|
||||
"x-saherelm.xrma.indicator",
|
||||
//
|
||||
// Inputs ...
|
||||
fastLength / 2,
|
||||
appliedTo);
|
||||
if (fastRMAHandler == INVALID_HANDLE)
|
||||
{
|
||||
//
|
||||
LogMessage("XRMA Indicator initialization failed: " + (string)GetLastError());
|
||||
return INIT_FAILED;
|
||||
}
|
||||
|
||||
//
|
||||
// Slow RMA ...
|
||||
slowRMAHandler = iCustom(
|
||||
_Symbol,
|
||||
_Period,
|
||||
"x-saherelm.xrma.indicator",
|
||||
//
|
||||
// Inputs ...
|
||||
slowLength / 2,
|
||||
appliedTo);
|
||||
if (fastRMAHandler == INVALID_HANDLE)
|
||||
{
|
||||
//
|
||||
LogMessage("XRMA Indicator initialization failed: " + (string)GetLastError());
|
||||
return INIT_FAILED;
|
||||
}
|
||||
|
||||
//
|
||||
// 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
|
||||
|
||||
//
|
||||
// Here we can handle De Initialization Reasons ...
|
||||
|
||||
//
|
||||
// ReDraw Chart ...
|
||||
ChartRedraw();
|
||||
|
||||
//
|
||||
Comment("");
|
||||
}
|
||||
|
||||
//
|
||||
// Calculations ...
|
||||
//
|
||||
// Calculating what we want ...
|
||||
int OnCalculate(
|
||||
//
|
||||
// total Candles on chart ...
|
||||
const int rates_total,
|
||||
//
|
||||
// total calculated Candles on charts ...
|
||||
const int prev_calculated,
|
||||
//
|
||||
// history of Candles Open Time ...
|
||||
const datetime &time[],
|
||||
//
|
||||
// history of Candles Open Price ...
|
||||
const double &open[],
|
||||
//
|
||||
// history of Candles High Price ...
|
||||
const double &high[],
|
||||
//
|
||||
// history of Candles Low Price ...
|
||||
const double &low[],
|
||||
//
|
||||
// history of Candles Close Price ...
|
||||
const double &close[],
|
||||
//
|
||||
// history of Tick Volumes on Candle ...
|
||||
const long &tick_volume[],
|
||||
//
|
||||
// history of Trade Volumes ...
|
||||
const long &volume[],
|
||||
//
|
||||
// history of Candles Spread Price ...
|
||||
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);
|
||||
|
||||
//
|
||||
// this counts Available Bars ...
|
||||
int limit;
|
||||
|
||||
//
|
||||
// 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 ...
|
||||
int maxLength = MathMax(fastLength, slowLength);
|
||||
maxLength = MathMax(maxLength, 14);
|
||||
|
||||
//
|
||||
// Get Retrieved Values from indicators ...
|
||||
int fastRMACalculatedBars = BarsCalculated(fastRMAHandler);
|
||||
int slowRMACalculatedBars = BarsCalculated(slowRMAHandler);
|
||||
int fastATRCalculatedBars = BarsCalculated(fastATRHandler);
|
||||
int slowATRCalculatedBars = BarsCalculated(slowATRHandler);
|
||||
|
||||
//
|
||||
// Check Calculated Bars ...
|
||||
if (
|
||||
fastRMACalculatedBars < maxLength ||
|
||||
slowRMACalculatedBars < maxLength ||
|
||||
fastATRCalculatedBars < maxLength ||
|
||||
slowATRCalculatedBars < maxLength)
|
||||
{
|
||||
return prev_calculated;
|
||||
}
|
||||
|
||||
//
|
||||
limit = prev_calculated < maxLength ? rates_total - (maxLength + 1) : rates_total - prev_calculated;
|
||||
|
||||
//
|
||||
// Copy Buffers from indicators ...
|
||||
int fastRMACopiedItems = CopyBuffer(fastRMAHandler, 0, 0, limit + 1, fastRMABuffer);
|
||||
int slowRMACopiedItems = CopyBuffer(slowRMAHandler, 0, 0, limit + 1, slowRMABuffer);
|
||||
int fastATRCopiedItems = CopyBuffer(fastATRHandler, 0, 0, limit + 1, fastATRBuffer);
|
||||
int slowATRCopiedItems = CopyBuffer(slowATRHandler, 0, 0, limit + 1, slowATRBuffer);
|
||||
|
||||
//
|
||||
// Check Copied Items Number ...
|
||||
if (
|
||||
fastRMACopiedItems <= 0 ||
|
||||
slowRMACopiedItems <= 0 ||
|
||||
fastATRCopiedItems <= 0 ||
|
||||
slowATRCopiedItems <= 0)
|
||||
{
|
||||
return prev_calculated;
|
||||
}
|
||||
|
||||
//
|
||||
// Main Loop ...
|
||||
for (int i = limit; i >= 0 && !IsStopped(); i--)
|
||||
{
|
||||
//
|
||||
CalculateBuffers(i);
|
||||
}
|
||||
|
||||
//
|
||||
return rates_total;
|
||||
}
|
||||
//
|
||||
// END Event Handlers ...
|
||||
//
|
||||
|
||||
//
|
||||
// START Functions ...
|
||||
//
|
||||
//
|
||||
// Validate Input Args for Initialization ...
|
||||
bool ValidateInputs()
|
||||
{
|
||||
//
|
||||
bool result = false;
|
||||
|
||||
//
|
||||
// Validate Args ...
|
||||
if (fastLength >= 4 && slowLength > fastLength)
|
||||
{
|
||||
result = true;
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
// Define Indexes and Styles ...
|
||||
void DefineBuffers()
|
||||
{
|
||||
//
|
||||
string fastBufferLabel = ShortName + " Fast (" + (string)fastLength + ")";
|
||||
string slowBufferLabel = ShortName + " Slow (" + (string)slowLength + ")";
|
||||
|
||||
//
|
||||
// Fast Buffer ...
|
||||
ArraySetAsSeries(fastBuffer, true);
|
||||
SetIndexBuffer(fastBufferIndex, fastBuffer, INDICATOR_DATA);
|
||||
PlotIndexSetInteger(fastBufferIndex, PLOT_DRAW_BEGIN, fastLength);
|
||||
PlotIndexSetString(fastBufferIndex, PLOT_LABEL, fastBufferLabel);
|
||||
|
||||
//
|
||||
// Slow Buffer ...
|
||||
ArraySetAsSeries(slowBuffer, true);
|
||||
SetIndexBuffer(slowBufferIndex, slowBuffer, INDICATOR_DATA);
|
||||
PlotIndexSetInteger(slowBufferIndex, PLOT_DRAW_BEGIN, slowLength);
|
||||
PlotIndexSetString(slowBufferIndex, PLOT_LABEL, slowBufferLabel);
|
||||
|
||||
//
|
||||
// Fast RMA Buffer ...
|
||||
ArraySetAsSeries(fastRMABuffer, true);
|
||||
SetIndexBuffer(fastRMABufferIndex, fastRMABuffer, INDICATOR_CALCULATIONS);
|
||||
|
||||
//
|
||||
// Slow RMA Buffer ...
|
||||
ArraySetAsSeries(slowRMABuffer, true);
|
||||
SetIndexBuffer(slowRMABufferIndex, slowRMABuffer, INDICATOR_CALCULATIONS);
|
||||
|
||||
//
|
||||
// Fast ATR Buffer ...
|
||||
ArraySetAsSeries(fastATRBuffer, true);
|
||||
SetIndexBuffer(fastATRBufferIndex, fastATRBuffer, INDICATOR_CALCULATIONS);
|
||||
|
||||
//
|
||||
// Slow ATR Buffer ...
|
||||
ArraySetAsSeries(slowATRBuffer, true);
|
||||
SetIndexBuffer(slowATRBufferIndex, slowATRBuffer, INDICATOR_CALCULATIONS);
|
||||
}
|
||||
|
||||
//
|
||||
// Set Indicator Short Name and also we can define Buffers Labels ...
|
||||
void SetIndicatorName()
|
||||
{
|
||||
//
|
||||
string indicatorShortName = "";
|
||||
StringConcatenate(indicatorShortName, ShortName, "");
|
||||
|
||||
//
|
||||
IndicatorSetString(INDICATOR_SHORTNAME, indicatorShortName);
|
||||
}
|
||||
|
||||
//
|
||||
// Calculate Buffers ...
|
||||
void CalculateBuffers(int bar_index)
|
||||
{
|
||||
//
|
||||
// Retrieve RMA Values ...
|
||||
double fastRMA = fastRMABuffer[bar_index];
|
||||
double slowRMA = slowRMABuffer[bar_index];
|
||||
|
||||
//
|
||||
// Calculate Offsetes ...
|
||||
double fastOffset = fastMultiplier * fastATRBuffer[bar_index];
|
||||
double slowOffset = slowMultiplier * slowATRBuffer[bar_index];
|
||||
|
||||
//
|
||||
int hFastLength = fastLength / 2;
|
||||
int hSlowLength = slowLength / 2;
|
||||
int qFastLength = hFastLength / 2;
|
||||
int qSlowLength = hSlowLength / 2;
|
||||
|
||||
//
|
||||
double price = GetAppliedPrice(bar_index, appliedTo);
|
||||
|
||||
//
|
||||
// Calculate Fast Top and Bottom ...
|
||||
double fastTOP = (ArraySize(fastRMABuffer) == qFastLength + 1 ? fastRMABuffer[qFastLength] : price) + fastOffset;
|
||||
double fastBOTTOM = (ArraySize(fastRMABuffer) == qFastLength + 1 ? fastRMABuffer[qFastLength] : price) - fastOffset;
|
||||
|
||||
//
|
||||
// Calculate Slow Top and Bottom ...
|
||||
double slowTOP = (ArraySize(slowRMABuffer) == qSlowLength + 1 ? slowRMABuffer[qSlowLength] : price) + slowOffset;
|
||||
double slowBOTTOM = (ArraySize(slowRMABuffer) == qSlowLength + 1 ? slowRMABuffer[qSlowLength] : price) - slowOffset;
|
||||
|
||||
//
|
||||
// Fast Average ...
|
||||
double fastAverage = (fastTOP + fastBOTTOM) / 2;
|
||||
|
||||
//
|
||||
// Calculate Buffer Values ...
|
||||
|
||||
//
|
||||
// Fast ...
|
||||
double fastValue = (price - slowBOTTOM) / (slowTOP - slowBOTTOM);
|
||||
fastBuffer[bar_index] = fastValue;
|
||||
|
||||
//
|
||||
// Slow ...
|
||||
double slowValue = (fastAverage - slowBOTTOM) / (slowTOP - slowBOTTOM);
|
||||
slowBuffer[bar_index] = slowValue;
|
||||
}
|
||||
//
|
||||
// END Functions ...
|
||||
//
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,443 @@
|
||||
/////////////////////////////////////////////////////////////
|
||||
//
|
||||
// SaherElm IT Center MQL5 Cycle Trend Detector Indicator
|
||||
// ---------------------------------------------------------
|
||||
// Name: XCTD
|
||||
// Description: trend detecting based on cycles ...
|
||||
//
|
||||
//
|
||||
// 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 XCTD Indicator"
|
||||
#property strict
|
||||
|
||||
//
|
||||
// START Constants ...
|
||||
//
|
||||
#define ShortName "XCTD"
|
||||
|
||||
//
|
||||
// Including Models Library ...
|
||||
#include "..\Libraries\x-saherelm.models.lib.mq5"
|
||||
//
|
||||
// END Constants ...
|
||||
//
|
||||
|
||||
//
|
||||
// START Inputs ...
|
||||
//
|
||||
//
|
||||
input int fastLength = 9; // Fast Length
|
||||
input int slowLength = 18; // Slow Length
|
||||
|
||||
//
|
||||
input ENUM_MA_METHOD method = MODE_EMA; // Calculation Method
|
||||
input ENUM_APPLIED_PRICE appliedTo = PRICE_CLOSE; // Applied To
|
||||
//
|
||||
// END Inputs ...
|
||||
//
|
||||
|
||||
//
|
||||
// Includes Logging Library ...
|
||||
#include "../Libraries/x-saherelm.log.lib.mq5";
|
||||
|
||||
//
|
||||
// Includes Common Library ...
|
||||
#include "../Libraries/x-saherelm.common.lib.mq5";
|
||||
|
||||
//
|
||||
// START Buffers ...
|
||||
//
|
||||
#property indicator_separate_window
|
||||
|
||||
//
|
||||
#property indicator_buffers 4
|
||||
#property indicator_plots 2
|
||||
|
||||
//
|
||||
#define fastBufferIndex 0
|
||||
#define slowBufferIndex 1
|
||||
#define stateBufferIndex 2
|
||||
#define maBufferIndex 3
|
||||
|
||||
//
|
||||
double fastBuffer[];
|
||||
double slowBuffer[];
|
||||
double stateBuffer[];
|
||||
double maBuffer[];
|
||||
|
||||
//
|
||||
#property indicator_label1 "XCTD Fast"
|
||||
#property indicator_type1 DRAW_LINE
|
||||
#property indicator_color1 clrAqua
|
||||
#property indicator_style1 STYLE_DOT
|
||||
#property indicator_width1 1
|
||||
|
||||
#property indicator_label2 "XCTD Slow"
|
||||
#property indicator_type2 DRAW_LINE
|
||||
#property indicator_color2 clrFuchsia
|
||||
#property indicator_style2 STYLE_DOT
|
||||
#property indicator_width2 1
|
||||
//
|
||||
// END Buffers ...
|
||||
//
|
||||
|
||||
//
|
||||
// START Global Definitions: Variables, Properties and etc ...
|
||||
//
|
||||
//
|
||||
ENUM_TIMEFRAMES cyclePeriod;
|
||||
int maHandler = INVALID_HANDLE;
|
||||
int fastHandler = INVALID_HANDLE;
|
||||
int slowHandler = INVALID_HANDLE;
|
||||
//
|
||||
// END Global Definitions: Variables, Properties and etc ...
|
||||
//
|
||||
|
||||
//
|
||||
// START Event Handlers ...
|
||||
//
|
||||
//
|
||||
// Initialization ...
|
||||
int OnInit()
|
||||
{
|
||||
//
|
||||
// Validate Inputs ...
|
||||
if (!ValidateInputs())
|
||||
{
|
||||
return INIT_PARAMETERS_INCORRECT;
|
||||
}
|
||||
|
||||
//
|
||||
logTag = ShortName;
|
||||
|
||||
//
|
||||
// Define Index Buffers ...
|
||||
DefineBuffers();
|
||||
|
||||
//
|
||||
// Set Indicator ShortName ...
|
||||
SetIndicatorName();
|
||||
|
||||
//
|
||||
// Resetting Last Error ...
|
||||
ResetLastError();
|
||||
|
||||
//
|
||||
// Initialized Requirements ...
|
||||
cyclePeriod = GetFarestTimeFrame();
|
||||
|
||||
//
|
||||
// Ma Buffer ...
|
||||
maHandler = iMA(
|
||||
_Symbol,
|
||||
_Period,
|
||||
14,
|
||||
0,
|
||||
MODE_SMA,
|
||||
PRICE_CLOSE);
|
||||
if (maHandler == INVALID_HANDLE)
|
||||
{
|
||||
//
|
||||
LogMessage("Error in Ma Indicator initialization: " + (string)GetLastError());
|
||||
|
||||
//
|
||||
return INIT_FAILED;
|
||||
}
|
||||
|
||||
//
|
||||
// fatst Handler ...
|
||||
fastHandler = iMA(
|
||||
_Symbol,
|
||||
cyclePeriod,
|
||||
fastLength,
|
||||
0,
|
||||
method,
|
||||
appliedTo);
|
||||
if (fastHandler == INVALID_HANDLE)
|
||||
{
|
||||
//
|
||||
LogMessage("Error in Fast Indicator initialization: " + (string)GetLastError());
|
||||
|
||||
//
|
||||
return INIT_FAILED;
|
||||
}
|
||||
|
||||
//
|
||||
// slow Handler ...
|
||||
slowHandler = iMA(
|
||||
_Symbol,
|
||||
cyclePeriod,
|
||||
slowLength,
|
||||
0,
|
||||
method,
|
||||
appliedTo);
|
||||
if (slowHandler == INVALID_HANDLE)
|
||||
{
|
||||
//
|
||||
LogMessage("Error in Slow Indicator initialization: " + (string)GetLastError());
|
||||
|
||||
//
|
||||
return INIT_FAILED;
|
||||
}
|
||||
|
||||
//
|
||||
Comment(ShortName + " Cycle Period: " + EnumToString(cyclePeriod));
|
||||
|
||||
//
|
||||
// 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
|
||||
|
||||
//
|
||||
// Here we can handle De Initialization Reasons ...
|
||||
|
||||
//
|
||||
// ReDraw Chart ...
|
||||
ChartRedraw();
|
||||
|
||||
//
|
||||
Comment("");
|
||||
}
|
||||
|
||||
//
|
||||
// Calculations ...
|
||||
//
|
||||
// Calculating what we want ...
|
||||
int OnCalculate(
|
||||
//
|
||||
// total Candles on chart ...
|
||||
const int rates_total,
|
||||
//
|
||||
// total calculated Candles on charts ...
|
||||
const int prev_calculated,
|
||||
//
|
||||
// history of Candles Open Time ...
|
||||
const datetime &time[],
|
||||
//
|
||||
// history of Candles Open Price ...
|
||||
const double &open[],
|
||||
//
|
||||
// history of Candles High Price ...
|
||||
const double &high[],
|
||||
//
|
||||
// history of Candles Low Price ...
|
||||
const double &low[],
|
||||
//
|
||||
// history of Candles Close Price ...
|
||||
const double &close[],
|
||||
//
|
||||
// history of Tick Volumes on Candle ...
|
||||
const long &tick_volume[],
|
||||
//
|
||||
// history of Trade Volumes ...
|
||||
const long &volume[],
|
||||
//
|
||||
// history of Candles Spread Price ...
|
||||
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);
|
||||
|
||||
//
|
||||
// this counts Available Bars ...
|
||||
int limit;
|
||||
|
||||
//
|
||||
// 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 ...
|
||||
int maxLength = MathMax(fastLength, slowLength);
|
||||
maxLength = MathMax(maxLength, 14);
|
||||
|
||||
//
|
||||
// Get Retrieved Values from indicators ...
|
||||
int maCalculatedBars = BarsCalculated(maHandler);
|
||||
int fastCalculatedBars = BarsCalculated(fastHandler);
|
||||
int slowCalculatedBars = BarsCalculated(slowHandler);
|
||||
|
||||
//
|
||||
// Check Calculated Bars ...
|
||||
if (
|
||||
maCalculatedBars < maxLength ||
|
||||
fastCalculatedBars < maxLength ||
|
||||
slowCalculatedBars < maxLength)
|
||||
{
|
||||
return prev_calculated;
|
||||
}
|
||||
|
||||
//
|
||||
limit = prev_calculated < maxLength ? rates_total - (maxLength + 1) : rates_total - prev_calculated;
|
||||
|
||||
//
|
||||
// Copy Buffers from indicators ...
|
||||
int maCopiedItems = CopyBuffer(maHandler, 0, 0, limit + 1, maBuffer);
|
||||
int fastCopiedItems = CopyBuffer(fastHandler, 0, 0, limit + 1, fastBuffer);
|
||||
int slowCopiedItems = CopyBuffer(slowHandler, 0, 0, limit + 1, slowBuffer);
|
||||
|
||||
//
|
||||
// Check Copied Items Number ...
|
||||
if (
|
||||
maCopiedItems <= 0 ||
|
||||
fastCopiedItems <= 0 ||
|
||||
slowCopiedItems <= 0)
|
||||
{
|
||||
return prev_calculated;
|
||||
}
|
||||
|
||||
//
|
||||
// Main Loop ...
|
||||
for (int i = limit; i >= 0 && !IsStopped(); i--)
|
||||
{
|
||||
//
|
||||
CalculateBuffers(i);
|
||||
}
|
||||
|
||||
//
|
||||
return rates_total;
|
||||
}
|
||||
//
|
||||
// END Event Handlers ...
|
||||
//
|
||||
|
||||
//
|
||||
// START Functions ...
|
||||
//
|
||||
//
|
||||
// Validate Input Args for Initialization ...
|
||||
bool ValidateInputs()
|
||||
{
|
||||
//
|
||||
bool result = false;
|
||||
|
||||
//
|
||||
// Validate Args ...
|
||||
if (fastLength >= 4 && slowLength > fastLength)
|
||||
{
|
||||
result = true;
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
// Define Indexes and Styles ...
|
||||
void DefineBuffers()
|
||||
{
|
||||
//
|
||||
string fastBufferLabel = ShortName + " Fast (" + (string)fastLength + ")";
|
||||
string slowBufferLabel = ShortName + " Slow (" + (string)slowLength + ")";
|
||||
|
||||
//
|
||||
// MA Buffer ...
|
||||
ArraySetAsSeries(maBuffer, true);
|
||||
SetIndexBuffer(maBufferIndex, maBuffer, INDICATOR_CALCULATIONS);
|
||||
|
||||
//
|
||||
// Fast Buffer ...
|
||||
ArraySetAsSeries(fastBuffer, true);
|
||||
SetIndexBuffer(fastBufferIndex, fastBuffer, INDICATOR_DATA);
|
||||
PlotIndexSetInteger(fastBufferIndex, PLOT_DRAW_BEGIN, fastLength);
|
||||
PlotIndexSetString(fastBufferIndex, PLOT_LABEL, fastBufferLabel);
|
||||
|
||||
//
|
||||
// Slow Buffer ...
|
||||
ArraySetAsSeries(slowBuffer, true);
|
||||
SetIndexBuffer(slowBufferIndex, slowBuffer, INDICATOR_DATA);
|
||||
PlotIndexSetInteger(slowBufferIndex, PLOT_DRAW_BEGIN, slowLength);
|
||||
PlotIndexSetString(slowBufferIndex, PLOT_LABEL, slowBufferLabel);
|
||||
|
||||
//
|
||||
// State Buffer ...
|
||||
ArraySetAsSeries(stateBuffer, true);
|
||||
SetIndexBuffer(stateBufferIndex, stateBuffer, INDICATOR_CALCULATIONS);
|
||||
}
|
||||
|
||||
//
|
||||
// Set Indicator Short Name and also we can define Buffers Labels ...
|
||||
void SetIndicatorName()
|
||||
{
|
||||
//
|
||||
string indicatorShortName = "";
|
||||
StringConcatenate(indicatorShortName, ShortName, "");
|
||||
|
||||
//
|
||||
IndicatorSetString(INDICATOR_SHORTNAME, indicatorShortName);
|
||||
}
|
||||
|
||||
//
|
||||
// Calculate Buffers ...
|
||||
void CalculateBuffers(int bar_index)
|
||||
{
|
||||
//
|
||||
// here we must calculate required Buffers ...
|
||||
// Ma, Fast and Slow buffers filled using CopyBuffer ...
|
||||
// StateBuffer needs to Calculate ...
|
||||
|
||||
//
|
||||
// Retrieve Different State ...
|
||||
bool isFastCrossedOverSlow = IsCrossOver(fastBuffer, slowBuffer, bar_index);
|
||||
bool isFastOverSlow = IsOver(fastBuffer, slowBuffer, bar_index);
|
||||
bool isFastCrossedUnderSlow = IsCrossUnder(fastBuffer, slowBuffer, bar_index);
|
||||
bool isFastUnderSlow = IsUnder(fastBuffer, slowBuffer, bar_index);
|
||||
|
||||
//
|
||||
// Calculate State Buffer ...
|
||||
double stateValue = X_XCTD_NEUTURAL;
|
||||
if (isFastCrossedOverSlow)
|
||||
{
|
||||
stateValue = X_XCTD_FAST_CROSSED_OVER_SLOW;
|
||||
}
|
||||
else if (isFastCrossedUnderSlow)
|
||||
{
|
||||
stateValue = X_XCTD_FAST_CROSSED_UNDER_SLOW;
|
||||
}
|
||||
else if (isFastOverSlow)
|
||||
{
|
||||
stateValue = X_XCTD_FAST_OVER_SLOW;
|
||||
}
|
||||
else if (isFastUnderSlow)
|
||||
{
|
||||
stateValue = X_XCTD_FAST_UNDER_SLOW;
|
||||
}
|
||||
|
||||
//
|
||||
stateBuffer[bar_index] = stateValue;
|
||||
}
|
||||
//
|
||||
// END Functions ...
|
||||
//
|
||||
@@ -0,0 +1,546 @@
|
||||
/////////////////////////////////////////////////////////////
|
||||
//
|
||||
// SaherElm IT Center MQL5 Cycle Trend Detector Indicator
|
||||
// ---------------------------------------------------------
|
||||
// Name: XCTD
|
||||
// Description: trend detecting based on cycles ...
|
||||
//
|
||||
//
|
||||
// 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 XCTD Indicator"
|
||||
#property strict
|
||||
|
||||
//
|
||||
// START Constants ...
|
||||
//
|
||||
#define ShortName "XCTD"
|
||||
|
||||
//
|
||||
// Including Models Library ...
|
||||
#include "..\Libraries\x-saherelm.models.lib.mq5"
|
||||
//
|
||||
// END Constants ...
|
||||
//
|
||||
|
||||
//
|
||||
// START Inputs ...
|
||||
//
|
||||
//
|
||||
input int fastLength = 9; // Fast Length
|
||||
input int slowLength = 18; // Slow Length
|
||||
|
||||
//
|
||||
input ENUM_MA_METHOD method = MODE_EMA; // Calculation Method
|
||||
input ENUM_APPLIED_PRICE appliedTo = PRICE_CLOSE; // Applied To
|
||||
//
|
||||
// END Inputs ...
|
||||
//
|
||||
|
||||
//
|
||||
// Includes Logging Library ...
|
||||
#include "../Libraries/x-saherelm.log.lib.mq5";
|
||||
|
||||
//
|
||||
// Includes Common Library ...
|
||||
#include "../Libraries/x-saherelm.common.lib.mq5";
|
||||
|
||||
//
|
||||
// START Buffers ...
|
||||
//
|
||||
#property indicator_chart_window
|
||||
|
||||
//
|
||||
#property indicator_buffers 6
|
||||
#property indicator_plots 2
|
||||
|
||||
//
|
||||
#define fastBufferIndex 0
|
||||
#define slowBufferIndex 1
|
||||
#define maBufferIndex 2
|
||||
#define stateBufferIndex 3
|
||||
#define fastPowerBufferIndex 4
|
||||
#define slowPowerBufferIndex 5
|
||||
|
||||
//
|
||||
double fastBuffer[];
|
||||
double slowBuffer[];
|
||||
|
||||
//
|
||||
// INDICATOR CALCULATIONS BUFFERS ...
|
||||
//
|
||||
// Just Holds a simple Moving Average for Chart Draw Long Cycles ...
|
||||
double maBuffer[];
|
||||
|
||||
//
|
||||
// holds Fast based On Slow States ...
|
||||
double stateBuffer[];
|
||||
|
||||
//
|
||||
// Holds Fast Summerize Value ...
|
||||
// if Fast[0] > Fast[1] => FastPow += Fast[1]
|
||||
// else FastPow -= Fast[1]
|
||||
double fastPowerBuffer[];
|
||||
|
||||
//
|
||||
// Holds Slow Summerize Value ...
|
||||
// if Slow[0] > Slow[1] => SlowPow += Slow[1]
|
||||
// else SlowPow -= Slow[1]
|
||||
double slowPowerBuffer[];
|
||||
|
||||
#property indicator_label1 "XCTD Fast"
|
||||
#property indicator_type1 DRAW_LINE
|
||||
#property indicator_color1 clrAqua
|
||||
#property indicator_style1 STYLE_DOT
|
||||
#property indicator_width1 1
|
||||
|
||||
#property indicator_label2 "XCTD Slow"
|
||||
#property indicator_type2 DRAW_LINE
|
||||
#property indicator_color2 clrFuchsia
|
||||
#property indicator_style2 STYLE_DOT
|
||||
#property indicator_width2 1
|
||||
//
|
||||
// END Buffers ...
|
||||
//
|
||||
|
||||
//
|
||||
// START Global Definitions: Variables, Properties and etc ...
|
||||
//
|
||||
//
|
||||
// XCTD States ...
|
||||
enum ENUM_X_XCTD_STATES
|
||||
{
|
||||
X_XCTD_FAST_CROSSED_OVER_SLOW = 2,
|
||||
X_XCTD_FAST_OVER_SLOW = 1,
|
||||
X_XCTD_NEUTURAL = 0,
|
||||
X_XCTD_FAST_UNDER_SLOW = -1,
|
||||
X_XCTD_FAST_CROSSED_UNDER_SLOW = -2,
|
||||
|
||||
};
|
||||
|
||||
//
|
||||
// XCTD Buffer Lines ...
|
||||
enum ENUM_X_XCTD_BUFFER_LINES
|
||||
{
|
||||
X_XCTD_FAST_LINE = 0,
|
||||
X_XCTD_SLOW_LINE = 1,
|
||||
X_XCTD_STATE_LINE = 2,
|
||||
};
|
||||
|
||||
//
|
||||
ENUM_TIMEFRAMES cyclePeriod;
|
||||
int maHandler = INVALID_HANDLE;
|
||||
int fastHandler = INVALID_HANDLE;
|
||||
int slowHandler = INVALID_HANDLE;
|
||||
//
|
||||
// END Global Definitions: Variables, Properties and etc ...
|
||||
//
|
||||
|
||||
//
|
||||
// START Event Handlers ...
|
||||
//
|
||||
//
|
||||
// Initialization ...
|
||||
int OnInit()
|
||||
{
|
||||
//
|
||||
// Validate Inputs ...
|
||||
if (!ValidateInputs())
|
||||
{
|
||||
return INIT_PARAMETERS_INCORRECT;
|
||||
}
|
||||
|
||||
//
|
||||
logTag = ShortName;
|
||||
|
||||
//
|
||||
// Define Index Buffers ...
|
||||
DefineBuffers();
|
||||
|
||||
//
|
||||
// Set Indicator ShortName ...
|
||||
SetIndicatorName();
|
||||
|
||||
//
|
||||
// Resetting Last Error ...
|
||||
ResetLastError();
|
||||
|
||||
//
|
||||
// Initialized Requirements ...
|
||||
cyclePeriod = GetFarestTimeFrame();
|
||||
|
||||
//
|
||||
// Ma Buffer ...
|
||||
maHandler = iMA(
|
||||
_Symbol,
|
||||
_Period,
|
||||
14,
|
||||
0,
|
||||
MODE_SMA,
|
||||
PRICE_CLOSE);
|
||||
if (maHandler == INVALID_HANDLE)
|
||||
{
|
||||
//
|
||||
LogMessage("Error in Ma Indicator initialization: " + (string)GetLastError());
|
||||
|
||||
//
|
||||
return INIT_FAILED;
|
||||
}
|
||||
|
||||
//
|
||||
// fatst Handler ...
|
||||
fastHandler = iMA(
|
||||
_Symbol,
|
||||
cyclePeriod,
|
||||
fastLength,
|
||||
0,
|
||||
method,
|
||||
appliedTo);
|
||||
if (fastHandler == INVALID_HANDLE)
|
||||
{
|
||||
//
|
||||
LogMessage("Error in Fast Indicator initialization: " + (string)GetLastError());
|
||||
|
||||
//
|
||||
return INIT_FAILED;
|
||||
}
|
||||
|
||||
//
|
||||
// slow Handler ...
|
||||
slowHandler = iMA(
|
||||
_Symbol,
|
||||
cyclePeriod,
|
||||
slowLength,
|
||||
0,
|
||||
method,
|
||||
appliedTo);
|
||||
if (slowHandler == INVALID_HANDLE)
|
||||
{
|
||||
//
|
||||
LogMessage("Error in Slow Indicator initialization: " + (string)GetLastError());
|
||||
|
||||
//
|
||||
return INIT_FAILED;
|
||||
}
|
||||
|
||||
//
|
||||
Comment(ShortName + " Cycle Period: " + EnumToString(cyclePeriod));
|
||||
|
||||
//
|
||||
// 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
|
||||
|
||||
//
|
||||
// Here we can handle De Initialization Reasons ...
|
||||
|
||||
//
|
||||
// ReDraw Chart ...
|
||||
ChartRedraw();
|
||||
|
||||
//
|
||||
Comment("");
|
||||
}
|
||||
|
||||
//
|
||||
// Calculations ...
|
||||
//
|
||||
// Calculating what we want ...
|
||||
int OnCalculate(
|
||||
//
|
||||
// total Candles on chart ...
|
||||
const int rates_total,
|
||||
//
|
||||
// total calculated Candles on charts ...
|
||||
const int prev_calculated,
|
||||
//
|
||||
// history of Candles Open Time ...
|
||||
const datetime &time[],
|
||||
//
|
||||
// history of Candles Open Price ...
|
||||
const double &open[],
|
||||
//
|
||||
// history of Candles High Price ...
|
||||
const double &high[],
|
||||
//
|
||||
// history of Candles Low Price ...
|
||||
const double &low[],
|
||||
//
|
||||
// history of Candles Close Price ...
|
||||
const double &close[],
|
||||
//
|
||||
// history of Tick Volumes on Candle ...
|
||||
const long &tick_volume[],
|
||||
//
|
||||
// history of Trade Volumes ...
|
||||
const long &volume[],
|
||||
//
|
||||
// history of Candles Spread Price ...
|
||||
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);
|
||||
|
||||
//
|
||||
// this counts Available Bars ...
|
||||
int limit;
|
||||
|
||||
//
|
||||
// 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 ...
|
||||
int maxLength = MathMax(fastLength, slowLength);
|
||||
maxLength = MathMax(maxLength, 14);
|
||||
|
||||
//
|
||||
// Get Retrieved Values from indicators ...
|
||||
int maCalculatedBars = BarsCalculated(maHandler);
|
||||
int fastCalculatedBars = BarsCalculated(fastHandler);
|
||||
int slowCalculatedBars = BarsCalculated(slowHandler);
|
||||
|
||||
//
|
||||
// Check Calculated Bars ...
|
||||
if (
|
||||
maCalculatedBars < maxLength ||
|
||||
fastCalculatedBars < maxLength ||
|
||||
slowCalculatedBars < maxLength)
|
||||
{
|
||||
return prev_calculated;
|
||||
}
|
||||
|
||||
//
|
||||
limit = prev_calculated < maxLength ? rates_total - (maxLength + 1) : rates_total - prev_calculated;
|
||||
|
||||
//
|
||||
// Copy Buffers from indicators ...
|
||||
int maCopiedItems = CopyBuffer(maHandler, 0, 0, limit + 1, maBuffer);
|
||||
int fastCopiedItems = CopyBuffer(fastHandler, 0, 0, limit + 1, fastBuffer);
|
||||
int slowCopiedItems = CopyBuffer(slowHandler, 0, 0, limit + 1, slowBuffer);
|
||||
|
||||
//
|
||||
// Check Copied Items Number ...
|
||||
if (
|
||||
maCopiedItems <= 0 ||
|
||||
fastCopiedItems <= 0 ||
|
||||
slowCopiedItems <= 0)
|
||||
{
|
||||
return prev_calculated;
|
||||
}
|
||||
|
||||
//
|
||||
// Main Loop ...
|
||||
for (int i = limit; i >= 0 && !IsStopped(); i--)
|
||||
{
|
||||
//
|
||||
CalculateBuffers(i);
|
||||
}
|
||||
|
||||
//
|
||||
return rates_total;
|
||||
}
|
||||
//
|
||||
// END Event Handlers ...
|
||||
//
|
||||
|
||||
//
|
||||
// START Functions ...
|
||||
//
|
||||
//
|
||||
// Validate Input Args for Initialization ...
|
||||
bool ValidateInputs()
|
||||
{
|
||||
//
|
||||
bool result = false;
|
||||
|
||||
//
|
||||
// Validate Args ...
|
||||
if (fastLength >= 4 && slowLength > fastLength)
|
||||
{
|
||||
result = true;
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
// Define Indexes and Styles ...
|
||||
void DefineBuffers()
|
||||
{
|
||||
//
|
||||
string fastBufferLabel = ShortName + " Fast (" + (string)fastLength + ")";
|
||||
string slowBufferLabel = ShortName + " Slow (" + (string)slowLength + ")";
|
||||
|
||||
//
|
||||
// MA Buffer ...
|
||||
ArraySetAsSeries(maBuffer, true);
|
||||
SetIndexBuffer(maBufferIndex, maBuffer, INDICATOR_CALCULATIONS);
|
||||
|
||||
//
|
||||
// Fast Buffer ...
|
||||
ArraySetAsSeries(fastBuffer, true);
|
||||
SetIndexBuffer(fastBufferIndex, fastBuffer, INDICATOR_DATA);
|
||||
PlotIndexSetInteger(fastBufferIndex, PLOT_DRAW_BEGIN, fastLength);
|
||||
PlotIndexSetString(fastBufferIndex, PLOT_LABEL, fastBufferLabel);
|
||||
|
||||
//
|
||||
// Slow Buffer ...
|
||||
ArraySetAsSeries(slowBuffer, true);
|
||||
SetIndexBuffer(slowBufferIndex, slowBuffer, INDICATOR_DATA);
|
||||
PlotIndexSetInteger(slowBufferIndex, PLOT_DRAW_BEGIN, slowLength);
|
||||
PlotIndexSetString(slowBufferIndex, PLOT_LABEL, slowBufferLabel);
|
||||
|
||||
//
|
||||
// State Buffer ...
|
||||
ArraySetAsSeries(stateBuffer, true);
|
||||
SetIndexBuffer(stateBufferIndex, stateBuffer, INDICATOR_CALCULATIONS);
|
||||
|
||||
//
|
||||
// Fast Power Buffer ...
|
||||
ArraySetAsSeries(fastPowerBuffer, true);
|
||||
SetIndexBuffer(fastPowerBufferIndex, fastPowerBuffer, INDICATOR_CALCULATIONS);
|
||||
|
||||
//
|
||||
// Slow Power Buffer ...
|
||||
ArraySetAsSeries(slowPowerBuffer, true);
|
||||
SetIndexBuffer(slowPowerBufferIndex, slowPowerBuffer, INDICATOR_CALCULATIONS);
|
||||
}
|
||||
|
||||
//
|
||||
// Set Indicator Short Name and also we can define Buffers Labels ...
|
||||
void SetIndicatorName()
|
||||
{
|
||||
//
|
||||
string indicatorShortName = "";
|
||||
StringConcatenate(indicatorShortName, ShortName, "");
|
||||
|
||||
//
|
||||
IndicatorSetString(INDICATOR_SHORTNAME, indicatorShortName);
|
||||
}
|
||||
|
||||
//
|
||||
// Calculate Buffers ...
|
||||
void CalculateBuffers(int bar_index)
|
||||
{
|
||||
//
|
||||
// here we must calculate required Buffers ...
|
||||
// Ma, Fast and Slow buffers filled using CopyBuffer ...
|
||||
// StateBuffer/FastPowerBuffer and SlowPowerBuffer needs to Calculate ...
|
||||
|
||||
//
|
||||
// Retrieve Different State ...
|
||||
bool isFastCrossedOverSlow = IsCrossOver(fastBuffer, slowBuffer, bar_index);
|
||||
bool isFastOverSlow = IsOver(fastBuffer, slowBuffer, bar_index);
|
||||
bool isFastCrossedUnderSlow = IsCrossUnder(fastBuffer, slowBuffer, bar_index);
|
||||
bool isFastUnderSlow = IsUnder(fastBuffer, slowBuffer, bar_index);
|
||||
|
||||
//
|
||||
// Calculate State Buffer ...
|
||||
double stateValue = X_XCTD_NEUTURAL;
|
||||
if (isFastCrossedOverSlow)
|
||||
{
|
||||
stateValue = X_XCTD_FAST_CROSSED_OVER_SLOW;
|
||||
}
|
||||
else if (isFastCrossedUnderSlow)
|
||||
{
|
||||
stateValue = X_XCTD_FAST_CROSSED_UNDER_SLOW;
|
||||
}
|
||||
else if (isFastOverSlow)
|
||||
{
|
||||
stateValue = X_XCTD_FAST_OVER_SLOW;
|
||||
}
|
||||
else if (isFastUnderSlow)
|
||||
{
|
||||
stateValue = X_XCTD_FAST_UNDER_SLOW;
|
||||
}
|
||||
|
||||
//
|
||||
stateBuffer[bar_index] = stateValue;
|
||||
|
||||
//
|
||||
// Calculate FastPower Buffer ...
|
||||
if (ArraySize(fastPowerBuffer) <= fastLength)
|
||||
{
|
||||
fastPowerBuffer[bar_index] = 0; // fastBuffer[bar_index];
|
||||
}
|
||||
else
|
||||
{
|
||||
//
|
||||
double fastDiff = fastBuffer[bar_index] - fastBuffer[bar_index + 1];
|
||||
double fastPowerValue = fastPowerBuffer[bar_index + 1] + fastDiff;
|
||||
// //
|
||||
// if (fastBuffer[bar_index] > fastBuffer[bar_index + 1])
|
||||
// {
|
||||
// fastPowerValue += fastBuffer[bar_index];
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// fastPowerValue -= fastBuffer[bar_index];
|
||||
// }
|
||||
|
||||
//
|
||||
fastPowerBuffer[bar_index] = fastPowerValue;
|
||||
}
|
||||
|
||||
//
|
||||
// Calculate SlowPower Buffer ...
|
||||
if (ArraySize(slowPowerBuffer) <= slowLength)
|
||||
{
|
||||
slowPowerBuffer[bar_index] = 0; // slowBuffer[bar_index];
|
||||
}
|
||||
else
|
||||
{
|
||||
//
|
||||
double slowDiff = slowBuffer[bar_index] - slowBuffer[bar_index + 1];
|
||||
double slowPowerValue = slowPowerBuffer[bar_index + 1] + slowDiff;
|
||||
|
||||
//
|
||||
// if (slowBuffer[bar_index] > slowBuffer[bar_index + 1])
|
||||
// {
|
||||
// slowPowerValue += slowBuffer[bar_index];
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// slowPowerValue -= slowBuffer[bar_index];
|
||||
// }
|
||||
|
||||
//
|
||||
slowPowerBuffer[bar_index] = slowPowerValue;
|
||||
}
|
||||
}
|
||||
//
|
||||
// END Functions ...
|
||||
//
|
||||
@@ -0,0 +1,443 @@
|
||||
/////////////////////////////////////////////////////////////
|
||||
//
|
||||
// SaherElm IT Center MQL5 Cycle Trend Detector Indicator
|
||||
// ---------------------------------------------------------
|
||||
// Name: XCTD
|
||||
// Description: trend detecting based on cycles ...
|
||||
//
|
||||
//
|
||||
// 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 XCTD Indicator"
|
||||
#property strict
|
||||
|
||||
//
|
||||
// START Constants ...
|
||||
//
|
||||
#define ShortName "XCTD"
|
||||
|
||||
//
|
||||
// Including Models Library ...
|
||||
#include "..\Libraries\x-saherelm.models.lib.mq5"
|
||||
//
|
||||
// END Constants ...
|
||||
//
|
||||
|
||||
//
|
||||
// START Inputs ...
|
||||
//
|
||||
//
|
||||
input int fastLength = 9; // Fast Length
|
||||
input int slowLength = 18; // Slow Length
|
||||
|
||||
//
|
||||
input ENUM_MA_METHOD method = MODE_EMA; // Calculation Method
|
||||
input ENUM_APPLIED_PRICE appliedTo = PRICE_CLOSE; // Applied To
|
||||
//
|
||||
// END Inputs ...
|
||||
//
|
||||
|
||||
//
|
||||
// Includes Logging Library ...
|
||||
#include "../Libraries/x-saherelm.log.lib.mq5";
|
||||
|
||||
//
|
||||
// Includes Common Library ...
|
||||
#include "../Libraries/x-saherelm.common.lib.mq5";
|
||||
|
||||
//
|
||||
// START Buffers ...
|
||||
//
|
||||
#property indicator_separate_window
|
||||
|
||||
//
|
||||
#property indicator_buffers 4
|
||||
#property indicator_plots 2
|
||||
|
||||
//
|
||||
#define fastBufferIndex 0
|
||||
#define slowBufferIndex 1
|
||||
#define stateBufferIndex 2
|
||||
#define maBufferIndex 3
|
||||
|
||||
//
|
||||
double fastBuffer[];
|
||||
double slowBuffer[];
|
||||
double stateBuffer[];
|
||||
double maBuffer[];
|
||||
|
||||
//
|
||||
#property indicator_label1 "XCTD Fast"
|
||||
#property indicator_type1 DRAW_LINE
|
||||
#property indicator_color1 clrAqua
|
||||
#property indicator_style1 STYLE_DOT
|
||||
#property indicator_width1 1
|
||||
|
||||
#property indicator_label2 "XCTD Slow"
|
||||
#property indicator_type2 DRAW_LINE
|
||||
#property indicator_color2 clrFuchsia
|
||||
#property indicator_style2 STYLE_DOT
|
||||
#property indicator_width2 1
|
||||
//
|
||||
// END Buffers ...
|
||||
//
|
||||
|
||||
//
|
||||
// START Global Definitions: Variables, Properties and etc ...
|
||||
//
|
||||
//
|
||||
ENUM_TIMEFRAMES cyclePeriod;
|
||||
int maHandler = INVALID_HANDLE;
|
||||
int fastHandler = INVALID_HANDLE;
|
||||
int slowHandler = INVALID_HANDLE;
|
||||
//
|
||||
// END Global Definitions: Variables, Properties and etc ...
|
||||
//
|
||||
|
||||
//
|
||||
// START Event Handlers ...
|
||||
//
|
||||
//
|
||||
// Initialization ...
|
||||
int OnInit()
|
||||
{
|
||||
//
|
||||
// Validate Inputs ...
|
||||
if (!ValidateInputs())
|
||||
{
|
||||
return INIT_PARAMETERS_INCORRECT;
|
||||
}
|
||||
|
||||
//
|
||||
logTag = ShortName;
|
||||
|
||||
//
|
||||
// Define Index Buffers ...
|
||||
DefineBuffers();
|
||||
|
||||
//
|
||||
// Set Indicator ShortName ...
|
||||
SetIndicatorName();
|
||||
|
||||
//
|
||||
// Resetting Last Error ...
|
||||
ResetLastError();
|
||||
|
||||
//
|
||||
// Initialized Requirements ...
|
||||
cyclePeriod = GetFarestTimeFrame();
|
||||
|
||||
//
|
||||
// Ma Buffer ...
|
||||
maHandler = iMA(
|
||||
_Symbol,
|
||||
_Period,
|
||||
14,
|
||||
0,
|
||||
MODE_SMA,
|
||||
PRICE_CLOSE);
|
||||
if (maHandler == INVALID_HANDLE)
|
||||
{
|
||||
//
|
||||
LogMessage("Error in Ma Indicator initialization: " + (string)GetLastError());
|
||||
|
||||
//
|
||||
return INIT_FAILED;
|
||||
}
|
||||
|
||||
//
|
||||
// fatst Handler ...
|
||||
fastHandler = iMA(
|
||||
_Symbol,
|
||||
cyclePeriod,
|
||||
fastLength,
|
||||
0,
|
||||
method,
|
||||
appliedTo);
|
||||
if (fastHandler == INVALID_HANDLE)
|
||||
{
|
||||
//
|
||||
LogMessage("Error in Fast Indicator initialization: " + (string)GetLastError());
|
||||
|
||||
//
|
||||
return INIT_FAILED;
|
||||
}
|
||||
|
||||
//
|
||||
// slow Handler ...
|
||||
slowHandler = iMA(
|
||||
_Symbol,
|
||||
cyclePeriod,
|
||||
slowLength,
|
||||
0,
|
||||
method,
|
||||
appliedTo);
|
||||
if (slowHandler == INVALID_HANDLE)
|
||||
{
|
||||
//
|
||||
LogMessage("Error in Slow Indicator initialization: " + (string)GetLastError());
|
||||
|
||||
//
|
||||
return INIT_FAILED;
|
||||
}
|
||||
|
||||
//
|
||||
Comment(ShortName + " Cycle Period: " + EnumToString(cyclePeriod));
|
||||
|
||||
//
|
||||
// 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
|
||||
|
||||
//
|
||||
// Here we can handle De Initialization Reasons ...
|
||||
|
||||
//
|
||||
// ReDraw Chart ...
|
||||
ChartRedraw();
|
||||
|
||||
//
|
||||
Comment("");
|
||||
}
|
||||
|
||||
//
|
||||
// Calculations ...
|
||||
//
|
||||
// Calculating what we want ...
|
||||
int OnCalculate(
|
||||
//
|
||||
// total Candles on chart ...
|
||||
const int rates_total,
|
||||
//
|
||||
// total calculated Candles on charts ...
|
||||
const int prev_calculated,
|
||||
//
|
||||
// history of Candles Open Time ...
|
||||
const datetime &time[],
|
||||
//
|
||||
// history of Candles Open Price ...
|
||||
const double &open[],
|
||||
//
|
||||
// history of Candles High Price ...
|
||||
const double &high[],
|
||||
//
|
||||
// history of Candles Low Price ...
|
||||
const double &low[],
|
||||
//
|
||||
// history of Candles Close Price ...
|
||||
const double &close[],
|
||||
//
|
||||
// history of Tick Volumes on Candle ...
|
||||
const long &tick_volume[],
|
||||
//
|
||||
// history of Trade Volumes ...
|
||||
const long &volume[],
|
||||
//
|
||||
// history of Candles Spread Price ...
|
||||
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);
|
||||
|
||||
//
|
||||
// this counts Available Bars ...
|
||||
int limit;
|
||||
|
||||
//
|
||||
// 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 ...
|
||||
int maxLength = MathMax(fastLength, slowLength);
|
||||
maxLength = MathMax(maxLength, 14);
|
||||
|
||||
//
|
||||
// Get Retrieved Values from indicators ...
|
||||
int maCalculatedBars = BarsCalculated(maHandler);
|
||||
int fastCalculatedBars = BarsCalculated(fastHandler);
|
||||
int slowCalculatedBars = BarsCalculated(slowHandler);
|
||||
|
||||
//
|
||||
// Check Calculated Bars ...
|
||||
if (
|
||||
maCalculatedBars < maxLength ||
|
||||
fastCalculatedBars < maxLength ||
|
||||
slowCalculatedBars < maxLength)
|
||||
{
|
||||
return prev_calculated;
|
||||
}
|
||||
|
||||
//
|
||||
limit = prev_calculated < maxLength ? rates_total - (maxLength + 1) : rates_total - prev_calculated;
|
||||
|
||||
//
|
||||
// Copy Buffers from indicators ...
|
||||
int maCopiedItems = CopyBuffer(maHandler, 0, 0, limit + 1, maBuffer);
|
||||
int fastCopiedItems = CopyBuffer(fastHandler, 0, 0, limit + 1, fastBuffer);
|
||||
int slowCopiedItems = CopyBuffer(slowHandler, 0, 0, limit + 1, slowBuffer);
|
||||
|
||||
//
|
||||
// Check Copied Items Number ...
|
||||
if (
|
||||
maCopiedItems <= 0 ||
|
||||
fastCopiedItems <= 0 ||
|
||||
slowCopiedItems <= 0)
|
||||
{
|
||||
return prev_calculated;
|
||||
}
|
||||
|
||||
//
|
||||
// Main Loop ...
|
||||
for (int i = limit; i >= 0 && !IsStopped(); i--)
|
||||
{
|
||||
//
|
||||
CalculateBuffers(i);
|
||||
}
|
||||
|
||||
//
|
||||
return rates_total;
|
||||
}
|
||||
//
|
||||
// END Event Handlers ...
|
||||
//
|
||||
|
||||
//
|
||||
// START Functions ...
|
||||
//
|
||||
//
|
||||
// Validate Input Args for Initialization ...
|
||||
bool ValidateInputs()
|
||||
{
|
||||
//
|
||||
bool result = false;
|
||||
|
||||
//
|
||||
// Validate Args ...
|
||||
if (fastLength >= 4 && slowLength > fastLength)
|
||||
{
|
||||
result = true;
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
// Define Indexes and Styles ...
|
||||
void DefineBuffers()
|
||||
{
|
||||
//
|
||||
string fastBufferLabel = ShortName + " Fast (" + (string)fastLength + ")";
|
||||
string slowBufferLabel = ShortName + " Slow (" + (string)slowLength + ")";
|
||||
|
||||
//
|
||||
// MA Buffer ...
|
||||
ArraySetAsSeries(maBuffer, true);
|
||||
SetIndexBuffer(maBufferIndex, maBuffer, INDICATOR_CALCULATIONS);
|
||||
|
||||
//
|
||||
// Fast Buffer ...
|
||||
ArraySetAsSeries(fastBuffer, true);
|
||||
SetIndexBuffer(fastBufferIndex, fastBuffer, INDICATOR_DATA);
|
||||
PlotIndexSetInteger(fastBufferIndex, PLOT_DRAW_BEGIN, fastLength);
|
||||
PlotIndexSetString(fastBufferIndex, PLOT_LABEL, fastBufferLabel);
|
||||
|
||||
//
|
||||
// Slow Buffer ...
|
||||
ArraySetAsSeries(slowBuffer, true);
|
||||
SetIndexBuffer(slowBufferIndex, slowBuffer, INDICATOR_DATA);
|
||||
PlotIndexSetInteger(slowBufferIndex, PLOT_DRAW_BEGIN, slowLength);
|
||||
PlotIndexSetString(slowBufferIndex, PLOT_LABEL, slowBufferLabel);
|
||||
|
||||
//
|
||||
// State Buffer ...
|
||||
ArraySetAsSeries(stateBuffer, true);
|
||||
SetIndexBuffer(stateBufferIndex, stateBuffer, INDICATOR_CALCULATIONS);
|
||||
}
|
||||
|
||||
//
|
||||
// Set Indicator Short Name and also we can define Buffers Labels ...
|
||||
void SetIndicatorName()
|
||||
{
|
||||
//
|
||||
string indicatorShortName = "";
|
||||
StringConcatenate(indicatorShortName, ShortName, "");
|
||||
|
||||
//
|
||||
IndicatorSetString(INDICATOR_SHORTNAME, indicatorShortName);
|
||||
}
|
||||
|
||||
//
|
||||
// Calculate Buffers ...
|
||||
void CalculateBuffers(int bar_index)
|
||||
{
|
||||
//
|
||||
// here we must calculate required Buffers ...
|
||||
// Ma, Fast and Slow buffers filled using CopyBuffer ...
|
||||
// StateBuffer needs to Calculate ...
|
||||
|
||||
//
|
||||
// Retrieve Different State ...
|
||||
bool isFastCrossedOverSlow = IsCrossOver(fastBuffer, slowBuffer, bar_index);
|
||||
bool isFastOverSlow = IsOver(fastBuffer, slowBuffer, bar_index);
|
||||
bool isFastCrossedUnderSlow = IsCrossUnder(fastBuffer, slowBuffer, bar_index);
|
||||
bool isFastUnderSlow = IsUnder(fastBuffer, slowBuffer, bar_index);
|
||||
|
||||
//
|
||||
// Calculate State Buffer ...
|
||||
double stateValue = X_XCTD_NEUTURAL;
|
||||
if (isFastCrossedOverSlow)
|
||||
{
|
||||
stateValue = X_XCTD_FAST_CROSSED_OVER_SLOW;
|
||||
}
|
||||
else if (isFastCrossedUnderSlow)
|
||||
{
|
||||
stateValue = X_XCTD_FAST_CROSSED_UNDER_SLOW;
|
||||
}
|
||||
else if (isFastOverSlow)
|
||||
{
|
||||
stateValue = X_XCTD_FAST_OVER_SLOW;
|
||||
}
|
||||
else if (isFastUnderSlow)
|
||||
{
|
||||
stateValue = X_XCTD_FAST_UNDER_SLOW;
|
||||
}
|
||||
|
||||
//
|
||||
stateBuffer[bar_index] = stateValue;
|
||||
}
|
||||
//
|
||||
// END Functions ...
|
||||
//
|
||||
@@ -0,0 +1,276 @@
|
||||
///////////////////////////////////////////////////////
|
||||
//
|
||||
// SaherElm IT Center MQL5 XMA Candle Indicator
|
||||
// ---------------------------------------------
|
||||
// Name: XMA
|
||||
// Description: Moving Average 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 XMA Indicator"
|
||||
#property strict
|
||||
|
||||
//
|
||||
// START Constants ...
|
||||
//
|
||||
#define ShortName "XMA"
|
||||
//
|
||||
// END Constants ...
|
||||
//
|
||||
|
||||
//
|
||||
// START Inputs ...
|
||||
//
|
||||
input int maPeriod = 14; // Period
|
||||
input int maShift = 0; // Shift
|
||||
input ENUM_MA_METHOD maMethod = MODE_SMA; // Method
|
||||
input ENUM_APPLIED_PRICE maAppliedTo = PRICE_CLOSE; // Applied To
|
||||
//
|
||||
// END Inputs ...
|
||||
//
|
||||
|
||||
//
|
||||
// Includes Logging Library ...
|
||||
#include "../Libraries/x-saherelm.log.lib.mq5";
|
||||
|
||||
//
|
||||
// START Buffers ...
|
||||
//
|
||||
#property indicator_chart_window
|
||||
// #property indicator_separate_window
|
||||
|
||||
//
|
||||
#property indicator_buffers 1
|
||||
#property indicator_plots 1
|
||||
|
||||
//
|
||||
// maBuffer ...
|
||||
#define maBufferIndex 0
|
||||
|
||||
double maBuffer[];
|
||||
|
||||
#property indicator_label1 "XMA"
|
||||
#property indicator_type1 DRAW_LINE
|
||||
#property indicator_color1 clrBlue
|
||||
#property indicator_style1 STYLE_SOLID
|
||||
#property indicator_width1 1
|
||||
//
|
||||
// END Buffers ...
|
||||
//
|
||||
|
||||
//
|
||||
// START Global Definitions: Variables, Properties and etc ...
|
||||
//
|
||||
int maHandler = INVALID_HANDLE;
|
||||
//
|
||||
// END Global Definitions: Variables, Properties and etc ...
|
||||
//
|
||||
|
||||
//
|
||||
// START Event Handlers ...
|
||||
//
|
||||
//
|
||||
// Initialization ...
|
||||
int OnInit()
|
||||
{
|
||||
//
|
||||
// Validate Inputs ...
|
||||
if (!ValidateInputs())
|
||||
{
|
||||
return INIT_PARAMETERS_INCORRECT;
|
||||
}
|
||||
|
||||
//
|
||||
logTag = ShortName;
|
||||
|
||||
//
|
||||
// Initializing MA Handler ...
|
||||
maHandler = iMA(
|
||||
_Symbol,
|
||||
_Period,
|
||||
maPeriod,
|
||||
maShift,
|
||||
maMethod,
|
||||
maAppliedTo);
|
||||
if (maHandler == INVALID_HANDLE)
|
||||
{
|
||||
//
|
||||
string message = "Error Initializing Ma Handler ...";
|
||||
LogMessage(message);
|
||||
|
||||
//
|
||||
return INIT_FAILED;
|
||||
}
|
||||
|
||||
//
|
||||
// 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
|
||||
|
||||
//
|
||||
// Here we can handle De Initialization Reasons ...
|
||||
}
|
||||
|
||||
//
|
||||
// Calculations ...
|
||||
//
|
||||
// Calculating what we want ...
|
||||
int OnCalculate(
|
||||
//
|
||||
// total Candles on chart ...
|
||||
const int rates_total,
|
||||
//
|
||||
// total calculated Candles on charts ...
|
||||
const int prev_calculated,
|
||||
//
|
||||
// history of Candles Open Time ...
|
||||
const datetime &time[],
|
||||
//
|
||||
// history of Candles Open Price ...
|
||||
const double &open[],
|
||||
//
|
||||
// history of Candles High Price ...
|
||||
const double &high[],
|
||||
//
|
||||
// history of Candles Low Price ...
|
||||
const double &low[],
|
||||
//
|
||||
// history of Candles Close Price ...
|
||||
const double &close[],
|
||||
//
|
||||
// history of Tick Volumes on Candle ...
|
||||
const long &tick_volume[],
|
||||
//
|
||||
// history of Trade Volumes ...
|
||||
const long &volume[],
|
||||
//
|
||||
// history of Candles Spread Price ...
|
||||
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);
|
||||
|
||||
//
|
||||
// this counts Available Bars ...
|
||||
int limit;
|
||||
|
||||
//
|
||||
// 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 ...
|
||||
int maxLength = MathMax(0, maPeriod);
|
||||
|
||||
//
|
||||
// Check Calculated Bars ...
|
||||
if (BarsCalculated(maHandler) < rates_total)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
//
|
||||
limit = (prev_calculated > rates_total || prev_calculated <= 0) ? rates_total : (rates_total - prev_calculated) + 1;
|
||||
|
||||
//
|
||||
// Buffers Copy ...
|
||||
if (CopyBuffer(maHandler, 0, 0, limit, maBuffer) <= 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
//
|
||||
return rates_total;
|
||||
}
|
||||
//
|
||||
// END Event Handlers ...
|
||||
//
|
||||
|
||||
//
|
||||
// START Functions ...
|
||||
//
|
||||
//
|
||||
// Validate Input Args for Initialization ...
|
||||
bool ValidateInputs()
|
||||
{
|
||||
//
|
||||
bool result = false;
|
||||
|
||||
//
|
||||
// Validate Args ...
|
||||
if (maPeriod >= 2)
|
||||
{
|
||||
result = true;
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
// Define Indexes and Styles ...
|
||||
void DefineBuffers()
|
||||
{
|
||||
//
|
||||
string maBufferLabel = "MA " + "(" + (string) maPeriod + ")";
|
||||
|
||||
//
|
||||
// Ma Buffer ...
|
||||
ArraySetAsSeries(maBuffer, true);
|
||||
SetIndexBuffer(maBufferIndex, maBuffer, INDICATOR_DATA);
|
||||
PlotIndexSetString(maBufferIndex, PLOT_LABEL, maBufferLabel);
|
||||
}
|
||||
|
||||
//
|
||||
// Set Indicator Short Name and also we can define Buffers Labels ...
|
||||
void SetIndicatorName()
|
||||
{
|
||||
//
|
||||
string indicatorShortName = "";
|
||||
StringConcatenate(indicatorShortName, ShortName, " ", "(", maPeriod, ")");
|
||||
|
||||
//
|
||||
IndicatorSetString(INDICATOR_SHORTNAME, indicatorShortName);
|
||||
}
|
||||
//
|
||||
// END Functions ...
|
||||
//
|
||||
@@ -0,0 +1,358 @@
|
||||
/////////////////////////////////////////////////////////
|
||||
//
|
||||
// SaherElm IT Center MQL5 OrderBlock Detector Indicator
|
||||
// -----------------------------------------------------
|
||||
// Name: XOBD
|
||||
// Description: detect Order Blocks based on Swings ...
|
||||
//
|
||||
//
|
||||
// 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 XOBD Indicator"
|
||||
#property strict
|
||||
|
||||
//
|
||||
// START Constants ...
|
||||
//
|
||||
#define ShortName "XOBD"
|
||||
//
|
||||
// END Constants ...
|
||||
//
|
||||
|
||||
//
|
||||
// START Inputs ...
|
||||
//
|
||||
//
|
||||
input int length = 5; // Market Length
|
||||
|
||||
//
|
||||
input double arrowDistanceFromPrice = 200; // Arrow Distrance from Price
|
||||
|
||||
//
|
||||
input uchar swingHighArrowCode = 108; // Swing High Arrow Code
|
||||
input color swingHighArrowColor = clrMagenta; // Swing High Arrow Color
|
||||
|
||||
//
|
||||
input uchar swingLowArrowCode = 108; // Swing Low Arrow Code
|
||||
input color swingLowArrowColor = clrAqua; // Swing Low Arrow Color
|
||||
//
|
||||
// END Inputs ...
|
||||
//
|
||||
|
||||
//
|
||||
// Includes Logging Library ...
|
||||
#include "../Libraries/x-saherelm.log.lib.mq5";
|
||||
|
||||
//
|
||||
// Include Common and Models Library ...
|
||||
#include "../Libraries/x-saherelm.common.lib.mq5";
|
||||
|
||||
//
|
||||
// Include Draw Library ...
|
||||
#include "../Libraries/x-saherelm.draw.lib.mq5";
|
||||
|
||||
//
|
||||
// START Buffers ...
|
||||
//
|
||||
#property indicator_chart_window
|
||||
|
||||
//
|
||||
#property indicator_buffers 1
|
||||
#property indicator_plots 0
|
||||
|
||||
//
|
||||
#define swingBufferIndex 0
|
||||
double swingBuffer[];
|
||||
//
|
||||
// END Buffers ...
|
||||
//
|
||||
|
||||
//
|
||||
// START Global Definitions: Variables, Properties and etc ...
|
||||
//
|
||||
//
|
||||
// END Global Definitions: Variables, Properties and etc ...
|
||||
//
|
||||
|
||||
//
|
||||
// START Event Handlers ...
|
||||
//
|
||||
//
|
||||
// Initialization ...
|
||||
int OnInit()
|
||||
{
|
||||
//
|
||||
// Validate Inputs ...
|
||||
if (!ValidateInputs())
|
||||
{
|
||||
return INIT_PARAMETERS_INCORRECT;
|
||||
}
|
||||
|
||||
//
|
||||
logTag = ShortName;
|
||||
drawPrefix = ShortName;
|
||||
|
||||
//
|
||||
// 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
|
||||
|
||||
//
|
||||
// Here we can handle De Initialization Reasons ...
|
||||
OnDeinitDrawLibrary();
|
||||
}
|
||||
|
||||
//
|
||||
// Calculations ...
|
||||
//
|
||||
// Calculating what we want ...
|
||||
int OnCalculate(
|
||||
//
|
||||
// total Candles on chart ...
|
||||
const int rates_total,
|
||||
//
|
||||
// total calculated Candles on charts ...
|
||||
const int prev_calculated,
|
||||
//
|
||||
// history of Candles Open Time ...
|
||||
const datetime &time[],
|
||||
//
|
||||
// history of Candles Open Price ...
|
||||
const double &open[],
|
||||
//
|
||||
// history of Candles High Price ...
|
||||
const double &high[],
|
||||
//
|
||||
// history of Candles Low Price ...
|
||||
const double &low[],
|
||||
//
|
||||
// history of Candles Close Price ...
|
||||
const double &close[],
|
||||
//
|
||||
// history of Tick Volumes on Candle ...
|
||||
const long &tick_volume[],
|
||||
//
|
||||
// history of Trade Volumes ...
|
||||
const long &volume[],
|
||||
//
|
||||
// history of Candles Spread Price ...
|
||||
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);
|
||||
|
||||
//
|
||||
// this counts Available Bars ...
|
||||
int limit;
|
||||
|
||||
//
|
||||
// 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 ...
|
||||
int maxLength = MathMax(0, length);
|
||||
|
||||
//
|
||||
limit = prev_calculated < maxLength ? rates_total - (maxLength + 1) : rates_total - prev_calculated;
|
||||
|
||||
//
|
||||
// Main Loop ...
|
||||
for (int i = limit; i >= 0 && !IsStopped(); i--)
|
||||
{
|
||||
//
|
||||
CalculateBuffers(i);
|
||||
}
|
||||
|
||||
//
|
||||
return rates_total;
|
||||
}
|
||||
//
|
||||
// END Event Handlers ...
|
||||
//
|
||||
|
||||
//
|
||||
// START Functions ...
|
||||
//
|
||||
//
|
||||
// Validate Input Args for Initialization ...
|
||||
bool ValidateInputs()
|
||||
{
|
||||
//
|
||||
bool result = false;
|
||||
|
||||
//
|
||||
// Validate Args ...
|
||||
if (length >= 2)
|
||||
{
|
||||
result = true;
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
// Define Indexes and Styles ...
|
||||
void DefineBuffers()
|
||||
{
|
||||
//
|
||||
ArraySetAsSeries(swingBuffer, true);
|
||||
SetIndexBuffer(swingBufferIndex, swingBuffer, INDICATOR_CALCULATIONS);
|
||||
}
|
||||
|
||||
//
|
||||
// Set Indicator Short Name and also we can define Buffers Labels ...
|
||||
void SetIndicatorName()
|
||||
{
|
||||
//
|
||||
string indicatorShortName = "";
|
||||
StringConcatenate(indicatorShortName, ShortName, " ", "( ", length, " )");
|
||||
|
||||
//
|
||||
IndicatorSetString(INDICATOR_SHORTNAME, indicatorShortName);
|
||||
}
|
||||
|
||||
//
|
||||
// Calculate Buffers ...
|
||||
void CalculateBuffers(int bar_index)
|
||||
{
|
||||
//
|
||||
// Check Loopback for Swings ...
|
||||
bool hasLoopbackSwingHigh = false;
|
||||
bool hasLoopbackSwingLow = false;
|
||||
for (int i = bar_index + 1; i < bar_index + length + 1; i++)
|
||||
{
|
||||
//
|
||||
ENUM_X_SWING_TYPE type = GetCandleSwing(i);
|
||||
if (type == X_SWING_HIGH)
|
||||
{
|
||||
hasLoopbackSwingHigh = true;
|
||||
}
|
||||
else if (type == X_SWING_LOW)
|
||||
{
|
||||
hasLoopbackSwingLow = true;
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
ENUM_X_SWING_TYPE type = GetCandleSwing(bar_index);
|
||||
switch (type)
|
||||
{
|
||||
//
|
||||
// Swing High ...
|
||||
case X_SWING_HIGH:
|
||||
//
|
||||
if (!hasLoopbackSwingHigh)
|
||||
{
|
||||
//
|
||||
swingBuffer[bar_index] = 1;
|
||||
DrawSwingArrow(1, bar_index);
|
||||
}
|
||||
else
|
||||
{
|
||||
swingBuffer[bar_index] = 0;
|
||||
}
|
||||
break;
|
||||
|
||||
//
|
||||
// Swinmg Low ...
|
||||
case X_SWING_LOW:
|
||||
//
|
||||
if (!hasLoopbackSwingLow)
|
||||
{
|
||||
//
|
||||
swingBuffer[bar_index] = -1;
|
||||
DrawSwingArrow(0, bar_index);
|
||||
}
|
||||
else
|
||||
{
|
||||
swingBuffer[bar_index] = 0;
|
||||
}
|
||||
break;
|
||||
|
||||
//
|
||||
// No Swing ...
|
||||
default:
|
||||
case X_NO_SWING:
|
||||
//
|
||||
swingBuffer[bar_index] = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// Draw a Swing Arrow Shape ...
|
||||
void DrawSwingArrow(
|
||||
int type, // Swing Type: 0 - Low / 1 - High ...
|
||||
int bar_index)
|
||||
{
|
||||
//
|
||||
bool isSwingHigh = type == 1;
|
||||
XOHCL candle = GetCandle(bar_index + 3);
|
||||
double arrowDistanceInPoint = arrowDistanceFromPrice * _Point;
|
||||
|
||||
//
|
||||
// Define Required Object Properties fro Draw an Arrow ...
|
||||
datetime time = iTime(_Symbol, _Period, bar_index + 3);
|
||||
double purePrice = isSwingHigh ? candle.high : candle.low;
|
||||
color clr = isSwingHigh ? swingHighArrowColor : swingLowArrowColor;
|
||||
ENUM_ARROW_ANCHOR anchor = isSwingHigh ? ANCHOR_BOTTOM : ANCHOR_TOP;
|
||||
uchar arrowCode = uchar(isSwingHigh ? swingHighArrowCode : swingLowArrowCode);
|
||||
double price = isSwingHigh ? purePrice + arrowDistanceInPoint : purePrice - arrowDistanceInPoint;
|
||||
string name = "Swing " + (isSwingHigh ? "High" : "Low") + "_" + (string)time + "_" + (string)purePrice;
|
||||
|
||||
//
|
||||
// Draw desired Arrow ...
|
||||
DrawArrow(
|
||||
0,
|
||||
name,
|
||||
0,
|
||||
time,
|
||||
price,
|
||||
arrowCode,
|
||||
anchor,
|
||||
clr);
|
||||
}
|
||||
//
|
||||
// END Functions ...
|
||||
//
|
||||
Binary file not shown.
@@ -0,0 +1,246 @@
|
||||
//////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// SaherElm IT Center MQL5 Price Action Candle Indicator/Oscillator
|
||||
// ----------------------------------------------------------------
|
||||
// Name: XPRC
|
||||
// Description: find down and up candlestick patterns
|
||||
// based on pure price action ...
|
||||
//
|
||||
//
|
||||
// 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 XPRC Indicator"
|
||||
#property strict
|
||||
|
||||
//
|
||||
// START Constants ...
|
||||
//
|
||||
#define ShortName "XPRC"
|
||||
//
|
||||
// END Constants ...
|
||||
//
|
||||
|
||||
//
|
||||
// START Inputs ...
|
||||
//
|
||||
//
|
||||
// END Inputs ...
|
||||
//
|
||||
|
||||
//
|
||||
// START Buffers ...
|
||||
//
|
||||
#property indicator_chart_window
|
||||
|
||||
//
|
||||
#property indicator_buffers 1
|
||||
#property indicator_plots 1
|
||||
|
||||
//
|
||||
// mBuffer ...
|
||||
#define mBufferIndex 0
|
||||
|
||||
double mBuffer[];
|
||||
|
||||
#property indicator_label1 "M"
|
||||
#property indicator_type1 DRAW_LINE
|
||||
#property indicator_color1 clrYellow
|
||||
#property indicator_style1 STYLE_DOT
|
||||
#property indicator_width1 2
|
||||
//
|
||||
// END Buffers ...
|
||||
//
|
||||
|
||||
//
|
||||
// START Global Definitions: Variables, Properties and etc ...
|
||||
//
|
||||
//
|
||||
// END Global Definitions: Variables, Properties and etc ...
|
||||
//
|
||||
|
||||
//
|
||||
// START Event Handlers ...
|
||||
//
|
||||
//
|
||||
// Initialization ...
|
||||
int OnInit()
|
||||
{
|
||||
//
|
||||
// Validate Inputs ...
|
||||
if (!ValidateInputs())
|
||||
{
|
||||
return INIT_PARAMETERS_INCORRECT;
|
||||
}
|
||||
|
||||
//
|
||||
// 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
|
||||
|
||||
//
|
||||
// Here we can handle De Initialization Reasons ...
|
||||
}
|
||||
|
||||
//
|
||||
// Calculations ...
|
||||
//
|
||||
// Calculating what we want ...
|
||||
int OnCalculate(
|
||||
//
|
||||
// total Candles on chart ...
|
||||
const int rates_total,
|
||||
//
|
||||
// total calculated Candles on charts ...
|
||||
const int prev_calculated,
|
||||
//
|
||||
// history of Candles Open Time ...
|
||||
const datetime &time[],
|
||||
//
|
||||
// history of Candles Open Price ...
|
||||
const double &open[],
|
||||
//
|
||||
// history of Candles High Price ...
|
||||
const double &high[],
|
||||
//
|
||||
// history of Candles Low Price ...
|
||||
const double &low[],
|
||||
//
|
||||
// history of Candles Close Price ...
|
||||
const double &close[],
|
||||
//
|
||||
// history of Tick Volumes on Candle ...
|
||||
const long &tick_volume[],
|
||||
//
|
||||
// history of Trade Volumes ...
|
||||
const long &volume[],
|
||||
//
|
||||
// history of Candles Spread Price ...
|
||||
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);
|
||||
|
||||
//
|
||||
// this counts Available Bars ...
|
||||
int limit;
|
||||
|
||||
//
|
||||
// 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 ...
|
||||
int maxLength = MathMax(0, 0);
|
||||
|
||||
//
|
||||
// input variable, we return 0 means nothing passed ...
|
||||
if (rates_total < maxLength)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
//
|
||||
// found which candles calculated before ...
|
||||
limit = (prev_calculated == 0) ? rates_total - maxLength - 1 : rates_total - prev_calculated + 1;
|
||||
|
||||
//
|
||||
// this is the main loop of calculations, for each bar index ...
|
||||
for (int i = limit - 1; i >= 0; i--)
|
||||
{
|
||||
//
|
||||
// Start Calculation here ...
|
||||
// i is bar_index ...
|
||||
|
||||
//
|
||||
// Calculate mBuffer ...
|
||||
CalculateMBuffer(i);
|
||||
}
|
||||
|
||||
//
|
||||
return rates_total;
|
||||
}
|
||||
//
|
||||
// END Event Handlers ...
|
||||
//
|
||||
|
||||
//
|
||||
// START Functions ...
|
||||
//
|
||||
//
|
||||
// Validate Input Args for Initialization ...
|
||||
bool ValidateInputs()
|
||||
{
|
||||
//
|
||||
bool result = false;
|
||||
|
||||
//
|
||||
// Validate Args ...
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
// Define Indexes and Styles ...
|
||||
void DefineBuffers()
|
||||
{
|
||||
//
|
||||
// BUFFER_NAME ...
|
||||
ArraySetAsSeries(mBuffer, true);
|
||||
SetIndexBuffer(mBufferIndex, mBuffer);
|
||||
}
|
||||
|
||||
//
|
||||
// Set Indicator Short Name and also we can define Buffers Labels ...
|
||||
void SetIndicatorName()
|
||||
{
|
||||
string indicatorShortName = "";
|
||||
StringConcatenate(indicatorShortName, ShortName, "");
|
||||
IndicatorSetString(INDICATOR_SHORTNAME, indicatorShortName);
|
||||
}
|
||||
|
||||
//
|
||||
// Calculate mBuffer ...
|
||||
void CalculateMBuffer(int bar_index) {}
|
||||
//
|
||||
// END Functions ...
|
||||
//
|
||||
@@ -0,0 +1,324 @@
|
||||
///////////////////////////////////////////////////////
|
||||
//
|
||||
// SaherElm IT Center MQL5 XRMA Indicator
|
||||
// ---------------------------------------------
|
||||
// Name: XRMA
|
||||
// Description: Relative Moving Average 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 XRMA Indicator"
|
||||
#property strict
|
||||
|
||||
//
|
||||
// START Constants ...
|
||||
//
|
||||
#define ShortName "XRMA"
|
||||
//
|
||||
// END Constants ...
|
||||
//
|
||||
|
||||
//
|
||||
// START Inputs ...
|
||||
//
|
||||
input int length = 14; // Length
|
||||
input ENUM_APPLIED_PRICE appliedTo = PRICE_CLOSE; // Applied To
|
||||
//
|
||||
// END Inputs ...
|
||||
//
|
||||
|
||||
//
|
||||
// Includes Logging Library ...
|
||||
#include "../Libraries/x-saherelm.log.lib.mq5";
|
||||
|
||||
//
|
||||
// Includes Common Library ...
|
||||
#include "../Libraries/x-saherelm.common.lib.mq5";
|
||||
|
||||
//
|
||||
// START Buffers ...
|
||||
//
|
||||
#property indicator_chart_window
|
||||
|
||||
//
|
||||
#property indicator_buffers 1
|
||||
#property indicator_plots 1
|
||||
|
||||
//
|
||||
// maBuffer ...
|
||||
#define rmaBufferIndex 0
|
||||
#define maBufferIndex 1
|
||||
|
||||
double rmaBuffer[];
|
||||
double maBuffer[];
|
||||
|
||||
#property indicator_label1 "XRMA"
|
||||
#property indicator_type1 DRAW_LINE
|
||||
#property indicator_color1 clrYellow
|
||||
#property indicator_style1 STYLE_SOLID
|
||||
#property indicator_width1 1
|
||||
//
|
||||
// END Buffers ...
|
||||
//
|
||||
|
||||
//
|
||||
// START Global Definitions: Variables, Properties and etc ...
|
||||
//
|
||||
int maHandler = INVALID_HANDLE;
|
||||
//
|
||||
// END Global Definitions: Variables, Properties and etc ...
|
||||
//
|
||||
|
||||
//
|
||||
// START Event Handlers ...
|
||||
//
|
||||
//
|
||||
// Initialization ...
|
||||
int OnInit()
|
||||
{
|
||||
//
|
||||
// Validate Inputs ...
|
||||
if (!ValidateInputs())
|
||||
{
|
||||
return INIT_PARAMETERS_INCORRECT;
|
||||
}
|
||||
|
||||
//
|
||||
logTag = ShortName;
|
||||
|
||||
//
|
||||
// Initializing MA Handler ...
|
||||
maHandler = iMA(
|
||||
_Symbol,
|
||||
_Period,
|
||||
length,
|
||||
0,
|
||||
MODE_SMA,
|
||||
appliedTo);
|
||||
if (maHandler == INVALID_HANDLE)
|
||||
{
|
||||
//
|
||||
string message = "Error Initializing Ma Indicator ...";
|
||||
LogMessage(message);
|
||||
|
||||
//
|
||||
return INIT_FAILED;
|
||||
}
|
||||
|
||||
//
|
||||
// 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
|
||||
|
||||
//
|
||||
// Here we can handle De Initialization Reasons ...
|
||||
}
|
||||
|
||||
//
|
||||
// Calculations ...
|
||||
//
|
||||
// Calculating what we want ...
|
||||
int OnCalculate(
|
||||
//
|
||||
// total Candles on chart ...
|
||||
const int rates_total,
|
||||
//
|
||||
// total calculated Candles on charts ...
|
||||
const int prev_calculated,
|
||||
//
|
||||
// history of Candles Open Time ...
|
||||
const datetime &time[],
|
||||
//
|
||||
// history of Candles Open Price ...
|
||||
const double &open[],
|
||||
//
|
||||
// history of Candles High Price ...
|
||||
const double &high[],
|
||||
//
|
||||
// history of Candles Low Price ...
|
||||
const double &low[],
|
||||
//
|
||||
// history of Candles Close Price ...
|
||||
const double &close[],
|
||||
//
|
||||
// history of Tick Volumes on Candle ...
|
||||
const long &tick_volume[],
|
||||
//
|
||||
// history of Trade Volumes ...
|
||||
const long &volume[],
|
||||
//
|
||||
// history of Candles Spread Price ...
|
||||
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);
|
||||
|
||||
//
|
||||
// this counts Available Bars ...
|
||||
int limit;
|
||||
|
||||
//
|
||||
// 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 ...
|
||||
int maxLength = MathMax(0, length);
|
||||
|
||||
//
|
||||
// Check Calculated Bars ...
|
||||
if (BarsCalculated(maHandler) < rates_total)
|
||||
{
|
||||
return prev_calculated;
|
||||
}
|
||||
|
||||
//
|
||||
limit = (prev_calculated > rates_total || prev_calculated <= 0) ? rates_total : (rates_total - prev_calculated) + 1;
|
||||
|
||||
//
|
||||
// Buffers Copy ...
|
||||
if (CopyBuffer(maHandler, 0, 0, limit, maBuffer) <= 0)
|
||||
{
|
||||
return prev_calculated;
|
||||
}
|
||||
|
||||
//
|
||||
// Main Loop ...
|
||||
for (int i = limit - 1; i >= 0 && !IsStopped(); i--)
|
||||
{
|
||||
//
|
||||
CalculateBuffers(i);
|
||||
}
|
||||
|
||||
//
|
||||
return rates_total;
|
||||
}
|
||||
//
|
||||
// END Event Handlers ...
|
||||
//
|
||||
|
||||
//
|
||||
// START Functions ...
|
||||
//
|
||||
//
|
||||
// Validate Input Args for Initialization ...
|
||||
bool ValidateInputs()
|
||||
{
|
||||
//
|
||||
bool result = false;
|
||||
|
||||
//
|
||||
// Validate Args ...
|
||||
if (length >= 2)
|
||||
{
|
||||
result = true;
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
// Define Indexes and Styles ...
|
||||
void DefineBuffers()
|
||||
{
|
||||
//
|
||||
string rmaBufferLabel = "XRMA " + "(" + (string)length + ")";
|
||||
|
||||
//
|
||||
// XRMA Buffer ...
|
||||
ArraySetAsSeries(rmaBuffer, true);
|
||||
SetIndexBuffer(rmaBufferIndex, rmaBuffer, INDICATOR_DATA);
|
||||
PlotIndexSetInteger(rmaBufferIndex, PLOT_DRAW_BEGIN, length);
|
||||
PlotIndexSetString(rmaBufferIndex, PLOT_LABEL, rmaBufferLabel);
|
||||
|
||||
//
|
||||
// MA Buffer ...
|
||||
ArraySetAsSeries(maBuffer, true);
|
||||
SetIndexBuffer(maBufferIndex, maBuffer, INDICATOR_CALCULATIONS);
|
||||
}
|
||||
|
||||
//
|
||||
// Set Indicator Short Name and also we can define Buffers Labels ...
|
||||
void SetIndicatorName()
|
||||
{
|
||||
//
|
||||
string indicatorShortName = "";
|
||||
StringConcatenate(indicatorShortName, ShortName, " ", "(", length, ")");
|
||||
|
||||
//
|
||||
IndicatorSetString(INDICATOR_SHORTNAME, indicatorShortName);
|
||||
}
|
||||
|
||||
//
|
||||
// Calculate Buffers ...
|
||||
void CalculateBuffers(
|
||||
int bar_index // Selected Bar Index
|
||||
)
|
||||
{
|
||||
//
|
||||
// Calculate Alpha ...
|
||||
double alpha = 1 / length;
|
||||
|
||||
//
|
||||
// Retrieve Applied Price ...
|
||||
double price = GetAppliedPrice(bar_index, appliedTo);
|
||||
|
||||
//
|
||||
// Last RMA Value ...
|
||||
// LogMessage("rma Size: " + (string)ArraySize(rmaBuffer) + ", bar_index: " + (string)bar_index);
|
||||
double nzValue = ArraySize(rmaBuffer) == bar_index + 2 ? rmaBuffer[bar_index + 1] : 0;
|
||||
double prevRMA = nzValue > 0 ? nzValue : maBuffer[bar_index];
|
||||
|
||||
//
|
||||
// Calculate RMA Value for Bar Index ...
|
||||
double rmaValue = alpha * price + (1 - alpha) * prevRMA;
|
||||
rmaValue = NormalizeDouble(rmaValue, _Digits);
|
||||
|
||||
//
|
||||
// Fill Buffer ...
|
||||
rmaBuffer[bar_index] = rmaValue;
|
||||
}
|
||||
//
|
||||
// END Functions ...
|
||||
//
|
||||
@@ -0,0 +1,440 @@
|
||||
///////////////////////////////////////////////////////
|
||||
//
|
||||
// SaherElm IT Center MQL5 Trend Detector Oscillator
|
||||
// --------------------------------------------------
|
||||
// Name: XTD
|
||||
// Description: trend detecting in both sides ...
|
||||
//
|
||||
//
|
||||
// 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 XTD Oscillator"
|
||||
#property strict
|
||||
|
||||
//
|
||||
// START Constants ...
|
||||
//
|
||||
#define ShortName "XTD"
|
||||
//
|
||||
// END Constants ...
|
||||
//
|
||||
|
||||
//
|
||||
// Include Models Library ...
|
||||
#include "../Libraries/x-saherelm.models.lib.mq5"
|
||||
|
||||
//
|
||||
// START Inputs ...
|
||||
//
|
||||
input int length = 14; // Market Length
|
||||
input bool drawCrosses = true; // Draw Cross Arrows
|
||||
|
||||
//
|
||||
// Bullish ...
|
||||
input uchar bullishArrowCode = 228; // Cross Over Arrow Code
|
||||
input color bullishArrowColor = clrAqua; // Cross Over Arrow Color
|
||||
|
||||
//
|
||||
// Bearish ...
|
||||
input uchar bearishArrowCode = 230; // Cross Under Arrow Code
|
||||
input color bearishArrowColor = clrMagenta; // Cross Under Arrow Color
|
||||
//
|
||||
// END Inputs ...
|
||||
//
|
||||
|
||||
//
|
||||
// Includes Logging Library ...
|
||||
#include "../Libraries/x-saherelm.log.lib.mq5";
|
||||
|
||||
//
|
||||
// Includes Common Library ...
|
||||
#include "../Libraries/x-saherelm.common.lib.mq5";
|
||||
|
||||
//
|
||||
// Includes Draw Library ...
|
||||
#include "../Libraries/x-saherelm.draw.lib.mq5";
|
||||
|
||||
//
|
||||
// START Buffers ...
|
||||
//
|
||||
#property indicator_separate_window
|
||||
|
||||
//
|
||||
#property indicator_buffers 3
|
||||
#property indicator_plots 2
|
||||
|
||||
//
|
||||
#define bullishBufferIndex 0
|
||||
#define bearishBufferIndex 1
|
||||
#define trendStateBufferIndex 2
|
||||
|
||||
//
|
||||
double bullishBuffer[];
|
||||
double bearishBuffer[];
|
||||
|
||||
//
|
||||
double trendStateBuffer[];
|
||||
|
||||
#property indicator_label1 "XBullPower"
|
||||
#property indicator_type1 DRAW_LINE
|
||||
#property indicator_color1 clrLime
|
||||
#property indicator_style1 STYLE_SOLID
|
||||
#property indicator_width1 1
|
||||
|
||||
#property indicator_label2 "XBearPower"
|
||||
#property indicator_type2 DRAW_LINE
|
||||
#property indicator_color2 clrRed
|
||||
#property indicator_style2 STYLE_SOLID
|
||||
#property indicator_width2 1
|
||||
//
|
||||
// END Buffers ...
|
||||
//
|
||||
|
||||
//
|
||||
// START Global Definitions: Variables, Properties and etc ...
|
||||
//
|
||||
//
|
||||
int xtdBullishCandleCount = 0;
|
||||
int xtdBearishCandleCount = 0;
|
||||
//
|
||||
// END Global Definitions: Variables, Properties and etc ...
|
||||
//
|
||||
|
||||
//
|
||||
// START Event Handlers ...
|
||||
//
|
||||
//
|
||||
// Initialization ...
|
||||
int OnInit()
|
||||
{
|
||||
//
|
||||
// Validate Inputs ...
|
||||
if (!ValidateInputs())
|
||||
{
|
||||
return INIT_PARAMETERS_INCORRECT;
|
||||
}
|
||||
|
||||
//
|
||||
logTag = ShortName;
|
||||
drawPrefix = ShortName;
|
||||
|
||||
//
|
||||
// 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
|
||||
|
||||
//
|
||||
// Here we can handle De Initialization Reasons ...
|
||||
|
||||
//
|
||||
// De Init Draw Library ...
|
||||
OnDeinitDrawLibrary();
|
||||
}
|
||||
|
||||
//
|
||||
// Calculations ...
|
||||
//
|
||||
// Calculating what we want ...
|
||||
int OnCalculate(
|
||||
//
|
||||
// total Candles on chart ...
|
||||
const int rates_total,
|
||||
//
|
||||
// total calculated Candles on charts ...
|
||||
const int prev_calculated,
|
||||
//
|
||||
// history of Candles Open Time ...
|
||||
const datetime &time[],
|
||||
//
|
||||
// history of Candles Open Price ...
|
||||
const double &open[],
|
||||
//
|
||||
// history of Candles High Price ...
|
||||
const double &high[],
|
||||
//
|
||||
// history of Candles Low Price ...
|
||||
const double &low[],
|
||||
//
|
||||
// history of Candles Close Price ...
|
||||
const double &close[],
|
||||
//
|
||||
// history of Tick Volumes on Candle ...
|
||||
const long &tick_volume[],
|
||||
//
|
||||
// history of Trade Volumes ...
|
||||
const long &volume[],
|
||||
//
|
||||
// history of Candles Spread Price ...
|
||||
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);
|
||||
|
||||
//
|
||||
// this counts Available Bars ...
|
||||
int limit;
|
||||
|
||||
//
|
||||
// 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 ...
|
||||
int maxLength = MathMax(0, length);
|
||||
|
||||
//
|
||||
limit = prev_calculated < maxLength ? rates_total - (maxLength + 1) : rates_total - prev_calculated;
|
||||
|
||||
//
|
||||
// Main Loop ...
|
||||
for (int i = limit; i >= 0 && !IsStopped(); i--)
|
||||
{
|
||||
//
|
||||
CalculateBuffers(i, close);
|
||||
}
|
||||
|
||||
//
|
||||
return rates_total;
|
||||
}
|
||||
//
|
||||
// END Event Handlers ...
|
||||
//
|
||||
|
||||
//
|
||||
// START Functions ...
|
||||
//
|
||||
//
|
||||
// Validate Input Args for Initialization ...
|
||||
bool ValidateInputs()
|
||||
{
|
||||
//
|
||||
bool result = false;
|
||||
|
||||
//
|
||||
// Validate Args ...
|
||||
if (length >= 5)
|
||||
{
|
||||
result = true;
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
// Define Indexes and Styles ...
|
||||
void DefineBuffers()
|
||||
{
|
||||
//
|
||||
string bullishBufferLabel = "BullPow " + "(" + (string)length + ")";
|
||||
string bearishBufferLabel = "BearPow " + "(" + (string)length + ")";
|
||||
|
||||
//
|
||||
// Bullish Buffer ...
|
||||
ArraySetAsSeries(bullishBuffer, true);
|
||||
SetIndexBuffer(bullishBufferIndex, bullishBuffer, INDICATOR_DATA);
|
||||
PlotIndexSetInteger(bullishBufferIndex, PLOT_DRAW_BEGIN, length * 2);
|
||||
PlotIndexSetString(bullishBufferIndex, PLOT_LABEL, bullishBufferLabel);
|
||||
|
||||
//
|
||||
// Bearish Buffer ...
|
||||
ArraySetAsSeries(bearishBuffer, true);
|
||||
SetIndexBuffer(bearishBufferIndex, bearishBuffer, INDICATOR_DATA);
|
||||
PlotIndexSetInteger(bearishBufferIndex, PLOT_DRAW_BEGIN, length * 2);
|
||||
PlotIndexSetString(bearishBufferIndex, PLOT_LABEL, bearishBufferLabel);
|
||||
|
||||
//
|
||||
// Trend State Buffer ...
|
||||
ArraySetAsSeries(trendStateBuffer, true);
|
||||
SetIndexBuffer(trendStateBufferIndex, trendStateBuffer, INDICATOR_CALCULATIONS);
|
||||
}
|
||||
|
||||
//
|
||||
// Set Indicator Short Name and also we can define Buffers Labels ...
|
||||
void SetIndicatorName()
|
||||
{
|
||||
//
|
||||
string indicatorShortName = "";
|
||||
StringConcatenate(indicatorShortName, ShortName, " ", "( ", length, " )");
|
||||
|
||||
//
|
||||
IndicatorSetString(INDICATOR_SHORTNAME, indicatorShortName);
|
||||
}
|
||||
|
||||
//
|
||||
// Calculate Buffers ...
|
||||
void CalculateBuffers(
|
||||
int bar_index,
|
||||
const double &close[])
|
||||
{
|
||||
//
|
||||
// Reset Global Counters ...
|
||||
xtdBullishCandleCount = 0;
|
||||
xtdBearishCandleCount = 0;
|
||||
|
||||
//
|
||||
// Loop through Closes ...
|
||||
for (int i = bar_index; i <= bar_index + length; i++)
|
||||
{
|
||||
for (int j = i + 1; j <= bar_index + length; j++)
|
||||
{
|
||||
//
|
||||
if (close[i] > close[j])
|
||||
{
|
||||
xtdBullishCandleCount++;
|
||||
}
|
||||
|
||||
//
|
||||
if (close[i] < close[j])
|
||||
{
|
||||
xtdBearishCandleCount++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// Bullish Power Buffer ...
|
||||
bullishBuffer[bar_index] = MathAbs(xtdBullishCandleCount);
|
||||
|
||||
//
|
||||
// Bearish Trend Buffer ...
|
||||
bearishBuffer[bar_index] = MathAbs(xtdBearishCandleCount);
|
||||
|
||||
//
|
||||
// Check Cross Points ...
|
||||
// bullishCrossedOverBearish = 2;
|
||||
// bullishOverBearish = 1;
|
||||
// bullishCrossedUnderBearish = -2;
|
||||
// bullishUnderBearish = -1;
|
||||
// OtherWise = 0;
|
||||
bool isBullishCrossedOverBearish = IsCrossOver(
|
||||
bullishBuffer,
|
||||
bearishBuffer,
|
||||
bar_index);
|
||||
bool isBullishCrossedUnderBearish = IsCrossUnder(
|
||||
bullishBuffer,
|
||||
bearishBuffer,
|
||||
bar_index);
|
||||
bool isBullishOverBearish = IsOver(
|
||||
bullishBuffer,
|
||||
bearishBuffer,
|
||||
bar_index);
|
||||
bool isBullishUnderBearish = IsUnder(
|
||||
bullishBuffer,
|
||||
bearishBuffer,
|
||||
bar_index);
|
||||
|
||||
//
|
||||
double trendStateValue = 0;
|
||||
if (isBullishCrossedOverBearish)
|
||||
{
|
||||
trendStateValue = 2;
|
||||
}
|
||||
else if (isBullishCrossedUnderBearish)
|
||||
{
|
||||
trendStateValue = -2;
|
||||
}
|
||||
else if (isBullishOverBearish)
|
||||
{
|
||||
trendStateValue = 1;
|
||||
}
|
||||
else if (isBullishUnderBearish)
|
||||
{
|
||||
trendStateValue = -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
trendStateValue = 0;
|
||||
}
|
||||
trendStateBuffer[bar_index] = trendStateValue;
|
||||
|
||||
//
|
||||
// Check and Draw Crosses if it's Specified ...
|
||||
if (drawCrosses)
|
||||
{
|
||||
DrawCrosses(
|
||||
trendStateValue,
|
||||
bar_index);
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// Draw Crosses ...
|
||||
void DrawCrosses(
|
||||
double stateValue, // trend state value which specified draw direction ...
|
||||
int bar_index // the bars which required to draw cross arrow on it ...
|
||||
)
|
||||
{
|
||||
//
|
||||
// Only Draw Crosses ...
|
||||
if (stateValue != 2 && stateValue != -2)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
//
|
||||
bool isOver = stateValue == 2;
|
||||
double low = iLow(_Symbol, _Period, bar_index);
|
||||
double high = iHigh(_Symbol, _Period, bar_index);
|
||||
|
||||
//
|
||||
double price = isOver ? low : high;
|
||||
datetime time = iTime(_Symbol, _Period, bar_index);
|
||||
ENUM_ARROW_ANCHOR anchor = isOver ? ANCHOR_TOP : ANCHOR_BOTTOM;
|
||||
uchar arrowCode = isOver ? bullishArrowCode : bearishArrowCode;
|
||||
color arrowColor = isOver ? bullishArrowColor : bearishArrowColor;
|
||||
string crossName = "Trend " + (isOver ? "Up" : "Down") + "_" + (string)time + "_" + (string)price;
|
||||
|
||||
//
|
||||
DrawArrow(
|
||||
0,
|
||||
crossName,
|
||||
0,
|
||||
time,
|
||||
price,
|
||||
arrowCode,
|
||||
anchor,
|
||||
arrowColor);
|
||||
}
|
||||
//
|
||||
// END Functions ...
|
||||
//
|
||||
@@ -0,0 +1,306 @@
|
||||
/////////////////////////////////////////////////////////////
|
||||
//
|
||||
// SaherElm IT Center MQL5 Trend Volume Detector Oscillator
|
||||
// ---------------------------------------------------------
|
||||
// Name: XTDV
|
||||
// Description: trend detecting based on volumes in both sides ...
|
||||
//
|
||||
//
|
||||
// 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 XTDV Oscillator"
|
||||
#property strict
|
||||
|
||||
//
|
||||
// START Constants ...
|
||||
//
|
||||
#define ShortName "XTDV"
|
||||
//
|
||||
// END Constants ...
|
||||
//
|
||||
|
||||
//
|
||||
// START Inputs ...
|
||||
//
|
||||
input int length = 14; // Market Length
|
||||
//
|
||||
// END Inputs ...
|
||||
//
|
||||
|
||||
//
|
||||
// Includes Logging Library ...
|
||||
#include "../Libraries/x-saherelm.log.lib.mq5";
|
||||
|
||||
//
|
||||
// START Buffers ...
|
||||
//
|
||||
#property indicator_separate_window
|
||||
|
||||
//
|
||||
#property indicator_buffers 2
|
||||
#property indicator_plots 2
|
||||
|
||||
//
|
||||
#define bullishVolumeBufferIndex 0
|
||||
#define bearishVolumeBufferIndex 1
|
||||
|
||||
//
|
||||
double bullishVolumeBuffer[];
|
||||
double bearishVolumeBuffer[];
|
||||
|
||||
#property indicator_label1 "XBullVolume"
|
||||
#property indicator_type1 DRAW_LINE
|
||||
#property indicator_color1 clrAqua
|
||||
#property indicator_style1 STYLE_DOT
|
||||
#property indicator_width1 1
|
||||
|
||||
#property indicator_label2 "XBearVolume"
|
||||
#property indicator_type2 DRAW_LINE
|
||||
#property indicator_color2 clrMagenta
|
||||
#property indicator_style2 STYLE_DOT
|
||||
#property indicator_width2 1
|
||||
//
|
||||
// END Buffers ...
|
||||
//
|
||||
|
||||
//
|
||||
// START Global Definitions: Variables, Properties and etc ...
|
||||
//
|
||||
ulong xtdBullishCandleVolumes = 0;
|
||||
ulong xtdBearishCandleVolumes = 0;
|
||||
//
|
||||
// END Global Definitions: Variables, Properties and etc ...
|
||||
//
|
||||
|
||||
//
|
||||
// START Event Handlers ...
|
||||
//
|
||||
//
|
||||
// Initialization ...
|
||||
int OnInit()
|
||||
{
|
||||
//
|
||||
// Validate Inputs ...
|
||||
if (!ValidateInputs())
|
||||
{
|
||||
return INIT_PARAMETERS_INCORRECT;
|
||||
}
|
||||
|
||||
//
|
||||
logTag = ShortName;
|
||||
|
||||
//
|
||||
// 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
|
||||
|
||||
//
|
||||
// Here we can handle De Initialization Reasons ...
|
||||
}
|
||||
|
||||
//
|
||||
// Calculations ...
|
||||
//
|
||||
// Calculating what we want ...
|
||||
int OnCalculate(
|
||||
//
|
||||
// total Candles on chart ...
|
||||
const int rates_total,
|
||||
//
|
||||
// total calculated Candles on charts ...
|
||||
const int prev_calculated,
|
||||
//
|
||||
// history of Candles Open Time ...
|
||||
const datetime &time[],
|
||||
//
|
||||
// history of Candles Open Price ...
|
||||
const double &open[],
|
||||
//
|
||||
// history of Candles High Price ...
|
||||
const double &high[],
|
||||
//
|
||||
// history of Candles Low Price ...
|
||||
const double &low[],
|
||||
//
|
||||
// history of Candles Close Price ...
|
||||
const double &close[],
|
||||
//
|
||||
// history of Tick Volumes on Candle ...
|
||||
const long &tick_volume[],
|
||||
//
|
||||
// history of Trade Volumes ...
|
||||
const long &volume[],
|
||||
//
|
||||
// history of Candles Spread Price ...
|
||||
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);
|
||||
|
||||
//
|
||||
// this counts Available Bars ...
|
||||
int limit;
|
||||
|
||||
//
|
||||
// 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 ...
|
||||
int maxLength = MathMax(0, length);
|
||||
|
||||
//
|
||||
limit = prev_calculated < maxLength ? rates_total - (maxLength + 1) : rates_total - prev_calculated;
|
||||
|
||||
//
|
||||
// Main Loop ...
|
||||
for (int i = limit; i >= 0 && !IsStopped(); i--)
|
||||
{
|
||||
//
|
||||
CalculateBuffers(i, close);
|
||||
}
|
||||
|
||||
//
|
||||
return rates_total;
|
||||
}
|
||||
//
|
||||
// END Event Handlers ...
|
||||
//
|
||||
|
||||
//
|
||||
// START Functions ...
|
||||
//
|
||||
//
|
||||
// Validate Input Args for Initialization ...
|
||||
bool ValidateInputs()
|
||||
{
|
||||
//
|
||||
bool result = false;
|
||||
|
||||
//
|
||||
// Validate Args ...
|
||||
if (length >= 5)
|
||||
{
|
||||
result = true;
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
// Define Indexes and Styles ...
|
||||
void DefineBuffers()
|
||||
{
|
||||
//
|
||||
string bullishVolumeBufferLabel = "BullVol " + "(" + (string)length + ")";
|
||||
string bearishVolumeBufferLabel = "BearVol " + "(" + (string)length + ")";
|
||||
|
||||
//
|
||||
// Bullish Volume Buffer ...
|
||||
ArraySetAsSeries(bullishVolumeBuffer, true);
|
||||
SetIndexBuffer(bullishVolumeBufferIndex, bullishVolumeBuffer, INDICATOR_DATA);
|
||||
PlotIndexSetInteger(bullishVolumeBufferIndex, PLOT_DRAW_BEGIN, length * 2);
|
||||
PlotIndexSetString(bullishVolumeBufferIndex, PLOT_LABEL, bullishVolumeBufferLabel);
|
||||
|
||||
//
|
||||
// Bearish Volume Buffer ...
|
||||
ArraySetAsSeries(bearishVolumeBuffer, true);
|
||||
SetIndexBuffer(bearishVolumeBufferIndex, bearishVolumeBuffer, INDICATOR_DATA);
|
||||
PlotIndexSetInteger(bearishVolumeBufferIndex, PLOT_DRAW_BEGIN, length * 2);
|
||||
PlotIndexSetString(bearishVolumeBufferIndex, PLOT_LABEL, bearishVolumeBufferLabel);
|
||||
}
|
||||
|
||||
//
|
||||
// Set Indicator Short Name and also we can define Buffers Labels ...
|
||||
void SetIndicatorName()
|
||||
{
|
||||
//
|
||||
string indicatorShortName = "";
|
||||
StringConcatenate(indicatorShortName, ShortName, " ", "( ", length, " )");
|
||||
|
||||
//
|
||||
IndicatorSetString(INDICATOR_SHORTNAME, indicatorShortName);
|
||||
}
|
||||
|
||||
//
|
||||
// Calculate Buffers ...
|
||||
void CalculateBuffers(
|
||||
int bar_index,
|
||||
const double &close[])
|
||||
{
|
||||
//
|
||||
// Reset Global Counters ...
|
||||
xtdBullishCandleVolumes = 0;
|
||||
xtdBearishCandleVolumes = 0;
|
||||
|
||||
//
|
||||
// Loop through Closes ...
|
||||
for (int i = bar_index; i <= bar_index + length; i++)
|
||||
{
|
||||
for (int j = i + 1; j <= bar_index + length; j++)
|
||||
{
|
||||
//
|
||||
if (close[i] > close[j])
|
||||
{
|
||||
xtdBullishCandleVolumes += iVolume(_Symbol, _Period, i);
|
||||
}
|
||||
|
||||
//
|
||||
if (close[i] < close[j])
|
||||
{
|
||||
xtdBearishCandleVolumes += iVolume(_Symbol, _Period, i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// Bullish Volume Buffer ...
|
||||
bullishVolumeBuffer[bar_index] = (double)xtdBullishCandleVolumes;
|
||||
|
||||
//
|
||||
// Bearish Trend Buffer ...
|
||||
bearishVolumeBuffer[bar_index] = (double)xtdBearishCandleVolumes;
|
||||
}
|
||||
//
|
||||
// END Functions ...
|
||||
//
|
||||
@@ -0,0 +1,340 @@
|
||||
///////////////////////////////////////////////////////
|
||||
//
|
||||
// SaherElm IT Center MQL5 Candle Indicator
|
||||
// ---------------------------------------------
|
||||
// Name: XTM
|
||||
// Description: Trend Magic 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 XTM Indicator"
|
||||
#property strict
|
||||
|
||||
//
|
||||
// START Constants ...
|
||||
//
|
||||
#define ShortName "XTM"
|
||||
//
|
||||
// END Constants ...
|
||||
//
|
||||
|
||||
//
|
||||
// START Inputs ...
|
||||
//
|
||||
input int maPeriod = 14; // Period
|
||||
input int maShift = 0; // Shift
|
||||
input ENUM_MA_METHOD maMethod = MODE_SMA; // Method
|
||||
input ENUM_APPLIED_PRICE maAppliedTo = PRICE_CLOSE; // Applied To
|
||||
//
|
||||
// END Inputs ...
|
||||
//
|
||||
|
||||
//
|
||||
// Includes Logging Library ...
|
||||
#include "../Libraries/x-saherelm.log.lib.mq5";
|
||||
|
||||
//
|
||||
// Includes Common Library ...
|
||||
#include "../Libraries/x-saherelm.common.lib.mq5";
|
||||
|
||||
//
|
||||
// START Buffers ...
|
||||
//
|
||||
#property indicator_chart_window
|
||||
|
||||
//
|
||||
#property indicator_buffers 3
|
||||
#property indicator_plots 1
|
||||
|
||||
//
|
||||
// maBuffer ...
|
||||
#define maBufferIndex 0
|
||||
#define maColorBufferIndex 1
|
||||
#define stateBufferIndex 2
|
||||
|
||||
double maBuffer[];
|
||||
double maColorBuffer[];
|
||||
double stateBuffer[];
|
||||
|
||||
#property indicator_label1 "XTM"
|
||||
#property indicator_type1 DRAW_COLOR_LINE
|
||||
#property indicator_color1 clrLime, clrGray, clrRed
|
||||
#property indicator_style1 STYLE_SOLID
|
||||
#property indicator_width1 1
|
||||
//
|
||||
// END Buffers ...
|
||||
//
|
||||
|
||||
//
|
||||
// START Global Definitions: Variables, Properties and etc ...
|
||||
//
|
||||
int maHandler = INVALID_HANDLE;
|
||||
//
|
||||
// END Global Definitions: Variables, Properties and etc ...
|
||||
//
|
||||
|
||||
//
|
||||
// START Event Handlers ...
|
||||
//
|
||||
//
|
||||
// Initialization ...
|
||||
int OnInit()
|
||||
{
|
||||
//
|
||||
// Validate Inputs ...
|
||||
if (!ValidateInputs())
|
||||
{
|
||||
return INIT_PARAMETERS_INCORRECT;
|
||||
}
|
||||
|
||||
//
|
||||
logTag = ShortName;
|
||||
|
||||
//
|
||||
// Initializing MA Handler ...
|
||||
maHandler = iMA(
|
||||
_Symbol,
|
||||
_Period,
|
||||
maPeriod,
|
||||
maShift,
|
||||
maMethod,
|
||||
maAppliedTo);
|
||||
if (maHandler == INVALID_HANDLE)
|
||||
{
|
||||
//
|
||||
string message = "Error Initializing Ma Handler ...";
|
||||
LogMessage(message);
|
||||
|
||||
//
|
||||
return INIT_FAILED;
|
||||
}
|
||||
|
||||
//
|
||||
// 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
|
||||
|
||||
//
|
||||
// Here we can handle De Initialization Reasons ...
|
||||
}
|
||||
|
||||
//
|
||||
// Calculations ...
|
||||
//
|
||||
// Calculating what we want ...
|
||||
int OnCalculate(
|
||||
//
|
||||
// total Candles on chart ...
|
||||
const int rates_total,
|
||||
//
|
||||
// total calculated Candles on charts ...
|
||||
const int prev_calculated,
|
||||
//
|
||||
// history of Candles Open Time ...
|
||||
const datetime &time[],
|
||||
//
|
||||
// history of Candles Open Price ...
|
||||
const double &open[],
|
||||
//
|
||||
// history of Candles High Price ...
|
||||
const double &high[],
|
||||
//
|
||||
// history of Candles Low Price ...
|
||||
const double &low[],
|
||||
//
|
||||
// history of Candles Close Price ...
|
||||
const double &close[],
|
||||
//
|
||||
// history of Tick Volumes on Candle ...
|
||||
const long &tick_volume[],
|
||||
//
|
||||
// history of Trade Volumes ...
|
||||
const long &volume[],
|
||||
//
|
||||
// history of Candles Spread Price ...
|
||||
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);
|
||||
|
||||
//
|
||||
// this counts Available Bars ...
|
||||
int limit;
|
||||
|
||||
//
|
||||
// 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 ...
|
||||
int maxLength = MathMax(0, maPeriod);
|
||||
|
||||
//
|
||||
// Check Calculated Bars ...
|
||||
int maCalculatedBars = BarsCalculated(maHandler);
|
||||
if (maCalculatedBars < maxLength)
|
||||
{
|
||||
return prev_calculated;
|
||||
}
|
||||
|
||||
//
|
||||
limit = (prev_calculated > rates_total || prev_calculated <= 0) ? rates_total : (rates_total - prev_calculated) + 1;
|
||||
|
||||
//
|
||||
// Buffers Copy ...
|
||||
int copiedMas = CopyBuffer(maHandler, 0, 0, limit, maBuffer);
|
||||
if (copiedMas <= 0)
|
||||
{
|
||||
return prev_calculated;
|
||||
}
|
||||
|
||||
//
|
||||
// Main Loop ...
|
||||
for (int i = limit - 1; i >= 0 && !IsStopped(); i--)
|
||||
{
|
||||
//
|
||||
// Do Buffers Calculation ...
|
||||
CalculateBuffers(i);
|
||||
}
|
||||
|
||||
//
|
||||
return rates_total;
|
||||
}
|
||||
//
|
||||
// END Event Handlers ...
|
||||
//
|
||||
|
||||
//
|
||||
// START Functions ...
|
||||
//
|
||||
//
|
||||
// Validate Input Args for Initialization ...
|
||||
bool ValidateInputs()
|
||||
{
|
||||
//
|
||||
bool result = false;
|
||||
|
||||
//
|
||||
// Validate Args ...
|
||||
if (maPeriod >= 2)
|
||||
{
|
||||
result = true;
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
// Define Indexes and Styles ...
|
||||
void DefineBuffers()
|
||||
{
|
||||
//
|
||||
string maBufferLabel = "XTM " + "(" + (string)maPeriod + ")";
|
||||
|
||||
//
|
||||
// Ma Buffer ...
|
||||
ArraySetAsSeries(maBuffer, true);
|
||||
SetIndexBuffer(maBufferIndex, maBuffer, INDICATOR_DATA);
|
||||
PlotIndexSetString(maBufferIndex, PLOT_LABEL, maBufferLabel);
|
||||
|
||||
//
|
||||
// Ma Color Buffer ...
|
||||
ArraySetAsSeries(maColorBuffer, true);
|
||||
SetIndexBuffer(maColorBufferIndex, maColorBuffer, INDICATOR_COLOR_INDEX);
|
||||
|
||||
//
|
||||
// State Buffer ...
|
||||
ArraySetAsSeries(stateBuffer, true);
|
||||
SetIndexBuffer(stateBufferIndex, stateBuffer, INDICATOR_CALCULATIONS);
|
||||
}
|
||||
|
||||
//
|
||||
// Set Indicator Short Name and also we can define Buffers Labels ...
|
||||
void SetIndicatorName()
|
||||
{
|
||||
//
|
||||
string indicatorShortName = "";
|
||||
StringConcatenate(indicatorShortName, ShortName, " ", "(", maPeriod, ")");
|
||||
|
||||
//
|
||||
IndicatorSetString(INDICATOR_SHORTNAME, indicatorShortName);
|
||||
}
|
||||
|
||||
//
|
||||
// Calculate Buffers ...
|
||||
void CalculateBuffers(int bar_index)
|
||||
{
|
||||
//
|
||||
// Requirements ...
|
||||
XOHCL candle = GetCandle(bar_index);
|
||||
|
||||
//
|
||||
double maValue = maBuffer[bar_index];
|
||||
|
||||
//
|
||||
double lineColorIndex =
|
||||
maValue < candle.low ? 0 : maValue > candle.high ? 2
|
||||
: 1;
|
||||
|
||||
//
|
||||
// Define State Value ...
|
||||
// lineColorIndex == 0 => Bullish => 1;
|
||||
// lineColorIndex == 1 => Neutural => 0;
|
||||
// lineColorIndex == 2 => Bearish => -1;
|
||||
double stateValue =
|
||||
lineColorIndex == 0 ? X_XTM_BULLISH : lineColorIndex == 2 ? X_XTM_BEARISH
|
||||
: X_XTM_NEUTURAL;
|
||||
|
||||
//
|
||||
// Set Ma Buffer Color Index ...
|
||||
maColorBuffer[bar_index] = lineColorIndex;
|
||||
|
||||
//
|
||||
// Set State Buffer Value ...
|
||||
stateBuffer[bar_index] = stateValue;
|
||||
}
|
||||
|
||||
//
|
||||
// END Functions ...
|
||||
//
|
||||
@@ -0,0 +1,351 @@
|
||||
/////////////////////////////////////////////////////////////
|
||||
//
|
||||
// SaherElm IT Center MQL5 Trend Power Detector Oscillator
|
||||
// ---------------------------------------------------------
|
||||
// Name: XTPD
|
||||
// Description: trend detecting based on volumes in both sides ...
|
||||
//
|
||||
//
|
||||
// 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 XTPD Oscillator"
|
||||
#property strict
|
||||
|
||||
//
|
||||
// START Constants ...
|
||||
//
|
||||
#define ShortName "XTPD"
|
||||
//
|
||||
// END Constants ...
|
||||
//
|
||||
|
||||
//
|
||||
// START Inputs ...
|
||||
//
|
||||
input int length = 14; // Market Length
|
||||
//
|
||||
// END Inputs ...
|
||||
//
|
||||
|
||||
//
|
||||
// Includes Logging Library ...
|
||||
#include "../Libraries/x-saherelm.log.lib.mq5";
|
||||
|
||||
//
|
||||
// Includes Common Library ...
|
||||
#include "../Libraries/x-saherelm.common.lib.mq5";
|
||||
|
||||
//
|
||||
// START Buffers ...
|
||||
//
|
||||
#property indicator_separate_window
|
||||
|
||||
//
|
||||
#property indicator_buffers 3
|
||||
#property indicator_plots 2
|
||||
|
||||
//
|
||||
#define bullishPowerBufferIndex 0
|
||||
#define bearishPowerBufferIndex 1
|
||||
#define stateBufferIndex 2
|
||||
|
||||
//
|
||||
double bullishPowerBuffer[];
|
||||
double bearishPowerBuffer[];
|
||||
double stateBuffer[];
|
||||
|
||||
#property indicator_label1 "XBullPower"
|
||||
#property indicator_type1 DRAW_LINE
|
||||
#property indicator_color1 clrLime
|
||||
#property indicator_style1 STYLE_DOT
|
||||
#property indicator_width1 1
|
||||
|
||||
#property indicator_label2 "XBearPower"
|
||||
#property indicator_type2 DRAW_LINE
|
||||
#property indicator_color2 clrRed
|
||||
#property indicator_style2 STYLE_DOT
|
||||
#property indicator_width2 1
|
||||
//
|
||||
// END Buffers ...
|
||||
//
|
||||
|
||||
//
|
||||
// START Global Definitions: Variables, Properties and etc ...
|
||||
//
|
||||
//
|
||||
// END Global Definitions: Variables, Properties and etc ...
|
||||
//
|
||||
|
||||
//
|
||||
// START Event Handlers ...
|
||||
//
|
||||
//
|
||||
// Initialization ...
|
||||
int OnInit()
|
||||
{
|
||||
//
|
||||
// Validate Inputs ...
|
||||
if (!ValidateInputs())
|
||||
{
|
||||
return INIT_PARAMETERS_INCORRECT;
|
||||
}
|
||||
|
||||
//
|
||||
logTag = ShortName;
|
||||
|
||||
//
|
||||
// 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
|
||||
|
||||
//
|
||||
// Here we can handle De Initialization Reasons ...
|
||||
}
|
||||
|
||||
//
|
||||
// Calculations ...
|
||||
//
|
||||
// Calculating what we want ...
|
||||
int OnCalculate(
|
||||
//
|
||||
// total Candles on chart ...
|
||||
const int rates_total,
|
||||
//
|
||||
// total calculated Candles on charts ...
|
||||
const int prev_calculated,
|
||||
//
|
||||
// history of Candles Open Time ...
|
||||
const datetime &time[],
|
||||
//
|
||||
// history of Candles Open Price ...
|
||||
const double &open[],
|
||||
//
|
||||
// history of Candles High Price ...
|
||||
const double &high[],
|
||||
//
|
||||
// history of Candles Low Price ...
|
||||
const double &low[],
|
||||
//
|
||||
// history of Candles Close Price ...
|
||||
const double &close[],
|
||||
//
|
||||
// history of Tick Volumes on Candle ...
|
||||
const long &tick_volume[],
|
||||
//
|
||||
// history of Trade Volumes ...
|
||||
const long &volume[],
|
||||
//
|
||||
// history of Candles Spread Price ...
|
||||
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);
|
||||
|
||||
//
|
||||
// this counts Available Bars ...
|
||||
int limit;
|
||||
|
||||
//
|
||||
// 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 ...
|
||||
int maxLength = MathMax(0, length);
|
||||
|
||||
//
|
||||
limit = prev_calculated < maxLength ? rates_total - (maxLength + 1) : rates_total - prev_calculated;
|
||||
|
||||
//
|
||||
// Main Loop ...
|
||||
for (int i = limit; i >= 0 && !IsStopped(); i--)
|
||||
{
|
||||
//
|
||||
CalculateBuffers(i);
|
||||
}
|
||||
|
||||
//
|
||||
return rates_total;
|
||||
}
|
||||
//
|
||||
// END Event Handlers ...
|
||||
//
|
||||
|
||||
//
|
||||
// START Functions ...
|
||||
//
|
||||
//
|
||||
// Validate Input Args for Initialization ...
|
||||
bool ValidateInputs()
|
||||
{
|
||||
//
|
||||
bool result = false;
|
||||
|
||||
//
|
||||
// Validate Args ...
|
||||
if (length >= 5)
|
||||
{
|
||||
result = true;
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
// Define Indexes and Styles ...
|
||||
void DefineBuffers()
|
||||
{
|
||||
//
|
||||
string bullishPowerBufferLabel = "BullPow " + "(" + (string)length + ")";
|
||||
string bearishPowerBufferLabel = "BearPow " + "(" + (string)length + ")";
|
||||
|
||||
//
|
||||
// Bullish Volume Buffer ...
|
||||
ArraySetAsSeries(bullishPowerBuffer, true);
|
||||
SetIndexBuffer(bullishPowerBufferIndex, bullishPowerBuffer, INDICATOR_DATA);
|
||||
PlotIndexSetInteger(bullishPowerBufferIndex, PLOT_DRAW_BEGIN, length * 2);
|
||||
PlotIndexSetString(bullishPowerBufferIndex, PLOT_LABEL, bullishPowerBufferLabel);
|
||||
|
||||
//
|
||||
// Bearish Volume Buffer ...
|
||||
ArraySetAsSeries(bearishPowerBuffer, true);
|
||||
SetIndexBuffer(bearishPowerBufferIndex, bearishPowerBuffer, INDICATOR_DATA);
|
||||
PlotIndexSetInteger(bearishPowerBufferIndex, PLOT_DRAW_BEGIN, length * 2);
|
||||
PlotIndexSetString(bearishPowerBufferIndex, PLOT_LABEL, bearishPowerBufferLabel);
|
||||
|
||||
//
|
||||
// State Buffer ...
|
||||
ArraySetAsSeries(stateBuffer, true);
|
||||
SetIndexBuffer(stateBufferIndex, stateBuffer, INDICATOR_CALCULATIONS);
|
||||
}
|
||||
|
||||
//
|
||||
// Set Indicator Short Name and also we can define Buffers Labels ...
|
||||
void SetIndicatorName()
|
||||
{
|
||||
//
|
||||
string indicatorShortName = "";
|
||||
StringConcatenate(indicatorShortName, ShortName, " ", "( ", length, " )");
|
||||
|
||||
//
|
||||
IndicatorSetString(INDICATOR_SHORTNAME, indicatorShortName);
|
||||
}
|
||||
|
||||
//
|
||||
// Calculate Buffers ...
|
||||
void CalculateBuffers(int bar_index)
|
||||
{
|
||||
//
|
||||
double xBullishPowerValue = 0;
|
||||
double xBearishPowerValue = 1;
|
||||
|
||||
//
|
||||
// Loop through bar index and Market Length ...
|
||||
for (int i = bar_index + length + 1; i > bar_index && !IsStopped(); i--)
|
||||
{
|
||||
//
|
||||
XOHCL candle = GetCandle(i);
|
||||
|
||||
//
|
||||
bool isBullish = IsBullishCandle(candle);
|
||||
bool isBearish = IsBearishCandle(candle);
|
||||
|
||||
//
|
||||
if (isBullish)
|
||||
{
|
||||
xBullishPowerValue++;
|
||||
}
|
||||
else if (isBearish)
|
||||
{
|
||||
xBearishPowerValue++;
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
bullishPowerBuffer[bar_index] = xBullishPowerValue;
|
||||
bearishPowerBuffer[bar_index] = xBearishPowerValue;
|
||||
|
||||
//
|
||||
// Calculate State ...
|
||||
bool isBullishCrossOverBearish = IsCrossOver(
|
||||
bullishPowerBuffer,
|
||||
bearishPowerBuffer,
|
||||
bar_index);
|
||||
bool isBullishOverBearish = IsOver(
|
||||
bullishPowerBuffer,
|
||||
bearishPowerBuffer,
|
||||
bar_index);
|
||||
bool isBullishCrossUnderBearish = IsCrossUnder(
|
||||
bullishPowerBuffer,
|
||||
bearishPowerBuffer,
|
||||
bar_index);
|
||||
bool isBullishUnderBearish = IsUnder(
|
||||
bullishPowerBuffer,
|
||||
bearishPowerBuffer,
|
||||
bar_index);
|
||||
|
||||
//
|
||||
double stateValue = X_XTPD_NEUTURAL;
|
||||
if (isBullishCrossOverBearish)
|
||||
{
|
||||
stateValue = X_XTPD_BULLISH_CROSSED_OVER_BEARISH;
|
||||
}
|
||||
else if (isBullishCrossUnderBearish)
|
||||
{
|
||||
stateValue = X_XTPD_BULLISH_CROSSED_UNDER_BEARISH;
|
||||
}
|
||||
else if (isBullishOverBearish)
|
||||
{
|
||||
stateValue = X_XTPD_BULLISH_OVER_BEARISH;
|
||||
}
|
||||
else if (isBullishUnderBearish)
|
||||
{
|
||||
stateValue = X_XTPD_BULLISH_UNDER_BEARISH;
|
||||
}
|
||||
|
||||
//
|
||||
stateBuffer[bar_index] = stateValue;
|
||||
}
|
||||
//
|
||||
// END Functions ...
|
||||
//
|
||||
Reference in New Issue
Block a user