Laguerre RSI in MQL5 — converted from Pine Script

The Laguerre RSI indicator by KivancOzbilgic, published on TradingView under the Mozilla Public License 2.0, converted to MetaTrader 5 by the Stratlate engine. The conversion was executed against the original on 500 synthetic bars and compared value by value: equivalent (fidelity 1.000).

Direction
Pine Script → MQL5
Kind
indicator, separate window
Verdict
EQUIVALENT on 500 bars
Engine
pinec 0.6.46
Conversion notes
1 informational
Source
36 lines of Pine v5

Inputs

InputDefault
Sourceclose
Alpha0.2
Change Color ?false

Plots

LaRSI.

Converted MQL5

Save as laguerre-rsi.mq5 next to PineRuntime.mqh (included with every conversion), compile in MetaEditor, then review and backtest before trading. Output of the engine alone; the AI review available after sign-in may improve it further.

//+------------------------------------------------------------------+
//| pine-transpiler 0.6.46 (pinec) - generated code
//| source sha256: 3ce66c386b4f   status: CLEAN
//| notes: 0 unsupported, 0 warn, 1 info
//| Review, compile and backtest before trading.
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| Laguerre RSI
//| Converted from Pine Script by pine-transpiler.
//| Review before trading: see the accompanying report for every
//| construct that was approximated or could not be converted.
//+------------------------------------------------------------------+
#property copyright "Converted by pine-transpiler"
#property version   "1.00"
#property indicator_separate_window
#property indicator_buffers 4
#property indicator_plots   3
#property indicator_label1  "LaRSI"
#property indicator_type1   DRAW_COLOR_LINE
#property indicator_color1  clrDodgerBlue
#property indicator_width1  2
#property indicator_label2  "Plot2"
#property indicator_type2   DRAW_LINE
#property indicator_color2  clrDodgerBlue
#property indicator_width2  1
#property indicator_label3  "Plot3"
#property indicator_type3   DRAW_LINE
#property indicator_color3  clrDodgerBlue
#property indicator_width3  1

#include "PineRuntime.mqh"

//--- plot buffers
double Buf0[];
double Buf1[];
double Buf2[];
double Buf3[];
color  __pal0[64];   // the colours plot 0 has drawn so far; see pine_color_slot
int    __paln0 = 0;

//--- script-level state
// The newest bar's index: `last_bar_index` and `barstate.islast`.
int    __pineLastBar = 0;
// Series-valued names are arrays because their history is read;
// the rest stay scalars.
double gamma = PINE_NA;
double L0[];
double L1[];
double L2[];
double L3[];
double cu = PINE_NA;
double cd = PINE_NA;
double temp = PINE_NA;
double LaRSI[];
double Color = PINE_NA;

//--- live-tick rollback: the state each bar started from
double __sv_gamma;
double __si_gamma;
double __sv_cu;
double __si_cu;
double __sv_cd;
double __si_cd;
double __sv_temp;
double __si_temp;
double __sv_Color;
double __si_Color;
int __pineSnapBar = -1;
void __pineSaveState()
{
   __sv_gamma = gamma;
   __sv_cu = cu;
   __sv_cd = cd;
   __sv_temp = temp;
   __sv_Color = Color;
   pine_draw_mark();
   __pineSaveGen++;   // helpers' static arrays snapshot lazily
}
void __pineRestoreState()
{
   gamma = __sv_gamma;
   cu = __sv_cu;
   cd = __sv_cd;
   temp = __sv_temp;
   Color = __sv_Color;
   pine_draw_rollback();
   __pineRestoreGen++;
   __pineRestoreSaveGen = __pineSaveGen;
}
void __pineSaveInitial()
{
   __si_gamma = gamma;
   __si_cu = cu;
   __si_cd = cd;
   __si_temp = temp;
   __si_Color = Color;
   pine_draw_save_initial();
}
void __pineRestoreInitial()
{
   gamma = __si_gamma;
   cu = __si_cu;
   cd = __si_cd;
   temp = __si_temp;
   Color = __si_Color;
   pine_draw_reset();
   __pineInitGen++;
}

double __st0[];    // accumulator for one stateful call site
double __stb0[];   // secondary buffer (e.g. true range for ATR)
double __st1[];    // accumulator for one stateful call site
double __stb1[];   // secondary buffer (e.g. true range for ATR)
double __ts0[];   // materialised ta.* source: 20
double __ts1[];   // materialised ta.* source: 80
double __ti0[];   // materialised ta.* source: (100 * LaRSI[__pineBar])

//--- inputs
input ENUM_APPLIED_PRICE src = PRICE_CLOSE;   // Source
input double alpha = 0.2;   // Alpha
input bool colorchange = false;   // Change Color ?

