/////////////////////////////////////////////////////// // // 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" // // 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, int 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, int 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, int 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 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 ... //