Files
xMQL4/xIndicators/x-saherelm.volume.mq4
2024-01-25 04:03:36 +03:30

270 lines
5.8 KiB
Plaintext

/////////////////////////////////////////////////////////////////
//
// SaherElm IT Center Volume Indicator
// -------------------------------------------------------------
// this indicator provides Volume ...
//
// 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 Volume Indicator"
#property strict
//
// START Inputs ...
//
input int length = 11; // Length
//
// END Inputs ...
//
//
// START Global Definitions: Variables, Properties and etc ...
//
//
// Includes our shared library ...
#include "../Libraries/x-saherelm.lib.mq4"
//
#property indicator_separate_window
//
// we have 2 buffer in this indicator ...
#property indicator_buffers 3
//
// also we have 2 line for draw on this indicator ...
#property indicator_plots 3
//
// Buy Volume ...
#property indicator_width1 1
#property indicator_color1 clrGreen
#property indicator_type1 DRAW_HISTOGRAM
#property indicator_style1 STYLE_DOT
//
// Sell Volume ...
#property indicator_width2 1
#property indicator_type2 DRAW_LINE
#property indicator_color2 clrRed
#property indicator_style2 STYLE_DOT
//
// Diff ...
#property indicator_width3 1
#property indicator_type3 DRAW_HISTOGRAM
#property indicator_color3 clrYellow
#property indicator_style3 STYLE_SOLID
//
//#property indicator_minimum 0
//#property indicator_maximum 1
//
// set color of horizontal levels ...
#property indicator_levelcolor clrGray
//
// set style of horizontal levels ...
#property indicator_levelstyle STYLE_DOT
//
// Declare Buffers ...
#define buyVolumeBufferIndex 0
#define sellVolumeBufferIndex 1
#define diffBufferIndex 2
//
double buyVolumeBuffer[];
double sellVolumeBuffer[];
double diffBuffer[];
//
// here we specify logging enabled or not ...
bool enableLogging = true;
//
// this is a Tag which attached to our Logger ...
string logTag = "XS_VOLUME";
//
// END Global Definitions: Variables, Properties and etc ...
//
//
// Start Event Handlers ...
//
//
// Initialization ...
int OnInit() {
//
// Initialize what we want ...
//
// Validating Input Parameters ...
if (length < 0) {
return INIT_PARAMETERS_INCORRECT;
}
//
// set descriptions of horizontal levels ...
SetLevelValue(0, 0.0);
IndicatorSetString(INDICATOR_LEVELTEXT,0,"");
//
// Buy Volume Buffer ...
SetIndexBuffer(buyVolumeBufferIndex, buyVolumeBuffer);
SetIndexLabel(buyVolumeBufferIndex, "Buy Volume");
//
// Sell Volume Buffer ...
SetIndexBuffer(sellVolumeBufferIndex, sellVolumeBuffer);
SetIndexLabel(sellVolumeBufferIndex, "Sell Volume");
//
// Diff Buffer ...
SetIndexBuffer(diffBufferIndex, diffBuffer);
SetIndexLabel(diffBufferIndex, "Diff");
//
return(INIT_SUCCEEDED);
}
//
// Do Calculation ...
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[]
) {
//
// 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);
//
// 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 ...
//
CalculateBuffers(
open,
close,
high,
low,
i
);
}
//
return rates_total;
}
//
// De Initialization ...
void OnDeinit(const int reason) {
}
//
// END Event Handlers ...
//
//
// START Functions ...
//
//
void CalculateBuffers(
const double &open[],
const double &close[],
const double &high[],
const double &low[],
const int &bar_index
) {
//
long volume = iVolume(
_Symbol,
_Period,
bar_index
);
//
double buyVolume = 0.0;
double sellVolume = 0.0;
//
// Up ...
if (close[bar_index] > open[bar_index]) {
//
double cuenta = (high[bar_index] - low[bar_index]) * 10000;
double cuenta1 = volume - cuenta;
//
buyVolume = ((volume - cuenta) / 2) + cuenta;
//
buyVolumeBuffer[bar_index] = buyVolume;
sellVolumeBuffer[bar_index] = volume - buyVolume;
} else
//
// Down ...
if (close[bar_index] < open[bar_index]) {
//
double cuenta1 = (high[bar_index] - low[bar_index]) * 10000;
double cuenta = volume - cuenta1;
//
sellVolume = ((volume - cuenta1) / 2) + cuenta1;
//
sellVolumeBuffer[bar_index] = sellVolume;
buyVolumeBuffer[bar_index] = volume - sellVolume;
} else
//
// Neutral ...
if (close[bar_index] == open[bar_index]) {
//
double cuenta = (volume / 2) * 1.0;
//
sellVolumeBuffer[bar_index] = cuenta;
buyVolumeBuffer[bar_index] = volume - cuenta;
}
//
diffBuffer[bar_index] = MathAbs(buyVolumeBuffer[bar_index]) - MathAbs(sellVolumeBuffer[bar_index]);
}
//
// END Functions ...
//