Summary: 本文对一段实用的MQL4移动止损脚本进行逐行源码解读,教你如何阅读EA源码、理解变量、OrderSend与OrderModify的区别,以及如何在重新编译前安全地修改参数。




许多交易者想在自己的修改参数免费EA源码,但看不懂MQL4语法。本篇源码解读将完整讲解一个移动止损脚本——比完整EA更简单的入门起点。阅读后,你将能够自信地编译与修改脚本。

完整源码(移动止损脚本)


``cpp
//+------------------------------------------------------------------+
//| Trailing.mq4 |
//| Generated by AutoCompile AI |
//| |
//+------------------------------------------------------------------+
#property copyright "AutoCompile AI"
#property link ""
#property version "1.00"
#property strict
#property show_inputs

input int TrailingStart = 15; // 盈利达到15点后启动移动止损
input int TrailingStep = 10; // 移动止损距离(点数)
input int Slippage = 30; // 修改订单的滑点

//+------------------------------------------------------------------+
//| 脚本启动函数 |
//+------------------------------------------------------------------+
void OnStart()
{
for(int i = OrdersTotal() - 1; i >= 0; i--)
{
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if(OrderSymbol() != Symbol()) continue;

if(OrderType() == OP_BUY)
{
double profitPips = (Bid - OrderOpenPrice()) / Point;
if(profitPips > TrailingStart)
{
double newStopLoss = Bid - TrailingStep Point;
if(newStopLoss > OrderStopLoss())
OrderModify(OrderTicket(), OrderOpenPrice(), newStopLoss, OrderTakeProfit(), 0, clrBlue);
}
}
else if(OrderType() == OP_SELL)
{
double profitPips = (OrderOpenPrice() - Ask) / Point;
if(profitPips > TrailingStart)
{
double newStopLoss = Ask + TrailingStep
Point;
if(newStopLoss < OrderStopLoss() || OrderStopLoss() == 0)
OrderModify(OrderTicket(), OrderOpenPrice(), newStopLoss, OrderTakeProfit(), 0, clrBlue);
}
}
}
}
}
//+------------------------------------------------------------------+
`

逐行解读



1. 属性声明(第4-8行)


`cpp
#property copyright "AutoCompile AI"
#property strict
#property show_inputs
`
  • #property strict:启用严格类型检查。现代MQL4必须包含。

  • #property show_inputs:运行脚本时弹出输入参数对话框。


  • 2. 输入参数(第10-12行)


    `cpp
    input int TrailingStart = 15;
    input int TrailingStep = 10;
    input int Slippage = 30;
    `
  • input关键字:创建用户可修改的参数,在脚本属性窗口可见。

  • 这些就是无需重新编译即可修改参数的地方(只需重新拖拽脚本)。


  • 3. OnStart()函数(第16行)


    `cpp
    void OnStart()
    `
  • 每个脚本的代码都在OnStart()内部执行。EA则使用OnTick()


  • 4. 订单循环(第17-18行)


    `cpp
    for(int i = OrdersTotal() - 1; i >= 0; i--)
    `
  • OrdersTotal()返回当前持仓数量。

  • 倒序循环(从最大索引到0)——当可能需要修改或平仓时更安全。


  • 5. OrderSelect()(第19行)


    `cpp
    if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
    `
  • 按持仓池中的位置(0,1,2…)选择一个订单。

  • 访问订单属性(如OrderOpenPrice())前必须先调用。


  • 6. 品种筛选(第21行)


    `cpp
    if(OrderSymbol() != Symbol()) continue;
    `
  • 只处理与当前图表品种匹配的订单。


  • 7. 买单逻辑(第23-32行)


    `cpp
    double profitPips = (Bid - OrderOpenPrice()) / Point;
    `
  • Bid:当前品种的买价(卖出价)。

  • Point:最小价格变动单位(多数品种为0.0001)。

  • 利润点数 = 差值除以Point。


  • `cpp
    if(profitPips > TrailingStart)
    `
  • 仅当利润超过启动阈值时才激活移动止损。


  • `cpp
    double newStopLoss = Bid - TrailingStep Point;
    `
  • 买单:新止损设在当前Bid下方TrailingStep个点。


  • `cpp
    if(newStopLoss > OrderStopLoss())
    `
  • 只有新止损高于现有止损时才修改。


  • `cpp
    OrderModify(OrderTicket(), OrderOpenPrice(), newStopLoss, OrderTakeProfit(), 0, clrBlue);
    `
  • OrderTicket():订单唯一编号。

  • 参数:订单号、开仓价、新止损、止盈价、有效期(0=无)、箭头颜色。


  • 8. 卖单逻辑(第33-42行)


    `cpp
    double profitPips = (OrderOpenPrice() - Ask) / Point;
    `
  • 卖单:利润 = 开仓价减去当前Ask。


  • `cpp
    double newStopLoss = Ask + TrailingStep
    Point;
    `
  • 新止损设在当前Ask上方TrailingStep个点。


  • 如何编译与修改


  • 保存为Trailing.mq4,放入Scripts文件夹。

  • 在MetaEditor中打开(F4),按F7<strong>编译</strong>。

  • 编译成功后,将脚本拖到图表上——会弹出输入对话框。

  • 无需修改代码即可<strong>修改参数</strong>,例如将TrailingStart从15点改为20点。

  • 要永久更改默认值:编辑input行的数值,然后重新编译(F7)。


  • 常见修改示例


    | 需求 | 修改哪一行 |
    |------|-----------|
    | 更改默认启动点数 |
    input int TrailingStart = 25; |
    | 更改移动止损距离 |
    input int TrailingStep = 15; |
    | 添加最小手数检查(进阶) | 在第23行后添加:
    if(OrderLots() < 0.01) continue; |
    | 修改时添加弹窗提醒 | 在OrderModify后添加:
    Alert(“移动止损已调整”); |

    这个MQL4教程展示了:阅读源码解读EA编程入门最快的途径。掌握脚本后,可以转向带有
    OnTick()和定时器函数的EA。订阅我们,获取每周关于高级EA工具的编译与修改指南。

    参考来源:AutoCompile AI - 原创代码解读教程,2025年。
    ``