comment2
Download scriptThis script is designed to enhance the way information is displayed on a trading chart within the MetaTrader platform. While the standard software provides basic ways to show notes, this tool creates a more structured and persistent "bulletin board" system that stores multiple lines of information in a dedicated data file.
Core Functionality
The primary purpose of this script is to manage a multi-line list of comments. Instead of simply overwriting a single piece of text every time a new update occurs, this logic allows the system to maintain a growing list of messages. It organizes these messages based on a specific "line number," allowing for organized data display without erasing previous entries.
How it Works Internally
The logic follows a four-step process to ensure your information is displayed correctly:
1. Data Retrieval and Organization When the script runs, it automatically identifies the correct data file based on the specific currency pair and timeframe you are currently viewing. It then opens this file and reads all existing lines of text into its active memory. This ensures that old information remains visible while new information is added.
2. Intelligent Text Cleaning To prevent the display from becoming messy, the script includes a "cleaning" phase. It automatically scans any new text you provide and strips out hidden line-break characters. This ensures that each individual entry stays on its own designated row and doesn't break the visual layout of the screen.
3. Dynamic List Management The script treats the information like a numbered list. If you provide a line number that is higher than the current amount of text saved, the script automatically expands the "list" to accommodate the new entry. It then places your new message into the specific slot you requested.
4. Persistent Saving and Rendering Once the new message is inserted into the list, the script saves the entire updated collection back into the data file. Finally, it combines all these saved lines into one cohesive block of text and displays it on your screen simultaneously.
Key Benefits for Users
- Persistence: Information is saved to a file, meaning the notes remain available even if the software is refreshed.
- Multi-Line Display: You can view a series of different messages (like status updates or multiple parameters) in a clean, organized vertical list rather than a single cramped line.
- Context Awareness: Because it generates file names based on the symbol and period, the script ensures that your notes stay relevant to the specific chart you are looking at.
//+------------------------------------------------------------------+
//| Comment2.mq4 |
//| Strator |
//| k-v-p@yandex.ru |
//+------------------------------------------------------------------+
#property copyright "Strator"
#property link "k-v-p@yandex.ru"
#property library
//+------------------------------------------------------------------+
//| Ðàñøèðÿåò âîçìîæíîñòè ñòàíäàðòíîé ôóíêöèè Comment äëÿ âûâîäà |
//| êîììåíòàðèåâ íà ãðàôèê. |
//| Ïîìåùàåò êîììåíòàðèé str_text â çàäàííóþ ñòðîêó str_no íà ýêðàíå.|
//+------------------------------------------------------------------+
int Comment2(string str_text, int str_no)
{
// ìàññèâ ñòðîê
string str[];
// ïîëíîå èìÿ ôàéëà
string file_name = Symbol() + Period() + "_" + "comment.dat";
// ñ÷èòàåì ñòðîêè èç ôàéëà â ìàññèâ ñòðîê
int str_cnt = FileReadToStringArray(file_name, str);
// åñëè ñòðîê â ìàññèâå íåäîñòàòî÷íî, óâåëè÷èì ðàçìåðíîñòü ìàññèâà
if(str_cnt < str_no)
ArrayResize(str, str_no);
// óäàëèì èç íîâîé ñòðîêè ñèìâîëû ïåðåâîäà ñòðîêè, åñëè åñòü
StringDeleteAllSubstr(str_text, "\n");
// ïîìåñòèì â ìàññèâ íîâóþ ñòðîêó
str[str_no-1] = str_text;
// ñîõðàíèì ñòðîêîâûé ìàññèâ â ôàéë
str_cnt = FileWriteFromStringArray(file_name, str);
// îòîáðàçèì ñòðîêè íà ýêðàíå
string str_tmp = "";
for(int i = 0; i < str_cnt; i++)
str_tmp = str_tmp + str[i] + "\n";
Comment(str_tmp);
return(0);
}
//+------------------------------------------------------------------+
//| ÷òåíèå ôàéëà â ìàññèâ òèïà string |
//+------------------------------------------------------------------+
int FileReadToStringArray(string file_name, string& str[])
{
int file_str_cnt = 0;
int file_id = FileOpen(file_name, FILE_CSV|FILE_READ|FILE_WRITE);
if(file_id > 0)
{
//î÷èñòèì ìàññèâ ñòðîê, åñëè îí íå ïóñòîé
if(ArraySize(str)!= 0)
ArrayResize(str, 0);
//çàïîëíèì ìàññèâ çíà÷åíèÿìè èç ôàéëà
while(!FileIsEnding(file_id))
{
file_str_cnt++;
int arr_size = ArrayResize(str, file_str_cnt);
if(arr_size == 0)
break;
str[file_str_cnt-1] = FileReadString(file_id);
}
FileClose(file_id);
}
else
return(-1);
return(file_str_cnt);
}
//+------------------------------------------------------------------+
//| çàïèñü ìàññèâà òèïà string â ôàéë |
//+------------------------------------------------------------------+
int FileWriteFromStringArray(string file_name, string& str[])
{
//êîëè÷åñòâî ñòðîê â ôàéëå
int file_str_cnt = 0;
//äåñêðèïòîð ôàéë
int file_id = FileOpen(file_name, FILE_BIN|FILE_WRITE);
//åñëè ôàéë îòêðûò óñïåøíî, ïðîèçâåñòè çàïèñü
if(file_id > 0)
{
for(int i = 0; i < ArraySize(str); i++)
{
string str_tmp = str[i] + "\n";
int str_len = StringLen(str_tmp);
str_len = FileWriteString(file_id, str_tmp, str_len);
if(str_len >= 0)
file_str_cnt++;
}
FileClose(file_id);
}
else
return(-1);
return(file_str_cnt);
}
//+------------------------------------------------------------------+
//| Óäàëåíèå èç ñòðîêè str âñåõ âõîæäåíèé ïîäñòðîêè substr |
//+------------------------------------------------------------------+
string StringDeleteAllSubstr(string str, string substr)
{
string str_tmp = "";
string str_tmp1 = "";
string str_tmp2 = "";
int pos_substr = StringFind(str, substr);
if(pos_substr >= 0)
{
if(pos_substr != 0)
str_tmp1 = StringSubstr(str, 0, pos_substr);
str_tmp2 = StringSubstr(str, pos_substr + StringLen(substr));
str_tmp = StringDeleteAllSubstr(str_tmp1 + str_tmp2, substr);
}
else
str_tmp = str;
return(str_tmp);
}
//+------------------------------------------------------------------+
Comments