Volume-Supported Linear Regression Trend in MQL5 — converted from Pine Script

The Volume-Supported Linear Regression Trend 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, separate window
Verdict
EQUIVALENT on 500 bars
Engine
pinec 0.6.46
Conversion notes
15 informational
Source
133 lines of Pine v4

Inputs

InputDefault
Sourceclose
Short Term Length20
Long Term Length50
Column Colors#00FF00ff
ccol12#00BC00
ccol13#00FF006f
ccol21#FF0000ff
ccol22#BF0000
ccol23#FF00006f
Line Colors#008EFF
col12#006ec5
col13#024478

Converted MQL5

Save as volume-supported-linear-regression-trend.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: 1c74ebe9e72b   status: CLEAN
//| notes: 0 unsupported, 0 warn, 15 info
//| Review, compile and backtest before trading.
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| Volume-Supported Linear Regression Trend
//| 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 2
#property indicator_plots   2
#property indicator_label1  "Plot1"
#property indicator_type1   DRAW_HISTOGRAM
#property indicator_color1  clrDodgerBlue
#property indicator_width1  1
#property indicator_label2  "Plot2"
#property indicator_type2   DRAW_LINE
#property indicator_color2  clrDodgerBlue
#property indicator_width2  3

#include "PineRuntime.mqh"

//--- plot buffers
double Buf0[];
double Buf1[];

//--- 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 slope_price = PINE_NA;
double slope_price_lt = PINE_NA;
double tw = PINE_NA;
double bw = PINE_NA;
double body = PINE_NA;
double slope_volume_up = PINE_NA;
double slope_volume_down = PINE_NA;
double column_col = PINE_NA;
double slope_volume_up_lt = PINE_NA;
double slope_volume_down_lt = PINE_NA;

//--- live-tick rollback: the state each bar started from
double __sv_slope_price;
double __si_slope_price;
double __sv_slope_price_lt;
double __si_slope_price_lt;
double __sv_tw;
double __si_tw;
double __sv_bw;
double __si_bw;
double __sv_body;
double __si_body;
double __sv_slope_volume_up;
double __si_slope_volume_up;
double __sv_slope_volume_down;
double __si_slope_volume_down;
double __sv_column_col;
double __si_column_col;
double __sv_slope_volume_up_lt;
double __si_slope_volume_up_lt;
double __sv_slope_volume_down_lt;
double __si_slope_volume_down_lt;
int __pineSnapBar = -1;
void __pineSaveState()
{
   __sv_slope_price = slope_price;
   __sv_slope_price_lt = slope_price_lt;
   __sv_tw = tw;
   __sv_bw = bw;
   __sv_body = body;
   __sv_slope_volume_up = slope_volume_up;
   __sv_slope_volume_down = slope_volume_down;
   __sv_column_col = column_col;
   __sv_slope_volume_up_lt = slope_volume_up_lt;
   __sv_slope_volume_down_lt = slope_volume_down_lt;
   pine_draw_mark();
   __pineSaveGen++;   // helpers' static arrays snapshot lazily
}
void __pineRestoreState()
{
   slope_price = __sv_slope_price;
   slope_price_lt = __sv_slope_price_lt;
   tw = __sv_tw;
   bw = __sv_bw;
   body = __sv_body;
   slope_volume_up = __sv_slope_volume_up;
   slope_volume_down = __sv_slope_volume_down;
   column_col = __sv_column_col;
   slope_volume_up_lt = __sv_slope_volume_up_lt;
   slope_volume_down_lt = __sv_slope_volume_down_lt;
   pine_draw_rollback();
   __pineRestoreGen++;
   __pineRestoreSaveGen = __pineSaveGen;
}
void __pineSaveInitial()
{
   __si_slope_price = slope_price;
   __si_slope_price_lt = slope_price_lt;
   __si_tw = tw;
   __si_bw = bw;
   __si_body = body;
   __si_slope_volume_up = slope_volume_up;
   __si_slope_volume_down = slope_volume_down;
   __si_column_col = column_col;
   __si_slope_volume_up_lt = slope_volume_up_lt;
   __si_slope_volume_down_lt = slope_volume_down_lt;
   pine_draw_save_initial();
}
void __pineRestoreInitial()
{
   slope_price = __si_slope_price;
   slope_price_lt = __si_slope_price_lt;
   tw = __si_tw;
   bw = __si_bw;
   body = __si_body;
   slope_volume_up = __si_slope_volume_up;
   slope_volume_down = __si_slope_volume_down;
   column_col = __si_column_col;
   slope_volume_up_lt = __si_slope_volume_up_lt;
   slope_volume_down_lt = __si_slope_volume_down_lt;
   pine_draw_reset();
   __pineInitGen++;
}

double __src[];   // Pine's `src` as a real series, filled per bar
double __sw0[];   // materialised ta.* source: _get_trend :: deltaup
double __sw1[];   // materialised ta.* source: _get_trend :: deltadown
double __sw2[];   // materialised ta.* source: _get_trend__site1 :: deltaup
double __sw3[];   // materialised ta.* source: _get_trend__site1 :: deltadown

