s_Table
Download scriptThis script is designed to automatically generate and display organized data tables directly on your MetaTrader trading chart. Instead of manually placing individual text elements, this tool allows you to create structured grids with headers, rows, and custom styling in one go.
Core Logic and Functionality
Dynamic Table Generation The script works by taking a list of information (the content) and automatically arranging it into a grid format. It handles the heavy lifting of positioning: it calculates exactly where each piece of text should appear based on the number of columns and rows you want, ensuring that everything remains perfectly aligned without overlapping.
Visual Customization The logic includes a comprehensive styling system. For every table created, you can define specific visual parameters, including:
- Positioning: You can choose the exact X and Y coordinates for where each individual table starts on your screen.
- Color Schemes: Each table supports unique colors for both the header backgrounds and the main content areas, as well as specific text colors to ensure readability against the background.
- Typography: The script manages different font sizes and weights (such as Bold headers) automatically to create a professional visual hierarchy.
Smart Scaling and Formatting One of the key features is the "Table Size" logic. Depending on how large you want the table to appear, the script automatically adjusts internal margins, box widths, and font sizes. This ensures that whether you are creating a small, compact data snippet or a large, information-heavy dashboard, the text remains proportional and aesthetically pleasing.
Automated Object Handling Under the hood, the script handles the creation of every individual label required to build the grid. It generates unique names for every cell (using prefixes like "header," "content," and "background") so that multiple tables can exist on the same chart simultaneously without conflicting with one another.
Summary for the User
In short, this script acts as a UI builder. You provide the raw data and choose the colors and positions, and the logic handles the math of drawing perfectly aligned, professional-looking tables on your trading platform. It turns a complex task of manual placement into a streamlined process of "set it and forget it."
//+------------------------------------------------------------------+
//| s_Table.mq4 |
//| * |
//| * |
//+------------------------------------------------------------------+
#property copyright "Integer"
#property link "for-good-letters@yandex.ru"
//+------------------------------------------------------------------+
//| script program start function |
//+------------------------------------------------------------------+
int start(){
// Ãîòîâèì ìàññèâ ñ çàãîëîâêàìè
string Header[]={"ÊÎËÎÍÊÀ 1","ÊÎËÎÍÊÀ 2"};
// Ãîòîâèì ìàññèâ ñ ðàçìåðàìè êîëîíîê
int ColWidth[]={9,9};
string Content[20][2];
// Ãîòîâìè ìàññèâ ñ ñîäåðæèìûì òàáëèöû
for(int i=0;i<20;i++){
for(int j=0;j<2;j++){
Content[i][j]="Êîëîíêà "+(j+1)+", ñòðîêà "+(i+1);
}
}
// Ðèñóåì òàáëèöó
fObjLableTable("Table_a",Header,ColWidth,Content,5,10,10,12,Silver,Navy,true,Gainsboro,Black);
fObjLableTable("Table_b",Header,ColWidth,Content,45,110,10,10,Red,Yellow,true,Lime,Blue);
fObjLableTable("Table_c",Header,ColWidth,Content,125,180,10,8,Black,White,true,White,Black);
fObjLableTable("Table_d",Header,ColWidth,Content,250,230,10,6,Aqua,Navy,true,DodgerBlue,Yellow);
fObjLableTable("Table_e",Header,ColWidth,Content,80,270,10,4,Yellow,Red,true,Khaki,Black);
fObjLableTable("Table_f",Header,ColWidth,Content,30,295,10,2,Blue,Aqua,true,Red,Yellow);
}
void fObjLableTable(
string aName,
string aHeader[], // Èìÿ òàáëèöû
int aColWidth[], // Ìàññèâ ñ øèðèíîé êàæäîé êîëîíêè. Â óñëîâíûõ åäèíèöàõ ñîîòâåòñâóþùèõ ðàçìåðó çíàêà ñ êîäîì 110 øðèôòà Wingdings
string aContent[][], // Ìàññèâ ñ ñîäåðæèìûì òàáëèöû. Ïåðâîå èçìåðåíèå - ñòðîêà, âòîðàÿ - êîëîíêà
int aX = 5, // Êîîðäèíàòà Õ âåðõíåãî ëåâîãî óãëà òàáëèöû
int aY = 10, // Êîîðäèíàòà Y âåðõíåãî ëåâîãî óãëà òàáëèöû
int aRows = 30, // Ðàçìåð òàáëèöû ïî âåðòèêàëè (â ñòðîêàõ). Åñëè â ìàññèâå aContent çàäàíî áîëüøåå êîëè÷åñòâî ñòðîê, òàáëèöà áóäåò ñîñòîÿòü èç íåñêîëüêèõ êîëîíîê
int tTableSize = 4, // Òèïîðàçìåð òàáëèöû 1-12
color aHeaderBackColor = Silver, // Öâåò ôîíà çàãîëîâêà
color aHeaderTextColor = Navy, // Öâåò òåêñòà çàãîëîâêà
bool aHeaderBold = true, // Æèðíûé òåêñò çàãîëîâêà
color aContentBackColor = Gainsboro, // Ôîí ñîäåðæèìîãî òàáëèöû
color aContentTextColor = Black // Öâåò òåêñòà ñîäåðæèìîãî òàáëèöû
){
/*
© Integer 2009, for-good-letters@yandex.ru
*/
int tFontSize;
int tBoxFontSize;
int tBoxWidth;
int tTextXMargin;
int tTextYMargin;
int tHeaderCorrector;
switch(tTableSize){
case 1: tFontSize=5; tBoxFontSize=10; tBoxWidth=8; tTextXMargin=5; tTextYMargin=4; tHeaderCorrector=-1; break;
case 2: tFontSize=6; tBoxFontSize=14; tBoxWidth=10; tTextXMargin=6; tTextYMargin=5; tHeaderCorrector=-1; break;
case 3: tFontSize=7; tBoxFontSize=16; tBoxWidth=12; tTextXMargin=6; tTextYMargin=6; tHeaderCorrector=-1; break;
case 4: tFontSize=8; tBoxFontSize=18; tBoxWidth=14; tTextXMargin=6; tTextYMargin=6; tHeaderCorrector=-1; break;
case 5: tFontSize=10; tBoxFontSize=22; tBoxWidth=17; tTextXMargin=8; tTextYMargin=8; tHeaderCorrector=-1; break;
case 6: tFontSize=12; tBoxFontSize=26; tBoxWidth=20; tTextXMargin=9; tTextYMargin=9; tHeaderCorrector=-2; break;
case 7: tFontSize=14; tBoxFontSize=32; tBoxWidth=24; tTextXMargin=12; tTextYMargin=12; tHeaderCorrector=-2; break;
case 8: tFontSize=16; tBoxFontSize=36; tBoxWidth=28; tTextXMargin=15; tTextYMargin=15; tHeaderCorrector=-3; break;
case 9: tFontSize=18; tBoxFontSize=42; tBoxWidth=32; tTextXMargin=18; tTextYMargin=18; tHeaderCorrector=-4; break;
case 10: tFontSize=20; tBoxFontSize=46; tBoxWidth=35; tTextXMargin=18; tTextYMargin=18; tHeaderCorrector=-4; break;
case 11: tFontSize=22; tBoxFontSize=48; tBoxWidth=37; tTextXMargin=18; tTextYMargin=18; tHeaderCorrector=-4; break;
case 12: tFontSize=24; tBoxFontSize=52; tBoxWidth=40; tTextXMargin=21; tTextYMargin=21; tHeaderCorrector=-6; break;
}
//-----------------------------------------------------------------------------------------------
int tCollXPOsAdd_1=0;
int tCollXPOsAdd_2=0;
int tCollXPOsAdd_3=0;
string tHeaderFont="Arial";
int tHeaderYAdd=0;
if(aHeaderBold){
tHeaderFont=tHeaderFont+" Black";
tHeaderYAdd=tHeaderCorrector;
}
int tBoxHeight=tBoxWidth+1;
int tHeaderCounter=0;
int tRowCounter=0;
int tWindowNumber=0;
for(int tk=0;tk<ArraySize(aContent)/ArraySize(aHeader);tk++){
if(tRowCounter==0){
tCollXPOsAdd_1=0;
for(int ti=0;ti<ArraySize(aHeader);ti++){
for(int tj=0;tj<aColWidth[ti];tj++){
string tName=aName+"_hb_"+tHeaderCounter+"_"+ti+"_"+tj;
fObjLabel(tName,aX+tCollXPOsAdd_2+tCollXPOsAdd_1+tj*tBoxWidth,aY,"n",0,aHeaderBackColor,tBoxFontSize,0,"WingDings",false);
}
fObjLabel(aName+"_ht_"+tHeaderCounter+"_"+ti,aX+tCollXPOsAdd_2+tCollXPOsAdd_1+tTextXMargin,aY+tTextYMargin+tHeaderYAdd,aHeader[ti],0,aHeaderTextColor,tFontSize,0,tHeaderFont,false);
tCollXPOsAdd_1+=aColWidth[ti]*tBoxWidth+1;
}
tCollXPOsAdd_3=tCollXPOsAdd_2;
tCollXPOsAdd_2+=tCollXPOsAdd_1+3;
tHeaderCounter++;
}
tCollXPOsAdd_1=0;
for(ti=0;ti<ArraySize(aHeader);ti++){
for(tj=0;tj<aColWidth[ti];tj++){
fObjLabel(aName+"_cb_"+tHeaderCounter+"_"+tRowCounter+"_"+ti+"_"+tj,aX+tCollXPOsAdd_3+tCollXPOsAdd_1+tj*tBoxWidth,aY+tRowCounter*tBoxHeight+tBoxHeight,"n",0,aContentBackColor,tBoxFontSize,0,"WingDings",false);
}
fObjLabel(aName+"_ct_"+tHeaderCounter+"_"+tRowCounter+"_"+ti,aX+tCollXPOsAdd_3+tCollXPOsAdd_1+tTextXMargin,aY+tTextYMargin+tRowCounter*tBoxHeight+tBoxHeight,aContent[tk][ti],0,aContentTextColor,tFontSize,0,"Arial",false);
tCollXPOsAdd_1+=aColWidth[ti]*tBoxWidth+1;
}
tRowCounter++;
if(tRowCounter>=aRows){
tRowCounter=0;
}
}
WindowRedraw();
}
void fObjLabel(
string aObjectName, // 1 èìÿ
int aX, // 2 õ
int aY, // 3 ó
string aText, // 4 òåêñò
int aCorner = 0, // 5 óãîë 0 1
// 2 3
color aColor = Red, // 6 öâåò
int aFontSize = 8, // 7 ðàçìåð øðèôòà
int aWindowNumber = 0, // 8 îêíî
string aFont = "Arial", // 9 øðèôò
bool aBack = false // 10 ôîí
){
/*
© Integer 2009, for-good-letters@yandex.ru
*/
if(ObjectFind(aObjectName)!=aWindowNumber){
ObjectCreate(aObjectName,OBJ_LABEL,aWindowNumber,0,0);
}
ObjectSet(aObjectName,OBJPROP_XDISTANCE,aX);
ObjectSet(aObjectName,OBJPROP_YDISTANCE,aY);
ObjectSetText(aObjectName,aText,aFontSize,aFont,aColor);
ObjectSet(aObjectName,OBJPROP_BACK,aBack);
ObjectSet(aObjectName,OBJPROP_CORNER,aCorner);
}
Comments