Optimized Trend Tracker in MQL5 — converted from Pine Script

The Optimized Trend Tracker 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, drawn on the price chart
Verdict
EQUIVALENT on 500 bars
Engine
pinec 0.6.46
Conversion notes
11 informational
Source
251 lines of Pine v4

Inputs

InputDefault
Sourceclose
OTT Period2
OTT Percent1.4
Show Support Line?true
Show Support Line Crossing Signals?true
Show Price/OTT Crossing Signals?false
Show OTT Color Changes?false
Show OTT Color Change Signals?false
Highlighter On/Off ?true
Moving Average Type"VAR"

Plots

Support Line.

Converted MQL5

Save as optimized-trend-tracker.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: d24067a6f265   status: CLEAN
//| notes: 0 unsupported, 0 warn, 11 info
//| Review, compile and backtest before trading.
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| Optimized Trend Tracker
//| 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 14
#property indicator_plots   11
#property indicator_label1  "UpTrend Highligter"
#property indicator_type1   DRAW_FILLING
#property indicator_color1  clrDodgerBlue
#property indicator_width1  1
#property indicator_label2  "DownTrend Highligter"
#property indicator_type2   DRAW_FILLING
#property indicator_color2  clrDodgerBlue
#property indicator_width2  1
#property indicator_label3  "Support Line"
#property indicator_type3   DRAW_LINE
#property indicator_color3  clrDodgerBlue
#property indicator_width3  2
#property indicator_label4  "OTT"
#property indicator_type4   DRAW_COLOR_LINE
#property indicator_color4  clrDodgerBlue
#property indicator_width4  2
#property indicator_label5  "Buy"
#property indicator_type5   DRAW_ARROW
#property indicator_color5  clrDodgerBlue
#property indicator_width5  1
#property indicator_label6  "Sell"
#property indicator_type6   DRAW_ARROW
#property indicator_color6  clrDodgerBlue
#property indicator_width6  1
#property indicator_label7  "Buy"
#property indicator_type7   DRAW_ARROW
#property indicator_color7  clrDodgerBlue
#property indicator_width7  1
#property indicator_label8  "Sell"
#property indicator_type8   DRAW_ARROW
#property indicator_color8  clrDodgerBlue
#property indicator_width8  1
#property indicator_label9  "Plot9"
#property indicator_type9   DRAW_NONE
#property indicator_color9  clrDodgerBlue
#property indicator_width9  1
#property indicator_label10  "Buy"
#property indicator_type10   DRAW_ARROW
#property indicator_color10  clrDodgerBlue
#property indicator_width10  1
#property indicator_label11  "Sell"
#property indicator_type11   DRAW_ARROW
#property indicator_color11  clrDodgerBlue
#property indicator_width11  1

#include "PineRuntime.mqh"

//--- plot buffers
double PineFill0[];
double PineFill1[];
double PineFill2[];
double PineFill3[];
double Buf4[];
double Buf5[];
double Buf6[];
double Buf7[];
double Buf8[];
double Buf9[];
double Buf10[];
double Buf11[];
double Buf12[];
double Buf13[];
color  __pal3[64];   // the colours plot 3 has drawn so far; see pine_color_slot
int    __paln3 = 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 VAR[];
double WWMA[];
double ZLEMA = PINE_NA;
double TSF = PINE_NA;
double MAvg = PINE_NA;
double fark = PINE_NA;
double longStop[];
double longStopPrev = PINE_NA;
double shortStop[];
double shortStopPrev = PINE_NA;
double dir[];
double MT = PINE_NA;
double OTT[];
double OTTC = PINE_NA;
bool buySignalk = false;
bool sellSignallk = false;
bool buySignalc = false;
bool sellSignallc = false;
double longFillColor = PINE_NA;
double shortFillColor = PINE_NA;
bool buySignalr = false;
bool sellSignallr = false;

