许多交易者想在自己的修改参数免费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
```
#### 2. 输入参数(第10-12行)
```cpp
input int TrailingStart = 15;
input int TrailingStep = 10;
input int Slippage = 30;
```
#### 3. OnStart()函数(第16行)
```cpp
void OnStart()
```
#### 4. 订单循环(第17-18行)
```cpp
for(int i = OrdersTotal() - 1; i >= 0; i--)
```
#### 5. OrderSelect()(第19行)
```cpp
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
```
#### 6. 品种筛选(第21行)
```cpp
if(OrderSymbol() != Symbol()) continue;
```
#### 7. 买单逻辑(第23-32行)
```cpp
double profitPips = (Bid - OrderOpenPrice()) / Point;
```
```cpp
if(profitPips > TrailingStart)
```
```cpp
double newStopLoss = Bid - TrailingStep * Point;
```
```cpp
if(newStopLoss > OrderStopLoss())
```
```cpp
OrderModify(OrderTicket(), OrderOpenPrice(), newStopLoss, OrderTakeProfit(), 0, clrBlue);
```
#### 8. 卖单逻辑(第33-42行)
```cpp
double profitPips = (OrderOpenPrice() - Ask) / Point;
```
```cpp
double newStopLoss = Ask + TrailingStep * Point;
```
如何编译与修改
1. 保存为`Trailing.mq4`,放入`Scripts`文件夹。
2. 在MetaEditor中打开(F4),按F7编译。
3. 编译成功后,将脚本拖到图表上——会弹出输入对话框。
4. 无需修改代码即可修改参数,例如将TrailingStart从15点改为20点。
5. 要永久更改默认值:编辑`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年。
```