Vizion-Trading-EA/ReEntry.mqh
213 lines
7.4 KiB
MQL5
#ifndef REENTRY_MQH
#define REENTRY_MQH
bool OpenBuy(const string reason, double lot_multiplier = 1.0);
bool OpenSell(const string reason, double lot_multiplier = 1.0);
struct ReEntryCandidate
{
bool active;
bool is_buy;
datetime closed_time;
int attempts;
string reason;
};
ReEntryCandidate g_reentry[16];
void InitReEntry()
{
for(int i = 0; i < ArraySize(g_reentry); ++i)
{
g_reentry[i].active = false;
g_reentry[i].is_buy = false;
g_reentry[i].closed_time = 0;
g_reentry[i].attempts = 0;
g_reentry[i].reason = "";
}
}
void QueueReEntryCandidate(const bool is_buy, const string reason)
{
if(!Enable_ReEntry)
return;
// one live candidate per direction: several closes in the same second must not queue a burst
if(ReEntry_One_Per_Direction)
for(int k = 0; k < ArraySize(g_reentry); ++k)
if(g_reentry[k].active && g_reentry[k].is_buy == is_buy)
return;
for(int i = 0; i < ArraySize(g_reentry); ++i)
{
if(!g_reentry[i].active)
{
g_reentry[i].active = true;
g_reentry[i].is_buy = is_buy;
g_reentry[i].closed_time = TimeCurrent();
g_reentry[i].attempts = 0;
g_reentry[i].reason = reason;
return;
}
}
}
// Is (setup, direction) armed for a re-entry right now? (Require_Setup mode)
bool ReEntryArmedFor(const string setup, const bool is_buy, int &idx)
{
idx = -1;
if(!Enable_ReEntry || !ReEntry_Require_Setup) return false;
for(int i = 0; i < ArraySize(g_reentry); ++i)
{
if(!g_reentry[i].active || g_reentry[i].is_buy != is_buy) continue;
if(g_reentry[i].reason != setup) continue;
if(TimeCurrent() - g_reentry[i].closed_time < ReEntry_Cooldown) return false;
if(g_reentry[i].attempts >= ReEntry_Max_Attempts) { g_reentry[i].active = false; return false; }
idx = i;
return true;
}
return false;
}
// Book the result of an armed re-entry attempt made by the setup engine.
void ReEntryConsume(const int idx, const bool opened)
{
if(idx < 0 || idx >= ArraySize(g_reentry)) return;
if(!opened) return; // the refusing gate journaled why; stay armed
g_reentry[idx].attempts++;
if(g_reentry[idx].attempts >= ReEntry_Max_Attempts)
g_reentry[idx].active = false;
else
g_reentry[idx].closed_time = TimeCurrent();
}
bool CanReEnter(const int direction)
{
if(direction > 0)
return (Active_Praise_Signals >= Active_Warnings);
if(direction < 0)
return (Active_Warnings >= Active_Praise_Signals);
return false;
}
void ExecuteReEntries()
{
if(!Enable_ReEntry)
return;
for(int i = 0; i < ArraySize(g_reentry); ++i)
{
if(!g_reentry[i].active)
continue;
// Setup-validated mode: expiry is handled here, opening happens in FireSetup when the
// armed setup fires again.
if(ReEntry_Require_Setup)
{
if(ReEntry_Max_Age_Sec > 0 && TimeCurrent() - g_reentry[i].closed_time > ReEntry_Max_Age_Sec)
{
Print("ReEntry: armed ", g_reentry[i].reason, " ", g_reentry[i].is_buy ? "BUY" : "SELL", " expired - setup did not return within ", ReEntry_Max_Age_Sec, "s");
g_reentry[i].active = false;
}
continue;
}
if(TimeCurrent() - g_reentry[i].closed_time < ReEntry_Cooldown)
continue;
if(g_reentry[i].attempts >= ReEntry_Max_Attempts)
{
g_reentry[i].active = false;
continue;
}
int confirms = 0;
if(g_reentry[i].is_buy)
{
if(IsBullBias()) confirms++;
if(Trend_Resumption || Bull_MA50_Reclaimed()) confirms++;
if(StochCrossUp() || Stoch_K_Current > Stoch_Mid) confirms++;
if(Coordinator_Bias_Direction >= 0 && Coordinator_AllowTrade) confirms++;
}
else
{
if(IsBearBias()) confirms++;
if(Trend_Resumption || Bear_MA50_Reclaimed()) confirms++;
if(StochCrossDown() || Stoch_K_Current < Stoch_Mid) confirms++;
if(Coordinator_Bias_Direction <= 0 && Coordinator_AllowTrade) confirms++;
}
if(Require_Stronger_Signal && confirms < 3)
continue;
// Age out instead of dying on the first refusal (cooldown, policy, AI gate, guards) — the
// old code cancelled the candidate the moment OpenBuy/OpenSell returned false, which the
// 300 s trade cooldown guaranteed 30 s after every close.
if(ReEntry_Max_Age_Sec > 0 && TimeCurrent() - g_reentry[i].closed_time > ReEntry_Max_Age_Sec)
{
Print("ReEntry: candidate expired (", g_reentry[i].is_buy ? "BUY" : "SELL", ", ", g_reentry[i].reason, ") after ", ReEntry_Max_Age_Sec, "s");
g_reentry[i].active = false;
continue;
}
// Same direction discipline as setups: with the trend in CONTINUATION, against it in a
// pullback/reversal when Pullback_Trade_Direction is COUNTER (helpers in TradingSetups.mqh).
// Re-entries follow State the same way setups do, both ways: in CONTINUATION only with-trend
// re-entries fire; in PULLBACK / REVERSAL only counter-trend ones. A candidate whose direction
// does not match the current state waits (until it matches or the candidate ages out).
if(ReEntry_Follow_State && Current_Mode == MODE_TRENDING && Current_Bias_Direction != 0)
{
bool with_trend = (g_reentry[i].is_buy == (Current_Bias_Direction > 0));
bool cont = (Current_State == STATE_CONTINUATION);
bool allowed = cont ? with_trend
: (Pullback_Trade_Direction == PB_COUNTER_TREND ? !with_trend
: Pullback_Trade_Direction == PB_WITH_TREND ? with_trend : true);
if(!allowed)
{
static datetime last_dir_note = 0;
if(TimeCurrent() - last_dir_note >= 60)
{
Print("ReEntry: ", g_reentry[i].is_buy ? "BUY" : "SELL", " candidate waiting - ",
with_trend ? "with-trend" : "counter-trend", " re-entry needs State ",
with_trend ? "CONTINUATION" : "PULLBACK/REVERSAL", " (now ",
cont ? "CONTINUATION" : (Current_State == STATE_REVERSAL ? "REVERSAL" : "PULLBACK"),
", ", Current_Bias_Direction > 0 ? "BULL" : "BEAR", " bias)");
last_dir_note = TimeCurrent();
}
continue;
}
}
string label = "RE-ENTRY " + IntegerToString(g_reentry[i].attempts + 1) + " | " + g_reentry[i].reason;
bool ok = false;
g_reentry_attempt = true; // lets IsInCooldown() step aside for re-entries
if(g_reentry[i].is_buy)
ok = OpenBuy(label, 1.0);
else
ok = OpenSell(label, 1.0);
g_reentry_attempt = false;
if(ok)
{
Print("ReEntry: ", label, " opened (attempt ", g_reentry[i].attempts + 1, "/", ReEntry_Max_Attempts, ")");
g_reentry[i].attempts++;
if(g_reentry[i].attempts >= ReEntry_Max_Attempts)
g_reentry[i].active = false;
else
g_reentry[i].closed_time = TimeCurrent();
}
else
{
// keep the candidate and retry on later ticks; the refusing gate journals its own reason
static datetime last_note = 0;
if(TimeCurrent() - last_note >= 60)
{
Print("ReEntry: ", label, " refused this tick - will retry until it expires");
last_note = TimeCurrent();
}
}
}
}
#endif