Stochastic Optimized Trend Tracker in MQL5 — converted from Pine Script

The Stochastic 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, separate window
Verdict
EQUIVALENT on 500 bars
Engine
pinec 0.6.46
Conversion notes
7 informational
Source
143 lines of Pine v4

Inputs

InputDefault
%K Length500
%K Smoothing200
Sourceclose
OTT Period2
OTT Percent0.5
Show Support Line?false
Show Stochastic/OTT Crossing Signals?false

Plots

%K, Support Line.

Converted MQL5

Save as stochastic-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: 798c904d6c1c   status: CLEAN
//| notes: 0 unsupported, 0 warn, 7 info
//| Review, compile and backtest before trading.
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| Stochastic 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_separate_window
#property indicator_buffers 9
#property indicator_plots   8
#property indicator_label1  "Background"
#property indicator_type1   DRAW_FILLING
#property indicator_color1  clrDodgerBlue
#property indicator_width1  1
#property indicator_label2  "Upper Band"
#property indicator_type2   DRAW_LINE
#property indicator_color2  clrDodgerBlue
#property indicator_width2  1
#property indicator_label3  "Lower Band"
#property indicator_type3   DRAW_LINE
#property indicator_color3  clrDodgerBlue
#property indicator_width3  1
#property indicator_label4  "%K"
#property indicator_type4   DRAW_LINE
#property indicator_color4  clrDodgerBlue
#property indicator_width4  1
#property indicator_label5  "Support Line"
#property indicator_type5   DRAW_LINE
#property indicator_color5  clrDodgerBlue
#property indicator_width5  2
#property indicator_label6  "OTT"
#property indicator_type6   DRAW_LINE
#property indicator_color6  clrDodgerBlue
#property indicator_width6  2
#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

#include "PineRuntime.mqh"

//--- plot buffers
double PineFill0[];
double PineFill1[];
double Buf2[];
double Buf3[];
double Buf4[];
double Buf5[];
double Buf6[];
double Buf7[];
double Buf8[];

//--- 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 VAR1[];
double k = PINE_NA;
double src[];
double VAR[];
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[];
color OTTC = clrNONE;
bool buySignalc = false;
bool sellSignallc = false;

