Easy Canvas (iCanvas) - MetaTrader 4 Library - MT4/MT5 Resources


This library and the iCanvas class simplify the process of writing programs using Canvas.
Below is an example of a simple indicator using this library.
In this example, the indicator body does not have the functionality to handle the OnChartEvent event. But it may exist.

# Property indicator chart window #propertystrict #Include <canvas\iCanvas.mqh> //+----------------------------------------------------------------------------------+ Integer initialization () { event set millisecond timer ( 30 ); Chart set integer ( 0 , CHART_CROSSHAIR_TOOL , wrong ); // Turn off crosshair return ( initialization successful ); } blank solution initialization ( const integer reason) { if (reason < 2 ) { ChartSetInteger ( 0 , CHART_CROSSHAIR_TOOL , True ); ChartRedraw (); }} //+---------------------------------------------------------------------------------+ blank timer () { Rest integer X= 0 ; Rest integer Y = 0 ; if (X!=W.MouseX || Y!=W.MouseY) { canvas.erase( 0 ); // Draw a fuzzy point for ( integer ix=- 20 ; ix<= 20 ; nine++) for ( integer iy=- 20 ; iy<= 20 ; iy++) { Double p= square root (ix*ix+iy*iy)/ 20.0 ; if (p> 1 ) p= 1 ; Uchar P=( Uchar ) round (( 1.0 -p)* 255.0 ); Canvas.PixelSet(W.MouseX+ix,W.MouseY+iy,ARGB(P, 100 , 0 , 200 )); } //Display information block integer Y1=( integer )Canvas.Y( canvas.Close (W.MouseX)); //Y coordinate - the closing price of the mouse pointer position. Canvas.FillRectangle(W.MouseX,Y1,W.MouseX+ 140 ,Y1+ 67 , 0xA0808080 ); Canvas.TextPosition(W.MouseX+ 5 ,Y1+ 2 ); Canvas.CurrentFont( "Song Dynasty" , 14 , 16 , innocent ); Canvas.Comm( "off=" + double to string ( canvas.off (W.MouseX), _number )); Canvas.Comm( "bar=" + double to string (W.MouseBar, 2 )); Canvas.Comm( String time (W.MouseTime, TIME_DATE | TIME_SECONDS )); Canvas.Comm( "subwindow=" + string (W.MouseSubWin)); // Crosshair Canvas.LineHorizontal( 0 ,W.Width- 1 ,W.MouseY,~W.Color); Canvas.LineVertical(W.MouseX, 0 ,W.Height- 1 ,~W.Color); Integer Ym=(W.MouseY>W.Height/ 2 )?(W.MouseY- 16 ):(W.MouseY+ 2 ); Integer Xm=(W.MouseX< W.width/ 2 )?(W.MouseX+ 4 ):(W.MouseX- 105 ); canvas. Text output (W. width - 50 , um, double to string (W. MousePrice, _number ), ~ W. color); canvas.unctions">Text output (Xm, W. height - 16 , string time (W.MouseTime, TIME_DATE | TIME_SECONDS ), ~W. color); Canvas.Update(); X=W.MouseX; Y=W.MouseY; } } //+------------------------------------------------------------------+ Integer calculation ( const integer rate_total, const integer previously calculated, const integer start, const double &price[]) { return (rate_total); }
Structure window:
structure window { Long Chart ID; // Current window identifier unit Color; // Window background color integer Width; // Window width integer Height; // Window height integer Height[]; // Sub-window height integer Left column; // The number of the leftmost column in the window doubles the right column; // The number of the rightmost column in the window doubles the total number of items; // The maximum number of items in the window integer Wind bar; // The number of visible bars in the window doubles Y_min; // The minimum value of the price in the window doubles Y_max; // The maximum value of the price in the window doubles dy_pix; // The price change of one pixel integer dx_pixels; // Changes the number of bars per pixel integer mouse Mouse price ; // Current price date and time of the mouse pointer Mouse time; // Integer of the current time of the mouse pointer Mouse subWin; // Integer of the sub-window number where the mouse pointer is located Windows total; // Total number of sub-windows, including the main window integer Sub-window; // Current sub-window date and time Time[]; // Array of opening times of all visible bars in the window };
iCanvas class:
class canvas: peoplecanvas { private : datetime T[ 1 ]; doublePu [ 1 ]; Boolean value FullWinCanvW; // Use a full window canvas by width Boolean value FullWinCanvH; // Use a full window canvas by height people : iCanvas ( long chart ID = 0 , integer ~iCanvas() { destroy(); chart redraw ();}; Double X ( double bar) { return (( double ) W.Left_bar-bar)*W.dx_pix;}; //The X coordinate represented by the bar number. Bar numbers must be of type double, otherwise the bars will be interpreted as times. Double X ( date time time); //X coordinate of time. Double Y ( double price) { if (W.dy_pix== 0 ) W.dy_pix= 1 ; return ((W.Y_max-price)/W.dy_pix); }; //Y coordinate of price. Double price ( integer y) { returns (W.Y_max-y*(W.Y_max-W.Y_min)/W.height);}; // Price of Y coordinate double bar ( integer x) {returns ( ( double )W.Left_bar + 1 - (double ) x/( double ) W.dx_pix );}; // Coordinate X bar number date time time position ( integer x); // Coordinate double open( integer x) { copy open ( _symbol , _period , integer (bar(x)), 1 ,Pr); return p[ 0 ];}; double high ( integer double low ( integer Boolean FullWinCanvWidth() { returns FullWinCanvW;}; // Use a full window canvas by width Boolean FullWinCanvHeight() { return FullWinCanvH;}; // Use a full window canvas by height Blank Communication( String Text) {ions">Text output (TextPosX, TextPosY, text, TextColor); TextPosY+=StepTextLine;}; // Print comment blank text position ( integer x, integer y) {TextPosX=x; TextPosY=y;}; // Set the XY position of comment output (in pixels) blank current font ( thin rope font name = "Express New" , integer size = 18 , integer line step = 20 , color clear = clr dark orchid , double transp = 1.0 ); // Set font options for comments. LineStep - step between lines. transp - transparency from 0 to 1 ( double x, double y); // Set the XY position of the output comment as a percentage of the window width and height. Blank line ( double x1, double y1, double x2, double y2, constant unit clear); integer text PosX; // X position of the comment text integer text position; // Position Y of the comment text integer step text line; // Comment line spacing unit text color; // Comment font color ENUM_PROGRAM_TYPE program type; integer subtemperature; long processing; };
If an instance of the Canvas class is not needed for the entire window, you can delete it and/or change it to a partial window.
delete canvas; Canvas = new iCanvas( 0 , 0 , "iCanvas" , 300 , 0 ); // Create only the left column of the window that is 300 pixels wide. Window resizing is controlled // Canvas= new iCanvas(0,0,"iCanvas",0,300); // Create only the top bar of the window with a height of 300 pixels. Window resizing is controlled // Canvas= new iCanvas(200,100,"iCanvas",300,300); // Create a 300x300 pixel square with a starting point of (200,100). Window resizing is not controlled.
You can also create new instances of the class and initialize them on creation.
iCanvas C2( 300 , 200 , "Canvas 2" , 200 , 150 , COLOR_FORMAT_XRGB_NOALPHA , 1 );
Important: Since the color format COLOR_FORMAT_ARGB_NORMALIZE with transparency is used by default, the high byte of transparency must be taken into account when specifying the color.
If you use a color name such as clrRed, the color will be absolutely transparent because the transparency bytes will be 0.
So you have to use the ColorToARGB function (e.g. ColorToARGB (clrRed))
Or specify the color explicitly using non-zero transparency:
- For example, 0xFFAABBCC (hex color representation),
FF - transparency bytes,
AA - is the red byte,
BB - is the green byte,
CC - is the blue byte.
PS: The library was developed for MQL5 and ported from MQL5 code. Therefore, it is not optimal for MQL4.
The MQL5 version is much faster.
Attachment download
📎 test_icanvas.mq4 (5.49 KB)
📎sine.mq4 (10.36 KB)
📎 icanvas.mqh (45.81 KB)
Source: MQL5 #23840
💡 Featured Recommendations
✍️ Latest by the author
- •
- •
- •
- •
- •
- •
📌 Popular topics
- •
- •
- •
- •
- •
- •
- •
- •
🔗 You May Be Interested In
- •
- •
- •
- •
- •
- •