368 lines
8.1 KiB
Plaintext
368 lines
8.1 KiB
Plaintext
///////////////////////////////////////////////////////
|
|
//
|
|
// 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 ...
|
|
//
|