MultiAgentTest v7: zero magic constants, neural orchestrator, multi-position, agent interaction fixes
This commit is contained in:
@@ -0,0 +1,112 @@
|
||||
#ifndef AGENT_BASE_MQH
|
||||
#define AGENT_BASE_MQH
|
||||
#include "../Core/MarketData.mqh"
|
||||
#include "../Core/Statistics.mqh"
|
||||
|
||||
double SHARED_regimeZ = 0.0;
|
||||
double SHARED_regimeH = 0.5;
|
||||
double SHARED_trendStrength = 0.0;
|
||||
double SHARED_adxZ = 0.0;
|
||||
double SHARED_adxRaw = 0.0;
|
||||
double SHARED_regimeConsensus = 0.0;
|
||||
double SHARED_regimeAgreement = 0.0;
|
||||
double SHARED_patternCode = 0.0;
|
||||
string SHARED_patternName = "";
|
||||
|
||||
class IAgent {
|
||||
public:
|
||||
string name;
|
||||
double weight;
|
||||
bool enabled;
|
||||
string symbol;
|
||||
ENUM_TIMEFRAMES timeframe;
|
||||
|
||||
double lastZScore;
|
||||
double lastRawSignal;
|
||||
KalmanNormalizer signalStats;
|
||||
|
||||
// --- Learning infrastructure ---
|
||||
RunningStats predictionError; // predictedZ - actualReturnZ (bias)
|
||||
RunningCorrelation predCorr; // correlation predictedZ vs actualReturnZ
|
||||
int learnMinSamples; // minimo campioni prima di applicare learning
|
||||
|
||||
IAgent(string n, double w=1.0)
|
||||
: name(n), weight(w), enabled(true), symbol(""), timeframe(PERIOD_CURRENT),
|
||||
lastZScore(0), lastRawSignal(0), signalStats(0.001, 1.0, 30),
|
||||
predictionError(0.1, 5, 500), predCorr(0.1, 5), learnMinSamples(5) {}
|
||||
|
||||
virtual ~IAgent() {}
|
||||
|
||||
virtual void Init(string sym, ENUM_TIMEFRAMES tf) {
|
||||
symbol = sym;
|
||||
timeframe = tf;
|
||||
}
|
||||
|
||||
virtual void Release() {}
|
||||
|
||||
virtual double Analyze(const MarketData &data) = 0;
|
||||
|
||||
virtual void Interact(IAgent *&allAgents[], int count) {}
|
||||
|
||||
// --- Apprendimento da trade outcome ---
|
||||
// predictedZ = z-score dell'agente al momento dell'entrata
|
||||
// actualReturnZ = ritorno normalizzato del trade chiuso
|
||||
virtual void Learn(double predictedZ, double actualReturnZ) {
|
||||
predictionError.Update(predictedZ - actualReturnZ);
|
||||
predCorr.Update(predictedZ, actualReturnZ);
|
||||
}
|
||||
|
||||
// Applica bias correction + confidence scaling a un raw z-score
|
||||
double CalibrateZ(double rawZ) {
|
||||
if(predictionError.Count() < learnMinSamples) return rawZ;
|
||||
|
||||
double corrected = rawZ;
|
||||
|
||||
// Bias correction: solo se statisticamente significativo (> 2 SE)
|
||||
double bias = predictionError.Mean();
|
||||
double biasSE = predictionError.Std() / MathSqrt((double)predictionError.Count());
|
||||
if(MathAbs(bias) > 2.0 * biasSE) {
|
||||
corrected -= bias * 0.3;
|
||||
}
|
||||
|
||||
// Confidence scaling: se correlazione bassa o negativa, riduci magnitudine
|
||||
if(predCorr.Ready()) {
|
||||
double rho = predCorr.Correlation();
|
||||
if(rho < 0.2) {
|
||||
corrected *= MathMax(0.05, MathMax(0.0, rho) / 0.2);
|
||||
}
|
||||
}
|
||||
|
||||
return corrected;
|
||||
}
|
||||
|
||||
virtual void Save(int fh) const {
|
||||
FileWriteInteger(fh, 3);
|
||||
signalStats.Save(fh);
|
||||
predictionError.Save(fh);
|
||||
predCorr.Save(fh);
|
||||
}
|
||||
virtual void Load(int fh) {
|
||||
int ver = FileReadInteger(fh);
|
||||
if(ver == 3) {
|
||||
signalStats.Load(fh);
|
||||
predictionError.Load(fh);
|
||||
predCorr.Load(fh);
|
||||
}
|
||||
else if(ver == 2) {
|
||||
signalStats.Load(fh);
|
||||
}
|
||||
}
|
||||
|
||||
virtual void Reset() {
|
||||
signalStats.Reset();
|
||||
predictionError.Reset();
|
||||
lastZScore = 0;
|
||||
lastRawSignal = 0;
|
||||
}
|
||||
|
||||
virtual string SignalInfo() const {
|
||||
return name + " z=" + StringFormat("%+.3f", lastZScore);
|
||||
}
|
||||
};
|
||||
#endif
|
||||
Reference in New Issue
Block a user