Certainly! Below is the full fixed and improved version of your NAS100 breakout Expert Advisor for MetaTrader 5, implementing your requested logic with proper time handling, session high/low marking, breakout detection, and confirmation on 1-minute chart.
Important:
- Adjust the input parameter
ServerToNYOffset according to your broker's server time zone offset from New York.
- Make sure symbol and timeframes are correct.
- The EA assumes trading NAS100 or similar instrument.
- Test extensively on demo before live trading.
//+------------------------------------------------------------------+
//| NAS100Breakout.mq5|
//| Example EA for NAS100 breakout strategy |
//+------------------------------------------------------------------+
#property copyright "ChatGPT"
#property link ""
#property version "1.01"
#property strict
input double Lots = 0.1; // Lot size
input int Slippage = 10; // Max slippage in points
input double RiskRewardRatio = 2.0; // TP multiplier relative to SL
input int StopLossPoints = 50; // Stop loss distance in points (adjust for instrument)
input ENUM_TIMEFRAMES ConfirmTF = PERIOD_M1; // Confirmation timeframe
input int ServerToNYOffset = -4; // Broker server time offset from NY (e.g. -4 for EDT)
// Session times (in NY time)
datetime asiaSessionStart, asiaSessionEnd;
datetime londonSessionStart, londonSessionEnd;
// Session highs/lows
double asiaHigh, asiaLow;
double londonHigh, londonLow;
// Trading state variables
bool breakoutTriggered = false;
double breakoutLevel = 0.0;
bool breakoutUp = false; // true if breakout above high, false if breakout below low
bool positionOpened = false;
// Last day tracked (for session reset)
datetime lastSessionResetDay = 0;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
ResetSessions();
Print("NAS100 Breakout EA initialized.");
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
}
//+------------------------------------------------------------------+
//| Normalize datetime to 00:00:00 (midnight) of that day |
//+------------------------------------------------------------------+
datetime NormalizeDateTime(datetime dt)
{
MqlDateTime ldt;
TimeToStruct(dt, ldt);
ldt.hour = 0;
ldt.min = 0;
ldt.sec = 0;
return StructToTime(ldt);
}
//+------------------------------------------------------------------+
//| Reset session times and highs/lows |
//+------------------------------------------------------------------+
void ResetSessions()
{
// Get current time and NY time
datetime serverTime = TimeCurrent();
datetime nyTimeNow = serverTime + ServerToNYOffset * 3600;
datetime nyDayStart = NormalizeDateTime(nyTimeNow);
// Set session times in NY time
// Asia session: previous day 18:00 - 00:00 (current day)
asiaSessionStart = nyDayStart - 6 * 3600; // 18:00 previous day
asiaSessionEnd = nyDayStart; // 00:00 current day
// London session: 00:00 - 06:00 current day
londonSessionStart = nyDayStart;
londonSessionEnd = nyDayStart + 6 * 3600;
// Reset highs and lows
asiaHigh = -DBL_MAX;
asiaLow = DBL_MAX;
londonHigh = -DBL_MAX;
londonLow = DBL_MAX;
// Reset trading flags
breakoutTriggered = false;
breakoutLevel = 0.0;
breakoutUp = false;
positionOpened = false;
lastSessionResetDay = nyDayStart;
PrintFormat("Sessions reset for day %s", TimeToString(nyDayStart, TIME_DATE));
}
//+------------------------------------------------------------------+
//| OnTick |
//+------------------------------------------------------------------+
void OnTick()
{
datetime serverTime = TimeCurrent();
datetime nyTimeNow = serverTime + ServerToNYOffset * 3600;
datetime nyDayStart = NormalizeDateTime(nyTimeNow);
// Reset sessions if new day
if(nyDayStart != lastSessionResetDay)
{
ResetSessions();
}
// Update session highs/lows dynamically while sessions active
if(nyTimeNow >= asiaSessionStart && nyTimeNow < asiaSessionEnd)
UpdateSessionHighLow(asiaSessionStart, asiaSessionEnd, asiaHigh, asiaLow);
if(nyTimeNow >= londonSessionStart && nyTimeNow < londonSessionEnd)
UpdateSessionHighLow(londonSessionStart, londonSessionEnd, londonHigh, londonLow);
// US Market open time 9:30 AM NY time
datetime usMarketOpen = nyDayStart + 9*3600 + 30*60;
// Wait for breakout only after market open, if no breakout triggered yet, and no position opened
if(nyTimeNow >= usMarketOpen && !breakoutTriggered && !positionOpened)
{
double sessionHigh = MathMax(asiaHigh, londonHigh);
double sessionLow = MathMin(asiaLow, londonLow);
double currentBid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
double currentAsk = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
// Detect breakout above high
if(currentBid > sessionHigh)
{
breakoutTriggered = true;
breakoutLevel = sessionHigh;
breakoutUp = true;
PrintFormat("Breakout up detected at %.5f", breakoutLevel);
}
// Detect breakout below low
else if(currentAsk < sessionLow)
{
breakoutTriggered = true;
breakoutLevel = sessionLow;
breakoutUp = false;
PrintFormat("Breakout down detected at %.5f", breakoutLevel);
}
}
// If breakout triggered and position not opened, check 1-minute confirmation
if(breakoutTriggered && !positionOpened)
{
if(ConfirmBreakout())
{
// Enter trade based on breakout direction
EnterTrade(breakoutUp);
positionOpened = true;
}
}
}
//+------------------------------------------------------------------+
//| Update session high and low for the given period on M15 |
//+------------------------------------------------------------------+
void UpdateSessionHighLow(datetime sessionStart, datetime sessionEnd, double &sessionHigh, double &sessionLow)
{
// Find bar indexes for session start and end on M15 timeframe
int barStart = iBarShift(_Symbol, PERIOD_M15, sessionStart, true);
int barEnd = iBarShift(_Symbol, PERIOD_M15, sessionEnd, true);
if(barStart < 0 || barEnd < 0)
return;
// Loop through bars in session range and update highs/lows
// Note: bar indexes go backward in time, bar with smaller index is later
for(int bar = barEnd; bar >= barStart; bar--)
{
double high = iHigh(_Symbol, PERIOD_M15, bar);
double low = iLow(_Symbol, PERIOD_M15, bar);
if(high > sessionHigh)
sessionHigh = high;
if(low < sessionLow)
sessionLow = low;
}
}
//+------------------------------------------------------------------+
//| Confirm breakout on 1-minute chart |
//+------------------------------------------------------------------+
bool ConfirmBreakout()
{
// Need at least 2 bars on confirmation tf to check last closed candle
if(iBars(_Symbol, ConfirmTF) < 2)
return false;
// Last closed candle index = 1
double openPrice = iOpen(_Symbol, ConfirmTF, 1);
double closePrice = iClose(_Symbol, ConfirmTF, 1);
if(breakoutUp)
{
// Confirm breakout up: candle closed above breakout level
if(closePrice > breakoutLevel)
{
PrintFormat("Breakout confirmation UP on 1min candle closed at %.5f > breakout level %.5f", closePrice, breakoutLevel);
return true;
}
}
else
{
// Confirm breakout down: candle closed below breakout level
if(closePrice < breakoutLevel)
{
PrintFormat("Breakout confirmation DOWN on 1min candle closed at %.5f < breakout level %.5f", closePrice, breakoutLevel);
return true;
}
}
return false;
}
//+------------------------------------------------------------------+
//| Enter trade with proper SL and TP |
//+------------------------------------------------------------------+
void EnterTrade(bool isLong)
{
double price, sl, tp;
double point = SymbolInfoDouble(_Symbol, SYMBOL_POINT);
int digits = (int)SymbolInfoInteger(_Symbol, SYMBOL_DIGITS);
if(isLong)
{
price = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
sl = breakoutLevel - StopLossPoints * point;
tp = price + RiskRewardRatio * (price - sl);
}
else
{
price = SymbolInfoDouble(_Symbol, SYMBOL_BID);
sl = breakoutLevel + StopLossPoints * point;
tp = price - RiskRewardRatio * (sl - price);
}
MqlTradeRequest request;
MqlTradeResult result;
ZeroMemory(request);
ZeroMemory(result);
request.action = TRADE_ACTION_DEAL;
request.symbol = _Symbol;
request.volume = Lots;
request.type = isLong ? ORDER_TYPE_BUY : ORDER_TYPE_SELL;
request.price = price;
request.sl = NormalizeDouble(sl, digits);
request.tp = NormalizeDouble(tp, digits);
request.deviation = Slippage;
request.magic = 123456; // Magic number to identify EA trades
if(!OrderSend(request, result))
{
PrintFormat("OrderSend failed with error %d", GetLastError());
}
else
{
if(result.retcode == TRADE_RETCODE_DONE)
PrintFormat("Trade opened: %s at price %.5f SL=%.5f TP=%.5f", isLong ? "LONG" : "SHORT", price, sl, tp);
else
PrintFormat("OrderSend failed, retcode: %d", result.retcode);
}
}
How This Works:
- Resets session times and session highs/lows at midnight NY time.
- Tracks Asia session (18:00-00:00 NY) and London session (00:00-06:00 NY) highs/lows on M15.
- At 9:30 AM NY time, watches for breakout above combined session high or below combined session low.
- Once breakout detected, checks 1-minute candle close on the breakout side to confirm.
- Places market order with SL and TP after confirmation.
- Prints info messages to Experts tab for progress and debugging.
Adjustments You May Need:
ServerToNYOffset: change to your broker's server offset from NY time.
StopLossPoints and RiskRewardRatio: tune to your preferred values.
- Enhance money management.
- Add trailing stops or time limits.
- Add error handling or more filters.
If you want me to add any enhancements or specific money management or multi-timeframe display, just tell me!