Complete .mq5 source code

NAS100 EA MT5: Nasdaq Expert Advisor with MQL5 Code & Backtest

MQL5 Expert Advisor for NAS100 (Nasdaq 100) on MetaTrader 5. Opening range breakout strategy on M5 with volatility filter, pre-market gap handling, and tech earnings avoidance.

Historical test results

57.6%
Win Rate
15.8%
Max Drawdown
1.33
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

Measure the opening range from 09:30–09:35 NY time. Enter on breakout of range high/low with buffer. Volatility filter: ATR must exceed 200 points. Skip if overnight gap exceeds 0.5% (pre-market gap risk avoidance). One trade per session maximum.

Exit conditions

Take profit at 1.5x the opening range size. Stop loss at 0.75x opening range. Close all positions at 12:00 NY time. Emergency close if drawdown on this trade exceeds 2x ATR.

MQL5 Expert Advisor Code

//+------------------------------------------------------------------+
//| NAS100 Opening Range Breakout EA                                |
//| Pineify.app                                                      |
//+------------------------------------------------------------------+
#property copyright "Pineify.app"
#property version   "1.00"

input int    MagicNumber  = 789012;
input double Lots         = 0.01;
input int    RangeMinutes = 5;       // Range duration in minutes
input double MinATR       = 200;     // Min ATR in points
input double MaxGapPct    = 0.5;     // Max overnight gap %
input int    CloseHour    = 12;      // NY time close hour

double rHigh=0, rLow=0, prevClose=0;
bool rangeSet=false, traded=false;
datetime lastDay=0;
int atrH; double atrBuf[];

int OnInit() {
   atrH = iATR(_Symbol, PERIOD_M5, 14);
   ArraySetAsSeries(atrBuf, true);
   return INIT_SUCCEEDED;
}

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

void OnTick() {
   MqlTick tick;
   if (!SymbolInfoTick(_Symbol, tick)) return;
   MqlDateTime dt; TimeToStruct(tick.time, dt);

   datetime today = StringToTime(TimeToString(tick.time, TIME_DATE));
   if (today != lastDay) {
      prevClose = iClose(_Symbol, PERIOD_D1, 1);
      rHigh=0; rLow=0; rangeSet=false; traded=false; lastDay=today;
   }

   if (dt.hour >= CloseHour) { CloseAll(); return; }

   // Build 5-minute opening range from 9:30
   if (dt.hour == 9 && dt.min >= 30 && dt.min < 30 + RangeMinutes) {
      if (rHigh == 0 || tick.ask > rHigh) rHigh = tick.ask;
      if (rLow  == 0 || tick.bid < rLow)  rLow  = tick.bid;
      return;
   }

   if (!rangeSet && dt.hour == 9 && dt.min >= 30 + RangeMinutes) rangeSet = true;
   if (!rangeSet || traded) return;

   // Gap filter
   if (prevClose > 0) {
      double gapPct = MathAbs(rHigh - prevClose) / prevClose * 100.0;
      if (gapPct > MaxGapPct) return;
   }

   CopyBuffer(atrH, 0, 0, 2, atrBuf);
   if (atrBuf[1] < MinATR * _Point) return;

   double range = rHigh - rLow;
   if (tick.ask > rHigh) {
      OpenTrade(ORDER_TYPE_BUY,  Lots, range * 0.75, range * 1.5, MagicNumber);
      traded = true;
   } else if (tick.bid < rLow) {
      OpenTrade(ORDER_TYPE_SELL, Lots, range * 0.75, range * 1.5, MagicNumber);
      traded = true;
   }
}

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 NAS100"; 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 NAS100 scalping 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.