int OnInit()
{
   if(alpha < 0) { Print("Alpha", " must be at least 0"); return INIT_PARAMETERS_INCORRECT; }
   if(alpha > 1) { Print("Alpha", " must be at most 1"); return INIT_PARAMETERS_INCORRECT; }
   gamma = (1 - alpha);
   SetIndexBuffer(0, Buf0, INDICATOR_DATA);
   SetIndexBuffer(1, Buf1, INDICATOR_COLOR_INDEX);
   SetIndexBuffer(2, Buf2, INDICATOR_DATA);
   SetIndexBuffer(3, Buf3, INDICATOR_DATA);
   PlotIndexSetDouble(0, PLOT_EMPTY_VALUE, EMPTY_VALUE);
   PlotIndexSetDouble(1, PLOT_EMPTY_VALUE, EMPTY_VALUE);
   PlotIndexSetDouble(2, PLOT_EMPTY_VALUE, EMPTY_VALUE);
   PlotIndexSetInteger(1, PLOT_LINE_COLOR, C'136,14,79');
   PlotIndexSetInteger(2, PLOT_LINE_COLOR, C'136,14,79');
   IndicatorSetString(INDICATOR_SHORTNAME, "Laguerre RSI");
   __pineSaveInitial();
   return INIT_SUCCEEDED;
}

//--- one bar == one evaluation of the original Pine script
void PineBar(const int __pineBar,
             const double   &open[],  const double &high[],
             const double   &low[],   const double &close[],
             const long     &volume[],
             const datetime &time[])
{
   __ts0[__pineBar] = 20;
   __ts1[__pineBar] = 80;
   if(__pineBar > 0)
   {
      L0[__pineBar] = L0[__pineBar - 1];
      L1[__pineBar] = L1[__pineBar - 1];
      L2[__pineBar] = L2[__pineBar - 1];
      L3[__pineBar] = L3[__pineBar - 1];
      LaRSI[__pineBar] = LaRSI[__pineBar - 1];
   }
   gamma = (1 - alpha);
   L0[__pineBar] = 0.0;
   L0[__pineBar] = (((1 - gamma) * pine_source(src, open, high, low, close, __pineBar)) + (gamma * pine_nz(pine_hist(L0, __pineBar, (int)(1)))));
   L1[__pineBar] = 0.0;
   L1[__pineBar] = ((((-gamma) * L0[__pineBar]) + pine_nz(pine_hist(L0, __pineBar, (int)(1)))) + (gamma * pine_nz(pine_hist(L1, __pineBar, (int)(1)))));
   L2[__pineBar] = 0.0;
   L2[__pineBar] = ((((-gamma) * L1[__pineBar]) + pine_nz(pine_hist(L1, __pineBar, (int)(1)))) + (gamma * pine_nz(pine_hist(L2, __pineBar, (int)(1)))));
   L3[__pineBar] = 0.0;
   L3[__pineBar] = ((((-gamma) * L2[__pineBar]) + pine_nz(pine_hist(L2, __pineBar, (int)(1)))) + (gamma * pine_nz(pine_hist(L3, __pineBar, (int)(1)))));
   cu = ((((L0[__pineBar] > L1[__pineBar]) ? (L0[__pineBar] - L1[__pineBar]) : 0) + ((L1[__pineBar] > L2[__pineBar]) ? (L1[__pineBar] - L2[__pineBar]) : 0)) + ((L2[__pineBar] > L3[__pineBar]) ? (L2[__pineBar] - L3[__pineBar]) : 0));
   cd = ((((L0[__pineBar] < L1[__pineBar]) ? (L1[__pineBar] - L0[__pineBar]) : 0) + ((L1[__pineBar] < L2[__pineBar]) ? (L2[__pineBar] - L1[__pineBar]) : 0)) + ((L2[__pineBar] < L3[__pineBar]) ? (L3[__pineBar] - L2[__pineBar]) : 0));
   temp = (((cu + cd) == 0) ? (-1) : (cu + cd));
   LaRSI[__pineBar] = ((temp == (-1)) ? 0 : pine_div(cu, temp));
   Color = (colorchange ? ((LaRSI[__pineBar] > pine_hist(LaRSI, __pineBar, (int)(1))) ? C'76,175,80' : C'242,54,69') : C'41,98,255');
   __ti0[__pineBar] = (100 * LaRSI[__pineBar]);
   pine_alertcond(pine_crossover(__ti0, __ts0, __pineBar), "Laguerre RSI crosses OVER 20 - BUY SIGNAL!", 157130384, __pineBar, __pineLastBar);
   __ti0[__pineBar] = (100 * LaRSI[__pineBar]);
   pine_alertcond(pine_crossunder(__ti0, __ts1, __pineBar), "Laguerre RSI crosses UNDER 80 - SELL SIGNAL!", 1848010887, __pineBar, __pineLastBar);
   double __pinePlotValue0 = (100 * LaRSI[__pineBar]);
   double __pinePlotColour0 = (double)(Color);
   Buf0[__pineBar] = __pinePlotValue0;
   Buf1[__pineBar] = pine_color_slot(0, pine_na(__pinePlotColour0) ? clrNONE : (color)__pinePlotColour0, __pal0, __paln0);
   Buf2[__pineBar] = 20;
   Buf3[__pineBar] = 80;
}