//--- live-tick rollback: the state each bar started from
double __sv_ZLEMA;
double __si_ZLEMA;
double __sv_TSF;
double __si_TSF;
double __sv_MAvg;
double __si_MAvg;
double __sv_fark;
double __si_fark;
double __sv_longStopPrev;
double __si_longStopPrev;
double __sv_shortStopPrev;
double __si_shortStopPrev;
double __sv_MT;
double __si_MT;
double __sv_OTTC;
double __si_OTTC;
bool __sv_buySignalk;
bool __si_buySignalk;
bool __sv_sellSignallk;
bool __si_sellSignallk;
bool __sv_buySignalc;
bool __si_buySignalc;
bool __sv_sellSignallc;
bool __si_sellSignallc;
double __sv_longFillColor;
double __si_longFillColor;
double __sv_shortFillColor;
double __si_shortFillColor;
bool __sv_buySignalr;
bool __si_buySignalr;
bool __sv_sellSignallr;
bool __si_sellSignallr;
int __pineSnapBar = -1;
void __pineSaveState()
{
   __sv_ZLEMA = ZLEMA;
   __sv_TSF = TSF;
   __sv_MAvg = MAvg;
   __sv_fark = fark;
   __sv_longStopPrev = longStopPrev;
   __sv_shortStopPrev = shortStopPrev;
   __sv_MT = MT;
   __sv_OTTC = OTTC;
   __sv_buySignalk = buySignalk;
   __sv_sellSignallk = sellSignallk;
   __sv_buySignalc = buySignalc;
   __sv_sellSignallc = sellSignallc;
   __sv_longFillColor = longFillColor;
   __sv_shortFillColor = shortFillColor;
   __sv_buySignalr = buySignalr;
   __sv_sellSignallr = sellSignallr;
   pine_draw_mark();
   __pineSaveGen++;   // helpers' static arrays snapshot lazily
}
void __pineRestoreState()
{
   ZLEMA = __sv_ZLEMA;
   TSF = __sv_TSF;
   MAvg = __sv_MAvg;
   fark = __sv_fark;
   longStopPrev = __sv_longStopPrev;
   shortStopPrev = __sv_shortStopPrev;
   MT = __sv_MT;
   OTTC = __sv_OTTC;
   buySignalk = __sv_buySignalk;
   sellSignallk = __sv_sellSignallk;
   buySignalc = __sv_buySignalc;
   sellSignallc = __sv_sellSignallc;
   longFillColor = __sv_longFillColor;
   shortFillColor = __sv_shortFillColor;
   buySignalr = __sv_buySignalr;
   sellSignallr = __sv_sellSignallr;
   pine_draw_rollback();
   __pineRestoreGen++;
   __pineRestoreSaveGen = __pineSaveGen;
}
void __pineSaveInitial()
{
   __si_ZLEMA = ZLEMA;
   __si_TSF = TSF;
   __si_MAvg = MAvg;
   __si_fark = fark;
   __si_longStopPrev = longStopPrev;
   __si_shortStopPrev = shortStopPrev;
   __si_MT = MT;
   __si_OTTC = OTTC;
   __si_buySignalk = buySignalk;
   __si_sellSignallk = sellSignallk;
   __si_buySignalc = buySignalc;
   __si_sellSignallc = sellSignallc;
   __si_longFillColor = longFillColor;
   __si_shortFillColor = shortFillColor;
   __si_buySignalr = buySignalr;
   __si_sellSignallr = sellSignallr;
   pine_draw_save_initial();
}
void __pineRestoreInitial()
{
   ZLEMA = __si_ZLEMA;
   TSF = __si_TSF;
   MAvg = __si_MAvg;
   fark = __si_fark;
   longStopPrev = __si_longStopPrev;
   shortStopPrev = __si_shortStopPrev;
   MT = __si_MT;
   OTTC = __si_OTTC;
   buySignalk = __si_buySignalk;
   sellSignallk = __si_sellSignallk;
   buySignalc = __si_buySignalc;
   sellSignallc = __si_sellSignallc;
   longFillColor = __si_longFillColor;
   shortFillColor = __si_shortFillColor;
   buySignalr = __si_buySignalr;
   sellSignallr = __si_sellSignallr;
   pine_draw_reset();
   __pineInitGen++;
}

string mav = "";   // input the script modifies; reset each bar
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 __st2[];    // accumulator for one stateful call site
double __stb2[];   // secondary buffer (e.g. true range for ATR)
double __st3[];    // accumulator for one stateful call site
double __stb3[];   // secondary buffer (e.g. true range for ATR)
double __st4[];    // accumulator for one stateful call site
double __stb4[];   // secondary buffer (e.g. true range for ATR)
double __st5[];    // accumulator for one stateful call site
double __stb5[];   // secondary buffer (e.g. true range for ATR)
double __st6[];    // accumulator for one stateful call site
double __stb6[];   // secondary buffer (e.g. true range for ATR)
double __st7[];    // accumulator for one stateful call site
double __stb7[];   // secondary buffer (e.g. true range for ATR)
double __st8[];    // accumulator for one stateful call site
double __stb8[];   // secondary buffer (e.g. true range for ATR)
double __st9[];    // accumulator for one stateful call site
double __stb9[];   // secondary buffer (e.g. true range for ATR)
double __st10[];    // accumulator for one stateful call site
double __stb10[];   // secondary buffer (e.g. true range for ATR)
double __st11[];    // accumulator for one stateful call site
double __stb11[];   // secondary buffer (e.g. true range for ATR)
double __st12[];    // accumulator for one stateful call site
double __stb12[];   // secondary buffer (e.g. true range for ATR)
double __st13[];    // accumulator for one stateful call site
double __stb13[];   // secondary buffer (e.g. true range for ATR)
double __st14[];    // accumulator for one stateful call site
double __stb14[];   // secondary buffer (e.g. true range for ATR)
double __st15[];    // accumulator for one stateful call site
double __stb15[];   // secondary buffer (e.g. true range for ATR)
double __st16[];    // accumulator for one stateful call site
double __stb16[];   // secondary buffer (e.g. true range for ATR)
double __src[];   // Pine's `src` as a real series, filled per bar
double __fl_Var_Func_VAR_l[];   // history of `VAR_l`, a local of Var_Func() the script reads with [n]
double __fl_Wwma_Func_WWMA_l[];   // history of `WWMA_l`, a local of Wwma_Func() the script reads with [n]
double __ti0[];   // materialised ta.* source: pine_hist(OTT, __pineBar, (int)(2))
double __ti1[];   // materialised ta.* source: pine_hist(OTT, __pineBar, (int)(3))
double __ti2[];   // materialised ta.* source: MAvg
double __sw0[];   // materialised ta.* source: Var_Func :: vud1
double __sw1[];   // materialised ta.* source: Var_Func :: vdd1
double __sw2[];   // materialised ta.* source: getMA :: pine_sma(__src, __pineBar, (int)(MathCeil(pine_div(length, 2))))

