Profit Maximizer in MQL5 — converted from Pine Script

The Profit Maximizer 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
12 informational
Source
213 lines of Pine v4

Inputs

InputDefault
Sourcehl2
ATR Length10
ATR Multiplier3.0
Moving Average Type"EMA"
Moving Average Length10
Change ATR Calculation Method ?true
Normalize ATR ?false
Show Moving Average?true
Show Crossing Signals?true
Show Price/Pmax Crossing Signals?false
Highlighter On/Off ?true

Plots

Moving Avg Line.

Converted MQL5

Save as profit-maximizer-pmax.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: d4cb140a171f   status: CLEAN
//| notes: 0 unsupported, 0 warn, 12 info
//| Review, compile and backtest before trading.
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| Profit Maximizer
//| 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 11
#property indicator_plots   9
#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  "Moving Avg Line"
#property indicator_type3   DRAW_LINE
#property indicator_color3  clrDodgerBlue
#property indicator_width3  2
#property indicator_label4  "PMax"
#property indicator_type4   DRAW_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

#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[];

//--- 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 atr2 = PINE_NA;
double atr = PINE_NA;
double valpha = PINE_NA;
double vud1 = PINE_NA;
double vdd1 = PINE_NA;
double vUD = PINE_NA;
double vDD = PINE_NA;
double vCMO = PINE_NA;
double VAR[];
double wwalpha = PINE_NA;
double WWMA[];
double zxLag = PINE_NA;
double zxEMAData = PINE_NA;
double ZLEMA = PINE_NA;
double lrc = PINE_NA;
double lrc1 = PINE_NA;
double lrs = PINE_NA;
double TSF = PINE_NA;
double MAvg = PINE_NA;
double longStop[];
double longStopPrev = PINE_NA;
double shortStop[];
double shortStopPrev = PINE_NA;
double dir[];
double PMax = PINE_NA;
bool buySignalk = false;
bool sellSignallk = false;
bool buySignalc = false;
bool sellSignallc = false;
double longFillColor = PINE_NA;
double shortFillColor = PINE_NA;