int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
{
   // Pine evaluates oldest-to-newest. MQL4 hands OnCalculate
   // SERIES-indexed arrays (0 == newest) while MQL5 hands over
   // oldest-first, so the direction is forced explicitly rather
   // than relying on either default. `x[n]` is then index i - n
   // in both dialects, and the generated logic is identical.
   ArraySetAsSeries(open,  false);
   ArraySetAsSeries(high,  false);
   ArraySetAsSeries(low,   false);
   ArraySetAsSeries(close, false);
   ArraySetAsSeries(tick_volume, false);
   ArraySetAsSeries(volume, false);
   ArraySetAsSeries(time,  false);
   ArrayResize(L0, rates_total);
   ArrayResize(L1, rates_total);
   ArrayResize(L2, rates_total);
   ArrayResize(L3, rates_total);
   ArrayResize(LaRSI, rates_total);
   ArrayResize(__st0, rates_total);
   ArrayResize(__stb0, rates_total);
   ArrayResize(__st1, rates_total);
   ArrayResize(__stb1, rates_total);
   ArrayResize(__ts0, rates_total);
   ArrayResize(__ts1, rates_total);
   ArrayResize(__ti0, rates_total);
   if(prev_calculated == 0)
   {
      ArrayInitialize(L0, PINE_NA);
      ArrayInitialize(L1, PINE_NA);
      ArrayInitialize(L2, PINE_NA);
      ArrayInitialize(L3, PINE_NA);
      ArrayInitialize(LaRSI, PINE_NA);
      ArrayInitialize(__st0, PINE_NA);
      ArrayInitialize(__stb0, PINE_NA);
      ArrayInitialize(__st1, PINE_NA);
      ArrayInitialize(__stb1, PINE_NA);
      ArrayInitialize(__ts0, PINE_NA);
      ArrayInitialize(__ts1, PINE_NA);
      ArrayInitialize(__ti0, PINE_NA);
   }

   __pineLastBar = rates_total - 1;
   const int start = (prev_calculated > 1) ? prev_calculated - 1 : 0;
   static bool __pineRealVolume = false;
   if(start == 0)
   {
      __pineRestoreInitial();
      __pineSnapBar = -1;
      __pineRealVolume = rates_total > 0 && volume[rates_total - 1] > 0;
   }
   __pineRealtime = prev_calculated > 0;
   for(int __pineBar = start; __pineBar < rates_total; __pineBar++)
   {
      if(__pineBar == __pineSnapBar)
         __pineRestoreState();   // a re-run of this bar: back to its start
      else if(__pineBar == __pineLastBar)
      {
         __pineSaveState();      // the forming bar: remember the state it starts from
         __pineSnapBar = __pineBar;
      }
      if(__pineRealVolume) PineBar(__pineBar, open, high, low, close, volume, time);
      else PineBar(__pineBar, open, high, low, close, tick_volume, time);
   }

   return rates_total;
}

Original Pine Script

© KivancOzbilgic — Mozilla Public License 2.0. Reproduced for attribution and comparison; the converted code above is a derivative work offered under the same licence.

//@version=5
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// Developer: John EHLERS
// © KivancOzbilgic
// Author:Kıvanç Özbilgiç
indicator('Laguerre RSI', shorttitle='LaRSI', overlay=false, timeframe = "")
src = input(title='Source', defval=close)
alpha = input.float(title='Alpha', minval=0, maxval=1, step=0.1, defval=0.2)
colorchange = input(title='Change Color ?', defval=false)
gamma = 1 - alpha
L0 = 0.0
L0 := (1 - gamma) * src + gamma * nz(L0[1])
L1 = 0.0
L1 := -gamma * L0 + nz(L0[1]) + gamma * nz(L1[1])

L2 = 0.0
L2 := -gamma * L1 + nz(L1[1]) + gamma * nz(L2[1])

L3 = 0.0
L3 := -gamma * L2 + nz(L2[1]) + gamma * nz(L3[1])

cu = (L0 > L1 ? L0 - L1 : 0) + (L1 > L2 ? L1 - L2 : 0) + (L2 > L3 ? L2 - L3 : 0)

cd = (L0 < L1 ? L1 - L0 : 0) + (L1 < L2 ? L2 - L1 : 0) + (L2 < L3 ? L3 - L2 : 0)

temp = cu + cd == 0 ? -1 : cu + cd
LaRSI = temp == -1 ? 0 : cu / temp

Color = colorchange ? LaRSI > LaRSI[1] ? color.green : color.red : color.blue
plot(100 * LaRSI, title='LaRSI', linewidth=2, color=Color, transp=0)
plot(20, linewidth=1, color=color.new(color.maroon, 0))
plot(80, linewidth=1, color=color.new(color.maroon, 0))
alertcondition(ta.crossover(100 * LaRSI, 20), title='LaRSI Crossover Alarm', message='Laguerre RSI crosses OVER 20 - BUY SIGNAL!')
alertcondition(ta.crossunder(100 * LaRSI, 80), title='Price Crossunder Alarm', message='Laguerre RSI crosses UNDER 80 - SELL SIGNAL!')

Convert your own Pine Script to MQL5

Your first conversion is free, in either direction. Paste a TradingView indicator or strategy and get MQL5 or MQL4 source, with every construct the engine could not map flagged by line.

Open the converter

More: all example conversions · how Pine Script maps to MQL5 · how the engine measures