Files
MQL5Data/Classes/x-saherelm.x-poi.drawer.class.mq5
T
2025-09-21 10:35:47 +03:30

1150 lines
23 KiB
Plaintext
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
///////////////////////////////////////////////////////
//
// SaherElm IT Center MQL5 Class Library
// --------------------------------------
// Name: XPOIDrawer
// Description: Provides Point of Interests
// Drawing 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 "../Classes/x-saherelm.base.class.mq5"
#include "../Classes/x-saherelm.x-cobject.class.mq5"
#include "../Libraries/x-saherelm.x-poi.extensions.lib.mq5"
//
// Definitions ...
//
// Extensions ...
void AddObjects(
CArrayObj *&source,
CArrayObj &dest //
)
{
//
bool has = source != NULL &&
source.Total() > 0;
if (!has)
{
return;
}
//
for (int i = 0; i < source.Total(); i++)
{
dest.Add(source.At(i));
}
}
//
// Implementations ...
class XCPOIDrawer : public XCBase
{
//
public:
//
// Props ...
//
XPOIStyle defaultBullishStyle;
XPOIStyle defaultBearishStyle;
//
XPOIStyle boxBullishStyle;
XPOIStyle boxBearishStyle;
//
// Constructor ...
void XCPOIDrawer()
{
//
Default();
}
//
// Deconstructor ...
void ~XCPOIDrawer()
{
//
boxBullishStyle.Clean();
boxBearishStyle.Clean();
defaultBullishStyle.Clean();
defaultBearishStyle.Clean();
}
//
// Setter Getter (s) ...
/**
* Get Prefix ...
*
* @return ( string )
*/
string Prefix()
{
return mPrefix;
}
/**
* Set Prifx ...
*
* @param value: String ...
*/
void Prefix(string value)
{
mPrefix = value;
}
/**
* Get Chart ID ...
*
* @return ( long )
*/
long ChartIdentification()
{
return mChartIdentification;
}
/**
* Set Chart ID ...
*
* @param value: Long ...
*/
void ChartIdentification(long value)
{
//
if (value < 0)
{
value = 0;
}
//
mChartIdentification = value;
}
/**
* Get SubWindow Id ...
*
* @return ( int )
*/
int SubWindowIdentification()
{
return mSubWindowIdentification;
}
/**
* Set SubWindow Id ...
*
* @param value: Integer ...
*/
void SubWindowIdentification(int value)
{
//
if (value < 0)
{
value = 0;
}
//
mSubWindowIdentification = value;
}
//
// XOHCL ...
/**
* Get Bar Width ...
*
* @return ( int )
*/
int BarWidth()
{
return mBarWidth;
}
/**
* Set Bar Width ...
*
* @param value: Integer ...
*/
void BarWidth(int value)
{
//
if (value < 0)
{
value = 0;
}
//
mBarWidth = value;
}
/**
* Get Bar Bullish Color ...
*
* @return ( color )
*/
color BarBullishColor()
{
return mBarBullishColor;
}
/**
* Set Bar Bullish Color ...
*
* @param value: Color ...
*/
void BarBullishColor(color value)
{
mBarBullishColor = value;
}
/**
* Get Bar Bearish Color ...
*
* @return ( color )
*/
color BarBearishColor()
{
return mBarBearishColor;
}
/**
* Set Bar Bearish Color ...
*
* @param value: Color ...
*/
void BarBearishColor(color value)
{
mBarBearishColor = value;
}
/**
* Get Fill Bar High Shadow State ...
*
* @return ( bool )
*/
bool BarFillHighShadow()
{
return mBarFillHighShadow;
}
/**
* Set Fill Bar High Shadow State ...
*
* @param value: Boolean ...
*/
void BarFillHighShadow(bool value)
{
mBarFillHighShadow = value;
}
/**
* Get Bar High Shadow Style ...
*
* @return ( ENUM_LINE_STYLE )
*/
ENUM_LINE_STYLE BarHighShadowStyle()
{
return mBarHighShadowStyle;
}
/**
* Set Bar High Shadow Style ...
*
* @param value: ENUM_LINE_STYLE member ...
*/
void BarHighShadowStyle(ENUM_LINE_STYLE value)
{
mBarHighShadowStyle = value;
}
/**
* Get Fill Bar Body State ...
*
* @return ( bool )
*/
bool BarFillBody()
{
return mBarFillBody;
}
/**
* Set Fill Bar Body State ...
*
* @param value: Boolean ...
*/
void BarFillBody(bool value)
{
mBarFillBody = value;
}
/**
* Get Bar Body Style ...
*
* @return ( ENUM_LINE_STYLE )
*/
ENUM_LINE_STYLE BarBodyStyle()
{
return mBarBodyStyle;
}
/**
* Set Bar Body Style ...
*
* @param value: ENUM_LINE_STYLE member ...
*/
void BarBodyStyle(ENUM_LINE_STYLE value)
{
mBarBodyStyle = value;
}
/**
* Get Fill Bar Low Show State ...
*
* @return ( bool )
*/
bool BarFillLowShadow()
{
return mBarFillLowShadow;
}
/**
* Set Fill Bar Low Show State ...
*
* @param value: Boolean ...
*/
void BarFillLowShadow(bool value)
{
mBarFillLowShadow = value;
}
/**
* Get Bar Low Shadow Style ...
*
* @return ( ENUM_LINE_STYLE )
*/
ENUM_LINE_STYLE BarLowShadowStyle()
{
return mBarLowShadowStyle;
}
/**
* Set Bar Low Shadow Style ...
*
* @param value: ENUM_LINE_STYLE member ...
*/
void BarLowShadowStyle(ENUM_LINE_STYLE value)
{
mBarLowShadowStyle = value;
}
//
// Virtuals ...
/**
* Apply Default Configurations ...
*/
virtual void Default()
{
//
// Apply Default Props ...
//
Prefix(NULL);
ChartIdentification(0);
SubWindowIdentification(0);
//
BarWidth(1);
BarBearishColor(clrRed);
BarBullishColor(clrLime);
BarFillBody(false);
BarFillHighShadow(false);
BarFillLowShadow(false);
BarBodyStyle(STYLE_DOT);
BarLowShadowStyle(STYLE_DOT);
BarHighShadowStyle(STYLE_DOT);
//
defaultBullishStyle.width = 1;
defaultBullishStyle.clr = clrLime;
defaultBullishStyle.style = STYLE_SOLID;
//
defaultBearishStyle.width = 1;
defaultBearishStyle.clr = clrRed;
defaultBearishStyle.style = STYLE_SOLID;
//
boxBullishStyle = defaultBullishStyle;
boxBearishStyle = defaultBearishStyle;
}
//
// Object Creators ...
/**
* Create Bar Object ...
*
* @param bar: XOHCL instance Reference ...
* @param object: XCOHCLObject pointer Reference ..
* @param to: Datetime ...
*
* @return ( bool )
*/
bool CreateBar(
XOHCL &bar,
XCOHCLObject *&object,
datetime to = NULL //
)
{
//
bool result = false;
//
object = NULL;
//
result = bar.IsValid();
if (!result)
{
return result;
}
//
to = NormalizeTime(to);
//
string prefix = Prefix();
long chartID = ChartIdentification();
int window = SubWindowIdentification();
//
int width = BarWidth();
//
color mBullishColor = BarBullishColor();
color mBearishColor = BarBearishColor();
//
bool fillBody = BarFillBody();
bool fillHighShadow = BarFillHighShadow();
bool fillLowShadow = BarFillLowShadow();
//
ENUM_LINE_STYLE bodyStyle = BarBodyStyle();
ENUM_LINE_STYLE highShadowStyle = BarHighShadowStyle();
ENUM_LINE_STYLE lowShadowStyle = BarLowShadowStyle();
//
object = new XCOHCLObject();
result = object.Create(
chartID,
window,
bar,
to,
prefix //
);
if (result)
{
//
// Apply Styling ...
//
object.BodyWidth(width);
object.LowShadowWidth(width);
object.HighShadowWidth(width);
//
object.BodyStyle(bodyStyle);
object.LowShadowStyle(lowShadowStyle);
object.HighShadowStyle(highShadowStyle);
//
bool isBullish = bar.IsBullish();
if (isBullish)
{
//
object.BodyColor(mBullishColor);
object.LowShadowColor(mBullishColor);
object.HighShadowColor(mBullishColor);
}
else
{
//
object.BodyColor(mBearishColor);
object.LowShadowColor(mBearishColor);
object.HighShadowColor(mBearishColor);
}
//
object.BodyFill(fillBody);
object.HighShadowFill(fillHighShadow);
object.LowShadowFill(fillLowShadow);
}
//
return result;
}
/**
* Create Bar Arrow Object ...
*
* @param bar: XOHCL instance Reference ...
* @param priceType: ENUM_X_PRICE member ...
* @param arrow: int, Arrow Code ...
* @param clr: color, Arro Color ...
* @param width: int, Arrow Width ...
* @param anchor: ENUM_ARROW_ANCHOR, anchor of Arrow ...
* @param name: string, name of Arrow ...
*
* @return ( bool )
*/
bool CreateBarArrow(
XOHCL &bar,
XCBarArrowObject *&object,
ENUM_X_PRICE priceType,
int arrow = 159,
color clr = clrYellow,
int width = 3,
ENUM_ARROW_ANCHOR anchor = ANCHOR_BOTTOM,
string name = NULL //
)
{
//
bool result = false;
//
object = NULL;
//
result = bar.IsValid();
if (!result)
{
return result;
}
//
string barTag = bar.GetTag(name);
//
string prefix = Prefix();
long chartID = ChartIdentification();
int window = SubWindowIdentification();
//
color mBullishColor = BarBullishColor();
color mBearishColor = BarBearishColor();
//
prefix =
(IsValid(prefix)
? prefix + "_"
: prefix) +
barTag;
//
object = new XCBarArrowObject();
result = object
.CreateByBar(
chartID,
prefix,
window,
arrow,
bar,
priceType //
);
if (result)
{
//
object.ArrowColor(clr);
object.ArrowWidth(width);
}
//
return result;
}
/**
* Create Specific Box on Chart ...
*
* @param upper: Double ...
* @param lower: Double ...
* @param from: Datetime ...
* @param to: Datetime ...
* @param clr: Color ...
* @param width: Integer ...
* @param fill: Boolean ...
* @param style: ENUM_LINE_STYLE member ...
*
* @return ( bool )
*/
bool CreateBox(
XCBoxObject *&object,
string name,
double upper,
double lower,
datetime from,
datetime to,
datetime at,
color clr = clrYellowGreen,
int width = 1,
bool fill = false,
ENUM_LINE_STYLE style = STYLE_SOLID //
)
{
//
bool result = false;
//
object = NULL;
//
to = NormalizeTime(to);
//
string dateStr = ToFormatString(from) + "|" + ToFormatString(to);
string dateMD5 = ToMD5(dateStr);
if (!IsValid(name))
{
//
name = "XBox_" +
ToXString(upper) + "_" +
ToXString(lower) + "_" +
dateMD5;
}
//
result =
upper > 0 &&
lower > 0 &&
to > from &&
IsValid(to) &&
IsValid(name) &&
IsValid(from) &&
upper > lower;
if (!result)
{
return result;
}
//
string prefix = Prefix();
long chartID = ChartIdentification();
int window = SubWindowIdentification();
//
object = new XCBoxObject();
result = object.Create(
chartID,
window,
name,
upper,
lower,
from,
to,
at,
prefix //
);
//
if (!result)
{
return result;
}
//
object.BoxColor(clr);
object.BoxFill(fill);
object.BoxWidth(width);
object.BoxStyle(style);
//
return result;
}
/**
* Create Specific Box on Chart ...
*
* @param box: XBoxZone instance Reference ...
* @param clr: Color ...
* @param width: Integer ...
* @param fill: Boolean ...
* @param style: ENUM_LINE_STYLE member ...
*
* @return ( bool )
*/
bool DrawBox(
XBoxZone &box,
XCBoxObject *&object,
bool ignoreAt = true //
)
{
//
bool result = false;
//
object = NULL;
//
result = box.IsValid();
if (!result)
{
return result;
}
//
if (ignoreAt)
{
box.at = NULL;
}
//
int width = 1;
bool fill = false;
color clr =
box.IsBullish()
? defaultBullishStyle.clr
: defaultBearishStyle.clr;
ENUM_LINE_STYLE style = STYLE_DASHDOTDOT;
string name = box.GetTag();
//
result = CreateBox(
object,
name,
box.upper,
box.lower,
box.from,
box.to,
box.at,
clr,
width,
fill,
style //
);
//
if (result)
{
//
if (box.IsBullish())
{
//
ApplyBoxStyle(
object,
boxBullishStyle //
);
}
else
{
//
ApplyBoxStyle(
object,
boxBearishStyle //
);
}
}
//
return result;
}
/**
* Create Specific Box on Chart ...
*
* @param box: XBoxZone instance Reference ...
* @param clr: Color ...
* @param width: Integer ...
* @param fill: Boolean ...
* @param style: ENUM_LINE_STYLE member ...
*
* @return ( bool )
*/
bool CreateBox(
XBoxZone &box,
XCBoxObject *&object,
color clr = clrYellowGreen,
int width = 1,
bool fill = false,
ENUM_LINE_STYLE style = STYLE_SOLID //
)
{
//
bool result = false;
//
object = NULL;
//
result = box.IsValid();
if (!result)
{
return result;
}
//
string name = box.GetTag();
//
result = CreateBox(
object,
name,
box.upper,
box.lower,
box.from,
box.to,
box.at,
clr,
width,
fill,
style //
);
//
return result;
}
/**
* Draw Specified Signal ...
*
* @param signal: XSignal instance ...
*/
bool DrawSignal(
XSignal &signal,
XCSignalObject *&object,
int length = 3 //
)
{
//
bool result = false;
//
object = NULL;
length = NormalizeInt(length, 3);
//
result = signal.IsValid();
if (!result)
{
return result;
}
//
string prefix = Prefix();
long chartID = ChartIdentification();
int window = SubWindowIdentification();
//
object = new XCSignalObject();
result = object.Create(
chartID,
window,
signal,
length,
prefix //
);
if (!result)
{
return result;
}
//
object.TPWidth(1);
object.TPColor(clrGreen);
object.TPWidth(STYLE_DOT);
//
object.SLWidth(1);
object.SLColor(clrRed);
object.SLWidth(STYLE_DOT);
//
object.TargetWidth(1);
object.TargetColor(clrLightBlue);
object.TargetWidth(STYLE_DOT);
//
object.EntryWidth(1);
object.EntryColor(clrYellow);
object.EntryWidth(STYLE_DOT);
//
return result;
}
/**
* Draw Specified Trend Line ...
*
* @param fromPrice: double, from Price ...
* @param fromTime: datetime, from Time ...
* @param toPrice: double, to Price ...
* @param toTime: datetime, to Time ...
* @param name: Object Name ...
* @param rayRight: bool, ray to Right ...
* @param rayLeft: boo, ray to Left ...
*
* @return ( bool )
*/
bool DrawTrendLine(
double fromPrice,
datetime fromTime,
double toPrice,
datetime toTime,
CChartObjectTrend *&object,
string name,
bool rayRight = false,
bool rayLeft = false //
)
{
//
bool result = false;
//
// Prepare ...
object = new CChartObjectTrend();
//
// Validate ...
result =
toPrice > 0 &&
fromPrice > 0 &&
IsSpecifiedValid(toTime) &&
IsSpecifiedValid(fromTime) &&
toTime > fromTime;
if (!result)
{
return result;
}
//
// Create Object ...
result = object.Create(
ChartIdentification(),
name,
SubWindowIdentification(),
fromTime,
fromPrice,
toTime,
toPrice //
);
if (result) {
//
object.RayLeft(rayLeft);
object.RayRight(rayRight);
}
//
return result;
}
/**
* Draw Trend Line based on Swings Points ...
*
* @param swings: XOHCL, Specified Swings ...
* @param dir: ENUM_X_DIRECTION, Specified Trend Direction ...
* @param rayRight: Ray to Right ...
* @param rayLeft: Ray to Left ...
*
* @return ( bool )
*/
bool DrawSwingTrend(
XOHCL &swings[],
ENUM_X_DIRECTION dir,
CChartObjectTrend *&object,
bool rayRight = false,
bool rayLeft = false //
)
{
//
bool result = false;
//
int count = ArraySize(swings);
result = HasChild(swings) &&
count >= 2 &&
HasDirection(dir);
if (!result)
{
return result;
}
//
bool isBullish = IsSpecifiedBullish(dir);
//
double price1 =
isBullish
? swings[0].low
: swings[0].high;
datetime time1 = swings[0].time;
//
double price2 =
isBullish
? swings[count - 1].low
: swings[count - 1].high;
datetime time2 = swings[count - 1].time;
//
ulong chartId = ChartIdentification();
int subWindow = SubWindowIdentification();
string name = ToXString(dir) + "_Trend_" + ToXString(price1) + "_" + ToXString(price2);
//
object = new CChartObjectTrend();
result = object.Create(
chartId,
name,
subWindow,
time1,
price1,
time2,
price2 //
);
if (result)
{
//
ApplyStyle(
object,
dir //
);
//
if (rayRight)
{
object.RayRight(true);
}
//
if (rayLeft)
{
object.RayLeft(true);
}
}
//
return result;
}
/**
* Apply Specified Style to Object ...
*/
void ApplyBoxStyle(
XCBoxObject *&object,
XPOIStyle &style //
)
{
//
bool has = object != NULL;
if (!has)
{
return;
}
//
object.BoxColor(style.clr);
object.BoxWidth(style.width);
object.BoxStyle(style.style);
}
/**
* Apply Specified Style to Object ...
*/
void ApplyBarArrowStyle(
XCBarArrowObject *&object,
XPOIStyle &style //
)
{
//
bool has = object != NULL;
if (!has)
{
return;
}
//
object.ArrowColor(style.clr);
object.ArrowWidth(style.width);
}
/**
* Apply Style to Specified Chart Object ...
*
* @param dir: Argument 2
*/
void ApplyStyle(
CChartObject *object,
ENUM_X_DIRECTION dir //
)
{
//
if (!HasDirection(dir))
{
return;
}
//
bool isBullish = IsSpecifiedBullish(dir);
if (isBullish)
{
//
object.Color(boxBullishStyle.clr);
object.Style(boxBullishStyle.style);
object.Width(boxBullishStyle.width);
}
else
{
//
object.Color(boxBearishStyle.clr);
object.Style(boxBearishStyle.style);
object.Width(boxBearishStyle.width);
}
}
//
protected:
//
//
private:
//
//
// Props ...
//
string mPrefix; // Prefix of Created Objects ...
long mChartIdentification; // Chart Id ...
int mSubWindowIdentification; // Sub Window ...
//
// XOHCL ...
int mBarWidth; // Bar Width ...
color mBarBullishColor; // Bar Bullish Color ...
color mBarBearishColor; // Bar Bearish Color ...
bool mBarFillHighShadow; // Fill Bar High Shadow ...
ENUM_LINE_STYLE mBarHighShadowStyle; // Bar High Shadow Style ...
bool mBarFillBody; // Fill Bar Body ...
ENUM_LINE_STYLE mBarBodyStyle; // Bar Body Style ...
bool mBarFillLowShadow; // Fill Bar Low Show ...
ENUM_LINE_STYLE mBarLowShadowStyle; // Bar Low Shadow Style ...
//
};