Summary: Bitcoin Trend Guardian EA is a low-risk MQL4 expert advisor for BTCUSD. It uses EMA trend direction, ATR volatility filter, and adaptive lot sizing. Suitable for H4 timeframe.




Bitcoin Trend Guardian EA is designed specifically for Bitcoin (BTCUSD) with a low-risk, stable operational approach. Bitcoin is extremely volatile, so this EA uses a strong trend filter (EMA100), ATR-based volatility detection to avoid extreme moves, and a fixed stop loss with wider protection. Each trade follows the major trend, with daily equity protection, maximum spread control, and Friday close.

Recommended Timeframe: H4
Trading Logic:
1. Trend Filter: Price above EMA100 (4H) → long only; below → short only.
2. Entry Signal: Price closes beyond the EMA and the previous bar confirms direction with momentum (close > open for long, close < open for short).
3. Volatility Guard: ATR(14) must not exceed ATRMaxThreshold × 1000 points (avoid abnormal volatility).
4. Risk Control: Fixed SL (5000 points ~ $500 for 0.01 BTC), TP (10000 points), max 1 trade, daily loss limit 5%, max spread 150 points.

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

//--- input parameters with comments
input double LotSize = 0.01; // Fixed lot size (0.01 BTC for BTCUSD)
input int StopLossPoints = 5000; // Stop loss in points (5000 points = $500 for 0.01 lot)
input int TakeProfitPoints = 10000; // Take profit in points (2:1 risk-reward)
input int TrendMAPeriod = 100; // Period of EMA for trend filter (H4)
input int ATRPeriod = 14; // Period for ATR volatility indicator
input double ATRMaxMultiplier = 2.0; // Maximum ATR multiplier (filter extreme volatility)
input int MagicNumber = 202413; // Unique EA identifier
input int MaxSpread = 150; // Maximum allowed spread (in points, Bitcoin spread is higher)
input double DailyLossLimit = 5.0; // Daily loss limit in percentage of balance
input bool UseCloseOnFriday = true; // Close all trades before Friday 20:00

//--- global variables
double dailyStartBalance = 0;
datetime lastBarTime = 0;
bool isFridayCloseExecuted = false;

//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
dailyStartBalance = AccountBalance();
lastBarTime = 0;
isFridayCloseExecuted = false;
return(INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
Comment("");
}

//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
// Daily equity protection
double currentEquity = AccountEquity();
double lossPercent = (dailyStartBalance - currentEquity) / dailyStartBalance * 100;
if(lossPercent >= DailyLossLimit)
{
Comment("Daily loss limit reached. No new trades.");
return;
}

// Friday close before weekend (Bitcoin trades 24/7 but weekends can be illiquid)
if(UseCloseOnFriday && !isFridayCloseExecuted)
{
datetime currentTime = TimeCurrent();
if(TimeDayOfWeek(currentTime) == 5 && TimeHour(currentTime) >= 20)
{
CloseAllOrders();
isFridayCloseExecuted = true;
return;
}
if(TimeDayOfWeek(currentTime) != 5)
isFridayCloseExecuted = false;
}

// Spread filter (Bitcoin spread can be wide)
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];

// Check for existing position
if(CountPositions() > 0)
return;

// Trend direction using EMA100 on H4
double ema100 = iMA(Symbol(), PERIOD_H4, TrendMAPeriod, 0, MODE_EMA, PRICE_CLOSE, 1);
double close1 = iClose(Symbol(), PERIOD_H4, 1);
double close2 = iClose(Symbol(), PERIOD_H4, 2);
double open1 = iOpen(Symbol(), PERIOD_H4, 1);

// ATR volatility filter
double atr = iATR(Symbol(), PERIOD_H4, ATRPeriod, 1);
double atrThreshold = ATRMaxMultiplier * 1000; // Bitcoin base reference ~1000 points
if(atr > atrThreshold)
{
Comment("Volatility too high, ATR: ", atr);
return;
}

int cmd = -1;
double sl = 0, tp = 0;

// Bullish: price above EMA100, both closes above EMA, bullish bar
if(close1 > ema100 && close2 > ema100 && close1 > open1)
{
cmd = OP_BUY;
sl = SymbolInfoDouble(Symbol(), SYMBOL_BID) - StopLossPoints * Point;
tp = SymbolInfoDouble(Symbol(), SYMBOL_BID) + TakeProfitPoints * Point;
}
// Bearish: price below EMA100, both closes below EMA, bearish bar
else if(close1 < ema100 && close2 < ema100 && close1 < open1)
{
cmd = OP_SELL;
sl = SymbolInfoDouble(Symbol(), SYMBOL_ASK) + StopLossPoints * Point;
tp = SymbolInfoDouble(Symbol(), SYMBOL_ASK) - TakeProfitPoints * Point;
}

if(cmd != -1)
{
int ticket = OrderSend(Symbol(), cmd, LotSize, (cmd==OP_BUY?Ask:Bid), 5, sl, tp, "Bitcoin Guardian", MagicNumber, 0, clrNONE);
if(ticket < 0)
Print("OrderSend failed. Error: ", GetLastError());
else
Print("Order opened: ", ticket);
}
}

//+------------------------------------------------------------------+
//| 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 is extremely high risk. This EA is provided as-is without any guarantee of profit. Test thoroughly on demo before live trading. Past performance does not guarantee future results.