一、MQL语言中Comment函数的缺陷
- Comment函数只能在图表上显示文本信息,无法将信息输出到日志文件或其他位置。
- Comment函数的文本信息会随着新的新的信息出现而更新,这可能导致就的信息被覆盖。
- Comment函数的文本信息长度有限,超过一定长度的文本将被截断显示。
- Comment函数的文本信息无法进行格式化,例如无法设置字体、颜色或其他样式。
二、新增Comment函数的功能
- 可以记忆历史信息,不被新的信息而覆盖。
- 可以控制显示的行数,便于实现屏幕监控日志的功能。
- 新增加的信息,才能增加行的方式显示,可以正向也可以反向。
三、扩展Comment函数功能的代码实现
// 默认最大显示行数为10行
#ifndef N_LINES
#define N_LINES 10
#define N_LINES_DEFINED
#endif//+------------------------------------------------------------------+
//| Multiline comment storage and display tool |
//+------------------------------------------------------------------+
class Comments
{const int capacity; // maximum number of linesconst bool reverse; // order of display (true means recents on top)string lines[]; // text bufferint cursor; // where to place next stringint size; // actual number of lines storedpublic:Comments(const int limit = N_LINES, const bool r = false):capacity(limit), reverse(r), cursor(0), size(0){ArrayResize(lines, capacity);}void add(const string line);void clear();
};//+------------------------------------------------------------------+
//| Clean up the chart comment and internal buffer |
//+------------------------------------------------------------------+
void Comments::clear()
{Comment("");cursor = 0;size = 0;
}//+------------------------------------------------------------------+
//| Add new line(s) of text to the chart comment |
//+------------------------------------------------------------------+
void Comments::add(const string line)
{if(line == NULL){clear();return;}// if input string contains several lines// split it by newline character into arraystring inputs[];const int n = StringSplit(line, '\n', inputs);// add new line(s) into the ring buffer// at the cursor position (overwriting most outdated records)// cursor is always incremented modulo capacity (reset to 0 on overflow)for(int i = 0; i < n; ++i){lines[cursor] = inputs[reverse ? n - i - 1 : i];cursor = (cursor + 1) % capacity;if(size < capacity) ++size;}// combine all text from the buffer in direct or reverse order// newline character is used as a gluestring result = "";for(int i = 0, k = size == capacity ? cursor % capacity : 0; i < size; ++i, k = ++k % capacity){if(reverse){result = lines[k] + "\n" + result;}else{result += lines[k] + "\n";}}// finally output the resultComment(result);
}//+------------------------------------------------------------------+
//| Continuous comment feed will show most recent posts on top |
//| in reverse chronological order |
//+------------------------------------------------------------------+
void MultiComment(const string line = NULL)
{static Comments com(N_LINES, true);com.add(line);
}//+------------------------------------------------------------------+
//| Bulk posts with multiple lines are better to show in natural |
//| chronological order (full story reading goes from top to bottom) |
//+------------------------------------------------------------------+
void ChronoComment(const string line = NULL)
{static Comments com(N_LINES, false);com.add(line);
}#ifdef N_LINES_DEFINED
#undef N_LINES
#endif
//+------------------------------------------------------------------+