MQL5 里那几个开箱即用的市场容器
MQL5 里那几个开箱即用的市场容器
写 EA 或指标前,先认清楚 MQL5 已经替你备好的预定义变量。它们不是你声明的,而是程序一挂到图表上就自动装进了当时市场的状态:交易品种、周期、报价精度、最小变动单位。 _Symbol 直接返回当前图表品种字符串,比如 "EURUSD";_Period 返回周期常量(PERIOD_M1=1,PERIOD_H1=16385 这类);_Digits 给出小数位,多数直盘是 4,日元对是 2 或 3;_Point 是该品种的最小价格步进,等于 1 点除以 10 的 _Digits 次方。 拿 _Digits 和 _Point 算止损距离就很直观:若 _Digits=4、_Point=0.0001,想放 20 点止损就是 20*_Point=0.0020。换到 USDJPY(_Digits=2、_Point=0.01)同一段代码不用改,自适应就完成了。 别把正态当圣经:预定义变量只反映「挂载瞬间」的图表环境,用 iCustom 或多品种调用时,不会自动跟着别的窗口变,跨品种逻辑里得手动传参。
◍ while 与 for 两种迭代写法怎么选
MQL5 里重复执行任务靠循环。while 的结构是 while (条件) { 代码 },只要条件为真就一直跑,适合次数不预先确定的场景,比如盯盘时持续判断持仓数是否低于某个阈值。
看一段典型 while 写法:先 int numberOfTrades = 0 设计数器,循环条件 numberOfTrades < 5 决定继续与否,体内 Print("You can take a trade ", numberOfTrades + 1) 输出信息,每次 numberOfTrades++ 自增,逼近 5 后退出并打印 Trade complete!。在 MT5 脚本里跑这段,终端会连续刷出 5 条带序号的提示,验证循环确实执行了 5 次。
for 循环把初始化、条件、迭代压进一行:for (int i = 1; i <= 5; i++)。它和 while 能互转,但优势在可读和受控——循环变量显式写明,一眼看出跑 5 次。下面代码块里几个 OnStart 片段虽不是循环本体,但展示了 MQL5 预定义变量(_Symbol、_Period、_Digits、_Point)怎么在脚本里直接取用,比如 _Point * 15 在 1.2222 报价上算出 SL 为 1.2207,这类取值常塞进循环做批量下单或改挂单。
外汇与贵金属杠杆高、滑点跳空频繁,用循环批量操作仓位时,要防死循环导致 EA 卡死或重复发单,实盘前务必在策略测试器里限次验证。
class="type">void OnStart() { Alert(_Symbol); } class="type">void OnStart() { class=class="str">"cmt">// a sample Script that prints the current timeframe to the user Print(_Period); } class="type">void OnStart() { class=class="str">"cmt">// Use the "_Digits" predefined variable to get the number of decimal places class="type">int decimalPlaces = _Digits; class=class="str">"cmt">// Print the result to the console Print("Number of Decimal Places: ", decimalPlaces, " (Each movement is a pip!)") } class="type">void OnStart() { class=class="str">"cmt">// setting sl by subtracting class="num">15 pips from a ask price of class="num">1.2222 class="type">class="kw">double Ask = class="num">1.2222; class=class="str">"cmt">// current market price class="type">class="kw">double sl = Ask - (_Point * class="num">15); Alert(sl); class=class="str">"cmt">// Result will be class="num">1.2207 } class="type">void OnStart() { Alert("Discoveries Await!"); } class="type">void OnStart() { Print("Discoveries Await!"); } { Comment("I love MQL5!"); } class="type">void OnStart() { class=class="str">"cmt">// Check a condition
「最后一句大实话」
把上面这些碎片拼起来,MT5 脚本的骨架就清楚了:加减乘除和取模决定仓位与成本,++ 和 -- 管计数器,== 和 if-else 分流逻辑,while 与 for 控制重复动作次数。 比如 while 循环里 numberOfTrades 从 0 跑到 4,Print 会吐出「You can take a trade 1」到 5 共五条;for 循环用 i=1 到 5 直接打印同样内容。外汇和贵金属波动剧烈、杠杆高风险大,这类基础代码只帮你验证逻辑,不等于信号能赚钱。 开 MT5 新建脚本把代码贴进去跑一遍,比看十遍都管用——哪行报错了,就是你理解漏的那一处。
if(class="num">5>class="num">4) { class=class="str">"cmt">// Play the sound PlaySound("alert.wav"); } } class="type">void OnStart() { class="type">int num1 = class="num">8; class="type">int num2 = class="num">5; class=class="str">"cmt">//addition of values class="type">int sum = num1 + num2; class=class="str">"cmt">//subtraction of values class="type">int minus = num1 - num2; Alert("Sum = ",sum,", minus = ",minus); } class="type">void OnStart() { class="type">class="kw">double price = class="num">50.25; class="type">int quantity = class="num">10; class="type">class="kw">double totalCost = price * quantity; class=class="str">"cmt">// Multiplication class="type">class="kw">double averagePrice = totalCost / quantity; class=class="str">"cmt">// Division Alert("totalCost = ",totalCost,", averagePrice = ",averagePrice); } class="type">void OnStart() { class="type">int num1 = class="num">10; class="type">int num2 = class="num">3; class="type">int remainder = num1 % num2; class=class="str">"cmt">// The variable &class="macro">#x27;remainder&class="macro">#x27; now holds the value class="num">1 } class="type">void OnStart() { class="type">int x = class="num">5; class="type">int y = class="num">8; x++; class=class="str">"cmt">// After this line, the value of &class="macro">#x27;x&class="macro">#x27; is class="num">6 y--; class=class="str">"cmt">// After this line, the value of &class="macro">#x27;y&class="macro">#x27; is class="num">7 } class="type">int x = class="num">10; class=class="str">"cmt">// Assigns the value class="num">10 to the variable x class="type">int a = class="num">5; class="type">int b = class="num">7; class="type">bool isEqual = (a == b); class=class="str">"cmt">// Checks if &class="macro">#x27;a&class="macro">#x27; is equal to &class="macro">#x27;b&class="macro">#x27;; &class="macro">#x27;isEqual&class="macro">#x27; is class="kw">false if(sun_is_shining) { Codey_fly_high_in_the_sky() } else { Codey_take_a_nap_on_the_ground() } class="type">void OnTick() { class=class="str">"cmt">// Initialization class="type">int numberOfTrades = class="num">0; class=class="str">"cmt">// While loop condition class="kw">while(numberOfTrades < class="num">5) { class=class="str">"cmt">// Magic within the loop Print("You can take a trade ", numberOfTrades + class="num">1); class=class="str">"cmt">// Counter increment numberOfTrades ++; } class=class="str">"cmt">// Magical finale Print("Trade complete!"); } class="type">void OnTick() { class=class="str">"cmt">// For loop to iterate from class="num">1 to class="num">5 for(class="type">int i = class="num">1; i <= class="num">5; i++) { class=class="str">"cmt">// Magic within the loop class=class="str">"cmt">//i represents number of trades Print("You can take a trade ", i); } class=class="str">"cmt">// Magical finale Print("Trade complete!"); }