v1.16: Add weekend protection to Confluence EA
This commit is contained in:
@@ -5,7 +5,7 @@
|
||||
//+------------------------------------------------------------------+
|
||||
#property copyright "Copyright 2025, Abbey Road Tech"
|
||||
#property link "https://www.abbeyroadtech.com"
|
||||
#property version "1.15"
|
||||
#property version "1.16"
|
||||
#property strict
|
||||
|
||||
#include <Trade\Trade.mqh>
|
||||
@@ -48,6 +48,10 @@ input bool InpDebugMode = true; // Print debug info
|
||||
input group "=== Equity Protection ==="
|
||||
input double InpMaxDailyDrawdown = 3.0; // Max daily drawdown % (0=disable)
|
||||
|
||||
input group "=== Weekend Protection ==="
|
||||
input bool InpCloseBeforeWeekend = true; // Close positions Friday before market close
|
||||
input int InpWeekendCloseHour = 17; // Hour to close (17 = 5 PM broker time)
|
||||
|
||||
//--- Global Variables
|
||||
CTrade Trade;
|
||||
int TrendMAHandle;
|
||||
@@ -62,6 +66,9 @@ int totalSellPositions = 0;
|
||||
double dailyStartEquity = 0;
|
||||
datetime lastEquityReset = 0;
|
||||
|
||||
//--- Weekend Protection
|
||||
bool weekendCloseExecuted = false;
|
||||
|
||||
//+------------------------------------------------------------------+
|
||||
//| Expert initialization function |
|
||||
//+------------------------------------------------------------------+
|
||||
@@ -85,7 +92,7 @@ int OnInit()
|
||||
|
||||
lastBarTime = iTime(_Symbol, _Period, 0);
|
||||
|
||||
Print("=== MultiSignal Confluence EA v1.15 Initialized ===");
|
||||
Print("=== MultiSignal Confluence EA v1.16 Initialized ===");
|
||||
Print("Symbol: ", _Symbol);
|
||||
Print("Magic: ", InpMagicNumber);
|
||||
Print("Min Confluence: ", InpMinConfluence);
|
||||
@@ -99,6 +106,9 @@ int OnInit()
|
||||
Print(" Trailing Stop: ", InpTrailingStop, " pts");
|
||||
Print(" Trailing Step: ", InpTrailingStep, " pts");
|
||||
}
|
||||
Print("Weekend Protection: ", InpCloseBeforeWeekend ? "ON" : "OFF");
|
||||
if(InpCloseBeforeWeekend)
|
||||
Print(" Close Hour: ", InpWeekendCloseHour, ":00 broker time");
|
||||
|
||||
return(INIT_SUCCEEDED);
|
||||
}
|
||||
@@ -689,6 +699,12 @@ void OnTick()
|
||||
return; // Don't trade if daily limit reached
|
||||
}
|
||||
|
||||
// Check weekend protection
|
||||
if(!CheckWeekendProtection())
|
||||
{
|
||||
return; // Don't trade, weekend close executed
|
||||
}
|
||||
|
||||
// Check for new bar
|
||||
datetime currentBarTime = iTime(_Symbol, _Period, 0);
|
||||
bool isNewBar = (currentBarTime != lastBarTime);
|
||||
@@ -866,4 +882,76 @@ void UpdateTrailingStops()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//+------------------------------------------------------------------+
|
||||
//| Close all positions for this EA |
|
||||
//+------------------------------------------------------------------+
|
||||
void CloseAllPositions(string reason)
|
||||
{
|
||||
int total = PositionsTotal();
|
||||
int closed = 0;
|
||||
|
||||
for(int i = total - 1; i >= 0; i--)
|
||||
{
|
||||
ulong ticket = PositionGetTicket(i);
|
||||
if(ticket == 0) continue;
|
||||
|
||||
// Only close positions for this EA and symbol
|
||||
if(PositionGetString(POSITION_SYMBOL) != _Symbol) continue;
|
||||
if(PositionGetInteger(POSITION_MAGIC) != InpMagicNumber) continue;
|
||||
|
||||
long posType = PositionGetInteger(POSITION_TYPE);
|
||||
|
||||
if(posType == POSITION_TYPE_BUY)
|
||||
{
|
||||
if(Trade.PositionClose(ticket))
|
||||
closed++;
|
||||
}
|
||||
else if(posType == POSITION_TYPE_SELL)
|
||||
{
|
||||
if(Trade.PositionClose(ticket))
|
||||
closed++;
|
||||
}
|
||||
}
|
||||
|
||||
Print("📊 Closed ", closed, " positions for weekend protection. Reason: ", reason);
|
||||
}
|
||||
|
||||
//+------------------------------------------------------------------+
|
||||
//| Check Weekend Protection |
|
||||
//+------------------------------------------------------------------+
|
||||
bool CheckWeekendProtection()
|
||||
{
|
||||
if(!InpCloseBeforeWeekend) return true;
|
||||
|
||||
MqlDateTime dt;
|
||||
TimeToStruct(TimeCurrent(), dt);
|
||||
|
||||
// Only check on Friday
|
||||
if(dt.day_of_week != FRIDAY)
|
||||
{
|
||||
// Reset flag on other days
|
||||
if(weekendCloseExecuted)
|
||||
weekendCloseExecuted = false;
|
||||
return true;
|
||||
}
|
||||
|
||||
// Already executed this Friday
|
||||
if(weekendCloseExecuted) return true;
|
||||
|
||||
// Check if it's close to weekend close time
|
||||
if(dt.hour >= InpWeekendCloseHour)
|
||||
{
|
||||
Print("⚠️ WEEKEND CLOSE: It's Friday ", dt.hour, ":00 - Closing all positions!");
|
||||
SendNotification("WEEKEND CLOSE: Closing all positions before weekend");
|
||||
|
||||
// Close all open positions
|
||||
CloseAllPositions("Weekend protection - Friday close");
|
||||
|
||||
weekendCloseExecuted = true;
|
||||
return false; // Block new trades
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
//+------------------------------------------------------------------+
|
||||
|
||||
Reference in New Issue
Block a user