Radius Trend [ChartPrime] in MQL5 — converted from Pine Script

The Radius Trend [ChartPrime] indicator by ChartPrime, 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
163 lines of Pine v5

Converted MQL5

Save as radius-trend-chartprime.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: e7ec3e5dc5fa   status: CLEAN
//| notes: 0 unsupported, 0 warn, 3 info
//| Review, compile and backtest before trading.
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| Radius Trend [ChartPrime]
//| 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 4
#property indicator_plots   3
#property indicator_label1  "Plot1"
#property indicator_type1   DRAW_COLOR_LINE
#property indicator_color1  clrDodgerBlue
#property indicator_width1  1
#property indicator_label2  "Plot2"
#property indicator_type2   DRAW_LINE
#property indicator_color2  clrDodgerBlue
#property indicator_width2  1
#property indicator_label3  "Plot3"
#property indicator_type3   DRAW_NONE
#property indicator_color3  clrDodgerBlue
#property indicator_width3  1

#include "PineRuntime.mqh"

//--- plot buffers
double Buf0[];
double Buf1[];
double Buf2[];
double Buf3[];
color  __pal0[64];   // the colours plot 0 has drawn so far; see pine_color_slot
int    __paln0 = 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 trend[];
double multi1 = 0.0;
double multi2 = 0.0;
int n = 3;
double band = 0.0;
double distance = PINE_NA;
double distance1 = PINE_NA;
double Sband = PINE_NA;
color color_ = clrNONE;
double band_upper = PINE_NA;
double band_lower = PINE_NA;
double band1 = PINE_NA;

//--- live-tick rollback: the state each bar started from
double __sv_multi1;
double __si_multi1;
double __sv_multi2;
double __si_multi2;
int __sv_n;
int __si_n;
double __sv_band;
double __si_band;
double __sv_distance;
double __si_distance;
double __sv_distance1;
double __si_distance1;
double __sv_Sband;
double __si_Sband;
color __sv_color_;
color __si_color_;
double __sv_band_upper;
double __si_band_upper;
double __sv_band_lower;
double __si_band_lower;
double __sv_band1;
double __si_band1;
int __pineSnapBar = -1;
void __pineSaveState()
{
   __sv_multi1 = multi1;
   __sv_multi2 = multi2;
   __sv_n = n;
   __sv_band = band;
   __sv_distance = distance;
   __sv_distance1 = distance1;
   __sv_Sband = Sband;
   __sv_color_ = color_;
   __sv_band_upper = band_upper;
   __sv_band_lower = band_lower;
   __sv_band1 = band1;
   pine_draw_mark();
   __pineSaveGen++;   // helpers' static arrays snapshot lazily
}
void __pineRestoreState()
{
   multi1 = __sv_multi1;
   multi2 = __sv_multi2;
   n = __sv_n;
   band = __sv_band;
   distance = __sv_distance;
   distance1 = __sv_distance1;
   Sband = __sv_Sband;
   color_ = __sv_color_;
   band_upper = __sv_band_upper;
   band_lower = __sv_band_lower;
   band1 = __sv_band1;
   pine_draw_rollback();
   __pineRestoreGen++;
   __pineRestoreSaveGen = __pineSaveGen;
}
void __pineSaveInitial()
{
   __si_multi1 = multi1;
   __si_multi2 = multi2;
   __si_n = n;
   __si_band = band;
   __si_distance = distance;
   __si_distance1 = distance1;
   __si_Sband = Sband;
   __si_color_ = color_;
   __si_band_upper = band_upper;
   __si_band_lower = band_lower;
   __si_band1 = band1;
   pine_draw_save_initial();
}
void __pineRestoreInitial()
{
   multi1 = __si_multi1;
   multi2 = __si_multi2;
   n = __si_n;
   band = __si_band;
   distance = __si_distance;
   distance1 = __si_distance1;
   Sband = __si_Sband;
   color_ = __si_color_;
   band_upper = __si_band_upper;
   band_lower = __si_band_lower;
   band1 = __si_band1;
   pine_draw_reset();
   __pineInitGen++;
}

