将市场报价中的复数货币对信息用来绘制图表的方式
2023年外汇交易商最新排名更多
由上依序取得市场报价中的货币对名称
在本篇章节中,将说明市场报价中复数货币对的相关处理方式。首先,将在市场报价的显示画面中,由上依序显示各个货币对。
在制作新档案时,选择「自定义指标」,并将文件名命名为「MultiSymbol_demo」。在「自定义指针程序的事件处理程序」画面中,无需勾选「OnTimer」「OnChartEvent」,直接点击「完成」即为雏形。
在一开始,于OnCalculate中使用SymbolName函数,从上方依序取得市场报价中的货币对名称。SymbolName函数的第二个括号内写入「true」,即可从市场报价列表中取得货币对名称。若无法取得货币对名称,则应去除for文体,并先以if文体进行break处理。
for(int i = 0; i < 1000; i++) {
string symbol = SymbolName(i, true);
if (symbol == NULL) break;
}
以Label对象显示取得的货币对名称
接下来,将以Labe对象显示取得的货币对名称;此处会从MQL4帮助文件中,复制Label组件的范例编码并加以运用。
点击MQL4帮助文件目录的「Constants, Enumerations and Structures」→「Objects Constants」→「Object Types」,便会出现对象一览;从中选择「OBJ_LABEL」后,复制预先准备的「Create a text label」编码,并贴至档案下方。
首先,从贴上编码的if文体中,删除不需要的「Print(__FUNCTION__,」以及「: failed to create text label! Error code = “,GetLastError());」2行。
为了将对象的标准位置设定为右边中央,将「// anchor type」处的「anchor = ANCHOR_LEFT_UPPER」变更为「anchor = ANCHOR_RIGHT」。
另外,颜色从「clrRed」改为「clrWhite」,字体则从「font = “Arial”」改为「font = “Arial Bold”」,呈现白色的粗体字。
其他项目则维持原状。
//+——————————————————————+其后,将LabelCreate处理写入于OnCalculate内的if文体下方。参数的间隔指定为X位置「90」、Y位置16像素,因此设定为「20 + i * 16」;标准位置设定为左上方的「CORNER_LEFT_UPPER」、文字则是「symbol」。
//| Create a text label |
//+——————————————————————+
bool LabelCreate(const long chart_ID=0, // chart’s ID
const string name=”Label”, // label name
const int sub_window=0, // subwindow index
const int x=0, // X coordinate
const int y=0, // Y coordinate
const ENUM_BASE_CORNER corner=CORNER_LEFT_UPPER, // chart corner for anchoring
const string text=”Label”, // text
const string font=”Arial Bold”, // font
const int font_size=10, // font size
const color clr=clrWhite, // color
const double angle=0.0, // text slope
const ENUM_ANCHOR_POINT anchor=ANCHOR_RIGHT, // anchor type
const bool back=false, // in the background
const bool selection=false, // highlight to move
const bool hidden=true, // hidden in the object list
const long z_order=0) // priority for mouse click
{
//— reset the error value
ResetLastError();
//— create a text label
if(!ObjectCreate(chart_ID,name,OBJ_LABEL,sub_window,0,0))
{
return(false);
}
//— set label coordinates
ObjectSetInteger(chart_ID,name,OBJPROP_XDISTANCE,x);
ObjectSetInteger(chart_ID,name,OBJPROP_YDISTANCE,y);
//— set the chart’s corner, relative to which point coordinates are defined
ObjectSetInteger(chart_ID,name,OBJPROP_CORNER,corner);
//— set the text
ObjectSetString(chart_ID,name,OBJPROP_TEXT,text);
//— set text font
ObjectSetString(chart_ID,name,OBJPROP_FONT,font);
//— set font size
ObjectSetInteger(chart_ID,name,OBJPROP_FONTSIZE,font_size);
//— set the slope angle of the text
ObjectSetDouble(chart_ID,name,OBJPROP_ANGLE,angle);
//— set anchor type
ObjectSetInteger(chart_ID,name,OBJPROP_ANCHOR,anchor);
//— set color
ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr);
//— display in the foreground (false) or background (true)
ObjectSetInteger(chart_ID,name,OBJPROP_BACK,back);
//— enable (true) or disable (false) the mode of moving the label by mouse
ObjectSetInteger(chart_ID,name,OBJPROP_SELECTABLE,selection);
ObjectSetInteger(chart_ID,name,OBJPROP_SELECTED,selection);
//— hide (true) or display (false) graphical object name in the object list
ObjectSetInteger(chart_ID,name,OBJPROP_HIDDEN,hidden);
//— set the priority for receiving the event of a mouse click in the chart
ObjectSetInteger(chart_ID,name,OBJPROP_ZORDER,z_order);
//— successful execution
return(true);
}
//+——————————————————————+
此处之下为初期参数、并不会产生任何问题,故可予以省略。
LabelCreate(0, “Symbol” + IntegerToString(i), 0, 90, 20 + i * 16, CORNER_LEFT_UPPER, symbol);如此进行编译并设定图表后,将可在图表的左上方,从上依序显示参考价的货币对名称。
在货币对名称旁显示价格变动的比例
接下来,将计算各个货币对的价格变动量,并制作以条形图显示的工具。
首先,将使用取得指定货币对K线收盘价的iClose函数,并将最新条列的前一条作为标准,藉此计算目前线条的收盘价波动。在「LabelCreate(0, “Symbol” + IntegerToString(i), 0, 90, 20 + i * 16, CORNER_LEFT_UPPER, symbol);」下方新增以下编码。
在执行除法时,若数字为0便会出现错误;为了避免在这样的状况下进行计算处理,使用continue来予以跳过。
double closePre = iClose(symbol, 0, 1);为了能够将对象一次删除,此处应先设定接头辞。在档案上方的属性「property indicator_chart_window」下,使用「#define」来定义「PREFIX」。
if (closePre == 0) continue;
double ratio = (iClose(symbol, 0, 0) – closePre) / closePre * 100;
#define PREFIX MQLInfoString(MQL_PROGRAM_NAME) + “_”其后,在「Custom indicator initialization function」下方设定「Custom indicator deinit function」,以使用ObjectsDeleteAll函数删除每一次的对象。
void OnDeinit(const int reason)如将「PREFIX」添加于显示货币对的LabelCreate括号中,便会在每一次都删除含有「PREFIX」文字的对象。
{
ObjectsDeleteAll(0, PREFIX);
}
LabelCreate(0, PREFIX + “Symbol” + IntegerToString(i), 0, 90, 20 + i * 16, CORNER_LEFT_UPPER, symbol);此处完成之后,在「double ratio = (iClose(symbol, 0, 0) – closePre) / closePre * 100;」下方,添加以数值显示计算结果的编码。沿用显示货币对名称的LabelCreate,并如以下所示加以改写。
LabelCreate(0, PREFIX + “Data” + IntegerToString(i), 0, 230, 20 + i * 16, CORNER_LEFT_UPPER, (string)ratio);如此进行编译并确认图表,将可见到计算结果显示于货币对名称旁。
用RECTANGLE LABEL来显示条形图
本次将以条形图来显示算出的价格波动比例。
由于已不需要价格波动的比例显示,故在「LabelCreate(0, PREFIX + “Data” + IntegerToString(i), 0, 230, 20 + i * 16, CORNER_LEFT_UPPER, (string)ratio);」开头添加「//」予以指定。
接下来将于其下定义条形图的长度。数值方面,使用绝对值的MathAbs函数,并将负数值变更为正数值。由于得出的数值变动比例极小,因此设定为100倍;此处的关键则是将其变换为整数型。
至于条形图的颜色,可先将正数值比例设定为红色、负数值则是蓝色。
// LabelCreate(0, PREFIX + “Data” + IntegerToString(i), 0, 200, 20 + i * 16, CORNER_LEFT_UPPER, (string)ratio);本次将以「RECTANGLE LABEL」来制作条形图,这是以XY坐标指定的长方形对象。
int len = (int)MathAbs(ratio * 100);
color clr = ratio > 0 ? clrRed : clrDodgerBlue;
可从MQL4帮助档中,复制并使用样本编码。点击MQL4帮助文件目录的「Constants, Enumerations and Structures」→「Objects Constants」→「Object Types」后,便可见到物件一览;从中选择「OBJ_RECTANGLE_LABEL」后,复制预先准备的「Create rectangle label」编码,并贴至档案的下方。
首先,从贴上编码的if文体中,删除不需要的「Print(__FUNCTION__,」以及「”: failed to create a rectangle label! Error code = “,GetLastError());」2行;并将此处「// flat border color (Flat)」的「clr = clrRed」变更为「clr = clrWhite」,让颜色改为白色。
若在此结束,当价格出现变动时,条形图的宽度也不会产生变化,因此需在if文体下方新增「ObjectSetInteger(chart_ID, name, OBJPROP_WIDTH, line_width);」,让宽度的部分能够改变。
//+——————————————————————+如此便完成了OBJ_RECTANGLE_LABEL的设定。
//| Create rectangle label |
//+——————————————————————+
bool RectLabelCreate(const long chart_ID=0, // chart’s ID
const string name=”RectLabel”, // label name
const int sub_window=0, // subwindow index
const int x=0, // X coordinate
const int y=0, // Y coordinate
const int width=50, // width
const int height=18, // height
const color back_clr=C’236,233,216′, // background color
const ENUM_BORDER_TYPE border=BORDER_SUNKEN, // border type
const ENUM_BASE_CORNER corner=CORNER_LEFT_UPPER, // chart corner for anchoring
const color clr=clrWhite, // flat border color (Flat)
const ENUM_LINE_STYLE style=STYLE_SOLID, // flat border style
const int line_width=1, // flat border width
const bool back=false, // in the background
const bool selection=false, // highlight to move
const bool hidden=true, // hidden in the object list
const long z_order=0) // priority for mouse click
{
//— reset the error value
ResetLastError();
//— create a rectangle label
if(!ObjectCreate(chart_ID,name,OBJ_RECTANGLE_LABEL,sub_window,0,0)) {
ObjectSetInteger(chart_ID, name, OBJPROP_WIDTH, line_width);
return(false);
}
//— set label coordinates
ObjectSetInteger(chart_ID,name,OBJPROP_XDISTANCE,x);
ObjectSetInteger(chart_ID,name,OBJPROP_YDISTANCE,y);
//— set label size
ObjectSetInteger(chart_ID,name,OBJPROP_XSIZE,width);
ObjectSetInteger(chart_ID,name,OBJPROP_YSIZE,height);
//— set background color
ObjectSetInteger(chart_ID,name,OBJPROP_BGCOLOR,back_clr);
//— set border type
ObjectSetInteger(chart_ID,name,OBJPROP_BORDER_TYPE,border);
//— set the chart’s corner, relative to which point coordinates are defined
ObjectSetInteger(chart_ID,name,OBJPROP_CORNER,corner);
//— set flat border color (in Flat mode)
ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr);
//— set flat border line style
ObjectSetInteger(chart_ID,name,OBJPROP_STYLE,style);
//— set flat border width
ObjectSetInteger(chart_ID,name,OBJPROP_WIDTH,line_width);
//— display in the foreground (false) or background (true)
ObjectSetInteger(chart_ID,name,OBJPROP_BACK,back);
//— enable (true) or disable (false) the mode of moving the label by mouse
ObjectSetInteger(chart_ID,name,OBJPROP_SELECTABLE,selection);
ObjectSetInteger(chart_ID,name,OBJPROP_SELECTED,selection);
//— hide (true) or display (false) graphical object name in the object list
ObjectSetInteger(chart_ID,name,OBJPROP_HIDDEN,hidden);
//— set the priority for receiving the event of a mouse click in the chart
ObjectSetInteger(chart_ID,name,OBJPROP_ZORDER,z_order);
//— successful execution
return(true);
}
制作条形图并绘制于图表上
接下来,将利用此内容来制作条形图。
在OnCalculate函数下执行已设定的RectLabelCreate。对象名称设定为「Graph」、X位置为「95」、Y位置为「14 + i * 16」、宽度为「len」、高度为「12」、阳线颜色为「红」、阴线颜色则是「蓝」。
RectLabelCreate(0, PREFIX + “Graph” + IntegerToString(i), 0, 95, 14 + i * 16, len, 12, clr);进行编译之后,便可见到价格波动的比例从数值变成了条形图。
在提示框中显示价格变动比例
在目前的状况下,虽然仅有市场报价中的货币对信息会显示于图表上,但若在参考价显示中点击右键、选择「全部显示」,便会出现能够显示的所有货币对名称以及价格波动比例条形图。
至于尚未读取的信息,由于会需要处理时间来形成条形图,因此会稍慢才出现。
另外,若将光标移至对象上,便可在提示框中显示价格波动比例的数值。本次的价格波动比例将显示至小数点以下3位数,故在数值后方贴上「%」。在OnCalculate函数中「color clr = ratio > 0 ? clrRed : clrDodgerBlue;」的下方定义「tt」,并于括号最后也新增「tt」作为RectLabelCreate参数。
string tt = DoubleToString(ratio, 3) + “%”;其后,在「Create rectangle label」的「// background color」下方新增以下公式作为「// tooltip」。「\n」的意义即是无初始值。
RectLabelCreate(0, PREFIX + “Graph” + IntegerToString(i), 0, 65, 14 + i * 16, len, 12, clr, tt);
const string tt = “\n”, // tooltip另外,在「ObjectSetInteger(chart_ID, name, OBJPROP_ZORDER, z_order);」下方,使用「ObjectSetString」来新增以下编码。
ObjectSetString(chart_ID, name, OBJPROP_TOOLTIP, tt);如此进行编译,即可确认各个价格变动的比例数值是否会显示于提示框中。
原始码
本次制作的原始码如以下所示。
//+——————————————————————+
//| MultiSymbol_demo.mq4 |
//| Copyright 2022, MetaQuotes Software Corp. |
//| https://www.mql5.com |
//+——————————————————————+
#property copyright “Copyright 2022, MetaQuotes Software Corp.”
#property link “https://www.mql5.com”
#property version “1.00”
#property strict
#property indicator_chart_window
#define PREFIX MQLInfoString(MQL_PROGRAM_NAME) + “_”
//+——————————————————————+
//| Custom indicator initialization function |
//+——————————————————————+
int OnInit()
{
//— indicator buffers mapping
//—
return(INIT_SUCCEEDED);
}
//+——————————————————————+
//| Custom indicator deinit function |
//+——————————————————————+
void OnDeinit(const int reason)
{
ObjectsDeleteAll(0, PREFIX);
}
//+——————————————————————+
//| Custom indicator iteration function |
//+——————————————————————+
int OnCalculate(const int rates_total,
const int prev_calculated,
const datetime &time[],
const double &open[],
const double &high[],
const double &low[],
const double &close[],
const long &tick_volume[],
const long &volume[],
const int &spread[])
{
//—
for(int i = 0; i < 1000; i++) {
string symbol = SymbolName(i, true);
if (symbol == NULL) break;
LabelCreate(0, PREFIX + “Symbol” + IntegerToString(i), 0, 90, 20 + i * 16, CORNER_LEFT_UPPER, symbol);
double closePre = iClose(symbol, 0, 1);
if (closePre == 0) continue;
double ratio = (iClose(symbol, 0, 0) – closePre) / closePre * 100;
// LabelCreate(0, PREFIX + “Data” + IntegerToString(i), 0, 230, 20 + i * 16, CORNER_LEFT_UPPER, (string)ratio);
int len = (int)MathAbs(ratio * 100);
color clr = ratio > 0 ? clrRed : clrDodgerBlue;
string tt = DoubleToString(ratio, 3) + “%”;
RectLabelCreate(0, PREFIX + “Graph” + IntegerToString(i), 0, 95, 14 + i * 16, len, 12, clr, tt);
}
//— return value of prev_calculated for next call
return(rates_total);
}
//+——————————————————————+
//| Create a text label |
//+——————————————————————+
bool LabelCreate(const long chart_ID=0, // chart’s ID
const string name=”Label”, // label name
const int sub_window=0, // subwindow index
const int x=0, // X coordinate
const int y=0, // Y coordinate
const ENUM_BASE_CORNER corner=CORNER_LEFT_UPPER, // chart corner for anchoring
const string text=”Label”, // text
const string font=”Arial Bold”, // font
const int font_size=10, // font size
const color clr=clrWhite, // color
const double angle=0.0, // text slope
const ENUM_ANCHOR_POINT anchor=ANCHOR_RIGHT, // anchor type
const bool back=false, // in the background
const bool selection=false, // highlight to move
const bool hidden=true, // hidden in the object list
const long z_order=0) // priority for mouse click
{
//— reset the error value
ResetLastError();
//— create a text label
if(!ObjectCreate(chart_ID,name,OBJ_LABEL,sub_window,0,0)){
return(false);
}
//— set label coordinates
ObjectSetInteger(chart_ID,name,OBJPROP_XDISTANCE,x);
ObjectSetInteger(chart_ID,name,OBJPROP_YDISTANCE,y);
//— set the chart’s corner, relative to which point coordinates are defined
ObjectSetInteger(chart_ID,name,OBJPROP_CORNER,corner);
//— set the text
ObjectSetString(chart_ID,name,OBJPROP_TEXT,text);
//— set text font
ObjectSetString(chart_ID,name,OBJPROP_FONT,font);
//— set font size
ObjectSetInteger(chart_ID,name,OBJPROP_FONTSIZE,font_size);
//— set the slope angle of the text
ObjectSetDouble(chart_ID,name,OBJPROP_ANGLE,angle);
//— set anchor type
ObjectSetInteger(chart_ID,name,OBJPROP_ANCHOR,anchor);
//— set color
ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr);
//— display in the foreground (false) or background (true)
ObjectSetInteger(chart_ID,name,OBJPROP_BACK,back);
//— enable (true) or disable (false) the mode of moving the label by mouse
ObjectSetInteger(chart_ID,name,OBJPROP_SELECTABLE,selection);
ObjectSetInteger(chart_ID,name,OBJPROP_SELECTED,selection);
//— hide (true) or display (false) graphical object name in the object list
ObjectSetInteger(chart_ID,name,OBJPROP_HIDDEN,hidden);
//— set the priority for receiving the event of a mouse click in the chart
ObjectSetInteger(chart_ID,name,OBJPROP_ZORDER,z_order);
//— successful execution
return(true);
}
//+——————————————————————+
//| Create rectangle label |
//+——————————————————————+
bool RectLabelCreate(const long chart_ID=0, // chart’s ID
const string name=”RectLabel”, // label name
const int sub_window=0, // subwindow index
const int x=0, // X coordinate
const int y=0, // Y coordinate
const int width=50, // width
const int height=18, // height
const color back_clr=C’236,233,216′, // background color
const string tt = “\n”, // tooltip
const ENUM_BORDER_TYPE border=BORDER_SUNKEN, // border type
const ENUM_BASE_CORNER corner=CORNER_LEFT_UPPER, // chart corner for anchoring
const color clr=clrWhite, // flat border color (Flat)
const ENUM_LINE_STYLE style=STYLE_SOLID, // flat border style
const int line_width=1, // flat border width
const bool back=false, // in the background
const bool selection=false, // highlight to move
const bool hidden=true, // hidden in the object list
const long z_order=0) // priority for mouse click
{
//— reset the error value
ResetLastError();
//— create a rectangle label
if(!ObjectCreate(chart_ID,name,OBJ_RECTANGLE_LABEL,sub_window,0,0)) {
ObjectSetInteger(chart_ID, name, OBJPROP_WIDTH, line_width);
return(false);
}
//— set label coordinates
ObjectSetInteger(chart_ID,name,OBJPROP_XDISTANCE,x);
ObjectSetInteger(chart_ID,name,OBJPROP_YDISTANCE,y);
//— set label size
ObjectSetInteger(chart_ID,name,OBJPROP_XSIZE,width);
ObjectSetInteger(chart_ID,name,OBJPROP_YSIZE,height);
//— set background color
ObjectSetInteger(chart_ID,name,OBJPROP_BGCOLOR,back_clr);
//— set border type
ObjectSetInteger(chart_ID,name,OBJPROP_BORDER_TYPE,border);
//— set the chart’s corner, relative to which point coordinates are defined
ObjectSetInteger(chart_ID,name,OBJPROP_CORNER,corner);
//— set flat border color (in Flat mode)
ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr);
//— set flat border line style
ObjectSetInteger(chart_ID,name,OBJPROP_STYLE,style);
//— set flat border width
ObjectSetInteger(chart_ID,name,OBJPROP_WIDTH,line_width);
//— display in the foreground (false) or background (true)
ObjectSetInteger(chart_ID,name,OBJPROP_BACK,back);
//— enable (true) or disable (false) the mode of moving the label by mouse
ObjectSetInteger(chart_ID,name,OBJPROP_SELECTABLE,selection);
ObjectSetInteger(chart_ID,name,OBJPROP_SELECTED,selection);
//— hide (true) or display (false) graphical object name in the object list
ObjectSetInteger(chart_ID,name,OBJPROP_HIDDEN,hidden);
//— set the priority for receiving the event of a mouse click in the chart
ObjectSetInteger(chart_ID,name,OBJPROP_ZORDER,z_order);
ObjectSetString(chart_ID, name, OBJPROP_TOOLTIP, tt);
//— successful execution
return(true);
}
哪些贵金属平台好?不错的贵金属交易汇平台推荐:哪些贵金属平台好?2023年平台最新排名
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。
本文相关:无相关信息
下一篇 :在复数图表中同步趋势线图式的方法
留言与评论(共有 0 条评论) |