Optimized Trend Tracker Bands in MQL5 — converted from Pine Script
The Optimized Trend Tracker Bands 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).
Inputs
| Input | Default |
|---|---|
| Source | close |
| OTT Moving Average Length | 2 |
| OTT Optimization Coeff | 15 |
| OTT UPPER BAND Optimization Coeff | 0.1 |
| OTT LOWER BAND Optimization Coeff | 0.077 |
| Show OTT Bands Fibonacci Levels? | false |
| Show Support Line? | false |
| Show OTT Support Line Color Changes? | false |
| Moving Average Type | "VAR" |
Plots
Support Line.
Converted MQL5
Save as optimized-trend-tracker-bands.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: 6fd8f55f5097 status: CLEAN
//| notes: 0 unsupported, 0 warn, 8 info
//| Review, compile and backtest before trading.
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| Optimized Trend Tracker Bands
//| 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 25
#property indicator_plots 16
#property indicator_label1 "Plot1"
#property indicator_type1 DRAW_FILLING
#property indicator_color1 clrDodgerBlue
#property indicator_width1 1
#property indicator_label2 "Plot2"
#property indicator_type2 DRAW_FILLING
#property indicator_color2 clrDodgerBlue
#property indicator_width2 1
#property indicator_label3 "Plot3"
#property indicator_type3 DRAW_FILLING
#property indicator_color3 clrDodgerBlue
#property indicator_width3 1
#property indicator_label4 "Plot4"
#property indicator_type4 DRAW_FILLING
#property indicator_color4 clrDodgerBlue
#property indicator_width4 1
#property indicator_label5 "Plot5"
#property indicator_type5 DRAW_FILLING
#property indicator_color5 clrDodgerBlue
#property indicator_width5 1
#property indicator_label6 "Plot6"
#property indicator_type6 DRAW_FILLING
#property indicator_color6 clrDodgerBlue
#property indicator_width6 1
#property indicator_label7 "Plot7"
#property indicator_type7 DRAW_FILLING
#property indicator_color7 clrDodgerBlue
#property indicator_width7 1
#property indicator_label8 "Plot8"
#property indicator_type8 DRAW_FILLING
#property indicator_color8 clrDodgerBlue
#property indicator_width8 1
#property indicator_label9 "OTT"
#property indicator_type9 DRAW_LINE
#property indicator_color9 clrDodgerBlue
#property indicator_width9 2
#property indicator_label10 "Support Line"
#property indicator_type10 DRAW_COLOR_LINE
#property indicator_color10 clrDodgerBlue
#property indicator_width10 2
#property indicator_label11 "OTT UPPER"
#property indicator_type11 DRAW_LINE
#property indicator_color11 clrDodgerBlue
#property indicator_width11 2
#property indicator_label12 "OTT UPPER HALF "
#property indicator_type12 DRAW_LINE
#property indicator_color12 clrDodgerBlue
#property indicator_width12 1
#property indicator_label13 "OTT UPPER HALF "
#property indicator_type13 DRAW_LINE
#property indicator_color13 clrDodgerBlue
#property indicator_width13 1
#property indicator_label14 "OTT LOWER"
#property indicator_type14 DRAW_LINE
#property indicator_color14 clrDodgerBlue
#property indicator_width14 2
#property indicator_label15 "OTT LOWER HALF"
#property indicator_type15 DRAW_LINE
#property indicator_color15 clrDodgerBlue
#property indicator_width15 1
#property indicator_label16 "OTT LOWER HALF"
#property indicator_type16 DRAW_LINE
#property indicator_color16 clrDodgerBlue
#property indicator_width16 1
#include "PineRuntime.mqh"
//--- plot buffers
double PineFill0[];
double PineFill1[];
double PineFill2[];
double PineFill3[];
double PineFill4[];
double PineFill5[];
double PineFill6[];
double PineFill7[];
double PineFill8[];
double PineFill9[];
double PineFill10[];
double PineFill11[];
double PineFill12[];
double PineFill13[];
double PineFill14[];
double PineFill15[];
double Buf16[];
double Buf17[];
double Buf18[];
double Buf19[];
double Buf20[];
double Buf21[];
double Buf22[];
double Buf23[];
double Buf24[];
color __pal9[64]; // the colours plot 9 has drawn so far; see pine_color_slot
int __paln9 = 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[];
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;
//--- live-tick rollback: the state each bar started from
double __sv_ZLEMA;
double __si_ZLEMA;
double __sv_TSF;
double __si_TSF;
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;
int __pineSnapBar = -1;
void __pineSaveState()
{
__sv_ZLEMA = ZLEMA;
__sv_TSF = TSF;
__sv_fark = fark;
__sv_longStopPrev = longStopPrev;
__sv_shortStopPrev = shortStopPrev;
__sv_MT = MT;
__sv_OTTC = OTTC;
pine_draw_mark();
__pineSaveGen++; // helpers' static arrays snapshot lazily
}
void __pineRestoreState()
{
ZLEMA = __sv_ZLEMA;
TSF = __sv_TSF;
fark = __sv_fark;
longStopPrev = __sv_longStopPrev;
shortStopPrev = __sv_shortStopPrev;
MT = __sv_MT;
OTTC = __sv_OTTC;
pine_draw_rollback();
__pineRestoreGen++;
__pineRestoreSaveGen = __pineSaveGen;
}
void __pineSaveInitial()
{
__si_ZLEMA = ZLEMA;
__si_TSF = TSF;
__si_fark = fark;
__si_longStopPrev = longStopPrev;
__si_shortStopPrev = shortStopPrev;
__si_MT = MT;
__si_OTTC = OTTC;
pine_draw_save_initial();
}
void __pineRestoreInitial()
{
ZLEMA = __si_ZLEMA;
TSF = __si_TSF;
fark = __si_fark;
longStopPrev = __si_longStopPrev;
shortStopPrev = __si_shortStopPrev;
MT = __si_MT;
OTTC = __si_OTTC;
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 __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 __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 Moving Average Length
input double percent = 15; // OTT Optimization Coeff
input double X3 = 0.1; // OTT UPPER BAND Optimization Coeff
input double X5 = 0.077; // OTT LOWER BAND Optimization Coeff
input bool showfibolines = false; // Show OTT Bands Fibonacci Levels?
input bool showsupport = false; // Show Support Line?
input bool highlight = false; // Show OTT Support Line Color Changes?
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 Moving Average Length", " must be at least 1"); return INIT_PARAMETERS_INCORRECT; }
if(percent < 0) { Print("OTT Optimization Coeff", " must be at least 0"); return INIT_PARAMETERS_INCORRECT; }
if(X3 < 0) { Print("OTT UPPER BAND Optimization Coeff", " must be at least 0"); return INIT_PARAMETERS_INCORRECT; }
if(X5 < 0) { Print("OTT LOWER BAND Optimization Coeff", " 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, PineFill4, INDICATOR_DATA);
SetIndexBuffer(5, PineFill5, INDICATOR_DATA);
SetIndexBuffer(6, PineFill6, INDICATOR_DATA);
SetIndexBuffer(7, PineFill7, INDICATOR_DATA);
SetIndexBuffer(8, PineFill8, INDICATOR_DATA);
SetIndexBuffer(9, PineFill9, INDICATOR_DATA);
SetIndexBuffer(10, PineFill10, INDICATOR_DATA);
SetIndexBuffer(11, PineFill11, INDICATOR_DATA);
SetIndexBuffer(12, PineFill12, INDICATOR_DATA);
SetIndexBuffer(13, PineFill13, INDICATOR_DATA);
SetIndexBuffer(14, PineFill14, INDICATOR_DATA);
SetIndexBuffer(15, PineFill15, INDICATOR_DATA);
SetIndexBuffer(16, Buf16, INDICATOR_DATA);
SetIndexBuffer(17, Buf17, INDICATOR_DATA);
SetIndexBuffer(18, Buf18, INDICATOR_COLOR_INDEX);
SetIndexBuffer(19, Buf19, INDICATOR_DATA);
SetIndexBuffer(20, Buf20, INDICATOR_DATA);
SetIndexBuffer(21, Buf21, INDICATOR_DATA);
SetIndexBuffer(22, Buf22, INDICATOR_DATA);
SetIndexBuffer(23, Buf23, INDICATOR_DATA);
SetIndexBuffer(24, Buf24, 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);
PlotIndexSetDouble(11, PLOT_EMPTY_VALUE, EMPTY_VALUE);
PlotIndexSetDouble(12, PLOT_EMPTY_VALUE, EMPTY_VALUE);
PlotIndexSetDouble(13, PLOT_EMPTY_VALUE, EMPTY_VALUE);
PlotIndexSetDouble(14, PLOT_EMPTY_VALUE, EMPTY_VALUE);
PlotIndexSetDouble(15, PLOT_EMPTY_VALUE, EMPTY_VALUE);
PlotIndexSetInteger(0, PLOT_SHOW_DATA, false);
PlotIndexSetInteger(0, PLOT_LINE_COLOR, 0, C'69,0,196');
PlotIndexSetInteger(0, PLOT_LINE_COLOR, 1, C'69,0,196');
PlotIndexSetInteger(1, PLOT_SHOW_DATA, false);
PlotIndexSetInteger(1, PLOT_LINE_COLOR, 0, C'132,0,255');
PlotIndexSetInteger(1, PLOT_LINE_COLOR, 1, C'132,0,255');
PlotIndexSetInteger(2, PLOT_SHOW_DATA, false);
PlotIndexSetInteger(2, PLOT_LINE_COLOR, 0, C'200,16,255');
PlotIndexSetInteger(2, PLOT_LINE_COLOR, 1, C'200,16,255');
PlotIndexSetInteger(3, PLOT_SHOW_DATA, false);
PlotIndexSetInteger(3, PLOT_LINE_COLOR, 0, C'200,16,255');
PlotIndexSetInteger(3, PLOT_LINE_COLOR, 1, C'200,16,255');
PlotIndexSetInteger(4, PLOT_SHOW_DATA, false);
PlotIndexSetInteger(4, PLOT_LINE_COLOR, 0, C'0,166,255');
PlotIndexSetInteger(4, PLOT_LINE_COLOR, 1, C'0,166,255');
PlotIndexSetInteger(5, PLOT_SHOW_DATA, false);
PlotIndexSetInteger(5, PLOT_LINE_COLOR, 0, C'0,123,189');
PlotIndexSetInteger(5, PLOT_LINE_COLOR, 1, C'0,123,189');
PlotIndexSetInteger(6, PLOT_SHOW_DATA, false);
PlotIndexSetInteger(6, PLOT_LINE_COLOR, 0, C'2,77,227');
PlotIndexSetInteger(6, PLOT_LINE_COLOR, 1, C'2,77,227');
PlotIndexSetInteger(7, PLOT_SHOW_DATA, false);
PlotIndexSetInteger(7, PLOT_LINE_COLOR, 0, C'2,77,227');
PlotIndexSetInteger(7, PLOT_LINE_COLOR, 1, C'2,77,227');
PlotIndexSetInteger(8, PLOT_LINE_COLOR, C'184,0,217');
PlotIndexSetInteger(10, PLOT_LINE_COLOR, C'69,0,196');
PlotIndexSetInteger(11, PLOT_LINE_COLOR, C'132,0,255');
PlotIndexSetInteger(12, PLOT_LINE_COLOR, C'200,48,255');
PlotIndexSetInteger(13, PLOT_LINE_COLOR, C'0,166,255');
PlotIndexSetInteger(14, PLOT_LINE_COLOR, C'0,123,189');
PlotIndexSetInteger(15, PLOT_LINE_COLOR, C'2,77,227');
IndicatorSetString(INDICATOR_SHORTNAME, "Optimized Trend Tracker Bands");
__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];
MAvg[__pineBar] = MAvg[__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[__pineBar] = getMA(pine_source(src, open, high, low, close, __pineBar), length, __pineBar);
fark = ((MAvg[__pineBar] * percent) * 0.01);
longStop[__pineBar] = (MAvg[__pineBar] - fark);
longStopPrev = pine_nz(pine_hist(longStop, __pineBar, (int)(1)), longStop[__pineBar]);
longStop[__pineBar] = ((MAvg[__pineBar] > longStopPrev) ? MathMax(longStop[__pineBar], longStopPrev) : longStop[__pineBar]);
shortStop[__pineBar] = (MAvg[__pineBar] + fark);
shortStopPrev = pine_nz(pine_hist(shortStop, __pineBar, (int)(1)), shortStop[__pineBar]);
shortStop[__pineBar] = ((MAvg[__pineBar] < 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[__pineBar] > shortStopPrev) ? 1 : 0)) != 0) ? 1 : (((((dir[__pineBar] == 1) ? 1 : 0) & ((MAvg[__pineBar] < longStopPrev) ? 1 : 0)) != 0) ? (-1) : dir[__pineBar]));
MT = ((dir[__pineBar] == 1) ? longStop[__pineBar] : shortStop[__pineBar]);
OTT[__pineBar] = ((MAvg[__pineBar] > MT) ? pine_div((MT * (200 + percent)), 200) : pine_div((MT * (200 - percent)), 200));
OTTC = (highlight ? ((MAvg[__pineBar] > pine_hist(MAvg, __pineBar, (int)(1))) ? C'76,175,80' : C'242,54,69') : C'5,133,225');
__ti0[__pineBar] = pine_hist(OTT, __pineBar, (int)(2));
pine_alertcond(pine_cross(MAvg, __ti0, __pineBar), "OTT - Support Line Crossing!", 1229456029, __pineBar, __pineLastBar);
__ti0[__pineBar] = pine_hist(OTT, __pineBar, (int)(2));
pine_alertcond(pine_crossover(MAvg, __ti0, __pineBar), "Support Line BUY SIGNAL!", 1377767253, __pineBar, __pineLastBar);
__ti0[__pineBar] = pine_hist(OTT, __pineBar, (int)(2));
pine_alertcond(pine_crossunder(MAvg, __ti0, __pineBar), "Support Line SELL SIGNAL!", 701707011, __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);
Buf16[__pineBar] = pine_nz(pine_hist(OTT, __pineBar, (int)(2)));
double __pinePlotValue9 = (showsupport ? MAvg[__pineBar] : PINE_NA);
double __pinePlotColour9 = (double)(OTTC);
Buf17[__pineBar] = __pinePlotValue9;
Buf18[__pineBar] = pine_color_slot(9, pine_na(__pinePlotColour9) ? clrNONE : (color)__pinePlotColour9, __pal9, __paln9);
Buf19[__pineBar] = (pine_nz(pine_hist(OTT, __pineBar, (int)(2))) * (1 + X3));
Buf20[__pineBar] = (showfibolines ? (pine_nz(pine_hist(OTT, __pineBar, (int)(2))) * (1 + (X3 * 0.618))) : (pine_nz(pine_hist(OTT, __pineBar, (int)(2))) * (1 + pine_div(X3, 2))));
Buf21[__pineBar] = (showfibolines ? (pine_nz(pine_hist(OTT, __pineBar, (int)(2))) * (1 + (X3 * 0.382))) : PINE_NA);
Buf22[__pineBar] = (pine_nz(pine_hist(OTT, __pineBar, (int)(2))) * (1 - X5));
Buf23[__pineBar] = (showfibolines ? (pine_nz(pine_hist(OTT, __pineBar, (int)(2))) * (1 - (X5 * 0.618))) : (pine_nz(pine_hist(OTT, __pineBar, (int)(2))) * (1 - pine_div(X5, 2))));
Buf24[__pineBar] = (showfibolines ? (pine_nz(pine_hist(OTT, __pineBar, (int)(2))) * (1 - (X5 * 0.382))) : PINE_NA);
PineFill0[__pineBar] = Buf19[__pineBar];
PineFill1[__pineBar] = Buf20[__pineBar];
PineFill2[__pineBar] = Buf21[__pineBar];
PineFill3[__pineBar] = Buf20[__pineBar];
PineFill4[__pineBar] = Buf20[__pineBar];
PineFill5[__pineBar] = Buf16[__pineBar];
PineFill6[__pineBar] = Buf21[__pineBar];
PineFill7[__pineBar] = Buf16[__pineBar];
PineFill8[__pineBar] = Buf22[__pineBar];
PineFill9[__pineBar] = Buf23[__pineBar];
PineFill10[__pineBar] = Buf24[__pineBar];
PineFill11[__pineBar] = Buf23[__pineBar];
PineFill12[__pineBar] = Buf23[__pineBar];
PineFill13[__pineBar] = Buf16[__pineBar];
PineFill14[__pineBar] = Buf24[__pineBar];
PineFill15[__pineBar] = Buf16[__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(MAvg, 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(__src, rates_total);
ArrayResize(__fl_Var_Func_VAR_l, rates_total);
ArrayResize(__fl_Wwma_Func_WWMA_l, rates_total);
ArrayResize(__ti0, 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(MAvg, 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(__src, PINE_NA);
ArrayInitialize(__fl_Var_Func_VAR_l, PINE_NA);
ArrayInitialize(__fl_Wwma_Func_WWMA_l, PINE_NA);
ArrayInitialize(__ti0, 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;
}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 Bands","OTT Bands", overlay=true,format=format.price, precision=2, resolution="")
src = input(close, title="Source")
length=input(2, "OTT Moving Average Length", minval=1)
percent=input(15, "OTT Optimization Coeff", type=input.float, step=0.1, minval=0)
X3=input(0.1, "OTT UPPER BAND Optimization Coeff", type=input.float, step=0.001, minval=0)
X5=input(0.077, "OTT LOWER BAND Optimization Coeff", type=input.float, step=0.001, minval=0)
showfibolines = input(title="Show OTT Bands Fibonacci Levels?", type=input.bool, defval=false)
showsupport = input(title="Show Support Line?", type=input.bool, defval=false)
highlight = input(title="Show OTT Support Line Color Changes?", type=input.bool, defval=false)
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
OTTC = highlight ? MAvg > MAvg[1] ? color.green : color.red : #0585E1
OTTMAIN=plot(nz(OTT[2]), color= #B800D9, linewidth=2, title="OTT", transp=0)
plot(showsupport ? MAvg : na, color=OTTC, linewidth=2, title="Support Line")
OTTUST=plot(nz(OTT[2])*(1+X3), color=#4500C4, linewidth=2, title="OTT UPPER", transp=0)
OTTUSTYARI=plot(showfibolines ? nz(OTT[2])*(1+X3*0.618) : nz(OTT[2])*(1+X3/2), color=#8400FF, linewidth=1, title="OTT UPPER HALF ", transp=0)
OTTFIBOUST2=plot(showfibolines ? nz(OTT[2])*(1+X3*0.382) : na, color=#C830FF, linewidth=1, title="OTT UPPER HALF ", transp=0)
OTTALT=plot(nz(OTT[2])*(1-X5), color=#00A6FF, linewidth=2, title="OTT LOWER", transp=0)
OTTALTYARI=plot(showfibolines ? nz(OTT[2])*(1-X5*0.618) : nz(OTT[2])*(1-X5/2), color=#007BBD, linewidth=1, title="OTT LOWER HALF", transp=0)
OTTALTFIBO2=plot(showfibolines ? nz(OTT[2])*(1-X5*0.382) : na, color=#024DE3, linewidth=1, title="OTT LOWER HALF", transp=0)
fill(OTTUST, OTTUSTYARI, color=#4500C4, transp=88)
fill(OTTFIBOUST2, OTTUSTYARI , color=#8400FF, transp=88)
fill(OTTUSTYARI, OTTMAIN, color=#C810FF, transp=88)
fill(OTTFIBOUST2, OTTMAIN, color=#C810FF, transp=88)
fill(OTTALT, OTTALTYARI, color=#00A6FF, transp=88)
fill(OTTALTFIBO2, OTTALTYARI , color=#007BBD, transp=88)
fill(OTTALTYARI, OTTMAIN, color=#024DE3, transp=88)
fill(OTTALTFIBO2, OTTMAIN, color=#024DE3, transp=88)
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!")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