US30 Scalping EA MT5: Dow Jones Expert Advisor with MQL5 Code
An educational US30 range-breakout EA with explicit broker-time minute inputs. It reconstructs the selected interval from completed M1 bars, checks completed M5 ATR and attempts a breakout entry with fixed lots. It has no Fed announcement filter, automatic New York timezone conversion or verified backtest results.
Strategy logic
Entry conditions
The default range is 09:00–09:30 exclusive in broker time; entry is allowed from 09:31 until 11:00. Every expected M1 timestamp must be present. A current ask above the range high plus the buffer qualifies a buy, or bid below the low minus buffer qualifies a sell. ATR and breakout thresholds are broker points, not necessarily index-price units. This is not automatically the US cash-opening range.
Exit conditions
Initial SL is half the recorded range width and TP is one width from the entry quote. No trailing stop is implemented. On available ticks, positions whose opening-day cutoff has passed receive close requests, including after reconnecting on a later day. The cutoff is an intent, not a guaranteed execution time. All position operations filter both symbol and magic.
MQL5 Expert Advisor Code
#property strict
input int MagicNumber=678901;
input double Lots=0.01;
input int RangeStartMinute=540; // 09:00 broker time
input int RangeEndMinute=570; // 09:30 exclusive
input int TradeStartMinute=571;
input int CloseMinute=660; // 11:00 broker time
input int BreakoutBufferPoints=10;
input double MinATRPoints=150;
int atrH=INVALID_HANDLE;
datetime day=0;
bool attempted=false,rangeReady=false;
double rangeHigh=0,rangeLow=0;
int OnInit()
{
if(MagicNumber<=0 || Lots<=0 || RangeStartMinute<0 || RangeEndMinute<=RangeStartMinute ||
TradeStartMinute<RangeEndMinute || CloseMinute<=TradeStartMinute || CloseMinute>=1440 ||
BreakoutBufferPoints<0 || MinATRPoints<0) return INIT_PARAMETERS_INCORRECT;
atrH=iATR(_Symbol,PERIOD_M5,14);
return atrH==INVALID_HANDLE?INIT_FAILED:INIT_SUCCEEDED;
}
void OnDeinit(const int reason) { if(atrH!=INVALID_HANDLE) IndicatorRelease(atrH); }
bool HasPosition()
{
for(int i=PositionsTotal()-1;i>=0;i--)
if(PositionGetTicket(i)>0 && PositionGetString(POSITION_SYMBOL)==_Symbol && PositionGetInteger(POSITION_MAGIC)==MagicNumber) return true;
return false;
}
void CloseExpired(datetime now)
{
for(int i=PositionsTotal()-1;i>=0;i--)
{
ulong ticket=PositionGetTicket(i);
if(ticket==0 || PositionGetString(POSITION_SYMBOL)!=_Symbol || PositionGetInteger(POSITION_MAGIC)!=MagicNumber) continue;
datetime opened=(datetime)PositionGetInteger(POSITION_TIME);
datetime openedDay=StringToTime(TimeToString(opened,TIME_DATE));
if(now<openedDay+CloseMinute*60) continue;
MqlTradeRequest req={}; MqlTradeResult res={};
req.action=TRADE_ACTION_DEAL; req.position=ticket; req.symbol=_Symbol; req.magic=MagicNumber;
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;
if(!OrderSend(req,res) || (res.retcode!=TRADE_RETCODE_DONE && res.retcode!=TRADE_RETCODE_DONE_PARTIAL)) Print("Close failed: ",res.retcode);
}
}
bool BuildRange(datetime today)
{
MqlRates bars[];
datetime start=today+RangeStartMinute*60,end=today+RangeEndMinute*60;
int expected=RangeEndMinute-RangeStartMinute;
int n=CopyRates(_Symbol,PERIOD_M1,start,end-1,bars);
if(n!=expected) return false;
double hi=-DBL_MAX,lo=DBL_MAX;
for(int i=0;i<n;i++)
{
if(bars[i].time!=start+i*60) return false;
hi=MathMax(hi,bars[i].high); lo=MathMin(lo,bars[i].low);
}
if(hi<=lo || lo<=0) return false;
rangeHigh=hi; rangeLow=lo; return true;
}
void OnTick()
{
MqlTick tick; if(!SymbolInfoTick(_Symbol,tick) || tick.bid<=0 || tick.ask<=0) return;
CloseExpired(tick.time);
datetime today=StringToTime(TimeToString(tick.time,TIME_DATE));
if(today!=day) { day=today; attempted=false; rangeReady=false; }
int minute=(int)((tick.time-today)/60);
if(minute<TradeStartMinute || minute>=CloseMinute || attempted || HasPosition()) return;
if(!rangeReady) rangeReady=BuildRange(today);
if(!rangeReady) return;
double atr[1]; if(CopyBuffer(atrH,0,1,1,atr)!=1 || atr[0]<=0 || atr[0]<MinATRPoints*_Point) return;
ENUM_ORDER_TYPE side;
if(tick.ask>rangeHigh+BreakoutBufferPoints*_Point) side=ORDER_TYPE_BUY;
else if(tick.bid<rangeLow-BreakoutBufferPoints*_Point) side=ORDER_TYPE_SELL;
else return;
attempted=true; // One attempt per server day while this instance remains attached.
double width=rangeHigh-rangeLow;
MqlTradeRequest req={}; MqlTradeResult res={};
req.action=TRADE_ACTION_DEAL; req.symbol=_Symbol; req.magic=MagicNumber; req.volume=Lots; req.type=side;
req.price=side==ORDER_TYPE_BUY?tick.ask:tick.bid;
req.sl=NormalizeDouble(req.price+(side==ORDER_TYPE_BUY?-0.5:0.5)*width,_Digits);
req.tp=NormalizeDouble(req.price+(side==ORDER_TYPE_BUY?1:-1)*width,_Digits);
req.type_filling=ORDER_FILLING_IOC;
if(!OrderSend(req,res) || (res.retcode!=TRADE_RETCODE_DONE && res.retcode!=TRADE_RETCODE_DONE_PARTIAL)) Print("Entry failed: ",res.retcode);
}Copy this code into MetaEditor, save it in the MQL5/Experts folder, and compile with F7.
Generate a custom US30 scalping EA
Describe the entry, exit, position, order, and risk rules you want. Pineify generates editable MQL5 source code that you can inspect and compile in MetaEditor.
Pine Script vs MQL5: Same Strategy, Different Platforms
| Aspect | Pine Script (TradingView) | MQL5 (MetaTrader 5) |
|---|---|---|
| Execution | Series evaluated on chart updates | Event handlers with explicit buffers |
| Deployment | Runs in TradingView with alerts | Runs in the MT5 terminal or on a VPS |
| Broker access | Strategy orders are simulated; live automation needs a separate integration | Direct broker connectivity |
| Backtesting | Strategy Tester for strategy scripts | Strategy Tester for Expert Advisors |
| Code complexity | Simpler, functional syntax | C++-like, more powerful |
The calculation can be implemented on either platform, but the runtime model and buffer APIs differ.
Frequently Asked Questions
Related MQL5 pages
Risk and testing note
These educational examples require independent compilation and testing. A compile pass or an illustrative setting does not establish execution correctness or profitability. Any reported performance must be supported by reproducible data and test settings; past results do not predict future returns. Trading involves significant risk of loss. This content is not financial advice.