ZLSMA - Zero Lag LSMA in MQL5 — converted from Pine Script
The ZLSMA - Zero Lag LSMA indicator by veryfid, 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).
Inputs
| Input | Default |
|---|---|
| Length | 32 |
| Offset | 0 |
| Source | close |
Converted MQL5
Save as zlsma-zero-lag-lsma.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: 7c0f7660585a status: CLEAN
//| notes: 0 unsupported, 0 warn, 2 info
//| Review, compile and backtest before trading.
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| ZLSMA - Zero Lag LSMA
//| 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_chart_window
#property indicator_buffers 1
#property indicator_plots 1
#property indicator_label1 "Plot1"
#property indicator_type1 DRAW_LINE
#property indicator_color1 clrDodgerBlue
#property indicator_width1 3
#include "PineRuntime.mqh"
//--- plot buffers
double Buf0[];
//--- 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 lsma = PINE_NA;
double lsma2 = PINE_NA;
double eq = PINE_NA;
double zlsma = PINE_NA;
//--- live-tick rollback: the state each bar started from
double __sv_lsma;
double __si_lsma;
double __sv_lsma2;
double __si_lsma2;
double __sv_eq;
double __si_eq;
double __sv_zlsma;
double __si_zlsma;
int __pineSnapBar = -1;
void __pineSaveState()
{
__sv_lsma = lsma;
__sv_lsma2 = lsma2;
__sv_eq = eq;
__sv_zlsma = zlsma;
pine_draw_mark();
__pineSaveGen++; // helpers' static arrays snapshot lazily
}
void __pineRestoreState()
{
lsma = __sv_lsma;
lsma2 = __sv_lsma2;
eq = __sv_eq;
zlsma = __sv_zlsma;
pine_draw_rollback();
__pineRestoreGen++;
__pineRestoreSaveGen = __pineSaveGen;
}
void __pineSaveInitial()
{
__si_lsma = lsma;
__si_lsma2 = lsma2;
__si_eq = eq;
__si_zlsma = zlsma;
pine_draw_save_initial();
}
void __pineRestoreInitial()
{
lsma = __si_lsma;
lsma2 = __si_lsma2;
eq = __si_eq;
zlsma = __si_zlsma;
pine_draw_reset();
__pineInitGen++;
}
double __src[]; // Pine's `src` as a real series, filled per bar
double __ti0[]; // materialised ta.* source: lsma
//--- inputs
input int length = 32; // Length
input int offset = 0; // Offset
input ENUM_APPLIED_PRICE src = PRICE_CLOSE; // Source
int OnInit()
{
SetIndexBuffer(0, Buf0, INDICATOR_DATA);
PlotIndexSetDouble(0, PLOT_EMPTY_VALUE, EMPTY_VALUE);
PlotIndexSetInteger(0, PLOT_LINE_COLOR, C'253,216,53');
IndicatorSetString(INDICATOR_SHORTNAME, "ZLSMA - Zero Lag LSMA");
__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[])
{
__src[__pineBar] = pine_source(src, open, high, low, close, __pineBar);
lsma = pine_linreg(__src, __pineBar, (int)(length), (int)(offset));
__ti0[__pineBar] = lsma;
lsma2 = pine_linreg(__ti0, __pineBar, (int)(length), (int)(offset));
eq = (lsma - lsma2);
zlsma = (lsma + eq);
Buf0[__pineBar] = zlsma;
}
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(__src, rates_total);
ArrayResize(__ti0, rates_total);
if(prev_calculated == 0)
{
ArrayInitialize(__src, 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
© veryfid — Mozilla Public License 2.0. Reproduced for attribution and comparison; the converted code above is a derivative work offered under the same licence.
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © veryfid
//@version=4
study(title = "ZLSMA - Zero Lag LSMA", shorttitle="ZLSMA", overlay=true, resolution="")
length = input(title="Length", type=input.integer, defval=32)
offset = input(title="Offset", type=input.integer, defval=0)
src = input(close, title="Source")
lsma = linreg(src, length, offset)
lsma2 = linreg(lsma, length, offset)
eq= lsma-lsma2
zlsma = lsma+eq
plot(zlsma, color=color.yellow, linewidth=3)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 converterMore: all example conversions · how Pine Script maps to MQL5 · how the engine measures