//--- live-tick rollback: the state each bar started from
double __sv_k;
double __si_k;
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;
color __sv_OTTC;
color __si_OTTC;
bool __sv_buySignalc;
bool __si_buySignalc;
bool __sv_sellSignallc;
bool __si_sellSignallc;
int __pineSnapBar = -1;
void __pineSaveState()
{
   __sv_k = k;
   __sv_MAvg = MAvg;
   __sv_fark = fark;
   __sv_longStopPrev = longStopPrev;
   __sv_shortStopPrev = shortStopPrev;
   __sv_MT = MT;
   __sv_OTTC = OTTC;
   __sv_buySignalc = buySignalc;
   __sv_sellSignallc = sellSignallc;
   pine_draw_mark();
   __pineSaveGen++;   // helpers' static arrays snapshot lazily
}
void __pineRestoreState()
{
   k = __sv_k;
   MAvg = __sv_MAvg;
   fark = __sv_fark;
   longStopPrev = __sv_longStopPrev;
   shortStopPrev = __sv_shortStopPrev;
   MT = __sv_MT;
   OTTC = __sv_OTTC;
   buySignalc = __sv_buySignalc;
   sellSignallc = __sv_sellSignallc;
   pine_draw_rollback();
   __pineRestoreGen++;
   __pineRestoreSaveGen = __pineSaveGen;
}
void __pineSaveInitial()
{
   __si_k = k;
   __si_MAvg = MAvg;
   __si_fark = fark;
   __si_longStopPrev = longStopPrev;
   __si_shortStopPrev = shortStopPrev;
   __si_MT = MT;
   __si_OTTC = OTTC;
   __si_buySignalc = buySignalc;
   __si_sellSignallc = sellSignallc;
   pine_draw_save_initial();
}
void __pineRestoreInitial()
{
   k = __si_k;
   MAvg = __si_MAvg;
   fark = __si_fark;
   longStopPrev = __si_longStopPrev;
   shortStopPrev = __si_shortStopPrev;
   MT = __si_MT;
   OTTC = __si_OTTC;
   buySignalc = __si_buySignalc;
   sellSignallc = __si_sellSignallc;
   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 __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 __src1[];   // Pine's `src1` as a real series, filled per bar
double __fl_Var_Func1_VAR1_l[];   // history of `VAR1_l`, a local of Var_Func1() the script reads with [n]
double __fl_Var_Func_VAR_l[];   // history of `VAR_l`, a local of Var_Func() the script reads with [n]
double __fl_Var_Func1__site1_VAR1_l[];   // history of `VAR1_l`, a local of Var_Func1__site1() the script reads with [n]
double __fl_Var_Func__site1_VAR_l[];   // history of `VAR_l`, a local of Var_Func__site1() the script reads with [n]
double __ts0[];   // materialised ta.* source: pine_stoch(close, high, low, __pineBar, (int)(periodK))
double __ti0[];   // materialised ta.* source: pine_hist(OTT, __pineBar, (int)(2))
double __sw0[];   // materialised ta.* source: Var_Func1 :: vud11
double __sw1[];   // materialised ta.* source: Var_Func1 :: vdd11
double __sw2[];   // materialised ta.* source: Var_Func :: vud1
double __sw3[];   // materialised ta.* source: Var_Func :: vdd1
double __sw4[];   // materialised ta.* source: Var_Func1__site1 :: vud11
double __sw5[];   // materialised ta.* source: Var_Func1__site1 :: vdd11
double __sw6[];   // materialised ta.* source: Var_Func__site1 :: vud1
double __sw7[];   // materialised ta.* source: Var_Func__site1 :: vdd1

//--- inputs
input int periodK = 500;   // %K Length
input int smoothK = 200;   // %K Smoothing
input ENUM_APPLIED_PRICE src1 = PRICE_CLOSE;   // Source
input int length = 2;   // OTT Period
input double percent = 0.5;   // OTT Percent
input bool showsupport = false;   // Show Support Line?
input bool showsignalsc = false;   // Show Stochastic/OTT Crossing Signals?

//--- user functions
double Var_Func1(const double &src1[], const int length, const int __pineBar)
{
   double valpha1 = pine_div(2, (length + 1));
   double vud11 = ((src1[__pineBar] > pine_hist(src1, __pineBar, (int)(1))) ? (src1[__pineBar] - pine_hist(src1, __pineBar, (int)(1))) : 0);
   double vdd11 = ((src1[__pineBar] < pine_hist(src1, __pineBar, (int)(1))) ? (pine_hist(src1, __pineBar, (int)(1)) - src1[__pineBar]) : 0);
   double vUD1 = pine_sum_step(vud11, __sw0, __pineBar, (int)(9));
   double vDD1 = pine_sum_step(vdd11, __sw1, __pineBar, (int)(9));
   double vCMO1 = pine_nz(pine_div((vUD1 - vDD1), (vUD1 + vDD1)));
   __fl_Var_Func1_VAR1_l[__pineBar] = 0.0;
   __fl_Var_Func1_VAR1_l[__pineBar] = (pine_nz(((valpha1 * MathAbs(vCMO1)) * src1[__pineBar])) + ((1 - (valpha1 * MathAbs(vCMO1))) * pine_nz(pine_hist(__fl_Var_Func1_VAR1_l, __pineBar, (int)(1)))));
   return __fl_Var_Func1_VAR1_l[__pineBar];
   return PINE_NA;
}

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, __sw2, __pineBar, (int)(9));
   double vDD = pine_sum_step(vdd1, __sw3, __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 Var_Func1__site1(const double &src1[], const int length, const int __pineBar)
{
   double valpha1 = pine_div(2, (length + 1));
   double vud11 = ((src1[__pineBar] > pine_hist(src1, __pineBar, (int)(1))) ? (src1[__pineBar] - pine_hist(src1, __pineBar, (int)(1))) : 0);
   double vdd11 = ((src1[__pineBar] < pine_hist(src1, __pineBar, (int)(1))) ? (pine_hist(src1, __pineBar, (int)(1)) - src1[__pineBar]) : 0);
   double vUD1 = pine_sum_step(vud11, __sw4, __pineBar, (int)(9));
   double vDD1 = pine_sum_step(vdd11, __sw5, __pineBar, (int)(9));
   double vCMO1 = pine_nz(pine_div((vUD1 - vDD1), (vUD1 + vDD1)));
   __fl_Var_Func1__site1_VAR1_l[__pineBar] = 0.0;
   __fl_Var_Func1__site1_VAR1_l[__pineBar] = (pine_nz(((valpha1 * MathAbs(vCMO1)) * src1[__pineBar])) + ((1 - (valpha1 * MathAbs(vCMO1))) * pine_nz(pine_hist(__fl_Var_Func1__site1_VAR1_l, __pineBar, (int)(1)))));
   return __fl_Var_Func1__site1_VAR1_l[__pineBar];
   return PINE_NA;
}

double Var_Func__site1(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, __sw6, __pineBar, (int)(9));
   double vDD = pine_sum_step(vdd1, __sw7, __pineBar, (int)(9));
   double vCMO = pine_nz(pine_div((vUD - vDD), (vUD + vDD)));
   __fl_Var_Func__site1_VAR_l[__pineBar] = 0.0;
   __fl_Var_Func__site1_VAR_l[__pineBar] = (pine_nz(((valpha * MathAbs(vCMO)) * src[__pineBar])) + ((1 - (valpha * MathAbs(vCMO))) * pine_nz(pine_hist(__fl_Var_Func__site1_VAR_l, __pineBar, (int)(1)))));
   return __fl_Var_Func__site1_VAR_l[__pineBar];
   return PINE_NA;
}

