Attachments forums

Re: 4 Time Frame and All TFs Indicators for MT4

Banzai, Fri Feb 20, 2026 4:45 am

4 Time Frame ZeroLag Stoch

I'm not sure if any coder did this before but here it is.
This indicator is useful for coders because there's a standalone iZeroLagStoch function.

Code: Select all

//-------------------------------------------------------------------
// ZeroLag Stochastic function - self-contained, no iCustom dependency
// mode: 0 = Main line (ZeroLag %K), 1 = Signal line (ZeroLag %D)
//-------------------------------------------------------------------
double iZeroLagStoch(const string symbol, int timeframe, int kperiod, int slowing, int dperiod, int mode, int index)
{
   int n, s;
   double sum, ema;
   
   // Need enough history for the full chain of smoothing
   int depth = kperiod + slowing*2 + dperiod*2 + 5;
   
   // Step 1: Compute raw %K for enough bars
   double rawK[];
   ArrayResize(rawK, depth);
   for (n = 0; n < depth; n++)
   {
      int shift = index + n;
      double hi = iHigh(symbol, timeframe, iHighest(symbol, timeframe, MODE_HIGH, kperiod, shift));
      double lo = iLow (symbol, timeframe, iLowest (symbol, timeframe, MODE_LOW,  kperiod, shift));
      if (hi != lo)
         rawK[n] = 100.0 * (iClose(symbol, timeframe, shift) - lo) / (hi - lo);
      else
         rawK[n] = 0;
   }
   
   // Step 2: SMA of rawK (Slowing) => smoothK
   int smoothLen = depth - slowing + 1;
   double smoothK[];
   ArrayResize(smoothK, smoothLen);
   for (n = 0; n < smoothLen; n++)
   {
      sum = 0;
      for (s = 0; s < slowing; s++)
         sum += rawK[n + s];
      smoothK[n] = sum / slowing;
   }
   
   // Step 3: ZeroLag Main = 2*smoothK - SMA(smoothK, slowing)
   int mainLen = smoothLen - slowing + 1;
   double mainLine[];
   ArrayResize(mainLine, mainLen);
   for (n = 0; n < mainLen; n++)
   {
      sum = 0;
      for (s = 0; s < slowing; s++)
         sum += smoothK[n + s];
      ema = sum / slowing;
      mainLine[n] = smoothK[n] + smoothK[n] - ema;
   }
   
   if (mode == 0)
      return(mainLine[0]);  // Main line at requested index
   
   // Step 4: SMA of mainLine (DPeriod) => smoothD
   int smoothDLen = mainLen - dperiod + 1;
   double smoothD[];
   ArrayResize(smoothD, smoothDLen);
   for (n = 0; n < smoothDLen; n++)
   {
      sum = 0;
      for (s = 0; s < dperiod; s++)
         sum += mainLine[n + s];
      smoothD[n] = sum / dperiod;
   }
   
   // Step 5: ZeroLag Signal = 2*smoothD - SMA(smoothD, dperiod)
   int sigLen = smoothDLen - dperiod + 1;
   if (sigLen < 1) return(0);
   
   sum = 0;
   for (s = 0; s < dperiod; s++)
      sum += smoothD[s];
   ema = sum / dperiod;
   
   return(smoothD[0] + smoothD[0] - ema);  // Signal line at requested index
}
//-------------------------------------------------------------------
All files in topic