Consolidation Zones - Live in MQL5 — converted from Pine Script

The Consolidation Zones - Live indicator by LonesomeTheBlue, 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
3 informational
Source
159 lines of Pine v4

Inputs

InputDefault
Loopback Period10
Min Consolidation Length5
Paint Consolidation Area true
Zone Colorcolor.new(color.blue

Converted MQL5

Save as consolidation-zones-live.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: a3fa0f337e49   status: CLEAN
//| notes: 0 unsupported, 0 warn, 3 info
//| Review, compile and backtest before trading.
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| Consolidation Zones - Live
//| 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 0
#property indicator_plots   0

#include "PineRuntime.mqh"

//--- plot buffers
// no plot() in source: nothing to bind (drawings only)

//--- 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 hb_ = PINE_NA;
double lb_ = PINE_NA;
double dir[];
double zz[];
double pp = PINE_NA;
int conscnt = 0;
double condhigh = PINE_NA;
double condlow = PINE_NA;
double H_ = PINE_NA;
double L_ = PINE_NA;
double upline = PINE_NA;
double dnline = PINE_NA;
bool breakoutup = false;
bool breakoutdown = false;

//--- live-tick rollback: the state each bar started from
double __sv_hb_;
double __si_hb_;
double __sv_lb_;
double __si_lb_;
double __sv_pp;
double __si_pp;
int __sv_conscnt;
int __si_conscnt;
double __sv_condhigh;
double __si_condhigh;
double __sv_condlow;
double __si_condlow;
double __sv_H_;
double __si_H_;
double __sv_L_;
double __si_L_;
double __sv_upline;
double __si_upline;
double __sv_dnline;
double __si_dnline;
bool __sv_breakoutup;
bool __si_breakoutup;
bool __sv_breakoutdown;
bool __si_breakoutdown;
int __pineSnapBar = -1;
void __pineSaveState()
{
   __sv_hb_ = hb_;
   __sv_lb_ = lb_;
   __sv_pp = pp;
   __sv_conscnt = conscnt;
   __sv_condhigh = condhigh;
   __sv_condlow = condlow;
   __sv_H_ = H_;
   __sv_L_ = L_;
   __sv_upline = upline;
   __sv_dnline = dnline;
   __sv_breakoutup = breakoutup;
   __sv_breakoutdown = breakoutdown;
   pine_draw_mark();
   __pineSaveGen++;   // helpers' static arrays snapshot lazily
}
void __pineRestoreState()
{
   hb_ = __sv_hb_;
   lb_ = __sv_lb_;
   pp = __sv_pp;
   conscnt = __sv_conscnt;
   condhigh = __sv_condhigh;
   condlow = __sv_condlow;
   H_ = __sv_H_;
   L_ = __sv_L_;
   upline = __sv_upline;
   dnline = __sv_dnline;
   breakoutup = __sv_breakoutup;
   breakoutdown = __sv_breakoutdown;
   pine_draw_rollback();
   __pineRestoreGen++;
   __pineRestoreSaveGen = __pineSaveGen;
}
void __pineSaveInitial()
{
   __si_hb_ = hb_;
   __si_lb_ = lb_;
   __si_pp = pp;
   __si_conscnt = conscnt;
   __si_condhigh = condhigh;
   __si_condlow = condlow;
   __si_H_ = H_;
   __si_L_ = L_;
   __si_upline = upline;
   __si_dnline = dnline;
   __si_breakoutup = breakoutup;
   __si_breakoutdown = breakoutdown;
   pine_draw_save_initial();
}
void __pineRestoreInitial()
{
   hb_ = __si_hb_;
   lb_ = __si_lb_;
   pp = __si_pp;
   conscnt = __si_conscnt;
   condhigh = __si_condhigh;
   condlow = __si_condlow;
   H_ = __si_H_;
   L_ = __si_L_;
   upline = __si_upline;
   dnline = __si_dnline;
   breakoutup = __si_breakoutup;
   breakoutdown = __si_breakoutdown;
   pine_draw_reset();
   __pineInitGen++;
}

double __ti0[];   // materialised ta.* source: pp

//--- inputs
input int prd = 10;   // Loopback Period
input int conslen = 5;   // Min Consolidation Length
input bool paintcons = true;   // Paint Consolidation Area 
input color zonecol = C'41,98,255';   // Zone Color

int OnInit()
{
   if(prd < 2) { Print("Loopback Period", " must be at least 2"); return INIT_PARAMETERS_INCORRECT; }
   if(prd > 50) { Print("Loopback Period", " must be at most 50"); return INIT_PARAMETERS_INCORRECT; }
   if(conslen < 2) { Print("Min Consolidation Length", " must be at least 2"); return INIT_PARAMETERS_INCORRECT; }
   if(conslen > 20) { Print("Min Consolidation Length", " must be at most 20"); return INIT_PARAMETERS_INCORRECT; }
   IndicatorSetString(INDICATOR_SHORTNAME, "Consolidation Zones - Live");
   __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[])
{
   if(__pineBar == 0)
   {
      dir[__pineBar] = 0;
   }
   if(__pineBar > 0)
   {
      dir[__pineBar] = dir[__pineBar - 1];
      zz[__pineBar] = zz[__pineBar - 1];
   }
   hb_ = ((pine_highestbars(high, __pineBar, (int)(prd)) == 0) ? high[__pineBar] : PINE_NA);
   lb_ = ((pine_lowestbars(low, __pineBar, (int)(prd)) == 0) ? low[__pineBar] : PINE_NA);
   zz[__pineBar] = PINE_NA;
   pp = PINE_NA;
   dir[__pineBar] = (((((pine_truthy(hb_) ? 1 : 0) & (pine_na(lb_) ? 1 : 0)) != 0)) ? (1) : ((((((pine_truthy(lb_) ? 1 : 0) & (pine_na(hb_) ? 1 : 0)) != 0)) ? ((-1)) : (dir[__pineBar]))));
   if((((pine_truthy(hb_) ? 1 : 0) & (pine_truthy(lb_) ? 1 : 0)) != 0))
   {
      if((dir[__pineBar] == 1))
      {
         zz[__pineBar] = hb_;
      }
      else
      {
         zz[__pineBar] = lb_;
      }
   }
   else
   {
      zz[__pineBar] = ((hb_) ? (hb_) : (((lb_) ? (lb_) : (PINE_NA))));
   }
   for(int x = (int)(0), __pineForStep0 = (x <= (int)(1000) ? (int)(1) : -(int)(1)); (__pineForStep0 > 0 ? x <= (int)(1000) : x >= (int)(1000)); x += __pineForStep0)
   {
      if((((pine_na(close[__pineBar]) ? 1 : 0) | ((dir[__pineBar] != pine_hist(dir, __pineBar, (int)(x))) ? 1 : 0)) != 0))
      {
         break;
      }
      if(pine_truthy(pine_hist(zz, __pineBar, (int)(x))))
      {
         if(pine_na(pp))
         {
            pp = pine_hist(zz, __pineBar, (int)(x));
         }
         else
         {
            if(((((pine_hist(dir, __pineBar, (int)(x)) == 1) ? 1 : 0) & ((pine_hist(zz, __pineBar, (int)(x)) > pp) ? 1 : 0)) != 0))
            {
               pp = pine_hist(zz, __pineBar, (int)(x));
            }
            if(((((pine_hist(dir, __pineBar, (int)(x)) == (-1)) ? 1 : 0) & ((pine_hist(zz, __pineBar, (int)(x)) < pp) ? 1 : 0)) != 0))
            {
               pp = pine_hist(zz, __pineBar, (int)(x));
            }
         }
      }
   }
   H_ = pine_highest(high, __pineBar, (int)(conslen));
   L_ = pine_lowest(low, __pineBar, (int)(conslen));
   breakoutup = false;
   breakoutdown = false;
   __ti0[__pineBar] = pp;
   if(pine_truthy(pine_change(__ti0, __pineBar, (int)(1))))
   {
      if((conscnt > conslen))
      {
         if((pp > condhigh))
         {
            breakoutup = true;
         }
         if((pp < condlow))
         {
            breakoutdown = true;
         }
      }
      if((((((((conscnt > 0) ? 1 : 0) & ((pp <= condhigh) ? 1 : 0)) != 0) ? 1 : 0) & ((pp >= condlow) ? 1 : 0)) != 0))
      {
         conscnt = (conscnt + 1);
      }
      else
      {
         conscnt = 0;
      }
   }
   else
   {
      conscnt = (conscnt + 1);
   }
   if((conscnt >= conslen))
   {
      if((conscnt == conslen))
      {
         condhigh = H_;
         condlow = L_;
      }
      else
      {
         pine_line_delete(upline);
         pine_line_delete(dnline);
         condhigh = MathMax(condhigh, high[__pineBar]);
         condlow = MathMin(condlow, low[__pineBar]);
      }
      upline = pine_line_style(pine_line_new(__pineBar, condhigh, (__pineBar - conscnt), condhigh), (color)(C'242,54,69'), (int)(STYLE_DASH), -1, -1);
      dnline = pine_line_style(pine_line_new(__pineBar, condlow, (__pineBar - conscnt), condlow), (color)(C'0,230,118'), (int)(STYLE_DASH), -1, -1);
   }
   0 /* fill dropped: visual only */;
   pine_alertcond(breakoutup, "Breakout Up", 1639269326, __pineBar, __pineLastBar);
   pine_alertcond(breakoutdown, "Breakout Down", 1483559872, __pineBar, __pineLastBar);
}

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(dir, rates_total);
   ArrayResize(zz, rates_total);
   ArrayResize(__ti0, rates_total);
   if(prev_calculated == 0)
   {
      ArrayInitialize(dir, PINE_NA);
      ArrayInitialize(zz, PINE_NA);
      ArrayInitialize(__ti0, PINE_NA);
   }

   __pineLastBar = rates_total - 1;
   const int start = (prev_calculated > 1) ? prev_calculated - 1 : 0;
   static bool __pineRealVolume = false;
   if(start == 0)
   {
      __pineRestoreInitial();
      __pineSnapBar = -1;
      __pineRealVolume = rates_total > 0 && volume[rates_total - 1] > 0;
   }
   __pineRealtime = prev_calculated > 0;
   for(int __pineBar = start; __pineBar < rates_total; __pineBar++)
   {
      if(__pineBar == __pineSnapBar)
         __pineRestoreState();   // a re-run of this bar: back to its start
      else if(__pineBar == __pineLastBar)
      {
         __pineSaveState();      // the forming bar: remember the state it starts from
         __pineSnapBar = __pineBar;
      }
      if(__pineRealVolume) PineBar(__pineBar, open, high, low, close, volume, time);
      else PineBar(__pineBar, open, high, low, close, tick_volume, time);
   }

   return rates_total;
}

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

