Complete .mq5 source code

Bitcoin MT5 Expert Advisor: MQL5 BTCUSD EA Code & Backtest 2026

MQL5 Expert Advisor for Bitcoin (BTCUSD) on MetaTrader 5. ATR-normalized trend strategy designed for crypto's 24/7 market, high volatility, and weekend gap risk.

Historical test results

52.3%
Win Rate
22.4%
Max Drawdown
1.09
Sharpe Ratio
2022–2025
Test Period

Past performance is not indicative of future results. Backtest statistics are based on historical data and do not guarantee future profits. Trading involves significant risk of loss. This content is for educational purposes only and does not constitute financial advice.

Strategy logic

Entry conditions

Enter long when close crosses above 200-period EMA on H4 and RSI is between 50–70 (momentum confirmation without overbought). ATR normalization scales position size inversely to current volatility. Avoid entries within 2 hours of weekend open.

Exit conditions

Stop loss at 2x ATR from entry, take profit at 4x ATR. Trailing stop at 1.5x ATR activates after 2x ATR profit. Close all positions Friday 20:00 server time to avoid weekend gaps.

MQL5 Expert Advisor Code

//+------------------------------------------------------------------+
//| Bitcoin Trend EA - BTCUSD H4                                    |
//| Pineify.app                                                      |
//+------------------------------------------------------------------+
#property copyright "Pineify.app"
#property version   "1.00"

input int    MagicNumber  = 890123;
input double RiskPercent  = 0.5;   // Lower risk for crypto
input int    EMA_Period   = 200;
input int    RSI_Period   = 14;
input int    ATR_Period   = 14;
input double ATR_SL_Mult  = 2.0;
input double ATR_TP_Mult  = 4.0;

int emaH, rsiH, atrH;
double emaBuf[], rsiBuf[], atrBuf[];

int OnInit() {
   emaH = iMA(_Symbol, PERIOD_H4, EMA_Period, 0, MODE_EMA, PRICE_CLOSE);
   rsiH = iRSI(_Symbol, PERIOD_H4, RSI_Period, PRICE_CLOSE);
   atrH = iATR(_Symbol, PERIOD_H4, ATR_Period);
   ArraySetAsSeries(emaBuf, true); ArraySetAsSeries(rsiBuf, true);
   ArraySetAsSeries(atrBuf, true);
   return INIT_SUCCEEDED;
}

void OnDeinit(const int reason) {
   IndicatorRelease(emaH); IndicatorRelease(rsiH); IndicatorRelease(atrH);
}

void OnTick() {
   if (!IsNewBar(PERIOD_H4)) return;

   // Weekend close: Friday 20:00
   MqlDateTime dt; TimeToStruct(TimeCurrent(), dt);
   if (dt.day_of_week == 5 && dt.hour >= 20) { CloseAll(); return; }
   if (dt.day_of_week == 6 || dt.day_of_week == 0) return;

   CopyBuffer(emaH, 0, 0, 3, emaBuf);
   CopyBuffer(rsiH, 0, 0, 3, rsiBuf);
   CopyBuffer(atrH, 0, 0, 3, atrBuf);

   if (CountPositions(MagicNumber) > 0) return;

   double close1 = iClose(_Symbol, PERIOD_H4, 1);
   double ema    = emaBuf[1];
   double rsi    = rsiBuf[1];
   double atr    = atrBuf[1];
   double lots   = CalcLots(atr * ATR_SL_Mult);

   bool longSignal  = close1 > ema && rsi > 50 && rsi < 70;
   bool shortSignal = close1 < ema && rsi < 50 && rsi > 30;

   if (longSignal)  OpenTrade(ORDER_TYPE_BUY,  lots, atr*ATR_SL_Mult, atr*ATR_TP_Mult, MagicNumber);
   if (shortSignal) OpenTrade(ORDER_TYPE_SELL, lots, atr*ATR_SL_Mult, atr*ATR_TP_Mult, MagicNumber);
}

bool IsNewBar(ENUM_TIMEFRAMES tf) {
   static datetime last=0; datetime cur=iTime(_Symbol,tf,0);
   if (cur!=last){last=cur;return true;} return false;
}