//--- inputs
input ENUM_APPLIED_PRICE src = PRICE_CLOSE;   // Source
input int len1 = 20;   // Short Term Length
input int len2 = 50;   // Long Term Length
input color ccol11 = C'0,255,0';   // Column Colors
input color ccol12 = C'0,188,0';
input color ccol13 = C'0,255,0';
input color ccol21 = C'255,0,0';
input color ccol22 = C'191,0,0';
input color ccol23 = C'255,0,0';
input color col11 = C'0,142,255';   // Line Colors
input color col12 = C'0,110,197';
input color col13 = C'2,68,120';
input color col21 = C'253,151,1';
input color col22 = C'206,122,0';
input color col23 = C'102,61,0';

//--- user functions
double _rate(const bool cond, const int __pineBar)
{
   double ret = pine_div((0.5 * ((tw + bw) + (cond ? (2 * body) : 0))), ((tw + bw) + body));
   ret = (pine_na(ret) ? 0.5 : ret);
   return ret;
   return PINE_NA;
}

void _get_trend(const int len, const int __pineBar,
      const double &open[],  const double &high[], const double &low[],
      const double &close[], const long &volume[], const datetime &time[], double &__out0, double &__out1)
{
   double deltaup = ((double)volume[__pineBar] * _rate((open[__pineBar] <= close[__pineBar]), __pineBar));
   double deltadown = ((double)volume[__pineBar] * _rate((open[__pineBar] > close[__pineBar]), __pineBar));
   double slope_volume_up_l = (pine_linreg_step(deltaup, __sw0, __pineBar, (int)(len), (int)(0)) - pine_linreg_step(deltaup, __sw0, __pineBar, (int)(len), (int)(1)));
   double slope_volume_down_l = (pine_linreg_step(deltadown, __sw1, __pineBar, (int)(len), (int)(0)) - pine_linreg_step(deltadown, __sw1, __pineBar, (int)(len), (int)(1)));
   __out0 = slope_volume_up_l;
   __out1 = slope_volume_down_l;
   return;
}

void _get_trend__site1(const int len, const int __pineBar,
      const double &open[],  const double &high[], const double &low[],
      const double &close[], const long &volume[], const datetime &time[], double &__out0, double &__out1)
{
   double deltaup = ((double)volume[__pineBar] * _rate((open[__pineBar] <= close[__pineBar]), __pineBar));
   double deltadown = ((double)volume[__pineBar] * _rate((open[__pineBar] > close[__pineBar]), __pineBar));
   double slope_volume_up_l = (pine_linreg_step(deltaup, __sw2, __pineBar, (int)(len), (int)(0)) - pine_linreg_step(deltaup, __sw2, __pineBar, (int)(len), (int)(1)));
   double slope_volume_down_l = (pine_linreg_step(deltadown, __sw3, __pineBar, (int)(len), (int)(0)) - pine_linreg_step(deltadown, __sw3, __pineBar, (int)(len), (int)(1)));
   __out0 = slope_volume_up_l;
   __out1 = slope_volume_down_l;
   return;
}