//--- live-tick rollback: the state each bar started from
double __sv_atr2;
double __si_atr2;
double __sv_atr;
double __si_atr;
double __sv_valpha;
double __si_valpha;
double __sv_vud1;
double __si_vud1;
double __sv_vdd1;
double __si_vdd1;
double __sv_vUD;
double __si_vUD;
double __sv_vDD;
double __si_vDD;
double __sv_vCMO;
double __si_vCMO;
double __sv_wwalpha;
double __si_wwalpha;
double __sv_zxLag;
double __si_zxLag;
double __sv_zxEMAData;
double __si_zxEMAData;
double __sv_ZLEMA;
double __si_ZLEMA;
double __sv_lrc;
double __si_lrc;
double __sv_lrc1;
double __si_lrc1;
double __sv_lrs;
double __si_lrs;
double __sv_TSF;
double __si_TSF;
double __sv_MAvg;
double __si_MAvg;
double __sv_longStopPrev;
double __si_longStopPrev;
double __sv_shortStopPrev;
double __si_shortStopPrev;
double __sv_PMax;
double __si_PMax;
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;
int __pineSnapBar = -1;
void __pineSaveState()
{
   __sv_atr2 = atr2;
   __sv_atr = atr;
   __sv_valpha = valpha;
   __sv_vud1 = vud1;
   __sv_vdd1 = vdd1;
   __sv_vUD = vUD;
   __sv_vDD = vDD;
   __sv_vCMO = vCMO;
   __sv_wwalpha = wwalpha;
   __sv_zxLag = zxLag;
   __sv_zxEMAData = zxEMAData;
   __sv_ZLEMA = ZLEMA;
   __sv_lrc = lrc;
   __sv_lrc1 = lrc1;
   __sv_lrs = lrs;
   __sv_TSF = TSF;
   __sv_MAvg = MAvg;
   __sv_longStopPrev = longStopPrev;
   __sv_shortStopPrev = shortStopPrev;
   __sv_PMax = PMax;
   __sv_buySignalk = buySignalk;
   __sv_sellSignallk = sellSignallk;
   __sv_buySignalc = buySignalc;
   __sv_sellSignallc = sellSignallc;
   __sv_longFillColor = longFillColor;
   __sv_shortFillColor = shortFillColor;
   pine_draw_mark();
   __pineSaveGen++;   // helpers' static arrays snapshot lazily
}
void __pineRestoreState()
{
   atr2 = __sv_atr2;
   atr = __sv_atr;
   valpha = __sv_valpha;
   vud1 = __sv_vud1;
   vdd1 = __sv_vdd1;
   vUD = __sv_vUD;
   vDD = __sv_vDD;
   vCMO = __sv_vCMO;
   wwalpha = __sv_wwalpha;
   zxLag = __sv_zxLag;
   zxEMAData = __sv_zxEMAData;
   ZLEMA = __sv_ZLEMA;
   lrc = __sv_lrc;
   lrc1 = __sv_lrc1;
   lrs = __sv_lrs;
   TSF = __sv_TSF;
   MAvg = __sv_MAvg;
   longStopPrev = __sv_longStopPrev;
   shortStopPrev = __sv_shortStopPrev;
   PMax = __sv_PMax;
   buySignalk = __sv_buySignalk;
   sellSignallk = __sv_sellSignallk;
   buySignalc = __sv_buySignalc;
   sellSignallc = __sv_sellSignallc;
   longFillColor = __sv_longFillColor;
   shortFillColor = __sv_shortFillColor;
   pine_draw_rollback();
   __pineRestoreGen++;
   __pineRestoreSaveGen = __pineSaveGen;
}
void __pineSaveInitial()
{
   __si_atr2 = atr2;
   __si_atr = atr;
   __si_valpha = valpha;
   __si_vud1 = vud1;
   __si_vdd1 = vdd1;
   __si_vUD = vUD;
   __si_vDD = vDD;
   __si_vCMO = vCMO;
   __si_wwalpha = wwalpha;
   __si_zxLag = zxLag;
   __si_zxEMAData = zxEMAData;
   __si_ZLEMA = ZLEMA;
   __si_lrc = lrc;
   __si_lrc1 = lrc1;
   __si_lrs = lrs;
   __si_TSF = TSF;
   __si_MAvg = MAvg;
   __si_longStopPrev = longStopPrev;
   __si_shortStopPrev = shortStopPrev;
   __si_PMax = PMax;
   __si_buySignalk = buySignalk;
   __si_sellSignallk = sellSignallk;
   __si_buySignalc = buySignalc;
   __si_sellSignallc = sellSignallc;
   __si_longFillColor = longFillColor;
   __si_shortFillColor = shortFillColor;
   pine_draw_save_initial();
}
void __pineRestoreInitial()
{
   atr2 = __si_atr2;
   atr = __si_atr;
   valpha = __si_valpha;
   vud1 = __si_vud1;
   vdd1 = __si_vdd1;
   vUD = __si_vUD;
   vDD = __si_vDD;
   vCMO = __si_vCMO;
   wwalpha = __si_wwalpha;
   zxLag = __si_zxLag;
   zxEMAData = __si_zxEMAData;
   ZLEMA = __si_ZLEMA;
   lrc = __si_lrc;
   lrc1 = __si_lrc1;
   lrs = __si_lrs;
   TSF = __si_TSF;
   MAvg = __si_MAvg;
   longStopPrev = __si_longStopPrev;
   shortStopPrev = __si_shortStopPrev;
   PMax = __si_PMax;
   buySignalk = __si_buySignalk;
   sellSignallk = __si_sellSignallk;
   buySignalc = __si_buySignalc;
   sellSignallc = __si_sellSignallc;
   longFillColor = __si_longFillColor;
   shortFillColor = __si_shortFillColor;
   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 __src[];   // Pine's `src` as a real series, filled per bar
double __ts0[];   // materialised ta.* source: pine_tr(high, low, close, __pineBar)
double __ti0[];   // materialised ta.* source: vud1
double __ti1[];   // materialised ta.* source: vdd1
double __ti2[];   // materialised ta.* source: zxEMAData
double __ti3[];   // materialised ta.* source: MAvg
double __ti4[];   // materialised ta.* source: PMax
double __sw0[];   // materialised ta.* source: getMA :: pine_sma(__src, __pineBar, (int)(MathCeil(pine_div(length, 2))))

//--- inputs
input ENUM_APPLIED_PRICE src = PRICE_MEDIAN;   // Source
input int Periods = 10;   // ATR Length
input double Multiplier = 3.0;   // ATR Multiplier
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_opt1;   // Moving Average Type
input int length = 10;   // Moving Average Length
input bool changeATR = true;   // Change ATR Calculation Method ?
input bool Normalize = false;   // Normalize ATR ?
input bool showsupport = true;   // Show Moving Average?
input bool showsignalsk = true;   // Show Crossing Signals?
input bool showsignalsc = false;   // Show Price/Pmax Crossing Signals?
input bool highlighting = true;   // Highlighter On/Off ?

//--- user functions
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, __st2, __stb2, __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)))), __sw0, __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("Moving Average Length", " must be at least 1"); return INIT_PARAMETERS_INCORRECT; }
   valpha = pine_div(2, (length + 1));
   wwalpha = pine_div(1, length);
   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_DATA);
   SetIndexBuffer(7, Buf7, INDICATOR_DATA);
   SetIndexBuffer(8, Buf8, INDICATOR_DATA);
   SetIndexBuffer(9, Buf9, INDICATOR_DATA);
   SetIndexBuffer(10, Buf10, 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);
   PlotIndexSetInteger(0, PLOT_SHOW_DATA, false);
   PlotIndexSetInteger(1, PLOT_SHOW_DATA, false);
   PlotIndexSetInteger(2, PLOT_LINE_COLOR, C'5,133,225');
   PlotIndexSetInteger(3, PLOT_LINE_COLOR, C'242,54,69');
   PlotIndexSetInteger(4, PLOT_LINE_COLOR, C'76,175,80');
   PlotIndexSetInteger(5, PLOT_LINE_COLOR, C'242,54,69');
   PlotIndexSetInteger(6, PLOT_LINE_COLOR, C'15,24,191');
   PlotIndexSetInteger(7, PLOT_LINE_COLOR, C'15,24,191');
   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(8, PLOT_SHOW_DATA, false);
   PlotIndexSetInteger(8, PLOT_DRAW_TYPE, DRAW_NONE);
   IndicatorSetString(INDICATOR_SHORTNAME, "Profit Maximizer");
   __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);
   __ts0[__pineBar] = pine_tr(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];
   }
   mav = mav_Labels[(int)mav_Input];
   atr2 = pine_sma(__ts0, __pineBar, (int)(Periods));
   atr = (changeATR ? pine_atr_hlc(high, low, close, __st0, __stb0, __pineBar, (int)(Periods)) : atr2);
   valpha = pine_div(2, (length + 1));
   vud1 = ((pine_source(src, open, high, low, close, __pineBar) > pine_hist(__src, __pineBar, (int)(1))) ? (pine_source(src, open, high, low, close, __pineBar) - pine_hist(__src, __pineBar, (int)(1))) : 0);
   vdd1 = ((pine_source(src, open, high, low, close, __pineBar) < pine_hist(__src, __pineBar, (int)(1))) ? (pine_hist(__src, __pineBar, (int)(1)) - pine_source(src, open, high, low, close, __pineBar)) : 0);
   __ti0[__pineBar] = vud1;
   vUD = pine_sum(__ti0, __pineBar, (int)(9));
   __ti1[__pineBar] = vdd1;
   vDD = pine_sum(__ti1, __pineBar, (int)(9));
   vCMO = pine_nz(pine_div((vUD - vDD), (vUD + vDD)));
   VAR[__pineBar] = 0.0;
   VAR[__pineBar] = (pine_nz(((valpha * MathAbs(vCMO)) * pine_source(src, open, high, low, close, __pineBar))) + ((1 - (valpha * MathAbs(vCMO))) * pine_nz(pine_hist(VAR, __pineBar, (int)(1)))));
   wwalpha = pine_div(1, length);
   WWMA[__pineBar] = 0.0;
   WWMA[__pineBar] = ((wwalpha * pine_source(src, open, high, low, close, __pineBar)) + ((1 - wwalpha) * pine_nz(pine_hist(WWMA, __pineBar, (int)(1)))));
   zxLag = ((pine_div(length, 2) == MathRound(pine_div(length, 2))) ? pine_div(length, 2) : pine_div((length - 1), 2));
   zxEMAData = (pine_source(src, open, high, low, close, __pineBar) + (pine_source(src, open, high, low, close, __pineBar) - pine_hist(__src, __pineBar, (int)(zxLag))));
   __ti2[__pineBar] = zxEMAData;
   ZLEMA = pine_ema(__ti2, __st1, __pineBar, (int)(length));
   lrc = pine_linreg(__src, __pineBar, (int)(length), (int)(0));
   lrc1 = pine_linreg(__src, __pineBar, (int)(length), (int)(1));
   lrs = (lrc - lrc1);
   TSF = (pine_linreg(__src, __pineBar, (int)(length), (int)(0)) + lrs);
   MAvg = getMA(pine_source(src, open, high, low, close, __pineBar), length, __pineBar);
   longStop[__pineBar] = (Normalize ? (MAvg - pine_div((Multiplier * atr), close[__pineBar])) : (MAvg - (Multiplier * atr)));
   longStopPrev = pine_nz(pine_hist(longStop, __pineBar, (int)(1)), longStop[__pineBar]);
   longStop[__pineBar] = ((MAvg > longStopPrev) ? MathMax(longStop[__pineBar], longStopPrev) : longStop[__pineBar]);
   shortStop[__pineBar] = (Normalize ? (MAvg + pine_div((Multiplier * atr), close[__pineBar])) : (MAvg + (Multiplier * atr)));
   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]));
   PMax = ((dir[__pineBar] == 1) ? longStop[__pineBar] : shortStop[__pineBar]);
   __ti3[__pineBar] = MAvg;
   __ti4[__pineBar] = PMax;
   pine_alertcond(pine_cross(__ti3, __ti4, __pineBar), "PMax - Moving Avg Crossing!", 1246421448, __pineBar, __pineLastBar);
   __ti3[__pineBar] = MAvg;
   __ti4[__pineBar] = PMax;
   pine_alertcond(pine_crossover(__ti3, __ti4, __pineBar), "Moving Avg BUY SIGNAL!", 357409259, __pineBar, __pineLastBar);
   __ti3[__pineBar] = MAvg;
   __ti4[__pineBar] = PMax;
   pine_alertcond(pine_crossunder(__ti3, __ti4, __pineBar), "Moving Avg SELL SIGNAL!", 1230772593, __pineBar, __pineLastBar);
   __ti4[__pineBar] = PMax;
   pine_alertcond(pine_cross(__src, __ti4, __pineBar), "PMax - Price Crossing!", 1523961871, __pineBar, __pineLastBar);
   __ti4[__pineBar] = PMax;
   pine_alertcond(pine_crossover(__src, __ti4, __pineBar), "PRICE OVER PMax - BUY SIGNAL!", 1581683019, __pineBar, __pineLastBar);
   __ti4[__pineBar] = PMax;
   pine_alertcond(pine_crossunder(__src, __ti4, __pineBar), "PRICE UNDER PMax - SELL SIGNAL!", 784673783, __pineBar, __pineLastBar);
   __ti3[__pineBar] = MAvg;
   __ti4[__pineBar] = PMax;
   buySignalk = pine_crossover(__ti3, __ti4, __pineBar);
   __ti3[__pineBar] = MAvg;
   __ti4[__pineBar] = PMax;
   sellSignallk = pine_crossunder(__ti3, __ti4, __pineBar);
   __ti4[__pineBar] = PMax;
   buySignalc = pine_crossover(__src, __ti4, __pineBar);
   __ti4[__pineBar] = PMax;
   sellSignallc = pine_crossunder(__src, __ti4, __pineBar);
   longFillColor = (highlighting ? ((MAvg > PMax) ? C'76,175,80' : PINE_NA) : PINE_NA);
   shortFillColor = (highlighting ? ((MAvg < PMax) ? C'242,54,69' : PINE_NA) : PINE_NA);
   Buf4[__pineBar] = (showsupport ? MAvg : PINE_NA);
   Buf5[__pineBar] = PMax;
   Buf6[__pineBar] = pine_truthy(((((buySignalk ? 1 : 0) & (showsignalsk ? 1 : 0)) != 0) ? (PMax * 0.995) : PINE_NA)) ? ((((buySignalk ? 1 : 0) & (showsignalsk ? 1 : 0)) != 0) ? (PMax * 0.995) : PINE_NA) : EMPTY_VALUE;
   pine_shape_text("6", __pineBar, Buf6[__pineBar], "Buy", C'76,175,80', true);
   Buf7[__pineBar] = pine_truthy(((((sellSignallk ? 1 : 0) & (showsignalsk ? 1 : 0)) != 0) ? (PMax * 1.005) : PINE_NA)) ? ((((sellSignallk ? 1 : 0) & (showsignalsk ? 1 : 0)) != 0) ? (PMax * 1.005) : PINE_NA) : EMPTY_VALUE;
   pine_shape_text("7", __pineBar, Buf7[__pineBar], "Sell", C'242,54,69', true);
   Buf8[__pineBar] = pine_truthy(((((buySignalc ? 1 : 0) & (showsignalsc ? 1 : 0)) != 0) ? (PMax * 0.995) : PINE_NA)) ? ((((buySignalc ? 1 : 0) & (showsignalsc ? 1 : 0)) != 0) ? (PMax * 0.995) : PINE_NA) : EMPTY_VALUE;
   pine_shape_text("8", __pineBar, Buf8[__pineBar], "Buy", C'15,24,191', true);
   Buf9[__pineBar] = pine_truthy(((((sellSignallc ? 1 : 0) & (showsignalsc ? 1 : 0)) != 0) ? (PMax * 1.005) : PINE_NA)) ? ((((sellSignallc ? 1 : 0) & (showsignalsc ? 1 : 0)) != 0) ? (PMax * 1.005) : PINE_NA) : EMPTY_VALUE;
   pine_shape_text("9", __pineBar, Buf9[__pineBar], "Sell", C'15,24,191', true);
   Buf10[__pineBar] = ((open[__pineBar] + high[__pineBar] + low[__pineBar] + close[__pineBar]) / 4.0);
   PineFill0[__pineBar] = Buf10[__pineBar];
   PineFill1[__pineBar] = Buf5[__pineBar];
   PineFill2[__pineBar] = Buf10[__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(__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(__src, rates_total);
   ArrayResize(__ts0, rates_total);
   ArrayResize(__ti0, rates_total);
   ArrayResize(__ti1, rates_total);
   ArrayResize(__ti2, rates_total);
   ArrayResize(__ti3, rates_total);
   ArrayResize(__ti4, rates_total);
   ArrayResize(__sw0, 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(__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(__src, PINE_NA);
      ArrayInitialize(__ts0, PINE_NA);
      ArrayInitialize(__ti0, PINE_NA);
      ArrayInitialize(__ti1, PINE_NA);
      ArrayInitialize(__ti2, PINE_NA);
      ArrayInitialize(__ti3, PINE_NA);
      ArrayInitialize(__ti4, PINE_NA);
      ArrayInitialize(__sw0, 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

//developer: @KivancOzbilgic

//author: @KivancOzbilgic



study("Profit Maximizer","PMax", overlay=true, format=format.price, precision=2, resolution="")

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

Periods = input(title="ATR Length", type=input.integer, defval=10)

Multiplier = input(title="ATR Multiplier", type=input.float, step=0.1, defval=3.0)

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

length =input(10, "Moving Average Length", minval=1)

changeATR= input(title="Change ATR Calculation Method ?", type=input.bool, defval=true)

Normalize= input(title="Normalize ATR ?", type=input.bool, defval=false)

showsupport = input(title="Show Moving Average?", type=input.bool, defval=true)

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

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

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

atr2 = sma(tr, Periods)

atr= changeATR ? atr(Periods) : atr2

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])

wwalpha = 1/ length

WWMA = 0.0

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

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

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

ZLEMA = ema(zxEMAData, length)

lrc = linreg(src, length, 0)

lrc1 = linreg(src,length,1)

lrs = (lrc-lrc1)

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

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)

longStop = Normalize ? MAvg - Multiplier*atr/close : MAvg - Multiplier*atr

longStopPrev = nz(longStop[1], longStop)

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

shortStop = Normalize ? MAvg + Multiplier*atr/close : MAvg + Multiplier*atr

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

PMax = dir==1 ? longStop: shortStop

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

pALL=plot(PMax, color=color.red, linewidth=2, title="PMax", transp=0)

alertcondition(cross(MAvg, PMax), title="Cross Alert", message="PMax - Moving Avg Crossing!")

alertcondition(crossover(MAvg, PMax), title="Crossover Alarm", message="Moving Avg BUY SIGNAL!")

alertcondition(crossunder(MAvg, PMax), title="Crossunder Alarm", message="Moving Avg SELL SIGNAL!")

alertcondition(cross(src, PMax), title="Price Cross Alert", message="PMax - Price Crossing!")

alertcondition(crossover(src, PMax), title="Price Crossover Alarm", message="PRICE OVER PMax - BUY SIGNAL!")

alertcondition(crossunder(src, PMax), title="Price Crossunder Alarm", message="PRICE UNDER PMax - SELL SIGNAL!")

buySignalk = crossover(MAvg, PMax)

plotshape(buySignalk and showsignalsk ? PMax*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, PMax)

plotshape(sellSignallk and showsignalsk ? PMax*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, PMax)

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

sellSignallc = crossunder(src, PMax)

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

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

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

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

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

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

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