首页 > 提升投资报酬 > 量化分析

将市场报价中的复数货币对信息用来绘制图表的方式

量化分析 2023-09-14 10:26:37 外汇报价

由上依序取得市场报价中的货币对名称


在本篇章节中,将说明市场报价中复数货币对的相关处理方式。首先,将在市场报价的显示画面中,由上依序显示各个货币对。

在制作新档案时,选择「自定义指标」,并将文件名命名为「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”」,呈现白色的粗体字。

其他项目则维持原状。
 
//+——————————————————————+

//| 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处理写入于OnCalculate内的if文体下方。参数的间隔指定为X位置「90」、Y位置16像素,因此设定为「20 + i * 16」;标准位置设定为左上方的「CORNER_LEFT_UPPER」、文字则是「symbol」。

此处之下为初期参数、并不会产生任何问题,故可予以省略。
 
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);

if (closePre == 0) continue;

double ratio = (iClose(symbol, 0, 0) – closePre) / closePre * 100;
为了能够将对象一次删除,此处应先设定接头辞。在档案上方的属性「property indicator_chart_window」下,使用「#define」来定义「PREFIX」。
 
#define PREFIX MQLInfoString(MQL_PROGRAM_NAME) + “_”
其后,在「Custom indicator initialization function」下方设定「Custom indicator deinit function」,以使用ObjectsDeleteAll函数删除每一次的对象。
 
void OnDeinit(const int reason)

{

ObjectsDeleteAll(0, PREFIX);

}
如将「PREFIX」添加于显示货币对的LabelCreate括号中,便会在每一次都删除含有「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);

int len = (int)MathAbs(ratio * 100);

color clr = ratio > 0 ? clrRed : clrDodgerBlue;
本次将以「RECTANGLE LABEL」来制作条形图,这是以XY坐标指定的长方形对象。

可从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);」,让宽度的部分能够改变。
 
//+——————————————————————+

//| 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);

}
如此便完成了OBJ_RECTANGLE_LABEL的设定。
 

制作条形图并绘制于图表上


接下来,将利用此内容来制作条形图。

在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) + “%”;

RectLabelCreate(0, PREFIX + “Graph” + IntegerToString(i), 0, 65, 14 + i * 16, len, 12, clr, tt);
其后,在「Create rectangle label」的「// background color」下方新增以下公式作为「// tooltip」。「\n」的意义即是无初始值。
 
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 条评论)
验证码:

免责声明

特别提示:根据《关于严厉查处非法外汇期货和外汇按金交易活动的通知》(证监发字[1978]105号)规定凡未经批准的机构在大陆境内擅自开展外汇期货交易均属非法,提高意识,谨防损失!

本网站所有刊登内容,以及所提供的信息资料,目的是为了更好地服务我们的访问者,本网站不保证所有信息、文本、图形、链接及其它项目的绝对准确性和完整性,网站没有任何盈利目的,故仅供访问者参照使用。本网站已尽力确保所有资料是准确、完整及最新的。就该资料的针对性、精确性以及特定用途的适合性而言,本网站不能作出最对应的方案。所以因依赖该资料所致的任何损失,本网均不负责。 除特别注明之服务条款外,其他一切因使用本站而引致的任何意外、疏忽、合约毁坏、隐秘汇漏、诽谤、版权或知识产权侵犯及其所造成的损失,本站概不负责,亦不承担任何法律责任。如您(单位或个人)认为本网站某部分内容有侵权嫌疑,敬请立即通知我们,我们将在第一时间予以更改或删除。以上声明之解释权归牛犇财经网站所有。法律上有相关解释的,以中国法律之解释为基准。如有争议限在我方所在地司法部门解决。