#ifndef VZ_CORE_MQH #define VZ_CORE_MQH // --------------------------------------------------------------------------- // VZ_Core.mqh — shared feature math for the VZ_* indicators and the EA (2026-09-16) // // One implementation of everything the EA computes in Indicators.mqh / FibLevels.mqh / // MarketState.mqh / NNFeatures.mqh, expressed per bar (series index i) so indicators, // the export service, backtests and training all see the same numbers. // // Parameters are the EA's: EMA 7/14/21/50/140/230/500/1400 on close, Stoch 14/3/3 SMA // low/high, ATR 14, ADX 14, fib lookback max(Fib_Lookback_Bars, 120). // // EA convention note: the EA fills MA_Current from CopyBuffer(...,0,2,b2) into a // non-series array, so its "current" MA is bar 1 and its "previous" is bar 0; its slope // features f4/f5 are therefore sign-inverted. VZ_Features() computes the true slope // (bar i minus bar i+1) and negates it when ea_compat is true, so exported features // match what the current model was trained on. Flip ea_compat off after retraining. // --------------------------------------------------------------------------- #define VZ_MA_COUNT 8 const int VZ_MA_PERIODS[VZ_MA_COUNT] = {7, 14, 21, 50, 140, 230, 500, 1400}; enum VZ_MODE { VZ_MODE_TRENDING = 0, VZ_MODE_RANGING = 1, VZ_MODE_CHOP = 2 }; struct VZ_Handles { int ma[VZ_MA_COUNT]; int stoch; int atr; int adx; }; struct VZ_Bar { double ma[VZ_MA_COUNT]; // EMA values at bar i double ma_prev[VZ_MA_COUNT]; // EMA values at bar i+1 double stoch_k, stoch_d; double atr, adx; double close; double fib_low, fib_high; // lookback extremes ending at bar i double level[7]; // fib ladder: 0, .236, .382, .5, .618, .786, 1 double mfib_236, mfib_382, mfib_500, mfib_618, mfib_786; // anchored at 0.32 in bias direction int bias; // 1 bull, -1 bear, 0 none (EA IsBullBias/IsBearBias) int mode; // VZ_MODE bool stack_bull, stack_bear; }; bool VZ_Init(VZ_Handles &h, const string sym, const ENUM_TIMEFRAMES tf) { bool ok = true; for(int i = 0; i < VZ_MA_COUNT; ++i) { h.ma[i] = iMA(sym, tf, VZ_MA_PERIODS[i], 0, MODE_EMA, PRICE_CLOSE); if(h.ma[i] == INVALID_HANDLE) ok = false; } h.stoch = iStochastic(sym, tf, 14, 3, 3, MODE_SMA, STO_LOWHIGH); h.atr = iATR(sym, tf, 14); h.adx = iADX(sym, tf, 14); if(h.stoch == INVALID_HANDLE || h.atr == INVALID_HANDLE || h.adx == INVALID_HANDLE) ok = false; return ok; } void VZ_Release(VZ_Handles &h) { for(int i = 0; i < VZ_MA_COUNT; ++i) if(h.ma[i] != INVALID_HANDLE) IndicatorRelease(h.ma[i]); if(h.stoch != INVALID_HANDLE) IndicatorRelease(h.stoch); if(h.atr != INVALID_HANDLE) IndicatorRelease(h.atr); if(h.adx != INVALID_HANDLE) IndicatorRelease(h.adx); } // Read two values (bar i and bar i+1) from an indicator buffer, series-ordered. bool VZ_Read2(const int handle, const int buffer, const int i, double &cur, double &prev) { double tmp[]; ArraySetAsSeries(tmp, true); if(CopyBuffer(handle, buffer, i, 2, tmp) < 2) return false; cur = tmp[0]; prev = tmp[1]; return true; } // Fill a VZ_Bar for series index i. Returns false while indicators are still loading. bool VZ_Fill(const VZ_Handles &h, const string sym, const ENUM_TIMEFRAMES tf, const int i, const int fib_lookback, VZ_Bar &b) { double d; for(int k = 0; k < VZ_MA_COUNT; ++k) if(!VZ_Read2(h.ma[k], 0, i, b.ma[k], b.ma_prev[k])) return false; if(!VZ_Read2(h.stoch, 0, i, b.stoch_k, d)) return false; if(!VZ_Read2(h.stoch, 1, i, b.stoch_d, d)) return false; if(!VZ_Read2(h.atr, 0, i, b.atr, d)) return false; if(!VZ_Read2(h.adx, 0, i, b.adx, d)) return false; b.close = iClose(sym, tf, i); if(b.close <= 0.0) return false; // Fib ladder over the lookback window ending at bar i (EA: CalculateLevels). int bars = MathMax(fib_lookback, 120); int hi = iHighest(sym, tf, MODE_HIGH, bars, i); int lo = iLowest(sym, tf, MODE_LOW, bars, i); if(hi < 0 || lo < 0) return false; b.fib_high = iHigh(sym, tf, hi); b.fib_low = iLow(sym, tf, lo); double range = b.fib_high - b.fib_low; if(range <= 0.0) return false; b.level[0] = b.fib_low; b.level[1] = b.fib_low + range * 0.236; b.level[2] = b.fib_low + range * 0.382; b.level[3] = b.fib_low + range * 0.500; b.level[4] = b.fib_low + range * 0.618; b.level[5] = b.fib_low + range * 0.786; b.level[6] = b.fib_high; // Bias (EA: IsBullBias / IsBearBias on EMA7 vs EMA50 vs EMA140) bool bull = (b.ma[0] > b.ma[3] && b.ma[3] > b.ma[4]); bool bear = (b.ma[0] < b.ma[3] && b.ma[3] < b.ma[4]); b.bias = bull ? 1 : (bear ? -1 : 0); // "Anchored and stacked at 0.32" in the bias direction (EA: MFIB_Level_*) double anchor = bear ? (b.fib_high - range * 0.32) : (b.fib_low + range * 0.32); double sgn = bear ? -1.0 : 1.0; b.mfib_236 = anchor + range * 0.236 * sgn; b.mfib_382 = anchor + range * 0.382 * sgn; b.mfib_500 = anchor + range * 0.500 * sgn; b.mfib_618 = anchor + range * 0.618 * sgn; b.mfib_786 = anchor + range * 0.786 * sgn; // Market mode (EA: UpdateModeAndState) b.stack_bull = (b.ma[0] > b.ma[1] && b.ma[1] > b.ma[2] && b.ma[2] > b.ma[3]); b.stack_bear = (b.ma[0] < b.ma[1] && b.ma[1] < b.ma[2] && b.ma[2] < b.ma[3]); double gap_pts = MathAbs(b.ma[0] - b.ma[3]) / SymbolInfoDouble(sym, SYMBOL_POINT); bool low_energy = (b.adx < 16.0 || gap_pts < 80.0); if((b.stack_bull || b.stack_bear) && b.adx >= 20.0) b.mode = VZ_MODE_TRENDING; else if(low_energy) b.mode = VZ_MODE_CHOP; else b.mode = VZ_MODE_RANGING; return true; } // The 12 NN features (EA: BuildNNFeatures). ea_compat negates the two slopes (see header). void VZ_Features(const VZ_Bar &b, const double point, const bool ea_compat, double &f[]) { ArrayResize(f, 12); f[0] = (b.ma[0] - b.ma[1]) / point; f[1] = (b.ma[1] - b.ma[2]) / point; f[2] = (b.ma[2] - b.ma[3]) / point; f[3] = (b.ma[3] - b.ma[4]) / point; double s0 = (b.ma[0] - b.ma_prev[0]) / point; double s1 = (b.ma[1] - b.ma_prev[1]) / point; f[4] = ea_compat ? -s0 : s0; f[5] = ea_compat ? -s1 : s1; f[6] = b.stoch_k; f[7] = b.stoch_d; f[8] = b.adx; f[9] = (b.atr > 0.0) ? (b.atr / b.close) * 10000.0 : 0.0; f[10] = (b.close - b.mfib_382) / point; f[11] = (b.close - b.mfib_618) / point; } #endif