Note

深入理解MQL4的数据类型

· Views 188

MQL4编程语言的核心之一就是其数据类型。任何程序的运作都离不开数据处理,而数据类型的选择直接影响到数据处理的效率和结果。本文将详细介绍MQL4的各种数据类型及其用途。

基础数据类型

MQL4提供了以下几种基础数据类型:

  • 整数(charshortintlongucharushortuintulong);
  • 逻辑(bool);
  • 字面值(ushort);
  • 字符串(string);
  • 浮点数(doublefloat);
  • 颜色(color);
  • 日期和时间(datetime);
  • 枚举(enum)。

例如,我们可以创建一个整数变量total,代码如下:

复制

int total = 100;

不同类型的数据处理速度各不相同,其中整数数据的处理速度最快,而浮点数数据由于其内部数据表示的复杂性,处理速度较慢。字符串数据的处理速度最慢,因为需要动态的内存分配/重新分配。

复杂数据类型

MQL4还提供了复杂的数据类型,如:

  • 结构;
  • 类。

在面向对象编程(OOP)中,复杂数据类型被称为抽象数据类型。例如,我们可以创建一个名为Trade的类,代码如下:

复制

class Trade {
    public:
    string symbol;
    int volume;
};

颜色和日期时间类型

颜色和日期时间类型的主要目的是为了方便可视化和从外部输入参数 – 来自专家顾问或自定义指标属性表的输入选项卡。颜色和日期时间类型的数据实际上是整数类型。整数类型和浮点类型被称为算术(数值)类型。

类型转换

在表达式中,除非指定了显式转换,否则只使用隐式类型转换。例如,我们可以将一个整数转换为浮点数,代码如下:

复制

int i = 10;
double d = double(i);

了解并熟悉MQL4的这些数据类型对于编写有效的MQL4代码至关重要。这些数据类型为我们提供了处理数据、创建变量和编写函数的基础工具。


Disclaimer: The content above represents only the views of the author or guest. It does not represent any views or positions of FOLLOWME and does not mean that FOLLOWME agrees with its statement or description, nor does it constitute any investment advice. For all actions taken by visitors based on information provided by the FOLLOWME community, the community does not assume any form of liability unless otherwise expressly promised in writing.

FOLLOWME Trading Community Website: https://www.followme.com

If you like, reward to support.
avatar

Hot

No comment on record. Start new comment.