I am trying to convert an MQL4 indicator into Pine Script but it yields a totally different output; the indicator values do not match. The MQL4 indicator line is smoother compared to PineScript
The MQL4 indicator code is
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 Crimson
#property indicator_width1 2
extern int period = 20;
double Signal_Buffer[];
int init() {
IndicatorDigits( Digits + 4 );
IndicatorShortName("Indi (" + string(period) + ")");
SetIndexBuffer(0, Signal_Buffer);
SetIndexStyle(0, DRAW_LINE);
SetIndexLabel(0, "Indi (" + string(period) + ")");
return (0);
}
int start() {
int bars_loaded = IndicatorCounted();
if (bars_loaded > 0) bars_loaded--;
int total_bars = Bars - bars_loaded;
double current_high = 0;
double current_low = 0;
double high_low_median = 0;
double signal = 0;
double a = 0;
double b = 0;
for (int i = 0; i < total_bars; i++) {
current_high = High[iHighest(Symbol(), Period(), MODE_HIGH, period, i)];
current_low = Low[iLowest(Symbol(), Period(), MODE_LOW, period, i)];
high_low_median = (High[i] + Low[i]) / 2.0;
a = 0.66 * ( (high_low_median - current_low) / (current_high - current_low) - 0.5 ) + 0.67 * b;
a = MathMin( MathMax( a, -0.999 ), 0.999 );
Signal_Buffer[i] = MathLog( ( a + 1.0 ) / ( 1 - a ) ) / 2.0 + signal / 2.0;
b = a;
signal = Signal_Buffer[i];
}
return (0);
}
and the Pine Script code is
//@version=5
indicator(title='Indi', shorttitle='Indi')
per = input.int( defval=20, title='Period', minval=1 )
a = 0.0
b = 0.0
signal = 0.0
h = ta.highest( high, per )
l = ta.lowest( low, per )
m = hl2
a := 0.66 * ( (m - l) / (h - l) - 0.5 ) + 0.67 * b
a := math.min( math.max( a, -0.999 ), 0.999 )
signal := math.log( ( a + 1.0 ) / ( 1 - a ) ) / 2.0 + signal / 2.0
b := a
plot( signal )
I think maybe you need to replace b
with nz(a[1]
in this line :
a = 0.66 * ( (high_low_median - current_low) / (current_high - current_low) - 0.5 ) + 0.67 * b;
to
a = 0.66 * ( (high_low_median - current_low) / (current_high - current_low) - 0.5 ) + 0.67 * nz(a[1]);