480 lines
11 KiB
Plaintext
480 lines
11 KiB
Plaintext
///////////////////////////////////////////////////////
|
|
//
|
|
// SaherElm IT Center MQL5 Indicator
|
|
// --------------------------------------------
|
|
// Name: XHK
|
|
// Description: Hiken Ashi ...
|
|
// Market Analysor ...
|
|
//
|
|
//
|
|
// 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 XHK Indicator"
|
|
#property strict
|
|
|
|
//
|
|
// Constants ...
|
|
|
|
//
|
|
// Indicator Short Name ...
|
|
#define ShortName "XHK"
|
|
|
|
//
|
|
// Imports ...
|
|
#include <MovingAverages.mqh>
|
|
#include "../Libraries/x-saherelm.common.lib.mq5"
|
|
|
|
//
|
|
// Inputs ...
|
|
|
|
//
|
|
input group "Market";
|
|
input int smoothingLength = 17; // Smoothing Length
|
|
//
|
|
input group "Presentation";
|
|
input bool drawHikenAshi = true; // Draw Hiken Ashi
|
|
input bool drawSmoothedHikenAshi = true; // Draw Smoothed Hiken Ashi
|
|
|
|
//
|
|
// Buffers ...
|
|
|
|
//
|
|
#property indicator_chart_window
|
|
|
|
//
|
|
#property indicator_buffers 10
|
|
#property indicator_plots 10
|
|
|
|
//
|
|
// Buffers ...
|
|
|
|
//
|
|
// Open ...
|
|
#define openBufferIndex 0
|
|
double openBuffer[];
|
|
|
|
//
|
|
// High ...
|
|
#define highBufferIndex 1
|
|
double highBuffer[];
|
|
|
|
//
|
|
// Low ...
|
|
#define lowBufferIndex 2
|
|
double lowBuffer[];
|
|
|
|
//
|
|
// Close ...
|
|
#define closeBufferIndex 3
|
|
double closeBuffer[];
|
|
|
|
//
|
|
// Candle Color ...
|
|
#define candleColorBufferIndex 4
|
|
double candleColorBuffer[];
|
|
|
|
//
|
|
#define candlesBufferIndex 0
|
|
|
|
//
|
|
#property indicator_label1 "XHK Open;XHK High;XHK Low;XHK Close"
|
|
#property indicator_type1 DRAW_COLOR_CANDLES
|
|
#property indicator_color1 CLR_NONE, clrAqua, clrMagenta
|
|
|
|
//
|
|
// SM Open ...
|
|
#define openSMBufferIndex 5
|
|
double openSMBuffer[];
|
|
|
|
//
|
|
// SM High ...
|
|
#define highSMBufferIndex 6
|
|
double highSMBuffer[];
|
|
|
|
//
|
|
// SM Low ...
|
|
#define lowSMBufferIndex 7
|
|
double lowSMBuffer[];
|
|
|
|
//
|
|
// SM Close ...
|
|
#define closeSMBufferIndex 8
|
|
double closeSMBuffer[];
|
|
|
|
//
|
|
// SM Candle Color ...
|
|
#define candleColorSMBufferIndex 9
|
|
double candleColorSMBuffer[];
|
|
|
|
//
|
|
#define candlesSMBufferIndex 1
|
|
|
|
//
|
|
#property indicator_label2 "XSMHK Open;XSMHK High;XSMHK Low;XSMHK Close"
|
|
#property indicator_type2 DRAW_COLOR_CANDLES
|
|
#property indicator_color2 CLR_NONE, clrDarkGreen, clrDarkRed
|
|
|
|
//
|
|
// Variables, Properties and etc ...
|
|
|
|
//
|
|
int maxLength;
|
|
|
|
//
|
|
double mHideColorIDX = 0;
|
|
|
|
//
|
|
// Event Handlers ...
|
|
|
|
//
|
|
// Initialization ...
|
|
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();
|
|
|
|
//
|
|
// 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, prev_calculated, rates_total, open, high, close, low);
|
|
}
|
|
|
|
//
|
|
CalculateSM(rates_total, prev_calculated);
|
|
|
|
//
|
|
return rates_total;
|
|
}
|
|
|
|
//
|
|
// 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()
|
|
{
|
|
//
|
|
// 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);
|
|
PlotIndexSetInteger(candleColorBufferIndex, PLOT_SHOW_DATA, false);
|
|
|
|
//
|
|
// Open ...
|
|
ArraySetAsSeries(openBuffer, true);
|
|
PlotIndexSetInteger(openBufferIndex, PLOT_SHOW_DATA, false);
|
|
SetIndexBuffer(openBufferIndex, openBuffer, INDICATOR_DATA);
|
|
|
|
//
|
|
// High ...
|
|
ArraySetAsSeries(highBuffer, true);
|
|
SetIndexBuffer(highBufferIndex, highBuffer, INDICATOR_DATA);
|
|
PlotIndexSetInteger(highBufferIndex, PLOT_SHOW_DATA, false);
|
|
|
|
//
|
|
// Close ...
|
|
ArraySetAsSeries(closeBuffer, true);
|
|
SetIndexBuffer(closeBufferIndex, closeBuffer, INDICATOR_DATA);
|
|
PlotIndexSetInteger(closeBufferIndex, PLOT_SHOW_DATA, false);
|
|
|
|
//
|
|
// Low ...
|
|
ArraySetAsSeries(lowBuffer, true);
|
|
SetIndexBuffer(lowBufferIndex, lowBuffer, INDICATOR_DATA);
|
|
PlotIndexSetInteger(lowBufferIndex, PLOT_SHOW_DATA, false);
|
|
|
|
//
|
|
// Candles Color ...
|
|
ArraySetAsSeries(candleColorSMBuffer, true);
|
|
SetIndexBuffer(candleColorSMBufferIndex, candleColorSMBuffer, INDICATOR_COLOR_INDEX);
|
|
|
|
//
|
|
// Set EMPTY_VALUE on Candles Buffer ...
|
|
PlotIndexSetDouble(candlesSMBufferIndex, PLOT_EMPTY_VALUE, 0.0);
|
|
|
|
//
|
|
PlotIndexSetInteger(candlesSMBufferIndex, PLOT_SHOW_DATA, false);
|
|
PlotIndexSetInteger(candleColorSMBufferIndex, PLOT_SHOW_DATA, false);
|
|
|
|
//
|
|
// Open ...
|
|
ArraySetAsSeries(openSMBuffer, true);
|
|
PlotIndexSetInteger(openSMBufferIndex, PLOT_SHOW_DATA, false);
|
|
SetIndexBuffer(openSMBufferIndex, openSMBuffer, INDICATOR_DATA);
|
|
|
|
//
|
|
// High ...
|
|
ArraySetAsSeries(highSMBuffer, true);
|
|
SetIndexBuffer(highSMBufferIndex, highSMBuffer, INDICATOR_DATA);
|
|
PlotIndexSetInteger(highSMBufferIndex, PLOT_SHOW_DATA, false);
|
|
|
|
//
|
|
// Close ...
|
|
ArraySetAsSeries(closeSMBuffer, true);
|
|
SetIndexBuffer(closeSMBufferIndex, closeSMBuffer, INDICATOR_DATA);
|
|
PlotIndexSetInteger(closeSMBufferIndex, PLOT_SHOW_DATA, false);
|
|
|
|
//
|
|
// Low ...
|
|
ArraySetAsSeries(lowSMBuffer, true);
|
|
SetIndexBuffer(lowSMBufferIndex, lowSMBuffer, INDICATOR_DATA);
|
|
PlotIndexSetInteger(lowSMBufferIndex, PLOT_SHOW_DATA, false);
|
|
}
|
|
|
|
//
|
|
// 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
|
|
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 = openBuffer[bar_index + 1];
|
|
mPrevHKCloseValue = closeBuffer[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));
|
|
|
|
//
|
|
openBuffer[bar_index] = mHKOpenValue;
|
|
highBuffer[bar_index] = mHKHighValue;
|
|
lowBuffer[bar_index] = mHKLowValue;
|
|
closeBuffer[bar_index] = mHKCloseValue;
|
|
|
|
//
|
|
bool isBearish =
|
|
//
|
|
openBuffer[bar_index] > closeBuffer[bar_index]
|
|
//
|
|
;
|
|
|
|
//
|
|
candleColorBuffer[bar_index] = !drawHikenAshi
|
|
? mHideColorIDX
|
|
: isBearish
|
|
? 2
|
|
: 1;
|
|
}
|
|
|
|
//
|
|
// Calculate Smoothed HikenAshi ...
|
|
void CalculateSM(
|
|
int ratesTotal, // Total Bars
|
|
int prevCalculated // Calculated Bars
|
|
)
|
|
{
|
|
//
|
|
// Open ...
|
|
int calculatedSMHKOpens = ExponentialMAOnBuffer(
|
|
ratesTotal,
|
|
prevCalculated,
|
|
0,
|
|
smoothingLength,
|
|
openBuffer,
|
|
openSMBuffer);
|
|
|
|
//
|
|
// High ...
|
|
int calculatedSMHKHighs = ExponentialMAOnBuffer(
|
|
ratesTotal,
|
|
prevCalculated,
|
|
0,
|
|
smoothingLength,
|
|
highBuffer,
|
|
highSMBuffer);
|
|
|
|
//
|
|
// Low ...
|
|
int calculatedSMHKLows = ExponentialMAOnBuffer(
|
|
ratesTotal,
|
|
prevCalculated,
|
|
0,
|
|
smoothingLength,
|
|
lowBuffer,
|
|
lowSMBuffer);
|
|
|
|
//
|
|
// Close ...
|
|
int calculatedSMHKCloses = ExponentialMAOnBuffer(
|
|
ratesTotal,
|
|
prevCalculated,
|
|
0,
|
|
smoothingLength,
|
|
closeBuffer,
|
|
closeSMBuffer);
|
|
|
|
//
|
|
// Find Calculated Items for Colors ...
|
|
int mNumberOfItems = MathMin(calculatedSMHKOpens, calculatedSMHKHighs);
|
|
mNumberOfItems = MathMin(mNumberOfItems, calculatedSMHKLows);
|
|
mNumberOfItems = MathMin(mNumberOfItems, calculatedSMHKCloses);
|
|
|
|
//
|
|
// Create a Loop for Color of Candles ...
|
|
for (int i = 0; i < mNumberOfItems; i++)
|
|
{
|
|
//
|
|
bool isBearish =
|
|
//
|
|
openSMBuffer[i] > closeSMBuffer[i]
|
|
//
|
|
;
|
|
|
|
//
|
|
if (drawSmoothedHikenAshi)
|
|
{
|
|
//
|
|
candleColorSMBuffer[i] = isBearish
|
|
? 2
|
|
: 1;
|
|
}
|
|
else
|
|
{
|
|
//
|
|
candleColorSMBuffer[i] = mHideColorIDX;
|
|
}
|
|
}
|
|
}
|