double __hl2[];   // Pine's `hl2` as a real series, filled per bar
double __ts0[];   // materialised ta.* source: MathAbs((high[__pineBar] - low[__pineBar]))
double __ti0[];   // materialised ta.* source: band
double __ti1[];   // materialised ta.* source: (band + (distance * 0.5))
double __ti2[];   // materialised ta.* source: (band - (distance * 0.5))

//--- inputs
input double step = 0.15;   // Radius Step
input double multi = 2;   // Start Points Distance

int OnInit()
{
   SetIndexBuffer(0, Buf0, INDICATOR_DATA);
   SetIndexBuffer(1, Buf1, INDICATOR_COLOR_INDEX);
   SetIndexBuffer(2, Buf2, INDICATOR_DATA);
   SetIndexBuffer(3, Buf3, INDICATOR_DATA);
   PlotIndexSetDouble(0, PLOT_EMPTY_VALUE, EMPTY_VALUE);
   PlotIndexSetDouble(1, PLOT_EMPTY_VALUE, EMPTY_VALUE);
   PlotIndexSetDouble(2, PLOT_EMPTY_VALUE, EMPTY_VALUE);
   PlotIndexSetInteger(0, PLOT_LINE_COLOR, pine_color_new((color)((color)ChartGetInteger(0, CHART_COLOR_FOREGROUND)), 50));
   PlotIndexSetInteger(1, PLOT_LINE_COLOR, C'120,123,134');
   PlotIndexSetInteger(2, PLOT_SHOW_DATA, false);
   PlotIndexSetInteger(2, PLOT_DRAW_TYPE, DRAW_NONE);
   IndicatorSetString(INDICATOR_SHORTNAME, "Radius Trend [ChartPrime]");
   __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[])
{
   __hl2[__pineBar] = (high[__pineBar] + low[__pineBar]) / 2.0;
   __ts0[__pineBar] = MathAbs((high[__pineBar] - low[__pineBar]));
   if(__pineBar > 0)
   {
      trend[__pineBar] = trend[__pineBar - 1];
   }
   trend[__pineBar] = PINE_NA;
   n = 3;
   distance = (pine_sma(__ts0, __pineBar, (int)(100)) * multi);
   distance1 = (pine_sma(__ts0, __pineBar, (int)(100)) * 0.2);
   if((__pineBar == 101))
   {
      trend[__pineBar] = true;
      band = (low[__pineBar] * 0.8);
   }
   if((close[__pineBar] < band))
   {
      trend[__pineBar] = false;
   }
   if((close[__pineBar] > band))
   {
      trend[__pineBar] = true;
   }
   if(((((pine_hist(trend, __pineBar, (int)(1)) == false) ? 1 : 0) & (pine_truthy(pine_change(trend, __pineBar, (int)(1))) ? 1 : 0)) != 0))
   {
      band = (low[__pineBar] - distance);
   }
   if(((((pine_hist(trend, __pineBar, (int)(1)) == true) ? 1 : 0) & (pine_truthy(pine_change(trend, __pineBar, (int)(1))) ? 1 : 0)) != 0))
   {
      band = (high[__pineBar] + distance);
   }
   if(((((pine_imod(__pineBar, n) == 0) ? 1 : 0) & (trend[__pineBar] ? 1 : 0)) != 0))
   {
      multi1 = 0;
      multi2 = (multi2 + step);
      band = (band + (distance1 * multi2));
   }
   if(((((pine_imod(__pineBar, n) == 0) ? 1 : 0) & ((!trend[__pineBar]) ? 1 : 0)) != 0))
   {
      multi1 = (multi1 + step);
      multi2 = 0;
      band = (band - (distance1 * multi1));
   }
   __ti0[__pineBar] = band;
   Sband = pine_sma(__ti0, __pineBar, (int)(n));
   color_ = (trend[__pineBar] ? C'84,182,212' : C'207,43,43');
   __ti1[__pineBar] = (band + (distance * 0.5));
   band_upper = pine_sma(__ti1, __pineBar, (int)(n));
   __ti2[__pineBar] = (band - (distance * 0.5));
   band_lower = pine_sma(__ti2, __pineBar, (int)(n));
   band1 = (trend[__pineBar] ? band_upper : band_lower);
   0 /* fill dropped: visual only */;
   double __pinePlotValue0 = band1;
   double __pinePlotColour0 = (double)(((pine_imod(__pineBar, 2) == 0) ? pine_color_new((color)((color)ChartGetInteger(0, CHART_COLOR_FOREGROUND)), 50) : PINE_NA));
   Buf0[__pineBar] = __pinePlotValue0;
   Buf1[__pineBar] = pine_color_slot(0, pine_na(__pinePlotColour0) ? clrNONE : (color)__pinePlotColour0, __pal0, __paln0);
   Buf2[__pineBar] = (pine_truthy(pine_change(trend, __pineBar, (int)(1))) ? PINE_NA : Sband);
   Buf3[__pineBar] = pine_sma(__hl2, __pineBar, (int)(20));
}

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(trend, rates_total);
   ArrayResize(__hl2, rates_total);
   ArrayResize(__ts0, rates_total);
   ArrayResize(__ti0, rates_total);
   ArrayResize(__ti1, rates_total);
   ArrayResize(__ti2, rates_total);
   if(prev_calculated == 0)
   {
      ArrayInitialize(trend, PINE_NA);
      ArrayInitialize(__hl2, PINE_NA);
      ArrayInitialize(__ts0, PINE_NA);
      ArrayInitialize(__ti0, PINE_NA);
      ArrayInitialize(__ti1, PINE_NA);
      ArrayInitialize(__ti2, 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

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

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

// ©ChartPrime



//@version=5

indicator("Radius Trend [ChartPrime]", overlay = true)



// --------------------------------------------------------------------------------------------------------------------}

// 𝙐𝙎𝙀𝙍 𝙄𝙉𝙋𝙐𝙏𝙎

// --------------------------------------------------------------------------------------------------------------------{

// @variable Step size for radius adjustment

float step       = input.float(0.15, "Radius Step", step = 0.001)

// @variable Multiplier for initial distance calculation

float multi      = input.float(2, "Start Points Distance", step = 0.1)



// Initialize variables

bool trend       = na

var float multi1 = 0.

var float multi2 = 0.

int n            = 3

var float band   = 0.



// --------------------------------------------------------------------------------------------------------------------}

// 𝙄𝙉𝘿𝙄𝘾𝘼𝙏𝙊𝙍 𝘾𝘼𝙇𝘾𝙐𝙇𝘼𝙏𝙄𝙊𝙉𝙎

// --------------------------------------------------------------------------------------------------------------------{

// Calculate distances for band placement

float distance  = ta.sma(math.abs(high-low), 100) * multi

float distance1 = ta.sma(math.abs(high-low), 100) * 0.2



// Initialize trend and band on the 101st bar

if bar_index == 101

    trend := true

    band := low * 0.8



// Update trend based on price relation to band

if close < band

    trend := false



if close > band

    trend := true



// Adjust band on trend changes

if trend[1] == false and ta.change(trend)

    band := low - distance



if trend[1] == true and ta.change(trend)

    band := high + distance



// Apply step angle to trend lines

if bar_index % n == 0 and trend

    multi1 := 0

    multi2 += step    

    band   += distance1 * multi2



if bar_index % n == 0 and not trend

    multi1 += step

    multi2 := 0   

    band   -= distance1 * multi1



// Smooth the band

Sband = ta.sma(band, n)



// Set color based on trend

color      = trend ? #54b6d4 : #cf2b2b



// Calculate upper and lower bands

band_upper = ta.sma(band + distance*0.5, n)

band_lower = ta.sma(band - distance*0.5, n)

band1      = trend ? band_upper : band_lower



// --------------------------------------------------------------------------------------------------------------------}

// 𝙑𝙄𝙎𝙐𝘼𝙇𝙄𝙕𝘼𝙏𝙄𝙊𝙉

// --------------------------------------------------------------------------------------------------------------------{



// Plot the outer band

plot(band1, color = bar_index % 2 == 0 ? color.new(chart.fg_color, 50) : na)



// Plot the main band and fill area

p1 = plot(ta.change(trend) ? na : Sband, style = plot.style_linebr, color = color.gray)

p2 = plot(ta.sma(hl2, 20), display = display.none)



fill(p1, p2, band, ta.sma(hl2, 20), color.new(color, 60), na)

// --------------------------------------------------------------------------------------------------------------------}

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