Summary: Bitcoin TrendGuard EA is a stable trend-following MQL4 expert advisor for BTCUSD. Uses triple EMA, ADX filter, and dynamic position sizing. Suitable for H4 timeframe.
Bitcoin TrendGuard EA is a robust trend-following system designed specifically for Bitcoin's unique market behavior. The EA uses a triple EMA cloud (fast, medium, slow) to identify trend direction and momentum, combined with ADX to filter out ranging markets. ATR-based dynamic stop loss adapts to Bitcoin's high volatility, while a volatility-adjusted position sizing reduces exposure during turbulent periods. The system includes equity protection, spread control, and weekend position closure to manage crypto-specific risks.
Recommended Timeframe: H4
Trading Logic:
1. Triple EMA Trend: EMA9 > EMA21 > EMA55 for uptrend; reverse for downtrend.
2. Trend Strength: ADX(14) must be above 25, with +DI above -DI for longs or -DI above +DI for shorts.
3. Entry Confirmation: Price must close beyond the slow EMA after a pullback (no trade on first touch).
4. Risk Management: Dynamic stop loss at 2.5x ATR, take profit at 5x ATR, trailing stop activates after 3x ATR profit. Max 1 trade, daily loss limit 5%, weekly loss limit 10%.
```mql4
//+------------------------------------------------------------------+
//| BitcoinTrendGuardEA.mq4 |
//+------------------------------------------------------------------+
#property copyright ""
#property link ""
#property version "1.00"
#property strict
//--- input parameters with comments
input double BaseLotSize = 0.01; // Base lot size (0.01 BTC per trade)
input int FastEMAPeriod = 9; // Fast EMA period (trend momentum)
input int MidEMAPeriod = 21; // Medium EMA period (trend direction)
input int SlowEMAPeriod = 55; // Slow EMA period (major trend)
input int ADXPeriod = 14; // ADX period for trend strength
input int ADXThreshold = 25; // Minimum ADX value to allow trading
input int ATRPeriod = 14; // ATR period for dynamic stop loss
input double ATRStopMultiplier = 2.5; // Stop loss as multiple of ATR
input double ATRTakeMultiplier = 5.0; // Take profit as multiple of ATR
input double TrailingStart = 3.0; // Trailing start in ATR multiples
input double TrailingStep = 1.0; // Trailing step in ATR multiples
input int MagicNumber = 202417; // Unique EA identifier
input int MaxSpread = 120; // Maximum allowed spread in points (BTC has wider spread)
input double DailyLossLimit = 5.0; // Daily loss limit as percentage of balance
input double WeeklyLossLimit = 10.0; // Weekly loss limit as percentage of balance
input bool UseSundayClose = true; // Close trades before Sunday 22:00 GMT
//--- global variables
double dailyStartBalance = 0;
double weeklyStartBalance = 0;
datetime lastBarTime = 0;
bool sundayCloseExecuted = false;
double avgATR = 0;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
dailyStartBalance = AccountBalance();
weeklyStartBalance = AccountBalance();
lastBarTime = 0;
sundayCloseExecuted = false;
avgATR = iATR(Symbol(), PERIOD_H4, ATRPeriod, 1);
if(avgATR <= 0) avgATR = 800 * Point;
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
Comment("");
}
//+------------------------------------------------------------------+
//| Calculate dynamic lot size based on volatility |
//+------------------------------------------------------------------+
double GetDynamicLotSize()
{
double atr = iATR(Symbol(), PERIOD_H4, ATRPeriod, 1);
if(atr <= 0) atr = avgATR;
double lot = BaseLotSize;
// Reduce lot size when volatility is higher than average
if(atr > avgATR)
lot = BaseLotSize * (avgATR / atr);
lot = NormalizeDouble(lot, 2);
if(lot < 0.01) lot = 0.01;
if(lot > 1.0) lot = 1.0;
return lot;
}
//+------------------------------------------------------------------+
//| Check triple EMA alignment for uptrend |
//+------------------------------------------------------------------+
bool IsTripleEMAUptrend()
{
double emaFast = iMA(Symbol(), PERIOD_H4, FastEMAPeriod, 0, MODE_EMA, PRICE_CLOSE, 1);
double emaMid = iMA(Symbol(), PERIOD_H4, MidEMAPeriod, 0, MODE_EMA, PRICE_CLOSE, 1);
double emaSlow = iMA(Symbol(), PERIOD_H4, SlowEMAPeriod, 0, MODE_EMA, PRICE_CLOSE, 1);
double close1 = iClose(Symbol(), PERIOD_H4, 1);
return (emaFast > emaMid && emaMid > emaSlow && close1 > emaSlow);
}
//+------------------------------------------------------------------+
//| Check triple EMA alignment for downtrend |
//+------------------------------------------------------------------+
bool IsTripleEMADowntrend()
{
double emaFast = iMA(Symbol(), PERIOD_H4, FastEMAPeriod, 0, MODE_EMA, PRICE_CLOSE, 1);
double emaMid = iMA(Symbol(), PERIOD_H4, MidEMAPeriod, 0, MODE_EMA, PRICE_CLOSE, 1);
double emaSlow = iMA(Symbol(), PERIOD_H4, SlowEMAPeriod, 0, MODE_EMA, PRICE_CLOSE, 1);
double close1 = iClose(Symbol(), PERIOD_H4, 1);
return (emaFast < emaMid && emaMid < emaSlow && close1 < emaSlow);
}
//+------------------------------------------------------------------+
//| Check ADX trend strength confirmation |
//+------------------------------------------------------------------+
bool IsADXBullish()
{
double adx = iADX(Symbol(), PERIOD_H4, ADXPeriod, PRICE_CLOSE, MODE_MAIN, 1);
double diPlus = iADX(Symbol(), PERIOD_H4, ADXPeriod, PRICE_CLOSE, MODE_PLUSDI, 1);
double diMinus = iADX(Symbol(), PERIOD_H4, ADXPeriod, PRICE_CLOSE, MODE_MINUSDI, 1);
return (adx > ADXThreshold && diPlus > diMinus);
}
bool IsADXBearish()
{
double adx = iADX(Symbol(), PERIOD_H4, ADXPeriod, PRICE_CLOSE, MODE_MAIN, 1);
double diPlus = iADX(Symbol(), PERIOD_H4, ADXPeriod, PRICE_CLOSE, MODE_PLUSDI, 1);
double diMinus = iADX(Symbol(), PERIOD_H4, ADXPeriod, PRICE_CLOSE, MODE_MINUSDI, 1);
return (adx > ADXThreshold && diMinus > diPlus);
}
//+------------------------------------------------------------------+
//| Manage trailing stop for open position |
//+------------------------------------------------------------------+
void ManageTrailingStop()
{
for(int i = OrdersTotal()-1; i >= 0; i--)
{
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
{
double atr = iATR(Symbol(), PERIOD_H4, ATRPeriod, 1);
if(atr <= 0) atr = avgATR;
double newSL = 0;
double trailTrigger = atr * TrailingStart;
if(OrderType() == OP_BUY)
{
double profitPoints = (Bid - OrderOpenPrice()) / Point;
if(profitPoints >= trailTrigger / Point)
{
newSL = Bid - (atr * TrailingStep);
if(newSL > OrderStopLoss())
{
OrderModify(OrderTicket(), OrderOpenPrice(), newSL, OrderTakeProfit(), 0, clrNONE);
}
}
}
else if(OrderType() == OP_SELL)
{
double profitPoints = (OrderOpenPrice() - Ask) / Point;
if(profitPoints >= trailTrigger / Point)
{
newSL = Ask + (atr * TrailingStep);
if(newSL < OrderStopLoss() || OrderStopLoss() == 0)
{
OrderModify(OrderTicket(), OrderOpenPrice(), newSL, OrderTakeProfit(), 0, clrNONE);
}
}
}
break;
}
}
}
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
// Daily equity protection
double currentEquity = AccountEquity();
double dailyLoss = (dailyStartBalance - currentEquity) / dailyStartBalance * 100;
double weeklyLoss = (weeklyStartBalance - currentEquity) / weeklyStartBalance * 100;
if(dailyLoss >= DailyLossLimit)
{
Comment("Daily loss limit reached. No new trades.");
return;
}
if(weeklyLoss >= WeeklyLossLimit)
{
Comment("Weekly loss limit reached. No new trades.");
return;
}
// Reset daily balance at new day
datetime currentTime = TimeCurrent();
static datetime lastDayCheck = 0;
if(TimeDayOfYear(currentTime) != TimeDayOfYear(lastDayCheck))
{
dailyStartBalance = AccountEquity();
lastDayCheck = currentTime;
}
// Reset weekly balance at new week
static datetime lastWeekCheck = 0;
if(TimeDayOfWeek(currentTime) == 1 && TimeDayOfYear(currentTime) != TimeDayOfYear(lastWeekCheck))
{
weeklyStartBalance = AccountEquity();
lastWeekCheck = currentTime;
}
// Sunday close before weekend gap
if(UseSundayClose && !sundayCloseExecuted)
{
if(TimeDayOfWeek(currentTime) == 0 && TimeHour(currentTime) >= 22)
{
CloseAllOrders();
sundayCloseExecuted = true;
return;
}
if(TimeDayOfWeek(currentTime) != 0)
sundayCloseExecuted = false;
}
// Spread filter
if(MarketInfo(Symbol(), MODE_SPREAD) > MaxSpread)
{
Comment("Spread too high: ", MarketInfo(Symbol(), MODE_SPREAD));
return;
}
// New bar logic (H4)
if(Time[0] == lastBarTime)
return;
lastBarTime = Time[0];
// Update average ATR
double atr = iATR(Symbol(), PERIOD_H4, ATRPeriod, 1);
if(atr > 0) avgATR = (avgATR * 0.95) + (atr * 0.05);
// Check existing position and manage trailing stop
if(CountPositions() > 0)
{
ManageTrailingStop();
return;
}
double lot = GetDynamicLotSize();
int cmd = -1;
double sl = 0, tp = 0;
double ask = Ask;
double bid = Bid;
// Long entry: triple EMA uptrend + ADX bullish confirmation
if(IsTripleEMAUptrend() && IsADXBullish())
{
cmd = OP_BUY;
sl = bid - (atr * ATRStopMultiplier);
tp = bid + (atr * ATRTakeMultiplier);
}
// Short entry: triple EMA downtrend + ADX bearish confirmation
else if(IsTripleEMADowntrend() && IsADXBearish())
{
cmd = OP_SELL;
sl = ask + (atr * ATRStopMultiplier);
tp = ask - (atr * ATRTakeMultiplier);
}
if(cmd != -1)
{
int ticket = OrderSend(Symbol(), cmd, lot, (cmd==OP_BUY?ask:bid), 5, sl, tp, "Bitcoin TrendGuard", MagicNumber, 0, clrNONE);
if(ticket < 0)
Print("OrderSend failed: ", GetLastError());
}
}
//+------------------------------------------------------------------+
//| Count open positions with this MagicNumber |
//+------------------------------------------------------------------+
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;
}
//+------------------------------------------------------------------+
//| Close all orders for this symbol and magic |
//+------------------------------------------------------------------+
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, 5, clrNONE);
else if(OrderType() == OP_SELL)
OrderClose(OrderTicket(), OrderLots(), Ask, 5, clrNONE);
}
}
}
}
//+------------------------------------------------------------------+
```
Reference: Original MQL4 code for educational purposes.
Disclaimer: Bitcoin trading carries extreme risk due to high volatility and potential market gaps. This EA is provided as-is without any guarantee of profit. Test thoroughly on a demo account for at least 3 months before live trading. Past performance does not guarantee future results.