Re: MQL4 Programming Language
2iZeroLagStoch function
standalone iZeroLagStoch function.
To see some actions, see: 4TF ZeroLag Stoch (arrows alerts).mq4
post1295580178.html#p1295580178
standalone iZeroLagStoch function.
To see some actions, see: 4TF ZeroLag Stoch (arrows alerts).mq4
post1295580178.html#p1295580178
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
}
//-------------------------------------------------------------------
Re: MQL4 Programming Language
3iRCI function
RCI = Rank Correlation Index aka Spearman Rank Correlation
Example: 4TF RCI slope alerts mtf.mq4
post1295402142.html#p1295402142
RCI is very popular in Japan.
RCI = Rank Correlation Index aka Spearman Rank Correlation
Example: 4TF RCI slope alerts mtf.mq4
post1295402142.html#p1295402142
RCI is very popular in Japan.
Code: Select all
//+-------------------------------------------------------------------
//https://qiita.com/bucchi49/items/a08f240b920fc5f90a87
double iRCI(const string symbol, int timeframe, int period, int index)
{
int rank;
double d = 0;
double close_arr[];
ArrayResize(close_arr, period);
for (int i = 0; i < period; i++) {
close_arr[i] = iClose(symbol, timeframe, index + i);
}
ArraySort(close_arr, WHOLE_ARRAY, 0, MODE_DESCEND);
for (int j = 0; j < period; j++) {
rank = ArrayBsearch(close_arr,
iClose(symbol, timeframe, index + j),
WHOLE_ARRAY,
0,
MODE_DESCEND);
d += MathPow(j - rank, 2);
}
return((1 - 6 * d / (period * (period * period - 1))) * 100);
}
//+-------------------------------------------------------------------
Re: MQL4 Programming Language
4iZeroLagMACD function
Sample: 4TF ZeroLag MACD (arrows alerts).mq4
post1295580194.html#p1295580194
Sample: 4TF ZeroLag MACD (arrows alerts).mq4
post1295580194.html#p1295580194
Code: Select all
//-------------------------------------------------------------------
// ZeroLag MACD function - self-contained, no iCustom dependency
// mode: 0 = MACD line, 1 = Signal line
// Uses double-EMA (EMA of EMA) for zero-lag filtering on fast, slow, and signal
//-------------------------------------------------------------------
double iZeroLagMACD(const string symbol, int timeframe, int fastPeriod, int slowPeriod, int signalPeriod, int priceType, int mode, int index)
{
// EMA needs enough warm-up bars for convergence
int depth = MathMax(slowPeriod, fastPeriod) * 10;
int totalBars = iBars(symbol, timeframe);
if (depth > totalBars - index) depth = totalBars - index;
if (depth < 2) return(0);
double alpha1 = 2.0 / (1.0 + fastPeriod);
double alpha2 = 2.0 / (1.0 + slowPeriod);
double alpha3 = 2.0 / (1.0 + signalPeriod);
// Work arrays: ema11=fast EMA1, ema12=fast EMA2, ema21=slow EMA1, ema22=slow EMA2
double ema11, ema12, ema21, ema22, ema31, ema32;
double macdVal, signalVal;
int n;
// Start from the oldest bar and iterate forward
int startBar = index + depth - 1;
double price = iMA(symbol, timeframe, 1, 0, MODE_SMA, priceType, startBar);
ema11 = price;
ema12 = price;
ema21 = price;
ema22 = price;
macdVal = 0;
ema31 = 0;
ema32 = 0;
for (n = startBar - 1; n >= index; n--)
{
price = iMA(symbol, timeframe, 1, 0, MODE_SMA, priceType, n);
ema11 = ema11 + alpha1 * (price - ema11);
ema12 = ema12 + alpha1 * (ema11 - ema12);
ema21 = ema21 + alpha2 * (price - ema21);
ema22 = ema22 + alpha2 * (ema21 - ema22);
macdVal = (2.0 * ema11 - ema12) - (2.0 * ema21 - ema22);
ema31 = ema31 + alpha3 * (macdVal - ema31);
ema32 = ema32 + alpha3 * (ema31 - ema32);
}
if (mode == 0)
return(macdVal); // MACD line
signalVal = 2.0 * ema31 - ema32;
return(signalVal); // Signal line
}
//-------------------------------------------------------------------