找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
首页论坛EAHub 外汇论坛
查看: 16|回复: 0

MQL4 语言入门之数组

[复制链接]

4万

主题

54

回帖

4万

积分

管理员

积分
45954
发表于 2026-8-22 06:00:00 | 显示全部楼层 |阅读模式
最后由 你是我的小脑被 于 2021-11-14 09:29 编辑

数组别怕。这很简单。看一看。假设您要记住五个价格。我们该怎么做?好吧,我们这么做:double price1 = 1.2341;

double price2 = 1.2321;

double price3 = 1.2361;

double price4 = 1.2411;

double price5 = 1.2301;我们得到五个变量,它们只有一个数据类型,且描述同一个参数 - 价格。我们可以换种方法,用一个数组。一个数组就是一组变量,指数不同,但名称相同。从五个元素声明一个数组,方式如下:double price[5];

常用形式:(数组类型)(数组名称) [元素数量];在我们的示例中:数组类型 - double(双类型),名称 - price(价格),元素数量 - 5。我们来看如何引用这些数组元素:double price[5];   // declare an array of 5 elements

price[0] = 1.2341; // refer to the first element of the array and   

                 // assign a price to it. Note

                   // that the index of the first element starts with 0

                   // It is an important feature,

                   // you should get used to it.

price[1] = 1.2321;  // refer to the second element

price[2] = 1.2361;  // and so on

price[3] = 1.2411;

price[4] = 1.2301;

就像常用变量一样,我们可以对数组元素执行任何运算。实际上,数组的元素就是常用变量。double price[2];   

price[0] = 1.2234;

price[1] = 1.2421;   

MessageBox("Middle price is " + (price[0] +            price[1]) / 2.0,"middle price");

声明一个数组时,可以向所有元素分配初始值。double price[2] = {1.2234, 1.2421};我们简单地列举元素的初始值(在大括号中用逗号隔开)。在这种情况下,您可让编译器自动放上元素的数量,而不是您手动写入。double price[] = {1.2234, 1.2421};毫无疑问这些都是可以的,不过,遗憾的是,这根本没什么用处。我们总得弄到一点实际的数据吧!例如,当前价格、时间、可用金额等。

集成或内置的数组和变量没有实际的数据,我们当然什么都做不了。要获得实际数据,我们只需参考对应的内置数组。以下是几个内置数组:High[0];   // refer to the last maximal price,

           // that the bar has achieved in the current timeframe

           // and current currency pair. Currency pair and

           // the timeframe depend on the chart, on which you

           // have started a script. It is very important to remember!

Low[0];    // minimal price of the last bar

           // in the current chart.

Volume[0]; // value of the last bar in the current chart.要正确理解内置数组和指数,看看这个:



最后一根条柱的指数(编号)为 0,旁边一根为 1,以此类推。

还有内置常用变量。例如,Bars 显示当前图表中的条柱数量。它是个常用变量,但它早就已经声明了,并非在您的脚本中进行的声明。此变量像其他内置数组和变量一样始终存在。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|EAHub外汇论坛

GMT+8, 2026-10-11 20:42 , Processed in 0.108123 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表