568 lines
14 KiB
Plaintext
568 lines
14 KiB
Plaintext
///////////////////////////////////////////////////////
|
|
//
|
|
// SaherElm IT Center MQL5 Ocillator
|
|
// -------------------------------------------------
|
|
// Name: X121 XVolume
|
|
// Description: Detect Bullish/Bearish Volumes ...
|
|
//
|
|
//
|
|
// 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 XVolume Ocillator"
|
|
#property strict
|
|
|
|
//
|
|
// Definitions ...
|
|
|
|
//
|
|
#define ShortName "X121_XVOLUME"
|
|
|
|
//
|
|
// Imports ...
|
|
#include "../Libraries/x-saherelm.common.lib.mq5"
|
|
|
|
//
|
|
// Inputs ...
|
|
|
|
//
|
|
// Makret ...
|
|
input group "Market";
|
|
input int maLength = 14; // Moving Averge Length
|
|
input ENUM_X_MA_METHOD maMethod = X_MA_MODE_EMA; // Moving Averge Method
|
|
|
|
//
|
|
// Presentation ...
|
|
input group "Presentation";
|
|
input int startCalculationForLastBars = 1000; // Calculate Last n Bars
|
|
input bool showBullishVolume = true; // Show Bullish Volume
|
|
input bool showBullishVolumeMa = true; // Show Bullish Volume Moving Average
|
|
input bool showBearishVolume = true; // Show Bearish Volume
|
|
input bool showBearishVolumeMa = true; // Show Bearish Volume Moving Average
|
|
|
|
//
|
|
// Buffers Props ...
|
|
|
|
//
|
|
#property indicator_separate_window
|
|
|
|
//
|
|
#property indicator_buffers 6
|
|
#property indicator_plots 4
|
|
|
|
//
|
|
// Buffers ...
|
|
|
|
//
|
|
#define bullishVolumeBufferIndex 0
|
|
double bullishVolumeBuffer[];
|
|
|
|
#property indicator_label1 "Bullish Volume"
|
|
#property indicator_type1 DRAW_HISTOGRAM
|
|
#property indicator_color1 clrLime
|
|
#property indicator_style1 STYLE_DOT
|
|
#property indicator_width1 1
|
|
|
|
//
|
|
#define bullishVolumeMaBufferIndex 1
|
|
double bullishVolumeMaBuffer[];
|
|
|
|
#property indicator_label2 "Bullish Volume MA"
|
|
#property indicator_type2 DRAW_LINE
|
|
#property indicator_color2 clrLime
|
|
#property indicator_style2 STYLE_SOLID
|
|
#property indicator_width2 1
|
|
|
|
//
|
|
#define bearishVolumeBufferIndex 2
|
|
double bearishVolumeBuffer[];
|
|
|
|
#property indicator_label3 "Bearish Volume"
|
|
#property indicator_type3 DRAW_HISTOGRAM
|
|
#property indicator_color3 clrRed
|
|
#property indicator_style3 STYLE_DOT
|
|
#property indicator_width3 1
|
|
|
|
//
|
|
#define bearishVolumeMaBufferIndex 3
|
|
double bearishVolumeMaBuffer[];
|
|
|
|
#property indicator_label4 "Bearish Volume MA"
|
|
#property indicator_type4 DRAW_LINE
|
|
#property indicator_color4 clrRed
|
|
#property indicator_style4 STYLE_SOLID
|
|
#property indicator_width4 1
|
|
|
|
//
|
|
// Data Buffers ...
|
|
|
|
#define mLastBufferIndex 3
|
|
|
|
//
|
|
#define rawBullishVolumeBufferIndex mLastBufferIndex + 1
|
|
double rawBullishVolumeBuffer[];
|
|
|
|
//
|
|
#define rawBearishVolumeBufferIndex mLastBufferIndex + 2
|
|
double rawBearishVolumeBuffer[];
|
|
|
|
//
|
|
// Variables, Properties and etc ...
|
|
|
|
//
|
|
// this counts Available Bars ...
|
|
int limit;
|
|
|
|
//
|
|
int maxLength;
|
|
|
|
//
|
|
int firstBarIndex;
|
|
|
|
//
|
|
// 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();
|
|
IndicatorSetInteger(INDICATOR_DIGITS, 2);
|
|
|
|
//
|
|
// Init Succeed ...
|
|
return INIT_SUCCEEDED;
|
|
}
|
|
|
|
/**
|
|
* De Initialize Indicator ...
|
|
*
|
|
* @param reason: Integer, De Initialization Reason ...
|
|
*/
|
|
void OnDeinit(const int reason)
|
|
{
|
|
//
|
|
// REASON_PROGRAM 0 The EA has stopped working calling the ExpertRemove() function
|
|
// REASON_REMOVE 1 Program removed from a chart
|
|
// REASON_RECOMPILE 2 Program recompiled
|
|
// REASON_CHARTCHANGE 3 A symbol or a chart period is changed
|
|
// REASON_CHARTCLOSE 4 Chart closed
|
|
// REASON_PARAMETERS 5 Inputs changed by a user
|
|
// REASON_ACCOUNT 6 Another account has been activated or reconnection to the trade server has occurred due to changes in the account settings
|
|
// REASON_TEMPLATE 7 Another chart template applied
|
|
// REASON_INITFAILED 8 The OnInit() handler returned a non-zero value
|
|
// REASON_CLOSE 9 Terminal closed
|
|
}
|
|
|
|
/**
|
|
* Calculate Bars ...
|
|
*
|
|
* @param rates_total: Integer, Total Bars on Chart ...
|
|
* @param prev_calculated: Integer, Total Calculated Bars on Charts ...
|
|
* @param time: DateTime Array, History of Open Time ...
|
|
* @param open: Double Array, History of Open Prices ...
|
|
* @param high: Double Array, History of High Prices ...
|
|
* @param low: Double Array, History of Low Prices ...
|
|
* @param close: Double Array, History of Close Prices ...
|
|
* @param tick_volume: Long, History of Tick Volumes on Bar ...
|
|
* @param volume: Long, History of Trade Volumes ...
|
|
* @param spread: Double, History of Spread Price ...
|
|
*
|
|
* @return ( int )
|
|
*/
|
|
int OnCalculate(
|
|
const int rates_total,
|
|
const int prev_calculated,
|
|
const datetime &time[],
|
|
const double &open[],
|
|
const double &high[],
|
|
const double &low[],
|
|
const double &close[],
|
|
const long &tick_volume[],
|
|
const long &volume[],
|
|
const int &spread[] //
|
|
)
|
|
{
|
|
//
|
|
// Prepare Buffers ...
|
|
ArraySetAsSeries(time, true);
|
|
ArraySetAsSeries(open, true);
|
|
ArraySetAsSeries(high, true);
|
|
ArraySetAsSeries(low, true);
|
|
ArraySetAsSeries(close, true);
|
|
ArraySetAsSeries(tick_volume, true);
|
|
ArraySetAsSeries(volume, true);
|
|
ArraySetAsSeries(spread, true);
|
|
|
|
//
|
|
limit = (prev_calculated > rates_total || prev_calculated <= 0)
|
|
? rates_total
|
|
: (rates_total - prev_calculated) + 1;
|
|
|
|
//
|
|
// Main Loop ...
|
|
for (int i = limit - 1; i >= 0 && !IsStopped(); i--)
|
|
{
|
|
//
|
|
CalculateBuffers(
|
|
//
|
|
i,
|
|
prev_calculated,
|
|
rates_total,
|
|
//
|
|
open,
|
|
high,
|
|
close,
|
|
low,
|
|
tick_volume //
|
|
);
|
|
}
|
|
|
|
//
|
|
return rates_total;
|
|
}
|
|
|
|
//
|
|
// Functions ...
|
|
|
|
/**
|
|
* Validate Input Args for Initialization ...
|
|
*
|
|
* @return ( bool )
|
|
*/
|
|
bool ValidateInputs()
|
|
{
|
|
//
|
|
bool result = false;
|
|
|
|
//
|
|
result = maLength > 0 &&
|
|
IsValid(maMethod);
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* Extract Max Length of Inputs ...
|
|
*
|
|
* @return ( int )
|
|
*/
|
|
int ExtractMaxLengthOfInputs()
|
|
{
|
|
//
|
|
int result = 0;
|
|
|
|
//
|
|
result = MathMax(0, maLength);
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* Set Indicator Short Name and also we can define Buffers Labels ...
|
|
*/
|
|
void SetIndicatorName()
|
|
{
|
|
IndicatorSetString(INDICATOR_SHORTNAME, ShortName);
|
|
}
|
|
|
|
/**
|
|
* Define Required Buffers ...
|
|
*/
|
|
void DefineBuffers()
|
|
{
|
|
//
|
|
// Plot Buffers ...
|
|
|
|
//
|
|
// Bullish ...
|
|
|
|
//
|
|
// Volume ...
|
|
ENUM_DRAW_TYPE bullishVolumeDrawType = showBullishVolume ? DRAW_HISTOGRAM : DRAW_NONE;
|
|
|
|
//
|
|
ArraySetAsSeries(bullishVolumeBuffer, true);
|
|
SetIndexBuffer(bullishVolumeBufferIndex, bullishVolumeBuffer, INDICATOR_DATA);
|
|
PlotIndexSetDouble(bullishVolumeBufferIndex, PLOT_EMPTY_VALUE, EMPTY_VALUE);
|
|
PlotIndexSetInteger(bullishVolumeBufferIndex, PLOT_SHOW_DATA, showBullishVolume);
|
|
PlotIndexSetInteger(bullishVolumeBufferIndex, PLOT_DRAW_TYPE, bullishVolumeDrawType);
|
|
|
|
//
|
|
// Average ...
|
|
ENUM_DRAW_TYPE bullishVolumeMaDrawType = showBullishVolumeMa ? DRAW_LINE : DRAW_NONE;
|
|
|
|
//
|
|
ArraySetAsSeries(bullishVolumeMaBuffer, true);
|
|
SetIndexBuffer(bullishVolumeMaBufferIndex, bullishVolumeMaBuffer, INDICATOR_DATA);
|
|
PlotIndexSetDouble(bullishVolumeMaBufferIndex, PLOT_EMPTY_VALUE, 0.0);
|
|
PlotIndexSetInteger(bullishVolumeMaBufferIndex, PLOT_SHOW_DATA, showBullishVolumeMa);
|
|
PlotIndexSetInteger(bullishVolumeMaBufferIndex, PLOT_DRAW_TYPE, bullishVolumeMaDrawType);
|
|
|
|
//
|
|
// Bearish ...
|
|
|
|
//
|
|
// Volume ...
|
|
ENUM_DRAW_TYPE bearishVolumeDrawType = showBearishVolume ? DRAW_HISTOGRAM : DRAW_NONE;
|
|
|
|
//
|
|
ArraySetAsSeries(bearishVolumeBuffer, true);
|
|
SetIndexBuffer(bearishVolumeBufferIndex, bearishVolumeBuffer, INDICATOR_DATA);
|
|
PlotIndexSetDouble(bearishVolumeBufferIndex, PLOT_EMPTY_VALUE, EMPTY_VALUE);
|
|
PlotIndexSetInteger(bearishVolumeBufferIndex, PLOT_SHOW_DATA, showBearishVolume);
|
|
PlotIndexSetInteger(bearishVolumeBufferIndex, PLOT_DRAW_TYPE, bearishVolumeDrawType);
|
|
|
|
//
|
|
// Average ...
|
|
ENUM_DRAW_TYPE bearishVolumeMaDrawType = showBearishVolumeMa ? DRAW_LINE : DRAW_NONE;
|
|
|
|
//
|
|
ArraySetAsSeries(bearishVolumeMaBuffer, true);
|
|
SetIndexBuffer(bearishVolumeMaBufferIndex, bearishVolumeMaBuffer, INDICATOR_DATA);
|
|
PlotIndexSetDouble(bearishVolumeMaBufferIndex, PLOT_EMPTY_VALUE, 0.0);
|
|
PlotIndexSetInteger(bearishVolumeMaBufferIndex, PLOT_SHOW_DATA, showBearishVolumeMa);
|
|
PlotIndexSetInteger(bearishVolumeMaBufferIndex, PLOT_DRAW_TYPE, bearishVolumeMaDrawType);
|
|
|
|
//
|
|
// Data Buffers ...
|
|
|
|
//
|
|
ArraySetAsSeries(rawBullishVolumeBuffer, true);
|
|
SetIndexBuffer(rawBullishVolumeBufferIndex, rawBullishVolumeBuffer, INDICATOR_CALCULATIONS);
|
|
|
|
//
|
|
ArraySetAsSeries(rawBearishVolumeBuffer, true);
|
|
SetIndexBuffer(rawBearishVolumeBufferIndex, rawBearishVolumeBuffer, INDICATOR_CALCULATIONS);
|
|
}
|
|
|
|
/**
|
|
* Calculate Custom Buffers ...
|
|
*
|
|
* @param bar_index: Integer, Represent Current Bar ...
|
|
* @param prevCalculated: Integer, Represent Previous Calculated Bars ...
|
|
* @param ratesTotal: Integer, Represents All Available Bars ...
|
|
* @param open: Double Array, History of Open Prices ...
|
|
* @param high: Double Array, History of High Prices ...
|
|
* @param close: Double Array, History of Close Prices ...
|
|
* @param low: Double Array, History of Low Prices ...
|
|
* @param tickVolume: Long, History of Tick Volumes on Bar ...
|
|
*/
|
|
void CalculateBuffers(
|
|
int bar_index, // Selected Bar Index
|
|
const int prevCalculated,
|
|
const int ratesTotal,
|
|
const double &open[],
|
|
const double &high[],
|
|
const double &close[],
|
|
const double &low[],
|
|
const long &tickVolume[] //
|
|
)
|
|
{
|
|
//
|
|
int barsLimit = startCalculationForLastBars > 0
|
|
? startCalculationForLastBars
|
|
: 0;
|
|
if (barsLimit == 0)
|
|
{
|
|
//
|
|
barsLimit = ratesTotal;
|
|
firstBarIndex = barsLimit - 1;
|
|
}
|
|
else
|
|
{
|
|
//
|
|
firstBarIndex = startCalculationForLastBars;
|
|
}
|
|
|
|
//
|
|
// bool canCalculate = true;
|
|
bool canCalculate =
|
|
bar_index <= barsLimit;
|
|
if (canCalculate)
|
|
{
|
|
//
|
|
CalculateValues(
|
|
bar_index,
|
|
prevCalculated,
|
|
ratesTotal,
|
|
open,
|
|
high,
|
|
close,
|
|
low,
|
|
tickVolume //
|
|
);
|
|
}
|
|
else
|
|
{
|
|
FillBuffersZero(bar_index);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Fill All Bufers to Zero Vlue for Specified Bar Index ...
|
|
*
|
|
* @param barIndex: Integer ...
|
|
*/
|
|
void FillBuffersZero(int barIndex)
|
|
{
|
|
//
|
|
bullishVolumeBuffer[barIndex] = EMPTY_VALUE;
|
|
bullishVolumeMaBuffer[barIndex] = EMPTY_VALUE;
|
|
rawBullishVolumeBuffer[barIndex] = 0;
|
|
|
|
//
|
|
bearishVolumeBuffer[barIndex] = EMPTY_VALUE;
|
|
bearishVolumeMaBuffer[barIndex] = EMPTY_VALUE;
|
|
rawBearishVolumeBuffer[barIndex] = 0;
|
|
}
|
|
|
|
/**
|
|
* Calculate Vales ...
|
|
*
|
|
* @param bar_index: Integer, Represent Current Bar ...
|
|
* @param prevCalculated: Integer, Represent Previous Calculated Bars ...
|
|
* @param ratesTotal: Integer, Represents All Available Bars ...
|
|
* @param open: Double Array, History of Open Prices ...
|
|
* @param high: Double Array, History of High Prices ...
|
|
* @param close: Double Array, History of Close Prices ...
|
|
* @param low: Double Array, History of Low Prices ...
|
|
* @param tickVolume: Long, History of Tick Volumes on Bar ...
|
|
*/
|
|
void CalculateValues(
|
|
int bar_index, // Selected Bar Index
|
|
const int prevCalculated,
|
|
const int ratesTotal,
|
|
const double &open[],
|
|
const double &high[],
|
|
const double &close[],
|
|
const double &low[],
|
|
const long &tickVolume[] //
|
|
)
|
|
{
|
|
//
|
|
// Check Prev Bar ...
|
|
//
|
|
int lastBarIndex = bar_index + 1;
|
|
bool isFirstBar =
|
|
startCalculationForLastBars > 0
|
|
? bar_index == startCalculationForLastBars
|
|
: bar_index == firstBarIndex;
|
|
|
|
//
|
|
XOHCL bar;
|
|
bool has = bar.Init(
|
|
_Symbol,
|
|
_Period,
|
|
bar_index //
|
|
);
|
|
if (!has)
|
|
{
|
|
return;
|
|
}
|
|
|
|
//
|
|
double iBullishVolume = 0;
|
|
double iBearishVolume = 0;
|
|
|
|
//
|
|
if (bar.IsBullish())
|
|
{
|
|
//
|
|
iBullishVolume = (double)bar.volume;
|
|
iBearishVolume = 0;
|
|
}
|
|
else if (bar.IsBearish())
|
|
{
|
|
//
|
|
iBearishVolume = (double)bar.volume;
|
|
iBullishVolume = 0;
|
|
}
|
|
else
|
|
{
|
|
//
|
|
double volume = ((double)bar.volume) / 2.0;
|
|
iBullishVolume = volume;
|
|
iBearishVolume = volume;
|
|
}
|
|
|
|
//
|
|
bullishVolumeBuffer[bar_index] =
|
|
iBullishVolume == 0
|
|
? EMPTY_VALUE
|
|
: iBullishVolume;
|
|
rawBullishVolumeBuffer[bar_index] = iBullishVolume;
|
|
|
|
//
|
|
bearishVolumeBuffer[bar_index] =
|
|
iBearishVolume == 0
|
|
? EMPTY_VALUE
|
|
: iBearishVolume;
|
|
rawBearishVolumeBuffer[bar_index] = iBearishVolume;
|
|
|
|
//
|
|
// Calculate Moving Averages ...
|
|
|
|
//
|
|
int bullishMAsCount = iMAOnBuffer(
|
|
ratesTotal,
|
|
prevCalculated,
|
|
bar_index,
|
|
maLength,
|
|
rawBullishVolumeBuffer,
|
|
bullishVolumeMaBuffer,
|
|
maMethod //
|
|
);
|
|
|
|
//
|
|
int bearishMAsCount = iMAOnBuffer(
|
|
ratesTotal,
|
|
prevCalculated,
|
|
bar_index,
|
|
maLength,
|
|
rawBearishVolumeBuffer,
|
|
bearishVolumeMaBuffer,
|
|
maMethod //
|
|
);
|
|
|
|
//
|
|
bar.Clean();
|
|
}
|
|
|
|
//
|