Bitcoin Safe Trader EA is designed specifically for Bitcoin (BTCUSD) with a low-risk, stable operational approach. Bitcoin is extremely volatile, so the EA uses ADX to trade only during strong trends, ATR to avoid entry during abnormal volatility spikes, and wider stop loss to accommodate crypto price swings. Each trade has a fixed stop loss and take profit based on ATR multiples, with daily equity protection and spread control.
Recommended Timeframe: H4
Trading Logic:
``
mql4
//+------------------------------------------------------------------+
//| BitcoinSafeEA.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 lot for BTCUSD)
input int StopLossPoints = 1500; // Stop loss in points (1500 points for Bitcoin)
input int TakeProfitPoints = 3000; // Take profit in points (2:1 risk-reward)
input int ADXPeriod = 14; // Period for ADX indicator
input int ADXThreshold = 25; // ADX threshold for trend strength (above = trending)
input int EMAPeriod = 200; // Long-term EMA for trend direction
input int ATRPeriod = 14; // Period for ATR volatility indicator
input double ATRMinMultiplier = 0.5; // Minimum ATR multiplier (filter low volatility)
input double ATRMaxMultiplier = 2.0; // Maximum ATR multiplier (filter high volatility)
input int MagicNumber = 202413; // Unique EA identifier
input int MaxSpread = 150; // Maximum allowed spread (in points for Bitcoin)
input double DailyLossLimit = 5.0; // Daily loss limit in percentage of balance
input bool UseCloseOnFriday = true; // Close all trades before Friday 21:00
//--- global variables
double dailyStartBalance = 0;
datetime lastBarTime = 0;
bool isFridayCloseExecuted = false;
double avgATR = 0;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
dailyStartBalance = AccountBalance();
lastBarTime = 0;
isFridayCloseExecuted = false;
avgATR = iATR(Symbol(), PERIOD_H4, ATRPeriod, 1);
if(avgATR <= 0) avgATR = 500;
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 (crypto trades 24/7 but safer to close)
if(UseCloseOnFriday && !isFridayCloseExecuted)
{
datetime currentTime = TimeCurrent();
if(TimeDayOfWeek(currentTime) == 5 && TimeHour(currentTime) >= 21)
{
CloseAllOrders();
isFridayCloseExecuted = true;
return;
}
if(TimeDayOfWeek(currentTime) != 5)
isFridayCloseExecuted = false;
}
// Spread filter for Bitcoin (wider than Forex)
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;
// ADX and DI values
double adx = iADX(Symbol(), PERIOD_H4, ADXPeriod, PRICE_CLOSE, MODE_MAIN, 1);
double plusDI = iADX(Symbol(), PERIOD_H4, ADXPeriod, PRICE_CLOSE, MODE_PLUSDI, 1);
double minusDI = iADX(Symbol(), PERIOD_H4, ADXPeriod, PRICE_CLOSE, MODE_MINUSDI, 1);
// Trend direction using EMA200
double ema200 = iMA(Symbol(), PERIOD_H4, EMAPeriod, 0, MODE_EMA, PRICE_CLOSE, 1);
double close1 = iClose(Symbol(), PERIOD_H4, 1);
// ATR volatility filter
double atr = iATR(Symbol(), PERIOD_H4, ATRPeriod, 1);
double atrMin = avgATR ATRMinMultiplier;
double atrMax = avgATR ATRMaxMultiplier;
if(atr < atrMin || atr > atrMax)
{
Comment("Volatility abnormal. ATR: ", atr);
return;
}
// Update rolling average ATR
avgATR = (avgATR 0.9) + (atr 0.1);
int cmd = -1;
double sl = 0, tp = 0;
// Long condition: ADX > threshold, +DI > -DI, price above EMA200
if(adx > ADXThreshold && plusDI > minusDI && close1 > ema200)
{
cmd = OP_BUY;
sl = SymbolInfoDouble(Symbol(), SYMBOL_BID) - StopLossPoints Point;
tp = SymbolInfoDouble(Symbol(), SYMBOL_BID) + TakeProfitPoints Point;
}
// Short condition: ADX > threshold, -DI > +DI, price below EMA200
else if(adx > ADXThreshold && minusDI > plusDI && close1 < ema200)
{
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), 3, sl, tp, "Bitcoin Safe EA", 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, 3, clrNONE);
else if(OrderType() == OP_SELL)
OrderClose(OrderTicket(), OrderLots(), Ask, 3, clrNONE);
}
}
}
}
//+------------------------------------------------------------------+
``Reference: Original MQL4 code for educational purposes.
Disclaimer: Bitcoin trading involves extreme risk and high volatility. 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.