Fix warningPrinted scope error - move declaration to function level

This commit is contained in:
2026-03-29 22:23:35 -04:00
parent 2f8c24a5d2
commit 04756ef2bd
6 changed files with 746 additions and 20 deletions

View File

@@ -58,6 +58,7 @@ input int LotsFactorPercent= 0;
input int BaseEquity= 10000;
input bool Master= false;
input bool DiagnosticModeOn= false;
input double InpMaxDailyDrawdown = 3.0; // Max daily drawdown % (0=disable)
//--- Trade Object
CTrade trade;
@@ -116,6 +117,51 @@ bool TradeExecutedToday = false;
bool R1HitToday = false;
bool S1HitToday = false;
//--- Daily Drawdown Protection Variables
double dailyStartEquity = 0;
datetime lastEquityReset = 0;
//+------------------------------------------------------------------+
//| Check Daily Drawdown Protection |
//+------------------------------------------------------------------+
bool CheckDailyDrawdown()
{
static bool warningPrinted = false;
if(InpMaxDailyDrawdown <= 0) return true; // Protection disabled
datetime today = TimeCurrent() / 86400 * 86400;
if(today != lastEquityReset)
{
// New day - reset equity tracking
dailyStartEquity = AccountInfoDouble(ACCOUNT_EQUITY);
lastEquityReset = today;
warningPrinted = false; // Reset warning for new day
Print("Daily equity reset: $", DoubleToString(dailyStartEquity, 2));
return true;
}
double currentEquity = AccountInfoDouble(ACCOUNT_EQUITY);
if(dailyStartEquity <= 0) return true;
double drawdownPercent = (dailyStartEquity - currentEquity) / dailyStartEquity * 100;
if(drawdownPercent >= InpMaxDailyDrawdown)
{
if(!warningPrinted)
{
Print("⚠️ DAILY DRAWDOWN LIMIT REACHED: ", DoubleToString(drawdownPercent, 2),
"% (Limit: ", InpMaxDailyDrawdown, "%)");
SendNotification("Grid EA: Daily drawdown limit reached!");
warningPrinted = true;
}
return false; // Block new trades
}
warningPrinted = false; // Reset when below limit
return true;
}
//+------------------------------------------------------------------+
//| Calculate Pivot Points |
//+------------------------------------------------------------------+
@@ -152,12 +198,17 @@ void CalculatePivotPoints()
if(UseATRFilter && atr > 0)
{
PivotR1 = NormalizeDouble(PivotP + (atr * ATRMultiplier), _Digits);
PivotS1 = NormalizeDouble(PivotP - (atr * ATRMultiplier), _Digits);
HIGH = NormalizeDouble(PivotP + (atr * ATRMultiplier), _Digits);
LOW = NormalizeDouble(PivotP - (atr * ATRMultiplier), _Digits);
}
else
{
// Use standard pivot levels
HIGH = PivotR1;
LOW = PivotS1;
}
// Use the calculated values
// Note: In actual usage, you would set HIGH/LOW from inputs
Print("AutoPivots Set: HIGH=", HIGH, " LOW=", LOW, " ATR=", atr);
}
Print("Pivot Calculated: P=", PivotP, " R1=", PivotR1, " S1=", PivotS1);
@@ -552,12 +603,25 @@ void OnTick()
MqlDateTime dt;
TimeToStruct(TimeCurrent(), dt);
// Recalculate pivots at new day (hour 0, first 5 minutes)
if(dt.hour == 0 && dt.min < 5)
// Check daily drawdown limit
if(!CheckDailyDrawdown())
{
Print("NEW DAY - Cancelling old grid and recalculating pivots");
// Drawdown limit reached - don't place new grids
if(gridPlaced)
{
Print("Daily drawdown limit reached - not placing new grids");
gridPlaced = false;
}
return;
}
// Recalculate pivots at new day (hour 0, first 5 minutes)
// Only cancel PENDING orders, let positions run to SL/TP
if(dt.hour == 0 && dt.min < 5 && gridPlaced)
{
Print("NEW DAY - Cancelling pending orders and recalculating pivots");
CancelAllOrders("End of day - new pivot calculation");
CloseAllPositions("End of day - close positions");
// NOTE: Positions are NOT closed - they continue to SL/TP
CalculatePivotPoints();
gridPlaced = false; // Reset grid for new day
}