int OnInit()
{
   if(len1 < 5) { Print("Short Term Length", " must be at least 5"); return INIT_PARAMETERS_INCORRECT; }
   if(len2 < 5) { Print("Long Term Length", " must be at least 5"); return INIT_PARAMETERS_INCORRECT; }
   SetIndexBuffer(0, Buf0, INDICATOR_DATA);
   SetIndexBuffer(1, Buf1, INDICATOR_DATA);
   PlotIndexSetDouble(0, PLOT_EMPTY_VALUE, EMPTY_VALUE);
   PlotIndexSetDouble(1, PLOT_EMPTY_VALUE, EMPTY_VALUE);
   IndicatorSetString(INDICATOR_SHORTNAME, "Volume-Supported Linear Regression Trend");
   __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);
   slope_price = (pine_linreg(__src, __pineBar, (int)(len1), (int)(0)) - pine_linreg(__src, __pineBar, (int)(len1), (int)(1)));
   slope_price_lt = (pine_linreg(__src, __pineBar, (int)(len2), (int)(0)) - pine_linreg(__src, __pineBar, (int)(len2), (int)(1)));
   tw = (high[__pineBar] - MathMax(open[__pineBar], close[__pineBar]));
   bw = (MathMin(open[__pineBar], close[__pineBar]) - low[__pineBar]);
   body = MathAbs((close[__pineBar] - open[__pineBar]));
   double __tc0_0 = PINE_NA;
   double __tc0_1 = PINE_NA;
   _get_trend(len1, __pineBar, open, high, low, close, volume, time, __tc0_0, __tc0_1);
   slope_volume_up = __tc0_0;
   slope_volume_down = __tc0_1;
   column_col = ((slope_price > 0) ? ((slope_volume_up > 0) ? ((slope_volume_up > slope_volume_down) ? ccol11 : ccol12) : ccol13) : ((slope_price < 0) ? ((slope_volume_down > 0) ? ((slope_volume_up < slope_volume_down) ? ccol21 : ccol22) : ccol23) : C'120,123,134'));
   double __tc1_0 = PINE_NA;
   double __tc1_1 = PINE_NA;
   _get_trend__site1(len2, __pineBar, open, high, low, close, volume, time, __tc1_0, __tc1_1);
   slope_volume_up_lt = __tc1_0;
   slope_volume_down_lt = __tc1_1;
   column_col = ((slope_price_lt > 0) ? ((slope_volume_up_lt > 0) ? ((slope_volume_up_lt > slope_volume_down_lt) ? col11 : col12) : col13) : ((slope_price_lt < 0) ? ((slope_volume_down_lt > 0) ? ((slope_volume_up_lt < slope_volume_down_lt) ? col21 : col22) : col23) : C'120,123,134'));
   Buf0[__pineBar] = (slope_price * len1);
   Buf1[__pineBar] = (slope_price_lt * len2);
}

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(__src, rates_total);
   ArrayResize(__sw0, rates_total);
   ArrayResize(__sw1, rates_total);
   ArrayResize(__sw2, rates_total);
   ArrayResize(__sw3, rates_total);
   if(prev_calculated == 0)
   {
      ArrayInitialize(__src, PINE_NA);
      ArrayInitialize(__sw0, PINE_NA);
      ArrayInitialize(__sw1, PINE_NA);
      ArrayInitialize(__sw2, PINE_NA);
      ArrayInitialize(__sw3, 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

© 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("Volume-Supported Linear Regression Trend", "VSLRT", overlay = false)

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

len1 = input(defval = 20, title = "Short Term Length", minval = 5)

len2 = input(defval = 50, title = "Long Term Length", minval = 5)

ccol11 = input(defval = #00FF00ff, title = "Column Colors", inline = "gr1")

ccol12 = input(defval = #00BC00, title = "", inline = "gr1")

ccol13 = input(defval = #00FF006f, title = "", inline = "gr1")

ccol21 = input(defval = #FF0000ff, title = "", inline = "gr1")

ccol22 = input(defval = #BF0000, title = "", inline = "gr1")

ccol23 = input(defval = #FF00006f, title = "", inline = "gr1")

col11 = input(defval = #008EFF, title = "Line Colors", inline = "gr2")

col12 = input(defval = #006ec5, title = "", inline = "gr2")

col13 = input(defval = #024478, title = "", inline = "gr2")

col21 = input(defval = #fd9701, title = "", inline = "gr2")

col22 = input(defval = #CE7A00, title = "", inline = "gr2")

col23 = input(defval = #663d00, title = "", inline = "gr2")



// get short/long-term regression slope

slope_price = linreg(src, len1, 0) - linreg(src, len1, 1)

slope_price_lt = linreg(src, len2, 0) - linreg(src, len2, 1)



// get the size of top/bottom/body of the candle

tw = high - max(open, close) 

bw = min(open, close) - low 

body = abs(close - open) 



//Calculate Buy/Sell Volume

_rate(cond) =>

    ret = 0.5 * (tw + bw + (cond ? 2 * body : 0)) / (tw + bw + body) 

    ret := na(ret) ? 0.5 : ret

    ret



// Calculate Regression Slope for Buy/Sell Volumes

_get_trend(len)=>   

    deltaup =  volume * _rate(open <= close) 

    deltadown = volume * _rate(open > close)

    

    slope_volume_up = linreg(deltaup, len, 0) - linreg(deltaup, len, 1)

    slope_volume_down = linreg(deltadown, len, 0) - linreg(deltadown, len, 1)

    [slope_volume_up, slope_volume_down]



// get buy/sell volume regression slopes for short term period

[slope_volume_up, slope_volume_down] = _get_trend(len1)



// coloring columns

column_col = slope_price > 0 ? slope_volume_up > 0 ? slope_volume_up > slope_volume_down ? ccol11 : ccol12 : ccol13 :

      slope_price < 0 ? slope_volume_down > 0 ? slope_volume_up < slope_volume_down ? ccol21 : ccol22 : ccol23 : 

      color.gray



// short term trend

plot(slope_price * len1, color = column_col, style = plot.style_columns)



// get buy/sell volume regression slopes for long term period

[slope_volume_up_lt, slope_volume_down_lt] = _get_trend(len2)



// coloring columns

column_col := slope_price_lt > 0 ? slope_volume_up_lt > 0 ? slope_volume_up_lt > slope_volume_down_lt ? col11 : col12 : col13 : 

       slope_price_lt < 0 ? slope_volume_down_lt > 0 ? slope_volume_up_lt < slope_volume_down_lt ? col21 : col22 : col23 :

       color.gray



// Long term trend

plot(slope_price_lt * len2, color = column_col, linewidth = 3)

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