DislikedSorry amvt My bad... I had the settings at 36 15 3 instead of 12 5 3 It is not you it's me LOL This is the zoomed out chart in question I think all the arrows are there now both indis set to 12 5 3 {image} I want to show something I have not seen yet...an anomaly, we have a red arrow after a swing high but no swing low first. Again, as above, it may be because of my higher settings {image}Ignored
//+------------------------------------------------------------------+
//| XAUUSD M5 Trendline Break EA (FINAL RUN VERSION) |
//| New York Session Only | SL 40 | TP 60 | BE@40 | Daily Fuse |
//+------------------------------------------------------------------+
#property strict
// === Risk Management Parameters ===
input double LotPer100 = 0.02; // Lots per 100 USD
input int StopLossPts = 400; // Stop Loss 40 points
input int TakeProfitPts = 600; // Take Profit 60 points
input int BreakEvenPts = 400; // Move to breakeven at +40 points
input double MaxDailyLossPct = 6.0; // Daily fuse (max loss %)
input int ATR_Min_Pts = 1000; // Minimum volatility filter (100 points)
// === New York Session (Server Time) ===
input int NY_StartHour = 20;
input int NY_StartMin = 0;
input int NY_EndHour = 23;
input int NY_EndMin = 30;
// === ZigZag Parameters ===
input int ZZ_Depth = 12;
input int ZZ_Deviation = 5;
input int ZZ_Backstep = 3;
// === Global Variables ===
int zzHandle;
double DayStartBalance;
int DayFlag;
//+------------------------------------------------------------------+
int OnInit()
{
zzHandle = iCustom(_Symbol, PERIOD_M5, "ZigZag",
ZZ_Depth, ZZ_Deviation, ZZ_Backstep);
if(zzHandle == INVALID_HANDLE)
{
Print("Failed to create ZigZag indicator handle");
return INIT_FAILED;
}
ResetDay();
return INIT_SUCCEEDED;
}
//+------------------------------------------------------------------+
void OnTick()
{
CheckNewDay();
// Daily loss fuse protection
if(IsDailyFuse()) return;
// Only trade during New York session
if(!IsNewYorkSession()) return;
// Volatility filter (ATR)
if(!HasVolatility()) return;
// Manage breakeven
ManageBreakEven();
// Only open one position at a time
if(PositionsTotal() > 0) return;
// Check for trendline breakout
if(TrendBreak(true))
OpenTrade(ORDER_TYPE_BUY);
else if(TrendBreak(false))
OpenTrade(ORDER_TYPE_SELL);
}
//+------------------------------------------------------------------+
void ResetDay()
{
DayStartBalance = AccountInfoDouble(ACCOUNT_BALANCE);
DayFlag = TimeDay(TimeCurrent());
}
//+------------------------------------------------------------------+
void CheckNewDay()
{
int currentDay = TimeDay(TimeCurrent());
if(currentDay != DayFlag)
ResetDay();
}
//+------------------------------------------------------------------+
bool IsDailyFuse()
{
double equity = AccountInfoDouble(ACCOUNT_EQUITY);
double lossPct = (DayStartBalance - equity) / DayStartBalance * 100.0;
if(lossPct >= MaxDailyLossPct)
{
// Optional: Print("Daily loss limit reached. Trading stopped for today.");
return true;
}
return false;
}
//+------------------------------------------------------------------+
bool IsNewYorkSession()
{
int h = TimeHour(TimeCurrent());
int m = TimeMinute(TimeCurrent());
if(h > NY_StartHour && h < NY_EndHour) return true;
if(h == NY_StartHour && m >= NY_StartMin) return true;
if(h == NY_EndHour && m <= NY_EndMin) return true;
return false;
}
//+------------------------------------------------------------------+
bool HasVolatility()
{
double atr = iATR(_Symbol, PERIOD_M5, 14, 0);
return (atr >= ATR_Min_Pts * _Point);
}
//+------------------------------------------------------------------+
bool TrendBreak(bool isBuy)
{
int bar1 = 14; // older bar
int bar2 = 1; // most recent closed bar
double p1 = Close[bar1];
double p2 = Close[bar2];
datetime t1 = Time[bar1];
datetime t2 = Time[bar2];
// Draw the trendline for visual reference (optional)
DrawTrendline(t1, p1, t2, p2);
// Calculate slope
double slope = (p2 - p1) / (bar2 - bar1);
// Project price 10 bars into the future
double projected = p2 + slope * 10;
double currentPrice = isBuy ? Ask : Bid;
// Breakout condition
if(isBuy && currentPrice > projected) return true;
if(!isBuy && currentPrice < projected) return true;
return false;
}
//+------------------------------------------------------------------+
void DrawTrendline(datetime t1, double p1, datetime t2, double p2)
{
string name = "EA_TREND";
ObjectDelete(0, name);
ObjectCreate(0, name, OBJ_TREND, 0, t1, p1, t2, p2);
ObjectSetInteger(0, name, OBJPROP_COLOR, clrDodgerBlue);
ObjectSetInteger(0, name, OBJPROP_STYLE, STYLE_SOLID);
ObjectSetInteger(0, name, OBJPROP_WIDTH, 1);
ObjectSetInteger(0, name, OBJPROP_RAY_RIGHT, true);
}
//+------------------------------------------------------------------+
void OpenTrade(ENUM_ORDER_TYPE type)
{
double lot = NormalizeDouble(AccountInfoDouble(ACCOUNT_BALANCE)/100.0 * LotPer100, 2);
double price = (type == ORDER_TYPE_BUY) ? Ask : Bid;
double sl = (type == ORDER_TYPE_BUY) ?
price - StopLossPts * _Point :
price + StopLossPts * _Point;
double tp = (type == ORDER_TYPE_BUY) ?
price + TakeProfitPts * _Point :
price - TakeProfitPts * _Point;
MqlTradeRequest req = {};
MqlTradeResult res = {};
req.action = TRADE_ACTION_DEAL;
req.symbol = _Symbol;
req.volume = lot;
req.type = type;
req.price = price;
req.sl = sl;
req.tp = tp;
req.magic = 406060;
req.deviation = 30;
if(!OrderSend(req, res))
{
Print("OrderSend failed: ", GetLastError());
}
}
//+------------------------------------------------------------------+
void ManageBreakEven()
{
for(int i = 0; i < PositionsTotal(); i++)
{
ulong ticket = PositionGetTicket(i);
if(!PositionSelectByTicket(ticket)) continue;
double openPrice = PositionGetDouble(POSITION_PRICE_OPEN);
double currentSL = PositionGetDouble(POSITION_SL);
double tp = PositionGetDouble(POSITION_TP);
long posType = PositionGetInteger(POSITION_TYPE);
double currentPrice = (posType == POSITION_TYPE_BUY) ? Bid : Ask;
// Move to breakeven if profit >= BreakEvenPts
if(MathAbs(currentPrice - openPrice) >= BreakEvenPts * _Point)
{
// Only modify if SL is not yet at open price
if((posType == POSITION_TYPE_BUY && currentSL < openPrice) ||
(posType == POSITION_TYPE_SELL && currentSL > openPrice))
{
MqlTradeRequest req = {};
MqlTradeResult res = {};
req.action = TRADE_ACTION_SLTP;
req.position = ticket;
req.symbol = _Symbol;
req.sl = openPrice;
req.tp = tp;
if(!OrderSend(req, res))
{
Print("Breakeven modification failed: ", GetLastError());
}
}
}
}
}
//+------------------------------------------------------------------+