//--- inputs
input ENUM_APPLIED_PRICE src = PRICE_CLOSE;   // Source
input int length = 2;   // OTT Period
input double percent = 1.4;   // OTT Percent
input bool showsupport = true;   // Show Support Line?
input bool showsignalsk = true;   // Show Support Line Crossing Signals?
input bool showsignalsc = false;   // Show Price/OTT Crossing Signals?
input bool highlight = false;   // Show OTT Color Changes?
input bool showsignalsr = false;   // Show OTT Color Change Signals?
input bool highlighting = true;   // Highlighter On/Off ?
enum ENUM_MAV_OPTS
{
   mav_opt0,   // SMA
   mav_opt1,   // EMA
   mav_opt2,   // WMA
   mav_opt3,   // TMA
   mav_opt4,   // VAR
   mav_opt5,   // WWMA
   mav_opt6,   // ZLEMA
   mav_opt7   // TSF
};
string mav_Labels[] = {"SMA", "EMA", "WMA", "TMA", "VAR", "WWMA", "ZLEMA", "TSF"};
input ENUM_MAV_OPTS mav_Input = mav_opt4;   // Moving Average Type

//--- user functions
double Var_Func(const double &src[], const int length, const int __pineBar)
{
   double valpha = pine_div(2, (length + 1));
   double vud1 = ((src[__pineBar] > pine_hist(src, __pineBar, (int)(1))) ? (src[__pineBar] - pine_hist(src, __pineBar, (int)(1))) : 0);
   double vdd1 = ((src[__pineBar] < pine_hist(src, __pineBar, (int)(1))) ? (pine_hist(src, __pineBar, (int)(1)) - src[__pineBar]) : 0);
   double vUD = pine_sum_step(vud1, __sw0, __pineBar, (int)(9));
   double vDD = pine_sum_step(vdd1, __sw1, __pineBar, (int)(9));
   double vCMO = pine_nz(pine_div((vUD - vDD), (vUD + vDD)));
   __fl_Var_Func_VAR_l[__pineBar] = 0.0;
   __fl_Var_Func_VAR_l[__pineBar] = (pine_nz(((valpha * MathAbs(vCMO)) * src[__pineBar])) + ((1 - (valpha * MathAbs(vCMO))) * pine_nz(pine_hist(__fl_Var_Func_VAR_l, __pineBar, (int)(1)))));
   return __fl_Var_Func_VAR_l[__pineBar];
   return PINE_NA;
}

double Wwma_Func(const double src, const int length, const int __pineBar)
{
   double wwalpha = pine_div(1, length);
   __fl_Wwma_Func_WWMA_l[__pineBar] = 0.0;
   __fl_Wwma_Func_WWMA_l[__pineBar] = ((wwalpha * src) + ((1 - wwalpha) * pine_nz(pine_hist(__fl_Wwma_Func_WWMA_l, __pineBar, (int)(1)))));
   return __fl_Wwma_Func_WWMA_l[__pineBar];
   return PINE_NA;
}

double Zlema_Func(const double &src[], const int length, const int __pineBar)
{
   double zxLag = ((pine_div(length, 2) == MathRound(pine_div(length, 2))) ? pine_div(length, 2) : pine_div((length - 1), 2));
   double zxEMAData = (src[__pineBar] + (src[__pineBar] - pine_hist(src, __pineBar, (int)(zxLag))));
   double ZLEMA_l = pine_ema_step(zxEMAData, __st0, __stb0, __pineBar, (int)(length));
   return ZLEMA_l;
   return PINE_NA;
}

double Tsf_Func(const double src, const int length, const int __pineBar)
{
   double lrc = pine_linreg(__src, __pineBar, (int)(length), (int)(0));
   double lrc1 = pine_linreg(__src, __pineBar, (int)(length), (int)(1));
   double lrs = (lrc - lrc1);
   double TSF_l = (pine_linreg(__src, __pineBar, (int)(length), (int)(0)) + lrs);
   return TSF_l;
   return PINE_NA;
}

double getMA(const double src, const int length, const int __pineBar)
{
   double ma = 0.0;
   if((mav == "SMA"))
   {
      ma = pine_sma(__src, __pineBar, (int)(length));
      ma;
   }
   if((mav == "EMA"))
   {
      ma = pine_ema_step(src, __st1, __stb1, __pineBar, (int)(length));
      ma;
   }
   if((mav == "WMA"))
   {
      ma = pine_wma(__src, __pineBar, (int)(length));
      ma;
   }
   if((mav == "TMA"))
   {
      ma = pine_sma_step(pine_sma(__src, __pineBar, (int)(MathCeil(pine_div(length, 2)))), __sw2, __pineBar, (int)((MathFloor(pine_div(length, 2)) + 1)));
      ma;
   }
   if((mav == "VAR"))
   {
      ma = VAR[__pineBar];
      ma;
   }
   if((mav == "WWMA"))
   {
      ma = WWMA[__pineBar];
      ma;
   }
   if((mav == "ZLEMA"))
   {
      ma = ZLEMA;
      ma;
   }
   if((mav == "TSF"))
   {
      ma = TSF;
      ma;
   }
   return ma;
   return PINE_NA;
}

