4.4 常用内置函数

第 23 节 / 共 36 节 课程:Mt4编程语言
MQL4提供丰富的内置函数。

数学函数

函数说明
MathAbs(x)绝对值
MathRound(x)四舍五入
MathFloor(x)向下取整
MathCeil(x)向上取整
MathSqrt(x)平方根
MathMax(x,y)最大值
MathMin(x,y)最小值
MathRand()随机数

NormalizeDouble

下单前必须规范价格:
double sl = NormalizeDouble(Bid-50*Point, Digits);
double tp = NormalizeDouble(Ask+100*Point, Digits);

字符串函数

函数说明
StringLen(s)长度
StringSubstr(s,s,c)截取
StringFind(s,s)查找
StringToUpper(s)转大写
StringSplit(s,sep,arr)分割
string parts[];
StringSplit("MACD_8888_BUY", "_", parts);
Print(parts[0]);

错误处理

函数说明
GetLastError()获取错误代码
ErrorDescription(c)转文字
ResetLastError()重置
ResetLastError();
int t = OrderSend(Symbol(),OP_BUY,0.1,Ask,3,0,0,"",0,0,clrNone);
if (t<0) Print("错误:",GetLastError());