DoEasy 库中的其他类(第六十八部分):图表窗口对象类和图表窗口中的指标对象类·进阶篇
(2/3)·从主图到子窗口的指标挂载逻辑,多数自建库卡在窗口属性枚举这一步
◍ 图表整数属性的枚举边界
MT5 图表对象的整数类属性在底层以枚举值管理,从 K 线颜色到浮动窗口坐标都在同一组常量里。上面列出的片段覆盖了多头/空头发线色、Bid/Ask/Last 价线色、止损止盈线色,以及交易关卡显示、拖拽开关、时间/价格轴显示、一键交易面板和窗口最大化/最小化/停靠状态等。
脱离停靠状态的图表窗口用虚拟屏幕左、上、右、下四个坐标描述位置,这部分对多屏布局的自动化调整有直接意义。
编译器层面用宏把整数属性总数钉死为 67:#define CHART_PROP_INTEGER_TOTAL (67)。开 MT5 的 MetaEditor 搜这个宏,能确认当前终端版本的整数属性枚举上限,写自定义图表管理 EA 时遍历属性别越界。
CHART_PROP_COLOR_CANDLE_BULL, class=class="str">"cmt">// Color of body of a bullish candlestick CHART_PROP_COLOR_CANDLE_BEAR, class=class="str">"cmt">// Color of body of a bearish candlestick CHART_PROP_COLOR_BID, class=class="str">"cmt">// Color of the Bid price line CHART_PROP_COLOR_ASK, class=class="str">"cmt">// Color of the Ask price line CHART_PROP_COLOR_LAST, class=class="str">"cmt">// Color of the last performed deal&class="macro">#x27;s price line(Last) CHART_PROP_COLOR_STOP_LEVEL, class=class="str">"cmt">// Color of stop order levels(Stop Loss and Take Profit) CHART_PROP_SHOW_TRADE_LEVELS, class=class="str">"cmt">// Display trade levels on the chart(levels of open positions, Stop Loss, Take Profit and pending orders) CHART_PROP_DRAG_TRADE_LEVELS, class=class="str">"cmt">// Enable the ability to drag trading levels on a chart class="kw">using mouse CHART_PROP_SHOW_DATE_SCALE, class=class="str">"cmt">// Display the time scale on a chart CHART_PROP_SHOW_PRICE_SCALE, class=class="str">"cmt">// Display a price scale on a chart CHART_PROP_SHOW_ONE_CLICK, class=class="str">"cmt">// Display the quick trading panel on the chart CHART_PROP_IS_MAXIMIZED, class=class="str">"cmt">// Chart window maximized CHART_PROP_IS_MINIMIZED, class=class="str">"cmt">// Chart window minimized CHART_PROP_IS_DOCKED, class=class="str">"cmt">// Chart window docked CHART_PROP_FLOAT_LEFT, class=class="str">"cmt">// Left coordinate of the undocked chart window relative to the class="kw">virtual screen CHART_PROP_FLOAT_TOP, class=class="str">"cmt">// Upper coordinate of the undocked chart window relative to the class="kw">virtual screen CHART_PROP_FLOAT_RIGHT, class=class="str">"cmt">// Right coordinate of the undocked chart window relative to the class="kw">virtual screen CHART_PROP_FLOAT_BOTTOM, class=class="str">"cmt">// Bottom coordinate of the undocked chart window relative to the class="kw">virtual screen class=class="str">"cmt">//--- CWndInd CHART_PROP_WINDOW_IND_HANDLE, class=class="str">"cmt">// Indicator handle in the chart window CHART_PROP_WINDOW_IND_INDEX, class=class="str">"cmt">// Indicator index in the chart window }; class="macro">#define CHART_PROP_INTEGER_TOTAL(class="num">67) class=class="str">"cmt">// Total number of integer properties
「给图表属性做枚举分层」
在 MT5 里想批量管理多图表,先得把图表属性按类型拆开。整数类、双精度类、字符串类各成一族,才能后续做排序或遍历。 下面这段定义把双精度属性单独列了 7 个:从零柱偏移百分比、固定左右位置,到固定最大最小值、每根 K 线点数缩放、图表价格上下限,总数由 CHART_PROP_DOUBLE_TOTAL 标记为 7。整数和双精度跳过数都设为 0,意味着全部属性都参与排序候选。 字符串属性在整数与双精度总数之后接续编号,共 5 个:图表注释、EA 名、脚本名、指标名、图表品种。CHART_PROP_STRING_TOTAL 写死为 5,想加自定义字段就得改这里和枚举体。 排序起点用宏算出来:FIRST_CHART_DBL_PROP 从整数总数减跳过数起步,FIRST_CHART_STR_PROP 再叠双精度总数。开 MT5 把这几个宏打印出来,能直接看清属性索引排布。
class="macro">#define CHART_PROP_INTEGER_SKIP(class="num">0) class=class="str">"cmt">// Number of integer DOM properties not used in sorting class=class="str">"cmt">//+------------------------------------------------------------------+ class=class="str">"cmt">//| Chart real properties | class=class="str">"cmt">//+------------------------------------------------------------------+ enum ENUM_CHART_PROP_DOUBLE { CHART_PROP_SHIFT_SIZE = CHART_PROP_INTEGER_TOTAL, class=class="str">"cmt">// Shift size of the zero bar from the right border in % CHART_PROP_FIXED_POSITION, class=class="str">"cmt">// Chart fixed position from the left border in % CHART_PROP_FIXED_MAX, class=class="str">"cmt">// Chart fixed maximum CHART_PROP_FIXED_MIN, class=class="str">"cmt">// Chart fixed minimum CHART_PROP_POINTS_PER_BAR, class=class="str">"cmt">// Scale in points per bar CHART_PROP_PRICE_MIN, class=class="str">"cmt">// Chart minimum CHART_PROP_PRICE_MAX, class=class="str">"cmt">// Chart maximum }; class="macro">#define CHART_PROP_DOUBLE_TOTAL(class="num">7) class=class="str">"cmt">// Total number of real properties class="macro">#define CHART_PROP_DOUBLE_SKIP(class="num">0) class=class="str">"cmt">// Number of real properties not used in sorting class=class="str">"cmt">//+------------------------------------------------------------------+ class=class="str">"cmt">//| Chart class="type">class="kw">string properties | class=class="str">"cmt">//+------------------------------------------------------------------+ enum ENUM_CHART_PROP_STRING { CHART_PROP_COMMENT = (CHART_PROP_INTEGER_TOTAL+CHART_PROP_DOUBLE_TOTAL), class=class="str">"cmt">// Chart comment text CHART_PROP_EXPERT_NAME, class=class="str">"cmt">// Name of an EA launched on the chart CHART_PROP_SCRIPT_NAME, class=class="str">"cmt">// Name of a script launched on the chart CHART_PROP_INDICATOR_NAME, class=class="str">"cmt">// Name of an indicator launched on the chart CHART_PROP_SYMBOL, class=class="str">"cmt">// Chart symbol }; class="macro">#define CHART_PROP_STRING_TOTAL(class="num">5) class=class="str">"cmt">// Total number of class="type">class="kw">string properties class=class="str">"cmt">//+------------------------------------------------------------------+ class=class="str">"cmt">//+------------------------------------------------------------------+ class=class="str">"cmt">//| Possible chart sorting criteria | class=class="str">"cmt">//+------------------------------------------------------------------+ class="macro">#define FIRST_CHART_DBL_PROP(CHART_PROP_INTEGER_TOTAL-CHART_PROP_INTEGER_SKIP) class="macro">#define FIRST_CHART_STR_PROP(CHART_PROP_INTEGER_TOTAL-CHART_PROP_INTEGER_SKIP+CHART_PROP_DOUBLE_TOTAL-CHART_PROP_DOUBLE_SKIP) enum ENUM_SORT_CHART_MODE
图表整数属性排序枚举怎么用
MT5 的图表对象排序接口里,有一组以 SORT_BY_CHART_ 开头的枚举,专门按图表的整数型属性做索引和遍历。从 SORT_BY_CHART_SHOW = 0 开始,后续每个枚举值对应一种图表状态或交互开关,比如是否显示价格图、是否为 OBJ_CHART 对象、是否置顶、是否禁用右键菜单等。 这些枚举直接喂给 ChartGetInteger / ChartSetInteger 类调用时的排序参数,就能批量筛出符合某种交互配置的图表。例如用 SORT_BY_CHART_MOUSE_SCROLL 可区分哪些图表允许左键拖动横向滚动,用 SORT_BY_CHART_SCALEFIX_11 可锁定 1:1 比例显示的图表。 外汇和贵金属图表多周期并行时,靠这类枚举做程序化分类,能减少手动翻图。注意 MT5 图表属性受终端版本影响,部分枚举在老版本可能未实现,开 MT5 用 MetaEditor 跳转到 chartdefineenum.mqh 核对当前版本列表再写循环。
{
class=class="str">"cmt">//--- Sort by integer properties
SORT_BY_CHART_SHOW = class="num">0, class=class="str">"cmt">// Sort by the price chart drawing attribute
SORT_BY_CHART_IS_OBJECT, class=class="str">"cmt">// Sort by chart object(OBJ_CHART) identification attribute
SORT_BY_CHART_BRING_TO_TOP, class=class="str">"cmt">// Sort by the flag of displaying a chart above all others
SORT_BY_CHART_CONTEXT_MENU, class=class="str">"cmt">// Sort by the flag of enabling/disabling access to the context menu class="kw">using the right click
SORT_BY_CHART_CROSSHAIR_TOO, class=class="str">"cmt">// Sort by the flag of enabling/disabling access to the Crosshair tool class="kw">using the middle click
SORT_BY_CHART_MOUSE_SCROLL, class=class="str">"cmt">// Sort by the flag of scrolling the chart horizontally class="kw">using the left mouse button
SORT_BY_CHART_EVENT_MOUSE_WHEEL, class=class="str">"cmt">// Sort by the flag of sending messages about mouse wheel events to all MQL5 programs on a chart
SORT_BY_CHART_EVENT_MOUSE_MOVE, class=class="str">"cmt">// Sort by the flag of sending messages about mouse button click and movement events to all MQL5 programs on a chart
SORT_BY_CHART_EVENT_OBJECT_CREATE, class=class="str">"cmt">// Sort by the flag of sending messages about the graphical object creation event to all MQL5 programs on a chart
SORT_BY_CHART_EVENT_OBJECT_DELETE, class=class="str">"cmt">// Sort by the flag of sending messages about the graphical object destruction event to all MQL5 programs on a chart
SORT_BY_CHART_MODE, class=class="str">"cmt">// Sort by chart type
SORT_BY_CHART_FOREGROUND, class=class="str">"cmt">// Sort by the "Price chart in the foreground" flag
SORT_BY_CHART_SHIFT, class=class="str">"cmt">// Sort by the "Mode of shift of the price chart from the right border" flag
SORT_BY_CHART_AUTOSCROLL, class=class="str">"cmt">// Sort by the "The mode of automatic shift to the right border of the chart" flag
SORT_BY_CHART_KEYBOARD_CONTROL, class=class="str">"cmt">// Sort by the flag allowing the chart management class="kw">using a keyboard
SORT_BY_CHART_QUICK_NAVIGATION, class=class="str">"cmt">// Sort by the flag allowing the chart to intercept Space and Enter key strokes to activate the quick navigation bar
SORT_BY_CHART_SCALE, class=class="str">"cmt">// Sort by scale
SORT_BY_CHART_SCALEFIX, class=class="str">"cmt">// Sort by the fixed scale flag
SORT_BY_CHART_SCALEFIX_11, class=class="str">"cmt">// Sort by the class="num">1:class="num">1 scale flag
SORT_BY_CHART_SCALE_PT_PER_BAR class=class="str">"cmt">// Sort by the flag of specifying the scale in points per bar
}◍ 图表属性枚举的排序维度拆解
在 MT5 的图表对象枚举体系里,有一组以 SORT_BY_CHART_ 开头的常量,专门用来按图表可视属性做排序或检索。它们覆盖了从左上角符号标签、OHLC 显示,到 Bid/Ask/Last 水平线,再到周期分隔符与网格的开关状态。 往下看还能发现更细的维度:成交量显示模式、对象文字描述开关、可见 K 线数量、子窗口总数与可见性、子窗口句柄,以及子窗口相对主窗口上边框的 Y 轴像素距离。 尺寸类属性也被纳入了同一组枚举,包括首根可见 K 线序号、以 K 线数和像素为单位的图表宽高,还有背景、前景(坐标轴/刻度/OHLC 线)、网格、成交量及开仓位的配色。开 MT5 在 MetaEditor 里搜 SORT_BY_CHART_ 前缀,能直接看到这组 27 个常量,验证你当前终端版本是否缺了某几个。 别把枚举当装饰 这些常量不是给肉眼读的,而是喂给 ChartGetInteger/ChartSetInteger 类调用做批量图表管理的。写多图表监控 EA 时,用 SORT_BY_CHART_VISIBLE_BARS 和 SORT_BY_CHART_WINDOWS_TOTAL 做过滤,比硬遍历省事得多。外汇与贵金属图表多窗口联动本身不保本,参数误用可能导致子窗口布局错乱。
SORT_BY_CHART_SHOW_TICKER, class=class="str">"cmt">// Sort by the flag displaying a symbol ticker in the upper left corner SORT_BY_CHART_SHOW_OHLC, class=class="str">"cmt">// Sort by the flag displaying OHLC values in the upper left corner SORT_BY_CHART_SHOW_BID_LINE, class=class="str">"cmt">// Sort by the flag displaying Bid value as a horizontal line on the chart SORT_BY_CHART_SHOW_ASK_LINE, class=class="str">"cmt">// Sort by the flag displaying Ask value as a horizontal line on the chart SORT_BY_CHART_SHOW_LAST_LINE, class=class="str">"cmt">// Sort by the flag displaying Last value as a horizontal line on the chart SORT_BY_CHART_SHOW_PERIOD_SEP, class=class="str">"cmt">// Sort by the flag displaying vertical separators between adjacent periods SORT_BY_CHART_SHOW_GRID, class=class="str">"cmt">// Sort by the flag of displaying a grid on the chart SORT_BY_CHART_SHOW_VOLUMES, class=class="str">"cmt">// Sort by the mode of displaying volumes on a chart SORT_BY_CHART_SHOW_OBJECT_DESCR, class=class="str">"cmt">// Sort by the flag of displaying object text descriptions SORT_BY_CHART_VISIBLE_BARS, class=class="str">"cmt">// Sort by the number of bars on a chart that are available for display SORT_BY_CHART_WINDOWS_TOTAL, class=class="str">"cmt">// Sort by the total number of chart windows including indicator subwindows SORT_BY_CHART_WINDOW_IS_VISIBLE, class=class="str">"cmt">// Sort by the subwindow visibility flag SORT_BY_CHART_WINDOW_HANDLE, class=class="str">"cmt">// Sort by the chart handle SORT_BY_CHART_WINDOW_YDISTANCE, class=class="str">"cmt">// Sort by the distance in Y axis pixels between the upper frame of the indicator subwindow and the upper frame of the chart main window SORT_BY_CHART_FIRST_VISIBLE_BAR, class=class="str">"cmt">// Sort by the number of the first visible bar on the chart SORT_BY_CHART_WIDTH_IN_BARS, class=class="str">"cmt">// Sort by the width of the chart in bars SORT_BY_CHART_WIDTH_IN_PIXELS, class=class="str">"cmt">// Sort by the width of the chart in pixels SORT_BY_CHART_HEIGHT_IN_PIXELS, class=class="str">"cmt">// Sort by the height of the chart in pixels SORT_BY_CHART_COLOR_BACKGROUND, class=class="str">"cmt">// Sort by the class="type">class="kw">color of the chart background SORT_BY_CHART_COLOR_FOREGROUND, class=class="str">"cmt">// Sort by class="type">class="kw">color of axes, scale and OHLC line SORT_BY_CHART_COLOR_GRID, class=class="str">"cmt">// Sort by grid class="type">class="kw">color SORT_BY_CHART_COLOR_VOLUME class=class="str">"cmt">// Sort by the class="type">class="kw">color of volumes and position opening levels
「图表属性枚举的排序锚点」
MT5 的图表对象排序接口里,有一组以 SORT_BY_CHART_ 开头的枚举值,专门用来按图表的视觉与状态属性做检索或排列。上面列出的片段从 K 线颜色一直覆盖到悬浮窗口坐标,说明终端把图表几乎所有可观测维度都暴露成了可编程字段。 比如 SORT_BY_CHART_COLOR_CANDLE_BULL 对应阳线实体颜色,SORT_BY_CHART_COLOR_CANDLE_BEAR 对应阴线实体颜色;而 SORT_BY_CHART_FLOAT_LEFT / TOP / RIGHT / BOTTOM 四个值则锁定未停靠窗口相对虚拟屏幕的四至坐标。 实际写 EA 或脚本时,若想批量整理多图表布局,直接引用这些枚举比手动遍历属性更高效。外汇与贵金属市场波动剧烈、杠杆风险高,用代码管理图表也别忽视滑点与断连可能。 最后那行被高亮的 SORT_BY_CHART_WINDOW_IND_HANDLE 值得单独看:它按图表窗口内指标的句柄排序,意味着你可以绕过图表名直接定位某个指标实例所在的窗口。
SORT_BY_CHART_COLOR_CHART_UP, class=class="str">"cmt">// Sort by the class="type">class="kw">color for the up bar, shadows and body borders of bull candlesticks SORT_BY_CHART_COLOR_CHART_DOWN, class=class="str">"cmt">// Sort by the class="type">class="kw">color of down bar, its shadow and border of body of the bullish candlestick SORT_BY_CHART_COLOR_CHART_LINE, class=class="str">"cmt">// Sort by the class="type">class="kw">color of the chart line and the Doji candlesticks SORT_BY_CHART_COLOR_CANDLE_BULL, class=class="str">"cmt">// Sort by the class="type">class="kw">color of a bullish candlestick body SORT_BY_CHART_COLOR_CANDLE_BEAR, class=class="str">"cmt">// Sort by the class="type">class="kw">color of a bearish candlestick body SORT_BY_CHART_COLOR_BID, class=class="str">"cmt">// Sort by the class="type">class="kw">color of the Bid price line SORT_BY_CHART_COLOR_ASK, class=class="str">"cmt">// Sort by the class="type">class="kw">color of the Ask price line SORT_BY_CHART_COLOR_LAST, class=class="str">"cmt">// Sort by the class="type">class="kw">color of the last performed deal&class="macro">#x27;s price line(Last) SORT_BY_CHART_COLOR_STOP_LEVEL, class=class="str">"cmt">// Sort by the class="type">class="kw">color of stop order levels(Stop Loss and Take Profit) SORT_BY_CHART_SHOW_TRADE_LEVELS, class=class="str">"cmt">// Sort by the flag of displaying trading levels on the chart SORT_BY_CHART_DRAG_TRADE_LEVELS, class=class="str">"cmt">// Sort by the flag enabling the ability to drag trading levels on a chart class="kw">using mouse SORT_BY_CHART_SHOW_DATE_SCALE, class=class="str">"cmt">// Sort by the flag of displaying the time scale on the chart SORT_BY_CHART_SHOW_PRICE_SCALE, class=class="str">"cmt">// Sort by the flag of displaying the price scale on the chart SORT_BY_CHART_SHOW_ONE_CLICK, class=class="str">"cmt">// Sort by the flag of displaying the quick trading panel on the chart SORT_BY_CHART_IS_MAXIMIZED, class=class="str">"cmt">// Sort by the "Chart window maximized" flag SORT_BY_CHART_IS_MINIMIZED, class=class="str">"cmt">// Sort by the "Chart window minimized" flag SORT_BY_CHART_IS_DOCKED, class=class="str">"cmt">// Sort by the "Chart window docked" flag SORT_BY_CHART_FLOAT_LEFT, class=class="str">"cmt">// Sort by the left coordinate of the undocked chart window relative to the class="kw">virtual screen SORT_BY_CHART_FLOAT_TOP, class=class="str">"cmt">// Sort by the upper coordinate of the undocked chart window relative to the class="kw">virtual screen SORT_BY_CHART_FLOAT_RIGHT, class=class="str">"cmt">// Sort by the right coordinate of the undocked chart window relative to the class="kw">virtual screen SORT_BY_CHART_FLOAT_BOTTOM, class=class="str">"cmt">// Sort by the bottom coordinate of the undocked chart window relative to the class="kw">virtual screen SORT_BY_CHART_WINDOW_IND_HANDLE, class=class="str">"cmt">// Sort by the indicator handle in the chart window
图表排序枚举里的真实属性与字符串键
上面这段枚举收尾定义了 Charts 集合按图表属性排序时的全部取值,从指标索引一路铺到字符串类属性。其中 SORT_BY_CHART_WINDOW_IND_INDEX 负责按子窗口中指标的序号排,SORT_BY_CHART_SYMBOL 与 SORT_BY_CHART_INDICATOR_NAME 则直接吃图表上的品种名和指标名做键值。 实数类排序从 FIRST_CHART_DBL_PROP 起算:SORT_BY_CHART_SHIFT_SIZE 用右边界零柱偏移百分比,SORT_BY_CHART_POINTS_PER_BAR 是每根 K 线的点数缩放值,SORT_BY_CHART_PRICE_MIN/MAX 抓可见区间的实时高低。这些字段若填了固定值,排序就会优先用固定边界而非自动边界。 字符串类从 FIRST_CHART_STR_PROP 开始,覆盖 COMMENT、EXPERT_NAME、SCRIPT_NAME 等运行体标识。实盘里若同时开了 12 个 EURUSD 图表挂不同 EA,用 SORT_BY_CHART_EXPERT_NAME 就能在脚本里把它们归堆,再批量调参数——外汇与贵金属杠杆高,批量操作前先在策略测试器跑一遍隔离验证。 ChartWnd.mqh 头部带 #property strict,说明这套封装兼顾 MQL4 迁移;它 include 了 ..\..\Objects\BaseObj.mqh,意味着图表对象继承自统一基类。打开 MT5 导航器翻到 Include 目录核对路径,能确认你本地标准库是否缺这一层。
SORT_BY_CHART_WINDOW_IND_INDEX, class=class="str">"cmt">// Sort by the indicator index in the chart window class=class="str">"cmt">//--- Sort by real properties SORT_BY_CHART_SHIFT_SIZE = FIRST_CHART_DBL_PROP, class=class="str">"cmt">// Sort by the shift size of the zero bar from the right border in % SORT_BY_CHART_FIXED_POSITION, class=class="str">"cmt">// Sort by the chart fixed position from the left border in % SORT_BY_CHART_FIXED_MAX, class=class="str">"cmt">// Sort by the fixed chart maximum SORT_BY_CHART_FIXED_MIN, class=class="str">"cmt">// Sort by the fixed chart minimum SORT_BY_CHART_POINTS_PER_BAR, class=class="str">"cmt">// Sort by the scale value in points per bar SORT_BY_CHART_PRICE_MIN, class=class="str">"cmt">// Sort by the chart minimum SORT_BY_CHART_PRICE_MAX, class=class="str">"cmt">// Sort by the chart maximum class=class="str">"cmt">//--- Sort by class="type">class="kw">string properties SORT_BY_CHART_COMMENT = FIRST_CHART_STR_PROP, class=class="str">"cmt">// Sort by a comment text on the chart SORT_BY_CHART_EXPERT_NAME, class=class="str">"cmt">// Sort by a name of an EA launched on the chart SORT_BY_CHART_SCRIPT_NAME, class=class="str">"cmt">// Sort by a name of a script launched on the chart SORT_BY_CHART_INDICATOR_NAME, class=class="str">"cmt">// Sort by a name of an indicator launched on the chart SORT_BY_CHART_SYMBOL, class=class="str">"cmt">// Sort by chart symbol };