//+------------------------------------------------------------------+
//| TrendGridEA.mq5 |
//| Generated by Assistant |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Generated by Assistant"
#property link "https://www.mql5.com"
#property version "1.00"
#include <Indicators\Trend.mqh>
#include <Trade\Trade.mqh>
#include <Trade\AccountInfo.mqh>
#include <Trade\PositionInfo.mqh>
#include <Trade\SymbolInfo.mqh>
//--- 输入参数
input group "=== 趋势判断参数 ==="
input int FastMAPeriod = 5; // 快线周期 (短期均线) - 优化建议: 15
input int SlowMAPeriod = 10; // 慢线周期 (长期均线) - 优化建议: 80
input group "=== 网格参数 ==="
input double GridSize_Points = 1000.0; // 网格间距 (点) - 优化结果: 1000点最佳
input double LotSize = 0.01; // 每单交易手数
input int MaxGridLevels = 3; // 单侧最大网格层数
input group "=== 风险参数 ==="
input double MaxEquityDrawdown = 10.0; // 最大权益回撤百分比 (触发清仓) - 优化结果: 10%最佳
input double SlipValue = 2; // 滑动值 - 优化建议: 2.2-3.6
//--- 全局变量
CiMA cima1, cima2;
CAccountInfo caccountInfo;
CTrade ctrade;
CPositionInfo cpositionInfo ;
CSymbolInfo csymbolInfo ;
int fastMAHandle, slowMAHandle; // 均线指标句柄
bool isUptrend = false; // 趋势状态标志
double initialEquity; // 初始资金,用于计算回撤
double currentMaxPrice = 0.0; // 这一轮买入中的最高价,用于滑动止盈
double openEquity; // 最高权益值,用于计算回撤基准
bool isCloseError = false; // 平仓错误标志
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//--- 创建均线指标句柄
fastMAHandle = cima1.Create(_Symbol, _Period, FastMAPeriod, 0, MODE_EMA, PRICE_CLOSE);
slowMAHandle = cima2.Create(_Symbol, _Period, SlowMAPeriod, 0, MODE_EMA, PRICE_CLOSE);
if(!fastMAHandle || !slowMAHandle)
{
Print("Error creating MA indicators");
return(INIT_FAILED);
}
//--- 记录初始资金
openEquity = initialEquity = caccountInfo.Equity();
Print("Trend-Filtered Grid EA Started.");
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert tick function - 主要交易逻辑 |
//+------------------------------------------------------------------+
void OnTick()
{
csymbolInfo.Refresh();
csymbolInfo.RefreshRates();
//--- 1. 【平仓条件】检查最大回撤风险和平仓错误
int position_count = PositionsTotal();
if(CheckEquityDrawdown() || isCloseError)
{
// 平仓条件1: 权益回撤达到最大设定值
// 平仓条件2: 上次平仓有遗漏单子需要重新平仓
CloseAllPositions();
currentMaxPrice = 0;
isCloseError = false;
return;
}
//--- 2. 判断趋势方向
bool previousTrend = isUptrend;
isUptrend = CheckTrendDirection();
//--- 3. 【平仓条件】如果趋势转为下跌或触发滑动止损
if(isSlipSell() || (previousTrend && !isUptrend && CalcAllProfit() > 0.5))
{
// 平仓条件3: 触发滑动止损
// 平仓条件4: 趋势由上升转为下跌且当前有盈利(>0.5)
Print("Trend changed to Downtrend! Closing all positions and resetting grid.");
CloseAllPositions();
currentMaxPrice = 0;
return;
}
//--- 4. 【开仓条件】只在上升趋势中且无持仓时执行网格交易
if(isUptrend && position_count == 0)
{
// 开仓条件1: 处于上升趋势(快线>慢线)
// 开仓条件2: 当前没有任何持仓
ExecuteGridStrategy();
}
}
//+------------------------------------------------------------------+
//| 检查趋势方向:快线 > 慢线 ? 上行 : 下行 |
//+------------------------------------------------------------------+
bool CheckTrendDirection()
{
//--- 判断当前K线的快线是否在慢线之上
cima1.Refresh();
cima2.Refresh();
return (cima1.Main(1) > cima2.Main(1)); // 索引1是当前未结束的K线值
}
//+------------------------------------------------------------------+
//| 执行网格策略 - 开仓逻辑 |
//+------------------------------------------------------------------+
void ExecuteGridStrategy()
{
// 更新最高权益基准(用于回撤计算)
openEquity = MathMax(caccountInfo.Equity(), initialEquity);
double currentAsk = csymbolInfo.Ask();
double point = csymbolInfo.Point();
double gridSize = GridSize_Points * point;
double currentBuyPrice = csymbolInfo.Bid() - gridSize;
// 开仓操作1: 立即市价买入第一单
ctrade.Buy(LotSize, _Symbol, currentAsk);
// 开仓操作2: 挂限价单建立网格
for(int i = 0; i < MaxGridLevels - 1; i ++)
{
// 在价格下跌时按网格间距分批挂限价买单
ctrade.BuyLimit(LotSize, currentBuyPrice, _Symbol);
currentBuyPrice -= gridSize;
}
Print("挂单成功 - 已建立", MaxGridLevels, "层网格");
}
//+------------------------------------------------------------------+
//| 检查资金回撤函数 |
//+------------------------------------------------------------------+
bool CheckEquityDrawdown()
{
double currentEquity = caccountInfo.Equity();
// 计算从最高权益开始的回撤百分比
double drawdownPercent = ((openEquity - currentEquity) / openEquity) * 100.0;
Print("当前权益: ", currentEquity, " 最高权益: ", openEquity);
if(drawdownPercent >= MaxEquityDrawdown)
{
Print("达到最大权益回撤! 回撤: ", drawdownPercent, "%");
return true;
}
return false;
}
//+------------------------------------------------------------------+
//| 平仓所有订单函数 |
//+------------------------------------------------------------------+
void CloseAllPositions()
{
for(int i = PositionsTotal() - 1; i >= 0; i--)
{
if(cpositionInfo.SelectByIndex(i))
{
if(!ctrade.PositionClose(_Symbol, 0))
{
// 记录平仓错误,下次tick会重试
isCloseError = true;
Print("平仓失败,将重试");
}
}
}
Print("所有持仓已平仓");
}
//+------------------------------------------------------------------+
//| 计算当前所有订单盈利 |
//+------------------------------------------------------------------+
double CalcAllProfit()
{
double sumProfit = 0;
for(int i = PositionsTotal() - 1; i >= 0; i--)
{
if(cpositionInfo.SelectByIndex(i))
{
sumProfit += cpositionInfo.Profit();
}
}
return sumProfit;
}
//+------------------------------------------------------------------+
//| 检查当前是否需要滑动止损 - 跟踪止盈逻辑 |
//+------------------------------------------------------------------+
bool isSlipSell()
{
// 当所有网格层数都建仓后,开始跟踪最高价
if(PositionsTotal() == MaxGridLevels && csymbolInfo.Bid() > currentMaxPrice)
currentMaxPrice = csymbolInfo.Bid();
// 从最高点回撤超过SlipValue点时触发平仓
return (currentMaxPrice - csymbolInfo.Bid() > SlipValue);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//--- 释放指标句柄
if(fastMAHandle != INVALID_HANDLE)
IndicatorRelease(fastMAHandle);
if(slowMAHandle != INVALID_HANDLE)
IndicatorRelease(slowMAHandle);
Print("EA Deinitialized.");
}
//+------------------------------------------------------------------+
📋 开仓平仓条件总结
🟢 开仓条件(同时满足):
趋势条件:快线EMA(5/10) > 慢线EMA(5/10) - 上升趋势
持仓条件:当前没有任何持仓(position_count == 0)
执行操作:市价买入1单 + 挂(MaxGridLevels-1)个限价买单建立网格
🔴 平仓条件(任一触发):
风险控制:权益回撤 ≥ MaxEquityDrawdown(10%)
错误处理:上次平仓有遗漏单子(isCloseError == true)
趋势反转:趋势由升转跌 + 当前盈利 > 0.5
止损保护:价格从最高点回撤 ≥ SlipValue(2点)

3265

被折叠的 条评论
为什么被折叠?