int OnInit()
{
   mav = mav_Labels[(int)mav_Input];
   if(length < 1) { Print("OTT Period", " must be at least 1"); return INIT_PARAMETERS_INCORRECT; }
   if(percent < 0) { Print("OTT Percent", " must be at least 0"); return INIT_PARAMETERS_INCORRECT; }
   SetIndexBuffer(0, PineFill0, INDICATOR_DATA);
   SetIndexBuffer(1, PineFill1, INDICATOR_DATA);
   SetIndexBuffer(2, PineFill2, INDICATOR_DATA);
   SetIndexBuffer(3, PineFill3, INDICATOR_DATA);
   SetIndexBuffer(4, Buf4, INDICATOR_DATA);
   SetIndexBuffer(5, Buf5, INDICATOR_DATA);
   SetIndexBuffer(6, Buf6, INDICATOR_COLOR_INDEX);
   SetIndexBuffer(7, Buf7, INDICATOR_DATA);
   SetIndexBuffer(8, Buf8, INDICATOR_DATA);
   SetIndexBuffer(9, Buf9, INDICATOR_DATA);
   SetIndexBuffer(10, Buf10, INDICATOR_DATA);
   SetIndexBuffer(11, Buf11, INDICATOR_DATA);
   SetIndexBuffer(12, Buf12, INDICATOR_DATA);
   SetIndexBuffer(13, Buf13, INDICATOR_DATA);
   PlotIndexSetDouble(0, PLOT_EMPTY_VALUE, EMPTY_VALUE);
   PlotIndexSetDouble(1, PLOT_EMPTY_VALUE, EMPTY_VALUE);
   PlotIndexSetDouble(2, PLOT_EMPTY_VALUE, EMPTY_VALUE);
   PlotIndexSetDouble(3, PLOT_EMPTY_VALUE, EMPTY_VALUE);
   PlotIndexSetDouble(4, PLOT_EMPTY_VALUE, EMPTY_VALUE);
   PlotIndexSetDouble(5, PLOT_EMPTY_VALUE, EMPTY_VALUE);
   PlotIndexSetDouble(6, PLOT_EMPTY_VALUE, EMPTY_VALUE);
   PlotIndexSetDouble(7, PLOT_EMPTY_VALUE, EMPTY_VALUE);
   PlotIndexSetDouble(8, PLOT_EMPTY_VALUE, EMPTY_VALUE);
   PlotIndexSetDouble(9, PLOT_EMPTY_VALUE, EMPTY_VALUE);
   PlotIndexSetDouble(10, PLOT_EMPTY_VALUE, EMPTY_VALUE);
   PlotIndexSetInteger(0, PLOT_SHOW_DATA, false);
   PlotIndexSetInteger(1, PLOT_SHOW_DATA, false);
   PlotIndexSetInteger(2, PLOT_LINE_COLOR, C'5,133,225');
   PlotIndexSetInteger(4, PLOT_LINE_COLOR, C'76,175,80');
   PlotIndexSetInteger(5, PLOT_LINE_COLOR, C'242,54,69');
   PlotIndexSetInteger(6, PLOT_LINE_COLOR, C'76,175,80');
   PlotIndexSetInteger(7, PLOT_LINE_COLOR, C'242,54,69');
   PlotIndexSetInteger(9, PLOT_LINE_COLOR, C'76,175,80');
   PlotIndexSetInteger(10, PLOT_LINE_COLOR, C'242,54,69');
   PlotIndexSetInteger(4, PLOT_DRAW_TYPE, DRAW_ARROW);
   PlotIndexSetInteger(4, PLOT_ARROW, 241);
   PlotIndexSetInteger(4, PLOT_ARROW_SHIFT, -16);
   PlotIndexSetInteger(5, PLOT_DRAW_TYPE, DRAW_ARROW);
   PlotIndexSetInteger(5, PLOT_ARROW, 242);
   PlotIndexSetInteger(5, PLOT_ARROW_SHIFT, -16);
   PlotIndexSetInteger(6, PLOT_DRAW_TYPE, DRAW_ARROW);
   PlotIndexSetInteger(6, PLOT_ARROW, 241);
   PlotIndexSetInteger(6, PLOT_ARROW_SHIFT, -16);
   PlotIndexSetInteger(7, PLOT_DRAW_TYPE, DRAW_ARROW);
   PlotIndexSetInteger(7, PLOT_ARROW, 242);
   PlotIndexSetInteger(7, PLOT_ARROW_SHIFT, -16);
   PlotIndexSetInteger(8, PLOT_DRAW_TYPE, DRAW_ARROW);
   PlotIndexSetInteger(8, PLOT_ARROW, 159);
   PlotIndexSetInteger(9, PLOT_DRAW_TYPE, DRAW_ARROW);
   PlotIndexSetInteger(9, PLOT_ARROW, 241);
   PlotIndexSetInteger(9, PLOT_ARROW_SHIFT, -16);
   PlotIndexSetInteger(10, PLOT_DRAW_TYPE, DRAW_ARROW);
   PlotIndexSetInteger(10, PLOT_ARROW, 242);
   PlotIndexSetInteger(10, PLOT_ARROW_SHIFT, -16);
   PlotIndexSetInteger(8, PLOT_SHOW_DATA, false);
   PlotIndexSetInteger(8, PLOT_DRAW_TYPE, DRAW_NONE);
   IndicatorSetString(INDICATOR_SHORTNAME, "Optimized Trend Tracker");
   __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);
   if(__pineBar > 0)
   {
      VAR[__pineBar] = VAR[__pineBar - 1];
      WWMA[__pineBar] = WWMA[__pineBar - 1];
      longStop[__pineBar] = longStop[__pineBar - 1];
      shortStop[__pineBar] = shortStop[__pineBar - 1];
      dir[__pineBar] = dir[__pineBar - 1];
      OTT[__pineBar] = OTT[__pineBar - 1];
   }
   mav = mav_Labels[(int)mav_Input];
   VAR[__pineBar] = Var_Func(__src, length, __pineBar);
   WWMA[__pineBar] = Wwma_Func(pine_source(src, open, high, low, close, __pineBar), length, __pineBar);
   ZLEMA = Zlema_Func(__src, length, __pineBar);
   TSF = Tsf_Func(pine_source(src, open, high, low, close, __pineBar), length, __pineBar);
   MAvg = getMA(pine_source(src, open, high, low, close, __pineBar), length, __pineBar);
   fark = ((MAvg * percent) * 0.01);
   longStop[__pineBar] = (MAvg - fark);
   longStopPrev = pine_nz(pine_hist(longStop, __pineBar, (int)(1)), longStop[__pineBar]);
   longStop[__pineBar] = ((MAvg > longStopPrev) ? MathMax(longStop[__pineBar], longStopPrev) : longStop[__pineBar]);
   shortStop[__pineBar] = (MAvg + fark);
   shortStopPrev = pine_nz(pine_hist(shortStop, __pineBar, (int)(1)), shortStop[__pineBar]);
   shortStop[__pineBar] = ((MAvg < shortStopPrev) ? MathMin(shortStop[__pineBar], shortStopPrev) : shortStop[__pineBar]);
   dir[__pineBar] = 1;
   dir[__pineBar] = pine_nz(pine_hist(dir, __pineBar, (int)(1)), dir[__pineBar]);
   dir[__pineBar] = (((((dir[__pineBar] == (-1)) ? 1 : 0) & ((MAvg > shortStopPrev) ? 1 : 0)) != 0) ? 1 : (((((dir[__pineBar] == 1) ? 1 : 0) & ((MAvg < longStopPrev) ? 1 : 0)) != 0) ? (-1) : dir[__pineBar]));
   MT = ((dir[__pineBar] == 1) ? longStop[__pineBar] : shortStop[__pineBar]);
   OTT[__pineBar] = ((MAvg > MT) ? pine_div((MT * (200 + percent)), 200) : pine_div((MT * (200 - percent)), 200));
   OTTC = (highlight ? ((pine_hist(OTT, __pineBar, (int)(2)) > pine_hist(OTT, __pineBar, (int)(3))) ? C'76,175,80' : C'242,54,69') : C'184,0,217');
   __ti0[__pineBar] = pine_hist(OTT, __pineBar, (int)(2));
   __ti1[__pineBar] = pine_hist(OTT, __pineBar, (int)(3));
   pine_alertcond(pine_cross(__ti0, __ti1, __pineBar), "OTT Has Changed Color!", 1221822621, __pineBar, __pineLastBar);
   __ti0[__pineBar] = pine_hist(OTT, __pineBar, (int)(2));
   __ti1[__pineBar] = pine_hist(OTT, __pineBar, (int)(3));
   pine_alertcond(pine_crossover(__ti0, __ti1, __pineBar), "OTT GREEN BUY SIGNAL!", 217881897, __pineBar, __pineLastBar);
   __ti0[__pineBar] = pine_hist(OTT, __pineBar, (int)(2));
   __ti1[__pineBar] = pine_hist(OTT, __pineBar, (int)(3));
   pine_alertcond(pine_crossunder(__ti0, __ti1, __pineBar), "OTT RED SELL SIGNAL!", 1032785277, __pineBar, __pineLastBar);
   __ti2[__pineBar] = MAvg;
   __ti0[__pineBar] = pine_hist(OTT, __pineBar, (int)(2));
   pine_alertcond(pine_cross(__ti2, __ti0, __pineBar), "OTT - Support Line Crossing!", 566814707, __pineBar, __pineLastBar);
   __ti2[__pineBar] = MAvg;
   __ti0[__pineBar] = pine_hist(OTT, __pineBar, (int)(2));
   pine_alertcond(pine_crossover(__ti2, __ti0, __pineBar), "Support Line BUY SIGNAL!", 405896067, __pineBar, __pineLastBar);
   __ti2[__pineBar] = MAvg;
   __ti0[__pineBar] = pine_hist(OTT, __pineBar, (int)(2));
   pine_alertcond(pine_crossunder(__ti2, __ti0, __pineBar), "Support Line SELL SIGNAL!", 105818713, __pineBar, __pineLastBar);
   __ti0[__pineBar] = pine_hist(OTT, __pineBar, (int)(2));
   pine_alertcond(pine_cross(__src, __ti0, __pineBar), "OTT - Price Crossing!", 980999596, __pineBar, __pineLastBar);
   __ti0[__pineBar] = pine_hist(OTT, __pineBar, (int)(2));
   pine_alertcond(pine_crossover(__src, __ti0, __pineBar), "PRICE OVER OTT - BUY SIGNAL!", 782094106, __pineBar, __pineLastBar);
   __ti0[__pineBar] = pine_hist(OTT, __pineBar, (int)(2));
   pine_alertcond(pine_crossunder(__src, __ti0, __pineBar), "PRICE UNDER OTT - SELL SIGNAL!", 273269808, __pineBar, __pineLastBar);
   __ti2[__pineBar] = MAvg;
   __ti0[__pineBar] = pine_hist(OTT, __pineBar, (int)(2));
   buySignalk = pine_crossover(__ti2, __ti0, __pineBar);
   __ti2[__pineBar] = MAvg;
   __ti0[__pineBar] = pine_hist(OTT, __pineBar, (int)(2));
   sellSignallk = pine_crossunder(__ti2, __ti0, __pineBar);
   __ti0[__pineBar] = pine_hist(OTT, __pineBar, (int)(2));
   buySignalc = pine_crossover(__src, __ti0, __pineBar);
   __ti0[__pineBar] = pine_hist(OTT, __pineBar, (int)(2));
   sellSignallc = pine_crossunder(__src, __ti0, __pineBar);
   longFillColor = (highlighting ? ((MAvg > OTT[__pineBar]) ? C'76,175,80' : PINE_NA) : PINE_NA);
   shortFillColor = (highlighting ? ((MAvg < OTT[__pineBar]) ? C'242,54,69' : PINE_NA) : PINE_NA);
   __ti0[__pineBar] = pine_hist(OTT, __pineBar, (int)(2));
   __ti1[__pineBar] = pine_hist(OTT, __pineBar, (int)(3));
   buySignalr = pine_crossover(__ti0, __ti1, __pineBar);
   __ti0[__pineBar] = pine_hist(OTT, __pineBar, (int)(2));
   __ti1[__pineBar] = pine_hist(OTT, __pineBar, (int)(3));
   sellSignallr = pine_crossunder(__ti0, __ti1, __pineBar);
   Buf4[__pineBar] = (showsupport ? MAvg : PINE_NA);
   double __pinePlotValue3 = pine_nz(pine_hist(OTT, __pineBar, (int)(2)));
   double __pinePlotColour3 = (double)(OTTC);
   Buf5[__pineBar] = __pinePlotValue3;
   Buf6[__pineBar] = pine_color_slot(3, pine_na(__pinePlotColour3) ? clrNONE : (color)__pinePlotColour3, __pal3, __paln3);
   Buf7[__pineBar] = pine_truthy(((((buySignalk ? 1 : 0) & (showsignalsk ? 1 : 0)) != 0) ? (OTT[__pineBar] * 0.995) : PINE_NA)) ? ((((buySignalk ? 1 : 0) & (showsignalsk ? 1 : 0)) != 0) ? (OTT[__pineBar] * 0.995) : PINE_NA) : EMPTY_VALUE;
   pine_shape_text("7", __pineBar, Buf7[__pineBar], "Buy", C'76,175,80', true);
   Buf8[__pineBar] = pine_truthy(((((sellSignallk ? 1 : 0) & (showsignalsk ? 1 : 0)) != 0) ? (OTT[__pineBar] * 1.005) : PINE_NA)) ? ((((sellSignallk ? 1 : 0) & (showsignalsk ? 1 : 0)) != 0) ? (OTT[__pineBar] * 1.005) : PINE_NA) : EMPTY_VALUE;
   pine_shape_text("8", __pineBar, Buf8[__pineBar], "Sell", C'242,54,69', true);
   Buf9[__pineBar] = pine_truthy(((((buySignalc ? 1 : 0) & (showsignalsc ? 1 : 0)) != 0) ? (OTT[__pineBar] * 0.995) : PINE_NA)) ? ((((buySignalc ? 1 : 0) & (showsignalsc ? 1 : 0)) != 0) ? (OTT[__pineBar] * 0.995) : PINE_NA) : EMPTY_VALUE;
   pine_shape_text("9", __pineBar, Buf9[__pineBar], "Buy", C'76,175,80', true);
   Buf10[__pineBar] = pine_truthy(((((sellSignallc ? 1 : 0) & (showsignalsc ? 1 : 0)) != 0) ? (OTT[__pineBar] * 1.005) : PINE_NA)) ? ((((sellSignallc ? 1 : 0) & (showsignalsc ? 1 : 0)) != 0) ? (OTT[__pineBar] * 1.005) : PINE_NA) : EMPTY_VALUE;
   pine_shape_text("10", __pineBar, Buf10[__pineBar], "Sell", C'242,54,69', true);
   Buf11[__pineBar] = ((open[__pineBar] + high[__pineBar] + low[__pineBar] + close[__pineBar]) / 4.0);
   Buf12[__pineBar] = pine_truthy(((((buySignalr ? 1 : 0) & (showsignalsr ? 1 : 0)) != 0) ? (OTT[__pineBar] * 0.995) : PINE_NA)) ? ((((buySignalr ? 1 : 0) & (showsignalsr ? 1 : 0)) != 0) ? (OTT[__pineBar] * 0.995) : PINE_NA) : EMPTY_VALUE;
   pine_shape_text("12", __pineBar, Buf12[__pineBar], "Buy", C'76,175,80', true);
   Buf13[__pineBar] = pine_truthy(((((sellSignallr ? 1 : 0) & (showsignalsr ? 1 : 0)) != 0) ? (OTT[__pineBar] * 1.005) : PINE_NA)) ? ((((sellSignallr ? 1 : 0) & (showsignalsr ? 1 : 0)) != 0) ? (OTT[__pineBar] * 1.005) : PINE_NA) : EMPTY_VALUE;
   pine_shape_text("13", __pineBar, Buf13[__pineBar], "Sell", C'242,54,69', true);
   PineFill0[__pineBar] = Buf11[__pineBar];
   PineFill1[__pineBar] = Buf5[__pineBar];
   PineFill2[__pineBar] = Buf11[__pineBar];
   PineFill3[__pineBar] = Buf5[__pineBar];
}

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(VAR, rates_total);
   ArrayResize(WWMA, rates_total);
   ArrayResize(longStop, rates_total);
   ArrayResize(shortStop, rates_total);
   ArrayResize(dir, rates_total);
   ArrayResize(OTT, rates_total);
   ArrayResize(__st0, rates_total);
   ArrayResize(__stb0, rates_total);
   ArrayResize(__st1, rates_total);
   ArrayResize(__stb1, rates_total);
   ArrayResize(__st2, rates_total);
   ArrayResize(__stb2, rates_total);
   ArrayResize(__st3, rates_total);
   ArrayResize(__stb3, rates_total);
   ArrayResize(__st4, rates_total);
   ArrayResize(__stb4, rates_total);
   ArrayResize(__st5, rates_total);
   ArrayResize(__stb5, rates_total);
   ArrayResize(__st6, rates_total);
   ArrayResize(__stb6, rates_total);
   ArrayResize(__st7, rates_total);
   ArrayResize(__stb7, rates_total);
   ArrayResize(__st8, rates_total);
   ArrayResize(__stb8, rates_total);
   ArrayResize(__st9, rates_total);
   ArrayResize(__stb9, rates_total);
   ArrayResize(__st10, rates_total);
   ArrayResize(__stb10, rates_total);
   ArrayResize(__st11, rates_total);
   ArrayResize(__stb11, rates_total);
   ArrayResize(__st12, rates_total);
   ArrayResize(__stb12, rates_total);
   ArrayResize(__st13, rates_total);
   ArrayResize(__stb13, rates_total);
   ArrayResize(__st14, rates_total);
   ArrayResize(__stb14, rates_total);
   ArrayResize(__st15, rates_total);
   ArrayResize(__stb15, rates_total);
   ArrayResize(__st16, rates_total);
   ArrayResize(__stb16, rates_total);
   ArrayResize(__src, rates_total);
   ArrayResize(__fl_Var_Func_VAR_l, rates_total);
   ArrayResize(__fl_Wwma_Func_WWMA_l, rates_total);
   ArrayResize(__ti0, rates_total);
   ArrayResize(__ti1, rates_total);
   ArrayResize(__ti2, rates_total);
   ArrayResize(__sw0, rates_total);
   ArrayResize(__sw1, rates_total);
   ArrayResize(__sw2, rates_total);
   if(prev_calculated == 0)
   {
      ArrayInitialize(VAR, PINE_NA);
      ArrayInitialize(WWMA, PINE_NA);
      ArrayInitialize(longStop, PINE_NA);
      ArrayInitialize(shortStop, PINE_NA);
      ArrayInitialize(dir, PINE_NA);
      ArrayInitialize(OTT, PINE_NA);
      ArrayInitialize(__st0, PINE_NA);
      ArrayInitialize(__stb0, PINE_NA);
      ArrayInitialize(__st1, PINE_NA);
      ArrayInitialize(__stb1, PINE_NA);
      ArrayInitialize(__st2, PINE_NA);
      ArrayInitialize(__stb2, PINE_NA);
      ArrayInitialize(__st3, PINE_NA);
      ArrayInitialize(__stb3, PINE_NA);
      ArrayInitialize(__st4, PINE_NA);
      ArrayInitialize(__stb4, PINE_NA);
      ArrayInitialize(__st5, PINE_NA);
      ArrayInitialize(__stb5, PINE_NA);
      ArrayInitialize(__st6, PINE_NA);
      ArrayInitialize(__stb6, PINE_NA);
      ArrayInitialize(__st7, PINE_NA);
      ArrayInitialize(__stb7, PINE_NA);
      ArrayInitialize(__st8, PINE_NA);
      ArrayInitialize(__stb8, PINE_NA);
      ArrayInitialize(__st9, PINE_NA);
      ArrayInitialize(__stb9, PINE_NA);
      ArrayInitialize(__st10, PINE_NA);
      ArrayInitialize(__stb10, PINE_NA);
      ArrayInitialize(__st11, PINE_NA);
      ArrayInitialize(__stb11, PINE_NA);
      ArrayInitialize(__st12, PINE_NA);
      ArrayInitialize(__stb12, PINE_NA);
      ArrayInitialize(__st13, PINE_NA);
      ArrayInitialize(__stb13, PINE_NA);
      ArrayInitialize(__st14, PINE_NA);
      ArrayInitialize(__stb14, PINE_NA);
      ArrayInitialize(__st15, PINE_NA);
      ArrayInitialize(__stb15, PINE_NA);
      ArrayInitialize(__st16, PINE_NA);
      ArrayInitialize(__stb16, PINE_NA);
      ArrayInitialize(__src, PINE_NA);
      ArrayInitialize(__fl_Var_Func_VAR_l, PINE_NA);
      ArrayInitialize(__fl_Wwma_Func_WWMA_l, PINE_NA);
      ArrayInitialize(__ti0, PINE_NA);
      ArrayInitialize(__ti1, PINE_NA);
      ArrayInitialize(__ti2, PINE_NA);
      ArrayInitialize(__sw0, PINE_NA);
      ArrayInitialize(__sw1, PINE_NA);
      ArrayInitialize(__sw2, 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;
}

void OnDeinit(const int reason)
{
   ObjectsDeleteAll(0, "pine_");
}

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=4

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/

// © KivancOzbilgic



//created by: @Anil_Ozeksi

//developer: ANIL ÖZEKŞİ

//author: @kivancozbilgic



study("Optimized Trend Tracker","OTT", overlay=true)

src = input(close, title="Source")

length=input(2, "OTT Period", minval=1)

percent=input(1.4, "OTT Percent", type=input.float, step=0.1, minval=0)

showsupport = input(title="Show Support Line?", type=input.bool, defval=true)

showsignalsk = input(title="Show Support Line Crossing Signals?", type=input.bool, defval=true)

showsignalsc = input(title="Show Price/OTT Crossing Signals?", type=input.bool, defval=false)

highlight = input(title="Show OTT Color Changes?", type=input.bool, defval=false)

showsignalsr = input(title="Show OTT Color Change Signals?", type=input.bool, defval=false)

highlighting = input(title="Highlighter On/Off ?", type=input.bool, defval=true)

mav = input(title="Moving Average Type", defval="VAR", options=["SMA", "EMA", "WMA", "TMA", "VAR", "WWMA", "ZLEMA", "TSF"])

Var_Func(src,length)=>

    valpha=2/(length+1)

    vud1=src>src[1] ? src-src[1] : 0

    vdd1=src<src[1] ? src[1]-src : 0

    vUD=sum(vud1,9)

    vDD=sum(vdd1,9)

    vCMO=nz((vUD-vDD)/(vUD+vDD))

    VAR=0.0

    VAR:=nz(valpha*abs(vCMO)*src)+(1-valpha*abs(vCMO))*nz(VAR[1])

VAR=Var_Func(src,length)

Wwma_Func(src,length)=>

    wwalpha = 1/ length

    WWMA = 0.0

    WWMA := wwalpha*src + (1-wwalpha)*nz(WWMA[1])

WWMA=Wwma_Func(src,length)

Zlema_Func(src,length)=>

    zxLag = length/2==round(length/2) ? length/2 : (length - 1) / 2

    zxEMAData = (src + (src - src[zxLag]))

    ZLEMA = ema(zxEMAData, length)

ZLEMA=Zlema_Func(src,length)

Tsf_Func(src,length)=>

    lrc = linreg(src, length, 0)

    lrc1 = linreg(src,length,1)

    lrs = (lrc-lrc1)

    TSF = linreg(src, length, 0)+lrs

TSF=Tsf_Func(src,length)

getMA(src, length) =>

    ma = 0.0

    if mav == "SMA"

        ma := sma(src, length)

        ma



    if mav == "EMA"

        ma := ema(src, length)

        ma



    if mav == "WMA"

        ma := wma(src, length)

        ma



    if mav == "TMA"

        ma := sma(sma(src, ceil(length / 2)), floor(length / 2) + 1)

        ma



    if mav == "VAR"

        ma := VAR

        ma



    if mav == "WWMA"

        ma := WWMA

        ma



    if mav == "ZLEMA"

        ma := ZLEMA

        ma



    if mav == "TSF"

        ma := TSF

        ma

    ma

    

MAvg=getMA(src, length)

fark=MAvg*percent*0.01

longStop = MAvg - fark

longStopPrev = nz(longStop[1], longStop)

longStop := MAvg > longStopPrev ? max(longStop, longStopPrev) : longStop

shortStop =  MAvg + fark

shortStopPrev = nz(shortStop[1], shortStop)

shortStop := MAvg < shortStopPrev ? min(shortStop, shortStopPrev) : shortStop

dir = 1

dir := nz(dir[1], dir)

dir := dir == -1 and MAvg > shortStopPrev ? 1 : dir == 1 and MAvg < longStopPrev ? -1 : dir

MT = dir==1 ? longStop: shortStop

OTT=MAvg>MT ? MT*(200+percent)/200 : MT*(200-percent)/200 

plot(showsupport ? MAvg : na, color=#0585E1, linewidth=2, title="Support Line")

OTTC = highlight ? OTT[2] > OTT[3] ? color.green : color.red : #B800D9 

pALL=plot(nz(OTT[2]), color=OTTC, linewidth=2, title="OTT", transp=0)

alertcondition(cross(OTT[2], OTT[3]), title="Color ALARM", message="OTT Has Changed Color!")

alertcondition(crossover(OTT[2], OTT[3]), title="GREEN ALERT", message="OTT GREEN BUY SIGNAL!")

alertcondition(crossunder(OTT[2], OTT[3]), title="RED ALERT", message="OTT RED SELL SIGNAL!")

alertcondition(cross(MAvg, OTT[2]), title="Cross Alert", message="OTT - Support Line Crossing!")

alertcondition(crossover(MAvg, OTT[2]), title="Crossover Alarm", message="Support Line BUY SIGNAL!")

alertcondition(crossunder(MAvg, OTT[2]), title="Crossunder Alarm", message="Support Line SELL SIGNAL!")

alertcondition(cross(src, OTT[2]), title="Price Cross Alert", message="OTT - Price Crossing!")

alertcondition(crossover(src, OTT[2]), title="Price Crossover Alarm", message="PRICE OVER OTT - BUY SIGNAL!")

alertcondition(crossunder(src, OTT[2]), title="Price Crossunder Alarm", message="PRICE UNDER OTT - SELL SIGNAL!")

buySignalk = crossover(MAvg, OTT[2])

plotshape(buySignalk and showsignalsk ? OTT*0.995 : na, title="Buy", text="Buy", location=location.absolute, style=shape.labelup, size=size.tiny, color=color.green, textcolor=color.white, transp=0)

sellSignallk = crossunder(MAvg, OTT[2])

plotshape(sellSignallk and showsignalsk ? OTT*1.005 : na, title="Sell", text="Sell", location=location.absolute, style=shape.labeldown, size=size.tiny, color=color.red, textcolor=color.white, transp=0)

buySignalc = crossover(src, OTT[2])

plotshape(buySignalc and showsignalsc ? OTT*0.995 : na, title="Buy", text="Buy", location=location.absolute, style=shape.labelup, size=size.tiny, color=color.green, textcolor=color.white, transp=0)

sellSignallc = crossunder(src, OTT[2])

plotshape(sellSignallc and showsignalsc ? OTT*1.005 : na, title="Sell", text="Sell", location=location.absolute, style=shape.labeldown, size=size.tiny, color=color.red, textcolor=color.white, transp=0)

mPlot = plot(ohlc4, title="", style=plot.style_circles, linewidth=0,display=display.none)

longFillColor = highlighting ? (MAvg>OTT ? color.green : na) : na

shortFillColor = highlighting ? (MAvg<OTT ? color.red : na) : na

fill(mPlot, pALL, title="UpTrend Highligter", color=longFillColor)

fill(mPlot, pALL, title="DownTrend Highligter", color=shortFillColor)

buySignalr = crossover(OTT[2], OTT[3])

plotshape(buySignalr and showsignalsr ? OTT*0.995 : na, title="Buy", text="Buy", location=location.absolute, style=shape.labelup, size=size.tiny, color=color.green, textcolor=color.white, transp=0)

sellSignallr = crossunder(OTT[2], OTT[3])

plotshape(sellSignallr and showsignalsr ? OTT*1.005 : na, title="Sell", text="Sell", location=location.absolute, style=shape.labeldown, size=size.tiny, color=color.red, textcolor=color.white, transp=0)

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