This commit is contained in:
2025-01-06 06:09:50 +03:30
parent 6f61bc6d30
commit 9d4b2ee578
21 changed files with 17827 additions and 4044 deletions
File diff suppressed because it is too large Load Diff
+650
View File
@@ -0,0 +1,650 @@
///////////////////////////////////////////////////////
//
// SaherElm IT Center MQL5 Indicator
// -------------------------------------------------
// Name: X121 XATR
// Description: XATR ...
//
//
// Maintainer:
// ------------
// Hadi Khazaee Asl (hadi_khazaee_asl@yahoo.com)
//
//////////////////////////////////////////////////////
//
// Global Properties ...
#property copyright "Copyright 2023, SaherElm IT Center"
#property link "https://saherelm.ir"
#property version "1.00"
#property description "SaherElm X121 XATR Indicator"
#property strict
//
// Definitions ...
//
#define ShortName "X121 XATR"
//
// Includes Common Library ...
#include "../Classes/x-saherelm.x-poi.class.mq5"
#include "../Classes/x-saherelm.x-poi.drawer.class.mq5"
#include "../Libraries/x-saherelm.common.lib.mq5"
//
// Inputs ...
//
// Market ...
input group "Market";
//
input group "RSI Detection";
input int rsiLength = 14; // Length
input ENUM_APPLIED_PRICE rsiAppliedTo = PRICE_CLOSE; // Applied To
//
input group "ATR Detection";
input int atrLength = 14; // Length
input double atrMultiplier = 1; // Multiplier
input ENUM_APPLIED_PRICE atrUpperAppliedTo = PRICE_HIGH; // Upper Zone Applied To
input ENUM_APPLIED_PRICE atrLowerAppliedTo = PRICE_LOW; // Lower Zone Applied To
input ENUM_X_MA_METHOD atrSmoothingMode = X_MA_MODE_EMA; // Smoothing Method
//
input group "Presentation";
//
input int startCalculationForLastBars = 1000; // Calculate Last n Bars
//
input bool showAtrUpper = true; // Show Upper Zone
input bool showAtrLower = true; // Show Lower Zone
//
// Buffers ...
//
#define hideColorIDX 0
#define bullishColorIDX 1
#define bearishColorIDX 2
#define neuturalColorIDX 3
//
#property indicator_chart_window
//
#property indicator_buffers 6
#property indicator_plots 2
//
// ATR ...
//
// Upper ...
//
#define atrUpperBufferIndex 0
double atrUpperBuffer[];
#property indicator_label1 "X121 ATRU"
#property indicator_type1 DRAW_LINE
#property indicator_color1 clrYellow
#property indicator_style1 STYLE_SOLID
#property indicator_width1 2
//
// Lower ...
//
#define atrLowerBufferIndex 1
double atrLowerBuffer[];
#property indicator_label2 "X121 ATRL"
#property indicator_type2 DRAW_LINE
#property indicator_color2 clrYellow
#property indicator_style2 STYLE_SOLID
#property indicator_width2 2
//
// Data Buffers ...
//
int mLastBufferIndex = 1;
//
// ATR ...
#define atrBufferIndex mLastBufferIndex + 1
double atrBuffer[];
#define atrUpperRawBufferIndex mLastBufferIndex + 2
double atrUpperRawBuffer[];
#define atrLowerRawBufferIndex mLastBufferIndex + 3
double atrLowerRawBuffer[];
//
// RSI ...
#define rsiBufferIndex mLastBufferIndex + 4
double rsiBuffer[];
//
// Variables, Properties and etc ...
//
// this counts Available Bars ...
int limit;
//
int maxLength;
//
// RSI Handler ...
int rsiHandler = INVALID_HANDLE;
//
// ATR Handler ...
int atrHandler = INVALID_HANDLE;
//
bool isStrTrendChanged;
bool isStrStartBearishTrend;
bool isStrStartBullishTrend;
//
// Event Handlers ...
/**
* Initialize Indicator ...
*
* @return ( int )
*/
int OnInit()
{
//
// Validate Inputs ...
if (!ValidateInputs())
{
return INIT_PARAMETERS_INCORRECT;
}
//
// Initialize Indicator Handlers ...
//
// RSI ...
rsiHandler = iRSI(
_Symbol,
_Period,
rsiLength,
rsiAppliedTo //
);
bool isInited = rsiHandler != INVALID_HANDLE;
if (!isInited)
{
return INIT_FAILED;
}
//
// ATR ...
atrHandler = iATR(
_Symbol,
_Period,
atrLength //
);
isInited = atrHandler != INVALID_HANDLE;
if (!isInited)
{
return INIT_FAILED;
}
//
// because in some cases we may have more than one input for
// calculation and we must prevent any calculation
// untill we pass the biggest input length, here we get max Input length
// and then wait until pass it ...
maxLength = ExtractMaxLengthOfInputs();
//
// Define Index Buffers ...
DefineBuffers();
//
// Set Indicator ShortName ...
SetIndicatorName();
//
// Init Succeed ...
return INIT_SUCCEEDED;
}
/**
* De Initialize Indicator ...
*
* @param reason: Integer, De Initialization Reason ...
*/
void OnDeinit(const int reason)
{
//
// REASON_PROGRAM 0 The EA has stopped working calling the ExpertRemove() function
// REASON_REMOVE 1 Program removed from a chart
// REASON_RECOMPILE 2 Program recompiled
// REASON_CHARTCHANGE 3 A symbol or a chart period is changed
// REASON_CHARTCLOSE 4 Chart closed
// REASON_PARAMETERS 5 Inputs changed by a user
// REASON_ACCOUNT 6 Another account has been activated or reconnection to the trade server has occurred due to changes in the account settings
// REASON_TEMPLATE 7 Another chart template applied
// REASON_INITFAILED 8 The OnInit() handler returned a non-zero value
// REASON_CLOSE 9 Terminal closed
//
IndicatorRelease(rsiHandler);
IndicatorRelease(atrHandler);
}
/**
* Calculate Bars ...
*
* @param rates_total: Integer, Total Bars on Chart ...
* @param prev_calculated: Integer, Total Calculated Bars on Charts ...
* @param time: DateTime Array, History of Open Time ...
* @param open: Double Array, History of Open Prices ...
* @param high: Double Array, History of High Prices ...
* @param low: Double Array, History of Low Prices ...
* @param close: Double Array, History of Close Prices ...
* @param tick_volume: Long, History of Tick Volumes on Bar ...
* @param volume: Long, History of Trade Volumes ...
* @param spread: Double, History of Spread Price ...
*
* @return ( int )
*/
int OnCalculate(
const int rates_total,
const int prev_calculated,
const datetime &time[],
const double &open[],
const double &high[],
const double &low[],
const double &close[],
const long &tick_volume[],
const long &volume[],
const int &spread[] //
)
{
//
// Prepare Buffers ...
ArraySetAsSeries(time, true);
ArraySetAsSeries(open, true);
ArraySetAsSeries(high, true);
ArraySetAsSeries(low, true);
ArraySetAsSeries(close, true);
ArraySetAsSeries(tick_volume, true);
ArraySetAsSeries(volume, true);
ArraySetAsSeries(spread, true);
//
// Fill All Buffers by Zero ...
if (prev_calculated == 0)
{
}
//
// Validate Calculated Bars ...
//
// RSI ...
int rsiCalculatedBars = BarsCalculated(rsiHandler);
//
// ATR ...
int atrCalculatedBars = BarsCalculated(atrHandler);
//
bool isPassedRequiredCalculatedBars =
//
// RSI ...
rsiCalculatedBars >= maxLength &&
//
// ATR ...
atrCalculatedBars >= maxLength
//
;
if (!isPassedRequiredCalculatedBars)
{
return prev_calculated;
}
//
limit = (prev_calculated > rates_total || prev_calculated <= 0)
? rates_total
: (rates_total - prev_calculated) + 1;
//
// Buffers Copy ...
//
// RSI ...
int copiedRsis = CopyBuffer(rsiHandler, 0, 0, limit, rsiBuffer);
//
// ATR ...
int copiedAtrs = CopyBuffer(atrHandler, 0, 0, limit, atrBuffer);
//
// Validate Copied Items ...
bool isPassedRequiredCopiedItems =
//
//
// RSI ...
copiedRsis > 0 &&
//
// ATR ...
copiedAtrs > 0
//
;
if (!isPassedRequiredCopiedItems)
{
return prev_calculated;
}
//
// Main Loop ...
for (int i = limit - 1; i >= 0 && !IsStopped(); i--)
{
//
CalculateBuffers(
i,
prev_calculated,
rates_total,
//
open,
high,
close,
low,
tick_volume //
);
}
//
return rates_total;
}
//
// Functions ...
/**
* Validate Input Args for Initialization ...
*
* @return ( bool )
*/
bool ValidateInputs()
{
//
bool result =
//
// RSI ...
rsiLength > 0 &&
//
// ATR ...
atrLength > 0 &&
atrMultiplier > 0
//
;
//
return result;
}
/**
* Extract Max Length of Inputs ...
*
* @return ( int )
*/
int ExtractMaxLengthOfInputs()
{
//
int result = 0;
//
result = MathMax(rsiLength, atrLength);
//
return result;
}
/**
* Define Required Buffers ...
*/
void DefineBuffers()
{
//
// ATR ...
//
// UPPER ...
bool canShowAtrUpper = showAtrUpper;
ENUM_DRAW_TYPE atrUpperDrawType = canShowAtrUpper ? DRAW_LINE : DRAW_NONE;
ArraySetAsSeries(atrUpperBuffer, true);
SetIndexBuffer(atrUpperBufferIndex, atrUpperBuffer, INDICATOR_DATA);
PlotIndexSetDouble(atrUpperBufferIndex, PLOT_EMPTY_VALUE, EMPTY_VALUE);
PlotIndexSetInteger(atrUpperBufferIndex, PLOT_SHOW_DATA, canShowAtrUpper);
PlotIndexSetInteger(atrUpperBufferIndex, PLOT_DRAW_TYPE, atrUpperDrawType);
//
// LOWER ...
bool canShowAtrLower = showAtrLower;
ENUM_DRAW_TYPE atrLowerDrawType = canShowAtrLower ? DRAW_LINE : DRAW_NONE;
ArraySetAsSeries(atrLowerBuffer, true);
SetIndexBuffer(atrLowerBufferIndex, atrLowerBuffer, INDICATOR_DATA);
PlotIndexSetDouble(atrLowerBufferIndex, PLOT_EMPTY_VALUE, EMPTY_VALUE);
PlotIndexSetInteger(atrLowerBufferIndex, PLOT_SHOW_DATA, canShowAtrLower);
PlotIndexSetInteger(atrLowerBufferIndex, PLOT_DRAW_TYPE, atrLowerDrawType);
//
// Data Buffers ...
//
// ATR ...
//
ArraySetAsSeries(atrBuffer, true);
SetIndexBuffer(atrBufferIndex, atrBuffer, INDICATOR_CALCULATIONS);
//
ArraySetAsSeries(atrUpperRawBuffer, true);
SetIndexBuffer(atrUpperRawBufferIndex, atrUpperRawBuffer, INDICATOR_CALCULATIONS);
//
ArraySetAsSeries(atrLowerRawBuffer, true);
SetIndexBuffer(atrLowerRawBufferIndex, atrLowerRawBuffer, INDICATOR_CALCULATIONS);
//
// RSI ...
//
ArraySetAsSeries(rsiBuffer, true);
SetIndexBuffer(rsiBufferIndex, rsiBuffer, INDICATOR_CALCULATIONS);
}
/**
* Set Indicator Short Name and also we can define Buffers Labels ...
*/
void SetIndicatorName()
{
IndicatorSetString(INDICATOR_SHORTNAME, ShortName);
}
/**
* Calculate Custom Buffers ...
*
* @param bar_index: Integer, Represent Current Bar ...
* @param prevCalculated: Integer, Represent Previous Calculated Bars ...
* @param ratesTotal: Integer, Represents All Available Bars ...
* @param open: Double Array, History of Open Prices ...
* @param high: Double Array, History of High Prices ...
* @param close: Double Array, History of Close Prices ...
* @param low: Double Array, History of Low Prices ...
* @param tickVolume: Long, History of Tick Volumes on Bar ...
*/
void CalculateBuffers(
int bar_index, // Selected Bar Index
const int prevCalculated,
const int ratesTotal,
const double &open[],
const double &high[],
const double &close[],
const double &low[],
const long &tickVolume[] //
)
{
//
int barsLimit = startCalculationForLastBars > 0
? startCalculationForLastBars
: 0;
//
// bool canCalculate = true;
bool canCalculate =
barsLimit == 0 ||
bar_index <= barsLimit;
if (canCalculate)
{
//
CalculateAtrZones(
bar_index,
prevCalculated,
ratesTotal,
open,
high,
close,
low //
);
}
else
{
FillBuffersZero(bar_index);
}
}
//
// Custom ...
/**
* Fill All Bufers to Zero Vlue for Specified Bar Index ...
*
* @param barIndex: Integer ...
*/
void FillBuffersZero(int barIndex)
{
//
// RSIS ...
rsiBuffer[barIndex] = 0;
//
// ATRS ...
atrBuffer[barIndex] = 0;
atrUpperBuffer[barIndex] = 0;
atrUpperRawBuffer[barIndex] = 0;
atrUpperBuffer[barIndex] = 0;
atrLowerRawBuffer[barIndex] = 0;
}
/**
* Calculate ATR Zones ...
*
* @param bar_index: Integer, Represent Current Bar ...
* @param prevCalculated: Integer, Represent Previous Calculated Bars ...
* @param ratesTotal: Integer, Represents All Available Bars ...
* @param open: Double Array, History of Open Prices ...
* @param high: Double Array, History of High Prices ...
* @param close: Double Array, History of Close Prices ...
* @param low: Double Array, History of Low Prices ...
*/
void CalculateAtrZones(
int bar_index, // Selected Bar Index
const int prevCalculated,
const int ratesTotal,
const double &open[],
const double &high[],
const double &close[],
const double &low[] //
)
{
//
// Select ATr Value ...
double atrValue = atrBuffer[bar_index];
double appliedAtrMultiplierValue = atrValue * atrMultiplier;
//
// Select Upper Price ...
double upperPrice = GetAppliedPrice(
atrUpperAppliedTo,
open,
high,
low,
close,
bar_index //
);
//
// Select Lower Price ...
double lowerPrice = GetAppliedPrice(
atrLowerAppliedTo,
open,
high,
low,
close,
bar_index //
);
//
// Calculate Raw Atrs ...
//
// Upper ...
double atrUpperRawValue = upperPrice + appliedAtrMultiplierValue;
atrUpperRawBuffer[bar_index] = atrUpperRawValue;
//
// Lower ...
double atrLowerRawValue = lowerPrice - appliedAtrMultiplierValue;
atrLowerRawBuffer[bar_index] = atrLowerRawValue;
//
bool canSmooth = atrSmoothingMode != X_MA_MODE_NONE;
if (canSmooth)
{
//
// Upper ...
iMAOnBuffer(
ratesTotal,
prevCalculated,
bar_index,
atrLength,
atrUpperRawBuffer,
atrUpperBuffer,
atrSmoothingMode //
);
//
// Lower ...
iMAOnBuffer(
ratesTotal,
prevCalculated,
bar_index,
atrLength,
atrLowerRawBuffer,
atrLowerBuffer,
atrSmoothingMode //
);
}
else
{
//
// Use Raw Values ...
atrUpperBuffer[bar_index] = atrUpperRawValue;
atrLowerBuffer[bar_index] = atrLowerRawValue;
}
}
//
+692
View File
@@ -0,0 +1,692 @@
///////////////////////////////////////////////////////
//
// SaherElm IT Center MQL5 Indicator
// -------------------------------------------------
// Name: X121 XDON
// Description: XDON ...
//
//
// Maintainer:
// ------------
// Hadi Khazaee Asl (hadi_khazaee_asl@yahoo.com)
//
//////////////////////////////////////////////////////
//
// Global Properties ...
#property copyright "Copyright 2023, SaherElm IT Center"
#property link "https://saherelm.ir"
#property version "1.00"
#property description "SaherElm X121 XDON Indicator"
#property strict
//
// Definitions ...
//
#define ShortName "X121 XDON"
//
// Includes Common Library ...
#include "../Libraries/x-saherelm.common.lib.mq5"
//
// Inputs ...
//
// Market ...
input group "Market";
input int donchainLength = 40; // Donchain Length
//
// Presentation ...
input group "Presentation";
//
input int startCalculationForLastBars = 1000; // Calculate Last n Bars
//
input bool showUpper = true; // Show Upper Band
input bool showLower = true; // Show Lower Band
//
input bool showOpen = true; // Show Open
input bool showHigh = true; // Show High
input bool showClose = true; // Show Close
input bool showLow = true; // Show Low
//
// Buffers ...
//
#define hideColorIDX 0
#define bullishColorIDX 1
#define bearishColorIDX 2
#define neuturalColorIDX 3
//
#property indicator_chart_window
//
#property indicator_buffers 8
#property indicator_plots 8
//
// Plot Buffers ...
//
// Open ...
//
#define donOpenUpperBufferIndex 0
double donOpenUpperBuffer[];
//
#define donOpenUpperPlotBufferIndex 0
#property indicator_label1 "X121 O U"
#property indicator_type1 DRAW_LINE
#property indicator_color1 clrCornflowerBlue
#property indicator_width1 1
//
#define donOpenLowerBufferIndex 1
double donOpenLowerBuffer[];
//
#define donOpenLowerPlotBufferIndex 1
#property indicator_label2 "X121 O L"
#property indicator_type2 DRAW_LINE
#property indicator_color2 clrCornflowerBlue
#property indicator_width2 1
//
// High ...
//
#define donHighUpperBufferIndex 2
double donHighUpperBuffer[];
//
#define donHighUpperPlotBufferIndex 2
#property indicator_label3 "X121 H U"
#property indicator_type3 DRAW_LINE
#property indicator_color3 clrAquamarine
#property indicator_width3 1
//
#define donHighLowerBufferIndex 3
double donHighLowerBuffer[];
//
#define donHighLowerPlotBufferIndex 3
#property indicator_label4 "X121 H L"
#property indicator_type4 DRAW_LINE
#property indicator_color4 clrAquamarine
#property indicator_width4 1
//
// Low ...
//
#define donLowUpperBufferIndex 4
double donLowUpperBuffer[];
//
#define donLowUpperPlotBufferIndex 4
#property indicator_label5 "X121 L U"
#property indicator_type5 DRAW_LINE
#property indicator_color5 clrDarkOrchid
#property indicator_width5 1
//
#define donLowLowerBufferIndex 5
double donLowLowerBuffer[];
//
#define donLowLowerPlotBufferIndex 5
#property indicator_label6 "X121 L L"
#property indicator_type6 DRAW_LINE
#property indicator_color6 clrDarkOrchid
#property indicator_width6 1
//
// Close ...
//
#define donCloseUpperBufferIndex 6
double donCloseUpperBuffer[];
//
#define donCloseUpperPlotBufferIndex 6
#property indicator_label7 "X121 C U"
#property indicator_type7 DRAW_LINE
#property indicator_color7 clrCoral
#property indicator_width7 1
//
#define donCloseLowerBufferIndex 7
double donCloseLowerBuffer[];
//
#define donCloseLowerPlotBufferIndex 7
#property indicator_label8 "X121 C L"
#property indicator_type8 DRAW_LINE
#property indicator_color8 clrCoral
#property indicator_width8 1
//
// Data Buffers ...
//
int mLastBufferIndex = 7;
//
// Variables, Properties and etc ...
//
// this counts Available Bars ...
int limit;
//
int maxLength;
//
// Event Handlers ...
/**
* Initialize Indicator ...
*
* @return ( int )
*/
int OnInit()
{
//
// Validate Inputs ...
if (!ValidateInputs())
{
return INIT_PARAMETERS_INCORRECT;
}
//
// Initialize Indicator Handlers ...
//
// because in some cases we may have more than one input for
// calculation and we must prevent any calculation
// untill we pass the biggest input length, here we get max Input length
// and then wait until pass it ...
maxLength = ExtractMaxLengthOfInputs();
//
// Define Index Buffers ...
DefineBuffers();
//
// Set Indicator ShortName ...
SetIndicatorName();
//
// Init Succeed ...
return INIT_SUCCEEDED;
}
/**
* De Initialize Indicator ...
*
* @param reason: Integer, De Initialization Reason ...
*/
void OnDeinit(const int reason)
{
//
// REASON_PROGRAM 0 The EA has stopped working calling the ExpertRemove() function
// REASON_REMOVE 1 Program removed from a chart
// REASON_RECOMPILE 2 Program recompiled
// REASON_CHARTCHANGE 3 A symbol or a chart period is changed
// REASON_CHARTCLOSE 4 Chart closed
// REASON_PARAMETERS 5 Inputs changed by a user
// REASON_ACCOUNT 6 Another account has been activated or reconnection to the trade server has occurred due to changes in the account settings
// REASON_TEMPLATE 7 Another chart template applied
// REASON_INITFAILED 8 The OnInit() handler returned a non-zero value
// REASON_CLOSE 9 Terminal closed
}
/**
* Calculate Bars ...
*
* @param rates_total: Integer, Total Bars on Chart ...
* @param prev_calculated: Integer, Total Calculated Bars on Charts ...
* @param time: DateTime Array, History of Open Time ...
* @param open: Double Array, History of Open Prices ...
* @param high: Double Array, History of High Prices ...
* @param low: Double Array, History of Low Prices ...
* @param close: Double Array, History of Close Prices ...
* @param tick_volume: Long, History of Tick Volumes on Bar ...
* @param volume: Long, History of Trade Volumes ...
* @param spread: Double, History of Spread Price ...
*
* @return ( int )
*/
int OnCalculate(
const int rates_total,
const int prev_calculated,
const datetime &time[],
const double &open[],
const double &high[],
const double &low[],
const double &close[],
const long &tick_volume[],
const long &volume[],
const int &spread[] //
)
{
//
// Prepare Buffers ...
ArraySetAsSeries(time, true);
ArraySetAsSeries(open, true);
ArraySetAsSeries(high, true);
ArraySetAsSeries(low, true);
ArraySetAsSeries(close, true);
ArraySetAsSeries(tick_volume, true);
ArraySetAsSeries(volume, true);
ArraySetAsSeries(spread, true);
//
// Validate Calculated Bars ...
//
limit = (prev_calculated > rates_total || prev_calculated <= 0)
? rates_total
: (rates_total - prev_calculated) + 1;
//
// Buffers Copy ...
//
// Main Loop ...
for (int i = limit - 1; i >= 0 && !IsStopped(); i--)
{
//
CalculateBuffers(
i,
prev_calculated,
rates_total,
//
open,
high,
close,
low,
tick_volume //
);
}
//
return rates_total;
}
//
// Functions ...
/**
* Validate Input Args for Initialization ...
*
* @return ( bool )
*/
bool ValidateInputs()
{
//
bool result = false;
//
result =
//
donchainLength > 0
//
;
//
return result;
}
/**
* Extract Max Length of Inputs ...
*
* @return ( int )
*/
int ExtractMaxLengthOfInputs()
{
//
int result = 0;
//
return result;
}
/**
* Define Required Buffers ...
*/
void DefineBuffers()
{
//
// Plot Buffers ...
//
// Open ...
//
// UPPER ...
bool canShowCOpenUpper = showUpper && showOpen;
ENUM_DRAW_TYPE donOpenUpperDrawType = canShowCOpenUpper ? DRAW_LINE : DRAW_NONE;
ArraySetAsSeries(donOpenUpperBuffer, true);
SetIndexBuffer(donOpenUpperBufferIndex, donOpenUpperBuffer, INDICATOR_DATA);
PlotIndexSetDouble(donOpenUpperPlotBufferIndex, PLOT_EMPTY_VALUE, EMPTY_VALUE);
PlotIndexSetInteger(donOpenUpperPlotBufferIndex, PLOT_SHOW_DATA, canShowCOpenUpper);
PlotIndexSetInteger(donOpenUpperPlotBufferIndex, PLOT_DRAW_TYPE, donOpenUpperDrawType);
//
// LOWER ...
bool canShowCOpenLower = showLower && showOpen;
ENUM_DRAW_TYPE donOpenLowerDrawType = canShowCOpenLower ? DRAW_LINE : DRAW_NONE;
ArraySetAsSeries(donOpenLowerBuffer, true);
SetIndexBuffer(donOpenLowerBufferIndex, donOpenLowerBuffer, INDICATOR_DATA);
PlotIndexSetDouble(donOpenLowerPlotBufferIndex, PLOT_EMPTY_VALUE, EMPTY_VALUE);
PlotIndexSetInteger(donOpenLowerPlotBufferIndex, PLOT_SHOW_DATA, canShowCOpenLower);
PlotIndexSetInteger(donOpenLowerPlotBufferIndex, PLOT_DRAW_TYPE, donOpenLowerDrawType);
//
// Close ...
//
// UPPER ...
bool canShowCCloseUpper = showUpper && showClose;
ENUM_DRAW_TYPE donCloseUpperDrawType = canShowCCloseUpper ? DRAW_LINE : DRAW_NONE;
ArraySetAsSeries(donCloseUpperBuffer, true);
SetIndexBuffer(donCloseUpperBufferIndex, donCloseUpperBuffer, INDICATOR_DATA);
PlotIndexSetDouble(donCloseUpperPlotBufferIndex, PLOT_EMPTY_VALUE, EMPTY_VALUE);
PlotIndexSetInteger(donCloseUpperPlotBufferIndex, PLOT_SHOW_DATA, canShowCCloseUpper);
PlotIndexSetInteger(donCloseUpperPlotBufferIndex, PLOT_DRAW_TYPE, donCloseUpperDrawType);
//
// LOWER ...
bool canShowCCloseLower = showLower && showClose;
ENUM_DRAW_TYPE donCloseLowerDrawType = canShowCCloseLower ? DRAW_LINE : DRAW_NONE;
ArraySetAsSeries(donCloseLowerBuffer, true);
SetIndexBuffer(donCloseLowerBufferIndex, donCloseLowerBuffer, INDICATOR_DATA);
PlotIndexSetDouble(donCloseLowerPlotBufferIndex, PLOT_EMPTY_VALUE, EMPTY_VALUE);
PlotIndexSetInteger(donCloseLowerPlotBufferIndex, PLOT_SHOW_DATA, canShowCCloseLower);
PlotIndexSetInteger(donCloseLowerPlotBufferIndex, PLOT_DRAW_TYPE, donCloseLowerDrawType);
//
// High ...
//
// UPPER ...
bool canShowCHighUpper = showUpper && showHigh;
ENUM_DRAW_TYPE donHighUpperDrawType = canShowCHighUpper ? DRAW_LINE : DRAW_NONE;
ArraySetAsSeries(donHighUpperBuffer, true);
SetIndexBuffer(donHighUpperBufferIndex, donHighUpperBuffer, INDICATOR_DATA);
PlotIndexSetDouble(donHighUpperPlotBufferIndex, PLOT_EMPTY_VALUE, EMPTY_VALUE);
PlotIndexSetInteger(donHighUpperPlotBufferIndex, PLOT_SHOW_DATA, canShowCHighUpper);
PlotIndexSetInteger(donHighUpperPlotBufferIndex, PLOT_DRAW_TYPE, donHighUpperDrawType);
//
// LOWER ...
bool canShowCHighLower = showLower && showHigh;
ENUM_DRAW_TYPE donHighLowerDrawType = canShowCHighLower ? DRAW_LINE : DRAW_NONE;
ArraySetAsSeries(donHighLowerBuffer, true);
SetIndexBuffer(donHighLowerBufferIndex, donHighLowerBuffer, INDICATOR_DATA);
PlotIndexSetDouble(donHighLowerPlotBufferIndex, PLOT_EMPTY_VALUE, EMPTY_VALUE);
PlotIndexSetInteger(donHighLowerPlotBufferIndex, PLOT_SHOW_DATA, canShowCHighLower);
PlotIndexSetInteger(donHighLowerPlotBufferIndex, PLOT_DRAW_TYPE, donHighLowerDrawType);
//
// Low ...
//
// UPPER ...
bool canShowCLowUpper = showUpper && showLow;
ENUM_DRAW_TYPE donLowUpperDrawType = canShowCLowUpper ? DRAW_LINE : DRAW_NONE;
ArraySetAsSeries(donLowUpperBuffer, true);
SetIndexBuffer(donLowUpperBufferIndex, donLowUpperBuffer, INDICATOR_DATA);
PlotIndexSetDouble(donLowUpperPlotBufferIndex, PLOT_EMPTY_VALUE, EMPTY_VALUE);
PlotIndexSetInteger(donLowUpperPlotBufferIndex, PLOT_SHOW_DATA, canShowCLowUpper);
PlotIndexSetInteger(donLowUpperPlotBufferIndex, PLOT_DRAW_TYPE, donLowUpperDrawType);
//
// LOWER ...
bool canShowCLowLower = showLower && showLow;
ENUM_DRAW_TYPE donLowLowerDrawType = canShowCLowLower ? DRAW_LINE : DRAW_NONE;
ArraySetAsSeries(donLowLowerBuffer, true);
SetIndexBuffer(donLowLowerBufferIndex, donLowLowerBuffer, INDICATOR_DATA);
PlotIndexSetDouble(donLowLowerPlotBufferIndex, PLOT_EMPTY_VALUE, EMPTY_VALUE);
PlotIndexSetInteger(donLowLowerPlotBufferIndex, PLOT_SHOW_DATA, canShowCLowLower);
PlotIndexSetInteger(donLowLowerPlotBufferIndex, PLOT_DRAW_TYPE, donLowLowerDrawType);
//
// Data Buffers ...
}
/**
* Set Indicator Short Name and also we can define Buffers Labels ...
*/
void SetIndicatorName()
{
IndicatorSetString(INDICATOR_SHORTNAME, ShortName);
}
/**
* Calculate Custom Buffers ...
*
* @param bar_index: Integer, Represent Current Bar ...
* @param prevCalculated: Integer, Represent Previous Calculated Bars ...
* @param ratesTotal: Integer, Represents All Available Bars ...
* @param open: Double Array, History of Open Prices ...
* @param high: Double Array, History of High Prices ...
* @param close: Double Array, History of Close Prices ...
* @param low: Double Array, History of Low Prices ...
* @param tickVolume: Long, History of Tick Volumes on Bar ...
*/
void CalculateBuffers(
int bar_index, // Selected Bar Index
const int prevCalculated,
const int ratesTotal,
const double &open[],
const double &high[],
const double &close[],
const double &low[],
const long &tickVolume[] //
)
{
//
int barsLimit = startCalculationForLastBars > 0
? startCalculationForLastBars
: 0;
//
// bool canCalculate = true;
bool canCalculate =
barsLimit == 0 ||
bar_index <= barsLimit;
if (canCalculate)
{
//
CalculateDonchains(
bar_index,
prevCalculated,
ratesTotal,
open,
high,
close,
low,
tickVolume //
);
}
else
{
FillBuffersZero(bar_index);
}
}
//
// Custom ...
/**
* Fill All Bufers to Zero Vlue for Specified Bar Index ...
*
* @param barIndex: Integer ...
*/
void FillBuffersZero(int barIndex)
{
//
donOpenUpperBuffer[barIndex] = EMPTY_VALUE;
donOpenLowerBuffer[barIndex] = EMPTY_VALUE;
donCloseUpperBuffer[barIndex] = EMPTY_VALUE;
donCloseLowerBuffer[barIndex] = EMPTY_VALUE;
donHighUpperBuffer[barIndex] = EMPTY_VALUE;
donHighLowerBuffer[barIndex] = EMPTY_VALUE;
donLowUpperBuffer[barIndex] = EMPTY_VALUE;
donLowLowerBuffer[barIndex] = EMPTY_VALUE;
}
/**
* Calculate Donchain ...
*
* @param bar_index: Integer, Bar Index ...
* @param prevCalculated: Integer, Represent Previous Calculated Bars ...
* @param ratesTotal: Integer, Represents All Available Bars ...
* @param open: Double Array, History of Open Prices ...
* @param high: Double Array, History of High Prices ...
* @param close: Double Array, History of Close Prices ...
* @param low: Double Array, History of Low Prices ...
* @param tickVolume: Long, History of Tick Volumes on Bar ...
* @param _length: Integer, Donchain Length ...
* @param _ouBuffer: Double Array Reference ...
* @param _olBuffer: Double Array Reference ...
* @param _huBuffer: Double Array Reference ...
* @param _hlBuffer: Double Array Reference ...
* @param _luBuffer: Double Array Reference ...
* @param _llBuffer: Double Array Reference ...
* @param _cuBuffer: Double Array Reference ...
* @param _clBuffer: Double Array Reference ...
*/
void CalculateDonchain(
int bar_index, // Selected Bar Index
const int prevCalculated,
const int ratesTotal,
//
const double &open[],
const double &high[],
const double &close[],
const double &low[],
const long &tickVolume[],
//
int _length,
//
double &_ouBuffer[],
double &_olBuffer[],
double &_huBuffer[],
double &_hlBuffer[],
double &_luBuffer[],
double &_llBuffer[],
double &_cuBuffer[],
double &_clBuffer[] //
)
{
//
double iUpper = 0;
double iLower = 0;
//
// OPEN ...
//
int from = bar_index;
//
iUpper = open[ArrayMaximum(open, from, _length)];
iLower = open[ArrayMinimum(open, from, _length)];
//
_ouBuffer[bar_index] = iUpper;
_olBuffer[bar_index] = iLower;
//
// HIGH ...
//
iUpper = high[ArrayMaximum(high, from, _length)];
iLower = high[ArrayMinimum(high, from, _length)];
//
_huBuffer[bar_index] = iUpper;
_hlBuffer[bar_index] = iLower;
//
// LOW ...
//
iUpper = low[ArrayMaximum(low, from, _length)];
iLower = low[ArrayMinimum(low, from, _length)];
//
_luBuffer[bar_index] = iUpper;
_llBuffer[bar_index] = iLower;
//
// CLOSE ...
//
iUpper = close[ArrayMaximum(close, from, _length)];
iLower = close[ArrayMinimum(close, from, _length)];
//
_cuBuffer[bar_index] = iUpper;
_clBuffer[bar_index] = iLower;
}
/**
* Calculate Different VWaps ...
*
* @param bar_index: Integer, Represent Current Bar ...
* @param prevCalculated: Integer, Represent Previous Calculated Bars ...
* @param ratesTotal: Integer, Represents All Available Bars ...
* @param open: Double Array, History of Open Prices ...
* @param high: Double Array, History of High Prices ...
* @param close: Double Array, History of Close Prices ...
* @param low: Double Array, History of Low Prices ...
* @param tickVolume: Long, History of Tick Volumes on Bar ...
*/
void CalculateDonchains(
int bar_index, // Selected Bar Index
const int prevCalculated,
const int ratesTotal,
const double &open[],
const double &high[],
const double &close[],
const double &low[],
const long &tickVolume[] //
)
{
//
CalculateDonchain(
//
bar_index,
prevCalculated,
ratesTotal,
//
open,
high,
close,
low,
tickVolume,
//
donchainLength,
//
donOpenUpperBuffer,
donOpenLowerBuffer,
donHighUpperBuffer,
donHighLowerBuffer,
donLowUpperBuffer,
donLowLowerBuffer,
donCloseUpperBuffer,
donCloseLowerBuffer //
);
}
//
File diff suppressed because it is too large Load Diff
+623
View File
@@ -0,0 +1,623 @@
///////////////////////////////////////////////////////
//
// SaherElm IT Center MQL5 Indicator
// -------------------------------------------------
// Name: X121 XSTR
// Description: XSTR ...
//
//
// Maintainer:
// ------------
// Hadi Khazaee Asl (hadi_khazaee_asl@yahoo.com)
//
//////////////////////////////////////////////////////
//
// Global Properties ...
#property copyright "Copyright 2023, SaherElm IT Center"
#property link "https://saherelm.ir"
#property version "1.00"
#property description "SaherElm X121 XSTR Indicator"
#property strict
//
// Definitions ...
//
#define ShortName "X121 XSTR"
//
// Includes Common Library ...
#include "../Classes/x-saherelm.x-poi.class.mq5"
#include "../Classes/x-saherelm.x-poi.drawer.class.mq5"
#include "../Libraries/x-saherelm.common.lib.mq5"
//
// Inputs ...
//
// Market ...
input group "Market";
input int strLength = 14; // Length
input double strMultiplier = 3; // Multiplier
input ENUM_APPLIED_PRICE strAppliedTo = PRICE_MEDIAN; // Applied To
//
input group "Presentation";
//
input bool showStr = true; // Show Str
//
// Buffers ...
//
#define hideColorIDX 0
#define bullishColorIDX 1
#define bearishColorIDX 2
#define neuturalColorIDX 3
//
#property indicator_chart_window
//
#property indicator_buffers 7
#property indicator_plots 1
//
// STR ...
#define strBufferIndex 0
double strBuffer[];
#define strColorBufferIndex 1
double strColorBuffer[];
//
#define strPlotBufferIndex 1
#property indicator_label1 "X121 STR"
#property indicator_type1 DRAW_COLOR_LINE
#property indicator_color1 CLR_NONE, clrLime, clrRed, clrGray
#property indicator_style1 STYLE_SOLID
#property indicator_width1 2
//
// Data Buffers ...
//
int mLastBufferIndex = 1;
//
// STR ...
//
// Atr ...
#define atrBufferIndex mLastBufferIndex + 1
double atrBuffer[];
//
// Price ...
#define strPriceBufferIndex mLastBufferIndex + 2
double strPriceBuffer[];
//
// Up ...
#define strUpBufferIndex mLastBufferIndex + 3
double strUpBuffer[];
//
// Down ...
#define strDownBufferIndex mLastBufferIndex + 4
double strDownBuffer[];
//
// Trend ...
#define strStateBufferIndex mLastBufferIndex + 5
double strStateBuffer[];
//
// Variables, Properties and etc ...
//
// this counts Available Bars ...
int limit;
//
int maxLength;
//
// ATR Handler ...
int atrHandler = INVALID_HANDLE;
//
bool isStrTrendChanged;
bool isStrStartBearishTrend;
bool isStrStartBullishTrend;
//
int changeOfTrend;
int startBearishTrend;
int startBullishTrend;
//
// Event Handlers ...
/**
* Initialize Indicator ...
*
* @return ( int )
*/
int OnInit()
{
//
// Validate Inputs ...
if (!ValidateInputs())
{
return INIT_PARAMETERS_INCORRECT;
}
//
// Initialize Indicator Handlers ...
//
// ATR ...
atrHandler = iATR(
_Symbol,
_Period,
strLength //
);
bool isInited = atrHandler != INVALID_HANDLE;
if (!isInited)
{
return INIT_FAILED;
}
//
// because in some cases we may have more than one input for
// calculation and we must prevent any calculation
// untill we pass the biggest input length, here we get max Input length
// and then wait until pass it ...
maxLength = ExtractMaxLengthOfInputs();
//
// Define Index Buffers ...
DefineBuffers();
//
// Set Indicator ShortName ...
SetIndicatorName();
//
// Init Succeed ...
return INIT_SUCCEEDED;
}
/**
* De Initialize Indicator ...
*
* @param reason: Integer, De Initialization Reason ...
*/
void OnDeinit(const int reason)
{
//
// REASON_PROGRAM 0 The EA has stopped working calling the ExpertRemove() function
// REASON_REMOVE 1 Program removed from a chart
// REASON_RECOMPILE 2 Program recompiled
// REASON_CHARTCHANGE 3 A symbol or a chart period is changed
// REASON_CHARTCLOSE 4 Chart closed
// REASON_PARAMETERS 5 Inputs changed by a user
// REASON_ACCOUNT 6 Another account has been activated or reconnection to the trade server has occurred due to changes in the account settings
// REASON_TEMPLATE 7 Another chart template applied
// REASON_INITFAILED 8 The OnInit() handler returned a non-zero value
// REASON_CLOSE 9 Terminal closed
//
IndicatorRelease(atrHandler);
}
/**
* Calculate Bars ...
*
* @param rates_total: Integer, Total Bars on Chart ...
* @param prev_calculated: Integer, Total Calculated Bars on Charts ...
* @param time: DateTime Array, History of Open Time ...
* @param open: Double Array, History of Open Prices ...
* @param high: Double Array, History of High Prices ...
* @param low: Double Array, History of Low Prices ...
* @param close: Double Array, History of Close Prices ...
* @param tick_volume: Long, History of Tick Volumes on Bar ...
* @param volume: Long, History of Trade Volumes ...
* @param spread: Double, History of Spread Price ...
*
* @return ( int )
*/
int OnCalculate(
const int rates_total,
const int prev_calculated,
const datetime &time[],
const double &open[],
const double &high[],
const double &low[],
const double &close[],
const long &tick_volume[],
const long &volume[],
const int &spread[] //
)
{
//
// Prepare Buffers ...
//
// Fill All Buffers by Zero ...
if (prev_calculated == 0)
{
}
//
int numOfRequiredAtrs = (prev_calculated > rates_total || prev_calculated < 0)
? rates_total
: rates_total - prev_calculated;
if (prev_calculated > 0)
{
numOfRequiredAtrs++;
}
//
// Validate Calculated Bars ...
//
// ATR ...
int atrCalculatedBars = BarsCalculated(atrHandler);
//
bool isPassedRequiredCalculatedBars =
//
// ATR ...
atrCalculatedBars >= numOfRequiredAtrs
//
;
if (!isPassedRequiredCalculatedBars)
{
return prev_calculated;
}
//
//
// checking for the limit start of calculation of an indicator ...
limit =
(prev_calculated > rates_total || prev_calculated <= 0)
? maxLength
: prev_calculated - 1;
//
// Buffers Copy ...
//
// ATR ...
int copiedAtrs = CopyBuffer(atrHandler, 0, 0, numOfRequiredAtrs, atrBuffer);
//
// Validate Copied Items ...
bool isPassedRequiredCopiedItems =
//
// ATR ...
copiedAtrs >= numOfRequiredAtrs
//
;
if (!isPassedRequiredCopiedItems)
{
return prev_calculated;
}
//
// Main Loop ...
for (int i = limit; i < rates_total && !IsStopped(); i++)
{
//
CalculateBuffers(
i,
prev_calculated,
rates_total,
//
open,
high,
close,
low,
tick_volume //
);
}
//
return rates_total;
}
//
// Functions ...
/**
* Validate Input Args for Initialization ...
*
* @return ( bool )
*/
bool ValidateInputs()
{
//
bool result =
//
// STR ...
strLength > 0 &&
strMultiplier > 0
//
;
//
return result;
}
/**
* Extract Max Length of Inputs ...
*
* @return ( int )
*/
int ExtractMaxLengthOfInputs()
{
//
int result = 0;
//
result = MathMax(1, strLength);
//
return result;
}
/**
* Define Required Buffers ...
*/
void DefineBuffers()
{
//
// STR ...
SetIndexBuffer(strBufferIndex, strBuffer, INDICATOR_DATA);
SetIndexBuffer(strColorBufferIndex, strColorBuffer, INDICATOR_COLOR_INDEX);
//
// Data Buffers ...
//
// STR ...
//
SetIndexBuffer(strUpBufferIndex, strUpBuffer, INDICATOR_CALCULATIONS);
SetIndexBuffer(strDownBufferIndex, strDownBuffer, INDICATOR_CALCULATIONS);
SetIndexBuffer(strPriceBufferIndex, strPriceBuffer, INDICATOR_CALCULATIONS);
SetIndexBuffer(strStateBufferIndex, strStateBuffer, INDICATOR_CALCULATIONS);
}
/**
* Set Indicator Short Name and also we can define Buffers Labels ...
*/
void SetIndicatorName()
{
IndicatorSetString(INDICATOR_SHORTNAME, ShortName);
}
/**
* Calculate Custom Buffers ...
*
* @param bar_index: Integer, Represent Current Bar ...
* @param prevCalculated: Integer, Represent Previous Calculated Bars ...
* @param ratesTotal: Integer, Represents All Available Bars ...
* @param open: Double Array, History of Open Prices ...
* @param high: Double Array, History of High Prices ...
* @param close: Double Array, History of Close Prices ...
* @param low: Double Array, History of Low Prices ...
* @param tickVolume: Long, History of Tick Volumes on Bar ...
*/
void CalculateBuffers(
int bar_index, // Selected Bar Index
const int prevCalculated,
const int ratesTotal,
const double &open[],
const double &high[],
const double &close[],
const double &low[],
const long &tickVolume[] //
)
{
//
CalculateStr(
bar_index,
prevCalculated,
ratesTotal,
open,
high,
close,
low //
);
}
//
// Custom ...
/**
* Fill All Bufers to Zero Vlue for Specified Bar Index ...
*
* @param barIndex: Integer ...
*/
void FillBuffersZero(int barIndex)
{
//
// STR ...
strBuffer[barIndex] = 0;
strUpBuffer[barIndex] = 0;
strDownBuffer[barIndex] = 0;
strPriceBuffer[barIndex] = 0;
strColorBuffer[barIndex] = hideColorIDX;
strStateBuffer[barIndex] = hideColorIDX;
}
/**
* Calculate STR ...
*
* @param bar_index: Integer, Represent Current Bar ...
* @param prevCalculated: Integer, Represent Previous Calculated Bars ...
* @param ratesTotal: Integer, Represents All Available Bars ...
* @param open: Double Array, History of Open Prices ...
* @param high: Double Array, History of High Prices ...
* @param close: Double Array, History of Close Prices ...
* @param low: Double Array, History of Low Prices ...
*/
void CalculateStr(
int bar_index, // Selected Bar Index
const int prevCalculated,
const int ratesTotal,
const double &open[],
const double &high[],
const double &close[],
const double &low[] //
)
{
//
// Calculated Price ...
double price = GetAppliedPrice(
strAppliedTo,
open,
high,
low,
close,
bar_index //
);
strPriceBuffer[bar_index] = price;
//
double atr = atrBuffer[bar_index];
//
// Up ...
strUpBuffer[bar_index] = price + (strMultiplier * atr);
//
// Down ...
strDownBuffer[bar_index] = price - (strMultiplier * atr);
//
if (close[bar_index] > strUpBuffer[bar_index - 1])
{
//
strStateBuffer[bar_index] = 1;
if (strStateBuffer[bar_index - 1] == -1)
{
changeOfTrend = 1;
}
}
else if (close[bar_index] < strDownBuffer[bar_index - 1])
{
//
strStateBuffer[bar_index] = -1;
if (strStateBuffer[bar_index - 1] == 1)
{
changeOfTrend = 1;
}
}
else if (strStateBuffer[bar_index - 1] == 1)
{
//
strStateBuffer[bar_index] = 1;
changeOfTrend = 0;
}
else if (strStateBuffer[bar_index - 1] == -1)
{
//
strStateBuffer[bar_index] = -1;
changeOfTrend = 0;
}
//
// Down Trend Starting ...
if (strStateBuffer[bar_index] < 0 && strStateBuffer[bar_index - 1] > 0)
{
startBearishTrend = 1;
}
else
{
startBearishTrend = 0;
}
//
// Up Trend Starting ...
if (strStateBuffer[bar_index] > 0 && strStateBuffer[bar_index - 1] < 0)
{
startBullishTrend = 1;
}
else
{
startBullishTrend = 0;
}
//
if (strStateBuffer[bar_index] > 0 && strDownBuffer[bar_index] < strDownBuffer[bar_index - 1])
{
strDownBuffer[bar_index] = strDownBuffer[bar_index - 1];
}
//
if (strStateBuffer[bar_index] < 0 && strUpBuffer[bar_index] > strUpBuffer[bar_index - 1])
{
strUpBuffer[bar_index] = strUpBuffer[bar_index - 1];
}
//
if (startBearishTrend == 1)
{
strUpBuffer[bar_index] = strPriceBuffer[bar_index] + (strMultiplier * atr);
}
//
if (startBullishTrend == 1)
{
strDownBuffer[bar_index] = strPriceBuffer[bar_index] - (strMultiplier * atr);
}
//
// Draw the indicator ...
//
double colorIDX = hideColorIDX;
strColorBuffer[bar_index] = colorIDX;
//
if (strStateBuffer[bar_index] == 1)
{
//
strBuffer[bar_index] = strDownBuffer[bar_index];
if (changeOfTrend == 1)
{
strBuffer[bar_index - 1] = strBuffer[bar_index - 2];
changeOfTrend = 0;
}
//
colorIDX = bullishColorIDX;
}
else if (strStateBuffer[bar_index] == -1)
{
//
strBuffer[bar_index] = strUpBuffer[bar_index];
if (changeOfTrend == 1)
{
//
strBuffer[bar_index - 1] = strBuffer[bar_index - 2];
changeOfTrend = 0;
}
//
colorIDX = bearishColorIDX;
}
//
if (showStr)
{
strColorBuffer[bar_index] = colorIDX;
}
}
//
+711
View File
@@ -0,0 +1,711 @@
///////////////////////////////////////////////////////
//
// SaherElm IT Center MQL5 Indicator
// -------------------------------------------------
// Name: X121 XVWAP
// Description: XVWAP ...
//
//
// Maintainer:
// ------------
// Hadi Khazaee Asl (hadi_khazaee_asl@yahoo.com)
//
//////////////////////////////////////////////////////
//
// Global Properties ...
#property copyright "Copyright 2023, SaherElm IT Center"
#property link "https://saherelm.ir"
#property version "1.00"
#property description "SaherElm X121 XVWAP Indicator"
#property strict
//
// Definitions ...
//
#define ShortName "X121 XVWAP"
//
// Includes Common Library ...
#include "../Libraries/x-saherelm.common.lib.mq5"
//
// Inputs ...
//
// Market ...
input group "Market";
input int vwapFastLength = 20; // Fast Length
input int vwapMidLength = 40; // Mid Length
input int vwapSlowLength = 60; // Slow Length
input ENUM_APPLIED_PRICE vwapAppliedTo = PRICE_CLOSE; // Applied To
//
// Presentation ...
input group "Presentation";
//
input int startCalculationForLastBars = 1000; // Calculate Last n Bars
//
input bool showVWapFast = true; // Show VWap Fast
input bool showVWapMedium = true; // Show VWap Medium
input bool showVWapSlow = true; // Show VWap Slow
//
// Buffers ...
//
#define hideColorIDX 0
#define bullishColorIDX 1
#define bearishColorIDX 2
#define neuturalColorIDX 3
//
#property indicator_chart_window
//
#property indicator_buffers 11
#property indicator_plots 3
//
// Plot Buffers ...
//
// FAST ...
#define vwapFastBufferIndex 0
double vwapFastBuffer[];
#define vwapFastColorBufferIndex 1
double vwapFastColorBuffer[];
//
#define vwapFastPlotBufferIndex 0
#property indicator_label1 "X121 VWF"
#property indicator_type1 DRAW_COLOR_LINE
#property indicator_color1 CLR_NONE, clrAqua, clrMagenta, clrGray
#property indicator_style1 STYLE_SOLID
#property indicator_width1 2
//
// MID ...
#define vwapMidBufferIndex 2
double vwapMidBuffer[];
#define vwapMidColorBufferIndex 3
double vwapMidColorBuffer[];
//
#define vwapMidPlotBufferIndex 1
#property indicator_label2 "X121 VWM"
#property indicator_type2 DRAW_COLOR_LINE
#property indicator_color2 CLR_NONE, clrGreen, clrRed, clrGray
#property indicator_style2 STYLE_SOLID
#property indicator_width2 2
//
// SLOW ...
#define vwapSlowBufferIndex 4
double vwapSlowBuffer[];
#define vwapSlowColorBufferIndex 5
double vwapSlowColorBuffer[];
//
#define vwapSlowPlotBufferIndex 2
#property indicator_label3 "X121 VWS"
#property indicator_type3 DRAW_COLOR_LINE
#property indicator_color3 CLR_NONE, clrSteelBlue, clrDarkRed, clrGray
#property indicator_style3 STYLE_SOLID
#property indicator_width3 2
//
// Data Buffers ...
//
int mLastBufferIndex = 5;
//
// Volume ...
#define vwapVolumeBufferIndex mLastBufferIndex + 1
double vwapVolumeBuffer[];
//
// Price ...
#define vwapPriceBufferIndex mLastBufferIndex + 2
double vwapPriceBuffer[];
//
// Fast State ...
#define vwapFastStateBufferIndex mLastBufferIndex + 3
double vwapFastStateBuffer[];
//
// Mid State ...
#define vwapMidStateBufferIndex mLastBufferIndex + 4
double vwapMidStateBuffer[];
//
// Slow State ...
#define vwapSlowStateBufferIndex mLastBufferIndex + 5
double vwapSlowStateBuffer[];
//
// Variables, Properties and etc ...
//
// this counts Available Bars ...
int limit;
//
int maxLength;
//
// Event Handlers ...
/**
* Initialize Indicator ...
*
* @return ( int )
*/
int OnInit()
{
//
// Validate Inputs ...
if (!ValidateInputs())
{
return INIT_PARAMETERS_INCORRECT;
}
//
// Initialize Indicator Handlers ...
//
// because in some cases we may have more than one input for
// calculation and we must prevent any calculation
// untill we pass the biggest input length, here we get max Input length
// and then wait until pass it ...
maxLength = ExtractMaxLengthOfInputs();
//
// Define Index Buffers ...
DefineBuffers();
//
// Set Indicator ShortName ...
SetIndicatorName();
//
// Init Succeed ...
return INIT_SUCCEEDED;
}
/**
* De Initialize Indicator ...
*
* @param reason: Integer, De Initialization Reason ...
*/
void OnDeinit(const int reason)
{
//
// REASON_PROGRAM 0 The EA has stopped working calling the ExpertRemove() function
// REASON_REMOVE 1 Program removed from a chart
// REASON_RECOMPILE 2 Program recompiled
// REASON_CHARTCHANGE 3 A symbol or a chart period is changed
// REASON_CHARTCLOSE 4 Chart closed
// REASON_PARAMETERS 5 Inputs changed by a user
// REASON_ACCOUNT 6 Another account has been activated or reconnection to the trade server has occurred due to changes in the account settings
// REASON_TEMPLATE 7 Another chart template applied
// REASON_INITFAILED 8 The OnInit() handler returned a non-zero value
// REASON_CLOSE 9 Terminal closed
}
/**
* Calculate Bars ...
*
* @param rates_total: Integer, Total Bars on Chart ...
* @param prev_calculated: Integer, Total Calculated Bars on Charts ...
* @param time: DateTime Array, History of Open Time ...
* @param open: Double Array, History of Open Prices ...
* @param high: Double Array, History of High Prices ...
* @param low: Double Array, History of Low Prices ...
* @param close: Double Array, History of Close Prices ...
* @param tick_volume: Long, History of Tick Volumes on Bar ...
* @param volume: Long, History of Trade Volumes ...
* @param spread: Double, History of Spread Price ...
*
* @return ( int )
*/
int OnCalculate(
const int rates_total,
const int prev_calculated,
const datetime &time[],
const double &open[],
const double &high[],
const double &low[],
const double &close[],
const long &tick_volume[],
const long &volume[],
const int &spread[] //
)
{
//
// Prepare Buffers ...
ArraySetAsSeries(time, true);
ArraySetAsSeries(open, true);
ArraySetAsSeries(high, true);
ArraySetAsSeries(low, true);
ArraySetAsSeries(close, true);
ArraySetAsSeries(tick_volume, true);
ArraySetAsSeries(volume, true);
ArraySetAsSeries(spread, true);
//
// Validate Calculated Bars ...
//
limit = (prev_calculated > rates_total || prev_calculated <= 0)
? rates_total
: (rates_total - prev_calculated) + 1;
//
// Buffers Copy ...
//
// Main Loop ...
for (int i = limit - 1; i >= 0 && !IsStopped(); i--)
{
//
CalculateBuffers(
i,
prev_calculated,
rates_total,
//
open,
high,
close,
low,
tick_volume //
);
}
//
return rates_total;
}
//
// Functions ...
/**
* Validate Input Args for Initialization ...
*
* @return ( bool )
*/
bool ValidateInputs()
{
//
bool result = false;
//
result =
//
vwapFastLength > 2 &&
vwapMidLength > vwapFastLength &&
vwapSlowLength > vwapMidLength
//
;
//
return result;
}
/**
* Extract Max Length of Inputs ...
*
* @return ( int )
*/
int ExtractMaxLengthOfInputs()
{
//
int result = 0;
//
// XVWAP ...
result = MathMax(result, vwapFastLength);
result = MathMax(result, vwapMidLength);
result = MathMax(result, vwapSlowLength);
//
return result;
}
/**
* Define Required Buffers ...
*/
void DefineBuffers()
{
//
// Plot Buffers ...
//
// Fast ...
ArraySetAsSeries(vwapFastBuffer, true);
ArraySetAsSeries(vwapFastColorBuffer, true);
SetIndexBuffer(vwapFastBufferIndex, vwapFastBuffer, INDICATOR_DATA);
SetIndexBuffer(vwapFastColorBufferIndex, vwapFastColorBuffer, INDICATOR_COLOR_INDEX);
//
// Mid ...
ArraySetAsSeries(vwapMidBuffer, true);
ArraySetAsSeries(vwapMidColorBuffer, true);
SetIndexBuffer(vwapMidBufferIndex, vwapMidBuffer, INDICATOR_DATA);
SetIndexBuffer(vwapMidColorBufferIndex, vwapMidColorBuffer, INDICATOR_COLOR_INDEX);
//
// Slow ...
ArraySetAsSeries(vwapSlowBuffer, true);
ArraySetAsSeries(vwapSlowColorBuffer, true);
SetIndexBuffer(vwapSlowBufferIndex, vwapSlowBuffer, INDICATOR_DATA);
SetIndexBuffer(vwapSlowColorBufferIndex, vwapSlowColorBuffer, INDICATOR_COLOR_INDEX);
//
// Data Buffers ...
//
// Volumes ...
ArraySetAsSeries(vwapVolumeBuffer, true);
SetIndexBuffer(vwapVolumeBufferIndex, vwapVolumeBuffer, INDICATOR_CALCULATIONS);
//
// Price ...
ArraySetAsSeries(vwapPriceBuffer, true);
SetIndexBuffer(vwapPriceBufferIndex, vwapPriceBuffer, INDICATOR_CALCULATIONS);
//
// Fast State ...
ArraySetAsSeries(vwapFastStateBuffer, true);
SetIndexBuffer(vwapFastStateBufferIndex, vwapFastStateBuffer, INDICATOR_CALCULATIONS);
//
// Mid State ...
ArraySetAsSeries(vwapMidStateBuffer, true);
SetIndexBuffer(vwapMidStateBufferIndex, vwapMidStateBuffer, INDICATOR_CALCULATIONS);
//
// Slow State ...
ArraySetAsSeries(vwapSlowStateBuffer, true);
SetIndexBuffer(vwapSlowStateBufferIndex, vwapSlowStateBuffer, INDICATOR_CALCULATIONS);
}
/**
* Set Indicator Short Name and also we can define Buffers Labels ...
*/
void SetIndicatorName()
{
IndicatorSetString(INDICATOR_SHORTNAME, ShortName);
}
/**
* Calculate Custom Buffers ...
*
* @param bar_index: Integer, Represent Current Bar ...
* @param prevCalculated: Integer, Represent Previous Calculated Bars ...
* @param ratesTotal: Integer, Represents All Available Bars ...
* @param open: Double Array, History of Open Prices ...
* @param high: Double Array, History of High Prices ...
* @param close: Double Array, History of Close Prices ...
* @param low: Double Array, History of Low Prices ...
* @param tickVolume: Long, History of Tick Volumes on Bar ...
*/
void CalculateBuffers(
int bar_index, // Selected Bar Index
const int prevCalculated,
const int ratesTotal,
const double &open[],
const double &high[],
const double &close[],
const double &low[],
const long &tickVolume[] //
)
{
//
int barsLimit = startCalculationForLastBars > 0
? startCalculationForLastBars
: 0;
//
// bool canCalculate = true;
bool canCalculate =
barsLimit == 0 ||
bar_index <= barsLimit;
if (canCalculate)
{
//
// Calculate Required VWAP Data Buffers ...
if (ratesTotal - bar_index <= maxLength)
{
//
CalculateVWAPDataBuffers(
bar_index,
prevCalculated,
ratesTotal,
open,
high,
close,
low,
tickVolume //
);
//
// Prevent Moving Forward ...
}
else
{
//
CalculateVWAPDataBuffers(
bar_index,
prevCalculated,
ratesTotal,
open,
high,
close,
low,
tickVolume //
);
//
CalculateVWAPS(
bar_index,
prevCalculated,
ratesTotal,
open,
high,
close,
low,
tickVolume //
);
}
}
else
{
FillBuffersZero(bar_index);
}
}
//
// Custom ...
/**
* Fill All Bufers to Zero Vlue for Specified Bar Index ...
*
* @param barIndex: Integer ...
*/
void FillBuffersZero(int barIndex)
{
//
vwapFastBuffer[barIndex] = EMPTY_VALUE;
vwapMidBuffer[barIndex] = EMPTY_VALUE;
vwapSlowBuffer[barIndex] = EMPTY_VALUE;
vwapVolumeBuffer[barIndex] = EMPTY_VALUE;
vwapPriceBuffer[barIndex] = EMPTY_VALUE;
//
vwapFastColorBuffer[barIndex] = hideColorIDX;
vwapMidColorBuffer[barIndex] = hideColorIDX;
vwapSlowColorBuffer[barIndex] = hideColorIDX;
vwapFastStateBuffer[barIndex] = hideColorIDX;
vwapMidStateBuffer[barIndex] = hideColorIDX;
vwapSlowStateBuffer[barIndex] = hideColorIDX;
}
/**
* Calculate VWAP Value for Specified Bar ...
*
* @param bar_index: Integer, Represent Current Bar ...
* @param prevCalculated: Integer, Represent Previous Calculated Bars ...
* @param ratesTotal: Integer, Represents All Available Bars ...
* @param open: Double Array, History of Open Prices ...
* @param high: Double Array, History of High Prices ...
* @param close: Double Array, History of Close Prices ...
* @param low: Double Array, History of Low Prices ...
* @param tickVolume: Long, History of Tick Volumes on Bar ...
* @param _length: Integer, Specified VWAP Length ...
* @param _show: Boolean, Specified Show Buffer or not ...
* @param _buffer: Double Array Reference, Points to Buffer ...
* @param _colorBuffer: Double Array Reference, Points to Color Buffer ...
* @param _stateBuffer: Double Array Reference, Points to State Buffer ...
*/
void CalculateVWAP(
int bar_index, // Selected Bar Index
const int prevCalculated,
const int ratesTotal,
const double &open[],
const double &high[],
const double &close[],
const double &low[],
const long &tickVolume[],
//
int _length, // Calculation Length
bool _show,
double &_buffer[],
double &_colorBuffer[],
double &_stateBuffer[] //
)
{
//
double vSum = 0;
double pSum = 0;
double mSum = 0;
for (int x = 0; x < _length; x++)
{
//
pSum += vwapPriceBuffer[x + bar_index];
vSum += vwapVolumeBuffer[x + bar_index];
mSum += vwapPriceBuffer[x + bar_index] * vwapVolumeBuffer[x + bar_index];
}
//
double iValue = mSum / vSum;
iValue = NormalizeDouble(iValue, _Digits);
//
_buffer[bar_index] = iValue;
//
bool isBullish = low[bar_index] > iValue;
bool isBearish = high[bar_index] < iValue;
//
double iColor =
isBullish
? bullishColorIDX
: isBearish
? bearishColorIDX
: neuturalColorIDX;
//
_colorBuffer[bar_index] = hideColorIDX;
_stateBuffer[bar_index] = iColor;
if (_show)
{
_colorBuffer[bar_index] = iColor;
}
}
/**
* Calculate VWAP Required Data Buffers ...
*
* @param bar_index: Integer, Represent Current Bar ...
* @param prevCalculated: Integer, Represent Previous Calculated Bars ...
* @param ratesTotal: Integer, Represents All Available Bars ...
* @param open: Double Array, History of Open Prices ...
* @param high: Double Array, History of High Prices ...
* @param close: Double Array, History of Close Prices ...
* @param low: Double Array, History of Low Prices ...
* @param tickVolume: Long, History of Tick Volumes on Bar ...
*/
void CalculateVWAPDataBuffers(
int bar_index, // Selected Bar Index
const int prevCalculated,
const int ratesTotal,
const double &open[],
const double &high[],
const double &close[],
const double &low[],
const long &tickVolume[] //
)
{
//
double price = GetAppliedPrice(
vwapAppliedTo,
open,
high,
low,
close,
bar_index //
);
vwapPriceBuffer[bar_index] = price;
vwapVolumeBuffer[bar_index] = (double)tickVolume[bar_index];
}
/**
* Calculate Different VWaps ...
*
* @param bar_index: Integer, Represent Current Bar ...
* @param prevCalculated: Integer, Represent Previous Calculated Bars ...
* @param ratesTotal: Integer, Represents All Available Bars ...
* @param open: Double Array, History of Open Prices ...
* @param high: Double Array, History of High Prices ...
* @param close: Double Array, History of Close Prices ...
* @param low: Double Array, History of Low Prices ...
* @param tickVolume: Long, History of Tick Volumes on Bar ...
*/
void CalculateVWAPS(
int bar_index, // Selected Bar Index
const int prevCalculated,
const int ratesTotal,
const double &open[],
const double &high[],
const double &close[],
const double &low[],
const long &tickVolume[] //
)
{
//
// Fast ...
CalculateVWAP(
bar_index,
prevCalculated,
ratesTotal,
open,
high,
close,
low,
tickVolume,
//
vwapFastLength,
showVWapFast,
vwapFastBuffer,
vwapFastColorBuffer,
vwapFastStateBuffer //
);
//
// Mid ...
CalculateVWAP(
bar_index,
prevCalculated,
ratesTotal,
open,
high,
close,
low,
tickVolume,
//
vwapMidLength,
showVWapMedium,
vwapMidBuffer,
vwapMidColorBuffer,
vwapMidStateBuffer //
);
//
// Fast ...
CalculateVWAP(
bar_index,
prevCalculated,
ratesTotal,
open,
high,
close,
low,
tickVolume,
//
vwapSlowLength,
showVWapSlow,
vwapSlowBuffer,
vwapSlowColorBuffer,
vwapSlowStateBuffer //
);
}
//