Original Pine Script

© LonesomeTheBlue — Mozilla Public License 2.0. Reproduced for attribution and comparison; the converted code above is a derivative work offered under the same licence.

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

// © LonesomeTheBlue



//@version=4

study("Consolidation Zones - Live", overlay = true, max_bars_back = 1100)

prd = input(defval = 10, title="Loopback Period", minval = 2, maxval = 50)

conslen = input(defval = 5, title="Min Consolidation Length", minval = 2, maxval = 20)

paintcons = input(defval = true, title = "Paint Consolidation Area ")

zonecol = input(defval = color.new(color.blue, 70), title = "Zone Color")



float hb_ = highestbars(prd) == 0 ? high : na

float lb_ = lowestbars(prd) == 0 ? low : na

var int dir = 0

float zz = na

float pp = na



dir := iff(hb_ and na(lb_), 1, iff(lb_ and na(hb_), -1, dir))

if hb_ and lb_

    if dir == 1

        zz := hb_

    else

        zz := lb_

else

    zz := iff(hb_, hb_, iff(lb_, lb_, na))



for x = 0 to 1000

    if na(close) or dir != dir[x]

        break

    if zz[x]

        if na(pp)

            pp := zz[x]

        else

            if dir[x] == 1 and zz[x] > pp

                pp := zz[x]

            if dir[x] == -1 and zz[x] < pp

                pp := zz[x]



