Fix warningPrinted scope error - move declaration to function level
This commit is contained in:
@@ -42,6 +42,9 @@ input ulong InpMagicNumber = 777777; // Magic number
|
||||
input int InpSlippage = 3; // Max slippage
|
||||
input bool InpDebugMode = true; // Print debug info
|
||||
|
||||
input group "=== Equity Protection ==="
|
||||
input double InpMaxDailyDrawdown = 3.0; // Max daily drawdown % (0=disable)
|
||||
|
||||
//--- Global Variables
|
||||
CTrade Trade;
|
||||
int TrendMAHandle;
|
||||
@@ -52,6 +55,10 @@ datetime lastBarTime = 0;
|
||||
int totalBuyPositions = 0;
|
||||
int totalSellPositions = 0;
|
||||
|
||||
//--- Daily Drawdown Protection
|
||||
double dailyStartEquity = 0;
|
||||
datetime lastEquityReset = 0;
|
||||
|
||||
//+------------------------------------------------------------------+
|
||||
//| Expert initialization function |
|
||||
//+------------------------------------------------------------------+
|
||||
@@ -293,21 +300,44 @@ void CheckSignals(int &buyCount, int &sellCount, string &sources)
|
||||
|
||||
// Dynamic threshold based on symbol point value
|
||||
double point = GetSymbolPoint();
|
||||
double threshold = 0.0010; // Keep original 10 pips threshold for strong signals
|
||||
double threshold = 0.0010; // 10 pips threshold for strong signals
|
||||
|
||||
// Buy at support
|
||||
if((MathAbs(close - s1) < threshold) || (MathAbs(close - s2) < threshold) ||
|
||||
(low <= s1 && close > s1) || (low <= s2 && close > s2))
|
||||
// Calculate distance to each level
|
||||
double distToS1 = MathAbs(close - s1);
|
||||
double distToS2 = MathAbs(close - s2);
|
||||
double distToR1 = MathAbs(close - r1);
|
||||
double distToR2 = MathAbs(close - r2);
|
||||
double distToP = MathAbs(close - p);
|
||||
|
||||
// Buy at support (price near support OR bounced off support)
|
||||
bool nearSupport = (distToS1 < threshold) || (distToS2 < threshold);
|
||||
bool bouncedFromSupport = (low <= s1 && close > s1) || (low <= s2 && close > s2) ||
|
||||
(low <= s1 * 1.0005 && close > s1); // Slight buffer
|
||||
bool abovePivot = close > p; // Above pivot = bullish context
|
||||
|
||||
if(nearSupport || (bouncedFromSupport && abovePivot))
|
||||
{
|
||||
buyCount++;
|
||||
sources += "P ";
|
||||
sources += "P" + IntegerToString((int)(distToS1 < distToS2 ? distToS1*10000 : distToS2*10000)) + " ";
|
||||
}
|
||||
// Sell at resistance
|
||||
else if((MathAbs(close - r1) < threshold) || (MathAbs(close - r2) < threshold) ||
|
||||
(high >= r1 && close < r1) || (high >= r2 && close < r2))
|
||||
|
||||
// Sell at resistance (price near resistance OR rejected from resistance)
|
||||
bool nearResistance = (distToR1 < threshold) || (distToR2 < threshold);
|
||||
bool rejectedFromResistance = (high >= r1 && close < r1) || (high >= r2 && close < r2) ||
|
||||
(high >= r1 * 0.9995 && close < r1); // Slight buffer
|
||||
bool belowPivot = close < p; // Below pivot = bearish context
|
||||
|
||||
if(nearResistance || (rejectedFromResistance && belowPivot))
|
||||
{
|
||||
sellCount++;
|
||||
sources += "P ";
|
||||
sources += "P" + IntegerToString((int)(distToR1 < distToR2 ? distToR1*10000 : distToR2*10000)) + " ";
|
||||
}
|
||||
|
||||
if(InpDebugMode && (nearSupport || nearResistance || bouncedFromSupport || rejectedFromResistance))
|
||||
{
|
||||
Print("Pivot Check: Close=", close, " P=", p, " R1=", r1, " S1=", s1,
|
||||
" | DistS1=", distToS1, " DistR1=", distToR1,
|
||||
" | AboveP=", abovePivot, " BelowP=", belowPivot);
|
||||
}
|
||||
|
||||
//--- CANDLESTICK SIGNALS
|
||||
@@ -594,11 +624,57 @@ void OpenSellPosition(double strength, string sources)
|
||||
}
|
||||
}
|
||||
|
||||
//+------------------------------------------------------------------+
|
||||
//| 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, "%)");
|
||||
warningPrinted = true;
|
||||
}
|
||||
return false; // Block new trades
|
||||
}
|
||||
|
||||
warningPrinted = false; // Reset when below limit
|
||||
return true;
|
||||
}
|
||||
|
||||
//+------------------------------------------------------------------+
|
||||
//| Expert tick function |
|
||||
//+------------------------------------------------------------------+
|
||||
void OnTick()
|
||||
{
|
||||
// Check daily drawdown limit first
|
||||
if(!CheckDailyDrawdown())
|
||||
{
|
||||
return; // Don't trade if daily limit reached
|
||||
}
|
||||
|
||||
// Check for new bar
|
||||
datetime currentBarTime = iTime(_Symbol, _Period, 0);
|
||||
bool isNewBar = (currentBarTime != lastBarTime);
|
||||
@@ -634,10 +710,22 @@ void OnTick()
|
||||
if(InpDebugMode)
|
||||
Print(_Symbol, " Bar ", TimeToString(currentBarTime), " Buy: ", buyCount, " Sell: ", sellCount, " Sources: ", sources);
|
||||
|
||||
// Calculate strength
|
||||
// Calculate strength (FIXED: handle conflicting signals properly)
|
||||
double strength = 0;
|
||||
if(buyCount > 0) strength = 0.6 + (buyCount * 0.15);
|
||||
if(sellCount > 0) strength = -(0.6 + (sellCount * 0.15));
|
||||
if(buyCount > 0 && sellCount == 0)
|
||||
strength = 0.6 + (buyCount * 0.15); // Pure buy signals
|
||||
else if(sellCount > 0 && buyCount == 0)
|
||||
strength = -(0.6 + (sellCount * 0.15)); // Pure sell signals
|
||||
else if(buyCount > 0 && sellCount > 0)
|
||||
{
|
||||
// Conflicting signals - only trade if one side is clearly stronger
|
||||
if(buyCount > sellCount + 1)
|
||||
strength = 0.6 + (buyCount * 0.15); // Buy dominates
|
||||
else if(sellCount > buyCount + 1)
|
||||
strength = -(0.6 + (sellCount * 0.15)); // Sell dominates
|
||||
else
|
||||
strength = 0; // Too conflicting
|
||||
}
|
||||
if(strength > 1.0) strength = 1.0;
|
||||
if(strength < -1.0) strength = -1.0;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user