74 lines
3.8 KiB
MQL5
#ifndef LADDER_RESEARCH_MQH
| |||
#define LADDER_RESEARCH_MQH
| |||
// ---------------------------------------------------------------------------
| |||
// LadderResearch.mqh — v7.27 (2026-11-09). The rotation leader scores every symbol on the weekly ladder
| |||
// (gateway watchlist_status "*" -> watch_symbols) on H1 and sends research_scores; the n8n weekly ladder
| |||
// uses them to move bench symbols up. Indicator handles are created once per symbol; a symbol whose data
| |||
// is not ready yet is skipped until the next run.
| |||
// ---------------------------------------------------------------------------
| |||
string g_rs_sym[]; int g_rs_adx[]; int g_rs_atr[]; int g_rs_e20[]; int g_rs_e50[];
| |||
| |||
int RS_Slot(const string sym)
| |||
{
| |||
int n = ArraySize(g_rs_sym);
| |||
for(int i = 0; i < n; ++i) if(g_rs_sym[i] == sym) return i;
| |||
if(!SymbolSelect(sym, true)) return -1;
| |||
ArrayResize(g_rs_sym, n + 1); ArrayResize(g_rs_adx, n + 1); ArrayResize(g_rs_atr, n + 1); ArrayResize(g_rs_e20, n + 1); ArrayResize(g_rs_e50, n + 1);
| |||
g_rs_sym[n] = sym;
| |||
g_rs_adx[n] = iADX(sym, PERIOD_H1, 14);
| |||
g_rs_atr[n] = iATR(sym, PERIOD_H1, 14);
| |||
g_rs_e20[n] = iMA(sym, PERIOD_H1, 20, 0, MODE_EMA, PRICE_CLOSE);
| |||
g_rs_e50[n] = iMA(sym, PERIOD_H1, 50, 0, MODE_EMA, PRICE_CLOSE);
| |||
return n;
| |||
}
| |||
| |||
bool RS_Last(const int handle, const int buffer, double &v)
| |||
{
| |||
double b[1];
| |||
if(handle == INVALID_HANDLE || CopyBuffer(handle, buffer, 1, 1, b) != 1) return false; // last closed bar
| |||
v = b[0]; return true;
| |||
}
| |||
| |||
void LadderResearchTick()
| |||
{
| |||
if(Research_Minutes <= 0 || !GW_Initialized) return;
| |||
static datetime last = 0;
| |||
if(last == 0) last = TimeLocal() - Research_Minutes * 60 + Leader_Start_Delay_Minutes * 60;
| |||
if(TimeLocal() - last < Research_Minutes * 60) return;
| |||
last = TimeLocal();
| |||
| |||
string resp, csv = "";
| |||
if(!GatewayRequest("watchlist_status", "{\"action\":\"watchlist_status\",\"symbol\":\"*\"}", resp)) return;
| |||
if(!GWExtractString(resp, "watch_symbols", csv) || StringLen(csv) == 0) return;
| |||
string syms[];
| |||
int n = StringSplit(csv, ',', syms);
| |||
string items = ""; int sent = 0, waiting = 0;
| |||
for(int i = 0; i < n; ++i)
| |||
{
| |||
string sym = syms[i]; StringTrimLeft(sym); StringTrimRight(sym);
| |||
if(StringLen(sym) == 0) continue;
| |||
int k = RS_Slot(sym);
| |||
if(k < 0) continue;
| |||
double adx = 0.0, atr = 0.0, e20 = 0.0, e50 = 0.0, close[];
| |||
ArraySetAsSeries(close, true);
| |||
if(!RS_Last(g_rs_adx[k], 0, adx) || !RS_Last(g_rs_atr[k], 0, atr) || !RS_Last(g_rs_e20[k], 0, e20) || !RS_Last(g_rs_e50[k], 0, e50)
| |||
|| CopyClose(sym, PERIOD_H1, 1, 25, close) != 25 || atr <= 0.0)
| |||
{ waiting++; continue; }
| |||
double path = 0.0;
| |||
for(int j = 0; j < 24; ++j) path += MathAbs(close[j] - close[j + 1]);
| |||
double er = path > 0.0 ? MathAbs(close[0] - close[24]) / path : 0.0;
| |||
double trend_atr = MathAbs(e20 - e50) / atr;
| |||
double score = 100.0 * (0.4 * er + 0.3 * MathMin(adx, 50.0) / 50.0 + 0.3 * MathMin(trend_atr, 3.0) / 3.0);
| |||
if(adx > 50.0) score *= 0.8;
| |||
if(sent > 0) items += ",";
| |||
items += "{\"symbol\":\"" + GWJsonEscape(sym) + "\",\"score\":" + DoubleToString(score, 1) + ",\"adx\":" + DoubleToString(adx, 1) +
| |||
",\"atr_pct\":" + DoubleToString(100.0 * atr / close[0], 3) + ",\"er\":" + DoubleToString(er, 3) +
| |||
",\"trend_atr\":" + DoubleToString(trend_atr, 2) + ",\"dir\":\"" + (e20 > e50 ? "up" : "down") + "\"}";
| |||
sent++;
| |||
}
| |||
if(sent == 0) { if(waiting > 0) last = TimeLocal() - Research_Minutes * 60 + 120; return; } // data loading: retry in 2 min
| |||
string payload = "{\"action\":\"research_scores\",\"scores\":[" + items + "]}";
| |||
if(GatewayRequest("research_scores", payload, resp))
| |||
Print("LadderResearch: scored ", sent, " ladder symbols", (waiting > 0 ? " (" + IntegerToString(waiting) + " still loading data)" : ""));
| |||
}
| |||
#endif
|