分享一个简单的均线交叉EA源码 新手可以拿去改着玩
翻了翻自己MT5文件夹 找到一个最早写的均线交叉EA 那时候刚学MQL5 写得很糙但逻辑是对的 就是金叉做多死叉做空 加了个简单的止损止盈 分享出来给刚入门的朋友拿去改着玩 别直接跑实盘 滑点和点差都没处理 思路就是短周期均线上穿长周期均线的时候开多 反过来开空 止损设了50个点 止盈80个点 没有移动止损 也没有过滤假信号 你们拿去改的时候可以加个ATR过滤 或者加大周期方向过滤 能少亏不少 有啥问题可以留言问 我看到会回
//+------------------------------------------------------------------+
//| MA_Cross_Basic.mq5 |
//| 简单均线交叉 EA, 短均线上穿长均线做多 |
//+------------------------------------------------------------------+
#property version "1.00"
#include <Trade\Trade.mqh>
input int FastMA = 10; // 短周期均线周期
input int SlowMA = 30; // 长周期均线周期
input double Lots = 0.01; // 下单手数
input int StopLoss = 50; // 止损, 单位: 点
input int TakeProfit = 80; // 止盈, 单位: 点
input int MagicNumber = 559; // 魔术号, 区分不同 EA 的单
CTrade trade;
int hFast = INVALID_HANDLE;
int hSlow = INVALID_HANDLE;
//+------------------------------------------------------------------+
//| 初始化: 创建两条均线的句柄 |
//+------------------------------------------------------------------+
int OnInit()
{
trade.SetExpertMagicNumber(MagicNumber);
trade.SetTypeFillingBySymbol(_Symbol);
hFast = iMA(_Symbol, _Period, FastMA, 0, MODE_EMA, PRICE_CLOSE);
hSlow = iMA(_Symbol, _Period, SlowMA, 0, MODE_EMA, PRICE_CLOSE);
if(hFast == INVALID_HANDLE || hSlow == INVALID_HANDLE)
{
Print("均线句柄创建失败, 检查参数");
return INIT_FAILED;
}
return INIT_SUCCEEDED;
}
//+------------------------------------------------------------------+
//| 卸载: 释放句柄 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
if(hFast != INVALID_HANDLE) IndicatorRelease(hFast);
if(hSlow != INVALID_HANDLE) IndicatorRelease(hSlow);
}
//+------------------------------------------------------------------+
//| 每根 tick 都会进来, 这里只在新 K 线开盘时判断一次 |
//+------------------------------------------------------------------+
void OnTick()
{
static datetime lastBarTime = 0;
datetime curBarTime = iTime(_Symbol, _Period, 0);
if(curBarTime == lastBarTime) return;
lastBarTime = curBarTime;
if(HasMyPosition()) return; // 已有持仓就不再开新单
double fastBuf[], slowBuf[];
if(CopyBuffer(hFast, 0, 0, 2, fastBuf) < 2) return;
if(CopyBuffer(hSlow, 0, 0, 2, slowBuf) < 2) return;
// [1] 是上一根 K 线, [0] 是当前这根
bool crossUp = (fastBuf[1] <= slowBuf[1] && fastBuf[0] > slowBuf[0]);
bool crossDown = (fastBuf[1] >= slowBuf[1] && fastBuf[0] < slowBuf[0]);
double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
double pt = _Point;
if(crossUp)
{
double sl = (StopLoss > 0) ? ask - StopLoss * pt : 0.0;
double tp = (TakeProfit > 0) ? ask + TakeProfit * pt : 0.0;
trade.Buy(Lots, _Symbol, ask, sl, tp, "MA Cross Long");
}
else if(crossDown)
{
double sl = (StopLoss > 0) ? bid + StopLoss * pt : 0.0;
double tp = (TakeProfit > 0) ? bid - TakeProfit * pt : 0.0;
trade.Sell(Lots, _Symbol, bid, sl, tp, "MA Cross Short");
}
}
//+------------------------------------------------------------------+
//| 判断这个品种、这个魔术号下是否已有持仓 |
//+------------------------------------------------------------------+
bool HasMyPosition()
{
for(int i = PositionsTotal() - 1; i >= 0; i--)
{
ulong ticket = PositionGetTicket(i);
if(ticket == 0) continue;
if(PositionGetString(POSITION_SYMBOL) != _Symbol) continue;
if(PositionGetInteger(POSITION_MAGIC) != MagicNumber) continue;
return true;
}
return false;
}全部回复(20)
发表回复
请先登录后再回复。
