Files
2024-05-17 04:54:01 +03:30

487 lines
12 KiB
Plaintext

///////////////////////////////////////////////////////
//
// SaherElm IT Center MQL5 Indicator
// --------------------------------------------
// Name: X121_HK
// 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 X121_XHK Indicator"
#property strict
//
// START Constants ...
//
//
// Indicator Short Name ...
#define ShortName "X121_XHK"
//
// END Constants ...
//
//
// START Inputs ...
//
//
input group "Presentation";
input bool drawCandles = true; // Draw Hiken Ashi Candle
input bool drawCandleLines = false; // Show HikenAshi Lines
//
// END Inputs ...
//
//
// START Includes and Imports ...
//
//
// Includes Common Library ...
#include "../Libraries/x-saherelm.common.lib.mq5"
//
// END Includes and Imports ...
//
//
// START Buffers ...
//
#property indicator_chart_window
//
#property indicator_buffers 13
#property indicator_plots 5
//
// PLOTTED 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 "X121_XHK Open;X121_XHK High;X121_XHK Low;X121_XHK Close"
#property indicator_type1 DRAW_COLOR_CANDLES
#property indicator_color1 clrAqua, clrMagenta, CLR_NONE
//
#define openHKBufferIndex 5
#define openHKColorBufferIndex 6
double openHKBuffer[];
double openHKColorBuffer[];
//
#define openHKBufferPlotIndex 1
#property indicator_label2 "X121_XHK O"
#property indicator_type2 DRAW_COLOR_LINE
#property indicator_color2 clrAqua, clrMagenta, clrLightGray
#property indicator_style2 STYLE_DOT
#property indicator_width2 1
//
#define highHKBufferIndex 7
#define highHKColorBufferIndex 8
double highHKBuffer[];
double highHKColorBuffer[];
//
#define highHKBufferPlotIndex 2
#property indicator_label3 "X121_XHK H"
#property indicator_type3 DRAW_COLOR_LINE
#property indicator_color3 clrAqua, clrMagenta, clrLightGray
#property indicator_style3 STYLE_DOT
#property indicator_width3 1
//
#define lowHKBufferIndex 9
#define lowHKColorBufferIndex 10
double lowHKBuffer[];
double lowHKColorBuffer[];
//
#define lowHKBufferPlotIndex 3
#property indicator_label4 "X121_XHK L"
#property indicator_type4 DRAW_COLOR_LINE
#property indicator_color4 clrAqua, clrMagenta, clrLightGray
#property indicator_style4 STYLE_DOT
#property indicator_width4 1
//
#define closeHKBufferIndex 11
#define closeHKColorBufferIndex 12
double closeHKBuffer[];
double closeHKColorBuffer[];
//
#define closeHKBufferPlotIndex 4
#property indicator_label5 "X121_XHK C"
#property indicator_type5 DRAW_COLOR_LINE
#property indicator_color5 clrAqua, clrMagenta, clrLightGray
#property indicator_style5 STYLE_DOT
#property indicator_width5 1
//
// END Buffers ...
//
//
// START Global Definitions: Variables, Properties and etc ...
//
//
int maxLength;
//
// END Global Definitions: Variables, Properties and etc ...
//
//
// START 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(
//
// total Candles on chart ...
const int rates_total,
//
// total calculated Candles on charts ...
const int prev_calculated,
//
// history of Candles Open Time ...
const datetime &time[],
//
// history of Candles Open Price ...
const double &open[],
//
// history of Candles High Price ...
const double &high[],
//
// history of Candles Low Price ...
const double &low[],
//
// history of Candles Close Price ...
const double &close[],
//
// history of Tick Volumes on Candle ...
const long &tick_volume[],
//
// history of Trade Volumes ...
const long &volume[],
//
// history of Candles Spread Price ...
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);
//
// 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);
}
//
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()
{
//
// 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);
//
// Open HK ...
ENUM_DRAW_TYPE openDrawType = drawCandleLines ? DRAW_COLOR_LINE : DRAW_NONE;
ArraySetAsSeries(openHKBuffer, true);
SetIndexBuffer(openHKBufferIndex, openHKBuffer, INDICATOR_DATA);
PlotIndexSetInteger(openHKBufferPlotIndex, PLOT_SHOW_DATA, drawCandleLines);
PlotIndexSetInteger(openHKBufferPlotIndex, PLOT_DRAW_TYPE, openDrawType);
//
ArraySetAsSeries(openHKColorBuffer, true);
SetIndexBuffer(openHKColorBufferIndex, openHKColorBuffer, INDICATOR_COLOR_INDEX);
//
// High HK ...
ENUM_DRAW_TYPE highDrawType = drawCandleLines ? DRAW_COLOR_LINE : DRAW_NONE;
ArraySetAsSeries(highHKBuffer, true);
SetIndexBuffer(highHKBufferIndex, highHKBuffer, INDICATOR_DATA);
PlotIndexSetInteger(highHKBufferPlotIndex, PLOT_SHOW_DATA, drawCandleLines);
PlotIndexSetInteger(highHKBufferPlotIndex, PLOT_DRAW_TYPE, highDrawType);
//
ArraySetAsSeries(highHKColorBuffer, true);
SetIndexBuffer(highHKColorBufferIndex, highHKColorBuffer, INDICATOR_COLOR_INDEX);
//
// Low HK ...
ENUM_DRAW_TYPE lowDrawType = drawCandleLines ? DRAW_COLOR_LINE : DRAW_NONE;
ArraySetAsSeries(lowHKBuffer, true);
SetIndexBuffer(lowHKBufferIndex, lowHKBuffer, INDICATOR_DATA);
PlotIndexSetInteger(lowHKBufferPlotIndex, PLOT_SHOW_DATA, drawCandleLines);
PlotIndexSetInteger(lowHKBufferPlotIndex, PLOT_DRAW_TYPE, lowDrawType);
//
ArraySetAsSeries(lowHKColorBuffer, true);
SetIndexBuffer(lowHKColorBufferIndex, lowHKColorBuffer, INDICATOR_COLOR_INDEX);
//
// Close HK ...
ENUM_DRAW_TYPE closeDrawType = drawCandleLines ? DRAW_COLOR_LINE : DRAW_NONE;
ArraySetAsSeries(closeHKBuffer, true);
SetIndexBuffer(closeHKBufferIndex, closeHKBuffer, INDICATOR_DATA);
PlotIndexSetInteger(closeHKBufferPlotIndex, PLOT_SHOW_DATA, drawCandleLines);
PlotIndexSetInteger(closeHKBufferPlotIndex, PLOT_DRAW_TYPE, closeDrawType);
//
ArraySetAsSeries(closeHKColorBuffer, true);
SetIndexBuffer(closeHKColorBufferIndex, closeHKColorBuffer, INDICATOR_COLOR_INDEX);
}
//
// 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;
//
double candleColorValue =
mHKOpenValue < mHKCloseValue ? 0 : 1;
//
candleColorBuffer[bar_index] = drawCandles ? candleColorValue : 2;
//
openHKBuffer[bar_index] = mHKOpenValue;
openHKColorBuffer[bar_index] = candleColorValue;
//
highHKBuffer[bar_index] = mHKHighValue;
highHKColorBuffer[bar_index] = candleColorValue;
//
lowHKBuffer[bar_index] = mHKLowValue;
lowHKColorBuffer[bar_index] = candleColorValue;
//
closeHKBuffer[bar_index] = mHKCloseValue;
closeHKColorBuffer[bar_index] = candleColorValue;
}
//
// END Functions ...
//