Files
MQL5Data/Classes/x-saherelm.x-chart-objects.class.mq5
T

985 lines
17 KiB
Plaintext

///////////////////////////////////////////////////////
//
// SaherElm IT Center MQL5 Class Library
// --------------------------------------
// Name: XChartObjects
// Description: provides all require Chart Objects ...
//
//
// 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
//
#include "../Libraries/x-saherelm.x-poi.lib.mq5"
//
#include <ChartObjects/ChartObject.mqh>
#include <ChartObjects/ChartObjectsLines.mqh>
#include <ChartObjects/ChartObjectsShapes.mqh>
//
// Definitions ...
enum ENUM_XCHARTOBJECTS
{
//
// Global ...
X_ZONE,
X_FROM_TO,
//
// Structured ...
X_TRADE_DAY,
X_MARKET_SESSION,
X_SUPPORT_ZONE,
X_RESISTANCE_ZONE,
X_SUPPLY_ZONE,
X_DEMAND_ZONE,
X_BULLISH_ORDERBLOCK,
X_BEARISH_ORDERBLOCK,
};
enum ENUM_X_CHART_IDS
{
X_TO,
X_FROM,
X_UPPER,
X_LOWER,
};
//
// Extensions ...
/**
* Convert ENUM_X_TRADE_DAY_IDS membr to String ...
*
* @param value: ENUM_X_TRADE_DAY_IDS member ...
*
* @return ( string )
*/
string ToString(ENUM_X_CHART_IDS value)
{
//
string result = NULL;
//
switch (value)
{
//
case X_TO:
result = "XTO";
break;
//
case X_FROM:
result = "XFROM";
break;
//
case X_UPPER:
result = "XUPPER";
break;
//
case X_LOWER:
result = "XLOWER";
break;
}
//
return result;
}
//
// Implementation ...
//
// Global Chart Objects ...
//
// From To Chart Object ...
class XCFromToObject : public CChartObject
{
//
public:
//
//
// Constructors ...
void XCFromToObject()
{
}
//
// Deconstructors ...
void ~XCFromToObject()
{
}
/**
* Create XCFromToObject ...
*
* @param chart_id: Long ...
* @param name: String ...
* @param window: Integer ...
* @param time1: DateTime ...
* @param time2: DateTime ...
*
* @return ( bool )
*/
bool Create(
long chart_id,
const string name,
const int window,
const datetime from,
const datetime to,
const string objprefix = NULL //
)
{
//
bool result = false;
//
string mPrefix = (IsValid(objprefix) ? objprefix + "_" : "");
//
string mFromVLineName =
mPrefix + ToString(X_FROM) + "_" + name;
result = mFromVLine.Create(chart_id, mFromVLineName, window, from);
if (!result)
{
return result;
}
//
string mToVLineName =
mPrefix + ToString(X_TO) + "_" + name;
result = mToVLine.Create(chart_id, mToVLineName, window, to);
if (!result)
{
return result;
}
//
return result;
}
/**
* Retrieve Object Type ...
*
* @return ( ENUM_XCHARTOBJECTS )
*/
virtual ENUM_XCHARTOBJECTS Type()
{
return X_FROM_TO;
}
/**
* Set From Date Line Color ...
*
* @param value: Color ...
*
* @return ( bool )
*/
bool FromColor(color value)
{
return mFromVLine.Color(value);
}
/**
* Set From Date Line Style ...
*
* @param value: ENUM_LINE_STYLE ...
*
* @return ( bool )
*/
bool FromStyle(ENUM_LINE_STYLE value)
{
return mFromVLine.Style(value);
}
/**
* Set To Date Line Color ...
*
* @param value: Color ...
*
* @return ( bool )
*/
bool ToColor(color value)
{
return mToVLine.Color(value);
}
/**
* Set To Date Line Style ...
*
* @param value: ENUM_LINE_STYLE ...
*
* @return ( bool )
*/
bool ToStyle(ENUM_LINE_STYLE value)
{
return mToVLine.Style(value);
}
//
protected:
//
//
private:
//
CChartObjectVLine mFromVLine;
CChartObjectVLine mToVLine;
//
};
//
// Zone Chart Object ...
class XCZoneObject : public CChartObject
{
//
public:
//
//
// Constructors ...
void XCZoneObject()
{
}
//
// Deconstructors ...
void ~XCZoneObject()
{
}
/**
* Create XCZoneObject ...
*
* @param chart_id: Long ...
* @param name: String ...
* @param window: Integer ...
* @param time1: DateTime ...
* @param time2: DateTime ...
* @param upperPrice1: Double ...
* @param upperPrice2: Double ...
* @param lowerPrice1: Double ...
* @param lowerPrice2: Double ...
* @param objprefix: String ...
* @param forceRect: Boolean ...
*
* @return ( bool )
*/
bool Create(
long chart_id,
const string name,
const int window,
const datetime time1,
const datetime time2,
const double upperPrice1,
const double upperPrice2,
const double lowerPrice1,
const double lowerPrice2,
const string objprefix = NULL,
const bool forceRect = false //
)
{
//
bool result = false;
//
// Validate Args ...
result = IsValid(name) && IsValid(time1);
if (!result)
{
return result;
}
//
NormalizeTime(time2);
//
result = time1 < time2;
if (!result)
{
return result;
}
//
string prefix =
!IsValid(objprefix)
? ""
: objprefix + "_";
//
// Check Params For Rectangle ...
bool canDrawRect =
forceRect &&
upperPrice1 == upperPrice2 &&
lowerPrice1 == lowerPrice1;
//
// Draw Rectangle ...
if (canDrawRect)
{
//
result = mRect.Create(
chart_id,
name,
window,
time1,
upperPrice1,
time2,
lowerPrice1 //
);
}
//
// Draw as Trend Line ...
else
{
//
// Upper Bound ...
//
string upperName = prefix + ToString(X_UPPER) + "_" + name;
//
result = mUpperTrend.Create(
chart_id,
upperName,
window,
time1,
upperPrice1,
time2,
upperPrice2 //
);
if (!result)
{
return result;
}
//
// Lower Bound ...
//
string lowerName = prefix + ToString(X_LOWER) + "_" + name;
//
result = mLowerTrend.Create(
chart_id,
lowerName,
window,
time1,
lowerPrice1,
time2,
lowerPrice2 //
);
if (!result)
{
//
mUpperTrend.Delete();
return result;
}
}
//
return result;
}
/**
* Create XCZoneObject by Providing a XZone ...
*
* @param chart_id: Long ...
* @param name: String ...
* @param window: Integer ...
* @param time1: DateTime ...
* @param time2: DateTime ...
* @param zone: XZone instance ...
* @param objprefix: String ...
* @param forceRect: Boolean ...
*
* @return ( bool )
*/
bool CreateByZone(
long chart_id,
const string name,
const int window,
const XZone &zone,
const string objprefix = NULL,
const bool forceRect = false //
)
{
//
bool result = false;
//
XZone tmpZone = zone;
result =
IsValid(name) &&
tmpZone.IsValid();
if (!result)
{
return result;
}
//
result = Create(
chart_id,
name,
window,
zone.from,
zone.to,
zone.upper,
zone.upper,
zone.lower,
zone.lower,
objprefix,
forceRect //
);
//
return result;
}
/**
* Retrieve Object Type ...
*
* @return ( ENUM_XCHARTOBJECTS )
*/
virtual ENUM_XCHARTOBJECTS Type()
{
return X_ZONE;
}
/**
* Set Zone Line Width ...
*
* @param value: Integer ...
*/
void ZoneWidth(int value)
{
//
if (value < 1)
{
value = 1;
}
//
mRect.Width(value);
mUpperTrend.Width(value);
mLowerTrend.Width(value);
}
/**
* Set Zone Color ...
*
* @param value: Color ...
*/
void ZoneColor(color value)
{
//
mRect.Color(value);
mUpperTrend.Color(value);
mLowerTrend.Color(value);
}
/**
* Set Zone Line Style ...
*
* @param value: ENUM_LINE_STYLE member ...
*/
void ZoneStyle(ENUM_LINE_STYLE value)
{
//
mRect.Style(value);
mUpperTrend.Style(value);
mLowerTrend.Style(value);
}
//
protected:
//
//
private:
//
CChartObjectTrend mUpperTrend;
CChartObjectTrend mLowerTrend;
CChartObjectRectangle mRect;
//
};
//
// Structured Chart Object ...
//
// XTradeDay Object ...
class XCTardeDayObject : public XCFromToObject
{
//
public:
//
//
// Constructors ...
void XCTardeDayObject()
{
}
//
// Deconstructors ...
void ~XCTardeDayObject()
{
}
/**
* Create Object Based on Given Structure ...
*
* @param chart_id: Long ...
* @param name: String ...
* @param window: Integer ...
* @param day: XTradeDay instance ...
*
* @return ( bool )
*/
bool CreateByDay(
long chart_id,
const string name,
const int window,
XTradeDay &day //
)
{
//
bool result = false;
//
result = Create(
chart_id,
name,
window,
day.from,
day.to,
"XTDAY" //
);
//
return result;
}
/**
* Retrieve Object Type ...
*
* @return ( ENUM_XCHARTOBJECTS )
*/
virtual ENUM_XCHARTOBJECTS Type()
{
return X_TRADE_DAY;
}
//
protected:
//
//
private:
//
//
};
//
// XMarketSession Object ...
class XCMarketSessionObject : public XCFromToObject
{
//
public:
//
//
// Constructors ...
void XCMarketSessionObject()
{
}
//
// Deconstructors ...
void ~XCMarketSessionObject()
{
}
/**
* Create Object Based on Given Structure ...
*
* @param chart_id: Long ...
* @param name: String ...
* @param window: Integer ...
* @param session: XMarketSession instance ...
*
* @return ( bool )
*/
bool CreateByMarketSession(
long chart_id,
const string name,
const int window,
XMarketSession &session //
)
{
//
bool result = false;
//
string sessionLabel = ToString(session.session);
result = Create(
chart_id,
name,
window,
session.from,
session.to,
sessionLabel //
);
//
return result;
}
/**
* Retrieve Object Type ...
*
* @return ( ENUM_XCHARTOBJECTS )
*/
virtual ENUM_XCHARTOBJECTS Type()
{
return X_MARKET_SESSION;
}
//
protected:
//
//
private:
//
//
};
//
// Support Zone ...
class XCSupportZoneObject : public XCZoneObject
{
//
public:
//
//
// Constructors ...
void XCSupportZoneObject()
{
}
//
// Deconstructors ...
void ~XCSupportZoneObject()
{
}
/**
* Draw Support Zone Object by Providing Support Zone ...
*
* @param chart_id: Long ...
* @param name: String ...
* @param window: Integer ...
* @param support: XOHCL instance ...
* @param time2: DateTime ...
* @param forceRect: Boolean ...
*
* @return ( bool )
*/
bool CreateBySupport(
long chart_id,
const string name,
const int window,
const XOHCL &support,
const datetime time2,
const bool forceRect = false //
)
{
//
bool result = false;
//
XOHCL tmpSupport = support;
result = tmpSupport.IsValid();
if (!result)
{
return result;
}
//
double bottom = tmpSupport.GetDown();
//
result = Create(
chart_id,
name,
window,
support.time,
time2,
bottom,
bottom,
support.low,
support.low,
NULL,
forceRect //
);
//
return result;
}
/**
* Retrieve Object Type ...
*
* @return ( ENUM_XCHARTOBJECTS )
*/
virtual ENUM_XCHARTOBJECTS Type()
{
return X_SUPPORT_ZONE;
}
//
};
//
// Resistance Zone ...
class XCResistanceZoneObject : public XCZoneObject
{
//
public:
//
//
// Constructors ...
void XCResistanceZoneObject()
{
}
//
// Deconstructors ...
void ~XCResistanceZoneObject()
{
}
/**
* Draw Resistance Zone Object by Providing Resistance Zone ...
*
* @param chart_id: Long ...
* @param name: String ...
* @param window: Integer ...
* @param resistance: XOHCL instance ...
* @param time2: DateTime ...
* @param forceRect: Boolean ...
*
* @return ( bool )
*/
bool CreateByResistance(
long chart_id,
const string name,
const int window,
const XOHCL &resistance,
const datetime time2,
const bool forceRect = false //
)
{
//
bool result = false;
//
XOHCL tmpResistance = resistance;
result = tmpResistance.IsValid();
if (!result)
{
return result;
}
//
double up = tmpResistance.GetUp();
//
result = Create(
chart_id,
name,
window,
resistance.time,
time2,
resistance.high,
resistance.high,
up,
up,
NULL,
forceRect //
);
//
return result;
}
/**
* Retrieve Object Type ...
*
* @return ( ENUM_XCHARTOBJECTS )
*/
virtual ENUM_XCHARTOBJECTS Type()
{
return X_RESISTANCE_ZONE;
}
//
};
//
// Supply Zone ...
class XCSupplyZoneObject : public XCZoneObject
{
//
public:
//
//
// Constructors ...
void XCSupplyZoneObject()
{
}
//
// Deconstructors ...
void ~XCSupplyZoneObject()
{
}
/**
* Retrieve Object Type ...
*
* @return ( ENUM_XCHARTOBJECTS )
*/
virtual ENUM_XCHARTOBJECTS Type()
{
return X_SUPPLY_ZONE;
}
//
};
//
// Demand Zone ...
class XCDemandZoneObject : public XCZoneObject
{
//
public:
//
//
// Constructors ...
void XCDemandZoneObject()
{
}
//
// Deconstructors ...
void ~XCDemandZoneObject()
{
}
/**
* Retrieve Object Type ...
*
* @return ( ENUM_XCHARTOBJECTS )
*/
virtual ENUM_XCHARTOBJECTS Type()
{
return X_DEMAND_ZONE;
}
//
};
//
// Bullish Order Block ...
class XCBullishOrderBlockObject : public XCZoneObject
{
//
public:
//
//
// Constructors ...
void XCBullishOrderBlockObject()
{
}
//
// Deconstructors ...
void ~XCBullishOrderBlockObject()
{
}
/**
* Retrieve Object Type ...
*
* @return ( ENUM_XCHARTOBJECTS )
*/
virtual ENUM_XCHARTOBJECTS Type()
{
return X_BULLISH_ORDERBLOCK;
}
//
};
//
// Bearish Order Block ...
class XCBearishOrderBlockObject : public XCZoneObject
{
//
public:
//
//
// Constructors ...
void XCBearishOrderBlockObject()
{
}
//
// Deconstructors ...
void ~XCBearishOrderBlockObject()
{
}
/**
* Retrieve Object Type ...
*
* @return ( ENUM_XCHARTOBJECTS )
*/
virtual ENUM_XCHARTOBJECTS Type()
{
return X_BEARISH_ORDERBLOCK;
}
//
};
//