MetaTrader 4 (MT4) 提供了一系列强大的图形工具,使交易者能够在图表上添加各种标记和图形对象,以便更好地分析市场趋势。本文将详细介绍如何在MT4中使用脚本绘制和操作“Thumbs Up”(竖起大拇指)图形对象,并深入探讨其相关函数的使用方法和技巧。
绘制“Thumbs Up”图形对象的步骤
在MT4中,绘制“Thumbs Up”图形对象主要通过编写MQL4脚本实现。以下是实现这一目标的详细步骤和代码示例:
准备脚本
首先,创建一个新的MQL4脚本,并设置必要的脚本属性和输入参数:
#property strict
#property description "Script draws \"Thumbs Up\" sign."
#property script_show_inputs
input string InpName="ThumbUp"; // 标记名称
input int InpDate=75; // 锚点日期,百分比
input int InpPrice=25; // 锚点价格,百分比
input ENUM_ARROW_ANCHOR InpAnchor=ANCHOR_TOP; // 锚点类型
input color InpColor=clrRed; // 标记颜色
input ENUM_LINE_STYLE InpStyle=STYLE_DOT; // 边线样式
input int InpWidth=5; // 标记大小
input bool InpBack=false; // 背景标记
input bool InpSelection=true; // 可移动
input bool InpHidden=true; // 隐藏在对象列表中
input long InpZOrder=0; // 鼠标点击优先级
创建和移动“Thumbs Up”图形对象
为了创建“Thumbs Up”图形对象,我们需要定义一个函数,该函数将处理对象的创建和属性设置:
bool ArrowThumbUpCreate(const long chart_ID=0, const string name="ThumbUp", const int sub_window=0, datetime time=0, double price=0, const ENUM_ARROW_ANCHOR anchor=ANCHOR_BOTTOM, const color clr=clrRed, const ENUM_LINE_STYLE style=STYLE_SOLID, const int width=3, const bool back=false, const bool selection=true, const bool hidden=true, const long z_order=0) {
ChangeArrowEmptyPoint(time, price);
ResetLastError();
if (!ObjectCreate(chart_ID, name, OBJ_ARROW_THUMB_UP, sub_window, time, price)) {
Print(__FUNCTION__, ": failed to create \"Thumbs Up\" sign! Error code = ", GetLastError());
return false;
}
ObjectSetInteger(chart_ID, name, OBJPROP_ANCHOR, anchor);
ObjectSetInteger(chart_ID, name, OBJPROP_COLOR, clr);
ObjectSetInteger(chart_ID, name, OBJPROP_STYLE, style);
ObjectSetInteger(chart_ID, name, OBJPROP_WIDTH, width);
ObjectSetInteger(chart_ID, name, OBJPROP_BACK, back);
ObjectSetInteger(chart_ID, name, OBJPROP_SELECTABLE, selection);
ObjectSetInteger(chart_ID, name, OBJPROP_SELECTED, selection);
ObjectSetInteger(chart_ID, name, OBJPROP_HIDDEN, hidden);
ObjectSetInteger(chart_ID, name, OBJPROP_ZORDER, z_order);
return true;
}
接下来,定义一个函数来移动“Thumbs Up”图形对象的锚点位置:
bool ArrowThumbUpMove(const long chart_ID=0, const string name="ThumbUp", datetime time=0, double price=0) {
if (!time)
time = TimeCurrent();
if (!price)
price = SymbolInfoDouble(Symbol(), SYMBOL_BID);
ResetLastError();
if (!ObjectMove(chart_ID, name, 0, time, price)) {
Print(__FUNCTION__, ": failed to move the anchor point! Error code = ", GetLastError());
return false;
}
return true;
}
删除和修改“Thumbs Up”图形对象
如果需要删除图形对象,可以使用以下函数:
bool ArrowThumbUpDelete(const long chart_ID=0, const string name="ThumbUp") {
ResetLastError();
if (!ObjectDelete(chart_ID, name)) {
Print(__FUNCTION__, ": failed to delete \"Thumbs Up\" sign! Error code = ", GetLastError());
return false;
}
return true;
}
修改锚点类型的函数如下:
bool ArrowThumbUpAnchorChange(const long chart_ID=0, const string name="ThumbUp", const ENUM_ARROW_ANCHOR anchor=ANCHOR_TOP) {
ResetLastError();
if (!ObjectSetInteger(chart_ID, name, OBJPROP_ANCHOR, anchor)) {
Print(__FUNCTION__, ": failed to change anchor type! Error code = ", GetLastError());
return false;
}
return true;
}
脚本启动函数
在脚本启动时,我们需要验证输入参数的有效性,并创建“Thumbs Up”图形对象:
void OnStart() {
if (InpDate < 0 || InpDate > 100 || InpPrice < 0 || InpPrice > 100) {
Print("Error! Incorrect values of input parameters!");
return;
}
int bars = (int)ChartGetInteger(0, CHART_VISIBLE_BARS);
int accuracy = 1000;
datetime date[];
double price[];
ArrayResize(date, bars);
ArrayResize(price, accuracy);
ResetLastError();
if (CopyTime(Symbol(), Period(), 0, bars, date) == -1) {
Print("Failed to copy time values! Error code = ", GetLastError());
return;
}
double max_price = ChartGetDouble(0, CHART_PRICE_MAX);
double min_price = ChartGetDouble(0, CHART_PRICE_MIN);
double step = (max_price - min_price) / accuracy;
for (int i = 0; i < accuracy; i++)
price[i] = min_price + i * step;
int d = InpDate * (bars - 1) / 100;
int p = InpPrice * (accuracy - 1) / 100;
if (!ArrowThumbUpCreate(0, InpName, 0, date[d], price[p], InpAnchor, InpColor, InpStyle, InpWidth, InpBack, InpSelection, InpHidden, InpZOrder))
return;
ChartRedraw();
Sleep(1000);
int h_steps = bars / 4;
for (int i = 0; i < h_steps; i++) {
if (d > 1)
d -= 1;
if (!ArrowThumbUpMove(0, InpName, date[d], price[p]))
return;
if (IsStopped())
return;
ChartRedraw();
Sleep(50);
}
Sleep(1000);
int v_steps = accuracy / 4;
for (int i = 0; i < v_steps; i++) {
if (p < accuracy - 1)
p += 1;
if (!ArrowThumbUpMove(0, InpName, date[d], price[p]))
return;
if (IsStopped())
return;
ChartRedraw();
}
ArrowThumbUpAnchorChange(0, InpName, ANCHOR_BOTTOM);
ChartRedraw();
Sleep(1000);
ArrowThumbUpDelete(0, InpName);
ChartRedraw();
Sleep(1000);
}
总之,通过以上代码示例和步骤,我们可以在MetaTrader 4中成功绘制和操作“Thumbs Up”图形对象。这不仅丰富了图表的可视化效果,还为交易者提供了更多分析市场的工具和手段。通过深入理解和灵活运用这些MQL4编程技巧,交易者可以更好地定制自己的交易策略和图表分析工具,提升交易效果。