double CalcLots(double slPts) {
   double tv=SymbolInfoDouble(_Symbol,SYMBOL_TRADE_TICK_VALUE);
   double ts=SymbolInfoDouble(_Symbol,SYMBOL_TRADE_TICK_SIZE);
   double bal=AccountInfoDouble(ACCOUNT_BALANCE);
   double lot=(bal*RiskPercent/100.0)/(slPts/ts*tv);
   double stp=SymbolInfoDouble(_Symbol,SYMBOL_VOLUME_STEP);
   return MathMax(SymbolInfoDouble(_Symbol,SYMBOL_VOLUME_MIN),
          MathMin(SymbolInfoDouble(_Symbol,SYMBOL_VOLUME_MAX),MathFloor(lot/stp)*stp));
}

int CountPositions(int magic) {
   int n=0;
   for (int i=PositionsTotal()-1;i>=0;i--) {
      ulong t=PositionGetTicket(i);
      if (PositionSelectByTicket(t)&&(int)PositionGetInteger(POSITION_MAGIC)==magic) n++;
   } return n;
}

void CloseAll() {
   for (int i=PositionsTotal()-1;i>=0;i--) {
      ulong t=PositionGetTicket(i);
      if (!PositionSelectByTicket(t)||(int)PositionGetInteger(POSITION_MAGIC)!=MagicNumber) continue;
      MqlTradeRequest req={}; MqlTradeResult res={};
      req.action=TRADE_ACTION_DEAL; req.position=t; req.symbol=_Symbol;
      req.volume=PositionGetDouble(POSITION_VOLUME);
      req.type=(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY)?ORDER_TYPE_SELL:ORDER_TYPE_BUY;
      req.price=(req.type==ORDER_TYPE_SELL)?SymbolInfoDouble(_Symbol,SYMBOL_BID):SymbolInfoDouble(_Symbol,SYMBOL_ASK);
      req.type_filling=ORDER_FILLING_IOC; OrderSend(req,res);
   }
}

void OpenTrade(ENUM_ORDER_TYPE type,double lots,double sl,double tp,int magic) {
   MqlTradeRequest req={}; MqlTradeResult res={};
   req.action=TRADE_ACTION_DEAL; req.symbol=_Symbol; req.volume=lots; req.type=type;
   req.price=(type==ORDER_TYPE_BUY)?SymbolInfoDouble(_Symbol,SYMBOL_ASK):SymbolInfoDouble(_Symbol,SYMBOL_BID);
   req.sl=(type==ORDER_TYPE_BUY)?req.price-sl:req.price+sl;
   req.tp=(type==ORDER_TYPE_BUY)?req.price+tp:req.price-tp;
   req.magic=magic; req.comment="Pineify BTC"; req.type_filling=ORDER_FILLING_IOC;
   OrderSend(req,res);
}

Copy this code into MetaEditor, save it in the MQL5/Experts folder, and compile with F7.

Generate a custom BTCUSD trend-following EA

Describe the ATR inputs, alert rules, visual style, or confirmation filter you want. Pineify generates editable MQL5 source code that you can inspect and compile in MetaEditor.

Pine Script vs MQL5: Same Strategy, Different Platforms

AspectPine Script (TradingView)MQL5 (MetaTrader 5)
ExecutionSeries evaluated on chart updatesEvent handlers with explicit buffers
DeploymentRuns in TradingView with alertsRuns in the MT5 terminal or on a VPS
Broker accessVia TradingView broker integrationDirect broker connectivity
BacktestingStrategy Tester for strategy scriptsStrategy Tester for Expert Advisors
Code complexitySimpler, functional syntaxC++-like, more powerful

The calculation can be implemented on either platform, but the runtime model and buffer APIs differ. Read the Pine Script Supertrend reference before porting logic between them.

Frequently Asked Questions

Related MQL5 pages

Risk and testing note

Past performance is not indicative of future results. Backtest statistics are based on historical data and do not guarantee future profits. Trading involves significant risk of loss. This content is for educational purposes only and does not constitute financial advice.