drawing
Download scriptThis script serves as a specialized visual toolkit for the MetaTrader platform, designed to automatically generate and manage graphical elements on a trading chart. Rather than performing mathematical trade analysis itself, it acts as a "drawing engine" that allows other tools to place precise visual markers like boxes, vertical lines, and trend lines dynamically.
Core Functionalities
Dynamic Shape Management The script includes dedicated instructions for creating three primary types of visual aids:
- Rectangles: Used to highlight specific price zones or historical areas of interest.
- Vertical Lines: Ideal for marking specific points in time, such as economic announcements or key session opens.
- Trend Lines: Designed to connect two specific points on a chart to visualize market direction.
The logic is built to be "intelligent" regarding these shapes; before placing a new object, the script checks if an object with that specific ID already exists. If it does, the script updates the existing shape's position or color instead of creating a duplicate, ensuring the chart remains clean and organized.
Data Visualization Tools Beyond individual shapes, the script provides a mechanism to translate raw data arrays into continuous visual lines. This allows for the seamless plotting of complex datasets across the price history. It also includes a mathematical mapping system that can take a value from one range (like a percentage) and proportionally scale it to another (like a price level), which is essential for creating accurate custom scales on a chart.
Advanced Aesthetics To improve the user experience, the script features sophisticated visual processing:
- Color Gradient Logic: It contains the math required to calculate smooth transitions between different colors based on specific positions. This allows for more nuanced visual representations of data rather than simple, flat colors.
- Color Conversion: It handles the translation of standard Red, Green, and Blue (RGB) values into a format the trading platform can interpret perfectly.
Cleanup and Organization To prevent clutter, the script includes "erase" functions. These allow the software to selectively remove only the graphics created by this specific tool while leaving other manual drawings intact, or alternatively, to wipe the entire chart clean of all objects instantly. This provides a high level of control for traders who want a professional, organized workspace.
//+------------------------------------------------------------------+
//| drawing.mq4 |
//| Julien Loutre |
//| http://www.thetradingtheory.com |
//+------------------------------------------------------------------+
#property copyright "Julien Loutre"
#property link "http://www.thetradingtheory.com"
#property library
void draw_bar(string uid, string i, int x, double y, int x2, double y2, color c) {
if (ObjectFind("shape_"+uid+"_rec_"+i) == -1) {
ObjectCreate("shape_"+uid+"_rec_"+i,OBJ_RECTANGLE,0,x,y,x2,y2);
} else {
ObjectSet("shape_"+uid+"_rec_"+i, OBJPROP_TIME1, x);
ObjectSet("shape_"+uid+"_rec_"+i, OBJPROP_PRICE1, y);
ObjectSet("shape_"+uid+"_rec_"+i, OBJPROP_TIME2, x2);
ObjectSet("shape_"+uid+"_rec_"+i, OBJPROP_PRICE2, y2);
}
ObjectSet("shape_"+uid+"_rec_"+i, OBJPROP_COLOR, c);
}
void draw_vline(string uid, string i, int x, double y, color c) {
if (ObjectFind("shape_"+uid+"_vline_"+i) == -1) {
ObjectCreate("shape_"+uid+"_vline_"+i,OBJ_VLINE,0,x,y);
} else {
ObjectSet("shape_"+uid+"_vline_"+i, OBJPROP_TIME1, x);
ObjectSet("shape_"+uid+"_vline_"+i, OBJPROP_PRICE1, y);
}
ObjectSet("shape_"+uid+"_vline_"+i, OBJPROP_COLOR, c);
}
void draw_line(string uid, string i, int x, double y, int x2, double y2, color c) {
if (ObjectFind("shape_"+uid+"_line_"+i) == -1) {
ObjectCreate("shape_"+uid+"_line_"+i,OBJ_TREND,0,x,y,x2,y2);
} else {
ObjectSet("shape_"+uid+"_line_"+i, OBJPROP_TIME1, x);
ObjectSet("shape_"+uid+"_line_"+i, OBJPROP_PRICE1, y);
ObjectSet("shape_"+uid+"_line_"+i, OBJPROP_TIME2, x2);
ObjectSet("shape_"+uid+"_line_"+i, OBJPROP_PRICE2, y2);
}
ObjectSet("shape_"+uid+"_line_"+i, OBJPROP_RAY, false);
ObjectSet("shape_"+uid+"_line_"+i, OBJPROP_COLOR, c);
}
void draw_array_as_line(string uid, double& a[], color c) {
for (int i=0;i<ArraySize(a);i++) {
draw_line(uid, "arrayline"+i, Time[i],a[i],Time[i+1],a[i+1], c);
}
}
void draw_erase(string uid) {
string name;
string tmp;
int i;
int t = ObjectsTotal();
for (i=0;i<t;i++) {
name = ObjectName(i);
if (StringFind(name,"shape_"+uid) != -1) {
ObjectDelete(name);
}
}
}
void draw_erase_all() {
ObjectsDeleteAll();
}
int rgb2int(int r, int g, int b) {
return (b*65536+g*256+r);
}
int colorGradient(int r, int g, int b, int r2, int g2, int b2, double min, double max, double pos) {
double steps = (max-min);
pos = max-pos;
double stepR = (r-r2)/(steps-1);
double stepG = (g-g2)/(steps-1);
double stepB = (b-b2)/(steps-1);
return (rgb2int((r-(stepR*pos)),(g-(stepG*pos)),(b-(stepB*pos))));
}
double map(double x, double in_min, double in_max, double out_min, double out_max) {
return ((x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min);
}
Comments