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

3609 lines
87 KiB
Plaintext

///////////////////////////////////////////////////////
//
// SaherElm IT Center MQL5 Draw Library
// --------------------------------------
// Name: XDraw
// Description: provides Draw abilities ...
//
//
// 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
//
// START Inputs ...
//
//
// END Inputs ...
//
//
// Include Logger Library ...
#include "x-saherelm.log.lib.mq5"
#include "x-saherelm.common.lib.mq5"
#include "../Classes/x-saherelm.xmd5.class.mq5"
//
// START Global Definitions: Variables, Properties and etc ...
//
//
string drawPrefix = "X-OBJ";
//
// END Global Definitions: Variables, Properties and etc ...
//
//
// START Provided Functions ...
//
//
// DeInitial Library if required ...
void OnDeinitDrawLibrary()
{
RemoveDraws();
}
//
// remove drawn objects ...
void RemoveDraws(
string tag = NULL,
long chartId = -1,
int subWindow = -1)
{
//
// Normalize Args ...
//
if (tag == NULL)
{
tag = drawPrefix;
}
//
if (chartId == -1)
{
chartId = 0;
}
//
if (subWindow == -1)
{
subWindow = 0;
}
//
for (int iObj = ObjectsTotal(chartId, subWindow) - 1; iObj >= 0; iObj--)
{
//
string on = ObjectName(chartId, iObj, subWindow);
if (StringFind(on, tag) >= 0)
{
ObjectDelete(chartId, on);
}
}
//
// Redraw Chart ...
ChartRedraw();
}
//
bool RemoveDraw(
string name,
long chartId = -1,
int subWindow = -1)
{
//
bool result = false;
//
if (chartId == -1)
{
chartId = 0;
}
//
if (subWindow == -1)
{
subWindow = 0;
}
//
string objName = "";
for (int iObj = ObjectsTotal(chartId, subWindow) - 1; iObj >= 0; iObj--)
{
//
string on = ObjectName(chartId, iObj, subWindow);
if (StringFind(on, name) >= 0)
{
//
objName = on;
break;
}
}
//
if (StringLen(objName) > 0)
{
result = ObjectDelete(chartId, objName);
}
//
return result;
}
//
// Deselect All Draws ...
void DeselectAllDraws()
{
//
int objs = ObjectsTotal(ChartID(), 0);
for (int o = 0; o < objs; o++)
{
ObjectSetInteger(ChartID(), ObjectName(ChartID(), o, 0), OBJPROP_SELECTED, false);
}
//
ChartRedraw();
}
//
// Check Object Exists ...
bool IsDrawExists(
string name,
long chartId = -1,
int subWindow = -1)
{
//
bool result = false;
//
if (chartId == -1)
{
chartId = 0;
}
//
if (subWindow == -1)
{
subWindow = 0;
}
//
for (int iObj = ObjectsTotal(chartId, subWindow) - 1; iObj >= 0; iObj--)
{
//
string on = ObjectName(chartId, iObj, subWindow);
result = StringFind(on, name) >= 0;
if (result)
{
break;
}
}
//
return result;
}
//
// END Provided Functions ...
//
//
// START Regular Draws ...
//
//
// START Arrow Functions ...
//
//
// Draw an Arrow Object on Chart ...
bool DrawArrow(
const long chartID = 0, // chart's ID
const string name = "Arrow", // arrow name
const int subWindow = 0, // subwindow index
datetime time = 0, // anchor point time
double price = 0, // anchor point price
const uchar arrowCode = 252, // arrow code
const ENUM_ARROW_ANCHOR anchor = ANCHOR_BOTTOM, // anchor point position
const color clr = clrAqua, // arrow color
const ENUM_LINE_STYLE style = STYLE_SOLID, // border line style
const int width = 3, // arrow size
const bool back = false, // in the background
const bool selection = false, // highlight to move
const bool hidden = false, // hidden in the object list
const long zOrder = 0 // Order Object ...
)
{
//
// set anchor point coordinates if they are not set ...
ChangeArrowEmptyPoint(time, price);
//
// reset the error value ...
ResetLastError();
//
// Normalize Name ...
string objName = drawPrefix + "_" + name;
//
// create an arrow ...
if (!ObjectCreate(chartID, objName, OBJ_ARROW, subWindow, time, price))
{
//
string message =
__FUNCTION__ +
": failed to create an arrow! Error code = " +
(string)GetLastError();
//
LogMessage(message);
//
return false;
}
//
// set the arrow code ...
ObjectSetInteger(chartID, objName, OBJPROP_ARROWCODE, arrowCode);
//
// set anchor type ...
ObjectSetInteger(chartID, objName, OBJPROP_ANCHOR, anchor);
//
// set the arrow color ...
ObjectSetInteger(chartID, objName, OBJPROP_COLOR, clr);
//
// set the border line style ...
ObjectSetInteger(chartID, objName, OBJPROP_STYLE, style);
//
// set the arrow's size ...
ObjectSetInteger(chartID, objName, OBJPROP_WIDTH, width);
//
// display in the foreground false or background true ...
ObjectSetInteger(chartID, objName, OBJPROP_BACK, back);
//
// enable true or disable false the mode of moving the arrow by mouse
// when creating a graphical object using ObjectCreate function, the object cannot be
// highlighted and moved by default. Inside this method, selection parameter
// is false by default, if you want making it possible to highlight and move the object
// change it to true ...
ObjectSetInteger(chartID, objName, OBJPROP_SELECTABLE, true);
ObjectSetInteger(chartID, objName, OBJPROP_SELECTED, selection);
//
// hide true or display false graphical object name in the object list ...
ObjectSetInteger(chartID, objName, OBJPROP_HIDDEN, hidden);
//
// set the priority for receiving the event of a mouse click in the chart ...
ObjectSetInteger(chartID, objName, OBJPROP_ZORDER, zOrder);
//
// successful execution ...
return true;
}
//
// Move the anchor point of Arrow ...
bool MoveArrow(
const long chartID = 0, // chart's ID
const string name = "Arrow", // object name
datetime time = 0, // anchor point time coordinate
double price = 0 // anchor point price coordinate
)
{
//
// if point position is not set, move it to the current bar having Bid price ...
if (!time)
{
time = TimeCurrent();
}
//
if (!price)
{
price = SymbolInfoDouble(_Symbol, SYMBOL_BID);
}
//
// reset the error value
ResetLastError();
//
// move the anchor point ...
if (!ObjectMove(chartID, name, 0, time, price))
{
//
string message =
__FUNCTION__ +
": failed to move the anchor point! Error code = " +
(string)GetLastError();
//
LogMessage(message);
//
return false;
}
//
// successful execution ...
return true;
}
//
// Delete an arrow ...
bool DeleteArrow(
const long chartID = 0, // chart's ID
const string name = "Arrow" // arrow name
)
{
//
// reset the error value ...
ResetLastError();
//
// delete an arrow ...
if (!ObjectDelete(chartID, name))
{
//
string message =
__FUNCTION__ +
": failed to delete an arrow! Error code = " +
(string)GetLastError();
//
LogMessage(message);
//
return false;
}
//
// successful execution ...
return true;
}
//
// Change the arrow code ...
bool ArrowCodeChange(
const long chartID = 0, // chart's ID
const string name = "Arrow", // object name
const uchar code = 252 // arrow code
)
{
//
// reset the error value ...
ResetLastError();
//
// change the arrow code ...
if (!ObjectSetInteger(chartID, name, OBJPROP_ARROWCODE, code))
{
//
string message =
__FUNCTION__ +
": failed to change the arrow code! Error code = " +
(string)GetLastError();
//
LogMessage(message);
//
return false;
}
//
// successful execution ...
return true;
}
//
// Change anchor type ...
bool ArrowAnchorChange(
const long chartID = 0, // chart's ID
const string name = "Arrow", // object name
const ENUM_ARROW_ANCHOR anchor = ANCHOR_TOP // anchor type
)
{
//
// reset the error value ...
ResetLastError();
//
// change anchor type ...
if (!ObjectSetInteger(chartID, name, OBJPROP_ANCHOR, anchor))
{
//
string message =
__FUNCTION__ +
": failed to change anchor type! Error code = " +
(string)GetLastError();
//
LogMessage(message);
//
return false;
}
//
// successful execution ...
return true;
}
//
// Check anchor point values and set default values
// for empty ones ...
void ChangeArrowEmptyPoint(
datetime &time,
double &price)
{
//
// if the point's time is not set, it will be on the current bar ...
if (!time)
{
time = TimeCurrent();
}
//
// if the point's price is not set, it will have Bid value ...
if (!price)
{
price = SymbolInfoDouble(_Symbol, SYMBOL_BID);
}
}
//
// END Arrow Functions ...
//
//
// START Horizontal Line Functions ...
//
//
// Create, Draw the horizontal line ...
bool DrawHorizontalLine(
const long chartID = 0, // chart's ID
const string name = "HLine", // line name
const int subWindow = 0, // subwindow index
double price = 0, // line price
const color clr = clrRed, // line color
const ENUM_LINE_STYLE style = STYLE_SOLID, // line style
const int width = 1, // line width
const bool back = false, // in the background
const bool selection = false, // highlight to move
const bool hidden = false, // hidden in the object list
const long zOrder = 0 // priority for mouse click
)
{
//
// if the price is not set, set it at the current Bid price level ...
if (!price)
{
price = SymbolInfoDouble(Symbol(), SYMBOL_BID);
}
//
// reset the error value ...
ResetLastError();
//
// Normalize Name ...
string objName = drawPrefix + "_" + name;
//
// create a horizontal line ...
if (!ObjectCreate(chartID, objName, OBJ_HLINE, subWindow, 0, price))
{
//
string message =
__FUNCTION__ +
": failed to create a horizontal line! Error code = " +
(string)GetLastError();
//
LogMessage(message);
//
return false;
}
//
// set line color ...
ObjectSetInteger(chartID, objName, OBJPROP_COLOR, clr);
//
// set line display style ...
ObjectSetInteger(chartID, objName, OBJPROP_STYLE, style);
//
// set line width ...
ObjectSetInteger(chartID, objName, OBJPROP_WIDTH, width);
//
// display in the foreground false or background true ...
ObjectSetInteger(chartID, objName, OBJPROP_BACK, back);
//
// enable true or disable false the mode of moving the line by mouse
// when creating a graphical object using ObjectCreate function, the object cannot be
// highlighted and moved by default. Inside this method, selection parameter
// is true by default making it possible to highlight and move the object
ObjectSetInteger(chartID, objName, OBJPROP_SELECTABLE, selection);
ObjectSetInteger(chartID, objName, OBJPROP_SELECTED, selection);
//
// hide true or display false graphical object name in the object list ...
ObjectSetInteger(chartID, objName, OBJPROP_HIDDEN, hidden);
//
// set the priority for receiving the event of a mouse click in the chart ...
ObjectSetInteger(chartID, objName, OBJPROP_ZORDER, zOrder);
//
// successful execution ...
return true;
}
//
// Move horizontal line ...
bool MoveHorizontalLine(
const long chartID = 0, // chart's ID
const string name = "HLine", // line name
double price = 0 // line price
)
{
//
// if the line price is not set, move it to the current Bid price level ...
if (!price)
{
price = SymbolInfoDouble(Symbol(), SYMBOL_BID);
}
//
// reset the error value ...
ResetLastError();
//
// move a horizontal line ...
if (!ObjectMove(chartID, name, 0, 0, price))
{
//
string message =
__FUNCTION__ +
": failed to move the horizontal line! Error code = " +
(string)GetLastError();
//
LogMessage(message);
//
return false;
}
//
// successful execution ...
return true;
}
//
// Delete a horizontal line ...
bool DeleteHorizontalLine(
const long chartID = 0, // chart's ID
const string name = "HLine" // line name
)
{
//
// reset the error value ...
ResetLastError();
//
// delete a horizontal line ...
if (!ObjectDelete(chartID, name))
{
//
string message =
__FUNCTION__ +
": failed to delete a horizontal line! Error code = " +
(string)GetLastError();
//
LogMessage(message);
//
return false;
}
//
// successful execution ...
return true;
}
//
// END Horizontal Line Functions ...
//
//
// START Vertical Line Functions ...
//
//
// Create the vertical line ...
bool DrawVerticalLine(
const long chartID = 0, // chart's ID
const string name = "VLine", // line name
const int subWindow = 0, // subwindow index
datetime time = 0, // line time
const color clr = clrRed, // line color
const ENUM_LINE_STYLE style = STYLE_SOLID, // line style
const int width = 1, // line width
const bool back = false, // in the background
const bool selection = false, // highlight to move
const bool hidden = false, // hidden in the object list
const long zOrder = 0 // priority for mouse click
)
{
//
// if the line time is not set, draw it via the last bar ...
if (!time)
{
time = TimeCurrent();
}
//
// reset the error value
ResetLastError();
//
// Normalize Name ...
string objName = drawPrefix + "_" + name;
//
// create a vertical line ...
if (!ObjectCreate(chartID, objName, OBJ_VLINE, subWindow, time, 0))
{
//
string message =
__FUNCTION__ +
": failed to create a vertical line! Error code = " +
(string)GetLastError();
//
LogMessage(message);
//
return false;
}
//
// set line color ...
ObjectSetInteger(chartID, objName, OBJPROP_COLOR, clr);
//
// set line display style ...
ObjectSetInteger(chartID, objName, OBJPROP_STYLE, style);
//
// set line width ...
ObjectSetInteger(chartID, objName, OBJPROP_WIDTH, width);
//
// display in the foreground false or background true ...
ObjectSetInteger(chartID, objName, OBJPROP_BACK, back);
//
// enable true or disable false the mode of moving the line by mouse
// when creating a graphical object using ObjectCreate function, the object cannot be
// highlighted and moved by default. Inside this method, selection parameter
// is true by default making it possible to highlight and move the object ...
ObjectSetInteger(chartID, objName, OBJPROP_SELECTABLE, true);
ObjectSetInteger(chartID, objName, OBJPROP_SELECTED, selection);
//
// hide true or display false graphical object name in the object list ...
ObjectSetInteger(chartID, objName, OBJPROP_HIDDEN, hidden);
//
// set the priority for receiving the event of a mouse click in the chart
ObjectSetInteger(chartID, objName, OBJPROP_ZORDER, zOrder);
//
// successful execution ...
return true;
}
//
// Move the vertical line ...
bool MoveVerticalLine(
const long chartID = 0, // chart's ID
const string name = "VLine", // line name
datetime time = 0 // line time
)
{
//
// if line time is not set, move the line to the last bar ...
if (!time)
{
time = TimeCurrent();
}
//
// reset the error value ...
ResetLastError();
//
// move the vertical line ...
if (!ObjectMove(chartID, name, 0, time, 0))
{
//
string message =
__FUNCTION__ +
": failed to move the vertical line! Error code = " +
(string)GetLastError();
//
LogMessage(message);
//
return false;
}
//
// successful execution ...
return true;
}
//
// Delete the vertical line ...
bool DeleteVerticalLine(
const long chartID = 0, // chart's ID
const string name = "VLine" // line name
)
{
//
// reset the error value ...
ResetLastError();
//
// delete the vertical line ...
if (!ObjectDelete(chartID, name))
{
//
string message =
__FUNCTION__ +
": failed to delete the vertical line! Error code = " +
(string)GetLastError();
//
LogMessage(message);
//
return false;
}
//
// successful execution ...
return true;
}
//
// END Vertical Line Functions ...
//
//
// START Rectangle Functions ...
//
//
// Create rectangle by the given coordinates ...
bool DrawRectangle(
const long chartID = 0, // chart's ID
const string name = "Rectangle", // rectangle name
const int subWindow = 0, // subwindow index
datetime time1 = 0, // first point time
double price1 = 0, // first point price
datetime time2 = 0, // second point time
double price2 = 0, // second point price
const color clr = clrRed, // rectangle color
const ENUM_LINE_STYLE style = STYLE_SOLID, // style of rectangle lines
const int width = 1, // width of rectangle lines
const bool fill = false, // filling rectangle with color
const bool back = false, // in the background
const bool selection = true, // highlight to move
const bool hidden = true, // hidden in the object list
const long zOrder = 0 // priority for mouse click
)
{
//
bool result = false;
//
// Set anchor points' coordinates if they are not set ...
ChangeRectangleEmptyPoints(time1, price1, time2, price2);
//
// Create a rectangle by the given coordinates ...
result = ObjectCreate(chartID, name, OBJ_RECTANGLE, subWindow, time1, price1, time2, price2);
if (!result)
{
return result;
}
//
// Set rectangle color ...
ObjectSetInteger(chartID, name, OBJPROP_COLOR, clr);
//
// Set the style of rectangle lines
ObjectSetInteger(chartID, name, OBJPROP_STYLE, style);
//
// Set width of the rectangle lines ...
ObjectSetInteger(chartID, name, OBJPROP_WIDTH, width);
//
// Enable (true) or disable (false) the mode of filling the rectangle ...
ObjectSetInteger(chartID, name, OBJPROP_FILL, fill);
//
// Display in the foreground (false) or background (true) ...
ObjectSetInteger(chartID, name, OBJPROP_BACK, back);
//
// Enable (true) or disable (false) the mode of highlighting the rectangle for moving
// when creating a graphical object using ObjectCreate function, the object cannot be
// highlighted and moved by default. Inside this method, selection parameter
// is true by default making it possible to highlight and move the object
ObjectSetInteger(chartID, name, OBJPROP_SELECTABLE, selection);
//
ObjectSetInteger(chartID, name, OBJPROP_SELECTED, selection);
//
// Hide (true) or display (false) graphical object name in the object list ...
ObjectSetInteger(chartID, name, OBJPROP_HIDDEN, hidden);
//
// Set the priority for receiving the event of a mouse click in the chart ...
ObjectSetInteger(chartID, name, OBJPROP_ZORDER, zOrder);
//
// Successful execution
return result;
}
//
// Move the rectangle anchor point ...s
bool RectanglePointChange(
const long chartID = 0, // chart's ID
const string name = "Rectangle", // rectangle name
const int pointIndex = 0, // anchor point index
datetime time = 0, // anchor point time coordinate
double price = 0 // anchor point price coordinate
)
{
//
bool result = false;
//
// if point position is not set, move it to the current bar having Bid price ...
if (!time)
{
time = TimeCurrent();
}
//
if (!price)
{
price = SymbolInfoDouble(Symbol(), SYMBOL_BID);
}
//
// Move the anchor point ...
result = ObjectMove(chartID, name, pointIndex, time, price);
if (!result)
{
return result;
}
//
// Successful execution
return result;
}
//
// Delete the rectangle ...
bool DeleteRectangle(
const long chartID = 0, // chart's ID
const string name = "Rectangle" // rectangle name
)
{
//
bool result = false;
//
result = ObjectDelete(chartID, name);
//
return result;
}
//
// Check the values of rectangle's anchor points and set default
// values for empty ones
void ChangeRectangleEmptyPoints(
datetime &time1, double &price1,
datetime &time2, double &price2)
{
//
// if the first point's time is not set, it will be on the current bar ...
if (!time1)
{
time1 = TimeCurrent();
}
//
// if the first point's price is not set, it will have Bid value ...
if (!price1)
{
price1 = SymbolInfoDouble(Symbol(), SYMBOL_BID);
}
//
// if the second point's time is not set, it is located 9 bars left from the second one ...
if (!time2)
{
//
// array for receiving the open time of the last 10 bars ...
datetime temp[10];
CopyTime(Symbol(), Period(), time1, 10, temp);
//
// set the second point 9 bars left from the first one ...
time2 = temp[0];
}
//
// if the second point's price is not set, move it 300 points lower than the first one ...
if (!price2)
{
price2 = price1 - 300 * SymbolInfoDouble(Symbol(), SYMBOL_POINT);
}
}
//
// END Rectangle Functions ...
//
//
// START RectangleLabel Functions ...
//
//
// Create Rectangle Label Object ...
bool DrawRectangleLabel(
const long chartID = 0, // chart's ID
const string name = "RectLabel", // label name
const int subWindow = 0, // subwindow index
const int x = 0, // X coordinate
const int y = 0, // Y coordinate
const int width = 50, // width
const int height = 18, // height
const color bgColor = clrWhiteSmoke, // background color
const ENUM_BORDER_TYPE border = BORDER_SUNKEN, // border type
const ENUM_BASE_CORNER corner = CORNER_LEFT_UPPER, // chart corner for anchoring
const color clr = clrRed, // flat border color (Flat)
const ENUM_LINE_STYLE style = STYLE_SOLID, // flat border style
const int lineWidth = 1, // flat border width
const bool back = false, // in the background
const bool selection = false, // highlight to move
const bool hidden = false, // hidden in the object list
const long zOrder = 0 // priority for mouse click
)
{
//
// reset the error value ...
ResetLastError();
//
// Normalize Name ...
string objName = drawPrefix + "_" + name;
//
//
// create a rectangle label
if (!ObjectCreate(chartID, objName, OBJ_RECTANGLE_LABEL, subWindow, 0, 0))
{
//
string message =
__FUNCTION__ +
": failed to create a rectangle label! Error code = " +
(string)GetLastError();
//
LogMessage(message);
//
return false;
}
//
// set label coordinates ...
ObjectSetInteger(chartID, objName, OBJPROP_XDISTANCE, x);
ObjectSetInteger(chartID, objName, OBJPROP_YDISTANCE, y);
//
// set label size ...
ObjectSetInteger(chartID, objName, OBJPROP_XSIZE, width);
ObjectSetInteger(chartID, objName, OBJPROP_YSIZE, height);
//
// set background color ...
ObjectSetInteger(chartID, objName, OBJPROP_BGCOLOR, bgColor);
//
// set border type ...
ObjectSetInteger(chartID, objName, OBJPROP_BORDER_TYPE, border);
//
// set the chart's corner, relative to which point coordinates are defined ...
ObjectSetInteger(chartID, objName, OBJPROP_CORNER, corner);
//
// set flat border color (in Flat mode) ...
ObjectSetInteger(chartID, objName, OBJPROP_COLOR, clr);
//
// set flat border line style ...
ObjectSetInteger(chartID, objName, OBJPROP_STYLE, style);
//
// set flat border width ...
ObjectSetInteger(chartID, objName, OBJPROP_WIDTH, lineWidth);
//
// display in the foreground false or background true ...
ObjectSetInteger(chartID, objName, OBJPROP_BACK, back);
//
// enable true or disable false the mode of moving the label by mouse ...
ObjectSetInteger(chartID, objName, OBJPROP_SELECTABLE, selection);
ObjectSetInteger(chartID, objName, OBJPROP_SELECTED, selection);
//
// hide true or display false graphical object name in the object list ...
ObjectSetInteger(chartID, objName, OBJPROP_HIDDEN, hidden);
//
// set the priority for receiving the event of a mouse click in the chart ...
ObjectSetInteger(chartID, objName, OBJPROP_ZORDER, zOrder);
//
// successful execution ...
return true;
}
//
// Move Rectangle Label ..
bool MoveRectangleLabel(
const long chartID = 0, // chart's ID
const string name = "RectLabel", // label name
const int x = 0, // X coordinate
const int y = 0 // Y coordinate
)
{
//
// reset the error value ...
ResetLastError();
//
// move the rectangle label ...
if (!ObjectSetInteger(chartID, name, OBJPROP_XDISTANCE, x))
{
//
string message =
__FUNCTION__ +
": failed to move X coordinate of the label! Error code = " +
(string)GetLastError();
//
LogMessage(message);
//
return false;
}
//
if (!ObjectSetInteger(chartID, name, OBJPROP_YDISTANCE, y))
{
//
string message =
__FUNCTION__ +
": failed to move Y coordinate of the label! Error code = " +
(string)GetLastError();
//
return false;
}
//
// successful execution ...
return true;
}
//
// Delete the rectangle label ...
bool DeleteRectangleLabel(
const long chartID = 0, // chart's ID
const string name = "RectLabel" // label name
)
{
//
// reset the error value ...
ResetLastError();
//
// delete the label ...
if (!ObjectDelete(chartID, name))
{
//
string message =
__FUNCTION__ +
": failed to delete a rectangle label! Error code = " +
(string)GetLastError();
//
LogMessage(message);
//
return false;
}
//
// successful execution ...
return true;
}
//
// Change Rectangle Label Size ...
bool ChangeSizeRectangleLabel(
const long chartID = 0, // chart's ID
const string name = "RectLabel", // label name
const int width = 50, // label width
const int height = 18 // label height
)
{
//
// reset the error value ...
ResetLastError();
//
// change label size ...
if (!ObjectSetInteger(chartID, name, OBJPROP_XSIZE, width))
{
//
string message =
__FUNCTION__ +
": failed to change the label's width! Error code = " +
(string)GetLastError();
//
LogMessage(message);
//
return false;
}
//
if (!ObjectSetInteger(chartID, name, OBJPROP_YSIZE, height))
{
//
string message =
__FUNCTION__ +
": failed to change the label's height! Error code = " +
(string)GetLastError();
//
LogMessage(message);
//
return false;
}
//
// successful execution ...
return true;
}
//
// Change rectangle label border type ...
bool RectLabelChangeBorderType(
const long chartID = 0, // chart's ID
const string name = "RectLabel", // label name
const ENUM_BORDER_TYPE border = BORDER_SUNKEN // border type
)
{
//
// reset the error value ...
ResetLastError();
//
// change border type ...
if (!ObjectSetInteger(chartID, name, OBJPROP_BORDER_TYPE, border))
{
//
string message =
__FUNCTION__ +
": failed to change the border type! Error code = " +
(string)GetLastError();
//
LogMessage(message);
//
return false;
}
//
// successful execution ...
return true;
}
//
// END RectangleLabel Functions ...
//
//
// START TrendLine Funcions ...
//
//
// Create a trend line by the given coordinates ...
bool DrawTrendLine(
const long chartID = 0, // chart's ID
const string name = "TrendLine", // line name
const int subWindow = 0, // subwindow index
datetime time1 = 0, // first point time
double price1 = 0, // first point price
datetime time2 = 0, // second point time
double price2 = 0, // second point price
const color clr = clrYellow, // line color
const ENUM_LINE_STYLE style = STYLE_SOLID, // line style
const int width = 1, // line width
const bool back = false, // in the background
const bool selection = false, // highlight to move
const bool rayRight = false, // line's continuation to the right
const bool hidden = false, // hidden in the object list
const long zOrder = 0 // priority for mouse click
)
{
//
// set anchor points' coordinates if they are not set ...
ChangeTrendLineEmptyPoints(
time1,
price1,
time2,
price2);
//
// reset the error value ...
ResetLastError();
//
// Normalize Name ...
string objName = drawPrefix + "_" + name;
//
// create a trend line by the given coordinates ...
if (!ObjectCreate(chartID, objName, OBJ_TREND, subWindow, time1, price1, time2, price2))
{
//
string message =
__FUNCTION__ +
": failed to create a trend line! Error code = " +
(string)GetLastError();
//
LogMessage(message);
//
return false;
}
//
// set line color ...
ObjectSetInteger(chartID, objName, OBJPROP_COLOR, clr);
//
// set line display style ...
ObjectSetInteger(chartID, objName, OBJPROP_STYLE, style);
//
// set line width ...
ObjectSetInteger(chartID, objName, OBJPROP_WIDTH, width);
//
// display in the foreground false or background true ...
ObjectSetInteger(chartID, objName, OBJPROP_BACK, back);
//
// enable true or disable false the mode of moving the line by mouse
// when creating a graphical object using ObjectCreate function, the object cannot be
// highlighted and moved by default. Inside this method, selection parameter
// is true by default making it possible to highlight and move the object ...
ObjectSetInteger(chartID, objName, OBJPROP_SELECTABLE, true);
ObjectSetInteger(chartID, objName, OBJPROP_SELECTED, selection);
//
// enable true or disable false the mode of continuation of the line's display to the right ...
ObjectSetInteger(chartID, objName, OBJPROP_RAY_RIGHT, rayRight);
//
// hide true or display false graphical object name in the object list ...
ObjectSetInteger(chartID, objName, OBJPROP_HIDDEN, hidden);
//
// set the priority for receiving the event of a mouse click in the chart ...
ObjectSetInteger(chartID, objName, OBJPROP_ZORDER, zOrder);
//
// successful execution ...
return true;
}
//
// Move trend line anchor point ... |
bool ChangeTrendLinePoint(
const long chartID = 0, // chart's ID
const string name = "TrendLine", // line name
const int pointindex = 0, // anchor point index
datetime time = 0, // anchor point time coordinate
double price = 0 // anchor point price coordinate
)
{
//
// if point position is not set, move it to the current bar having Bid price ...
if (!time)
{
time = TimeCurrent();
}
//
if (!price)
{
price = SymbolInfoDouble(Symbol(), SYMBOL_BID);
}
//
// reset the error value ...
ResetLastError();
//
// move trend line's anchor point ...
if (!ObjectMove(chartID, name, pointindex, time, price))
{
//
string message =
__FUNCTION__ +
": failed to move the anchor point! Error code = " +
(string)GetLastError();
//
LogMessage(message);
//
return false;
}
//
// successful execution ...
return true;
}
//
// The function deletes the trend line from the chart ...
bool DeleteTrendLine(
const long chartID = 0, // chart's ID
const string name = "TrendLine" // line name
)
{
//
// reset the error value ...
ResetLastError();
//
// delete a trend line ...
if (!ObjectDelete(chartID, name))
{
//
string message =
__FUNCTION__ +
": failed to delete a trend line! Error code = " +
(string)GetLastError();
//
LogMessage(message);
//
return false;
}
//
// successful execution ...
return true;
}
//
// Check the values of trend line's anchor points and set default ...
// values for empty ones ...
void ChangeTrendLineEmptyPoints(
datetime &time1,
double &price1,
datetime &time2,
double &price2)
{
//
// if the first point's time is not set, it will be on the current bar ...
if (!time1)
{
time1 = TimeCurrent();
}
//
// if the first point's price is not set, it will have Bid value ...
if (!price1)
{
price1 = SymbolInfoDouble(Symbol(), SYMBOL_BID);
}
//
// if the second point's time is not set, it is located 9 bars left from the second one ...
if (!time2)
{
//
// array for receiving the open time of the last 10 bars ...
datetime temp[10];
CopyTime(Symbol(), Period(), time1, 10, temp);
//
// set the second point 9 bars left from the first one ...
time2 = temp[0];
}
//
// if the second point's price is not set, it is equal to the first point's one ...
if (!price2)
{
price2 = price1;
}
}
//
// END TrendLine Functions ...
//
//
// START Text Functions ...
//
//
// Create and Draw a Text Object ...
bool DrawText(
const long chartID = 0, // chart's ID
const string name = "Text", // object name
const int subWindow = 0, // subwindow index
datetime time = 0, // anchor point time
double price = 0, // anchor point price
const string text = "Text", // the text itself
const string font = "Arial", // font
const int fontSize = 10, // font size
const color clr = clrRed, // color
const double angle = 0.0, // text slope
const ENUM_ANCHOR_POINT anchor = ANCHOR_CENTER, // anchor type
const bool back = false, // in the background
const bool selection = false, // highlight to move
const bool hidden = true, // hidden in the object list
const long zOrder = 0 // priority for mouse click
)
{
//
// set anchor point coordinates if they are not set ...
ChangeTextEmptyPoint(time, price);
//
// reset the error value ...
ResetLastError();
//
// Normalize Name ...
string objName = drawPrefix + "_" + name;
//
//
// create Text object
if (!ObjectCreate(chartID, objName, OBJ_TEXT, subWindow, time, price))
{
//
string message =
__FUNCTION__ +
": failed to create \"Text\" object! Error code = " +
(string)GetLastError();
//
LogMessage(message);
//
return false;
}
//
// set the text ...
ObjectSetString(chartID, objName, OBJPROP_TEXT, text);
//
// set text font ...
ObjectSetString(chartID, objName, OBJPROP_FONT, font);
//
// set font size ...
ObjectSetInteger(chartID, objName, OBJPROP_FONTSIZE, fontSize);
//
// set the slope angle of the text ...
ObjectSetDouble(chartID, objName, OBJPROP_ANGLE, angle);
//
// set anchor type ...
ObjectSetInteger(chartID, objName, OBJPROP_ANCHOR, anchor);
//
// set color ...
ObjectSetInteger(chartID, objName, OBJPROP_COLOR, clr);
//
// display in the foreground false or background true ...
ObjectSetInteger(chartID, objName, OBJPROP_BACK, back);
//
// enable true or disable false the mode of moving the object by mouse ...
ObjectSetInteger(chartID, objName, OBJPROP_SELECTABLE, true);
ObjectSetInteger(chartID, objName, OBJPROP_SELECTED, selection);
//
// hide true or display false graphical object name in the object list ...
ObjectSetInteger(chartID, objName, OBJPROP_HIDDEN, hidden);
//
// set the priority for receiving the event of a mouse click in the chart ...
ObjectSetInteger(chartID, objName, OBJPROP_ZORDER, zOrder);
//
// successful execution ...
return true;
}
//
// Move the anchor point ...
bool MoveText(
const long chartID = 0, // chart's ID
const string name = "Text", // object name
datetime time = 0, // anchor point time coordinate
double price = 0 // anchor point price coordinate
)
{
//
//
// if point position is not set, move it to the current bar having Bid price
if (!time)
{
time = TimeCurrent();
}
//
if (!price)
{
price = SymbolInfoDouble(_Symbol, SYMBOL_BID);
}
//
// reset the error value ...
ResetLastError();
//
// move the anchor point ..
if (!ObjectMove(chartID, name, 0, time, price))
{
//
string message =
__FUNCTION__ +
": failed to move the anchor point! Error code = " +
(string)GetLastError();
//
LogMessage(message);
//
return false;
}
//
// successful execution ...
return true;
}
//
// Change the object text ...
bool ChangeText(
const long chartID = 0, // chart's ID
const string name = "Text", // object name
const string text = "Text" // text
)
{
//
// reset the error value ...
ResetLastError();
//
// change object text ...
if (!ObjectSetString(chartID, name, OBJPROP_TEXT, text))
{
//
string message =
__FUNCTION__ +
": failed to change the text! Error code = " +
(string)GetLastError();
//
LogMessage(message);
//
return false;
}
//
// successful execution ...
return true;
}
//
// Delete Text object ...
bool DeleteText(
const long chartID = 0, // chart's ID
const string name = "Text" // object name
)
{
//
// reset the error value ...
ResetLastError();
//
// delete the object ...
if (!ObjectDelete(chartID, name))
{
//
string message =
__FUNCTION__ +
": failed to delete \"Text\" object! Error code = " +
(string)GetLastError();
//
LogMessage(message);
//
return false;
}
//
// successful execution
return true;
}
//
// Check anchor point values and set default values
// for empty ones ...
void ChangeTextEmptyPoint(
datetime &time,
double &price)
{
//
// if the point's time is not set, it will be on the current bar ...
if (!time)
{
time = TimeCurrent();
}
//
// if the point's price is not set, it will have Bid value ...
if (!price)
{
price = SymbolInfoDouble(_Symbol, SYMBOL_BID);
}
}
//
// END Text Functions ...
//
//
// START Fibonacchi Functions ...
//
//
// Create Fibonacci Retracement by the given coordinates ...
bool DrawFibonacci(
const long chartID = 0, // chart's ID
const string name = "Fibonacci", // object name
const int subWindow = 0, // subwindow index
datetime time1 = 0, // first point time
double price1 = 0, // first point price
datetime time2 = 0, // second point time
double price2 = 0, // second point price
const color clr = clrRed, // object color
const ENUM_LINE_STYLE style = STYLE_SOLID, // object line style
const int width = 1, // object line width
const bool back = false, // in the background
const bool selection = true, // highlight to move
const bool rayRight = false, // object's continuation to the right
const bool hidden = true, // hidden in the object list
const long zOrder = 0 // priority for mouse click
)
{
//
// set anchor points' coordinates if they are not set ...
ChangeFibonacciEmptyPoints(time1, price1, time2, price2);
//
// reset the error value ...
ResetLastError();
//
// Normalize Name ...
string objName = drawPrefix + "_" + name;
//
// Create Fibonacci Retracement by the given coordinates ...
if (!ObjectCreate(chartID, objName, OBJ_FIBO, subWindow, time1, price1, time2, price2))
{
//
string message =
__FUNCTION__ +
": failed to create \"Fibonacci Retracement\"! Error code = " +
(string)GetLastError();
//
LogMessage(message);
//
return false;
}
//
// set color ...
ObjectSetInteger(chartID, objName, OBJPROP_COLOR, clr);
//
// set line style ...
ObjectSetInteger(chartID, objName, OBJPROP_STYLE, style);
//
// set line width ...
ObjectSetInteger(chartID, objName, OBJPROP_WIDTH, width);
//
// display in the foreground false or background true ...
ObjectSetInteger(chartID, objName, OBJPROP_BACK, back);
//
// enable true or disable false the mode of highlighting the channel for moving
// when creating a graphical object using ObjectCreate function, the object cannot be
// highlighted and moved by default. Inside this method, selection parameter
// is true by default making it possible to highlight and move the object ...
ObjectSetInteger(chartID, objName, OBJPROP_SELECTABLE, true);
ObjectSetInteger(chartID, objName, OBJPROP_SELECTED, selection);
//
// enable true or disable false the mode of continuation of the object's display to the right ...
ObjectSetInteger(chartID, objName, OBJPROP_RAY_RIGHT, rayRight);
//
// hide true or display false graphical object name in the object list ...
ObjectSetInteger(chartID, objName, OBJPROP_HIDDEN, hidden);
//
// set the priority for receiving the event of a mouse click in the chart ...
ObjectSetInteger(chartID, objName, OBJPROP_ZORDER, zOrder);
//
// successful execution ...
return true;
}
//
// Create Fibonacci Retracement by the given coordinates ...
bool DrawCustomFibonacci(
//
int levels, // number of level lines
double &values[], // values of level lines
color &colors[], // color of level lines
ENUM_LINE_STYLE &styles[], // style of level lines
int &widths[], // width of level lines
string &descriptions[], // level Descriptions
//
const long chartID = 0, // chart's ID
const string name = "Fibonacci", // object name
const int subWindow = 0, // subwindow index
datetime time1 = 0, // first point time
double price1 = 0, // first point price
datetime time2 = 0, // second point time
double price2 = 0, // second point price
const color clr = clrAqua, // object color
const ENUM_LINE_STYLE style = STYLE_DOT, // object line style
const int width = 1, // object line width
const bool back = false, // in the background
const bool selection = true, // highlight to move
const bool rayRight = false, // object's continuation to the right
const bool hidden = true, // hidden in the object list
const long zOrder = 0 // priority for mouse click
)
{
//
// set anchor points' coordinates if they are not set ...
ChangeFibonacciEmptyPoints(time1, price1, time2, price2);
//
// reset the error value ...
ResetLastError();
//
// Normalize Name ...
string objName = drawPrefix + "_" + name;
//
// Create Fibonacci Retracement by the given coordinates ...
if (!ObjectCreate(chartID, name, OBJ_FIBO, subWindow, time1, price1, time2, price2))
{
//
string message =
__FUNCTION__ +
": failed to create \"Fibonacci Retracement\"! Error code = " +
(string)GetLastError();
//
LogMessage(message);
//
return false;
}
//
// set color ...
ObjectSetInteger(chartID, objName, OBJPROP_COLOR, clr);
//
// set line style ...
ObjectSetInteger(chartID, objName, OBJPROP_STYLE, style);
//
// set line width ...
ObjectSetInteger(chartID, objName, OBJPROP_WIDTH, width);
//
// display in the foreground false or background true ...
ObjectSetInteger(chartID, objName, OBJPROP_BACK, back);
//
// enable true or disable false the mode of highlighting the channel for moving
// when creating a graphical object using ObjectCreate function, the object cannot be
// highlighted and moved by default. Inside this method, selection parameter
// is true by default making it possible to highlight and move the object ...
ObjectSetInteger(chartID, objName, OBJPROP_SELECTABLE, true);
ObjectSetInteger(chartID, objName, OBJPROP_SELECTED, selection);
//
// enable true or disable false the mode of continuation of the object's display to the right ...
ObjectSetInteger(chartID, objName, OBJPROP_RAY_RIGHT, rayRight);
//
// hide true or display false graphical object name in the object list ...
ObjectSetInteger(chartID, objName, OBJPROP_HIDDEN, hidden);
//
// set the priority for receiving the event of a mouse click in the chart ...
ObjectSetInteger(chartID, objName, OBJPROP_ZORDER, zOrder);
//
// Change Levels Of Fibonacci ...
bool result = FibonacciLevelsSet(
levels,
values,
colors,
styles,
widths,
descriptions,
chartID,
objName);
//
// successful execution ...
return result;
}
//
// Set number of levels and their parameters ...
bool FibonacciLevelsSet(
int levels, // number of level lines
double &values[], // values of level lines
color &colors[], // color of level lines
ENUM_LINE_STYLE &styles[], // style of level lines
int &widths[], // width of level lines
string &descriptions[], // level Descriptions
const long chartID = 0, // chart's ID
const string name = "Fibonacci" // object name
)
{
//
// check array sizes ...
if (
levels != ArraySize(colors) ||
levels != ArraySize(styles) ||
levels != ArraySize(widths) ||
levels != ArraySize(widths))
{
//
string message =
__FUNCTION__ +
": array length does not correspond to the number of levels, error!";
//
LogMessage(message);
//
return false;
}
//
// set the number of levels ...
ObjectSetInteger(chartID, name, OBJPROP_LEVELS, levels);
//
// set the properties of levels in the loop ...
for (int i = 0; i < levels; i++)
{
//
// level value ...
ObjectSetDouble(chartID, name, OBJPROP_LEVELVALUE, i, values[i]);
//
// level color ...
ObjectSetInteger(chartID, name, OBJPROP_LEVELCOLOR, i, colors[i]);
//
// level style ...
ObjectSetInteger(chartID, name, OBJPROP_LEVELSTYLE, i, styles[i]);
//
// level width ...
ObjectSetInteger(chartID, name, OBJPROP_LEVELWIDTH, i, widths[i]);
//
// level description ...
// ObjectSetString(chartID, name, OBJPROP_LEVELTEXT, i, DoubleToString(100 * values[i], 1));
ObjectSetString(chartID, name, OBJPROP_LEVELTEXT, i, descriptions[i]);
}
//
// successful execution ...
return true;
}
//
// Move Fibonacci Retracement anchor point ...
bool FibonacciPointChange(
const long chartID = 0, // chart's ID
const string name = "Fibonacci", // object name
const int pointIndex = 0, // anchor point index
datetime time = 0, // anchor point time coordinate
double price = 0 // anchor point price coordinate
)
{
//
// if point position is not set, move it to the current bar having Bid price ...
if (!time)
{
time = TimeCurrent();
}
//
if (!price)
{
price = SymbolInfoDouble(_Symbol, SYMBOL_BID);
}
//
// reset the error value ...
ResetLastError();
//
// move the anchor point ...
if (!ObjectMove(chartID, name, pointIndex, time, price))
{
//
string message =
__FUNCTION__ +
": failed to move the anchor point! Error code = " +
(string)GetLastError();
//
LogMessage(message);
//
return false;
}
//
// successful execution ...
return true;
}
//
// Delete Fibonacci Retracement ...
bool DeleteFibonachi(
const long chartID = 0, // chart's ID
const string name = "Fibonacci" // object name
)
{
//
// reset the error value ...
ResetLastError();
//
// delete the object ...
if (!ObjectDelete(chartID, name))
{
//
string message =
__FUNCTION__ +
": failed to delete \"Fibonacci Retracement\"! Error code = " +
(string)GetLastError();
//
LogMessage(message);
//
return false;
}
//
// successful execution ...
return true;
}
//
// Check the values of Fibonacci Retracement anchor points and set
// default values for empty ones ...
void ChangeFibonacciEmptyPoints(
datetime &time1,
double &price1,
datetime &time2,
double &price2)
{
//
// if the second point's time is not set, it will be on the current bar ...
if (!time2)
{
time2 = TimeCurrent();
}
//
// if the second point's price is not set, it will have Bid value ...
if (!price2)
{
price2 = SymbolInfoDouble(_Symbol, SYMBOL_BID);
}
//
// if the first point's time is not set, it is located 9 bars left from the second one ...
if (!time1)
{
//
// array for receiving the open time of the last 10 bars ...
datetime temp[10];
CopyTime(Symbol(), Period(), time2, 10, temp);
//
// set the first point 9 bars left from the second one ...
time1 = temp[0];
}
//
// if the first point's price is not set, move it 200 points below the second one ...
if (!price1)
{
price1 = price2 - 200 * SymbolInfoDouble(_Symbol, SYMBOL_POINT);
}
}
//
// END Fibonacci Functions ...
//
//
// START Event Functions ...
//
//
// Create an Event ...
bool EventCreate(
const long chartID = 0, // chart's ID
const string name = "Event", // object name
const int subWindow = 0, // subwindow index
const string text = "Content", // event text
datetime time = 0, // anchor point time
const color clr = clrRed, // color
const int width = 1, // point width when highlighted
const bool back = false, // in the background
const bool selection = false, // highlight to move
const bool hidden = true, // hidden in the object list
const long zOrder = 0 // priority for mouse click
)
{
//
if (!time)
{
time = TimeCurrent();
}
//
ResetLastError();
//
if (!ObjectCreate(chartID, name, OBJ_EVENT, subWindow, time, 0))
{
//
string message =
__FUNCTION__ +
": failed to create \"Event\" object! Error code = " +
(string)GetLastError();
//
LogMessage(message);
//
return false;
}
//
// set event text
ObjectSetString(chartID, name, OBJPROP_TEXT, text);
//
// set color
ObjectSetInteger(chartID, name, OBJPROP_COLOR, clr);
//
// set anchor point width if the object is highlighted
ObjectSetInteger(chartID, name, OBJPROP_WIDTH, width);
//
// display in the foreground false or background true
ObjectSetInteger(chartID, name, OBJPROP_BACK, back);
//
// enable true or disable false the mode of moving event by mouse
ObjectSetInteger(chartID, name, OBJPROP_SELECTABLE, selection);
ObjectSetInteger(chartID, name, OBJPROP_SELECTED, selection);
//
// hide true or display false graphical object name in the object list
ObjectSetInteger(chartID, name, OBJPROP_HIDDEN, hidden);
//
// set the priority for receiving the event of a mouse click in the chart
ObjectSetInteger(chartID, name, OBJPROP_ZORDER, zOrder);
//
// successful execution
return true;
}
//
// Change Event object text
bool EventTextChange(
const long chartID = 0, // chart's ID
const string name = "Event", // event name
const string text = "Content" // text
)
{
//
// reset the error value
ResetLastError();
//
// change object text
if (!ObjectSetString(chartID, name, OBJPROP_TEXT, text))
{
//
string message =
__FUNCTION__ +
": failed to change the text! Error code = " +
(string)GetLastError();
//
LogMessage(message);
//
return false;
}
//
// successful execution
return true;
}
//
// Move Event object
bool EventMove(
const long chartID = 0, // chart's ID
const string name = "Event", // event name
datetime time = 0 // time
)
{
//
// if time is not set, move event to the last bar
if (!time)
{
time = TimeCurrent();
}
//
// reset the error value
ResetLastError();
//
// move the object
if (!ObjectMove(chartID, name, 0, time, 0))
{
//
string message =
__FUNCTION__ +
": failed to move \"Event\" object! Error code = " +
(string)GetLastError();
//
LogMessage(message);
//
return false;
}
//
// successful execution
return true;
}
//
// Delete Event object
bool EventDelete(
const long chartID = 0, // chart's ID
const string name = "Event" // event name
)
{
//
// reset the error value
ResetLastError();
//
// delete the object
if (!ObjectDelete(chartID, name))
{
//
string message =
__FUNCTION__ +
": failed to delete \"Event\" object! Error code = " +
(string)GetLastError();
//
LogMessage(message);
//
return false;
}
//
// successful execution
return true;
}
//
// END Event Functions ...
//
//
// START Button Functions ...
//
//
// Create the button ...
bool ButtonCreate(
const long chartID = 0, // chart's ID
const string name = "Button", // button name
const int subWindow = 0, // subwindow index
const int x = 0, // X coordinate
const int y = 0, // Y coordinate
const int width = 50, // button width
const int height = 15, // button height
const ENUM_BASE_CORNER corner = CORNER_LEFT_UPPER, // chart corner for anchoring
const string text = "Button", // text
const string font = "Arial", // font
const int fontSize = 8, // font size
const color clr = clrBlack, // text color
const color bgColor = clrLightGray, // background color
const color borderColor = clrNONE, // border color
const bool state = false, // pressed/released
const bool back = false, // in the background
const bool selection = false, // highlight to move
const bool hidden = true, // hidden in the object list
const long zOrder = 0 // priority for mouse click
)
{
//
bool result = false;
//
// create the button
result = ObjectCreate(chartID, name, OBJ_BUTTON, subWindow, 0, 0);
if (!result)
{
return result;
}
//
// set button coordinates
ObjectSetInteger(chartID, name, OBJPROP_XDISTANCE, x);
ObjectSetInteger(chartID, name, OBJPROP_YDISTANCE, y);
//
// set button size
ObjectSetInteger(chartID, name, OBJPROP_XSIZE, width);
ObjectSetInteger(chartID, name, OBJPROP_YSIZE, height);
//
// set the chart's corner, relative to which point coordinates are defined
ObjectSetInteger(chartID, name, OBJPROP_CORNER, corner);
//
// set the text
ObjectSetString(chartID, name, OBJPROP_TEXT, text);
//
// set text font
ObjectSetString(chartID, name, OBJPROP_FONT, font);
//
// set font size
ObjectSetInteger(chartID, name, OBJPROP_FONTSIZE, fontSize);
//
// set text color
ObjectSetInteger(chartID, name, OBJPROP_COLOR, clr);
//
// set background color
ObjectSetInteger(chartID, name, OBJPROP_BGCOLOR, bgColor);
//
// set border color
ObjectSetInteger(chartID, name, OBJPROP_BORDER_COLOR, borderColor);
//
// display in the foreground false or background true
ObjectSetInteger(chartID, name, OBJPROP_BACK, back);
//
// set button state
ObjectSetInteger(chartID, name, OBJPROP_STATE, state);
//
// enable true or disable false the mode of moving the button by mouse
ObjectSetInteger(chartID, name, OBJPROP_SELECTABLE, selection);
ObjectSetInteger(chartID, name, OBJPROP_SELECTED, selection);
//
// hide true or display false graphical object name in the object list
ObjectSetInteger(chartID, name, OBJPROP_HIDDEN, hidden);
//
// set the priority for receiving the event of a mouse click in the chart
ObjectSetInteger(chartID, name, OBJPROP_ZORDER, zOrder);
//
return result;
}
//
// Move the button ...
bool ButtonMove(
const long chartID = 0, // chart's ID
const string name = "Button", // button name
const int x = 0, // X coordinate
const int y = 0 // Y coordinate
)
{
//
bool result = false;
//
// move the button
//
result = ObjectSetInteger(chartID, name, OBJPROP_XDISTANCE, x);
if (!result)
{
return result;
}
//
result = ObjectSetInteger(chartID, name, OBJPROP_YDISTANCE, y);
//
return result;
}
//
// Change button size ...
bool ButtonChangeSize(
const long chartID = 0, // chart's ID
const string name = "Button", // button name
const int width = 50, // button width
const int height = 18 // button height
)
{
//
bool result = false;
//
// change the button size
//
result = ObjectSetInteger(chartID, name, OBJPROP_XSIZE, width);
if (!result)
{
return result;
}
//
result = ObjectSetInteger(chartID, name, OBJPROP_YSIZE, height);
//
return result;
}
//
// Change corner of the chart for binding the button ...
bool ButtonChangeCorner(
const long chartID = 0, // chart's ID
const string name = "Button", // button name
const ENUM_BASE_CORNER corner = CORNER_LEFT_UPPER // chart corner for anchoring
)
{
//
bool result = false;
//
// change anchor corner
//
result = ObjectSetInteger(chartID, name, OBJPROP_CORNER, corner);
//
return result;
}
//
// Change button text ...
bool ButtonTextChange(
const long chartID = 0, // chart's ID
const string name = "Button", // button name
const string text = "Text" // text
)
{
//
bool result = false;
//
// change object text
result = ObjectSetString(chartID, name, OBJPROP_TEXT, text);
//
return result;
}
//
// Delete the button ...
bool ButtonDelete(
const long chartID = 0, // chart's ID
const string name = "Button" // button name
)
{
//
bool result = false;
//
// delete the button
result = ObjectDelete(chartID, name);
//
return result;
}
//
// END Button Functions ...
//
//
// END Regular Draws ...
//
//
// START Chart Style Drawers ...
//
//
// Apply Styles On Chart ...
void ApplyChartStyle(
long mChartId = -1, // chart's ID
ENUM_CHART_MODE mMode = CHART_CANDLES, // chart's mMode
bool mShowBidLine = true, // show bid line
bool mShowAskLine = true, // show ask line
bool mShowGrid = false, // show grids on chart
bool mShowVolumes = false, // show volumes
bool mShowTradeLevels = true, // Displaying trade levels in the chart (levels of open positions, Stop Loss, Take Profit and pending orders)
bool mAutoScroll = true, // chart autoscroll
bool mQuickNavigation = true, // chart quick navigation state
color mForeGroundColor = clrWhite, // chart's foreground color
color mBackGroundColor = clrBlack, // chart's background color
color mUpColor = clrGreen, // Up Color
color mDownColor = clrRed, // Down Color
color mBullishColor = clrGreen, // Bullish color
color mBearishColor = clrRed, // Bearish color
color mGridColor = clrGray, // grid color
color mBidLineColor = clrGray, // bid line color
color mAskLineColor = clrRed, // ask line color
color mLineColor = clrLime, // line mMode and doji candlestick color
color mStopColor = clrGold, // Color of stop order levels (Stop Loss and Take Profit)
color mVolumesColor = clrGreen // volumes color
)
{
//
// Validate Args ...
if (mChartId == -1)
{
mChartId = 0;
}
//
ChartSetInteger(mChartId, CHART_MODE, mMode);
ChartSetInteger(mChartId, CHART_SHOW_BID_LINE, mShowBidLine);
ChartSetInteger(mChartId, CHART_SHOW_ASK_LINE, mShowAskLine);
ChartSetInteger(mChartId, CHART_SHOW_GRID, mShowGrid);
ChartSetInteger(mChartId, CHART_SHOW_VOLUMES, mShowVolumes);
ChartSetInteger(mChartId, CHART_SHOW_TRADE_LEVELS, mShowTradeLevels);
ChartSetInteger(mChartId, CHART_AUTOSCROLL, mAutoScroll);
ChartSetInteger(mChartId, CHART_QUICK_NAVIGATION, mQuickNavigation);
ChartSetInteger(mChartId, CHART_COLOR_FOREGROUND, mForeGroundColor);
ChartSetInteger(mChartId, CHART_COLOR_BACKGROUND, mBackGroundColor);
ChartSetInteger(mChartId, CHART_COLOR_CHART_UP, mUpColor);
ChartSetInteger(mChartId, CHART_COLOR_CHART_DOWN, mDownColor);
ChartSetInteger(mChartId, CHART_COLOR_CANDLE_BULL, mBullishColor);
ChartSetInteger(mChartId, CHART_COLOR_CANDLE_BEAR, mBearishColor);
ChartSetInteger(mChartId, CHART_COLOR_GRID, mGridColor);
ChartSetInteger(mChartId, CHART_COLOR_BID, mBidLineColor);
ChartSetInteger(mChartId, CHART_COLOR_ASK, mAskLineColor);
ChartSetInteger(mChartId, CHART_COLOR_STOP_LEVEL, mStopColor);
ChartSetInteger(mChartId, CHART_COLOR_CHART_LINE, mLineColor);
ChartSetInteger(mChartId, CHART_COLOR_VOLUME, mVolumesColor);
}
//
// Retrieve Chart Size in Pixel ...
struct XSize
{
long width;
long height;
//
int x;
int y;
int w;
int h;
//
int wStep;
int hStep;
};
//
// Retrieve Chart Size ...
bool GetChartSize(
XSize &result, // Holds Result ...
long chartID = 0 // Specified Chart ID ...
)
{
//
bool mResult = false;
//
mResult = ChartGetInteger(0, CHART_WIDTH_IN_PIXELS, 0, result.width);
if (!mResult)
{
return mResult;
}
//
mResult = ChartGetInteger(0, CHART_HEIGHT_IN_PIXELS, 0, result.height);
//
return mResult;
}
//
// END Chart Style Drawers ...
//
//
// START Custom Drawings ...
//
//
// Define a Draw Specifications ...
struct XDrawSpecifications
{
//
// Props ...
//
bool draw; // Draw or Not ...
//
int width; // Draw Witdth ...
color clr; // Draw Color ...
ENUM_LINE_STYLE style; // Draw Style ...
//
bool back; // in the background
bool fill; // fill object if supported
bool selection; // highlight to move
bool rayRight; // line's continuation to the right
bool hidden; // hidden in the object list
long zOrder; // priority for mouse click
//
string label; // Label ...
color labelColor; // Label Color ...
double labelAngel; // Label Angle ...
string labelFont; // Label Font ...
int labelFontSize; // Label Font Size ...
ENUM_ANCHOR_POINT labelAnchor; // Label Anchor Point ...
//
// Constructor ...
XDrawSpecifications()
{
Clean();
}
//
// Operator ...
void operator=(const XDrawSpecifications &value)
{
//
clr = value.clr;
draw = value.draw;
width = value.width;
style = value.style;
//
back = value.back;
hidden = value.hidden;
zOrder = value.zOrder;
rayRight = value.rayRight;
selection = value.selection;
//
fill = value.fill;
//
label = value.label;
labelColor = value.labelColor;
labelAngel = value.labelAngel;
labelFont = value.labelFont;
labelFontSize = value.labelFontSize;
labelAnchor = value.labelAnchor;
}
//
// Functions ...
//
void Clean()
{
//
draw = false;
width = 1;
clr = CLR_NONE;
style = STYLE_SOLID;
//
back = false;
hidden = false;
rayRight = false;
selection = false;
fill = false;
//
zOrder = 0;
//
label = "";
labelColor = CLR_NONE;
labelAngel = 0;
labelFont = "Arial";
labelFontSize = 20;
labelAnchor = ANCHOR_LEFT_UPPER;
}
//
void Label(
string mLabel = "", // Label ...
color mLabelColor = CLR_NONE, // Label Color ...
string mLabelFont = "Arial", // Label Font ...
int mLabelFontSize = 20, // Label Font Size ...
ENUM_ANCHOR_POINT mLabelAnchor = ANCHOR_LEFT_UPPER, // Label Anchor Point ...
double mLabelAngel = 0 // Label Angle ...
)
{
//
label = mLabel;
labelFont = mLabelFont;
labelColor = mLabelColor;
labelAngel = mLabelAngel;
labelAnchor = mLabelAnchor;
labelFontSize = mLabelFontSize;
}
};
//
// Candle Draw Specifications ...
struct XOHCLDrawSpecification
{
//
// Props ...
XDrawSpecifications openStyle;
XDrawSpecifications highStyle;
XDrawSpecifications closeStyle;
XDrawSpecifications lowStyle;
//
// Constructors ...
XOHCLDrawSpecification()
{
Clean();
}
//
// Operators ...
void operator=(const XOHCLDrawSpecification &value)
{
//
lowStyle = value.lowStyle;
openStyle = value.openStyle;
highStyle = value.highStyle;
closeStyle = value.closeStyle;
}
//
// Functions ...
//
void Clean()
{
//
lowStyle.Clean();
openStyle.Clean();
highStyle.Clean();
closeStyle.Clean();
}
//
void Default()
{
//
// Open ...
openStyle.width = 1;
openStyle.draw = true;
openStyle.clr = clrLime;
openStyle.style = STYLE_SOLID;
openStyle.Label(
"O",
openStyle.clr);
//
// High ...
highStyle.width = 1;
highStyle.draw = true;
highStyle.clr = clrAqua;
highStyle.style = STYLE_SOLID;
highStyle.Label(
"H",
highStyle.clr);
//
// Close ...
closeStyle.width = 1;
closeStyle.draw = true;
closeStyle.clr = clrRed;
closeStyle.style = STYLE_SOLID;
closeStyle.Label(
"C",
closeStyle.clr);
//
// Low ...
lowStyle.width = 1;
lowStyle.draw = true;
lowStyle.clr = clrMagenta;
lowStyle.style = STYLE_SOLID;
lowStyle.Label(
"L",
lowStyle.clr);
}
//
void Width(int value)
{
//
lowStyle.width = value;
openStyle.width = value;
highStyle.width = value;
closeStyle.width = value;
}
//
void Style(ENUM_LINE_STYLE value)
{
//
lowStyle.style = value;
openStyle.style = value;
highStyle.style = value;
closeStyle.style = value;
}
//
void Color(color value)
{
//
lowStyle.clr = value;
openStyle.clr = value;
highStyle.clr = value;
closeStyle.clr = value;
}
//
void RayRight(bool value)
{
//
lowStyle.rayRight = value;
openStyle.rayRight = value;
highStyle.rayRight = value;
closeStyle.rayRight = value;
}
//
void Draw(bool value)
{
//
lowStyle.draw = value;
openStyle.draw = value;
highStyle.draw = value;
closeStyle.draw = value;
}
//
void Back(bool value)
{
//
lowStyle.back = value;
openStyle.back = value;
highStyle.back = value;
closeStyle.back = value;
}
//
void Fill(bool value)
{
//
lowStyle.fill = value;
openStyle.fill = value;
highStyle.fill = value;
closeStyle.fill = value;
}
//
void Label(string value)
{
//
lowStyle.Label(value);
openStyle.Label(value);
highStyle.Label(value);
closeStyle.Label(value);
}
//
void SetLabelPrefix(string value)
{
//
lowStyle.label = value + " " + lowStyle.label;
openStyle.label = value + " " + openStyle.label;
highStyle.label = value + " " + highStyle.label;
closeStyle.label = value + " " + closeStyle.label;
}
};
//
bool RemoveCandle(
XOHCL &c,
long chartId = 0,
int subWindow = 0 //
)
{
//
bool result = false;
//
string cTag = GenerateTag(c);
//
result = IsDrawExists(
cTag,
chartId,
subWindow);
if (!result)
{
return result;
}
//
RemoveDraws(
cTag,
chartId,
subWindow);
//
return result;
}
//
// XOHCL ...
void DrawCandleAsLine(
long chartId, // Chart ID ...
int subWindow, // SubWindow ...
XOHCL &c, // the Candle Which required to Draw ...
const datetime time, // Candle Time ...
XOHCLDrawSpecification &config, // Draw Specifications ...
string tag = "", // Draw Tag ...
bool update = true, // Update Draw if Exists ...
bool drawMidLine = true // Draw Open Close Middle Line ...
)
{
//
datetime cTime = TimeCurrent();
XDrawSpecifications specs;
//
string baseTag = tag + GenerateTag(c);
//
// Open ...
specs = config.openStyle;
if (specs.draw)
{
//
double value = c.open;
string valueTag = "|O|";
//
string mTag = baseTag + valueTag + ToString(value);
string mLblTag = baseTag + valueTag + "_lbl";
//
// Check if Object Exists Delete it ...
bool isExists = IsDrawExists(
mTag,
chartId,
subWindow //
);
bool canDraw = !isExists || (isExists && update);
//
if (isExists && canDraw)
{
//
DeleteText(
chartId,
mLblTag //
);
//
DeleteTrendLine(
chartId,
mTag //
);
}
//
if (canDraw)
{
//
// Draw Label ...
if (StringLen(specs.label) > 0)
{
DrawText(
chartId,
mLblTag,
subWindow,
cTime,
value,
specs.label,
specs.labelFont,
specs.labelFontSize,
specs.labelColor,
specs.labelAngel,
specs.labelAnchor,
specs.back,
specs.selection,
specs.hidden,
specs.zOrder //
);
}
//
// Draw ...
DrawTrendLine(
chartId,
mTag,
subWindow,
time,
value,
cTime,
value,
specs.clr,
specs.style,
specs.width,
specs.back,
specs.selection,
specs.rayRight,
specs.hidden,
specs.zOrder //
);
}
}
//
// High ...
specs = config.highStyle;
if (specs.draw)
{
//
double value = c.high;
string valueTag = "|H|";
//
string mTag = baseTag + valueTag + ToString(value);
string mLblTag = baseTag + valueTag + "_lbl";
//
// Check if Object Exists Delete it ...
bool isExists = IsDrawExists(
mTag,
chartId,
subWindow //
);
bool canDraw = !isExists || (isExists && update);
//
if (isExists && canDraw)
{
//
DeleteText(
chartId,
mLblTag //
);
//
DeleteTrendLine(
chartId,
mTag //
);
}
//
if (canDraw)
{
//
// Draw Label ...
if (StringLen(specs.label) > 0)
{
DrawText(
chartId,
mLblTag,
subWindow,
cTime,
value,
specs.label,
specs.labelFont,
specs.labelFontSize,
specs.labelColor,
specs.labelAngel,
specs.labelAnchor,
specs.back,
specs.selection,
specs.hidden,
specs.zOrder //
);
}
//
// Draw ...
DrawTrendLine(
chartId,
mTag,
subWindow,
time,
value,
cTime,
value,
specs.clr,
specs.style,
specs.width,
specs.back,
specs.selection,
specs.rayRight,
specs.hidden,
specs.zOrder //
);
}
}
//
// Close ...
specs = config.closeStyle;
if (specs.draw)
{
//
double value = c.close;
string valueTag = "|C|";
//
string mTag = baseTag + valueTag + ToString(value);
string mLblTag = baseTag + valueTag + "_lbl";
//
// Check if Object Exists Delete it ...
bool isExists = IsDrawExists(
mTag,
chartId,
subWindow //
);
bool canDraw = !isExists || (isExists && update);
//
if (isExists && canDraw)
{
//
DeleteText(
chartId,
mLblTag //
);
//
DeleteTrendLine(
chartId,
mTag);
}
//
if (canDraw)
{
//
// Draw Label ...
if (StringLen(specs.label) > 0)
{
DrawText(
chartId,
mLblTag,
subWindow,
cTime,
value,
specs.label,
specs.labelFont,
specs.labelFontSize,
specs.labelColor,
specs.labelAngel,
specs.labelAnchor,
specs.back,
specs.selection,
specs.hidden,
specs.zOrder //
);
}
//
// Draw ...
DrawTrendLine(
chartId,
mTag,
subWindow,
time,
value,
cTime,
value,
specs.clr,
specs.style,
specs.width,
specs.back,
specs.selection,
specs.rayRight,
specs.hidden,
specs.zOrder //
);
}
}
//
// Low ...
specs = config.lowStyle;
if (specs.draw)
{
//
double value = c.low;
string valueTag = "|L|";
//
string mTag = baseTag + valueTag + ToString(value);
string mLblTag = baseTag + valueTag + "_lbl";
//
// Check if Object Exists Delete it ...
bool isExists = IsDrawExists(
mTag,
chartId,
subWindow //
);
bool canDraw = !isExists || (isExists && update);
//
if (isExists && canDraw)
{
//
DeleteText(
chartId,
mLblTag //
);
//
DeleteTrendLine(
chartId,
mTag);
}
//
if (canDraw)
{
//
// Draw Label ...
if (StringLen(specs.label) > 0)
{
DrawText(
chartId,
mLblTag,
subWindow,
cTime,
value,
specs.label,
specs.labelFont,
specs.labelFontSize,
specs.labelColor,
specs.labelAngel,
specs.labelAnchor,
specs.back,
specs.selection,
specs.hidden,
specs.zOrder //
);
}
//
// Draw ...
DrawTrendLine(
chartId,
mTag,
subWindow,
time,
value,
cTime,
value,
specs.clr,
specs.style,
specs.width,
specs.back,
specs.selection,
specs.rayRight,
specs.hidden,
specs.zOrder //
);
}
}
}
//
void DrawCandleAsBox(
long chartId, // Chart ID ...
int subWindow, // SubWindow ...
XOHCL &c, // the Candle Which required to Draw ...
const datetime time, // Candle Time ...
XOHCLDrawSpecification &config, // Draw Specifications ...
string tag = "", // Draw Tag ...
bool drawBody = true, // Draw Body ...
bool drawShadows = true, // Draw Shadows ...
bool update = true // Update Draw if Exists ...
)
{
//
datetime cTime = TimeCurrent();
//
// First Check all Styles Must Be Draw true ...
bool canDraw =
//
config.openStyle.draw &&
config.highStyle.draw &&
config.closeStyle.draw &&
config.lowStyle.draw
//
;
//
if (!canDraw)
{
return;
}
//
// Next we have to determine Candle is Bullish or Bearish ...
//
bool isBullish =
c.open < c.close;
//
bool isBearish =
c.open > c.close;
//
XDrawSpecifications bodySpec;
if (isBullish)
{
bodySpec = config.openStyle;
}
else if (isBearish)
{
bodySpec = config.closeStyle;
}
else
{
//
bodySpec.clr = clrGray;
}
//
// Now We have to Draw 3 Rectangle ...
// 1- Up Shadow
// 2- Candle Body
// 3- Down Shadow
//
double upPrice = MathMax(c.open, c.close);
double downPrice = MathMin(c.open, c.close);
double upShadowSize = c.high - upPrice;
double downShadowSize = downPrice - c.low;
//
string mTag = GenerateTag(c);
//
// Draw Up Shadow ...
if (drawShadows)
{
//
string upShadowTag = mTag + "|US|";
XDrawSpecifications spec = config.highStyle;
//
// Check if Object Exists Delete it ...
bool isExists = IsDrawExists(
upShadowTag,
chartId,
subWindow //
);
//
bool canDraw = !isExists || (isExists && update);
//
if (isExists && canDraw)
{
//
DeleteRectangle(
chartId,
upShadowTag //
);
}
//
if (canDraw)
{
//
// New ...
DrawRectangle(
chartId,
upShadowTag,
subWindow,
time,
c.high,
cTime,
upPrice,
spec.clr,
spec.style,
spec.width,
spec.fill,
spec.back,
spec.selection,
spec.hidden,
spec.zOrder //
);
}
}
//
// Draw Body ...
if (drawBody)
{
//
string bodyTag = mTag + "|BDY|";
//
// Check if Object Exists Delete it ...
bool isExists = IsDrawExists(
bodyTag,
chartId,
subWindow //
);
//
bool canDraw = !isExists || (isExists && update);
//
if (isExists && canDraw)
{
//
DeleteRectangle(
chartId,
bodyTag //
);
}
//
if (canDraw)
{
//
// New ...
DrawRectangle(
chartId,
bodyTag,
subWindow,
time,
upPrice,
cTime,
downPrice,
bodySpec.clr,
bodySpec.style,
bodySpec.width,
bodySpec.fill,
bodySpec.back,
bodySpec.selection,
bodySpec.hidden,
bodySpec.zOrder //
);
}
}
//
// Draw Down Shadow ...
if (drawShadows)
{
//
string downShadowTag = mTag + "|DS|";
XDrawSpecifications spec = config.lowStyle;
//
// Check if Object Exists Delete it ...
bool isExists = IsDrawExists(
downShadowTag,
chartId,
subWindow //
);
//
bool canDraw = !isExists || (isExists && update);
//
if (isExists && canDraw)
{
//
DeleteRectangle(
chartId,
downShadowTag //
);
}
//
if (canDraw)
{
//
// New ...
DrawRectangle(
chartId,
downShadowTag,
subWindow,
time,
downPrice,
cTime,
downPrice + downShadowSize,
spec.clr,
spec.style,
spec.width,
spec.fill,
spec.back,
spec.selection,
spec.hidden,
spec.zOrder //
);
}
}
}
//
// END Custom Drawings ...
//