int OnInit()
{
   if(periodK < 1) { Print("%K Length", " must be at least 1"); return INIT_PARAMETERS_INCORRECT; }
   if(smoothK < 1) { Print("%K Smoothing", " must be at least 1"); return INIT_PARAMETERS_INCORRECT; }
   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; }
   OTTC = C'184,0,217';
   SetIndexBuffer(0, PineFill0, INDICATOR_DATA);
   SetIndexBuffer(1, PineFill1, INDICATOR_DATA);
   SetIndexBuffer(2, Buf2, INDICATOR_DATA);
   SetIndexBuffer(3, Buf3, INDICATOR_DATA);
   SetIndexBuffer(4, Buf4, INDICATOR_DATA);
   SetIndexBuffer(5, Buf5, INDICATOR_DATA);
   SetIndexBuffer(6, Buf6, INDICATOR_DATA);
   SetIndexBuffer(7, Buf7, INDICATOR_DATA);
   SetIndexBuffer(8, Buf8, 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);
   PlotIndexSetInteger(0, PLOT_SHOW_DATA, false);
   PlotIndexSetInteger(0, PLOT_LINE_COLOR, 0, C'153,21,255');
   PlotIndexSetInteger(0, PLOT_LINE_COLOR, 1, C'153,21,255');
   PlotIndexSetInteger(1, PLOT_LINE_COLOR, C'96,96,96');
   PlotIndexSetInteger(2, PLOT_LINE_COLOR, C'96,96,96');
   PlotIndexSetInteger(3, PLOT_LINE_COLOR, C'0,148,255');
   PlotIndexSetInteger(4, PLOT_LINE_COLOR, C'5,133,225');
   PlotIndexSetInteger(5, PLOT_LINE_COLOR, OTTC);
   PlotIndexSetInteger(6, PLOT_LINE_COLOR, C'76,175,80');
   PlotIndexSetInteger(7, PLOT_LINE_COLOR, C'242,54,69');
   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);
   IndicatorSetString(INDICATOR_SHORTNAME, "Stochastic 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[])
{
   __src1[__pineBar] = pine_source(src1, open, high, low, close, __pineBar);
   __ts0[__pineBar] = pine_stoch(close, high, low, __pineBar, (int)(periodK));
   if(__pineBar > 0)
   {
      VAR1[__pineBar] = VAR1[__pineBar - 1];
      src[__pineBar] = src[__pineBar - 1];
      VAR[__pineBar] = VAR[__pineBar - 1];
      longStop[__pineBar] = longStop[__pineBar - 1];
      shortStop[__pineBar] = shortStop[__pineBar - 1];
      dir[__pineBar] = dir[__pineBar - 1];
      OTT[__pineBar] = OTT[__pineBar - 1];
   }
   VAR1[__pineBar] = Var_Func1(__src1, length, __pineBar);
   k = Var_Func1__site1(__ts0, smoothK, __pineBar);
   src[__pineBar] = (k + 1000);
   VAR[__pineBar] = Var_Func(src, length, __pineBar);
   MAvg = Var_Func__site1(src, 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 = C'184,0,217';
   __ti0[__pineBar] = pine_hist(OTT, __pineBar, (int)(2));
   pine_alertcond(pine_cross(src, __ti0, __pineBar), "OTT - Price Crossing!", 883354572, __pineBar, __pineLastBar);
   __ti0[__pineBar] = pine_hist(OTT, __pineBar, (int)(2));
   pine_alertcond(pine_crossover(src, __ti0, __pineBar), "PRICE OVER OTT - BUY SIGNAL!", 237828090, __pineBar, __pineLastBar);
   __ti0[__pineBar] = pine_hist(OTT, __pineBar, (int)(2));
   pine_alertcond(pine_crossunder(src, __ti0, __pineBar), "PRICE UNDER OTT - SELL SIGNAL!", 372685968, __pineBar, __pineLastBar);
   __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);
   Buf2[__pineBar] = 1080;
   Buf3[__pineBar] = 1020;
   Buf4[__pineBar] = (k + 1000);
   Buf5[__pineBar] = (showsupport ? MAvg : PINE_NA);
   Buf6[__pineBar] = pine_nz(pine_hist(OTT, __pineBar, (int)(2)));
   Buf7[__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("7", __pineBar, Buf7[__pineBar], "Buy", C'76,175,80', true);
   Buf8[__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("8", __pineBar, Buf8[__pineBar], "Sell", C'242,54,69', true);
   PineFill0[__pineBar] = Buf2[__pineBar];
   PineFill1[__pineBar] = Buf3[__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(VAR1, rates_total);
   ArrayResize(src, rates_total);
   ArrayResize(VAR, 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(__src1, rates_total);
   ArrayResize(__fl_Var_Func1_VAR1_l, rates_total);
   ArrayResize(__fl_Var_Func_VAR_l, rates_total);
   ArrayResize(__fl_Var_Func1__site1_VAR1_l, rates_total);
   ArrayResize(__fl_Var_Func__site1_VAR_l, rates_total);
   ArrayResize(__ts0, rates_total);
   ArrayResize(__ti0, rates_total);
   ArrayResize(__sw0, rates_total);
   ArrayResize(__sw1, rates_total);
   ArrayResize(__sw2, rates_total);
   ArrayResize(__sw3, rates_total);
   ArrayResize(__sw4, rates_total);
   ArrayResize(__sw5, rates_total);
   ArrayResize(__sw6, rates_total);
   ArrayResize(__sw7, rates_total);
   if(prev_calculated == 0)
   {
      ArrayInitialize(VAR1, PINE_NA);
      ArrayInitialize(src, PINE_NA);
      ArrayInitialize(VAR, 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(__src1, PINE_NA);
      ArrayInitialize(__fl_Var_Func1_VAR1_l, PINE_NA);
      ArrayInitialize(__fl_Var_Func_VAR_l, PINE_NA);
      ArrayInitialize(__fl_Var_Func1__site1_VAR1_l, PINE_NA);
      ArrayInitialize(__fl_Var_Func__site1_VAR_l, PINE_NA);
      ArrayInitialize(__ts0, PINE_NA);
      ArrayInitialize(__ti0, PINE_NA);
      ArrayInitialize(__sw0, PINE_NA);
      ArrayInitialize(__sw1, PINE_NA);
      ArrayInitialize(__sw2, PINE_NA);
      ArrayInitialize(__sw3, PINE_NA);
      ArrayInitialize(__sw4, PINE_NA);
      ArrayInitialize(__sw5, PINE_NA);
      ArrayInitialize(__sw6, PINE_NA);
      ArrayInitialize(__sw7, 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(title="Stochastic Optimized Trend Tracker", shorttitle="SOTT", format=format.price, precision=2, resolution="")

periodK = input(500, title="%K Length", minval=1)

smoothK = input(200, title="%K Smoothing", minval=1)

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

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

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

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

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

Var_Func1(src1,length)=>

    valpha1=2/(length+1)

    vud11=src1>src1[1] ? src1-src1[1] : 0

    vdd11=src1<src1[1] ? src1[1]-src1 : 0

    vUD1=sum(vud11,9)

    vDD1=sum(vdd11,9)

    vCMO1=nz((vUD1-vDD1)/(vUD1+vDD1))

    VAR1=0.0

    VAR1:=nz(valpha1*abs(vCMO1)*src1)+(1-valpha1*abs(vCMO1))*nz(VAR1[1])

VAR1=Var_Func1(src1,length)

k = Var_Func1(stoch(close, high, low, periodK), smoothK)

src=k+1000

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)

h0 = hline(1080, "Upper Band", color=#606060)

h1 = hline(1020, "Lower Band", color=#606060)

fill(h0, h1, color=#9915FF, transp=80, title="Background")

plot(k+1000, title="%K", color=#0094FF)

MAvg=Var_Func(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 = #B800D9 

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

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!")

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)

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