Files
MQL5Data/Libraries/x-saherelm.tools.lib.mq5
T

1280 lines
20 KiB
Plaintext

///////////////////////////////////////////////////////
//
// SaherElm IT Center MQL5 Tools Library
// --------------------------------------
// Name: XToolsLib
// Description: provide all commonly used Tools ...
//
//
// Maintainer:
// ------------
// Hadi Khazaee Asl (hadi_khazaee_asl@yahoo.com)
//
//////////////////////////////////////////////////////
//
// Global Properties ...
#property library
#property copyright "Copyright 2023, SaherElm IT Center"
#property link "https://www.saherelm.ir"
#property version "1.00"
#property strict
//
// Imports ...
#include "../Libraries/x-saherelm.draw.lib.mq5"
#include "../Libraries/x-saherelm.common.lib.mq5"
//
// Definitions ...
//
// Drawing Tools ...
//
// Hammer ...
//
string GetHammerTag(XOHCL &bar)
{
return "Hammer-" + GetBarTimeTag(bar);
}
//
bool DrawHammer(
XOHCL &bar,
long chartID,
color clr = clrAqua,
uint width = 1,
uchar arrow = 233 //
)
{
//
bool result = false;
//
string name = GetHammerTag(bar);
//
result = DrawArrow(
chartID,
name,
0,
bar.time,
bar.low,
arrow,
ANCHOR_TOP,
clr,
STYLE_SOLID,
width //
);
//
return result;
}
//
void RemoveHammer(XOHCL &bar)
{
RemoveDraws(GetHammerTag(bar));
}
//
// Shooting Star ...
//
string GetShootingStarTag(XOHCL &bar)
{
return "ShootingStar-" + GetBarTimeTag(bar);
}
//
bool DrawShootingStar(
XOHCL &bar,
long chartID,
color clr = clrMagenta,
uint width = 1,
uchar arrow = 234 //
)
{
//
bool result = false;
//
string name = GetShootingStarTag(bar);
//
result = DrawArrow(
chartID,
name,
0,
bar.time,
bar.high,
arrow,
ANCHOR_BOTTOM,
clr,
STYLE_SOLID,
width //
);
//
return result;
}
//
void RemoveShootingStar(XOHCL &bar)
{
RemoveDraws(GetShootingStarTag(bar));
}
//
// Bullish Engulfing ...
//
string GetBullishEngulfingTag(XOHCL &bar)
{
return "BullishEngulfing-" + GetBarTimeTag(bar);
}
//
bool DrawBullishEngulfing(
XOHCL &bar,
long chartID,
color clr = clrAqua,
bool fill = false,
uint width = 1,
ENUM_LINE_STYLE style = STYLE_SOLID //
)
{
//
bool result = false;
//
XOHCL pBar;
result = bar.GetPreviousBar(pBar);
if (!result)
{
return result;
}
//
string name = GetBullishEngulfingTag(bar);
//
DrawRectangle(
chartID,
name,
0,
pBar.time,
pBar.low,
bar.time,
bar.high,
clr,
style,
width,
fill //
);
//
return result;
}
//
void RemoveBullishEngulfing(XOHCL &bar)
{
RemoveDraws(GetBullishEngulfingTag(bar));
}
//
// Bearish Engulfing ...
//
string GetBearishEngulfingTag(XOHCL &bar)
{
return "BearishEngulfing-" + GetBarTimeTag(bar);
}
//
bool DrawBearishEngulfing(
XOHCL &bar,
long chartID,
color clr = clrMagenta,
bool fill = false,
uint width = 1,
ENUM_LINE_STYLE style = STYLE_SOLID //
)
{
//
bool result = false;
//
XOHCL pBar;
result = bar.GetPreviousBar(pBar);
if (!result)
{
return result;
}
//
string name = GetBearishEngulfingTag(bar);
//
DrawRectangle(
chartID,
name,
0,
pBar.time,
pBar.low,
bar.time,
bar.high,
clr,
style,
width,
fill //
);
//
return result;
}
//
void RemoveBearishEngulfing(XOHCL &bar)
{
RemoveDraws(GetBearishEngulfingTag(bar));
}
//
// Morning Star ...
//
string GetMorningStarTag(XOHCL &bar)
{
return "MorningStar-" + GetBarTimeTag(bar);
}
//
bool DrawMorningStar(
XOHCL &bar,
long chartID,
color clr = clrAqua,
uint width = 1,
uchar arrow = 233 //
)
{
//
bool result = false;
//
string name = GetMorningStarTag(bar);
//
result = DrawArrow(
chartID,
name,
0,
bar.time,
bar.low,
arrow,
ANCHOR_TOP,
clr,
STYLE_SOLID,
width //
);
//
return result;
}
//
void RemoveMorningStar(XOHCL &bar)
{
RemoveDraws(GetMorningStarTag(bar));
}
//
// Evening Star ...
//
string GetEveningStarTag(XOHCL &bar)
{
return "EveningStar-" + GetBarTimeTag(bar);
}
//
bool DrawEveningStar(
XOHCL &bar,
long chartID,
color clr = clrMagenta,
uint width = 1,
uchar arrow = 234 //
)
{
//
bool result = false;
//
string name = GetEveningStarTag(bar);
//
result = DrawArrow(
chartID,
name,
0,
bar.time,
bar.high,
arrow,
ANCHOR_BOTTOM,
clr,
STYLE_SOLID,
width //
);
//
return result;
}
//
void RemoveEveningStar(XOHCL &bar)
{
RemoveDraws(GetEveningStarTag(bar));
}
//
// Swing High ...
//
string GetSwingHighTag(XOHCL &bar)
{
return "SwingHigh-" + GetBarTimeTag(bar);
}
//
bool DrawSwingHigh(
XOHCL &bar,
long chartID,
color clr = clrMagenta,
uint width = 1,
uchar arrow = 108 //
)
{
//
bool result = false;
//
string name = GetSwingHighTag(bar);
//
result = DrawArrow(
chartID,
name,
0,
bar.time,
bar.high,
arrow,
ANCHOR_BOTTOM,
clr,
STYLE_SOLID,
width //
);
//
return result;
}
//
void RemoveSwingHigh(XOHCL &bar)
{
RemoveDraws(GetSwingHighTag(bar));
}
//
// Swing Low ...
//
string GetSwingLowTag(XOHCL &bar)
{
return "SwingLow-" + GetBarTimeTag(bar);
}
//
bool DrawSwingLow(
XOHCL &bar,
long chartID,
color clr = clrAqua,
uint width = 1,
uchar arrow = 108 //
)
{
//
bool result = false;
//
string name = GetSwingLowTag(bar);
//
result = DrawArrow(
chartID,
name,
0,
bar.time,
bar.low,
arrow,
ANCHOR_TOP,
clr,
STYLE_SOLID,
width //
);
//
return result;
}
//
void RemoveSwingLow(XOHCL &bar)
{
RemoveDraws(GetSwingLowTag(bar));
}
//
// Bullish FVG ...
//
string GetBullishFVGTag(XOHCL &bar)
{
return "BullishFVG-" + GetBarTimeTag(bar);
}
//
bool DrawBullishFVG(
XOHCL &bar,
double upper,
double lower,
long chartID,
color clr = clrAqua,
bool fill = false,
uint width = 1,
ENUM_LINE_STYLE style = STYLE_SOLID //
)
{
//
bool result = false;
//
XOHCL pBar;
result = bar.GetPreviousBar(pBar);
if (!result)
{
return result;
}
//
XOHCL ppBar;
result = pBar.GetPreviousBar(ppBar);
if (!result)
{
return result;
}
//
string name = GetBullishFVGTag(bar);
//
DrawRectangle(
chartID,
name,
0,
ppBar.time,
lower,
bar.time,
upper,
clr,
style,
width,
fill //
);
//
return result;
}
//
void RemoveBullishFVG(XOHCL &bar)
{
RemoveDraws(GetBullishFVGTag(bar));
}
//
// Bearish FVG ...
//
string GetBearishFVGTag(XOHCL &bar)
{
return "BearishFVG-" + GetBarTimeTag(bar);
}
//
bool DrawBearishFVG(
XOHCL &bar,
double upper,
double lower,
long chartID,
color clr = clrMagenta,
bool fill = false,
uint width = 1,
ENUM_LINE_STYLE style = STYLE_SOLID //
)
{
//
bool result = false;
//
XOHCL pBar;
result = bar.GetPreviousBar(pBar);
if (!result)
{
return result;
}
//
XOHCL ppBar;
result = pBar.GetPreviousBar(ppBar);
if (!result)
{
return result;
}
//
string name = GetBearishFVGTag(bar);
//
DrawRectangle(
chartID,
name,
0,
ppBar.time,
upper,
bar.time,
lower,
clr,
style,
width,
fill //
);
//
return result;
}
//
void RemoveBearishFVG(XOHCL &bar)
{
RemoveDraws(GetBearishFVGTag(bar));
}
//
// Bullish Order Block ...
//
string GetBullishOrderBlockTag(XOHCL &bar)
{
return "BullishOrderBlock-" + GetBarTimeTag(bar);
}
//
bool DrawBullishOrderBlock(
XOHCL &bar,
long chartID,
bool ray = true,
color clr = clrAqua,
ENUM_LINE_STYLE style = STYLE_DOT,
uint width = 1 //
)
{
//
bool result = false;
//
string name = GetBullishOrderBlockTag(bar);
datetime cTime = TimeCurrent();
//
// Draw Upper Line ...
result = DrawTrendLine(
chartID,
name + "-Upper",
0,
bar.time,
bar.high,
cTime,
bar.high,
clr,
style,
width,
false,
false,
ray //
);
if (!result) {
return result;
}
//
// Draw Lower Line ...
result = DrawTrendLine(
chartID,
name + "-Lower",
0,
bar.time,
bar.low,
cTime,
bar.low,
clr,
style,
width,
false,
false,
ray //
);
if (!result) {
return result;
}
//
return result;
}
//
void RemoveBullishOrderBlock(XOHCL &bar)
{
RemoveDraws(GetBullishOrderBlockTag(bar));
}
//
// Bearish Order Block ...
//
string GetBearishOrderBlockTag(XOHCL &bar)
{
return "BearishOrderBlock-" + GetBarTimeTag(bar);
}
//
bool DrawBearishOrderBlock(
XOHCL &bar,
long chartID,
bool ray = true,
color clr = clrMagenta,
ENUM_LINE_STYLE style = STYLE_DOT,
uint width = 1 //
)
{
//
bool result = false;
//
string name = GetBearishOrderBlockTag(bar);
datetime cTime = TimeCurrent();
//
// Draw Upper Line ...
result = DrawTrendLine(
chartID,
name + "-Upper",
0,
bar.time,
bar.high,
cTime,
bar.high,
clr,
style,
width,
false,
false,
ray //
);
if (!result) {
return result;
}
//
// Draw Lower Line ...
result = DrawTrendLine(
chartID,
name + "-Lower",
0,
bar.time,
bar.low,
cTime,
bar.low,
clr,
style,
width,
false,
false,
ray //
);
if (!result) {
return result;
}
//
return result;
}
//
void RemoveBearishOrderBlock(XOHCL &bar)
{
RemoveDraws(GetBearishOrderBlockTag(bar));
}
//
// Support ...
//
string GetSupportTag(XOHCL &bar)
{
return "Support-" + GetBarTimeTag(bar);
}
//
bool DrawSupport(
XOHCL &bar,
long chartID,
double _threshold = 0,
bool ray = true,
color clr = clrGreen,
ENUM_LINE_STYLE style = STYLE_DASH,
uint width = 1,
color thresholdColor = clrAqua //
)
{
//
bool result = false;
//
datetime time2 = TimeCurrent();
//
string name = GetSupportTag(bar);
//
result = DrawTrendLine(
chartID,
name,
0,
bar.time,
bar.high,
time2,
bar.high,
clr,
style,
width,
false,
false,
ray //
);
//
if (result && _threshold > 0)
{
//
double threshold = GetPoints(bar.symbol) * _threshold;
//
string upThresholdName = name + "UP";
string downThresholdName = name + "DOWN";
//
result = DrawTrendLine(
chartID,
upThresholdName,
0,
bar.time,
bar.high + threshold,
time2,
bar.high + threshold,
thresholdColor,
style,
width,
false,
false,
ray //
);
//
if (result)
{
//
result = DrawTrendLine(
chartID,
downThresholdName,
0,
bar.time,
bar.high - threshold,
time2,
bar.high - threshold,
thresholdColor,
style,
width,
false,
false,
ray //
);
}
}
//
return result;
}
//
void RemoveSupport(XOHCL &bar)
{
RemoveDraws(GetSupportTag(bar));
}
//
// Support ...
//
string GetResistanceTag(XOHCL &bar)
{
return "Resistance-" + GetBarTimeTag(bar);
}
//
bool DrawResistance(
XOHCL &bar,
long chartID,
double _threshold = 0,
bool ray = true,
color clr = clrRed,
ENUM_LINE_STYLE style = STYLE_DASH,
uint width = 1,
color thresholdColor = clrMagenta //
)
{
//
bool result = false;
//
datetime time2 = TimeCurrent();
//
string name = GetResistanceTag(bar);
//
result = DrawTrendLine(
chartID,
name,
0,
bar.time,
bar.low,
time2,
bar.low,
clr,
style,
width,
false,
false,
ray //
);
//
if (result && _threshold > 0)
{
//
double threshold = GetPoints(bar.symbol) * _threshold;
//
string upThresholdName = name + "UP";
string downThresholdName = name + "DOWN";
//
result = DrawTrendLine(
chartID,
upThresholdName,
0,
bar.time,
bar.low + threshold,
time2,
bar.low + threshold,
thresholdColor,
style,
width,
false,
false,
ray //
);
//
if (result)
{
//
result = DrawTrendLine(
chartID,
downThresholdName,
0,
bar.time,
bar.low - threshold,
time2,
bar.low - threshold,
thresholdColor,
style,
width,
false,
false,
ray //
);
}
}
//
return result;
}
//
void RemoveResistance(XOHCL &bar)
{
RemoveDraws(GetResistanceTag(bar));
}
//
// Bullish Trend ...
//
string GetBullishTrendTag(XOHCL &bar)
{
return "BullishTrend-" + GetBarTimeTag(bar);
}
//
bool DrawBullishTrend(
XOHCL &bar,
XOHCL &highSwingBars[],
XOHCL &lowSwingBars[],
long chartID,
bool ray = true,
bool drawSwings = true,
color clr = clrAqua,
ENUM_LINE_STYLE style = STYLE_DOT,
uint width = 1,
uchar swingHighArrow = 202,
color swingHighColor = clrAqua,
uchar swingLowArrow = 202,
color swingLowColor = clrMagenta //
)
{
//
bool result = false;
//
string name = GetBullishTrendTag(bar);
//
XOHCL first;
XOHCL last;
//
// Draw Upper Trend Line ...
first = highSwingBars[ArraySize(highSwingBars) - 1];
last = highSwingBars[0];
//
result = DrawTrendLine(
chartID,
name + "-Upper",
0,
first.time,
first.high,
last.time,
last.high,
clr,
style,
width,
false,
false,
ray //
);
if (!result) {
return result;
}
//
// Draw Lower Trend Line ...
first = lowSwingBars[ArraySize(lowSwingBars) - 1];
last = lowSwingBars[0];
//
result = DrawTrendLine(
chartID,
name + "-Lower",
0,
first.time,
first.high,
last.time,
last.high,
clr,
style,
width,
false,
false,
ray //
);
if (!result) {
return result;
}
//
if (result && drawSwings)
{
//
// High Swings ...
for (int i = 0; i < ArraySize(highSwingBars); i++)
{
//
XOHCL iBar = highSwingBars[i];
//
result = DrawArrow(
chartID,
name + "-SwingHigh-" + ToString(i),
0,
iBar.time,
iBar.high,
swingHighArrow,
ANCHOR_BOTTOM,
swingHighColor,
STYLE_SOLID,
width //
);
if (!result)
{
break;
}
}
//
// Low Swings ...
for (int i = 0; i < ArraySize(lowSwingBars); i++)
{
//
XOHCL iBar = lowSwingBars[i];
//
result = DrawArrow(
chartID,
name + "-SwingLow-" + ToString(i),
0,
iBar.time,
iBar.low,
swingLowArrow,
ANCHOR_TOP,
swingLowColor,
STYLE_SOLID,
width //
);
if (!result)
{
break;
}
}
}
//
return result;
}
//
void RemoveBullishTrend(XOHCL &bar)
{
RemoveDraws(GetBullishTrendTag(bar));
}
//
// Bearish Trend ...
//
string GetBearishTrendTag(XOHCL &bar)
{
return "BearishTrend-" + GetBarTimeTag(bar);
}
//
bool DrawBearishTrend(
XOHCL &bar,
XOHCL &highSwingBars[],
XOHCL &lowSwingBars[],
long chartID,
bool ray = true,
bool drawSwings = true,
color clr = clrMagenta,
ENUM_LINE_STYLE style = STYLE_DOT,
uint width = 1,
uchar swingHighArrow = 202,
color swingHighColor = clrMagenta,
uchar swingLowArrow = 202,
color swingLowColor = clrAqua //
)
{
//
bool result = false;
//
string name = GetBearishTrendTag(bar);
//
XOHCL first;
XOHCL last;
//
// Draw Upper Trend Line ...
first = highSwingBars[ArraySize(highSwingBars) - 1];
last = highSwingBars[0];
//
result = DrawTrendLine(
chartID,
name + "-Upper",
0,
first.time,
first.high,
last.time,
last.high,
clr,
style,
width,
false,
false,
ray //
);
if (!result) {
return result;
}
//
// Draw Lower Trend Line ...
first = lowSwingBars[ArraySize(lowSwingBars) - 1];
last = lowSwingBars[0];
//
result = DrawTrendLine(
chartID,
name + "-Lower",
0,
first.time,
first.high,
last.time,
last.high,
clr,
style,
width,
false,
false,
ray //
);
if (!result) {
return result;
}
//
if (result && drawSwings)
{
//
// High Swings ...
for (int i = 0; i < ArraySize(highSwingBars); i++)
{
//
XOHCL iBar = highSwingBars[i];
//
result = DrawArrow(
chartID,
name + "-SwingHigh-" + ToString(i),
0,
iBar.time,
iBar.high,
swingHighArrow,
ANCHOR_BOTTOM,
swingHighColor,
STYLE_SOLID,
width //
);
if (!result)
{
break;
}
}
//
// Low Swings ...
for (int i = 0; i < ArraySize(lowSwingBars); i++)
{
//
XOHCL iBar = lowSwingBars[i];
//
result = DrawArrow(
chartID,
name + "-SwingLow-" + ToString(i),
0,
iBar.time,
iBar.low,
swingLowArrow,
ANCHOR_TOP,
swingLowColor,
STYLE_SOLID,
width //
);
if (!result)
{
break;
}
}
}
//
return result;
}
//
void RemoveBearishTrend(XOHCL &bar)
{
RemoveDraws(GetBearishTrendTag(bar));
}
//
//
//
//
string GetBarTimeTag(XOHCL &bar)
{
//
string result = NULL;
//
result =
//
ToString((int)bar.time)
//
;
//
return result;
}
//
string GeShootingStarTag(XOHCL &bar)
{
return "ShootingStar-" + GetBarTimeTag(bar);
}
//
// string GeShootingStarTag(XOHCL &bar)
// {
// return "ShootingStar-" + GetBarTimeTag(bar);
// }
//