add and backup indicators ...
This commit is contained in:
@@ -0,0 +1,233 @@
|
|||||||
|
//+------------------------------------------------------------------+
|
||||||
|
//| Consolidation Detector EA |
|
||||||
|
//| Detects consolidations using multiple methods |
|
||||||
|
//+------------------------------------------------------------------+
|
||||||
|
#property copyright "Your Name"
|
||||||
|
#property link "https://www.example.com"
|
||||||
|
#property version "1.01"
|
||||||
|
|
||||||
|
// Input Parameters
|
||||||
|
input group "Consolidation Detection Settings"
|
||||||
|
input bool UsePriceRange = true; // Enable Price Range Analysis
|
||||||
|
input int PriceRangeBars = 20; // Bars for Price Range
|
||||||
|
input double PriceRangeThreshold = 0.5; // Price Range Threshold (% of ATR)
|
||||||
|
|
||||||
|
input bool UseBollingerSqueeze = true; // Enable Bollinger Bands Squeeze
|
||||||
|
input int BollingerPeriod = 20; // Bollinger Bands Period
|
||||||
|
input double BollingerThreshold = 0.02; // Bollinger Bandwidth Threshold
|
||||||
|
|
||||||
|
input bool UseATRCrossover = true; // Enable ATR Crossover
|
||||||
|
input int FastATRPeriod = 5; // Fast ATR Period
|
||||||
|
input int SlowATRPeriod = 20; // Slow ATR Period
|
||||||
|
|
||||||
|
input bool UseVolumeAnalysis = true; // Enable Volume Analysis
|
||||||
|
input int VolumeBars = 20; // Bars for Volume Analysis
|
||||||
|
input double VolumeThreshold = 0.7; // Volume Threshold (% of Avg Volume)
|
||||||
|
|
||||||
|
input group "Visualization and Alerts"
|
||||||
|
input bool DrawZones = true; // Draw Consolidation Zones
|
||||||
|
input bool EnableAlerts = true; // Enable Alerts for Consolidation
|
||||||
|
input color ZoneColor = clrPurple; // Color for Consolidation Zones
|
||||||
|
|
||||||
|
// Global Variables
|
||||||
|
datetime lastBarTime; // Track last processed bar time
|
||||||
|
int bollingerHandle; // Handle for Bollinger Bands
|
||||||
|
int fastATRHandle; // Handle for Fast ATR
|
||||||
|
int slowATRHandle; // Handle for Slow ATR
|
||||||
|
int priceATRHandle; // Handle for Price Range ATR
|
||||||
|
|
||||||
|
//+------------------------------------------------------------------+
|
||||||
|
//| Expert initialization function |
|
||||||
|
//+------------------------------------------------------------------+
|
||||||
|
int OnInit()
|
||||||
|
{
|
||||||
|
// Initialize Bollinger Bands handle
|
||||||
|
bollingerHandle = iBands(_Symbol, PERIOD_CURRENT, BollingerPeriod, 0, 2.0, PRICE_CLOSE);
|
||||||
|
if (bollingerHandle == INVALID_HANDLE)
|
||||||
|
{
|
||||||
|
Print("Failed to initialize Bollinger Bands");
|
||||||
|
return(INIT_FAILED);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Initialize ATR handles
|
||||||
|
priceATRHandle = iATR(_Symbol, PERIOD_CURRENT, PriceRangeBars);
|
||||||
|
fastATRHandle = iATR(_Symbol, PERIOD_CURRENT, FastATRPeriod);
|
||||||
|
slowATRHandle = iATR(_Symbol, PERIOD_CURRENT, SlowATRPeriod);
|
||||||
|
|
||||||
|
if (priceATRHandle == INVALID_HANDLE || fastATRHandle == INVALID_HANDLE || slowATRHandle == INVALID_HANDLE)
|
||||||
|
{
|
||||||
|
Print("Failed to initialize ATR indicators");
|
||||||
|
return(INIT_FAILED);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set last bar time to avoid processing same bar multiple times
|
||||||
|
lastBarTime = TimeCurrent();
|
||||||
|
|
||||||
|
return(INIT_SUCCEEDED);
|
||||||
|
}
|
||||||
|
|
||||||
|
//+------------------------------------------------------------------+
|
||||||
|
//| Expert deinitialization function |
|
||||||
|
//+------------------------------------------------------------------+
|
||||||
|
void OnDeinit(const int reason)
|
||||||
|
{
|
||||||
|
// Release indicator handles
|
||||||
|
IndicatorRelease(bollingerHandle);
|
||||||
|
IndicatorRelease(priceATRHandle);
|
||||||
|
IndicatorRelease(fastATRHandle);
|
||||||
|
IndicatorRelease(slowATRHandle);
|
||||||
|
|
||||||
|
// Clean up chart objects
|
||||||
|
ObjectsDeleteAll(0, "Consolidation_");
|
||||||
|
}
|
||||||
|
|
||||||
|
//+------------------------------------------------------------------+
|
||||||
|
//| Expert tick function |
|
||||||
|
//+------------------------------------------------------------------+
|
||||||
|
void OnTick()
|
||||||
|
{
|
||||||
|
// Process only on new bar
|
||||||
|
datetime currentBarTime = iTime(_Symbol, PERIOD_CURRENT, 0);
|
||||||
|
if (currentBarTime == lastBarTime)
|
||||||
|
return;
|
||||||
|
lastBarTime = currentBarTime;
|
||||||
|
|
||||||
|
bool isConsolidation = false;
|
||||||
|
string consolidationMethods = "";
|
||||||
|
|
||||||
|
// Get high and low for visualization
|
||||||
|
double high = iHigh(_Symbol, PERIOD_CURRENT, 1);
|
||||||
|
double low = iLow(_Symbol, PERIOD_CURRENT, 1);
|
||||||
|
datetime startTime = iTime(_Symbol, PERIOD_CURRENT, PriceRangeBars);
|
||||||
|
datetime endTime = currentBarTime;
|
||||||
|
|
||||||
|
// Check Price Range Consolidation
|
||||||
|
// if (UsePriceRange && IsPriceRangeConsolidation(PriceRangeBars, PriceRangeThreshold))
|
||||||
|
// {
|
||||||
|
// isConsolidation = true;
|
||||||
|
// consolidationMethods += "Price Range, ";
|
||||||
|
// }
|
||||||
|
|
||||||
|
// // Check Bollinger Bands Squeeze
|
||||||
|
// if (UseBollingerSqueeze && IsBollingerSqueeze(BollingerPeriod, BollingerThreshold))
|
||||||
|
// {
|
||||||
|
// isConsolidation = true;
|
||||||
|
// consolidationMethods += "Bollinger Squeeze, ";
|
||||||
|
// }
|
||||||
|
|
||||||
|
// // Check ATR Crossover
|
||||||
|
// if (UseATRCrossover && IsATRConsolidation(FastATRPeriod, SlowATRPeriod))
|
||||||
|
// {
|
||||||
|
// isConsolidation = true;
|
||||||
|
// consolidationMethods += "ATR Crossover, ";
|
||||||
|
// }
|
||||||
|
|
||||||
|
// // Check Volume Analysis
|
||||||
|
// if (UseVolumeAnalysis && IsVolumeConsolidation(VolumeBars, VolumeThreshold))
|
||||||
|
// {
|
||||||
|
// isConsolidation = true;
|
||||||
|
// consolidationMethods += "Volume Analysis, ";
|
||||||
|
// }
|
||||||
|
|
||||||
|
// Output and Visualization
|
||||||
|
if (isConsolidation)
|
||||||
|
{
|
||||||
|
string message = "Consolidation detected by: " + consolidationMethods;
|
||||||
|
Print(message);
|
||||||
|
if (EnableAlerts)
|
||||||
|
Alert(message);
|
||||||
|
if (DrawZones)
|
||||||
|
DrawConsolidationZone(high, low, startTime, endTime);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Print("No consolidation detected");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//+------------------------------------------------------------------+
|
||||||
|
//| Price Range Consolidation Detection |
|
||||||
|
//+------------------------------------------------------------------+
|
||||||
|
bool IsPriceRangeConsolidation(int bars, double threshold)
|
||||||
|
{
|
||||||
|
double rangeSum = 0.0;
|
||||||
|
double high[], low[], atr[];
|
||||||
|
ArraySetAsSeries(high, true);
|
||||||
|
ArraySetAsSeries(low, true);
|
||||||
|
ArraySetAsSeries(atr, true);
|
||||||
|
|
||||||
|
CopyHigh(_Symbol, PERIOD_CURRENT, 1, bars, high);
|
||||||
|
CopyLow(_Symbol, PERIOD_CURRENT, 1, bars, low);
|
||||||
|
CopyBuffer(priceATRHandle, 0, 1, 1, atr); // Get ATR for shift 1
|
||||||
|
|
||||||
|
for (int i = 0; i < bars; i++)
|
||||||
|
rangeSum += high[i] - low[i];
|
||||||
|
|
||||||
|
double avgRange = rangeSum / bars;
|
||||||
|
|
||||||
|
return (avgRange < threshold * atr[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
//+------------------------------------------------------------------+
|
||||||
|
//| Bollinger Bands Squeeze Detection |
|
||||||
|
//+------------------------------------------------------------------+
|
||||||
|
bool IsBollingerSqueeze(int period, double threshold)
|
||||||
|
{
|
||||||
|
double upper[], lower[], middle[];
|
||||||
|
ArraySetAsSeries(upper, true);
|
||||||
|
ArraySetAsSeries(lower, true);
|
||||||
|
ArraySetAsSeries(middle, true);
|
||||||
|
|
||||||
|
CopyBuffer(bollingerHandle, 0, 0, 3, middle); // Middle band
|
||||||
|
CopyBuffer(bollingerHandle, 1, 0, 3, upper); // Upper band
|
||||||
|
CopyBuffer(bollingerHandle, 2, 0, 3, lower); // Lower band
|
||||||
|
|
||||||
|
double bandwidth = (upper[1] - lower[1]) / middle[1];
|
||||||
|
return (bandwidth < threshold);
|
||||||
|
}
|
||||||
|
|
||||||
|
//+------------------------------------------------------------------+
|
||||||
|
//| ATR Crossover Consolidation Detection |
|
||||||
|
//+------------------------------------------------------------------+
|
||||||
|
bool IsATRConsolidation(int fastPeriod, int slowPeriod)
|
||||||
|
{
|
||||||
|
double fastATR[], slowATR[];
|
||||||
|
ArraySetAsSeries(fastATR, true);
|
||||||
|
ArraySetAsSeries(slowATR, true);
|
||||||
|
|
||||||
|
CopyBuffer(fastATRHandle, 0, 1, 1, fastATR); // Fast ATR for shift 1
|
||||||
|
CopyBuffer(slowATRHandle, 0, 1, 1, slowATR); // Slow ATR for shift 1
|
||||||
|
|
||||||
|
return (fastATR[0] < slowATR[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
//+------------------------------------------------------------------+
|
||||||
|
//| Volume Analysis Consolidation Detection |
|
||||||
|
//+------------------------------------------------------------------+
|
||||||
|
bool IsVolumeConsolidation(int bars, double threshold)
|
||||||
|
{
|
||||||
|
double volume[];
|
||||||
|
ArraySetAsSeries(volume, true);
|
||||||
|
CopyBuffer(iVolume(_Symbol, PERIOD_CURRENT, 0), 0, 0, bars, volume);
|
||||||
|
|
||||||
|
double avgVolume = 0.0;
|
||||||
|
for (int i = 0; i < bars; i++)
|
||||||
|
avgVolume += volume[i];
|
||||||
|
avgVolume /= bars;
|
||||||
|
|
||||||
|
double currentVolume = volume[1];
|
||||||
|
return (currentVolume < threshold * avgVolume);
|
||||||
|
}
|
||||||
|
|
||||||
|
//+------------------------------------------------------------------+
|
||||||
|
//| Draw Consolidation Zone on Chart |
|
||||||
|
//+------------------------------------------------------------------+
|
||||||
|
void DrawConsolidationZone(double high, double low, datetime start, datetime end)
|
||||||
|
{
|
||||||
|
string objName = "Consolidation_" + TimeToString(start);
|
||||||
|
ObjectCreate(0, objName, OBJ_RECTANGLE, 0, start, high, end, low);
|
||||||
|
ObjectSetInteger(0, objName, OBJPROP_COLOR, ZoneColor);
|
||||||
|
ObjectSetInteger(0, objName, OBJPROP_STYLE, STYLE_SOLID);
|
||||||
|
ObjectSetInteger(0, objName, OBJPROP_FILL, true);
|
||||||
|
ObjectSetInteger(0, objName, OBJPROP_BACK, true);
|
||||||
|
}
|
||||||
@@ -0,0 +1,788 @@
|
|||||||
|
///////////////////////////////////////////////////////
|
||||||
|
//
|
||||||
|
// SaherElm IT Center MQL5 Indicator
|
||||||
|
// -------------------------------------------------
|
||||||
|
// Name: X121 X3MA
|
||||||
|
// Description: X3MA ...
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 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 X3MA Indicator"
|
||||||
|
#property strict
|
||||||
|
|
||||||
|
//
|
||||||
|
// Definitions ...
|
||||||
|
|
||||||
|
//
|
||||||
|
#define ShortName "X121 X3MA"
|
||||||
|
|
||||||
|
//
|
||||||
|
// Includes Common Library ...
|
||||||
|
#include "../Libraries/x-saherelm.common.lib.mq5"
|
||||||
|
|
||||||
|
//
|
||||||
|
// Inputs ...
|
||||||
|
|
||||||
|
//
|
||||||
|
// Fast ...
|
||||||
|
input group "Fast";
|
||||||
|
input int fastLength = 6; // Length
|
||||||
|
input ENUM_MA_METHOD fastMethod = MODE_SMA; // Method
|
||||||
|
input ENUM_APPLIED_PRICE fastAppliedTo = PRICE_CLOSE; // Applied To
|
||||||
|
|
||||||
|
//
|
||||||
|
// Medium ...
|
||||||
|
input group "Medium";
|
||||||
|
input int mediumLength = 21; // Length
|
||||||
|
input ENUM_MA_METHOD mediumMethod = MODE_SMA; // Method
|
||||||
|
input ENUM_APPLIED_PRICE mediumAppliedTo = PRICE_CLOSE; // Applied To
|
||||||
|
|
||||||
|
//
|
||||||
|
// Slow ...
|
||||||
|
input group "Slow";
|
||||||
|
input int slowLength = 34; // Length
|
||||||
|
input ENUM_MA_METHOD slowMethod = MODE_SMA; // Method
|
||||||
|
input ENUM_APPLIED_PRICE slowAppliedTo = PRICE_CLOSE; // Applied To
|
||||||
|
|
||||||
|
//
|
||||||
|
// Presentation ...
|
||||||
|
input group "Presentation";
|
||||||
|
|
||||||
|
//
|
||||||
|
input int startCalculationForLastBars = 1000; // Calculate Last n Bars
|
||||||
|
|
||||||
|
//
|
||||||
|
input bool applyColor = false;
|
||||||
|
|
||||||
|
//
|
||||||
|
input bool showFast = true; // Show Fast
|
||||||
|
input bool showMedium = true; // Show Medium
|
||||||
|
input bool showSlow = true; // Show Slow
|
||||||
|
|
||||||
|
//
|
||||||
|
// Buffers ...
|
||||||
|
|
||||||
|
//
|
||||||
|
#define hideColorIDX 0
|
||||||
|
#define bullishColorIDX 1
|
||||||
|
#define bearishColorIDX 2
|
||||||
|
#define neuturalColorIDX 3
|
||||||
|
|
||||||
|
//
|
||||||
|
#define bullishState 1
|
||||||
|
#define neuturalState 0
|
||||||
|
#define bearishState -1
|
||||||
|
|
||||||
|
//
|
||||||
|
#define emptyValue 0.0
|
||||||
|
|
||||||
|
//
|
||||||
|
#property indicator_chart_window
|
||||||
|
|
||||||
|
//
|
||||||
|
#property indicator_buffers 9
|
||||||
|
#property indicator_plots 3
|
||||||
|
|
||||||
|
//
|
||||||
|
// Plot Buffers ...
|
||||||
|
|
||||||
|
//
|
||||||
|
// FAST ...
|
||||||
|
|
||||||
|
//
|
||||||
|
#define fastBufferIndex 0
|
||||||
|
double fastBuffer[];
|
||||||
|
|
||||||
|
//
|
||||||
|
#define fastColorBufferIndex 1
|
||||||
|
double fastColorBuffer[];
|
||||||
|
|
||||||
|
//
|
||||||
|
#property indicator_label1 "X121 X3MA Fast"
|
||||||
|
#property indicator_type1 DRAW_COLOR_LINE
|
||||||
|
#property indicator_color1 CLR_NONE, clrYellow, clrChocolate, clrGray
|
||||||
|
#property indicator_style1 STYLE_SOLID
|
||||||
|
#property indicator_width1 2
|
||||||
|
|
||||||
|
//
|
||||||
|
// MEDIUM ...
|
||||||
|
|
||||||
|
//
|
||||||
|
#define mediumBufferIndex 2
|
||||||
|
double mediumBuffer[];
|
||||||
|
|
||||||
|
//
|
||||||
|
#define mediumColorBufferIndex 3
|
||||||
|
double mediumColorBuffer[];
|
||||||
|
|
||||||
|
//
|
||||||
|
#property indicator_label2 "X121 X3MA Medium"
|
||||||
|
#property indicator_type2 DRAW_COLOR_LINE
|
||||||
|
#property indicator_color2 CLR_NONE, clrAqua, clrMagenta, clrGray
|
||||||
|
#property indicator_style2 STYLE_DASHDOTDOT
|
||||||
|
#property indicator_width2 2
|
||||||
|
|
||||||
|
//
|
||||||
|
// SLOW ...
|
||||||
|
|
||||||
|
//
|
||||||
|
#define slowBufferIndex 4
|
||||||
|
double slowBuffer[];
|
||||||
|
|
||||||
|
//
|
||||||
|
#define slowColorBufferIndex 5
|
||||||
|
double slowColorBuffer[];
|
||||||
|
|
||||||
|
//
|
||||||
|
#property indicator_label3 "X121 X3MA Slow"
|
||||||
|
#property indicator_type3 DRAW_COLOR_LINE
|
||||||
|
#property indicator_color3 CLR_NONE, clrLime, clrRed, clrGray
|
||||||
|
#property indicator_style3 STYLE_DASH
|
||||||
|
#property indicator_width3 2
|
||||||
|
|
||||||
|
//
|
||||||
|
// Data Buffers ...
|
||||||
|
|
||||||
|
#define mLastBufferIndex 5
|
||||||
|
|
||||||
|
//
|
||||||
|
#define fastStateBufferIndex mLastBufferIndex + 1
|
||||||
|
double fastStateBuffer[];
|
||||||
|
|
||||||
|
//
|
||||||
|
#define mediumStateBufferIndex mLastBufferIndex + 2
|
||||||
|
double mediumStateBuffer[];
|
||||||
|
|
||||||
|
//
|
||||||
|
#define slowStateBufferIndex mLastBufferIndex + 3
|
||||||
|
double slowStateBuffer[];
|
||||||
|
|
||||||
|
//
|
||||||
|
// Variables, Properties and etc ...
|
||||||
|
|
||||||
|
//
|
||||||
|
// this counts Available Bars ...
|
||||||
|
int limit;
|
||||||
|
|
||||||
|
//
|
||||||
|
int maxLength;
|
||||||
|
|
||||||
|
//
|
||||||
|
int fastHandler = INVALID_HANDLE;
|
||||||
|
int mediumHandler = INVALID_HANDLE;
|
||||||
|
int slowHandler = INVALID_HANDLE;
|
||||||
|
|
||||||
|
//
|
||||||
|
// Event Handlers ...
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initialize Indicator ...
|
||||||
|
*
|
||||||
|
* @return ( int )
|
||||||
|
*/
|
||||||
|
int OnInit()
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Validate Inputs ...
|
||||||
|
if (!ValidateInputs())
|
||||||
|
{
|
||||||
|
return INIT_PARAMETERS_INCORRECT;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Initialize Indicator Handlers ...
|
||||||
|
|
||||||
|
//
|
||||||
|
// FAST ...
|
||||||
|
fastHandler = iMA(
|
||||||
|
_Symbol,
|
||||||
|
_Period,
|
||||||
|
fastLength,
|
||||||
|
0,
|
||||||
|
fastMethod,
|
||||||
|
fastAppliedTo //
|
||||||
|
);
|
||||||
|
bool isInited = fastHandler != INVALID_HANDLE;
|
||||||
|
if (!isInited)
|
||||||
|
{
|
||||||
|
return INIT_FAILED;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// MEDIUM ...
|
||||||
|
mediumHandler = iMA(
|
||||||
|
_Symbol,
|
||||||
|
_Period,
|
||||||
|
mediumLength,
|
||||||
|
0,
|
||||||
|
mediumMethod,
|
||||||
|
mediumAppliedTo //
|
||||||
|
);
|
||||||
|
isInited = mediumHandler != INVALID_HANDLE;
|
||||||
|
if (!isInited)
|
||||||
|
{
|
||||||
|
return INIT_FAILED;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// SLOW ...
|
||||||
|
slowHandler = iMA(
|
||||||
|
_Symbol,
|
||||||
|
_Period,
|
||||||
|
slowLength,
|
||||||
|
0,
|
||||||
|
slowMethod,
|
||||||
|
slowAppliedTo //
|
||||||
|
);
|
||||||
|
isInited = slowHandler != 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(fastHandler);
|
||||||
|
IndicatorRelease(mediumHandler);
|
||||||
|
IndicatorRelease(slowHandler);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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 ...
|
||||||
|
|
||||||
|
//
|
||||||
|
// FAST ...
|
||||||
|
int fastCalculatedBars = BarsCalculated(fastHandler);
|
||||||
|
|
||||||
|
//
|
||||||
|
// MEDIUM ...
|
||||||
|
int mediumCalculatedBars = BarsCalculated(mediumHandler);
|
||||||
|
|
||||||
|
//
|
||||||
|
// SLOW ...
|
||||||
|
int slowCalculatedBars = BarsCalculated(slowHandler);
|
||||||
|
|
||||||
|
//
|
||||||
|
bool isPassedRequiredCalculatedBars =
|
||||||
|
//
|
||||||
|
// FAST ...
|
||||||
|
fastCalculatedBars >= maxLength &&
|
||||||
|
//
|
||||||
|
// MEDIUM ...
|
||||||
|
mediumCalculatedBars >= maxLength &&
|
||||||
|
//
|
||||||
|
// SLOW ...
|
||||||
|
slowCalculatedBars >= maxLength
|
||||||
|
//
|
||||||
|
;
|
||||||
|
if (!isPassedRequiredCalculatedBars)
|
||||||
|
{
|
||||||
|
return prev_calculated;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
limit = (prev_calculated > rates_total || prev_calculated <= 0)
|
||||||
|
? rates_total
|
||||||
|
: (rates_total - prev_calculated) + 1;
|
||||||
|
|
||||||
|
//
|
||||||
|
// Buffers Copy ...
|
||||||
|
|
||||||
|
//
|
||||||
|
// FAST ...
|
||||||
|
int copiedFasts = CopyBuffer(fastHandler, 0, 0, limit, fastBuffer);
|
||||||
|
|
||||||
|
//
|
||||||
|
// MEDIUM ...
|
||||||
|
int copiedMediumss = CopyBuffer(mediumHandler, 0, 0, limit, mediumBuffer);
|
||||||
|
|
||||||
|
//
|
||||||
|
// SLOW ...
|
||||||
|
int copiedSlows = CopyBuffer(slowHandler, 0, 0, limit, slowBuffer);
|
||||||
|
|
||||||
|
//
|
||||||
|
// Validate Copied Items ...
|
||||||
|
bool isPassedRequiredCopiedItems =
|
||||||
|
//
|
||||||
|
// FAST ...
|
||||||
|
copiedFasts >= limit &&
|
||||||
|
//
|
||||||
|
// MEDIUM ...
|
||||||
|
copiedMediumss >= limit &&
|
||||||
|
//
|
||||||
|
// SLOW ...
|
||||||
|
copiedSlows >= limit
|
||||||
|
//
|
||||||
|
;
|
||||||
|
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 = false;
|
||||||
|
|
||||||
|
//
|
||||||
|
result =
|
||||||
|
//
|
||||||
|
fastLength > 2 &&
|
||||||
|
mediumLength > 2 &&
|
||||||
|
slowLength > 2 &&
|
||||||
|
mediumLength > fastLength &&
|
||||||
|
mediumLength < slowLength
|
||||||
|
//
|
||||||
|
;
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Extract Max Length of Inputs ...
|
||||||
|
*
|
||||||
|
* @return ( int )
|
||||||
|
*/
|
||||||
|
int ExtractMaxLengthOfInputs()
|
||||||
|
{
|
||||||
|
//
|
||||||
|
int result = 0;
|
||||||
|
|
||||||
|
//
|
||||||
|
result = MathMax(fastLength, mediumLength);
|
||||||
|
result = MathMax(result, slowLength);
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Define Required Buffers ...
|
||||||
|
*/
|
||||||
|
void DefineBuffers()
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Plot Buffers ...
|
||||||
|
|
||||||
|
//
|
||||||
|
// FAST ...
|
||||||
|
|
||||||
|
//
|
||||||
|
ArraySetAsSeries(fastBuffer, true);
|
||||||
|
SetIndexBuffer(fastBufferIndex, fastBuffer, INDICATOR_DATA);
|
||||||
|
|
||||||
|
//
|
||||||
|
PlotIndexSetInteger(fastBufferIndex, PLOT_SHOW_DATA, showFast);
|
||||||
|
|
||||||
|
//
|
||||||
|
PlotIndexSetDouble(fastBufferIndex, PLOT_EMPTY_VALUE, emptyValue);
|
||||||
|
|
||||||
|
//
|
||||||
|
ArraySetAsSeries(fastColorBuffer, true);
|
||||||
|
SetIndexBuffer(fastColorBufferIndex, fastColorBuffer, INDICATOR_COLOR_INDEX);
|
||||||
|
|
||||||
|
//
|
||||||
|
// MEDIUM ...
|
||||||
|
|
||||||
|
//
|
||||||
|
ArraySetAsSeries(mediumBuffer, true);
|
||||||
|
SetIndexBuffer(mediumBufferIndex, mediumBuffer, INDICATOR_DATA);
|
||||||
|
|
||||||
|
//
|
||||||
|
PlotIndexSetInteger(mediumBufferIndex, PLOT_SHOW_DATA, showMedium);
|
||||||
|
|
||||||
|
//
|
||||||
|
PlotIndexSetDouble(mediumBufferIndex, PLOT_EMPTY_VALUE, emptyValue);
|
||||||
|
|
||||||
|
//
|
||||||
|
ArraySetAsSeries(mediumColorBuffer, true);
|
||||||
|
SetIndexBuffer(mediumColorBufferIndex, mediumColorBuffer, INDICATOR_COLOR_INDEX);
|
||||||
|
|
||||||
|
//
|
||||||
|
// SLOW ...
|
||||||
|
|
||||||
|
//
|
||||||
|
ArraySetAsSeries(slowBuffer, true);
|
||||||
|
SetIndexBuffer(slowBufferIndex, slowBuffer, INDICATOR_DATA);
|
||||||
|
|
||||||
|
//
|
||||||
|
PlotIndexSetInteger(slowBufferIndex, PLOT_SHOW_DATA, showSlow);
|
||||||
|
|
||||||
|
//
|
||||||
|
PlotIndexSetDouble(slowBufferIndex, PLOT_EMPTY_VALUE, emptyValue);
|
||||||
|
|
||||||
|
//
|
||||||
|
ArraySetAsSeries(slowColorBuffer, true);
|
||||||
|
SetIndexBuffer(slowColorBufferIndex, slowColorBuffer, INDICATOR_COLOR_INDEX);
|
||||||
|
|
||||||
|
//
|
||||||
|
// Data Buffers ...
|
||||||
|
|
||||||
|
//
|
||||||
|
// FAST State ...
|
||||||
|
ArraySetAsSeries(fastStateBuffer, true);
|
||||||
|
SetIndexBuffer(fastStateBufferIndex, fastStateBuffer, INDICATOR_CALCULATIONS);
|
||||||
|
|
||||||
|
//
|
||||||
|
// MEDIUM State ...
|
||||||
|
ArraySetAsSeries(mediumStateBuffer, true);
|
||||||
|
SetIndexBuffer(mediumStateBufferIndex, mediumStateBuffer, INDICATOR_CALCULATIONS);
|
||||||
|
|
||||||
|
//
|
||||||
|
// SLOW State ...
|
||||||
|
ArraySetAsSeries(slowStateBuffer, true);
|
||||||
|
SetIndexBuffer(slowStateBufferIndex, slowStateBuffer, INDICATOR_CALCULATIONS);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set Indicator Short Name and also we can define Buffers Labels ...
|
||||||
|
*/
|
||||||
|
void SetIndicatorName()
|
||||||
|
{
|
||||||
|
//
|
||||||
|
IndicatorSetInteger(INDICATOR_DIGITS, _Digits);
|
||||||
|
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 Values ...
|
||||||
|
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)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// FAST ...
|
||||||
|
fastBuffer[barIndex] = emptyValue;
|
||||||
|
fastColorBuffer[barIndex] = hideColorIDX;
|
||||||
|
fastStateBuffer[barIndex] = neuturalState;
|
||||||
|
|
||||||
|
//
|
||||||
|
// MEDIUM ...
|
||||||
|
mediumBuffer[barIndex] = emptyValue;
|
||||||
|
mediumColorBuffer[barIndex] = hideColorIDX;
|
||||||
|
mediumStateBuffer[barIndex] = neuturalState;
|
||||||
|
|
||||||
|
//
|
||||||
|
// SLOW ...
|
||||||
|
slowBuffer[barIndex] = emptyValue;
|
||||||
|
slowColorBuffer[barIndex] = hideColorIDX;
|
||||||
|
slowStateBuffer[barIndex] = neuturalState;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calculate Values ...
|
||||||
|
*
|
||||||
|
* @param bar_index: int, Specified Bar Index ...
|
||||||
|
* @param prevCalculated: int, Provides Previous Calculated Bars ...
|
||||||
|
* @param ratesTotal: int, Provides All Availabled Bars ...
|
||||||
|
* @param open: double Collection, Provides Open Prices Time Series ...
|
||||||
|
* @param high: double Collection, Provides High Prices Time Series ...
|
||||||
|
* @param close: double Collection, Provides Close Prices Time Series ...
|
||||||
|
* @param low: double Collection, Provides Low Prices Time Series ...
|
||||||
|
* @param tickVolume: long Collection, Provides Ticks Volume Prices Time Series ...
|
||||||
|
*/
|
||||||
|
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 == ratesTotal;
|
||||||
|
|
||||||
|
//
|
||||||
|
double iLow = low[bar_index];
|
||||||
|
double iHigh = high[bar_index];
|
||||||
|
double iClose = close[bar_index];
|
||||||
|
|
||||||
|
//
|
||||||
|
// FAST ...
|
||||||
|
|
||||||
|
//
|
||||||
|
double iFast = fastBuffer[bar_index];
|
||||||
|
|
||||||
|
//
|
||||||
|
double iFastState =
|
||||||
|
iLow > iFast
|
||||||
|
? bullishState
|
||||||
|
: iHigh < iFast
|
||||||
|
? bearishState
|
||||||
|
: neuturalState;
|
||||||
|
double iFastColor = hideColorIDX;
|
||||||
|
if (showFast)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
if (applyColor)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
iFastColor =
|
||||||
|
iFastState == bullishState
|
||||||
|
? bullishColorIDX
|
||||||
|
: iFastState == bearishState
|
||||||
|
? bearishColorIDX
|
||||||
|
: neuturalColorIDX;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
iFastColor = bullishColorIDX;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fastColorBuffer[bar_index] = iFastColor;
|
||||||
|
fastStateBuffer[bar_index] = iFastState;
|
||||||
|
|
||||||
|
//
|
||||||
|
// MEDIUM ...
|
||||||
|
|
||||||
|
//
|
||||||
|
double iMedium = mediumBuffer[bar_index];
|
||||||
|
|
||||||
|
//
|
||||||
|
double iMediumState =
|
||||||
|
iLow > iMedium
|
||||||
|
? bullishState
|
||||||
|
: iHigh < iMedium
|
||||||
|
? bearishState
|
||||||
|
: neuturalState;
|
||||||
|
double iMediumColor = hideColorIDX;
|
||||||
|
if (showMedium)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
if (applyColor)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
iMediumColor =
|
||||||
|
iMediumState == bullishState
|
||||||
|
? bullishColorIDX
|
||||||
|
: iMediumState == bearishState
|
||||||
|
? bearishColorIDX
|
||||||
|
: neuturalColorIDX;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
iMediumColor = bullishColorIDX;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
mediumColorBuffer[bar_index] = iMediumColor;
|
||||||
|
mediumStateBuffer[bar_index] = iMediumState;
|
||||||
|
|
||||||
|
//
|
||||||
|
// SLOW ...
|
||||||
|
|
||||||
|
//
|
||||||
|
double iSlow = slowBuffer[bar_index];
|
||||||
|
|
||||||
|
//
|
||||||
|
double iSlowState =
|
||||||
|
iLow > iSlow
|
||||||
|
? bullishState
|
||||||
|
: iHigh < iSlow
|
||||||
|
? bearishState
|
||||||
|
: neuturalState;
|
||||||
|
double iSlowColor = hideColorIDX;
|
||||||
|
if (showSlow)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
if (applyColor)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
iSlowColor =
|
||||||
|
iSlowState == bullishState
|
||||||
|
? bullishColorIDX
|
||||||
|
: iSlowState == bearishState
|
||||||
|
? bearishColorIDX
|
||||||
|
: neuturalColorIDX;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
iSlowColor = bullishColorIDX;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
slowColorBuffer[bar_index] = iSlowColor;
|
||||||
|
slowStateBuffer[bar_index] = iSlowState;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
@@ -0,0 +1,882 @@
|
|||||||
|
///////////////////////////////////////////////////////
|
||||||
|
//
|
||||||
|
// 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 "../Libraries/x-saherelm.common.lib.mq5"
|
||||||
|
|
||||||
|
//
|
||||||
|
// Inputs ...
|
||||||
|
|
||||||
|
//
|
||||||
|
// Market ...
|
||||||
|
input group "Market";
|
||||||
|
|
||||||
|
//
|
||||||
|
input group "RSI Detection";
|
||||||
|
input int rsiLength = 14; // Length
|
||||||
|
input ENUM_X_PRICE rsiPriceType = X_PRICE_CLOSE; // Applied To
|
||||||
|
input ENUM_X_MA_METHOD rsiSmoothingMethod = X_MA_MODE_SMA; // Smoothing Method;
|
||||||
|
|
||||||
|
//
|
||||||
|
input group "ATR Detection";
|
||||||
|
input int atrLength = 14; // Length
|
||||||
|
input double atrMultiplier = 1; // Multiplier
|
||||||
|
input ENUM_X_PRICE atrUpperPriceType = X_PRICE_HIGH; // Upper Zone Applied To
|
||||||
|
input ENUM_X_PRICE atrLowerPriceType = X_PRICE_LOW; // Lower Zone Applied To
|
||||||
|
input ENUM_X_MA_METHOD atrSmoothingMethod = X_MA_MODE_SMA; // Smoothing Method
|
||||||
|
|
||||||
|
//
|
||||||
|
input group "Price Change";
|
||||||
|
input int priceChangeSmoothingLength = 14; // Length
|
||||||
|
input ENUM_X_PRICE priceChangeType = X_PRICE_CLOSE; // Price Type
|
||||||
|
input ENUM_X_MA_METHOD priceChangeSmoothingMethod = X_MA_MODE_SMA; // Moving Average Moethod
|
||||||
|
|
||||||
|
//
|
||||||
|
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
|
||||||
|
input bool showSmoothedAtrUpper = true; // Show Smoothed Upper Zone
|
||||||
|
input bool showSmoothedAtrLower = true; // Show Smoothed Lower Zone
|
||||||
|
input bool showRSIChange = true; // Show RSI Change
|
||||||
|
input bool showSmoothedRSIChange = true; // ShowSmoothed RSI Change
|
||||||
|
input bool showPriceChange = true; // Show Price Change
|
||||||
|
input bool showSmoothedPriceChange = true; // ShowSmoothed Price Change
|
||||||
|
|
||||||
|
//
|
||||||
|
// Buffers ...
|
||||||
|
|
||||||
|
//
|
||||||
|
#define hideColorIDX 0
|
||||||
|
#define bullishColorIDX 1
|
||||||
|
#define bearishColorIDX 2
|
||||||
|
#define neuturalColorIDX 3
|
||||||
|
|
||||||
|
//
|
||||||
|
#property indicator_chart_window
|
||||||
|
|
||||||
|
//
|
||||||
|
#property indicator_buffers 10
|
||||||
|
#property indicator_plots 8
|
||||||
|
|
||||||
|
//
|
||||||
|
// 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 1
|
||||||
|
|
||||||
|
//
|
||||||
|
// 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 1
|
||||||
|
|
||||||
|
//
|
||||||
|
// ATR Smoothed ...
|
||||||
|
|
||||||
|
//
|
||||||
|
// Upper ...
|
||||||
|
|
||||||
|
//
|
||||||
|
#define atrSmoothedUpperBufferIndex 2
|
||||||
|
double atrSmoothedUpperBuffer[];
|
||||||
|
|
||||||
|
#property indicator_label3 "X121 ATRUSM"
|
||||||
|
#property indicator_type3 DRAW_LINE
|
||||||
|
#property indicator_color3 clrYellow
|
||||||
|
#property indicator_style3 STYLE_DASH
|
||||||
|
#property indicator_width3 1
|
||||||
|
|
||||||
|
//
|
||||||
|
// Lower ...
|
||||||
|
|
||||||
|
//
|
||||||
|
#define atrSmoothedLowerBufferIndex 3
|
||||||
|
double atrSmoothedLowerBuffer[];
|
||||||
|
|
||||||
|
#property indicator_label4 "X121 ATRLSM"
|
||||||
|
#property indicator_type4 DRAW_LINE
|
||||||
|
#property indicator_color4 clrYellow
|
||||||
|
#property indicator_style4 STYLE_DASH
|
||||||
|
#property indicator_width4 1
|
||||||
|
|
||||||
|
//
|
||||||
|
// Price Change ...
|
||||||
|
|
||||||
|
//
|
||||||
|
#define priceChangeBufferIndex 4
|
||||||
|
double priceChangeBuffer[];
|
||||||
|
|
||||||
|
#property indicator_label5 "X121 PCH"
|
||||||
|
#property indicator_type5 DRAW_LINE
|
||||||
|
#property indicator_color5 clrOrchid
|
||||||
|
#property indicator_style5 STYLE_SOLID
|
||||||
|
#property indicator_width5 1
|
||||||
|
|
||||||
|
//
|
||||||
|
// Price Change Smoothed ...
|
||||||
|
|
||||||
|
//
|
||||||
|
#define priceChangeSmoothedBufferIndex 5
|
||||||
|
double priceChangeSmoothedBuffer[];
|
||||||
|
|
||||||
|
#property indicator_label6 "X121 PCHSM"
|
||||||
|
#property indicator_type6 DRAW_LINE
|
||||||
|
#property indicator_color6 clrOrchid
|
||||||
|
#property indicator_style6 STYLE_SOLID
|
||||||
|
#property indicator_width6 1
|
||||||
|
|
||||||
|
//
|
||||||
|
// RSI Change ...
|
||||||
|
|
||||||
|
//
|
||||||
|
#define rsiChangeBufferIndex 6
|
||||||
|
double rsiChangeBuffer[];
|
||||||
|
|
||||||
|
#property indicator_label7 "X121 RSICH"
|
||||||
|
#property indicator_type7 DRAW_LINE
|
||||||
|
#property indicator_color7 clrCornflowerBlue
|
||||||
|
#property indicator_style7 STYLE_SOLID
|
||||||
|
#property indicator_width7 1
|
||||||
|
|
||||||
|
//
|
||||||
|
// RSI Change Smoothed ...
|
||||||
|
|
||||||
|
//
|
||||||
|
#define rsiChangeSmoothedBufferIndex 7
|
||||||
|
double rsiChangeSmoothedBuffer[];
|
||||||
|
|
||||||
|
#property indicator_label8 "X121 RSICHSM"
|
||||||
|
#property indicator_type8 DRAW_LINE
|
||||||
|
#property indicator_color8 clrCornflowerBlue
|
||||||
|
#property indicator_style8 STYLE_SOLID
|
||||||
|
#property indicator_width8 1
|
||||||
|
|
||||||
|
//
|
||||||
|
// Data Buffers ...
|
||||||
|
|
||||||
|
//
|
||||||
|
int mLastBufferIndex = 7;
|
||||||
|
|
||||||
|
//
|
||||||
|
// ATR ...
|
||||||
|
|
||||||
|
#define atrBufferIndex mLastBufferIndex + 1
|
||||||
|
double atrBuffer[];
|
||||||
|
|
||||||
|
//
|
||||||
|
// RSI ...
|
||||||
|
#define rsiBufferIndex mLastBufferIndex + 2
|
||||||
|
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;
|
||||||
|
|
||||||
|
//
|
||||||
|
ENUM_APPLIED_PRICE rsiAppliedTo = ToAppliedPrice(rsiPriceType);
|
||||||
|
|
||||||
|
//
|
||||||
|
// 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 &&
|
||||||
|
//
|
||||||
|
priceChangeSmoothingLength > 0 &&
|
||||||
|
//
|
||||||
|
IsValid(rsiPriceType) &&
|
||||||
|
IsValid(priceChangeType) &&
|
||||||
|
IsValid(atrUpperPriceType) &&
|
||||||
|
IsValid(atrLowerPriceType)
|
||||||
|
//
|
||||||
|
;
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Extract Max Length of Inputs ...
|
||||||
|
*
|
||||||
|
* @return ( int )
|
||||||
|
*/
|
||||||
|
int ExtractMaxLengthOfInputs()
|
||||||
|
{
|
||||||
|
//
|
||||||
|
int result = 0;
|
||||||
|
|
||||||
|
//
|
||||||
|
result = MathMax(rsiLength, atrLength);
|
||||||
|
result = MathMax(result, priceChangeSmoothingLength);
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Define Required Buffers ...
|
||||||
|
*/
|
||||||
|
void DefineBuffers()
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// ATR ...
|
||||||
|
|
||||||
|
//
|
||||||
|
// UPPER ...
|
||||||
|
ENUM_DRAW_TYPE atrUpperDrawType = showAtrUpper ? DRAW_LINE : DRAW_NONE;
|
||||||
|
ArraySetAsSeries(atrUpperBuffer, true);
|
||||||
|
SetIndexBuffer(atrUpperBufferIndex, atrUpperBuffer, INDICATOR_DATA);
|
||||||
|
PlotIndexSetDouble(atrUpperBufferIndex, PLOT_EMPTY_VALUE, EMPTY_VALUE);
|
||||||
|
PlotIndexSetInteger(atrUpperBufferIndex, PLOT_SHOW_DATA, showAtrUpper);
|
||||||
|
PlotIndexSetInteger(atrUpperBufferIndex, PLOT_DRAW_TYPE, atrUpperDrawType);
|
||||||
|
|
||||||
|
//
|
||||||
|
// LOWER ...
|
||||||
|
ENUM_DRAW_TYPE atrLowerDrawType = showAtrLower ? DRAW_LINE : DRAW_NONE;
|
||||||
|
ArraySetAsSeries(atrLowerBuffer, true);
|
||||||
|
SetIndexBuffer(atrLowerBufferIndex, atrLowerBuffer, INDICATOR_DATA);
|
||||||
|
PlotIndexSetDouble(atrLowerBufferIndex, PLOT_EMPTY_VALUE, EMPTY_VALUE);
|
||||||
|
PlotIndexSetInteger(atrLowerBufferIndex, PLOT_SHOW_DATA, showAtrLower);
|
||||||
|
PlotIndexSetInteger(atrLowerBufferIndex, PLOT_DRAW_TYPE, atrLowerDrawType);
|
||||||
|
|
||||||
|
//
|
||||||
|
// ATR Smoothed ...
|
||||||
|
|
||||||
|
//
|
||||||
|
// UPPER ...
|
||||||
|
ENUM_DRAW_TYPE atrSmoothedUpperDrawType = showSmoothedAtrUpper ? DRAW_LINE : DRAW_NONE;
|
||||||
|
ArraySetAsSeries(atrSmoothedUpperBuffer, true);
|
||||||
|
SetIndexBuffer(atrSmoothedUpperBufferIndex, atrSmoothedUpperBuffer, INDICATOR_DATA);
|
||||||
|
PlotIndexSetDouble(atrSmoothedUpperBufferIndex, PLOT_EMPTY_VALUE, EMPTY_VALUE);
|
||||||
|
PlotIndexSetInteger(atrSmoothedUpperBufferIndex, PLOT_SHOW_DATA, showSmoothedAtrUpper);
|
||||||
|
PlotIndexSetInteger(atrSmoothedUpperBufferIndex, PLOT_DRAW_TYPE, atrSmoothedUpperDrawType);
|
||||||
|
|
||||||
|
//
|
||||||
|
// LOWER ...
|
||||||
|
ENUM_DRAW_TYPE atrSmoothedLowerDrawType = showSmoothedAtrLower ? DRAW_LINE : DRAW_NONE;
|
||||||
|
ArraySetAsSeries(atrSmoothedLowerBuffer, true);
|
||||||
|
SetIndexBuffer(atrSmoothedLowerBufferIndex, atrSmoothedLowerBuffer, INDICATOR_DATA);
|
||||||
|
PlotIndexSetDouble(atrSmoothedLowerBufferIndex, PLOT_EMPTY_VALUE, EMPTY_VALUE);
|
||||||
|
PlotIndexSetInteger(atrSmoothedLowerBufferIndex, PLOT_SHOW_DATA, showSmoothedAtrLower);
|
||||||
|
PlotIndexSetInteger(atrSmoothedLowerBufferIndex, PLOT_DRAW_TYPE, atrSmoothedLowerDrawType);
|
||||||
|
|
||||||
|
//
|
||||||
|
// PriceChange ...
|
||||||
|
ENUM_DRAW_TYPE priceChangeDrawType = showPriceChange ? DRAW_LINE : DRAW_NONE;
|
||||||
|
ArraySetAsSeries(priceChangeBuffer, true);
|
||||||
|
SetIndexBuffer(priceChangeBufferIndex, priceChangeBuffer, INDICATOR_DATA);
|
||||||
|
PlotIndexSetDouble(priceChangeBufferIndex, PLOT_EMPTY_VALUE, 0.0);
|
||||||
|
PlotIndexSetInteger(priceChangeBufferIndex, PLOT_SHOW_DATA, showPriceChange);
|
||||||
|
PlotIndexSetInteger(priceChangeBufferIndex, PLOT_DRAW_TYPE, priceChangeDrawType);
|
||||||
|
|
||||||
|
//
|
||||||
|
// PriceChange Smoothed ...
|
||||||
|
ENUM_DRAW_TYPE priceChangeSmoothedaDrawType = showSmoothedPriceChange ? DRAW_LINE : DRAW_NONE;
|
||||||
|
ArraySetAsSeries(priceChangeSmoothedBuffer, true);
|
||||||
|
SetIndexBuffer(priceChangeSmoothedBufferIndex, priceChangeSmoothedBuffer, INDICATOR_DATA);
|
||||||
|
PlotIndexSetDouble(priceChangeSmoothedBufferIndex, PLOT_EMPTY_VALUE, 0.0);
|
||||||
|
PlotIndexSetInteger(priceChangeSmoothedBufferIndex, PLOT_SHOW_DATA, showSmoothedPriceChange);
|
||||||
|
PlotIndexSetInteger(priceChangeSmoothedBufferIndex, PLOT_DRAW_TYPE, priceChangeSmoothedaDrawType);
|
||||||
|
|
||||||
|
//
|
||||||
|
// RSIChange ...
|
||||||
|
ENUM_DRAW_TYPE rsiChangeDrawType = showRSIChange ? DRAW_LINE : DRAW_NONE;
|
||||||
|
ArraySetAsSeries(rsiChangeBuffer, true);
|
||||||
|
SetIndexBuffer(rsiChangeBufferIndex, rsiChangeBuffer, INDICATOR_DATA);
|
||||||
|
PlotIndexSetDouble(rsiChangeBufferIndex, PLOT_EMPTY_VALUE, 0.0);
|
||||||
|
PlotIndexSetInteger(rsiChangeBufferIndex, PLOT_SHOW_DATA, showRSIChange);
|
||||||
|
PlotIndexSetInteger(rsiChangeBufferIndex, PLOT_DRAW_TYPE, rsiChangeDrawType);
|
||||||
|
|
||||||
|
//
|
||||||
|
// RSIChangeMa ...
|
||||||
|
ENUM_DRAW_TYPE rsiChangeMaDrawType = showSmoothedRSIChange ? DRAW_LINE : DRAW_NONE;
|
||||||
|
ArraySetAsSeries(rsiChangeSmoothedBuffer, true);
|
||||||
|
SetIndexBuffer(rsiChangeSmoothedBufferIndex, rsiChangeSmoothedBuffer, INDICATOR_DATA);
|
||||||
|
PlotIndexSetDouble(rsiChangeSmoothedBufferIndex, PLOT_EMPTY_VALUE, 0.0);
|
||||||
|
PlotIndexSetInteger(rsiChangeSmoothedBufferIndex, PLOT_SHOW_DATA, showSmoothedRSIChange);
|
||||||
|
PlotIndexSetInteger(rsiChangeSmoothedBufferIndex, PLOT_DRAW_TYPE, rsiChangeMaDrawType);
|
||||||
|
|
||||||
|
//
|
||||||
|
// Data Buffers ...
|
||||||
|
|
||||||
|
//
|
||||||
|
// ATR ...
|
||||||
|
ArraySetAsSeries(atrBuffer, true);
|
||||||
|
SetIndexBuffer(atrBufferIndex, atrBuffer, 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);
|
||||||
|
IndicatorSetInteger(INDICATOR_DIGITS, 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
rsiBuffer[barIndex] = 0;
|
||||||
|
atrBuffer[barIndex] = 0;
|
||||||
|
atrUpperBuffer[barIndex] = 0;
|
||||||
|
atrLowerBuffer[barIndex] = 0;
|
||||||
|
rsiChangeBuffer[barIndex] = 0;
|
||||||
|
priceChangeBuffer[barIndex] = 0;
|
||||||
|
atrSmoothedUpperBuffer[barIndex] = 0;
|
||||||
|
atrSmoothedLowerBuffer[barIndex] = 0;
|
||||||
|
rsiChangeSmoothedBuffer[barIndex] = 0;
|
||||||
|
priceChangeSmoothedBuffer[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[] //
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
double points = GetPoints(_Symbol);
|
||||||
|
|
||||||
|
//
|
||||||
|
// ATR Calculations ...
|
||||||
|
double iAtr = atrBuffer[bar_index];
|
||||||
|
double iMultiPliedAtr = iAtr * atrMultiplier;
|
||||||
|
|
||||||
|
//
|
||||||
|
// Select Upper Price ...
|
||||||
|
double iUpperPrice = GetAppliedPrice(
|
||||||
|
atrUpperPriceType,
|
||||||
|
open,
|
||||||
|
high,
|
||||||
|
low,
|
||||||
|
close,
|
||||||
|
bar_index //
|
||||||
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
// Select Lower Price ...
|
||||||
|
double iLowerPrice = GetAppliedPrice(
|
||||||
|
atrLowerPriceType,
|
||||||
|
open,
|
||||||
|
high,
|
||||||
|
low,
|
||||||
|
close,
|
||||||
|
bar_index //
|
||||||
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
// Calculate Atrs ...
|
||||||
|
|
||||||
|
//
|
||||||
|
double iAtrUpper = iUpperPrice + iMultiPliedAtr;
|
||||||
|
double iAtrLower = iLowerPrice - iMultiPliedAtr;
|
||||||
|
|
||||||
|
//
|
||||||
|
atrUpperBuffer[bar_index] = iAtrUpper;
|
||||||
|
atrLowerBuffer[bar_index] = iAtrLower;
|
||||||
|
|
||||||
|
//
|
||||||
|
bool canSmoothAtr = atrSmoothingMethod != X_MA_MODE_NONE;
|
||||||
|
if (canSmoothAtr)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Upper ...
|
||||||
|
iMAOnBuffer(
|
||||||
|
ratesTotal,
|
||||||
|
prevCalculated,
|
||||||
|
bar_index,
|
||||||
|
atrLength,
|
||||||
|
atrUpperBuffer,
|
||||||
|
atrSmoothedUpperBuffer,
|
||||||
|
atrSmoothingMethod //
|
||||||
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
// Lower ...
|
||||||
|
iMAOnBuffer(
|
||||||
|
ratesTotal,
|
||||||
|
prevCalculated,
|
||||||
|
bar_index,
|
||||||
|
atrLength,
|
||||||
|
atrLowerBuffer,
|
||||||
|
atrSmoothedLowerBuffer,
|
||||||
|
atrSmoothingMethod //
|
||||||
|
);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
//
|
||||||
|
atrSmoothedUpperBuffer[bar_index] = iAtrUpper;
|
||||||
|
atrSmoothedLowerBuffer[bar_index] = iAtrLower;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// RSI Change Calculations ...
|
||||||
|
|
||||||
|
//
|
||||||
|
double iRsi = rsiBuffer[bar_index];
|
||||||
|
double iRsiP = rsiBuffer[bar_index + 1];
|
||||||
|
double iRsiPrice = GetAppliedPrice(
|
||||||
|
rsiPriceType,
|
||||||
|
open,
|
||||||
|
high,
|
||||||
|
low,
|
||||||
|
close,
|
||||||
|
bar_index //
|
||||||
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
double iRsiChanged = iRsi - iRsiP;
|
||||||
|
double iRsiPointsChanged = iRsiChanged / points;
|
||||||
|
|
||||||
|
//
|
||||||
|
double iRChange = iRsiPrice + (iRsiChanged * points);
|
||||||
|
rsiChangeBuffer[bar_index] = iRChange;
|
||||||
|
|
||||||
|
//
|
||||||
|
bool canSmoothRsi = rsiSmoothingMethod != X_MA_MODE_NONE;
|
||||||
|
if (canSmoothRsi)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Upper ...
|
||||||
|
iMAOnBuffer(
|
||||||
|
ratesTotal,
|
||||||
|
prevCalculated,
|
||||||
|
bar_index,
|
||||||
|
atrLength,
|
||||||
|
rsiChangeBuffer,
|
||||||
|
rsiChangeSmoothedBuffer,
|
||||||
|
rsiSmoothingMethod //
|
||||||
|
);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
rsiChangeSmoothedBuffer[bar_index] = iRChange;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// PRICE Change Calculation ...
|
||||||
|
|
||||||
|
//
|
||||||
|
double iPChangePrice = GetAppliedPrice(
|
||||||
|
priceChangeType,
|
||||||
|
open,
|
||||||
|
high,
|
||||||
|
low,
|
||||||
|
close,
|
||||||
|
bar_index //
|
||||||
|
);
|
||||||
|
double iPPChangePrice = GetAppliedPrice(
|
||||||
|
priceChangeType,
|
||||||
|
open,
|
||||||
|
high,
|
||||||
|
low,
|
||||||
|
close,
|
||||||
|
bar_index + 1 //
|
||||||
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
double iPriceChange = iPChangePrice - iPPChangePrice;
|
||||||
|
double iPricePointsChanged = iPriceChange / points;
|
||||||
|
double iVolatilityChange = iPriceChange / iAtr;
|
||||||
|
|
||||||
|
//
|
||||||
|
double iPChange = iPChangePrice + (iPricePointsChanged * points);
|
||||||
|
priceChangeBuffer[bar_index] = iPChange;
|
||||||
|
|
||||||
|
//
|
||||||
|
bool canSmoothPriceChange = priceChangeSmoothingMethod != X_MA_MODE_NONE;
|
||||||
|
if (canSmoothPriceChange)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Upper ...
|
||||||
|
iMAOnBuffer(
|
||||||
|
ratesTotal,
|
||||||
|
prevCalculated,
|
||||||
|
bar_index,
|
||||||
|
atrLength,
|
||||||
|
priceChangeBuffer,
|
||||||
|
priceChangeSmoothedBuffer,
|
||||||
|
priceChangeSmoothingMethod //
|
||||||
|
);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
priceChangeSmoothedBuffer[bar_index] = iPChange;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,437 @@
|
|||||||
|
///////////////////////////////////////////////////////
|
||||||
|
//
|
||||||
|
// SaherElm IT Center MQL5 Candle Styles
|
||||||
|
// ---------------------------------------------------
|
||||||
|
// Name: X121 XCC
|
||||||
|
// Description: Candle Styles ...
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 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 XCC Indicator"
|
||||||
|
#property strict
|
||||||
|
|
||||||
|
//
|
||||||
|
// START Constants ...
|
||||||
|
//
|
||||||
|
|
||||||
|
//
|
||||||
|
// Indicator Short Name ...
|
||||||
|
#define ShortName "X121 XCC"
|
||||||
|
|
||||||
|
//
|
||||||
|
// Holds an SnapShot of Charts Configuration ...
|
||||||
|
struct XChartStyle
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// chart's ID ...
|
||||||
|
long chartId;
|
||||||
|
//
|
||||||
|
// chart's mode ...
|
||||||
|
ENUM_CHART_MODE mode;
|
||||||
|
//
|
||||||
|
// show bid line ...
|
||||||
|
bool showBidLine;
|
||||||
|
//
|
||||||
|
// show ask line ...
|
||||||
|
bool showAskLine;
|
||||||
|
//
|
||||||
|
// show grids on chart ...
|
||||||
|
bool showGrid;
|
||||||
|
//
|
||||||
|
// show volumes ...
|
||||||
|
bool showVolumes;
|
||||||
|
//
|
||||||
|
// Displaying trade levels in the chart (levels of open positions, Stop Loss, Take Profit and pending orders) ...
|
||||||
|
bool showTradeLevels;
|
||||||
|
//
|
||||||
|
// chart autoscroll ...
|
||||||
|
bool autoScroll;
|
||||||
|
//
|
||||||
|
// chart quick navigation state ...
|
||||||
|
bool quickNavigation;
|
||||||
|
//
|
||||||
|
// chart's foreground color ...
|
||||||
|
color foreGroundColor;
|
||||||
|
//
|
||||||
|
// chart's background color ...
|
||||||
|
color backGroundColor;
|
||||||
|
//
|
||||||
|
// Up Color ...
|
||||||
|
color upColor;
|
||||||
|
//
|
||||||
|
// Down Color ...
|
||||||
|
color downColor;
|
||||||
|
//
|
||||||
|
// Bullish color ...
|
||||||
|
color bullishColor;
|
||||||
|
//
|
||||||
|
// Bearish color ...
|
||||||
|
color bearishColor;
|
||||||
|
//
|
||||||
|
// grid color ...
|
||||||
|
color gridColor;
|
||||||
|
//
|
||||||
|
// bid line color ...
|
||||||
|
color bidLineColor;
|
||||||
|
//
|
||||||
|
// ask line color ...
|
||||||
|
color askLineColor;
|
||||||
|
//
|
||||||
|
// line mode and doji candlestick color ...
|
||||||
|
color lineColor;
|
||||||
|
//
|
||||||
|
// Color of stop order levels (Stop Loss and Take Profit) ...
|
||||||
|
color stopColor;
|
||||||
|
//
|
||||||
|
// volumes color ...
|
||||||
|
color volumesColor;
|
||||||
|
};
|
||||||
|
|
||||||
|
//
|
||||||
|
// END Constants ...
|
||||||
|
//
|
||||||
|
|
||||||
|
//
|
||||||
|
// START Inputs ...
|
||||||
|
//
|
||||||
|
|
||||||
|
//
|
||||||
|
input group "Chart Style";
|
||||||
|
input ENUM_CHART_MODE mode = CHART_CANDLES; // Mode
|
||||||
|
input color upColor = clrGreen; // Up Color
|
||||||
|
input color downColor = clrRed; // Down Color
|
||||||
|
input color lineColor = clrGreen; // Line mode and Doji candlestick Color
|
||||||
|
input color bullishColor = clrGreen; // Bullish Color
|
||||||
|
input color bearishColor = clrRed; // Bearish Color
|
||||||
|
input color volumesColor = clrGreen; // Volumes Color
|
||||||
|
|
||||||
|
//
|
||||||
|
input group "Presentation";
|
||||||
|
input bool showCandles = true; // Show Candles
|
||||||
|
|
||||||
|
//
|
||||||
|
// END Inputs ...
|
||||||
|
//
|
||||||
|
|
||||||
|
//
|
||||||
|
// START Includes and Imports ...
|
||||||
|
//
|
||||||
|
|
||||||
|
//
|
||||||
|
// Includes Draw Library ...
|
||||||
|
#include "../Libraries/x-saherelm.draw.lib.mq5"
|
||||||
|
|
||||||
|
//
|
||||||
|
// Includes Common Library ...
|
||||||
|
#include "../Libraries/x-saherelm.common.lib.mq5"
|
||||||
|
|
||||||
|
//
|
||||||
|
// END Includes and Imports ...
|
||||||
|
//
|
||||||
|
|
||||||
|
//
|
||||||
|
// START Buffers ...
|
||||||
|
//
|
||||||
|
|
||||||
|
//
|
||||||
|
#property indicator_chart_window
|
||||||
|
|
||||||
|
//
|
||||||
|
#property indicator_buffers 0
|
||||||
|
#property indicator_plots 0
|
||||||
|
|
||||||
|
//
|
||||||
|
// END Buffers ...
|
||||||
|
//
|
||||||
|
|
||||||
|
//
|
||||||
|
// START Global Definitions: Variables, Properties and etc ...
|
||||||
|
//
|
||||||
|
|
||||||
|
//
|
||||||
|
int maxLength;
|
||||||
|
|
||||||
|
//
|
||||||
|
XChartStyle chartStyle;
|
||||||
|
XChartStyle clearStyle;
|
||||||
|
|
||||||
|
//
|
||||||
|
// END Global Definitions: Variables, Properties and etc ...
|
||||||
|
//
|
||||||
|
|
||||||
|
//
|
||||||
|
// START Event Handlers ...
|
||||||
|
//
|
||||||
|
|
||||||
|
//
|
||||||
|
// Initialization ...
|
||||||
|
int OnInit()
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Validate Inputs ...
|
||||||
|
if (!ValidateInputs())
|
||||||
|
{
|
||||||
|
return INIT_PARAMETERS_INCORRECT;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
drawPrefix = ShortName;
|
||||||
|
|
||||||
|
//
|
||||||
|
// 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();
|
||||||
|
|
||||||
|
//
|
||||||
|
// Apply Chart Style ...
|
||||||
|
ApplyCustomChartStyle();
|
||||||
|
|
||||||
|
//
|
||||||
|
// 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
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Calculating what we want ...
|
||||||
|
int OnCalculate(
|
||||||
|
const int rates_total, // Total Bars on Chart ...
|
||||||
|
const int prev_calculated, // Total Calculated Bars on Charts ...
|
||||||
|
const datetime &time[], // History of Open Time ...
|
||||||
|
const double &open[], // History of Open Price ...
|
||||||
|
const double &high[], // History of High Price ...
|
||||||
|
const double &low[], // History of Low Price ...
|
||||||
|
const double &close[], // History of Close Price ...
|
||||||
|
const long &tick_volume[], // History of Tick Volumes on Bar ...
|
||||||
|
const long &volume[], // History of Trade Volumes ...
|
||||||
|
const int &spread[] // History of Spread Price ...
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// 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;
|
||||||
|
|
||||||
|
//
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
return rates_total;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// END Event Handlers ...
|
||||||
|
//
|
||||||
|
|
||||||
|
//
|
||||||
|
// START Functions ...
|
||||||
|
//
|
||||||
|
|
||||||
|
//
|
||||||
|
// Validate Input Args for Initialization ...
|
||||||
|
bool ValidateInputs()
|
||||||
|
{
|
||||||
|
//
|
||||||
|
bool result = true;
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Retrieve all Exists Input Max Length ...
|
||||||
|
// use for Start Of Drawing ...
|
||||||
|
int ExtractMaxLengthOfInputs()
|
||||||
|
{
|
||||||
|
//
|
||||||
|
int result = 0;
|
||||||
|
|
||||||
|
//
|
||||||
|
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 // Selected Bar Index
|
||||||
|
)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Apply Custom Chart Style ...
|
||||||
|
void ApplyCustomChartStyle()
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Read Current Chart Config and Store it ...
|
||||||
|
ReadChartStyle();
|
||||||
|
|
||||||
|
//
|
||||||
|
// After Reading Current Chart Style ...
|
||||||
|
// we have to Save Configs and Change Styles ...
|
||||||
|
clearStyle = chartStyle;
|
||||||
|
|
||||||
|
//
|
||||||
|
clearStyle.upColor = CLR_NONE;
|
||||||
|
clearStyle.downColor = CLR_NONE;
|
||||||
|
clearStyle.lineColor = CLR_NONE;
|
||||||
|
clearStyle.bullishColor = CLR_NONE;
|
||||||
|
clearStyle.bearishColor = CLR_NONE;
|
||||||
|
|
||||||
|
//
|
||||||
|
// Decide to Show or not Candles based on User Input ...
|
||||||
|
if (showCandles)
|
||||||
|
{
|
||||||
|
SetChartStyle(chartStyle);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
SetChartStyle(clearStyle);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Read Previous Chart Style ...
|
||||||
|
void ReadChartStyle()
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Retrieve Current Chart ID ...
|
||||||
|
long chartId = ChartID();
|
||||||
|
chartStyle.chartId = chartId;
|
||||||
|
|
||||||
|
//
|
||||||
|
// Retrieve Chart Mode ...
|
||||||
|
chartStyle.mode = (ENUM_CHART_MODE)ChartGetInteger(chartId, CHART_MODE);
|
||||||
|
|
||||||
|
//
|
||||||
|
chartStyle.showBidLine = (bool)ChartGetInteger(chartId, CHART_SHOW_BID_LINE);
|
||||||
|
chartStyle.showAskLine = (bool)ChartGetInteger(chartId, CHART_SHOW_ASK_LINE);
|
||||||
|
chartStyle.showGrid = (bool)ChartGetInteger(chartId, CHART_SHOW_GRID);
|
||||||
|
chartStyle.showVolumes = (bool)ChartGetInteger(chartId, CHART_SHOW_VOLUMES);
|
||||||
|
chartStyle.showTradeLevels = (bool)ChartGetInteger(chartId, CHART_SHOW_TRADE_LEVELS);
|
||||||
|
chartStyle.autoScroll = (bool)ChartGetInteger(chartId, CHART_AUTOSCROLL);
|
||||||
|
chartStyle.quickNavigation = (bool)ChartGetInteger(chartId, CHART_QUICK_NAVIGATION);
|
||||||
|
|
||||||
|
//
|
||||||
|
chartStyle.foreGroundColor = (color)ChartGetInteger(chartId, CHART_COLOR_FOREGROUND);
|
||||||
|
chartStyle.backGroundColor = (color)ChartGetInteger(chartId, CHART_COLOR_BACKGROUND);
|
||||||
|
chartStyle.upColor = (color)ChartGetInteger(chartId, CHART_COLOR_CHART_UP);
|
||||||
|
chartStyle.downColor = (color)ChartGetInteger(chartId, CHART_COLOR_CHART_DOWN);
|
||||||
|
chartStyle.bullishColor = (color)ChartGetInteger(chartId, CHART_COLOR_CANDLE_BULL);
|
||||||
|
chartStyle.bearishColor = (color)ChartGetInteger(chartId, CHART_COLOR_CANDLE_BEAR);
|
||||||
|
chartStyle.gridColor = (color)ChartGetInteger(chartId, CHART_COLOR_GRID);
|
||||||
|
chartStyle.bidLineColor = (color)ChartGetInteger(chartId, CHART_COLOR_BID);
|
||||||
|
chartStyle.askLineColor = (color)ChartGetInteger(chartId, CHART_COLOR_ASK);
|
||||||
|
chartStyle.stopColor = (color)ChartGetInteger(chartId, CHART_COLOR_STOP_LEVEL);
|
||||||
|
chartStyle.lineColor = (color)ChartGetInteger(chartId, CHART_COLOR_CHART_LINE);
|
||||||
|
chartStyle.volumesColor = (color)ChartGetInteger(chartId, CHART_COLOR_VOLUME);
|
||||||
|
|
||||||
|
//
|
||||||
|
chartStyle.mode = mode;
|
||||||
|
chartStyle.upColor = upColor;
|
||||||
|
chartStyle.downColor = downColor;
|
||||||
|
chartStyle.lineColor = lineColor;
|
||||||
|
chartStyle.bearishColor = bearishColor;
|
||||||
|
chartStyle.bullishColor = bullishColor;
|
||||||
|
chartStyle.volumesColor = volumesColor;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Set Chart Style ...
|
||||||
|
void SetChartStyle(
|
||||||
|
XChartStyle &mChartStyle // Chart Style to Apply
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
ApplyChartStyle(
|
||||||
|
mChartStyle.chartId,
|
||||||
|
mChartStyle.mode,
|
||||||
|
mChartStyle.showBidLine,
|
||||||
|
mChartStyle.showAskLine,
|
||||||
|
mChartStyle.showGrid,
|
||||||
|
mChartStyle.showVolumes,
|
||||||
|
mChartStyle.showTradeLevels,
|
||||||
|
mChartStyle.autoScroll,
|
||||||
|
mChartStyle.quickNavigation,
|
||||||
|
mChartStyle.foreGroundColor,
|
||||||
|
mChartStyle.backGroundColor,
|
||||||
|
mChartStyle.upColor,
|
||||||
|
mChartStyle.downColor,
|
||||||
|
mChartStyle.bullishColor,
|
||||||
|
mChartStyle.bearishColor,
|
||||||
|
mChartStyle.gridColor,
|
||||||
|
mChartStyle.bidLineColor,
|
||||||
|
mChartStyle.askLineColor,
|
||||||
|
mChartStyle.lineColor,
|
||||||
|
mChartStyle.stopColor,
|
||||||
|
mChartStyle.volumesColor);
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// END Functions ...
|
||||||
|
//
|
||||||
@@ -0,0 +1,624 @@
|
|||||||
|
///////////////////////////////////////////////////////
|
||||||
|
//
|
||||||
|
// SaherElm IT Center MQL5 Indicator
|
||||||
|
// -------------------------------------------------
|
||||||
|
// Name: X121 XCHE
|
||||||
|
// Description: XCHE ...
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 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 XCHE Indicator"
|
||||||
|
#property strict
|
||||||
|
|
||||||
|
//
|
||||||
|
// Definitions ...
|
||||||
|
|
||||||
|
//
|
||||||
|
#define ShortName "X121 XCHE"
|
||||||
|
|
||||||
|
//
|
||||||
|
// Includes Common Library ...
|
||||||
|
#include "../Libraries/x-saherelm.common.lib.mq5"
|
||||||
|
|
||||||
|
//
|
||||||
|
// Inputs ...
|
||||||
|
|
||||||
|
//
|
||||||
|
// Market ...
|
||||||
|
input group "Market";
|
||||||
|
input int cheLength = 35; // Length
|
||||||
|
input int cheLoopback = 26; // Loopback
|
||||||
|
input double cheMultiplier1 = 3.0; // 1st Multiplier
|
||||||
|
input double cheMultiplier2 = 3.5; // 2nd Multiplier
|
||||||
|
input ENUM_APPLIED_PRICE cheUpperAppliedTo = PRICE_HIGH; // Upper Zone Applied to
|
||||||
|
input ENUM_APPLIED_PRICE cheLowerAppliedTo = PRICE_LOW; // Lower Zone Applied to
|
||||||
|
|
||||||
|
//
|
||||||
|
input group "Presentation";
|
||||||
|
|
||||||
|
//
|
||||||
|
input int cheArrowCode = 159; // CHE Arrow Code
|
||||||
|
|
||||||
|
//
|
||||||
|
input bool showLE1 = true; // Show 1st Long Exit
|
||||||
|
input bool showSE1 = true; // Show 1st Short Exit
|
||||||
|
input bool showLE2 = true; // Show 2st Long Exit
|
||||||
|
input bool showSE2 = true; // Show 2st Short Exit
|
||||||
|
|
||||||
|
//
|
||||||
|
// 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
|
||||||
|
|
||||||
|
//
|
||||||
|
// Exit 1 ...
|
||||||
|
|
||||||
|
//
|
||||||
|
// LONG ...
|
||||||
|
|
||||||
|
//
|
||||||
|
#define le1BufferIndex 0
|
||||||
|
double le1Buffer[];
|
||||||
|
|
||||||
|
#property indicator_label1 "X121 LE1"
|
||||||
|
#property indicator_type1 DRAW_LINE
|
||||||
|
#property indicator_color1 clrAqua
|
||||||
|
#property indicator_style1 STYLE_DOT
|
||||||
|
#property indicator_width1 1
|
||||||
|
|
||||||
|
//
|
||||||
|
// SHORT ...
|
||||||
|
|
||||||
|
//
|
||||||
|
#define se1BufferIndex 1
|
||||||
|
double se1Buffer[];
|
||||||
|
|
||||||
|
#property indicator_label2 "X121 SE1"
|
||||||
|
#property indicator_type2 DRAW_LINE
|
||||||
|
#property indicator_color2 clrMagenta
|
||||||
|
#property indicator_style2 STYLE_DOT
|
||||||
|
#property indicator_width2 1
|
||||||
|
|
||||||
|
//
|
||||||
|
// Exit 2 ...
|
||||||
|
|
||||||
|
//
|
||||||
|
// LONG ...
|
||||||
|
|
||||||
|
//
|
||||||
|
#define le2BufferIndex 2
|
||||||
|
double le2Buffer[];
|
||||||
|
|
||||||
|
#property indicator_label3 "X121 LE2"
|
||||||
|
#property indicator_type3 DRAW_LINE
|
||||||
|
#property indicator_color3 clrAqua
|
||||||
|
#property indicator_style3 STYLE_SOLID
|
||||||
|
#property indicator_width3 1
|
||||||
|
|
||||||
|
//
|
||||||
|
// SHORT ...
|
||||||
|
|
||||||
|
//
|
||||||
|
#define se2BufferIndex 3
|
||||||
|
double se2Buffer[];
|
||||||
|
|
||||||
|
#property indicator_label4 "X121 SE2"
|
||||||
|
#property indicator_type4 DRAW_LINE
|
||||||
|
#property indicator_color4 clrMagenta
|
||||||
|
#property indicator_style4 STYLE_SOLID
|
||||||
|
#property indicator_width4 1
|
||||||
|
|
||||||
|
//
|
||||||
|
// Start Buffers ...
|
||||||
|
|
||||||
|
//
|
||||||
|
#define le1StartBufferIndex 4
|
||||||
|
double le1StartBuffer[];
|
||||||
|
|
||||||
|
//
|
||||||
|
#property indicator_label5 "X121 LE1 S"
|
||||||
|
#property indicator_type5 DRAW_ARROW
|
||||||
|
#property indicator_color5 clrLime
|
||||||
|
|
||||||
|
//
|
||||||
|
#define se1StartBufferIndex 5
|
||||||
|
double se1StartBuffer[];
|
||||||
|
|
||||||
|
//
|
||||||
|
#property indicator_label6 "X121 SE1 S"
|
||||||
|
#property indicator_type6 DRAW_ARROW
|
||||||
|
#property indicator_color6 clrRed
|
||||||
|
|
||||||
|
//
|
||||||
|
#define le2StartBufferIndex 6
|
||||||
|
double le2StartBuffer[];
|
||||||
|
|
||||||
|
//
|
||||||
|
#property indicator_label7 "X121 LE2 S"
|
||||||
|
#property indicator_type7 DRAW_ARROW
|
||||||
|
#property indicator_color7 clrLime
|
||||||
|
|
||||||
|
//
|
||||||
|
#define se2StartBufferIndex 7
|
||||||
|
double se2StartBuffer[];
|
||||||
|
|
||||||
|
//
|
||||||
|
#property indicator_label8 "X121 SE2 S"
|
||||||
|
#property indicator_type8 DRAW_ARROW
|
||||||
|
#property indicator_color8 clrRed
|
||||||
|
|
||||||
|
//
|
||||||
|
// Variables, Properties and etc ...
|
||||||
|
|
||||||
|
//
|
||||||
|
// this counts Available Bars ...
|
||||||
|
int limit;
|
||||||
|
|
||||||
|
//
|
||||||
|
int maxLength;
|
||||||
|
|
||||||
|
//
|
||||||
|
// Working Array ...
|
||||||
|
double work[][6];
|
||||||
|
#define hi1Idx 0
|
||||||
|
#define lo1Idx 1
|
||||||
|
#define hi2Idx 2
|
||||||
|
#define lo2Idx 3
|
||||||
|
#define trend1Idx 4
|
||||||
|
#define trend2Idx 5
|
||||||
|
|
||||||
|
//
|
||||||
|
// Event Handlers ...
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initialize Indicator ...
|
||||||
|
*
|
||||||
|
* @return ( int )
|
||||||
|
*/
|
||||||
|
int OnInit()
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Validate Inputs ...
|
||||||
|
if (!ValidateInputs())
|
||||||
|
{
|
||||||
|
return INIT_PARAMETERS_INCORRECT;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// 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 cheLength, here we get max Input cheLength
|
||||||
|
// 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[] //
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
if (Bars(_Symbol, _Period) < rates_total)
|
||||||
|
{
|
||||||
|
return (prev_calculated);
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
if (ArrayRange(work, 0) != rates_total)
|
||||||
|
{
|
||||||
|
ArrayResize(work, rates_total);
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// this counts Available Bars ...
|
||||||
|
|
||||||
|
//
|
||||||
|
limit = prev_calculated - 1;
|
||||||
|
if (limit < 0)
|
||||||
|
{
|
||||||
|
limit = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Main Loop ...
|
||||||
|
for (int i = limit; i < rates_total && !IsStopped(); i++)
|
||||||
|
{
|
||||||
|
CalculateBuffers(
|
||||||
|
i,
|
||||||
|
prev_calculated,
|
||||||
|
rates_total,
|
||||||
|
//
|
||||||
|
open,
|
||||||
|
high,
|
||||||
|
low,
|
||||||
|
close);
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
return rates_total;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Functions ...
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Validate Input Args for Initialization ...
|
||||||
|
*
|
||||||
|
* @return ( bool )
|
||||||
|
*/
|
||||||
|
bool ValidateInputs()
|
||||||
|
{
|
||||||
|
//
|
||||||
|
bool result = true;
|
||||||
|
|
||||||
|
//
|
||||||
|
result =
|
||||||
|
cheLength >= 9 &&
|
||||||
|
cheLoopback >= 0;
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Extract Max Length of Inputs ...
|
||||||
|
*
|
||||||
|
* @return ( int )
|
||||||
|
*/
|
||||||
|
int ExtractMaxLengthOfInputs()
|
||||||
|
{
|
||||||
|
//
|
||||||
|
int result = 0;
|
||||||
|
|
||||||
|
//
|
||||||
|
result = MathMax(cheLength, cheLoopback);
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Define Required Buffers ...
|
||||||
|
*/
|
||||||
|
void DefineBuffers()
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// LEVEL 1 ...
|
||||||
|
|
||||||
|
//
|
||||||
|
// Long Exit 1 ...
|
||||||
|
|
||||||
|
//
|
||||||
|
// Draw Type ...
|
||||||
|
ENUM_DRAW_TYPE le1DrawType = showLE1 ? DRAW_LINE : DRAW_NONE;
|
||||||
|
ENUM_DRAW_TYPE le1StartDrawType = showLE1 ? DRAW_ARROW : DRAW_NONE;
|
||||||
|
|
||||||
|
//
|
||||||
|
// Buffer ...
|
||||||
|
SetIndexBuffer(le1BufferIndex, le1Buffer, INDICATOR_DATA);
|
||||||
|
PlotIndexSetInteger(le1BufferIndex, PLOT_SHOW_DATA, showLE1);
|
||||||
|
PlotIndexSetInteger(le1BufferIndex, PLOT_DRAW_TYPE, le1DrawType);
|
||||||
|
|
||||||
|
//
|
||||||
|
// Start Buffer ...
|
||||||
|
SetIndexBuffer(le1StartBufferIndex, le1StartBuffer, INDICATOR_DATA);
|
||||||
|
PlotIndexSetInteger(le1StartBufferIndex, PLOT_SHOW_DATA, false);
|
||||||
|
PlotIndexSetInteger(le1StartBufferIndex, PLOT_DRAW_TYPE, le1StartDrawType);
|
||||||
|
PlotIndexSetInteger(le1StartBufferIndex, PLOT_ARROW, cheArrowCode);
|
||||||
|
|
||||||
|
//
|
||||||
|
// Short Exit 1 ...
|
||||||
|
|
||||||
|
//
|
||||||
|
// Draw Type ...
|
||||||
|
ENUM_DRAW_TYPE se1DrawType = showSE1 ? DRAW_LINE : DRAW_NONE;
|
||||||
|
ENUM_DRAW_TYPE se1StartDrawType = showSE1 ? DRAW_ARROW : DRAW_NONE;
|
||||||
|
|
||||||
|
//
|
||||||
|
// Buffer ...
|
||||||
|
SetIndexBuffer(se1BufferIndex, se1Buffer, INDICATOR_DATA);
|
||||||
|
PlotIndexSetInteger(se1BufferIndex, PLOT_SHOW_DATA, showSE1);
|
||||||
|
PlotIndexSetInteger(se1BufferIndex, PLOT_DRAW_TYPE, se1DrawType);
|
||||||
|
|
||||||
|
//
|
||||||
|
// Start Buffer ...
|
||||||
|
SetIndexBuffer(se1StartBufferIndex, se1StartBuffer, INDICATOR_DATA);
|
||||||
|
PlotIndexSetInteger(se1StartBufferIndex, PLOT_SHOW_DATA, false);
|
||||||
|
PlotIndexSetInteger(se1StartBufferIndex, PLOT_DRAW_TYPE, se1StartDrawType);
|
||||||
|
PlotIndexSetInteger(se1StartBufferIndex, PLOT_ARROW, cheArrowCode);
|
||||||
|
|
||||||
|
//
|
||||||
|
// LEVEL 2 ...
|
||||||
|
|
||||||
|
//
|
||||||
|
// Long Exit 2 ...
|
||||||
|
|
||||||
|
//
|
||||||
|
// Draw Type ...
|
||||||
|
ENUM_DRAW_TYPE le2DrawType = showLE2 ? DRAW_LINE : DRAW_NONE;
|
||||||
|
ENUM_DRAW_TYPE le2StartDrawType = showLE2 ? DRAW_ARROW : DRAW_NONE;
|
||||||
|
|
||||||
|
//
|
||||||
|
// Buffer ...
|
||||||
|
SetIndexBuffer(le2BufferIndex, le2Buffer, INDICATOR_DATA);
|
||||||
|
PlotIndexSetInteger(le2BufferIndex, PLOT_SHOW_DATA, showLE2);
|
||||||
|
PlotIndexSetInteger(le2BufferIndex, PLOT_DRAW_TYPE, le2DrawType);
|
||||||
|
|
||||||
|
//
|
||||||
|
// Start Buffer ...
|
||||||
|
SetIndexBuffer(le2StartBufferIndex, le2StartBuffer, INDICATOR_DATA);
|
||||||
|
PlotIndexSetInteger(le2StartBufferIndex, PLOT_SHOW_DATA, false);
|
||||||
|
PlotIndexSetInteger(le2StartBufferIndex, PLOT_DRAW_TYPE, le2StartDrawType);
|
||||||
|
PlotIndexSetInteger(le2StartBufferIndex, PLOT_ARROW, cheArrowCode);
|
||||||
|
|
||||||
|
//
|
||||||
|
// Short Exit 2 ...
|
||||||
|
|
||||||
|
//
|
||||||
|
// Draw Type ...
|
||||||
|
ENUM_DRAW_TYPE se2DrawType = showSE2 ? DRAW_LINE : DRAW_NONE;
|
||||||
|
ENUM_DRAW_TYPE se2StartDrawType = showSE2 ? DRAW_ARROW : DRAW_NONE;
|
||||||
|
|
||||||
|
//
|
||||||
|
// Buffer ...
|
||||||
|
SetIndexBuffer(se2BufferIndex, se2Buffer, INDICATOR_DATA);
|
||||||
|
PlotIndexSetInteger(se2BufferIndex, PLOT_SHOW_DATA, showSE2);
|
||||||
|
PlotIndexSetInteger(se2BufferIndex, PLOT_DRAW_TYPE, se2DrawType);
|
||||||
|
|
||||||
|
//
|
||||||
|
// Start Buffer ...
|
||||||
|
SetIndexBuffer(se2StartBufferIndex, se2StartBuffer, INDICATOR_DATA);
|
||||||
|
PlotIndexSetInteger(se2StartBufferIndex, PLOT_SHOW_DATA, false);
|
||||||
|
PlotIndexSetInteger(se2StartBufferIndex, PLOT_DRAW_TYPE, se2StartDrawType);
|
||||||
|
PlotIndexSetInteger(se2StartBufferIndex, PLOT_ARROW, cheArrowCode);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set Indicator Short Name and also we can define Buffers Labels ...
|
||||||
|
*/
|
||||||
|
void SetIndicatorName()
|
||||||
|
{
|
||||||
|
IndicatorSetString(INDICATOR_SHORTNAME, ShortName);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calculate Buffers ...
|
||||||
|
*/
|
||||||
|
void CalculateBuffers(
|
||||||
|
int bar_index, // Selected Bar Index
|
||||||
|
int prevCalculated, // Previous Calculated
|
||||||
|
int ratesTotal, // Total Rates
|
||||||
|
const double &open[], // Rates Open ...
|
||||||
|
const double &high[], // Rates High ...
|
||||||
|
const double &low[], // Rates Low ...
|
||||||
|
const double &close[] // Rates Close ...
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Cleanup Buffers ...
|
||||||
|
le1Buffer[bar_index] = le1StartBuffer[bar_index] = se1Buffer[bar_index] = se1StartBuffer[bar_index] = EMPTY_VALUE;
|
||||||
|
le2Buffer[bar_index] = le2StartBuffer[bar_index] = se2Buffer[bar_index] = se2StartBuffer[bar_index] = EMPTY_VALUE;
|
||||||
|
|
||||||
|
//
|
||||||
|
int start = MathMax(bar_index - cheLoopback, 0);
|
||||||
|
|
||||||
|
//
|
||||||
|
// Calculate ATR Value ...
|
||||||
|
double atrValue = 0;
|
||||||
|
for (int k = 1; k <= cheLength && (bar_index - k - 1) >= 0; k++)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
atrValue += MathMax(high[bar_index - k], close[MathMax(bar_index - k - 1, 0)]) -
|
||||||
|
MathMin(low[bar_index - k], close[MathMax(bar_index - k - 1, 0)]);
|
||||||
|
}
|
||||||
|
atrValue /= (double)cheLength;
|
||||||
|
|
||||||
|
//
|
||||||
|
// Retrieve Highest High and Lowest Lows ...
|
||||||
|
double cheLoopbackMax = high[ArrayMaximum(high, start, cheLoopback)];
|
||||||
|
double cheLoopbackMin = low[ArrayMinimum(low, start, cheLoopback)];
|
||||||
|
|
||||||
|
//
|
||||||
|
// Fill Multi Dimesional Working Array ...
|
||||||
|
work[bar_index][hi1Idx] = cheLoopbackMax - cheMultiplier1 * atrValue;
|
||||||
|
work[bar_index][lo1Idx] = cheLoopbackMin + cheMultiplier1 * atrValue;
|
||||||
|
work[bar_index][hi2Idx] = cheLoopbackMax - cheMultiplier2 * atrValue;
|
||||||
|
work[bar_index][lo2Idx] = cheLoopbackMin + cheMultiplier2 * atrValue;
|
||||||
|
work[bar_index][trend1Idx] = (bar_index > 0) ? work[bar_index - 1][trend1Idx] : 0;
|
||||||
|
work[bar_index][trend2Idx] = (bar_index > 0) ? work[bar_index - 1][trend2Idx] : 0;
|
||||||
|
|
||||||
|
//
|
||||||
|
if (bar_index > 0)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Calculate Trends ...
|
||||||
|
if (close[bar_index] > work[bar_index - 1][lo1Idx])
|
||||||
|
{
|
||||||
|
work[bar_index][trend1Idx] = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
if (close[bar_index] < work[bar_index - 1][hi1Idx])
|
||||||
|
{
|
||||||
|
work[bar_index][trend1Idx] = -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
if (close[bar_index] > work[bar_index - 1][lo2Idx])
|
||||||
|
{
|
||||||
|
work[bar_index][trend2Idx] = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
if (close[bar_index] < work[bar_index - 1][hi2Idx])
|
||||||
|
{
|
||||||
|
work[bar_index][trend2Idx] = -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Calculate Exit Values ...
|
||||||
|
|
||||||
|
//
|
||||||
|
// Exit 1 ...
|
||||||
|
|
||||||
|
//
|
||||||
|
// Long ...
|
||||||
|
if (cheMultiplier1 > 0 && work[bar_index][trend1Idx] == 1)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
if (work[bar_index][hi1Idx] < work[bar_index - 1][hi1Idx])
|
||||||
|
{
|
||||||
|
work[bar_index][hi1Idx] = work[bar_index - 1][hi1Idx];
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
le1Buffer[bar_index] = work[bar_index][hi1Idx];
|
||||||
|
|
||||||
|
//
|
||||||
|
if (le1Buffer[bar_index - 1] == EMPTY_VALUE)
|
||||||
|
{
|
||||||
|
le1StartBuffer[bar_index] = le1Buffer[bar_index];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Short ...
|
||||||
|
if (cheMultiplier1 > 0 && work[bar_index][trend1Idx] == -1)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
if (work[bar_index][lo1Idx] > work[bar_index - 1][lo1Idx])
|
||||||
|
{
|
||||||
|
work[bar_index][lo1Idx] = work[bar_index - 1][lo1Idx];
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
se1Buffer[bar_index] = work[bar_index][lo1Idx];
|
||||||
|
|
||||||
|
//
|
||||||
|
if (se1Buffer[bar_index - 1] == EMPTY_VALUE)
|
||||||
|
{
|
||||||
|
se1StartBuffer[bar_index] = se1Buffer[bar_index];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Exit 2 ...
|
||||||
|
|
||||||
|
//
|
||||||
|
// Long ...
|
||||||
|
if (cheMultiplier2 > 0 && work[bar_index][trend2Idx] == 1)
|
||||||
|
{
|
||||||
|
if (work[bar_index][hi2Idx] < work[bar_index - 1][hi2Idx])
|
||||||
|
{
|
||||||
|
work[bar_index][hi2Idx] = work[bar_index - 1][hi2Idx];
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
le2Buffer[bar_index] = work[bar_index][hi2Idx];
|
||||||
|
|
||||||
|
//
|
||||||
|
if (le2Buffer[bar_index - 1] == EMPTY_VALUE)
|
||||||
|
{
|
||||||
|
le2StartBuffer[bar_index] = le2Buffer[bar_index];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Short ...
|
||||||
|
if (cheMultiplier2 > 0 && work[bar_index][trend2Idx] == -1)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
if (work[bar_index][lo2Idx] > work[bar_index - 1][lo2Idx])
|
||||||
|
{
|
||||||
|
work[bar_index][lo2Idx] = work[bar_index - 1][lo2Idx];
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
se2Buffer[bar_index] = work[bar_index][lo2Idx];
|
||||||
|
|
||||||
|
//
|
||||||
|
if (se2Buffer[bar_index - 1] == EMPTY_VALUE)
|
||||||
|
{
|
||||||
|
se2StartBuffer[bar_index] = se2Buffer[bar_index];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
@@ -0,0 +1,262 @@
|
|||||||
|
///////////////////////////////////////////////////////
|
||||||
|
//
|
||||||
|
// SaherElm IT Center MQL5 Indicator
|
||||||
|
// ---------------------------------------------
|
||||||
|
// Name: X121 XCT
|
||||||
|
// 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 X121 XCT Indicator"
|
||||||
|
#property strict
|
||||||
|
|
||||||
|
//
|
||||||
|
// START Constants ...
|
||||||
|
//
|
||||||
|
|
||||||
|
//
|
||||||
|
#define ShortName "X121 XCT"
|
||||||
|
#include "../Libraries/x-saherelm.common.lib.mq5"
|
||||||
|
|
||||||
|
//
|
||||||
|
// END Constants ...
|
||||||
|
//
|
||||||
|
|
||||||
|
//
|
||||||
|
// START Inputs ...
|
||||||
|
//
|
||||||
|
|
||||||
|
//
|
||||||
|
input int fontSize = 15; // Font Size
|
||||||
|
input string font = "Arial"; // Font
|
||||||
|
input color clr = clrYellow; // Text Color
|
||||||
|
input ENUM_BASE_CORNER corner = CORNER_RIGHT_LOWER; // Text Position
|
||||||
|
|
||||||
|
//
|
||||||
|
input group "Presentation";
|
||||||
|
input bool showTimer = true; // Show Candle Time
|
||||||
|
|
||||||
|
//
|
||||||
|
// END Inputs ...
|
||||||
|
//
|
||||||
|
|
||||||
|
//
|
||||||
|
// START Buffers ...
|
||||||
|
//
|
||||||
|
|
||||||
|
//
|
||||||
|
#property indicator_chart_window
|
||||||
|
|
||||||
|
//
|
||||||
|
#property indicator_buffers 0
|
||||||
|
#property indicator_plots 0
|
||||||
|
|
||||||
|
//
|
||||||
|
// END Buffers ...
|
||||||
|
//
|
||||||
|
|
||||||
|
//
|
||||||
|
// START Global Definitions: Variables, Properties and etc ...
|
||||||
|
//
|
||||||
|
bool isCreatedObject;
|
||||||
|
string tag;
|
||||||
|
//
|
||||||
|
// END Global Definitions: Variables, Properties and etc ...
|
||||||
|
//
|
||||||
|
|
||||||
|
//
|
||||||
|
// START Event Handlers ...
|
||||||
|
//
|
||||||
|
|
||||||
|
//
|
||||||
|
// Initialization ...
|
||||||
|
int OnInit()
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Validate Inputs ...
|
||||||
|
|
||||||
|
//
|
||||||
|
tag = ShortName + _Symbol + ToString(_Period);
|
||||||
|
|
||||||
|
//
|
||||||
|
// 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
|
||||||
|
|
||||||
|
//
|
||||||
|
// Delete Object ...
|
||||||
|
ObjectDelete(0, tag);
|
||||||
|
|
||||||
|
//
|
||||||
|
Comment("");
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Calculating what we want ...
|
||||||
|
int OnCalculate(
|
||||||
|
const int rates_total, // Total Bars on Chart ...
|
||||||
|
const int prev_calculated, // Total Calculated Bars on Charts ...
|
||||||
|
const datetime &time[], // History of Open Time ...
|
||||||
|
const double &open[], // History of Open Price ...
|
||||||
|
const double &high[], // History of High Price ...
|
||||||
|
const double &low[], // History of Low Price ...
|
||||||
|
const double &close[], // History of Close Price ...
|
||||||
|
const long &tick_volume[], // History of Tick Volumes on Bar ...
|
||||||
|
const long &volume[], // History of Trade Volumes ...
|
||||||
|
const int &spread[] // History of Spread Price ...
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
if (!showTimer)
|
||||||
|
{
|
||||||
|
return rates_total;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
XBarRemainsTime barRemains;
|
||||||
|
barRemains.Init(
|
||||||
|
_Symbol,
|
||||||
|
_Period
|
||||||
|
//
|
||||||
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
string msg = barRemains
|
||||||
|
.ToString(
|
||||||
|
"",
|
||||||
|
"",
|
||||||
|
"",
|
||||||
|
"",
|
||||||
|
"",
|
||||||
|
":",
|
||||||
|
true
|
||||||
|
//
|
||||||
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
long mSpread = SymbolInfoInteger(Symbol(), SYMBOL_SPREAD);
|
||||||
|
|
||||||
|
//
|
||||||
|
string _sp = "";
|
||||||
|
if (mSpread < 10)
|
||||||
|
_sp = "..";
|
||||||
|
else if (mSpread < 100)
|
||||||
|
_sp = ".";
|
||||||
|
|
||||||
|
//
|
||||||
|
string objText = "Spread: " + IntegerToString(mSpread) + _sp + ", Next Bar in: " + msg;
|
||||||
|
|
||||||
|
//
|
||||||
|
if (IsValid(tag) && !isCreatedObject)
|
||||||
|
{
|
||||||
|
CreateIndicatorObject();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
if (isCreatedObject)
|
||||||
|
{
|
||||||
|
ObjectSetString(0, tag, OBJPROP_TEXT, objText);
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
return rates_total;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// END Event Handlers ...
|
||||||
|
//
|
||||||
|
|
||||||
|
//
|
||||||
|
// START Functions ...
|
||||||
|
//
|
||||||
|
|
||||||
|
//
|
||||||
|
// Set Indicator Short Name and also we can define Buffers Labels ...
|
||||||
|
void SetIndicatorName()
|
||||||
|
{
|
||||||
|
IndicatorSetString(INDICATOR_SHORTNAME, ShortName);
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
void CreateIndicatorObject()
|
||||||
|
{
|
||||||
|
//
|
||||||
|
if (!showTimer ||
|
||||||
|
!IsValid(tag))
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
isCreatedObject = ObjectCreate(0, tag, OBJ_LABEL, 0, 0, 0);
|
||||||
|
ObjectSetInteger(0, tag, OBJPROP_CORNER, corner);
|
||||||
|
ObjectSetInteger(0, tag, OBJPROP_XDISTANCE, 10);
|
||||||
|
ObjectSetInteger(0, tag, OBJPROP_YDISTANCE, 2);
|
||||||
|
|
||||||
|
//
|
||||||
|
ENUM_ANCHOR_POINT Anchor = ANCHOR_LEFT_UPPER;
|
||||||
|
switch (corner)
|
||||||
|
{
|
||||||
|
case CORNER_LEFT_UPPER:
|
||||||
|
Anchor = ANCHOR_LEFT_UPPER;
|
||||||
|
break;
|
||||||
|
case CORNER_RIGHT_UPPER:
|
||||||
|
Anchor = ANCHOR_RIGHT_UPPER;
|
||||||
|
break;
|
||||||
|
case CORNER_LEFT_LOWER:
|
||||||
|
Anchor = ANCHOR_LEFT_LOWER;
|
||||||
|
break;
|
||||||
|
case CORNER_RIGHT_LOWER:
|
||||||
|
Anchor = ANCHOR_RIGHT_LOWER;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
int mFontSize = fontSize > 0
|
||||||
|
? fontSize
|
||||||
|
: 10;
|
||||||
|
|
||||||
|
//
|
||||||
|
string mFont = IsValid(font)
|
||||||
|
? font
|
||||||
|
: "Arial";
|
||||||
|
|
||||||
|
//
|
||||||
|
ObjectSetString(0, tag, OBJPROP_FONT, mFont);
|
||||||
|
ObjectSetInteger(0, tag, OBJPROP_COLOR, clr);
|
||||||
|
ObjectSetInteger(0, tag, OBJPROP_ANCHOR, Anchor);
|
||||||
|
ObjectSetInteger(0, tag, OBJPROP_FONTSIZE, mFontSize);
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// END Functions ...
|
||||||
|
//
|
||||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,550 @@
|
|||||||
|
///////////////////////////////////////////////////////
|
||||||
|
//
|
||||||
|
// SaherElm IT Center MQL5 Ocillator
|
||||||
|
// -------------------------------------------------
|
||||||
|
// Name: X121 XDelta
|
||||||
|
// Description: Detect Comulative Volumes Delta ...
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 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 XDelta Ocillator"
|
||||||
|
#property strict
|
||||||
|
|
||||||
|
//
|
||||||
|
// Definitions ...
|
||||||
|
|
||||||
|
//
|
||||||
|
#define ShortName "X121_XDELTA"
|
||||||
|
|
||||||
|
//
|
||||||
|
// 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 showDeltaLine = true; // Show Delta Line
|
||||||
|
input bool showDeltaHistogram = true; // Show Delta Histogram
|
||||||
|
input bool showDeltaMa = true; // Show Delta Moving Average
|
||||||
|
|
||||||
|
//
|
||||||
|
// Buffers Props ...
|
||||||
|
|
||||||
|
//
|
||||||
|
#property indicator_separate_window
|
||||||
|
|
||||||
|
//
|
||||||
|
#property indicator_buffers 6
|
||||||
|
#property indicator_plots 3
|
||||||
|
|
||||||
|
//
|
||||||
|
#define hideColorIDX 0
|
||||||
|
#define bullishColorIDX 1
|
||||||
|
#define bearishColorIDX 2
|
||||||
|
|
||||||
|
//
|
||||||
|
// Buffers ...
|
||||||
|
|
||||||
|
//
|
||||||
|
#define deltaLineBufferIndex 0
|
||||||
|
double deltaLineBuffer[];
|
||||||
|
|
||||||
|
#property indicator_label1 "Delta Line"
|
||||||
|
#property indicator_type1 DRAW_LINE
|
||||||
|
#property indicator_color1 clrYellow
|
||||||
|
#property indicator_style1 STYLE_SOLID
|
||||||
|
#property indicator_width1 1
|
||||||
|
|
||||||
|
//
|
||||||
|
#define deltaHistogramBufferIndex 1
|
||||||
|
double deltaHistogramBuffer[];
|
||||||
|
|
||||||
|
//
|
||||||
|
#define deltaHistogramColorBufferIndex 2
|
||||||
|
double deltaHistogramColorBuffer[];
|
||||||
|
|
||||||
|
#property indicator_label2 "Delta Histogram"
|
||||||
|
#property indicator_type2 DRAW_COLOR_HISTOGRAM
|
||||||
|
#property indicator_color2 CLR_NONE, clrGreen, clrDarkRed
|
||||||
|
#property indicator_style2 STYLE_DOT
|
||||||
|
#property indicator_width2 1
|
||||||
|
|
||||||
|
//
|
||||||
|
#define deltaMaBufferIndex 3
|
||||||
|
double deltaMaBuffer[];
|
||||||
|
|
||||||
|
#property indicator_label3 "Delta MA"
|
||||||
|
#property indicator_type3 DRAW_LINE
|
||||||
|
#property indicator_color3 clrOrchid
|
||||||
|
#property indicator_style3 STYLE_SOLID
|
||||||
|
#property indicator_width3 1
|
||||||
|
|
||||||
|
//
|
||||||
|
// Data Buffers ...
|
||||||
|
|
||||||
|
#define mLastBufferIndex 3
|
||||||
|
|
||||||
|
//
|
||||||
|
#define rawDeltaBufferIndex mLastBufferIndex + 1
|
||||||
|
double rawDeltaBuffer[];
|
||||||
|
|
||||||
|
//
|
||||||
|
#define deltaStateBufferIndex mLastBufferIndex + 2
|
||||||
|
double deltaStateBuffer[];
|
||||||
|
|
||||||
|
//
|
||||||
|
// 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 ...
|
||||||
|
|
||||||
|
//
|
||||||
|
// Delta Line ...
|
||||||
|
ENUM_DRAW_TYPE deltaLineDrawType = showDeltaLine ? DRAW_LINE : DRAW_NONE;
|
||||||
|
|
||||||
|
//
|
||||||
|
ArraySetAsSeries(deltaLineBuffer, true);
|
||||||
|
SetIndexBuffer(deltaLineBufferIndex, deltaLineBuffer, INDICATOR_DATA);
|
||||||
|
PlotIndexSetDouble(deltaLineBufferIndex, PLOT_EMPTY_VALUE, 0.0);
|
||||||
|
PlotIndexSetInteger(deltaLineBufferIndex, PLOT_SHOW_DATA, showDeltaLine);
|
||||||
|
PlotIndexSetInteger(deltaLineBufferIndex, PLOT_DRAW_TYPE, deltaLineDrawType);
|
||||||
|
|
||||||
|
//
|
||||||
|
// Delta Histogram ...
|
||||||
|
ENUM_DRAW_TYPE deltaHistogramDrawType = showDeltaHistogram ? DRAW_COLOR_HISTOGRAM : DRAW_NONE;
|
||||||
|
|
||||||
|
//
|
||||||
|
ArraySetAsSeries(deltaHistogramBuffer, true);
|
||||||
|
SetIndexBuffer(deltaHistogramBufferIndex, deltaHistogramBuffer, INDICATOR_DATA);
|
||||||
|
PlotIndexSetDouble(deltaHistogramBufferIndex, PLOT_EMPTY_VALUE, 0.0);
|
||||||
|
PlotIndexSetInteger(deltaHistogramBufferIndex, PLOT_SHOW_DATA, showDeltaHistogram);
|
||||||
|
PlotIndexSetInteger(deltaHistogramBufferIndex, PLOT_DRAW_TYPE, deltaHistogramDrawType);
|
||||||
|
|
||||||
|
//
|
||||||
|
ArraySetAsSeries(deltaHistogramColorBuffer, true);
|
||||||
|
SetIndexBuffer(deltaHistogramColorBufferIndex, deltaHistogramColorBuffer, INDICATOR_COLOR_INDEX);
|
||||||
|
|
||||||
|
//
|
||||||
|
// Delta Ma ...
|
||||||
|
ENUM_DRAW_TYPE deltaMaDrawType = showDeltaMa ? DRAW_LINE : DRAW_NONE;
|
||||||
|
|
||||||
|
//
|
||||||
|
ArraySetAsSeries(deltaMaBuffer, true);
|
||||||
|
SetIndexBuffer(deltaMaBufferIndex, deltaMaBuffer, INDICATOR_DATA);
|
||||||
|
PlotIndexSetDouble(deltaMaBufferIndex, PLOT_EMPTY_VALUE, EMPTY_VALUE);
|
||||||
|
PlotIndexSetInteger(deltaMaBufferIndex, PLOT_SHOW_DATA, showDeltaMa);
|
||||||
|
PlotIndexSetInteger(deltaMaBufferIndex, PLOT_DRAW_TYPE, deltaMaDrawType);
|
||||||
|
|
||||||
|
//
|
||||||
|
// Data Buffers ...
|
||||||
|
|
||||||
|
//
|
||||||
|
ArraySetAsSeries(rawDeltaBuffer, true);
|
||||||
|
SetIndexBuffer(rawDeltaBufferIndex, rawDeltaBuffer, INDICATOR_CALCULATIONS);
|
||||||
|
|
||||||
|
//
|
||||||
|
ArraySetAsSeries(deltaStateBuffer, true);
|
||||||
|
SetIndexBuffer(deltaStateBufferIndex, deltaStateBuffer, 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)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
rawDeltaBuffer[barIndex] = 0.0;
|
||||||
|
deltaLineBuffer[barIndex] = 0.0;
|
||||||
|
deltaStateBuffer[barIndex] = 0.0;
|
||||||
|
deltaHistogramBuffer[barIndex] = 0.0;
|
||||||
|
deltaMaBuffer[barIndex] = 0.0;
|
||||||
|
deltaHistogramColorBuffer[barIndex] = hideColorIDX;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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 iDelta = 0.0;
|
||||||
|
|
||||||
|
//
|
||||||
|
// Detecting Delta Volume ...
|
||||||
|
if (bar.IsBullish())
|
||||||
|
{
|
||||||
|
iDelta = (double)bar.volume;
|
||||||
|
}
|
||||||
|
else if (bar.IsBearish())
|
||||||
|
{
|
||||||
|
iDelta = -(double)bar.volume;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Setting iDelta Value ...
|
||||||
|
if (isFirstBar)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
rawDeltaBuffer[bar_index] = iDelta;
|
||||||
|
deltaLineBuffer[bar_index] = iDelta;
|
||||||
|
deltaHistogramBuffer[bar_index] = iDelta;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
//
|
||||||
|
iDelta += deltaLineBuffer[bar_index + 1];
|
||||||
|
|
||||||
|
//
|
||||||
|
rawDeltaBuffer[bar_index] = iDelta;
|
||||||
|
deltaLineBuffer[bar_index] = iDelta;
|
||||||
|
deltaHistogramBuffer[bar_index] = iDelta;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Setting iDelta State ...
|
||||||
|
double iState = iDelta > 0
|
||||||
|
? bullishColorIDX
|
||||||
|
: iDelta < 0
|
||||||
|
? bearishColorIDX
|
||||||
|
: hideColorIDX;
|
||||||
|
//
|
||||||
|
// Setting iDelta Color ...
|
||||||
|
double iDeltaColor = iDelta > 0
|
||||||
|
? bullishColorIDX
|
||||||
|
: iDelta < 0
|
||||||
|
? bearishColorIDX
|
||||||
|
: hideColorIDX;
|
||||||
|
|
||||||
|
//
|
||||||
|
deltaStateBuffer[bar_index] = iState;
|
||||||
|
deltaHistogramColorBuffer[bar_index] = iDeltaColor;
|
||||||
|
|
||||||
|
//
|
||||||
|
// Calculate Moving Averages ...
|
||||||
|
int deltaMAsCount = iMAOnBuffer(
|
||||||
|
ratesTotal,
|
||||||
|
prevCalculated,
|
||||||
|
bar_index,
|
||||||
|
maLength,
|
||||||
|
rawDeltaBuffer,
|
||||||
|
deltaMaBuffer,
|
||||||
|
maMethod //
|
||||||
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
bar.Clean();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
@@ -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 //
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
@@ -0,0 +1,616 @@
|
|||||||
|
///////////////////////////////////////////////////////
|
||||||
|
//
|
||||||
|
// SaherElm IT Center MQL5 Indicator
|
||||||
|
// -------------------------------------------------
|
||||||
|
// Name: X121 XHK
|
||||||
|
// Description: XHK ...
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 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 XHK Indicator"
|
||||||
|
#property strict
|
||||||
|
|
||||||
|
//
|
||||||
|
// Definitions ...
|
||||||
|
|
||||||
|
//
|
||||||
|
#define ShortName "X121 XHK"
|
||||||
|
|
||||||
|
//
|
||||||
|
// Includes Common Library ...
|
||||||
|
#include "../Libraries/x-saherelm.common.lib.mq5"
|
||||||
|
|
||||||
|
//
|
||||||
|
// Inputs ...
|
||||||
|
|
||||||
|
//
|
||||||
|
// Market ...
|
||||||
|
input group "Market";
|
||||||
|
input int xhkSmoothingLength = 14; // Length
|
||||||
|
input ENUM_X_MA_METHOD xhkSmoothingMode = X_MA_MODE_EMA; // Smoothing Method
|
||||||
|
input bool xhkIgnoreSmoothingShadows = false; // Ignore Smoothed Bar Shadows
|
||||||
|
|
||||||
|
//
|
||||||
|
input group "Presentation";
|
||||||
|
|
||||||
|
//
|
||||||
|
input int startCalculationForLastBars = 1000; // Calculate Last n Bars
|
||||||
|
|
||||||
|
//
|
||||||
|
input bool drawRawXHKCandles = true; // Draw Raw Candle
|
||||||
|
input bool drawSmoothedXHKCandles = true; // Draw Smoothed Candle
|
||||||
|
|
||||||
|
//
|
||||||
|
// Buffers ...
|
||||||
|
|
||||||
|
//
|
||||||
|
#property indicator_chart_window
|
||||||
|
|
||||||
|
//
|
||||||
|
#property indicator_buffers 10
|
||||||
|
#property indicator_plots 2
|
||||||
|
|
||||||
|
//
|
||||||
|
#define hideColorIDX 0
|
||||||
|
#define bullishColorIDX 1
|
||||||
|
#define bearishColorIDX 2
|
||||||
|
#define neuturalColorIDX 3
|
||||||
|
|
||||||
|
//
|
||||||
|
// PLOTTED Buffers ...
|
||||||
|
|
||||||
|
//
|
||||||
|
#define openHKBufferIndex 0
|
||||||
|
double openHKBuffer[];
|
||||||
|
|
||||||
|
//
|
||||||
|
#define highHKBufferIndex 1
|
||||||
|
double highHKBuffer[];
|
||||||
|
|
||||||
|
//
|
||||||
|
#define lowHKBufferIndex 2
|
||||||
|
double lowHKBuffer[];
|
||||||
|
|
||||||
|
//
|
||||||
|
#define closeHKBufferIndex 3
|
||||||
|
double closeHKBuffer[];
|
||||||
|
|
||||||
|
//
|
||||||
|
#define rawHKColorBufferIndex 4
|
||||||
|
double rawHKColorBuffer[];
|
||||||
|
|
||||||
|
//
|
||||||
|
#define rawHKBufferIndex 0
|
||||||
|
#property indicator_label1 "X121 XHK Open;X121 XHK High;X121 XHK Low;X121 XHK Close"
|
||||||
|
#property indicator_type1 DRAW_COLOR_CANDLES
|
||||||
|
#property indicator_color1 CLR_NONE, clrDodgerBlue, clrDarkGoldenrod
|
||||||
|
|
||||||
|
//
|
||||||
|
// Open ...
|
||||||
|
#define openBufferIndex 5
|
||||||
|
double openBuffer[];
|
||||||
|
|
||||||
|
//
|
||||||
|
// High ...
|
||||||
|
#define highBufferIndex 6
|
||||||
|
double highBuffer[];
|
||||||
|
|
||||||
|
//
|
||||||
|
// Low ...
|
||||||
|
#define lowBufferIndex 7
|
||||||
|
double lowBuffer[];
|
||||||
|
|
||||||
|
//
|
||||||
|
// Close ...
|
||||||
|
#define closeBufferIndex 8
|
||||||
|
double closeBuffer[];
|
||||||
|
|
||||||
|
//
|
||||||
|
// Candle Color ...
|
||||||
|
#define candleColorBufferIndex 9
|
||||||
|
double candleColorBuffer[];
|
||||||
|
|
||||||
|
//
|
||||||
|
#define candlesBufferIndex 1
|
||||||
|
#property indicator_label2 "X121 XHKSM Open;X121 XHKSM High;X121 XHKSM Low;X121 XHKSM Close"
|
||||||
|
#property indicator_type2 DRAW_COLOR_CANDLES
|
||||||
|
#property indicator_color2 CLR_NONE, clrAqua, clrMagenta
|
||||||
|
|
||||||
|
//
|
||||||
|
// 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);
|
||||||
|
|
||||||
|
//
|
||||||
|
// Fill All Buffers by Zero ...
|
||||||
|
if (prev_calculated == 0)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
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 =
|
||||||
|
//
|
||||||
|
true
|
||||||
|
//
|
||||||
|
;
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Extract Max Length of Inputs ...
|
||||||
|
*
|
||||||
|
* @return ( int )
|
||||||
|
*/
|
||||||
|
int ExtractMaxLengthOfInputs()
|
||||||
|
{
|
||||||
|
//
|
||||||
|
int result = 0;
|
||||||
|
|
||||||
|
//
|
||||||
|
result = MathMax(0, xhkSmoothingLength);
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Define Required Buffers ...
|
||||||
|
*/
|
||||||
|
void DefineBuffers()
|
||||||
|
{
|
||||||
|
|
||||||
|
//
|
||||||
|
// RAW ...
|
||||||
|
|
||||||
|
//
|
||||||
|
// Raw Candles Color ...
|
||||||
|
ArraySetAsSeries(rawHKColorBuffer, true);
|
||||||
|
SetIndexBuffer(rawHKColorBufferIndex, rawHKColorBuffer, INDICATOR_COLOR_INDEX);
|
||||||
|
|
||||||
|
//
|
||||||
|
// Set EMPTY_VALUE on Candles Buffer ...
|
||||||
|
PlotIndexSetDouble(rawHKBufferIndex, PLOT_EMPTY_VALUE, 0.0);
|
||||||
|
PlotIndexSetInteger(rawHKBufferIndex, PLOT_SHOW_DATA, false);
|
||||||
|
|
||||||
|
//
|
||||||
|
// Open ...
|
||||||
|
ArraySetAsSeries(openHKBuffer, true);
|
||||||
|
SetIndexBuffer(openHKBufferIndex, openHKBuffer, INDICATOR_DATA);
|
||||||
|
|
||||||
|
//
|
||||||
|
// High ...
|
||||||
|
ArraySetAsSeries(highHKBuffer, true);
|
||||||
|
SetIndexBuffer(highHKBufferIndex, highHKBuffer, INDICATOR_DATA);
|
||||||
|
|
||||||
|
//
|
||||||
|
// Close ...
|
||||||
|
ArraySetAsSeries(closeHKBuffer, true);
|
||||||
|
SetIndexBuffer(closeHKBufferIndex, closeHKBuffer, INDICATOR_DATA);
|
||||||
|
|
||||||
|
//
|
||||||
|
// Low ...
|
||||||
|
ArraySetAsSeries(lowHKBuffer, true);
|
||||||
|
SetIndexBuffer(lowHKBufferIndex, lowHKBuffer, INDICATOR_DATA);
|
||||||
|
|
||||||
|
//
|
||||||
|
// SMOOTHED ...
|
||||||
|
|
||||||
|
//
|
||||||
|
// Candles Color ...
|
||||||
|
ArraySetAsSeries(candleColorBuffer, true);
|
||||||
|
SetIndexBuffer(candleColorBufferIndex, candleColorBuffer, INDICATOR_COLOR_INDEX);
|
||||||
|
|
||||||
|
//
|
||||||
|
// Set EMPTY_VALUE on Candles Buffer ...
|
||||||
|
PlotIndexSetDouble(candlesBufferIndex, PLOT_EMPTY_VALUE, 0.0);
|
||||||
|
PlotIndexSetInteger(candlesBufferIndex, PLOT_SHOW_DATA, false);
|
||||||
|
|
||||||
|
//
|
||||||
|
// Open ...
|
||||||
|
ArraySetAsSeries(openBuffer, true);
|
||||||
|
SetIndexBuffer(openBufferIndex, openBuffer, INDICATOR_DATA);
|
||||||
|
|
||||||
|
//
|
||||||
|
// High ...
|
||||||
|
ArraySetAsSeries(highBuffer, true);
|
||||||
|
SetIndexBuffer(highBufferIndex, highBuffer, INDICATOR_DATA);
|
||||||
|
|
||||||
|
//
|
||||||
|
// Close ...
|
||||||
|
ArraySetAsSeries(closeBuffer, true);
|
||||||
|
SetIndexBuffer(closeBufferIndex, closeBuffer, INDICATOR_DATA);
|
||||||
|
|
||||||
|
//
|
||||||
|
// Low ...
|
||||||
|
ArraySetAsSeries(lowBuffer, true);
|
||||||
|
SetIndexBuffer(lowBufferIndex, lowBuffer, INDICATOR_DATA);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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 =
|
||||||
|
barsLimit == 0 ||
|
||||||
|
bar_index <= barsLimit;
|
||||||
|
if (canCalculate)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
CalculateXHK(
|
||||||
|
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)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
lowHKBuffer[barIndex] = 0.0;
|
||||||
|
openHKBuffer[barIndex] = 0.0;
|
||||||
|
highHKBuffer[barIndex] = 0.0;
|
||||||
|
closeHKBuffer[barIndex] = 0.0;
|
||||||
|
rawHKColorBuffer[barIndex] = hideColorIDX;
|
||||||
|
|
||||||
|
//
|
||||||
|
lowBuffer[barIndex] = 0.0;
|
||||||
|
openBuffer[barIndex] = 0.0;
|
||||||
|
highBuffer[barIndex] = 0.0;
|
||||||
|
closeBuffer[barIndex] = 0.0;
|
||||||
|
candleColorBuffer[barIndex] = hideColorIDX;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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 CalculateXHK(
|
||||||
|
int bar_index, // Selected Bar Index
|
||||||
|
const int prevCalculated,
|
||||||
|
const int ratesTotal,
|
||||||
|
const double &open[],
|
||||||
|
const double &high[],
|
||||||
|
const double &close[],
|
||||||
|
const double &low[] //
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
double mPrevHKOpenValue;
|
||||||
|
double mPrevHKCloseValue;
|
||||||
|
|
||||||
|
//
|
||||||
|
if (ArraySize(open) <= bar_index + 1)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
mPrevHKOpenValue = 0;
|
||||||
|
mPrevHKCloseValue = 0;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
//
|
||||||
|
mPrevHKOpenValue = openHKBuffer[bar_index + 1];
|
||||||
|
mPrevHKCloseValue = closeHKBuffer[bar_index + 1];
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
double mHKOpenValue = (mPrevHKOpenValue + mPrevHKCloseValue) / 2;
|
||||||
|
double mHKCloseValue = (open[bar_index] + high[bar_index] + close[bar_index] + low[bar_index]) / 4;
|
||||||
|
double mHKHighValue = MathMax(high[bar_index], MathMax(mHKOpenValue, mHKCloseValue));
|
||||||
|
double mHKLowValue = MathMin(low[bar_index], MathMin(mHKOpenValue, mHKCloseValue));
|
||||||
|
|
||||||
|
//
|
||||||
|
double candleColorValue =
|
||||||
|
mHKOpenValue < mHKCloseValue ? bullishColorIDX : bearishColorIDX;
|
||||||
|
|
||||||
|
//
|
||||||
|
// Raw HK Buffers ...
|
||||||
|
openHKBuffer[bar_index] = mHKOpenValue;
|
||||||
|
highHKBuffer[bar_index] = mHKHighValue;
|
||||||
|
lowHKBuffer[bar_index] = mHKLowValue;
|
||||||
|
closeHKBuffer[bar_index] = mHKCloseValue;
|
||||||
|
rawHKColorBuffer[bar_index] = drawRawXHKCandles ? candleColorValue : hideColorIDX;
|
||||||
|
|
||||||
|
//
|
||||||
|
// Apply Smoothing ...
|
||||||
|
bool canSmooth = xhkSmoothingLength > 0 &&
|
||||||
|
xhkSmoothingMode != X_MA_MODE_NONE;
|
||||||
|
if (!canSmooth)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
double min = MathMin(mHKOpenValue, mHKCloseValue);
|
||||||
|
double max = MathMax(mHKOpenValue, mHKCloseValue);
|
||||||
|
|
||||||
|
//
|
||||||
|
openBuffer[bar_index] = mHKOpenValue;
|
||||||
|
highBuffer[bar_index] = xhkIgnoreSmoothingShadows ? max : mHKHighValue;
|
||||||
|
lowBuffer[bar_index] = xhkIgnoreSmoothingShadows ? min : mHKLowValue;
|
||||||
|
closeBuffer[bar_index] = mHKCloseValue;
|
||||||
|
candleColorBuffer[bar_index] = drawSmoothedXHKCandles ? candleColorValue : hideColorIDX;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Open ...
|
||||||
|
iMAOnBuffer(
|
||||||
|
ratesTotal,
|
||||||
|
prevCalculated,
|
||||||
|
bar_index,
|
||||||
|
xhkSmoothingLength,
|
||||||
|
openHKBuffer,
|
||||||
|
openBuffer,
|
||||||
|
xhkSmoothingMode //
|
||||||
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
// Close ...
|
||||||
|
iMAOnBuffer(
|
||||||
|
ratesTotal,
|
||||||
|
prevCalculated,
|
||||||
|
bar_index,
|
||||||
|
xhkSmoothingLength,
|
||||||
|
closeHKBuffer,
|
||||||
|
closeBuffer,
|
||||||
|
xhkSmoothingMode //
|
||||||
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
// Ignoring Shadows ...
|
||||||
|
if (!xhkIgnoreSmoothingShadows)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// High ...
|
||||||
|
iMAOnBuffer(
|
||||||
|
ratesTotal,
|
||||||
|
prevCalculated,
|
||||||
|
bar_index,
|
||||||
|
xhkSmoothingLength,
|
||||||
|
highHKBuffer,
|
||||||
|
highBuffer,
|
||||||
|
xhkSmoothingMode //
|
||||||
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
// Low ...
|
||||||
|
iMAOnBuffer(
|
||||||
|
ratesTotal,
|
||||||
|
prevCalculated,
|
||||||
|
bar_index,
|
||||||
|
xhkSmoothingLength,
|
||||||
|
lowHKBuffer,
|
||||||
|
lowBuffer,
|
||||||
|
xhkSmoothingMode //
|
||||||
|
);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
//
|
||||||
|
double min = MathMin(closeBuffer[bar_index], openBuffer[bar_index]);
|
||||||
|
double max = MathMin(closeBuffer[bar_index], openBuffer[bar_index]);
|
||||||
|
|
||||||
|
//
|
||||||
|
lowBuffer[bar_index] = min;
|
||||||
|
highBuffer[bar_index] = max;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Calculate Smoothed Color ...
|
||||||
|
candleColorValue =
|
||||||
|
openBuffer[bar_index] < closeBuffer[bar_index] ? bullishColorIDX : bearishColorIDX;
|
||||||
|
candleColorBuffer[bar_index] = drawSmoothedXHKCandles ? candleColorValue : hideColorIDX;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
@@ -0,0 +1,625 @@
|
|||||||
|
///////////////////////////////////////////////////////
|
||||||
|
//
|
||||||
|
// SaherElm IT Center MQL5 Indicator
|
||||||
|
// -------------------------------------------------
|
||||||
|
// Name: X121 XWZ
|
||||||
|
// Description: XWZ ...
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 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 XWZ Indicator"
|
||||||
|
#property strict
|
||||||
|
|
||||||
|
//
|
||||||
|
// Definitions ...
|
||||||
|
|
||||||
|
//
|
||||||
|
#define ShortName "X121_XICH"
|
||||||
|
|
||||||
|
//
|
||||||
|
// Imports ...
|
||||||
|
#include "../Libraries/x-saherelm.common.lib.mq5"
|
||||||
|
|
||||||
|
//
|
||||||
|
// Inputs ...
|
||||||
|
|
||||||
|
//
|
||||||
|
// Market ...
|
||||||
|
input group "Market";
|
||||||
|
input ENUM_X_PRICE chikouSpanMode = X_PRICE_CLOSE; // ChikouSpan Mode
|
||||||
|
input int tenkanSenLength = 9; // TenkanSen Length
|
||||||
|
input ENUM_X_BOUNDARY_PRICE tenkanSenMode = X_BOUNDARY_PRICE_HIGH_LOW; // TencanSen Mode
|
||||||
|
input int kijunSenLength = 26; // KijunSen Length
|
||||||
|
input ENUM_X_BOUNDARY_PRICE kijunSenMode = X_BOUNDARY_PRICE_HIGH_LOW; // KijunSen Mode
|
||||||
|
input int senkouSpanBLength = 52; // SenkouSpan B Length
|
||||||
|
input ENUM_X_BOUNDARY_PRICE senkouSpanBMode = X_BOUNDARY_PRICE_HIGH_LOW; // SenkouSpan B Mode
|
||||||
|
|
||||||
|
//
|
||||||
|
// Presentation ...
|
||||||
|
input group "Presentation";
|
||||||
|
|
||||||
|
//
|
||||||
|
input int startCalculationForLastBars = 1000; // Calculate Last n Bars
|
||||||
|
input bool shiftKumo = true; // Shift Kumo
|
||||||
|
input bool showTenkanSen = true; // Show TenkanSen
|
||||||
|
input bool showKijunSen = true; // Show KijunSen
|
||||||
|
input bool showChikouSpan = true; // Show ChikouSpan
|
||||||
|
input bool showSenkouSpanA = true; // Show Senkou Span A
|
||||||
|
input bool showSenkouSpanB = true; // Show Senkou Span B
|
||||||
|
input bool showKumo = true; // Show Kumo
|
||||||
|
|
||||||
|
//
|
||||||
|
// Buffers ...
|
||||||
|
|
||||||
|
//
|
||||||
|
#define hideColorIDX 0
|
||||||
|
#define bullishColorIDX 1
|
||||||
|
#define bearishColorIDX 2
|
||||||
|
#define neuturalColorIDX 3
|
||||||
|
|
||||||
|
//
|
||||||
|
#property indicator_chart_window
|
||||||
|
|
||||||
|
//
|
||||||
|
#property indicator_buffers 8
|
||||||
|
#property indicator_plots 6
|
||||||
|
|
||||||
|
//
|
||||||
|
// Plot Buffers ...
|
||||||
|
|
||||||
|
//
|
||||||
|
#define tenkanSenBufferIndex 0
|
||||||
|
double tenkanSenBuffer[];
|
||||||
|
|
||||||
|
//
|
||||||
|
#property indicator_label1 "XICH TK"
|
||||||
|
#property indicator_type1 DRAW_LINE
|
||||||
|
#property indicator_color1 clrBrown
|
||||||
|
#property indicator_style1 STYLE_SOLID
|
||||||
|
#property indicator_width1 2
|
||||||
|
|
||||||
|
//
|
||||||
|
#define kijunSenBufferIndex 1
|
||||||
|
double kijunSenBuffer[];
|
||||||
|
|
||||||
|
//
|
||||||
|
#property indicator_label2 "XICH KJ"
|
||||||
|
#property indicator_type2 DRAW_LINE
|
||||||
|
#property indicator_color2 clrDodgerBlue
|
||||||
|
#property indicator_style2 STYLE_SOLID
|
||||||
|
#property indicator_width2 2
|
||||||
|
|
||||||
|
//
|
||||||
|
#define chikouSpanBufferIndex 2
|
||||||
|
double chikouSpanBuffer[];
|
||||||
|
|
||||||
|
//
|
||||||
|
#property indicator_label3 "XICH CS"
|
||||||
|
#property indicator_type3 DRAW_LINE
|
||||||
|
#property indicator_color3 clrDarkGreen
|
||||||
|
#property indicator_style3 STYLE_SOLID
|
||||||
|
#property indicator_width3 2
|
||||||
|
|
||||||
|
//
|
||||||
|
#define senkouSpanABufferIndex 3
|
||||||
|
double senkouSpanABuffer[];
|
||||||
|
|
||||||
|
//
|
||||||
|
#property indicator_label4 "XICH SSA"
|
||||||
|
#property indicator_type4 DRAW_LINE
|
||||||
|
#property indicator_color4 clrLightGray
|
||||||
|
#property indicator_style4 STYLE_DASHDOTDOT
|
||||||
|
#property indicator_width4 2
|
||||||
|
|
||||||
|
//
|
||||||
|
#define senkouSpanBBufferIndex 4
|
||||||
|
double senkouSpanBBuffer[];
|
||||||
|
|
||||||
|
//
|
||||||
|
#property indicator_label5 "XICH SSB"
|
||||||
|
#property indicator_type5 DRAW_LINE
|
||||||
|
#property indicator_color5 clrLightGray
|
||||||
|
#property indicator_style5 STYLE_DASHDOTDOT
|
||||||
|
#property indicator_width5 2
|
||||||
|
|
||||||
|
//
|
||||||
|
#define senkouABufferIndex 5
|
||||||
|
double senkouABuffer[];
|
||||||
|
|
||||||
|
//
|
||||||
|
#define senkouBBufferIndex 6
|
||||||
|
double senkouBBuffer[];
|
||||||
|
|
||||||
|
//
|
||||||
|
#define kumoBufferIndex 5
|
||||||
|
|
||||||
|
//
|
||||||
|
#property indicator_label6 "XICH Kumo"
|
||||||
|
#property indicator_type6 DRAW_FILLING
|
||||||
|
#property indicator_color6 clrAqua, clrMagenta
|
||||||
|
#property indicator_style6 STYLE_SOLID
|
||||||
|
#property indicator_width6 2
|
||||||
|
|
||||||
|
//
|
||||||
|
// Data Buffers ...
|
||||||
|
|
||||||
|
//
|
||||||
|
int mLastBufferIndex = 6;
|
||||||
|
|
||||||
|
//
|
||||||
|
// 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// 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 =
|
||||||
|
//
|
||||||
|
kijunSenLength > 0 &&
|
||||||
|
tenkanSenLength > 0 &&
|
||||||
|
senkouSpanBLength > 0 &&
|
||||||
|
IsValid(kijunSenMode) &&
|
||||||
|
IsValid(tenkanSenMode) &&
|
||||||
|
IsValid(chikouSpanMode) &&
|
||||||
|
IsValid(senkouSpanBMode)
|
||||||
|
//
|
||||||
|
;
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Extract Max Length of Inputs ...
|
||||||
|
*
|
||||||
|
* @return ( int )
|
||||||
|
*/
|
||||||
|
int ExtractMaxLengthOfInputs()
|
||||||
|
{
|
||||||
|
//
|
||||||
|
int result = 0;
|
||||||
|
|
||||||
|
//
|
||||||
|
result = MathMax(kijunSenLength, tenkanSenLength);
|
||||||
|
result = MathMax(result, senkouSpanBLength);
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set Indicator Short Name and also we can define Buffers Labels ...
|
||||||
|
*/
|
||||||
|
void SetIndicatorName()
|
||||||
|
{
|
||||||
|
IndicatorSetString(INDICATOR_SHORTNAME, ShortName);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Define Required Buffers ...
|
||||||
|
*/
|
||||||
|
void DefineBuffers()
|
||||||
|
{
|
||||||
|
//
|
||||||
|
ENUM_DRAW_TYPE tenkanSenDrawType = showTenkanSen ? DRAW_LINE : DRAW_NONE;
|
||||||
|
|
||||||
|
//
|
||||||
|
ArraySetAsSeries(tenkanSenBuffer, true);
|
||||||
|
SetIndexBuffer(tenkanSenBufferIndex, tenkanSenBuffer, INDICATOR_DATA);
|
||||||
|
PlotIndexSetInteger(tenkanSenBufferIndex, PLOT_DRAW_BEGIN, maxLength);
|
||||||
|
PlotIndexSetInteger(tenkanSenBufferIndex, PLOT_SHOW_DATA, showTenkanSen);
|
||||||
|
PlotIndexSetInteger(tenkanSenBufferIndex, PLOT_DRAW_TYPE, tenkanSenDrawType);
|
||||||
|
|
||||||
|
//
|
||||||
|
ENUM_DRAW_TYPE kijunSenDrawType = showKijunSen ? DRAW_LINE : DRAW_NONE;
|
||||||
|
|
||||||
|
//
|
||||||
|
ArraySetAsSeries(kijunSenBuffer, true);
|
||||||
|
SetIndexBuffer(kijunSenBufferIndex, kijunSenBuffer, INDICATOR_DATA);
|
||||||
|
PlotIndexSetInteger(kijunSenBufferIndex, PLOT_DRAW_BEGIN, maxLength);
|
||||||
|
PlotIndexSetInteger(kijunSenBufferIndex, PLOT_SHOW_DATA, showKijunSen);
|
||||||
|
PlotIndexSetInteger(kijunSenBufferIndex, PLOT_DRAW_TYPE, kijunSenDrawType);
|
||||||
|
|
||||||
|
//
|
||||||
|
ENUM_DRAW_TYPE chikouSpanDrawType = showChikouSpan ? DRAW_LINE : DRAW_NONE;
|
||||||
|
|
||||||
|
//
|
||||||
|
ArraySetAsSeries(chikouSpanBuffer, true);
|
||||||
|
SetIndexBuffer(chikouSpanBufferIndex, chikouSpanBuffer, INDICATOR_DATA);
|
||||||
|
PlotIndexSetInteger(chikouSpanBufferIndex, PLOT_DRAW_BEGIN, maxLength);
|
||||||
|
PlotIndexSetInteger(chikouSpanBufferIndex, PLOT_SHIFT, -kijunSenLength);
|
||||||
|
PlotIndexSetInteger(chikouSpanBufferIndex, PLOT_SHOW_DATA, showChikouSpan);
|
||||||
|
PlotIndexSetInteger(chikouSpanBufferIndex, PLOT_DRAW_TYPE, chikouSpanDrawType);
|
||||||
|
|
||||||
|
//
|
||||||
|
int shiftSize = shiftKumo ? kijunSenLength : 0;
|
||||||
|
|
||||||
|
//
|
||||||
|
ENUM_DRAW_TYPE senkouSpanADrawType = showSenkouSpanA ? DRAW_LINE : DRAW_NONE;
|
||||||
|
|
||||||
|
//
|
||||||
|
ArraySetAsSeries(senkouSpanABuffer, true);
|
||||||
|
SetIndexBuffer(senkouSpanABufferIndex, senkouSpanABuffer, INDICATOR_DATA);
|
||||||
|
PlotIndexSetInteger(senkouSpanABufferIndex, PLOT_SHIFT, shiftSize);
|
||||||
|
PlotIndexSetInteger(senkouSpanABufferIndex, PLOT_SHOW_DATA, showSenkouSpanA);
|
||||||
|
PlotIndexSetInteger(senkouSpanABufferIndex, PLOT_DRAW_TYPE, senkouSpanADrawType);
|
||||||
|
|
||||||
|
//
|
||||||
|
ENUM_DRAW_TYPE senkouSpanBDrawType = showSenkouSpanB ? DRAW_LINE : DRAW_NONE;
|
||||||
|
|
||||||
|
//
|
||||||
|
ArraySetAsSeries(senkouSpanBBuffer, true);
|
||||||
|
SetIndexBuffer(senkouSpanBBufferIndex, senkouSpanBBuffer, INDICATOR_DATA);
|
||||||
|
PlotIndexSetInteger(senkouSpanBBufferIndex, PLOT_SHIFT, shiftSize);
|
||||||
|
PlotIndexSetInteger(senkouSpanBBufferIndex, PLOT_SHOW_DATA, showSenkouSpanB);
|
||||||
|
PlotIndexSetInteger(senkouSpanBBufferIndex, PLOT_DRAW_TYPE, senkouSpanBDrawType);
|
||||||
|
|
||||||
|
//
|
||||||
|
ArraySetAsSeries(senkouABuffer, true);
|
||||||
|
SetIndexBuffer(senkouABufferIndex, senkouABuffer, INDICATOR_DATA);
|
||||||
|
PlotIndexSetInteger(senkouABufferIndex, PLOT_DRAW_BEGIN, maxLength);
|
||||||
|
|
||||||
|
//
|
||||||
|
ArraySetAsSeries(senkouBBuffer, true);
|
||||||
|
SetIndexBuffer(senkouBBufferIndex, senkouBBuffer, INDICATOR_DATA);
|
||||||
|
PlotIndexSetInteger(senkouBBufferIndex, PLOT_DRAW_BEGIN, maxLength);
|
||||||
|
|
||||||
|
//
|
||||||
|
ENUM_DRAW_TYPE kumoDrawType = showKumo ? DRAW_FILLING : DRAW_NONE;
|
||||||
|
|
||||||
|
//
|
||||||
|
PlotIndexSetInteger(kumoBufferIndex, PLOT_SHIFT, shiftSize);
|
||||||
|
PlotIndexSetInteger(kumoBufferIndex, PLOT_SHOW_DATA, false);
|
||||||
|
PlotIndexSetInteger(kumoBufferIndex, PLOT_DRAW_TYPE, kumoDrawType);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
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)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
senkouABuffer[barIndex] = 0;
|
||||||
|
senkouBBuffer[barIndex] = 0;
|
||||||
|
kijunSenBuffer[barIndex] = 0;
|
||||||
|
tenkanSenBuffer[barIndex] = 0;
|
||||||
|
chikouSpanBuffer[barIndex] = 0;
|
||||||
|
senkouSpanABuffer[barIndex] = 0;
|
||||||
|
senkouSpanBBuffer[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[] //
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
double uppers[];
|
||||||
|
double lowers[];
|
||||||
|
|
||||||
|
//
|
||||||
|
double uppersMax = 0;
|
||||||
|
double uppersMin = 0;
|
||||||
|
|
||||||
|
//
|
||||||
|
double lowersMax = 0;
|
||||||
|
double lowersMin = 0;
|
||||||
|
|
||||||
|
//
|
||||||
|
// Calculate ChikouSpan ...
|
||||||
|
double iChikouSpan = GetAppliedPrice(
|
||||||
|
_Symbol,
|
||||||
|
_Period,
|
||||||
|
bar_index,
|
||||||
|
chikouSpanMode //
|
||||||
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
// Calculate TenkanSen ...
|
||||||
|
GetPriceBoundary(
|
||||||
|
uppers,
|
||||||
|
lowers,
|
||||||
|
_Symbol,
|
||||||
|
_Period,
|
||||||
|
tenkanSenMode,
|
||||||
|
tenkanSenLength,
|
||||||
|
bar_index //
|
||||||
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
uppersMax = uppers[ArrayMaximum(uppers)];
|
||||||
|
uppersMin = uppers[ArrayMinimum(uppers)];
|
||||||
|
|
||||||
|
//
|
||||||
|
lowersMax = lowers[ArrayMaximum(lowers)];
|
||||||
|
lowersMin = lowers[ArrayMinimum(lowers)];
|
||||||
|
|
||||||
|
//
|
||||||
|
double iTenkanSen = (uppersMax + lowersMin) / 2;
|
||||||
|
|
||||||
|
//
|
||||||
|
// Calculate KijunSen ...
|
||||||
|
GetPriceBoundary(
|
||||||
|
uppers,
|
||||||
|
lowers,
|
||||||
|
_Symbol,
|
||||||
|
_Period,
|
||||||
|
kijunSenMode,
|
||||||
|
kijunSenLength,
|
||||||
|
bar_index //
|
||||||
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
uppersMax = uppers[ArrayMaximum(uppers)];
|
||||||
|
uppersMin = uppers[ArrayMinimum(uppers)];
|
||||||
|
|
||||||
|
//
|
||||||
|
lowersMax = lowers[ArrayMaximum(lowers)];
|
||||||
|
lowersMin = lowers[ArrayMinimum(lowers)];
|
||||||
|
|
||||||
|
//
|
||||||
|
double iKijunSen = (uppersMax + lowersMin) / 2;
|
||||||
|
|
||||||
|
//
|
||||||
|
// Calculate SenkouSpanB ...
|
||||||
|
GetPriceBoundary(
|
||||||
|
uppers,
|
||||||
|
lowers,
|
||||||
|
_Symbol,
|
||||||
|
_Period,
|
||||||
|
senkouSpanBMode,
|
||||||
|
senkouSpanBLength,
|
||||||
|
bar_index //
|
||||||
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
uppersMax = uppers[ArrayMaximum(uppers)];
|
||||||
|
uppersMin = uppers[ArrayMinimum(uppers)];
|
||||||
|
|
||||||
|
//
|
||||||
|
lowersMax = lowers[ArrayMaximum(lowers)];
|
||||||
|
lowersMin = lowers[ArrayMinimum(lowers)];
|
||||||
|
|
||||||
|
//
|
||||||
|
double iSenkouSpanB = (uppersMax + lowersMin) / 2;
|
||||||
|
|
||||||
|
//
|
||||||
|
// Calculate SenkouSpanA ...
|
||||||
|
double iSenkouSpanA = (iTenkanSen + iKijunSen) / 2;
|
||||||
|
|
||||||
|
//
|
||||||
|
// Setting Buffers ...
|
||||||
|
kijunSenBuffer[bar_index] = iKijunSen;
|
||||||
|
senkouABuffer[bar_index] = iSenkouSpanA;
|
||||||
|
senkouBBuffer[bar_index] = iSenkouSpanB;
|
||||||
|
tenkanSenBuffer[bar_index] = iTenkanSen;
|
||||||
|
chikouSpanBuffer[bar_index] = iChikouSpan;
|
||||||
|
senkouSpanABuffer[bar_index] = iSenkouSpanA;
|
||||||
|
senkouSpanBBuffer[bar_index] = iSenkouSpanB;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,812 @@
|
|||||||
|
///////////////////////////////////////////////////////
|
||||||
|
//
|
||||||
|
// 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 "../Libraries/x-saherelm.common.lib.mq5"
|
||||||
|
|
||||||
|
//
|
||||||
|
// Inputs ...
|
||||||
|
|
||||||
|
//
|
||||||
|
// Market ...
|
||||||
|
input group "Market";
|
||||||
|
input int strLength = 14; // Length
|
||||||
|
input double strMultiplier = 3; // Multiplier
|
||||||
|
input int vidyaLength = 14; // Smothing Length
|
||||||
|
input ENUM_MA_METHOD strBoundaryMode = MODE_SMA; // Boundary Mode
|
||||||
|
input ENUM_APPLIED_PRICE strAppliedTo = PRICE_MEDIAN; // Applied To
|
||||||
|
input ENUM_APPLIED_PRICE vidyaAppliedTo = PRICE_CLOSE; // Smoothing Applied To
|
||||||
|
|
||||||
|
//
|
||||||
|
input group "Presentation";
|
||||||
|
input bool showStr = true; // Show Str
|
||||||
|
input bool showVidya = true; // Show Vidya
|
||||||
|
input bool showStrUpper = true; // Show Str Upper
|
||||||
|
input bool showStrLower = true; // Show Str Lower
|
||||||
|
input bool showStrLowMa = true; // Show Low Smoothed Buffer
|
||||||
|
input bool showStrHighMa = true; // Show High Smoothed Buffer
|
||||||
|
|
||||||
|
//
|
||||||
|
// Buffers ...
|
||||||
|
|
||||||
|
//
|
||||||
|
#define hideColorIDX 0
|
||||||
|
#define bullishColorIDX 1
|
||||||
|
#define bearishColorIDX 2
|
||||||
|
#define neuturalColorIDX 3
|
||||||
|
|
||||||
|
//
|
||||||
|
#define emptyValue 0.0
|
||||||
|
|
||||||
|
//
|
||||||
|
#property indicator_chart_window
|
||||||
|
|
||||||
|
//
|
||||||
|
#property indicator_buffers 10
|
||||||
|
#property indicator_plots 6
|
||||||
|
|
||||||
|
//
|
||||||
|
// STR ...
|
||||||
|
#define strBufferIndex 0
|
||||||
|
double strBuffer[];
|
||||||
|
|
||||||
|
#define strColorBufferIndex 1
|
||||||
|
double strColorBuffer[];
|
||||||
|
|
||||||
|
//
|
||||||
|
#define strPlotBufferIndex 0
|
||||||
|
#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
|
||||||
|
|
||||||
|
//
|
||||||
|
// Up ...
|
||||||
|
#define strUpBufferIndex 2
|
||||||
|
double strUpBuffer[];
|
||||||
|
|
||||||
|
//
|
||||||
|
#define strUpPlotBufferIndex 1
|
||||||
|
#property indicator_label2 "X121 STR U"
|
||||||
|
#property indicator_type2 DRAW_LINE
|
||||||
|
#property indicator_color2 clrBlueViolet
|
||||||
|
#property indicator_style2 STYLE_SOLID
|
||||||
|
#property indicator_width2 1
|
||||||
|
|
||||||
|
//
|
||||||
|
// Down ...
|
||||||
|
#define strDownBufferIndex 3
|
||||||
|
double strDownBuffer[];
|
||||||
|
|
||||||
|
//
|
||||||
|
#define strDownPlotBufferIndex 2
|
||||||
|
#property indicator_label3 "X121 STR D"
|
||||||
|
#property indicator_type3 DRAW_LINE
|
||||||
|
#property indicator_color3 clrBlueViolet
|
||||||
|
#property indicator_style3 STYLE_SOLID
|
||||||
|
#property indicator_width3 1
|
||||||
|
|
||||||
|
//
|
||||||
|
// High MA ...
|
||||||
|
#define strHighMaBufferIndex 4
|
||||||
|
double strHighMaBuffer[];
|
||||||
|
|
||||||
|
//
|
||||||
|
#define strHighMaPlotBufferIndex 3
|
||||||
|
#property indicator_label4 "X121 STR HSM"
|
||||||
|
#property indicator_type4 DRAW_LINE
|
||||||
|
#property indicator_color4 clrAqua
|
||||||
|
#property indicator_style4 STYLE_SOLID
|
||||||
|
#property indicator_width4 1
|
||||||
|
|
||||||
|
//
|
||||||
|
// Low Ma ...
|
||||||
|
#define strLowMaBufferIndex 5
|
||||||
|
double strLowMaBuffer[];
|
||||||
|
|
||||||
|
//
|
||||||
|
#define strLowMaPlotBufferIndex 4
|
||||||
|
#property indicator_label5 "X121 STR LSM"
|
||||||
|
#property indicator_type5 DRAW_LINE
|
||||||
|
#property indicator_color5 clrMagenta
|
||||||
|
#property indicator_style5 STYLE_SOLID
|
||||||
|
#property indicator_width5 1
|
||||||
|
|
||||||
|
//
|
||||||
|
// VIDYA ...
|
||||||
|
#define vidyaBufferIndex 6
|
||||||
|
double vidyaBuffer[];
|
||||||
|
|
||||||
|
//
|
||||||
|
#define vidyaPlotBufferIndex 5
|
||||||
|
#property indicator_label6 "X121 VIDYA"
|
||||||
|
#property indicator_type6 DRAW_LINE
|
||||||
|
#property indicator_color6 clrYellow
|
||||||
|
#property indicator_style6 STYLE_SOLID
|
||||||
|
#property indicator_width6 1
|
||||||
|
|
||||||
|
//
|
||||||
|
// Data Buffers ...
|
||||||
|
|
||||||
|
//
|
||||||
|
int mLastBufferIndex = 6;
|
||||||
|
|
||||||
|
//
|
||||||
|
// STR ...
|
||||||
|
|
||||||
|
//
|
||||||
|
// Atr ...
|
||||||
|
#define atrBufferIndex mLastBufferIndex + 1
|
||||||
|
double atrBuffer[];
|
||||||
|
|
||||||
|
//
|
||||||
|
// Price ...
|
||||||
|
#define strPriceBufferIndex mLastBufferIndex + 2
|
||||||
|
double strPriceBuffer[];
|
||||||
|
|
||||||
|
//
|
||||||
|
// Trend ...
|
||||||
|
#define strStateBufferIndex mLastBufferIndex + 3
|
||||||
|
double strStateBuffer[];
|
||||||
|
|
||||||
|
//
|
||||||
|
// Variables, Properties and etc ...
|
||||||
|
|
||||||
|
//
|
||||||
|
// this counts Available Bars ...
|
||||||
|
int limit;
|
||||||
|
|
||||||
|
//
|
||||||
|
int maxLength;
|
||||||
|
|
||||||
|
//
|
||||||
|
// ATR Handler ...
|
||||||
|
int atrHandler = INVALID_HANDLE;
|
||||||
|
|
||||||
|
//
|
||||||
|
// MA Handler ...
|
||||||
|
int lowMaHandler = INVALID_HANDLE;
|
||||||
|
int highMaHandler = INVALID_HANDLE;
|
||||||
|
int vidyaHandler = 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Low Ma ...
|
||||||
|
lowMaHandler = iMA(
|
||||||
|
_Symbol,
|
||||||
|
_Period,
|
||||||
|
strLength,
|
||||||
|
0,
|
||||||
|
strBoundaryMode,
|
||||||
|
PRICE_LOW //
|
||||||
|
);
|
||||||
|
isInited = lowMaHandler != INVALID_HANDLE;
|
||||||
|
if (!isInited)
|
||||||
|
{
|
||||||
|
return INIT_FAILED;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// High Ma ...
|
||||||
|
highMaHandler = iMA(
|
||||||
|
_Symbol,
|
||||||
|
_Period,
|
||||||
|
strLength,
|
||||||
|
0,
|
||||||
|
strBoundaryMode,
|
||||||
|
PRICE_HIGH //
|
||||||
|
);
|
||||||
|
isInited = highMaHandler != INVALID_HANDLE;
|
||||||
|
if (!isInited)
|
||||||
|
{
|
||||||
|
return INIT_FAILED;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Vidya ...
|
||||||
|
vidyaHandler = iVIDyA(
|
||||||
|
_Symbol,
|
||||||
|
_Period,
|
||||||
|
strLength,
|
||||||
|
vidyaLength,
|
||||||
|
0, // Shift ...
|
||||||
|
vidyaAppliedTo //
|
||||||
|
);
|
||||||
|
isInited = vidyaHandler != 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);
|
||||||
|
IndicatorRelease(vidyaHandler);
|
||||||
|
IndicatorRelease(lowMaHandler);
|
||||||
|
IndicatorRelease(highMaHandler);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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 ...
|
||||||
|
|
||||||
|
//
|
||||||
|
// Validate Calculated Bars ...
|
||||||
|
|
||||||
|
//
|
||||||
|
// ATR ...
|
||||||
|
int atrCalculatedBars = BarsCalculated(atrHandler);
|
||||||
|
|
||||||
|
//
|
||||||
|
// VIDYA ...
|
||||||
|
int vidyaCalculatedBars = BarsCalculated(vidyaHandler);
|
||||||
|
|
||||||
|
//
|
||||||
|
// Low Ma ...
|
||||||
|
int lowMaCalculatedBars = BarsCalculated(lowMaHandler);
|
||||||
|
|
||||||
|
//
|
||||||
|
// High Ma ...
|
||||||
|
int highMaCalculatedBars = BarsCalculated(highMaHandler);
|
||||||
|
|
||||||
|
//
|
||||||
|
bool isPassedRequiredCalculatedBars =
|
||||||
|
//
|
||||||
|
// ATR ...
|
||||||
|
atrCalculatedBars >= 0 &&
|
||||||
|
//
|
||||||
|
// VIDYA ...
|
||||||
|
vidyaCalculatedBars >= 0 &&
|
||||||
|
//
|
||||||
|
// Low Ma ...
|
||||||
|
lowMaCalculatedBars >= 0 &&
|
||||||
|
//
|
||||||
|
// High Ma ...
|
||||||
|
highMaCalculatedBars >= 0
|
||||||
|
//
|
||||||
|
;
|
||||||
|
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, rates_total, atrBuffer);
|
||||||
|
|
||||||
|
//
|
||||||
|
// VYDIA ...
|
||||||
|
int copiedVidyas = CopyBuffer(vidyaHandler, 0, 0, rates_total, vidyaBuffer);
|
||||||
|
|
||||||
|
//
|
||||||
|
// Low Ma ...
|
||||||
|
int copiedLowMas = CopyBuffer(lowMaHandler, 0, 0, rates_total, strLowMaBuffer);
|
||||||
|
|
||||||
|
//
|
||||||
|
// High Ma ...
|
||||||
|
int copiedHighMas = CopyBuffer(highMaHandler, 0, 0, rates_total, strHighMaBuffer);
|
||||||
|
|
||||||
|
//
|
||||||
|
// Validate Copied Items ...
|
||||||
|
bool isPassedRequiredCopiedItems =
|
||||||
|
//
|
||||||
|
// ATR ...
|
||||||
|
copiedAtrs >= 0 &&
|
||||||
|
//
|
||||||
|
// VIDYA ...
|
||||||
|
copiedVidyas >= 0 &&
|
||||||
|
//
|
||||||
|
// Low Ma ...
|
||||||
|
copiedLowMas >= 0 &&
|
||||||
|
//
|
||||||
|
// Hig Ma ...
|
||||||
|
copiedHighMas >= 0
|
||||||
|
//
|
||||||
|
;
|
||||||
|
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);
|
||||||
|
|
||||||
|
//
|
||||||
|
ENUM_DRAW_TYPE strUpDrawType = showStrUpper ? DRAW_LINE : DRAW_NONE;
|
||||||
|
SetIndexBuffer(strUpBufferIndex, strUpBuffer, INDICATOR_DATA);
|
||||||
|
PlotIndexSetDouble(strUpPlotBufferIndex, PLOT_EMPTY_VALUE, emptyValue);
|
||||||
|
PlotIndexSetInteger(strUpPlotBufferIndex, PLOT_SHOW_DATA, showStrUpper);
|
||||||
|
PlotIndexSetInteger(strUpPlotBufferIndex, PLOT_DRAW_TYPE, strUpDrawType);
|
||||||
|
|
||||||
|
//
|
||||||
|
ENUM_DRAW_TYPE strDownDrawType = showStrLower ? DRAW_LINE : DRAW_NONE;
|
||||||
|
SetIndexBuffer(strDownBufferIndex, strDownBuffer, INDICATOR_DATA);
|
||||||
|
PlotIndexSetDouble(strDownPlotBufferIndex, PLOT_EMPTY_VALUE, emptyValue);
|
||||||
|
PlotIndexSetInteger(strDownPlotBufferIndex, PLOT_SHOW_DATA, showStrLower);
|
||||||
|
PlotIndexSetInteger(strDownPlotBufferIndex, PLOT_DRAW_TYPE, strDownDrawType);
|
||||||
|
|
||||||
|
//
|
||||||
|
ENUM_DRAW_TYPE strLowMaDrawType = showStrLowMa ? DRAW_LINE : DRAW_NONE;
|
||||||
|
SetIndexBuffer(strLowMaBufferIndex, strLowMaBuffer, INDICATOR_DATA);
|
||||||
|
PlotIndexSetDouble(strLowMaPlotBufferIndex, PLOT_EMPTY_VALUE, emptyValue);
|
||||||
|
PlotIndexSetInteger(strLowMaPlotBufferIndex, PLOT_SHOW_DATA, showStrLowMa);
|
||||||
|
PlotIndexSetInteger(strLowMaPlotBufferIndex, PLOT_DRAW_TYPE, strLowMaDrawType);
|
||||||
|
|
||||||
|
//
|
||||||
|
ENUM_DRAW_TYPE strHighMaDrawType = showStrHighMa ? DRAW_LINE : DRAW_NONE;
|
||||||
|
SetIndexBuffer(strHighMaBufferIndex, strHighMaBuffer, INDICATOR_DATA);
|
||||||
|
PlotIndexSetDouble(strHighMaPlotBufferIndex, PLOT_EMPTY_VALUE, emptyValue);
|
||||||
|
PlotIndexSetInteger(strHighMaPlotBufferIndex, PLOT_SHOW_DATA, showStrHighMa);
|
||||||
|
PlotIndexSetInteger(strHighMaPlotBufferIndex, PLOT_DRAW_TYPE, strHighMaDrawType);
|
||||||
|
|
||||||
|
//
|
||||||
|
ENUM_DRAW_TYPE vidyaDrawType = showVidya ? DRAW_LINE : DRAW_NONE;
|
||||||
|
SetIndexBuffer(vidyaBufferIndex, vidyaBuffer, INDICATOR_DATA);
|
||||||
|
PlotIndexSetDouble(vidyaPlotBufferIndex, PLOT_EMPTY_VALUE, emptyValue);
|
||||||
|
PlotIndexSetInteger(vidyaPlotBufferIndex, PLOT_SHOW_DATA, showVidya);
|
||||||
|
PlotIndexSetInteger(vidyaPlotBufferIndex, PLOT_DRAW_TYPE, vidyaDrawType);
|
||||||
|
|
||||||
|
//
|
||||||
|
// Data Buffers ...
|
||||||
|
|
||||||
|
//
|
||||||
|
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] = emptyValue;
|
||||||
|
strUpBuffer[barIndex] = emptyValue;
|
||||||
|
strDownBuffer[barIndex] = emptyValue;
|
||||||
|
strPriceBuffer[barIndex] = emptyValue;
|
||||||
|
strLowMaBuffer[barIndex] = emptyValue;
|
||||||
|
strHighMaBuffer[barIndex] = emptyValue;
|
||||||
|
|
||||||
|
//
|
||||||
|
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 ...
|
||||||
|
ENUM_X_PRICE mPType = ToXPrice(strAppliedTo);
|
||||||
|
double price = GetAppliedPrice(
|
||||||
|
mPType,
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Str Mid ...
|
||||||
|
|
||||||
|
//
|
||||||
|
double iStrMid = strDownBuffer[bar_index] + ((strUpBuffer[bar_index] - strDownBuffer[bar_index]) / 2);
|
||||||
|
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
@@ -0,0 +1,567 @@
|
|||||||
|
///////////////////////////////////////////////////////
|
||||||
|
//
|
||||||
|
// 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();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
@@ -0,0 +1,712 @@
|
|||||||
|
///////////////////////////////////////////////////////
|
||||||
|
//
|
||||||
|
// 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[] //
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
ENUM_X_PRICE mPType = ToXPrice(vwapAppliedTo);
|
||||||
|
double price = GetAppliedPrice(
|
||||||
|
mPType,
|
||||||
|
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 //
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,882 @@
|
|||||||
|
///////////////////////////////////////////////////////
|
||||||
|
//
|
||||||
|
// 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 "../Libraries/x-saherelm.common.lib.mq5"
|
||||||
|
|
||||||
|
//
|
||||||
|
// Inputs ...
|
||||||
|
|
||||||
|
//
|
||||||
|
// Market ...
|
||||||
|
input group "Market";
|
||||||
|
|
||||||
|
//
|
||||||
|
input group "RSI Detection";
|
||||||
|
input int rsiLength = 14; // Length
|
||||||
|
input ENUM_X_PRICE rsiPriceType = X_PRICE_CLOSE; // Applied To
|
||||||
|
input ENUM_X_MA_METHOD rsiSmoothingMethod = X_MA_MODE_SMA; // Smoothing Method;
|
||||||
|
|
||||||
|
//
|
||||||
|
input group "ATR Detection";
|
||||||
|
input int atrLength = 14; // Length
|
||||||
|
input double atrMultiplier = 1; // Multiplier
|
||||||
|
input ENUM_X_PRICE atrUpperPriceType = X_PRICE_HIGH; // Upper Zone Applied To
|
||||||
|
input ENUM_X_PRICE atrLowerPriceType = X_PRICE_LOW; // Lower Zone Applied To
|
||||||
|
input ENUM_X_MA_METHOD atrSmoothingMethod = X_MA_MODE_SMA; // Smoothing Method
|
||||||
|
|
||||||
|
//
|
||||||
|
input group "Price Change";
|
||||||
|
input int priceChangeSmoothingLength = 14; // Length
|
||||||
|
input ENUM_X_PRICE priceChangeType = X_PRICE_CLOSE; // Price Type
|
||||||
|
input ENUM_X_MA_METHOD priceChangeSmoothingMethod = X_MA_MODE_SMA; // Moving Average Moethod
|
||||||
|
|
||||||
|
//
|
||||||
|
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
|
||||||
|
input bool showSmoothedAtrUpper = true; // Show Smoothed Upper Zone
|
||||||
|
input bool showSmoothedAtrLower = true; // Show Smoothed Lower Zone
|
||||||
|
input bool showRSIChange = true; // Show RSI Change
|
||||||
|
input bool showSmoothedRSIChange = true; // ShowSmoothed RSI Change
|
||||||
|
input bool showPriceChange = true; // Show Price Change
|
||||||
|
input bool showSmoothedPriceChange = true; // ShowSmoothed Price Change
|
||||||
|
|
||||||
|
//
|
||||||
|
// Buffers ...
|
||||||
|
|
||||||
|
//
|
||||||
|
#define hideColorIDX 0
|
||||||
|
#define bullishColorIDX 1
|
||||||
|
#define bearishColorIDX 2
|
||||||
|
#define neuturalColorIDX 3
|
||||||
|
|
||||||
|
//
|
||||||
|
#property indicator_chart_window
|
||||||
|
|
||||||
|
//
|
||||||
|
#property indicator_buffers 10
|
||||||
|
#property indicator_plots 8
|
||||||
|
|
||||||
|
//
|
||||||
|
// 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 1
|
||||||
|
|
||||||
|
//
|
||||||
|
// 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 1
|
||||||
|
|
||||||
|
//
|
||||||
|
// ATR Smoothed ...
|
||||||
|
|
||||||
|
//
|
||||||
|
// Upper ...
|
||||||
|
|
||||||
|
//
|
||||||
|
#define atrSmoothedUpperBufferIndex 2
|
||||||
|
double atrSmoothedUpperBuffer[];
|
||||||
|
|
||||||
|
#property indicator_label3 "X121 ATRUSM"
|
||||||
|
#property indicator_type3 DRAW_LINE
|
||||||
|
#property indicator_color3 clrYellow
|
||||||
|
#property indicator_style3 STYLE_DASH
|
||||||
|
#property indicator_width3 1
|
||||||
|
|
||||||
|
//
|
||||||
|
// Lower ...
|
||||||
|
|
||||||
|
//
|
||||||
|
#define atrSmoothedLowerBufferIndex 3
|
||||||
|
double atrSmoothedLowerBuffer[];
|
||||||
|
|
||||||
|
#property indicator_label4 "X121 ATRLSM"
|
||||||
|
#property indicator_type4 DRAW_LINE
|
||||||
|
#property indicator_color4 clrYellow
|
||||||
|
#property indicator_style4 STYLE_DASH
|
||||||
|
#property indicator_width4 1
|
||||||
|
|
||||||
|
//
|
||||||
|
// Price Change ...
|
||||||
|
|
||||||
|
//
|
||||||
|
#define priceChangeBufferIndex 4
|
||||||
|
double priceChangeBuffer[];
|
||||||
|
|
||||||
|
#property indicator_label5 "X121 PCH"
|
||||||
|
#property indicator_type5 DRAW_LINE
|
||||||
|
#property indicator_color5 clrOrchid
|
||||||
|
#property indicator_style5 STYLE_SOLID
|
||||||
|
#property indicator_width5 1
|
||||||
|
|
||||||
|
//
|
||||||
|
// Price Change Smoothed ...
|
||||||
|
|
||||||
|
//
|
||||||
|
#define priceChangeSmoothedBufferIndex 5
|
||||||
|
double priceChangeSmoothedBuffer[];
|
||||||
|
|
||||||
|
#property indicator_label6 "X121 PCHSM"
|
||||||
|
#property indicator_type6 DRAW_LINE
|
||||||
|
#property indicator_color6 clrOrchid
|
||||||
|
#property indicator_style6 STYLE_SOLID
|
||||||
|
#property indicator_width6 1
|
||||||
|
|
||||||
|
//
|
||||||
|
// RSI Change ...
|
||||||
|
|
||||||
|
//
|
||||||
|
#define rsiChangeBufferIndex 6
|
||||||
|
double rsiChangeBuffer[];
|
||||||
|
|
||||||
|
#property indicator_label7 "X121 RSICH"
|
||||||
|
#property indicator_type7 DRAW_LINE
|
||||||
|
#property indicator_color7 clrCornflowerBlue
|
||||||
|
#property indicator_style7 STYLE_SOLID
|
||||||
|
#property indicator_width7 1
|
||||||
|
|
||||||
|
//
|
||||||
|
// RSI Change Smoothed ...
|
||||||
|
|
||||||
|
//
|
||||||
|
#define rsiChangeSmoothedBufferIndex 7
|
||||||
|
double rsiChangeSmoothedBuffer[];
|
||||||
|
|
||||||
|
#property indicator_label8 "X121 RSICHSM"
|
||||||
|
#property indicator_type8 DRAW_LINE
|
||||||
|
#property indicator_color8 clrCornflowerBlue
|
||||||
|
#property indicator_style8 STYLE_SOLID
|
||||||
|
#property indicator_width8 1
|
||||||
|
|
||||||
|
//
|
||||||
|
// Data Buffers ...
|
||||||
|
|
||||||
|
//
|
||||||
|
int mLastBufferIndex = 7;
|
||||||
|
|
||||||
|
//
|
||||||
|
// ATR ...
|
||||||
|
|
||||||
|
#define atrBufferIndex mLastBufferIndex + 1
|
||||||
|
double atrBuffer[];
|
||||||
|
|
||||||
|
//
|
||||||
|
// RSI ...
|
||||||
|
#define rsiBufferIndex mLastBufferIndex + 2
|
||||||
|
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;
|
||||||
|
|
||||||
|
//
|
||||||
|
ENUM_APPLIED_PRICE rsiAppliedTo = ToAppliedPrice(rsiPriceType);
|
||||||
|
|
||||||
|
//
|
||||||
|
// 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 &&
|
||||||
|
//
|
||||||
|
priceChangeSmoothingLength > 0 &&
|
||||||
|
//
|
||||||
|
IsValid(rsiPriceType) &&
|
||||||
|
IsValid(priceChangeType) &&
|
||||||
|
IsValid(atrUpperPriceType) &&
|
||||||
|
IsValid(atrLowerPriceType)
|
||||||
|
//
|
||||||
|
;
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Extract Max Length of Inputs ...
|
||||||
|
*
|
||||||
|
* @return ( int )
|
||||||
|
*/
|
||||||
|
int ExtractMaxLengthOfInputs()
|
||||||
|
{
|
||||||
|
//
|
||||||
|
int result = 0;
|
||||||
|
|
||||||
|
//
|
||||||
|
result = MathMax(rsiLength, atrLength);
|
||||||
|
result = MathMax(result, priceChangeSmoothingLength);
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Define Required Buffers ...
|
||||||
|
*/
|
||||||
|
void DefineBuffers()
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// ATR ...
|
||||||
|
|
||||||
|
//
|
||||||
|
// UPPER ...
|
||||||
|
ENUM_DRAW_TYPE atrUpperDrawType = showAtrUpper ? DRAW_LINE : DRAW_NONE;
|
||||||
|
ArraySetAsSeries(atrUpperBuffer, true);
|
||||||
|
SetIndexBuffer(atrUpperBufferIndex, atrUpperBuffer, INDICATOR_DATA);
|
||||||
|
PlotIndexSetDouble(atrUpperBufferIndex, PLOT_EMPTY_VALUE, EMPTY_VALUE);
|
||||||
|
PlotIndexSetInteger(atrUpperBufferIndex, PLOT_SHOW_DATA, showAtrUpper);
|
||||||
|
PlotIndexSetInteger(atrUpperBufferIndex, PLOT_DRAW_TYPE, atrUpperDrawType);
|
||||||
|
|
||||||
|
//
|
||||||
|
// LOWER ...
|
||||||
|
ENUM_DRAW_TYPE atrLowerDrawType = showAtrLower ? DRAW_LINE : DRAW_NONE;
|
||||||
|
ArraySetAsSeries(atrLowerBuffer, true);
|
||||||
|
SetIndexBuffer(atrLowerBufferIndex, atrLowerBuffer, INDICATOR_DATA);
|
||||||
|
PlotIndexSetDouble(atrLowerBufferIndex, PLOT_EMPTY_VALUE, EMPTY_VALUE);
|
||||||
|
PlotIndexSetInteger(atrLowerBufferIndex, PLOT_SHOW_DATA, showAtrLower);
|
||||||
|
PlotIndexSetInteger(atrLowerBufferIndex, PLOT_DRAW_TYPE, atrLowerDrawType);
|
||||||
|
|
||||||
|
//
|
||||||
|
// ATR Smoothed ...
|
||||||
|
|
||||||
|
//
|
||||||
|
// UPPER ...
|
||||||
|
ENUM_DRAW_TYPE atrSmoothedUpperDrawType = showSmoothedAtrUpper ? DRAW_LINE : DRAW_NONE;
|
||||||
|
ArraySetAsSeries(atrSmoothedUpperBuffer, true);
|
||||||
|
SetIndexBuffer(atrSmoothedUpperBufferIndex, atrSmoothedUpperBuffer, INDICATOR_DATA);
|
||||||
|
PlotIndexSetDouble(atrSmoothedUpperBufferIndex, PLOT_EMPTY_VALUE, EMPTY_VALUE);
|
||||||
|
PlotIndexSetInteger(atrSmoothedUpperBufferIndex, PLOT_SHOW_DATA, showSmoothedAtrUpper);
|
||||||
|
PlotIndexSetInteger(atrSmoothedUpperBufferIndex, PLOT_DRAW_TYPE, atrSmoothedUpperDrawType);
|
||||||
|
|
||||||
|
//
|
||||||
|
// LOWER ...
|
||||||
|
ENUM_DRAW_TYPE atrSmoothedLowerDrawType = showSmoothedAtrLower ? DRAW_LINE : DRAW_NONE;
|
||||||
|
ArraySetAsSeries(atrSmoothedLowerBuffer, true);
|
||||||
|
SetIndexBuffer(atrSmoothedLowerBufferIndex, atrSmoothedLowerBuffer, INDICATOR_DATA);
|
||||||
|
PlotIndexSetDouble(atrSmoothedLowerBufferIndex, PLOT_EMPTY_VALUE, EMPTY_VALUE);
|
||||||
|
PlotIndexSetInteger(atrSmoothedLowerBufferIndex, PLOT_SHOW_DATA, showSmoothedAtrLower);
|
||||||
|
PlotIndexSetInteger(atrSmoothedLowerBufferIndex, PLOT_DRAW_TYPE, atrSmoothedLowerDrawType);
|
||||||
|
|
||||||
|
//
|
||||||
|
// PriceChange ...
|
||||||
|
ENUM_DRAW_TYPE priceChangeDrawType = showPriceChange ? DRAW_LINE : DRAW_NONE;
|
||||||
|
ArraySetAsSeries(priceChangeBuffer, true);
|
||||||
|
SetIndexBuffer(priceChangeBufferIndex, priceChangeBuffer, INDICATOR_DATA);
|
||||||
|
PlotIndexSetDouble(priceChangeBufferIndex, PLOT_EMPTY_VALUE, 0.0);
|
||||||
|
PlotIndexSetInteger(priceChangeBufferIndex, PLOT_SHOW_DATA, showPriceChange);
|
||||||
|
PlotIndexSetInteger(priceChangeBufferIndex, PLOT_DRAW_TYPE, priceChangeDrawType);
|
||||||
|
|
||||||
|
//
|
||||||
|
// PriceChange Smoothed ...
|
||||||
|
ENUM_DRAW_TYPE priceChangeSmoothedaDrawType = showSmoothedPriceChange ? DRAW_LINE : DRAW_NONE;
|
||||||
|
ArraySetAsSeries(priceChangeSmoothedBuffer, true);
|
||||||
|
SetIndexBuffer(priceChangeSmoothedBufferIndex, priceChangeSmoothedBuffer, INDICATOR_DATA);
|
||||||
|
PlotIndexSetDouble(priceChangeSmoothedBufferIndex, PLOT_EMPTY_VALUE, 0.0);
|
||||||
|
PlotIndexSetInteger(priceChangeSmoothedBufferIndex, PLOT_SHOW_DATA, showSmoothedPriceChange);
|
||||||
|
PlotIndexSetInteger(priceChangeSmoothedBufferIndex, PLOT_DRAW_TYPE, priceChangeSmoothedaDrawType);
|
||||||
|
|
||||||
|
//
|
||||||
|
// RSIChange ...
|
||||||
|
ENUM_DRAW_TYPE rsiChangeDrawType = showRSIChange ? DRAW_LINE : DRAW_NONE;
|
||||||
|
ArraySetAsSeries(rsiChangeBuffer, true);
|
||||||
|
SetIndexBuffer(rsiChangeBufferIndex, rsiChangeBuffer, INDICATOR_DATA);
|
||||||
|
PlotIndexSetDouble(rsiChangeBufferIndex, PLOT_EMPTY_VALUE, 0.0);
|
||||||
|
PlotIndexSetInteger(rsiChangeBufferIndex, PLOT_SHOW_DATA, showRSIChange);
|
||||||
|
PlotIndexSetInteger(rsiChangeBufferIndex, PLOT_DRAW_TYPE, rsiChangeDrawType);
|
||||||
|
|
||||||
|
//
|
||||||
|
// RSIChangeMa ...
|
||||||
|
ENUM_DRAW_TYPE rsiChangeMaDrawType = showSmoothedRSIChange ? DRAW_LINE : DRAW_NONE;
|
||||||
|
ArraySetAsSeries(rsiChangeSmoothedBuffer, true);
|
||||||
|
SetIndexBuffer(rsiChangeSmoothedBufferIndex, rsiChangeSmoothedBuffer, INDICATOR_DATA);
|
||||||
|
PlotIndexSetDouble(rsiChangeSmoothedBufferIndex, PLOT_EMPTY_VALUE, 0.0);
|
||||||
|
PlotIndexSetInteger(rsiChangeSmoothedBufferIndex, PLOT_SHOW_DATA, showSmoothedRSIChange);
|
||||||
|
PlotIndexSetInteger(rsiChangeSmoothedBufferIndex, PLOT_DRAW_TYPE, rsiChangeMaDrawType);
|
||||||
|
|
||||||
|
//
|
||||||
|
// Data Buffers ...
|
||||||
|
|
||||||
|
//
|
||||||
|
// ATR ...
|
||||||
|
ArraySetAsSeries(atrBuffer, true);
|
||||||
|
SetIndexBuffer(atrBufferIndex, atrBuffer, 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);
|
||||||
|
IndicatorSetInteger(INDICATOR_DIGITS, 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
rsiBuffer[barIndex] = 0;
|
||||||
|
atrBuffer[barIndex] = 0;
|
||||||
|
atrUpperBuffer[barIndex] = 0;
|
||||||
|
atrLowerBuffer[barIndex] = 0;
|
||||||
|
rsiChangeBuffer[barIndex] = 0;
|
||||||
|
priceChangeBuffer[barIndex] = 0;
|
||||||
|
atrSmoothedUpperBuffer[barIndex] = 0;
|
||||||
|
atrSmoothedLowerBuffer[barIndex] = 0;
|
||||||
|
rsiChangeSmoothedBuffer[barIndex] = 0;
|
||||||
|
priceChangeSmoothedBuffer[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[] //
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
double points = GetPoints(_Symbol);
|
||||||
|
|
||||||
|
//
|
||||||
|
// ATR Calculations ...
|
||||||
|
double iAtr = atrBuffer[bar_index];
|
||||||
|
double iMultiPliedAtr = iAtr * atrMultiplier;
|
||||||
|
|
||||||
|
//
|
||||||
|
// Select Upper Price ...
|
||||||
|
double iUpperPrice = GetAppliedPrice(
|
||||||
|
atrUpperPriceType,
|
||||||
|
open,
|
||||||
|
high,
|
||||||
|
low,
|
||||||
|
close,
|
||||||
|
bar_index //
|
||||||
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
// Select Lower Price ...
|
||||||
|
double iLowerPrice = GetAppliedPrice(
|
||||||
|
atrLowerPriceType,
|
||||||
|
open,
|
||||||
|
high,
|
||||||
|
low,
|
||||||
|
close,
|
||||||
|
bar_index //
|
||||||
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
// Calculate Atrs ...
|
||||||
|
|
||||||
|
//
|
||||||
|
double iAtrUpper = iUpperPrice + iMultiPliedAtr;
|
||||||
|
double iAtrLower = iLowerPrice - iMultiPliedAtr;
|
||||||
|
|
||||||
|
//
|
||||||
|
atrUpperBuffer[bar_index] = iAtrUpper;
|
||||||
|
atrLowerBuffer[bar_index] = iAtrLower;
|
||||||
|
|
||||||
|
//
|
||||||
|
bool canSmoothAtr = atrSmoothingMethod != X_MA_MODE_NONE;
|
||||||
|
if (canSmoothAtr)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Upper ...
|
||||||
|
iMAOnBuffer(
|
||||||
|
ratesTotal,
|
||||||
|
prevCalculated,
|
||||||
|
bar_index,
|
||||||
|
atrLength,
|
||||||
|
atrUpperBuffer,
|
||||||
|
atrSmoothedUpperBuffer,
|
||||||
|
atrSmoothingMethod //
|
||||||
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
// Lower ...
|
||||||
|
iMAOnBuffer(
|
||||||
|
ratesTotal,
|
||||||
|
prevCalculated,
|
||||||
|
bar_index,
|
||||||
|
atrLength,
|
||||||
|
atrLowerBuffer,
|
||||||
|
atrSmoothedLowerBuffer,
|
||||||
|
atrSmoothingMethod //
|
||||||
|
);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
//
|
||||||
|
atrSmoothedUpperBuffer[bar_index] = iAtrUpper;
|
||||||
|
atrSmoothedLowerBuffer[bar_index] = iAtrLower;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// RSI Change Calculations ...
|
||||||
|
|
||||||
|
//
|
||||||
|
double iRsi = rsiBuffer[bar_index];
|
||||||
|
double iRsiP = rsiBuffer[bar_index + 1];
|
||||||
|
double iRsiPrice = GetAppliedPrice(
|
||||||
|
rsiPriceType,
|
||||||
|
open,
|
||||||
|
high,
|
||||||
|
low,
|
||||||
|
close,
|
||||||
|
bar_index //
|
||||||
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
double iRsiChanged = iRsi - iRsiP;
|
||||||
|
double iRsiPointsChanged = iRsiChanged / points;
|
||||||
|
|
||||||
|
//
|
||||||
|
double iRChange = iRsiPrice + (iRsiChanged * points);
|
||||||
|
rsiChangeBuffer[bar_index] = iRChange;
|
||||||
|
|
||||||
|
//
|
||||||
|
bool canSmoothRsi = rsiSmoothingMethod != X_MA_MODE_NONE;
|
||||||
|
if (canSmoothRsi)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Upper ...
|
||||||
|
iMAOnBuffer(
|
||||||
|
ratesTotal,
|
||||||
|
prevCalculated,
|
||||||
|
bar_index,
|
||||||
|
atrLength,
|
||||||
|
rsiChangeBuffer,
|
||||||
|
rsiChangeSmoothedBuffer,
|
||||||
|
rsiSmoothingMethod //
|
||||||
|
);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
rsiChangeSmoothedBuffer[bar_index] = iRChange;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// PRICE Change Calculation ...
|
||||||
|
|
||||||
|
//
|
||||||
|
double iPChangePrice = GetAppliedPrice(
|
||||||
|
priceChangeType,
|
||||||
|
open,
|
||||||
|
high,
|
||||||
|
low,
|
||||||
|
close,
|
||||||
|
bar_index //
|
||||||
|
);
|
||||||
|
double iPPChangePrice = GetAppliedPrice(
|
||||||
|
priceChangeType,
|
||||||
|
open,
|
||||||
|
high,
|
||||||
|
low,
|
||||||
|
close,
|
||||||
|
bar_index + 1 //
|
||||||
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
double iPriceChange = iPChangePrice - iPPChangePrice;
|
||||||
|
double iPricePointsChanged = iPriceChange / points;
|
||||||
|
double iVolatilityChange = iPriceChange / iAtr;
|
||||||
|
|
||||||
|
//
|
||||||
|
double iPChange = iPChangePrice + (iPricePointsChanged * points);
|
||||||
|
priceChangeBuffer[bar_index] = iPChange;
|
||||||
|
|
||||||
|
//
|
||||||
|
bool canSmoothPriceChange = priceChangeSmoothingMethod != X_MA_MODE_NONE;
|
||||||
|
if (canSmoothPriceChange)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Upper ...
|
||||||
|
iMAOnBuffer(
|
||||||
|
ratesTotal,
|
||||||
|
prevCalculated,
|
||||||
|
bar_index,
|
||||||
|
atrLength,
|
||||||
|
priceChangeBuffer,
|
||||||
|
priceChangeSmoothedBuffer,
|
||||||
|
priceChangeSmoothingMethod //
|
||||||
|
);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
priceChangeSmoothedBuffer[bar_index] = iPChange;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,550 @@
|
|||||||
|
///////////////////////////////////////////////////////
|
||||||
|
//
|
||||||
|
// SaherElm IT Center MQL5 Ocillator
|
||||||
|
// -------------------------------------------------
|
||||||
|
// Name: X121 XDelta
|
||||||
|
// Description: Detect Comulative Volumes Delta ...
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 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 XDelta Ocillator"
|
||||||
|
#property strict
|
||||||
|
|
||||||
|
//
|
||||||
|
// Definitions ...
|
||||||
|
|
||||||
|
//
|
||||||
|
#define ShortName "X121_XDELTA"
|
||||||
|
|
||||||
|
//
|
||||||
|
// 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 showDeltaLine = true; // Show Delta Line
|
||||||
|
input bool showDeltaHistogram = true; // Show Delta Histogram
|
||||||
|
input bool showDeltaMa = true; // Show Delta Moving Average
|
||||||
|
|
||||||
|
//
|
||||||
|
// Buffers Props ...
|
||||||
|
|
||||||
|
//
|
||||||
|
#property indicator_separate_window
|
||||||
|
|
||||||
|
//
|
||||||
|
#property indicator_buffers 6
|
||||||
|
#property indicator_plots 3
|
||||||
|
|
||||||
|
//
|
||||||
|
#define hideColorIDX 0
|
||||||
|
#define bullishColorIDX 1
|
||||||
|
#define bearishColorIDX 2
|
||||||
|
|
||||||
|
//
|
||||||
|
// Buffers ...
|
||||||
|
|
||||||
|
//
|
||||||
|
#define deltaLineBufferIndex 0
|
||||||
|
double deltaLineBuffer[];
|
||||||
|
|
||||||
|
#property indicator_label1 "Delta Line"
|
||||||
|
#property indicator_type1 DRAW_LINE
|
||||||
|
#property indicator_color1 clrYellow
|
||||||
|
#property indicator_style1 STYLE_SOLID
|
||||||
|
#property indicator_width1 1
|
||||||
|
|
||||||
|
//
|
||||||
|
#define deltaHistogramBufferIndex 1
|
||||||
|
double deltaHistogramBuffer[];
|
||||||
|
|
||||||
|
//
|
||||||
|
#define deltaHistogramColorBufferIndex 2
|
||||||
|
double deltaHistogramColorBuffer[];
|
||||||
|
|
||||||
|
#property indicator_label2 "Delta Histogram"
|
||||||
|
#property indicator_type2 DRAW_COLOR_HISTOGRAM
|
||||||
|
#property indicator_color2 CLR_NONE, clrGreen, clrDarkRed
|
||||||
|
#property indicator_style2 STYLE_DOT
|
||||||
|
#property indicator_width2 1
|
||||||
|
|
||||||
|
//
|
||||||
|
#define deltaMaBufferIndex 3
|
||||||
|
double deltaMaBuffer[];
|
||||||
|
|
||||||
|
#property indicator_label3 "Delta MA"
|
||||||
|
#property indicator_type3 DRAW_LINE
|
||||||
|
#property indicator_color3 clrOrchid
|
||||||
|
#property indicator_style3 STYLE_SOLID
|
||||||
|
#property indicator_width3 1
|
||||||
|
|
||||||
|
//
|
||||||
|
// Data Buffers ...
|
||||||
|
|
||||||
|
#define mLastBufferIndex 3
|
||||||
|
|
||||||
|
//
|
||||||
|
#define rawDeltaBufferIndex mLastBufferIndex + 1
|
||||||
|
double rawDeltaBuffer[];
|
||||||
|
|
||||||
|
//
|
||||||
|
#define deltaStateBufferIndex mLastBufferIndex + 2
|
||||||
|
double deltaStateBuffer[];
|
||||||
|
|
||||||
|
//
|
||||||
|
// 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 ...
|
||||||
|
|
||||||
|
//
|
||||||
|
// Delta Line ...
|
||||||
|
ENUM_DRAW_TYPE deltaLineDrawType = showDeltaLine ? DRAW_LINE : DRAW_NONE;
|
||||||
|
|
||||||
|
//
|
||||||
|
ArraySetAsSeries(deltaLineBuffer, true);
|
||||||
|
SetIndexBuffer(deltaLineBufferIndex, deltaLineBuffer, INDICATOR_DATA);
|
||||||
|
PlotIndexSetDouble(deltaLineBufferIndex, PLOT_EMPTY_VALUE, 0.0);
|
||||||
|
PlotIndexSetInteger(deltaLineBufferIndex, PLOT_SHOW_DATA, showDeltaLine);
|
||||||
|
PlotIndexSetInteger(deltaLineBufferIndex, PLOT_DRAW_TYPE, deltaLineDrawType);
|
||||||
|
|
||||||
|
//
|
||||||
|
// Delta Histogram ...
|
||||||
|
ENUM_DRAW_TYPE deltaHistogramDrawType = showDeltaHistogram ? DRAW_COLOR_HISTOGRAM : DRAW_NONE;
|
||||||
|
|
||||||
|
//
|
||||||
|
ArraySetAsSeries(deltaHistogramBuffer, true);
|
||||||
|
SetIndexBuffer(deltaHistogramBufferIndex, deltaHistogramBuffer, INDICATOR_DATA);
|
||||||
|
PlotIndexSetDouble(deltaHistogramBufferIndex, PLOT_EMPTY_VALUE, 0.0);
|
||||||
|
PlotIndexSetInteger(deltaHistogramBufferIndex, PLOT_SHOW_DATA, showDeltaHistogram);
|
||||||
|
PlotIndexSetInteger(deltaHistogramBufferIndex, PLOT_DRAW_TYPE, deltaHistogramDrawType);
|
||||||
|
|
||||||
|
//
|
||||||
|
ArraySetAsSeries(deltaHistogramColorBuffer, true);
|
||||||
|
SetIndexBuffer(deltaHistogramColorBufferIndex, deltaHistogramColorBuffer, INDICATOR_COLOR_INDEX);
|
||||||
|
|
||||||
|
//
|
||||||
|
// Delta Ma ...
|
||||||
|
ENUM_DRAW_TYPE deltaMaDrawType = showDeltaMa ? DRAW_LINE : DRAW_NONE;
|
||||||
|
|
||||||
|
//
|
||||||
|
ArraySetAsSeries(deltaMaBuffer, true);
|
||||||
|
SetIndexBuffer(deltaMaBufferIndex, deltaMaBuffer, INDICATOR_DATA);
|
||||||
|
PlotIndexSetDouble(deltaMaBufferIndex, PLOT_EMPTY_VALUE, EMPTY_VALUE);
|
||||||
|
PlotIndexSetInteger(deltaMaBufferIndex, PLOT_SHOW_DATA, showDeltaMa);
|
||||||
|
PlotIndexSetInteger(deltaMaBufferIndex, PLOT_DRAW_TYPE, deltaMaDrawType);
|
||||||
|
|
||||||
|
//
|
||||||
|
// Data Buffers ...
|
||||||
|
|
||||||
|
//
|
||||||
|
ArraySetAsSeries(rawDeltaBuffer, true);
|
||||||
|
SetIndexBuffer(rawDeltaBufferIndex, rawDeltaBuffer, INDICATOR_CALCULATIONS);
|
||||||
|
|
||||||
|
//
|
||||||
|
ArraySetAsSeries(deltaStateBuffer, true);
|
||||||
|
SetIndexBuffer(deltaStateBufferIndex, deltaStateBuffer, 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)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
rawDeltaBuffer[barIndex] = 0.0;
|
||||||
|
deltaLineBuffer[barIndex] = 0.0;
|
||||||
|
deltaStateBuffer[barIndex] = 0.0;
|
||||||
|
deltaHistogramBuffer[barIndex] = 0.0;
|
||||||
|
deltaMaBuffer[barIndex] = 0.0;
|
||||||
|
deltaHistogramColorBuffer[barIndex] = hideColorIDX;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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 iDelta = 0.0;
|
||||||
|
|
||||||
|
//
|
||||||
|
// Detecting Delta Volume ...
|
||||||
|
if (bar.IsBullish())
|
||||||
|
{
|
||||||
|
iDelta = (double)bar.volume;
|
||||||
|
}
|
||||||
|
else if (bar.IsBearish())
|
||||||
|
{
|
||||||
|
iDelta = -(double)bar.volume;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Setting iDelta Value ...
|
||||||
|
if (isFirstBar)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
rawDeltaBuffer[bar_index] = iDelta;
|
||||||
|
deltaLineBuffer[bar_index] = iDelta;
|
||||||
|
deltaHistogramBuffer[bar_index] = iDelta;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
//
|
||||||
|
iDelta += deltaLineBuffer[bar_index + 1];
|
||||||
|
|
||||||
|
//
|
||||||
|
rawDeltaBuffer[bar_index] = iDelta;
|
||||||
|
deltaLineBuffer[bar_index] = iDelta;
|
||||||
|
deltaHistogramBuffer[bar_index] = iDelta;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Setting iDelta State ...
|
||||||
|
double iState = iDelta > 0
|
||||||
|
? bullishColorIDX
|
||||||
|
: iDelta < 0
|
||||||
|
? bearishColorIDX
|
||||||
|
: hideColorIDX;
|
||||||
|
//
|
||||||
|
// Setting iDelta Color ...
|
||||||
|
double iDeltaColor = iDelta > 0
|
||||||
|
? bullishColorIDX
|
||||||
|
: iDelta < 0
|
||||||
|
? bearishColorIDX
|
||||||
|
: hideColorIDX;
|
||||||
|
|
||||||
|
//
|
||||||
|
deltaStateBuffer[bar_index] = iState;
|
||||||
|
deltaHistogramColorBuffer[bar_index] = iDeltaColor;
|
||||||
|
|
||||||
|
//
|
||||||
|
// Calculate Moving Averages ...
|
||||||
|
int deltaMAsCount = iMAOnBuffer(
|
||||||
|
ratesTotal,
|
||||||
|
prevCalculated,
|
||||||
|
bar_index,
|
||||||
|
maLength,
|
||||||
|
rawDeltaBuffer,
|
||||||
|
deltaMaBuffer,
|
||||||
|
maMethod //
|
||||||
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
bar.Clean();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
@@ -0,0 +1,567 @@
|
|||||||
|
///////////////////////////////////////////////////////
|
||||||
|
//
|
||||||
|
// 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();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
@@ -0,0 +1,712 @@
|
|||||||
|
///////////////////////////////////////////////////////
|
||||||
|
//
|
||||||
|
// 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[] //
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
ENUM_X_PRICE mPType = ToXPrice(vwapAppliedTo);
|
||||||
|
double price = GetAppliedPrice(
|
||||||
|
mPType,
|
||||||
|
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 //
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
Reference in New Issue
Block a user