Summary: 完整双均线交叉EA的MQL4源码,含参数指南与编译步骤,适合EA编程初学者。




双均线交叉EA — 完整MQL4源码
本EA采用快慢均线交叉产生买卖信号,内置固定手数、止损止盈及移动止损功能,适合EA编程入门学习。

完整MQL4源码
mql4
//+------------------------------------------------------------------+

//| DualMA_Crossover_EA.mq4 |

//| Copyright 2026, 自主编译 |

//| 免费EA下载 |

//+------------------------------------------------------------------+

#property copyright "自主编译"

#property version "1.00"

#property strict



//--- 输入参数

input int FastMA_Period = 10; // 快线周期

input int SlowMA_Period = 50; // 慢线周期

input int MA_Method = 0; // 均线类型: 0=SMA, 1=EMA

input double LotSize = 0.01; // 固定手数

input int StopLoss = 50; // 止损(点)

input int TakeProfit = 100; // 止盈(点)

input bool UseTrailing = true; // 启用移动止损

input int TrailingStop = 30; // 移动止损距离

input int TrailingStep = 10; // 移动步长

input int MagicNumber = 123456; // 魔术号



//--- 全局变量

double fastMA, slowMA;



//+------------------------------------------------------------------+

//| 初始化函数 |

//+------------------------------------------------------------------+

int OnInit()

{

Print("双均线EA已加载. 快线=", FastMA_Period, " 慢线=", SlowMA_Period);

return(INIT_SUCCEEDED);

}



//+------------------------------------------------------------------+

//| 反初始化函数 |

//+------------------------------------------------------------------+

void OnDeinit(const int reason)

{

Print("EA已移除. 原因: ", reason);

}



//+------------------------------------------------------------------+

//| ticks函数 |

//+------------------------------------------------------------------+

void OnTick()

{

fastMA = iMA(NULL, 0, FastMA_Period, 0, MA_Method, PRICE_CLOSE, 0);

slowMA = iMA(NULL, 0, SlowMA_Period, 0, MA_Method, PRICE_CLOSE, 0);



double fastMA_prev = iMA(NULL, 0, FastMA_Period, 0, MA_Method, PRICE_CLOSE, 1);

double slowMA_prev = iMA(NULL, 0, SlowMA_Period, 0, MA_Method, PRICE_CLOSE, 1);



//--- 买入信号:快线上穿慢线

if(fastMA_prev <= slowMA_prev && fastMA > slowMA)

{

if(CountOrders(OP_BUY) == 0) OpenOrder(OP_BUY);

}



//--- 卖出信号:快线下穿慢线

if(fastMA_prev >= slowMA_prev && fastMA < slowMA)

{

if(CountOrders(OP_SELL) == 0) OpenOrder(OP_SELL);

}



if(UseTrailing) TrailStop();

}



//+------------------------------------------------------------------+

//| 下单函数 |

//+------------------------------------------------------------------+

void OpenOrder(int orderType)

{

double price, sl, tp;

if(orderType == OP_BUY)

{

price = Ask;

sl = StopLoss > 0 ? Ask - StopLoss Point 10 : 0;

tp = TakeProfit > 0 ? Ask + TakeProfit Point 10 : 0;

}

else

{

price = Bid;

sl = StopLoss > 0 ? Bid + StopLoss Point 10 : 0;

tp = TakeProfit > 0 ? Bid - TakeProfit Point 10 : 0;

}



int ticket = OrderSend(Symbol(), orderType, LotSize, price, 3, sl, tp, "双均线EA", MagicNumber, 0, orderType == OP_BUY ? clrGreen : clrRed);

if(ticket < 0) Print("下单失败. 错误代码: ", GetLastError());

else Print("订单已开: ", ticket);

}



//+------------------------------------------------------------------+

//| 统计持仓数量 |

//+------------------------------------------------------------------+

int CountOrders(int type)

{

int count = 0;

for(int i = 0; i < OrdersTotal(); i++)

if(OrderSelect(i, SELECT_BY_POS) && OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber && OrderType() == type)

count++;

return count;

}



//+------------------------------------------------------------------+

//| 移动止损函数 |

//+------------------------------------------------------------------+

void TrailStop()

{

for(int i = 0; i < OrdersTotal(); i++)

{

if(OrderSelect(i, SELECT_BY_POS) && OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)

{

if(OrderType() == OP_BUY)

{

double newSL = Bid - TrailingStop Point 10;

if(newSL > OrderStopLoss() + TrailingStep Point 10 && newSL < Bid)

OrderModify(OrderTicket(), OrderOpenPrice(), newSL, OrderTakeProfit(), 0, clrGreen);

}

else if(OrderType() == OP_SELL)

{

double newSL = Ask + TrailingStop Point 10;

if((newSL < OrderStopLoss() - TrailingStep Point 10 || OrderStopLoss() == 0) && newSL > Ask)

OrderModify(OrderTicket(), OrderOpenPrice(), newSL, OrderTakeProfit(), 0, clrRed);

}

}

}

}

//+------------------------------------------------------------------+

参数说明
参数 默认值 说明
FastMA_Period 10 快线周期
SlowMA_Period 50 慢线周期
MA_Method 0 0=SMA,1=EMA
LotSize 0.01 每手固定手数
StopLoss 50 止损(点)
TakeProfit 100 止盈(点)
UseTrailing true 是否启用移动止损
TrailingStop 30 移动止损距离
MagicNumber 123456 EA唯一标识
编译使用步骤(MQL4教程)
MT4中按F4打开MetaEditor
文件 → 新建 → 专家顾问 → 粘贴代码
按F7编译,确认零错误
将EA拖入图表,开启自动交易
在输入参数中调整各项数值
参考来源
自主编译MQL4源码(2026)
MQL4官方文档:https://www.mql5.com/zh/docs
如需带RSI背离过滤或多周期逻辑的进阶EA,可订阅我们的付费EA合集,均经过实盘验证并提供完整技术支持。