MT4和MT5都是流行的外汇交易平台,箭头指标在其中扮演着重要的角色,帮助交易者识别潜在的买卖信号。然而,由于MT4和MT5采用不同的编程语言(MQL4和MQL5)和平台架构,其箭头指标的代码实现存在显著差异。理解这些差异对于希望自定义或移植箭头指标的交易者至关重要。
MT4 使用 MQL4 语言,这是一种基于 C++ 的过程式编程语言。其核心代码结构通常包含以下几个部分:
以下是一个简单的 MQL4 箭头指标示例,用于在价格上涨时绘制向上箭头,价格下跌时绘制向下箭头:
#property indicator_separate_window#property indicator_buffers 1#property indicator_color1 Bluedouble ExtMapBuffer[];int init() { SetIndexBuffer(0,ExtMapBuffer); SetIndexStyle(0,DRAW_ARROW,EMPTY,3,Blue); SetIndexArrow(0,233); // 233 是向上箭头的 Wingdings 字符代码 return(0); }int deinit() { return(0); }int start() { int counted_bars=IndicatorCounted(); if(counted_bars<0) return(-1); if(counted_bars>0) counted_bars--; int limit=Bars-counted_bars; for(int i=limit-1; i>=0; i--) { if(Close[i] > Open[i]) { ExtMapBuffer[i] = Low[i] - Point*5; } else if (Close[i] < Open[i]) { ExtMapBuffer[i] = High[i] + Point*5; SetIndexArrow(0,234); // 234 是向下箭头的 Wingdings 字符代码 } else ExtMapBuffer[i] = EMPTY_VALUE; } return(0); }
MT5 使用 MQL5 语言,这是一种面向对象的编程语言,也基于 C++。相比 MQL4,MQL5 在语法、数据类型和函数库方面都有显著改进,提供了更强大的功能和更高的灵活性。
MQL5 的核心代码结构通常包含以下几个部分:
以下是一个简单的 MQL5 箭头指标示例,功能与上面的 MQL4 示例相同:
#property indicator_separate_window#property indicator_buffers 1#property indicator_color1 clrBluedouble ExtMapBuffer[];int OnInit() { IndicatorSetInteger(INDICATOR_DIGITS, _Digits); SetIndexBuffer(0,ExtMapBuffer,INDICATOR_DATA); PlotIndexSetInteger(0,PLOT_DRAW_TYPE,DRAW_ARROW); PlotIndexSetInteger(0,PLOT_ARROW,233); // 233 是向上箭头的 Wingdings 字符代码 PlotIndexSetInteger(0,PLOT_COLOR,clrBlue); return(INIT_SUCCEEDED); }void OnDeinit(const int reason) { }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[]) { int limit = rates_total - prev_calculated; for(int i=limit-1; i>=0; i--) { if(close[i] > open[i]) { ExtMapBuffer[i] = low[i] - Point()*5; } else if (close[i] < open[i]) { ExtMapBuffer[i] = high[i] + Point()*5; PlotIndexSetInteger(0,PLOT_ARROW,234); // 234 是向下箭头的 Wingdings 字符代码 } else ExtMapBuffer[i] = EMPTY_VALUE; } return(rates_total); }
特性 | MT4 (MQL4) | MT5 (MQL5) |
---|---|---|
编程语言 | 过程式 | 面向对象 |
核心函数 | start() | OnCalculate() |
输入参数 | 通常通过全局变量定义 | 使用 input 关键字显式声明 |
箭头字符 | 使用 SetIndexArrow() 函数设置 | 使用 PlotIndexSetInteger(0,PLOT_ARROW,value) 函数设置 |
数据访问 | 使用预定义的数组 (例如 Close[], High[]) | 通过 OnCalculate() 函数的参数传递 (例如 close[], high[]) |
希望本文能帮助您理解 mt5和mt4的箭头怎么不一样的代码 差异,并为自定义箭头指标提供指导。 请记住,编写指标需要一定的编程知识和耐心,祝您交易顺利!