Summary: 黄金K线稳定EA是一款专为XAUUSD设计的MQL4智能交易系统。仅使用纯K线价格行为:内含K线突破形态和Pin Bar反转形态,适合H1周期稳定运行。




黄金K线稳定EA完全基于K线价格行为构建,不使用传统指标。它在黄金上识别两种高概率K线形态:内含K线(盘整后突破)和Pin Bar(拒绝/反转信号)。EA使用基于ATR的波动率确认来过滤交易,并包含一个可选移动平均线趋势背景过滤器。每笔交易根据形态的最高/最低价设置固定止损,止盈为2倍风险。EA包含净值保护、点差过滤和周五平仓机制。

推荐加载周期: H1
策略核心逻辑:
1. 内含K线形态:当前K线完全包含在前一根K线范围内。当价格突破母K线最高/最低价并伴随1.5倍ATR确认时入场。
2. Pin Bar形态:影线长度≥2倍实体的K线。在影线相反方向收盘时入场。
3. 趋势过滤(可选):使用EMA(50),价格高于EMA只做多,低于EMA只做空。
4. 风险管理:止损设在形态极点(内含K线为母K线高低点,Pin Bar为影线末端)。止盈为2倍止损距离。

```mql4
//+------------------------------------------------------------------+
//| GoldCandleStabilityEA.mq4 |
//+------------------------------------------------------------------+
#property copyright ""
#property link ""
#property version "1.00"
#property strict

//--- 输入参数及注释
input double LotSize = 0.01; // 固定交易手数(黄金0.01手)
input bool UseTrendFilter = true; // 使用EMA50趋势过滤(是/否)
input int TrendMAPeriod = 50; // 趋势过滤EMA周期
input double InsideBreakoutATR = 1.5; // 内含K线突破确认的ATR倍数
input double PinBodyRatio = 2.0; // Pin Bar最小影线/实体比例(影线/实体 >= 此值)
input double RiskReward = 2.0; // 风险回报比(止盈 = 止损 × 此值)
input int ATRPeriod = 14; // ATR波动率周期
input double MaxATRMultiplier = 1.8; // 最大ATR倍数(跳过过高波动)
input int MagicNumber = 202418; // EA魔术号
input int MaxSpread = 35; // 最大允许点差(单位:点)
input double DailyLossLimit = 5.0; // 每日亏损限额(账户余额百分比)
input bool UseFridayClose = true; // 周五20:00 GMT前平仓

//--- 全局变量
double dailyStartBalance = 0;
datetime lastBarTime = 0;
bool fridayCloseExecuted = false;
double avgATR = 0;

//+------------------------------------------------------------------+
//| EA初始化函数 |
//+------------------------------------------------------------------+
int OnInit()
{
dailyStartBalance = AccountBalance();
lastBarTime = 0;
fridayCloseExecuted = false;
avgATR = iATR(Symbol(), PERIOD_H1, ATRPeriod, 1);
if(avgATR <= 0) avgATR = 250 * Point;
return(INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
//| EA退出函数 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
Comment("");
}

//+------------------------------------------------------------------+
//| 检测内含K线形态 |
//+------------------------------------------------------------------+
bool IsInsideBar(int &direction, double &entryLevel, double &stopLevel)
{
double prevHigh = iHigh(Symbol(), PERIOD_H1, 2);
double prevLow = iLow(Symbol(), PERIOD_H1, 2);
double currHigh = iHigh(Symbol(), PERIOD_H1, 1);
double currLow = iLow(Symbol(), PERIOD_H1, 1);
double close1 = iClose(Symbol(), PERIOD_H1, 1);

// 内含K线条件:当前K线完全在上一根K线范围内
bool isInside = (currHigh <= prevHigh && currLow >= prevLow);

if(!isInside) return false;

double atr = iATR(Symbol(), PERIOD_H1, ATRPeriod, 1);
double breakoutDist = atr * InsideBreakoutATR;

// 看涨内含K线突破(收盘价靠近高点)
if(close1 > (prevHigh - breakoutDist * 0.3))
{
direction = 1; // 买入
entryLevel = prevHigh + breakoutDist;
stopLevel = prevLow - breakoutDist * 0.5;
return true;
}
// 看跌内含K线突破(收盘价靠近低点)
else if(close1 < (prevLow + breakoutDist * 0.3))
{
direction = -1; // 卖出
entryLevel = prevLow - breakoutDist;
stopLevel = prevHigh + breakoutDist * 0.5;
return true;
}

return false;
}

//+------------------------------------------------------------------+
//| 检测Pin Bar形态 |
//+------------------------------------------------------------------+
bool IsPinBar(int &direction, double &entryLevel, double &stopLevel)
{
double open = iOpen(Symbol(), PERIOD_H1, 1);
double close = iClose(Symbol(), PERIOD_H1, 1);
double high = iHigh(Symbol(), PERIOD_H1, 1);
double low = iLow(Symbol(), PERIOD_H1, 1);

double body = MathAbs(close - open);
double upperWick = high - MathMax(open, close);
double lowerWick = MathMin(open, close) - low;

if(body <= 0) return false;

// 看涨Pin Bar(锤子线):下影线长,上影线短,收盘在上半部
if(lowerWick >= body * PinBodyRatio && upperWick < body)
{
direction = 1; // 买入
entryLevel = close;
stopLevel = low - (high - low) * 0.3;
return true;
}
// 看跌Pin Bar(射击之星):上影线长,下影线短,收盘在下半部
else if(upperWick >= body * PinBodyRatio && lowerWick < body)
{
direction = -1; // 卖出
entryLevel = close;
stopLevel = high + (high - low) * 0.3;
return true;
}

return false;
}

//+------------------------------------------------------------------+
//| EA主循环函数(每Tick执行) |
//+------------------------------------------------------------------+
void OnTick()
{
// 每日净值保护
double currentEquity = AccountEquity();
double lossPercent = (dailyStartBalance - currentEquity) / dailyStartBalance * 100;
if(lossPercent >= DailyLossLimit)
{
Comment("已达每日亏损上限,停止开新仓");
return;
}

// 周五收盘前平仓(规避周末跳空)
if(UseFridayClose && !fridayCloseExecuted)
{
datetime currentTime = TimeCurrent();
if(TimeDayOfWeek(currentTime) == 5 && TimeHour(currentTime) >= 20)
{
CloseAllOrders();
fridayCloseExecuted = true;
return;
}
if(TimeDayOfWeek(currentTime) != 5)
fridayCloseExecuted = false;
}

// 点差过滤
if(MarketInfo(Symbol(), MODE_SPREAD) > MaxSpread)
{
Comment("当前点差过大:", MarketInfo(Symbol(), MODE_SPREAD));
return;
}

// 仅在新K线开始时检测入场(H1)
if(Time[0] == lastBarTime)
return;
lastBarTime = Time[0];

// 检查现有持仓
if(CountPositions() > 0)
return;

// 波动率过滤
double atr = iATR(Symbol(), PERIOD_H1, ATRPeriod, 1);
if(atr <= 0) atr = avgATR;
avgATR = (avgATR * 0.95) + (atr * 0.05);

if(atr > avgATR * MaxATRMultiplier)
{
Comment("当前波动率过高,ATR: ", atr);
return;
}

// 趋势过滤(可选)
double ema = iMA(Symbol(), PERIOD_H1, TrendMAPeriod, 0, MODE_EMA, PRICE_CLOSE, 1);
double close1 = iClose(Symbol(), PERIOD_H1, 1);
bool trendUp = (close1 > ema);
bool trendDown = (close1 < ema);

int direction = 0;
double entryLevel = 0;
double stopLevel = 0;
int patternType = 0; // 1=内含K线, 2=Pin Bar

// 检测形态
int insideDir = 0, pinDir = 0;
double insideEntry = 0, insideStop = 0, pinEntry = 0, pinStop = 0;

if(IsInsideBar(insideDir, insideEntry, insideStop))
{
direction = insideDir;
entryLevel = insideEntry;
stopLevel = insideStop;
patternType = 1;
}
else if(IsPinBar(pinDir, pinEntry, pinStop))
{
direction = pinDir;
entryLevel = pinEntry;
stopLevel = pinStop;
patternType = 2;
}

// 应用趋势过滤(如启用)
if(UseTrendFilter)
{
if(direction == 1 && !trendUp) direction = 0;
if(direction == -1 && !trendDown) direction = 0;
}

if(direction != 0)
{
double sl = 0, tp = 0;
double ask = Ask;
double bid = Bid;
double stopDist = 0;

if(direction == 1) // 买入
{
stopDist = MathAbs(entryLevel - stopLevel);
sl = stopLevel;
tp = entryLevel + (stopDist * RiskReward);
}
else // 卖出
{
stopDist = MathAbs(stopLevel - entryLevel);
sl = stopLevel;
tp = entryLevel - (stopDist * RiskReward);
}

int ticket = OrderSend(Symbol(), (direction==1?OP_BUY:OP_SELL), LotSize, (direction==1?ask:bid), 3, sl, tp, "Gold Candle EA", MagicNumber, 0, clrNONE);
if(ticket < 0)
{
Print("开仓失败,错误码:", GetLastError());
}
else
{
Print("开仓成功。形态:", (patternType==1?"内含K线":"Pin Bar"));
}
}
}

//+------------------------------------------------------------------+
//| 统计当前魔术号的持仓数量 |
//+------------------------------------------------------------------+
int CountPositions()
{
int count = 0;
for(int i = OrdersTotal()-1; i >= 0; i--)
{
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
count++;
}
}
return count;
}

//+------------------------------------------------------------------+
//| 平仓当前品种下所有属于该EA的订单 |
//+------------------------------------------------------------------+
void CloseAllOrders()
{
for(int i = OrdersTotal()-1; i >= 0; i--)
{
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
{
if(OrderType() == OP_BUY)
OrderClose(OrderTicket(), OrderLots(), Bid, 3, clrNONE);
else if(OrderType() == OP_SELL)
OrderClose(OrderTicket(), OrderLots(), Ask, 3, clrNONE);
}
}
}
}
//+------------------------------------------------------------------+
```
参考来源: 原创MQL4代码,仅供学习参考。
免责声明: 黄金交易(XAUUSD)因高波动性具有显著风险。本EA按“原样”提供,不保证盈利。实盘交易前请在模拟账户充分测试。历史表现不代表未来结果。
```