var int conscnt = 0

var float condhigh = na

var float condlow = na

float H_ = highest(conslen)

float L_ = lowest(conslen)

var line upline = na

var line dnline = na

bool breakoutup = false

bool breakoutdown = false



if change(pp)

    if conscnt > conslen

        if pp > condhigh

            breakoutup := true

        if pp < condlow

            breakoutdown := true

    if conscnt > 0 and pp <= condhigh and pp >= condlow

        conscnt := conscnt + 1

    else

        conscnt := 0

else

    conscnt := conscnt + 1



if conscnt >= conslen

    if conscnt == conslen

        condhigh := H_

        condlow  := L_

    else

        line.delete(upline)

        line.delete(dnline)

        condhigh := max(condhigh, high)

        condlow := min(condlow, low)

    

    upline := line.new(bar_index, condhigh, bar_index - conscnt, condhigh, color = color.red, style = line.style_dashed)

    dnline := line.new(bar_index, condlow , bar_index - conscnt, condlow, color = color.lime, style = line.style_dashed)



fill(plot(condhigh, color = na, style = plot.style_stepline),

   plot(condlow, color = na, style = plot.style_stepline),

   color = paintcons and conscnt > conslen ? zonecol : color.new(color.white, 100))



alertcondition(breakoutup, title='Breakout Up', message='Breakout Up')

alertcondition(breakoutdown, title='Breakout Down', message='Breakout Down')

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