Trend Following Moving Averages in MQL5 — converted from Pine Script
The Trend Following Moving Averages 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).
Inputs
| Input | Default |
|---|---|
| MA Type | 'EMA' |
| Period to Check Trend | 20 |
| Trend Channel Rate % | 1 |
| Use Linear Regression | true |
| Linear Regression Period | 10 |
Converted MQL5
Save as trend-following-moving-averages.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: 2ad81da62375 status: CLEAN
//| notes: 0 unsupported, 0 warn, 5 info
//| Review, compile and backtest before trading.
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| Trend Following Moving Averages
//| 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 40
#property indicator_plots 20
#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_COLOR_LINE
#property indicator_color2 clrDodgerBlue
#property indicator_width2 1
#property indicator_label3 "Plot3"
#property indicator_type3 DRAW_COLOR_LINE
#property indicator_color3 clrDodgerBlue
#property indicator_width3 1
#property indicator_label4 "Plot4"
#property indicator_type4 DRAW_COLOR_LINE
#property indicator_color4 clrDodgerBlue
#property indicator_width4 1
#property indicator_label5 "Plot5"
#property indicator_type5 DRAW_COLOR_LINE
#property indicator_color5 clrDodgerBlue
#property indicator_width5 1
#property indicator_label6 "Plot6"
#property indicator_type6 DRAW_COLOR_LINE
#property indicator_color6 clrDodgerBlue
#property indicator_width6 1
#property indicator_label7 "Plot7"
#property indicator_type7 DRAW_COLOR_LINE
#property indicator_color7 clrDodgerBlue
#property indicator_width7 1
#property indicator_label8 "Plot8"
#property indicator_type8 DRAW_COLOR_LINE
#property indicator_color8 clrDodgerBlue
#property indicator_width8 1
#property indicator_label9 "Plot9"
#property indicator_type9 DRAW_COLOR_LINE
#property indicator_color9 clrDodgerBlue
#property indicator_width9 1
#property indicator_label10 "Plot10"
#property indicator_type10 DRAW_COLOR_LINE
#property indicator_color10 clrDodgerBlue
#property indicator_width10 1
#property indicator_label11 "Plot11"
#property indicator_type11 DRAW_COLOR_LINE
#property indicator_color11 clrDodgerBlue
#property indicator_width11 1
#property indicator_label12 "Plot12"
#property indicator_type12 DRAW_COLOR_LINE
#property indicator_color12 clrDodgerBlue
#property indicator_width12 1
#property indicator_label13 "Plot13"
#property indicator_type13 DRAW_COLOR_LINE
#property indicator_color13 clrDodgerBlue
#property indicator_width13 1
#property indicator_label14 "Plot14"
#property indicator_type14 DRAW_COLOR_LINE
#property indicator_color14 clrDodgerBlue
#property indicator_width14 1
#property indicator_label15 "Plot15"
#property indicator_type15 DRAW_COLOR_LINE
#property indicator_color15 clrDodgerBlue
#property indicator_width15 1
#property indicator_label16 "Plot16"
#property indicator_type16 DRAW_COLOR_LINE
#property indicator_color16 clrDodgerBlue
#property indicator_width16 1
#property indicator_label17 "Plot17"
#property indicator_type17 DRAW_COLOR_LINE
#property indicator_color17 clrDodgerBlue
#property indicator_width17 1
#property indicator_label18 "Plot18"
#property indicator_type18 DRAW_COLOR_LINE
#property indicator_color18 clrDodgerBlue
#property indicator_width18 1
#property indicator_label19 "Plot19"
#property indicator_type19 DRAW_COLOR_LINE
#property indicator_color19 clrDodgerBlue
#property indicator_width19 1
#property indicator_label20 "Plot20"
#property indicator_type20 DRAW_COLOR_LINE
#property indicator_color20 clrDodgerBlue
#property indicator_width20 1
#include "PineRuntime.mqh"
//--- plot buffers
double Buf0[];
double Buf1[];
double Buf2[];
double Buf3[];
double Buf4[];
double Buf5[];
double Buf6[];
double Buf7[];
double Buf8[];
double Buf9[];
double Buf10[];
double Buf11[];
double Buf12[];
double Buf13[];
double Buf14[];
double Buf15[];
double Buf16[];
double Buf17[];
double Buf18[];
double Buf19[];
double Buf20[];
double Buf21[];
double Buf22[];
double Buf23[];
double Buf24[];
double Buf25[];
double Buf26[];
double Buf27[];
double Buf28[];
double Buf29[];
double Buf30[];
double Buf31[];
double Buf32[];
double Buf33[];
double Buf34[];
double Buf35[];
double Buf36[];
double Buf37[];
double Buf38[];
double Buf39[];
color __pal0[64]; // the colours plot 0 has drawn so far; see pine_color_slot
int __paln0 = 0;
color __pal1[64]; // the colours plot 1 has drawn so far; see pine_color_slot
int __paln1 = 0;
color __pal2[64]; // the colours plot 2 has drawn so far; see pine_color_slot
int __paln2 = 0;
color __pal3[64]; // the colours plot 3 has drawn so far; see pine_color_slot
int __paln3 = 0;
color __pal4[64]; // the colours plot 4 has drawn so far; see pine_color_slot
int __paln4 = 0;
color __pal5[64]; // the colours plot 5 has drawn so far; see pine_color_slot
int __paln5 = 0;
color __pal6[64]; // the colours plot 6 has drawn so far; see pine_color_slot
int __paln6 = 0;
color __pal7[64]; // the colours plot 7 has drawn so far; see pine_color_slot
int __paln7 = 0;
color __pal8[64]; // the colours plot 8 has drawn so far; see pine_color_slot
int __paln8 = 0;
color __pal9[64]; // the colours plot 9 has drawn so far; see pine_color_slot
int __paln9 = 0;
color __pal10[64]; // the colours plot 10 has drawn so far; see pine_color_slot
int __paln10 = 0;
color __pal11[64]; // the colours plot 11 has drawn so far; see pine_color_slot
int __paln11 = 0;
color __pal12[64]; // the colours plot 12 has drawn so far; see pine_color_slot
int __paln12 = 0;
color __pal13[64]; // the colours plot 13 has drawn so far; see pine_color_slot
int __paln13 = 0;
color __pal14[64]; // the colours plot 14 has drawn so far; see pine_color_slot
int __paln14 = 0;
color __pal15[64]; // the colours plot 15 has drawn so far; see pine_color_slot
int __paln15 = 0;
color __pal16[64]; // the colours plot 16 has drawn so far; see pine_color_slot
int __paln16 = 0;
color __pal17[64]; // the colours plot 17 has drawn so far; see pine_color_slot
int __paln17 = 0;
color __pal18[64]; // the colours plot 18 has drawn so far; see pine_color_slot
int __paln18 = 0;
color __pal19[64]; // the colours plot 19 has drawn so far; see pine_color_slot
int __paln19 = 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 rate = PINE_NA;
double pricerange = PINE_NA;
double chan = PINE_NA;
//--- live-tick rollback: the state each bar started from
double __sv_rate;
double __si_rate;
double __sv_pricerange;
double __si_pricerange;
double __sv_chan;
double __si_chan;
int __pineSnapBar = -1;
void __pineSaveState()
{
__sv_rate = rate;
__sv_pricerange = pricerange;
__sv_chan = chan;
pine_draw_mark();
__pineSaveGen++; // helpers' static arrays snapshot lazily
}
void __pineRestoreState()
{
rate = __sv_rate;
pricerange = __sv_pricerange;
chan = __sv_chan;
pine_draw_rollback();
__pineRestoreGen++;
__pineRestoreSaveGen = __pineSaveGen;
}
void __pineSaveInitial()
{
__si_rate = rate;
__si_pricerange = pricerange;
__si_chan = chan;
pine_draw_save_initial();
}
void __pineRestoreInitial()
{
rate = __si_rate;
pricerange = __si_pricerange;
chan = __si_chan;
pine_draw_reset();
__pineInitGen++;
}
string matype = ""; // 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 __st13[]; // accumulator for one stateful call site
double __stb13[]; // secondary buffer (e.g. true range for ATR)
double __st14[]; // accumulator for one stateful call site
double __stb14[]; // secondary buffer (e.g. true range for ATR)
double __st15[]; // accumulator for one stateful call site
double __stb15[]; // secondary buffer (e.g. true range for ATR)
double __st16[]; // accumulator for one stateful call site
double __stb16[]; // secondary buffer (e.g. true range for ATR)
double __st17[]; // accumulator for one stateful call site
double __stb17[]; // secondary buffer (e.g. true range for ATR)
double __st18[]; // accumulator for one stateful call site
double __stb18[]; // secondary buffer (e.g. true range for ATR)
double __st19[]; // accumulator for one stateful call site
double __stb19[]; // secondary buffer (e.g. true range for ATR)
double __st20[]; // accumulator for one stateful call site
double __stb20[]; // secondary buffer (e.g. true range for ATR)
double __st21[]; // accumulator for one stateful call site
double __stb21[]; // secondary buffer (e.g. true range for ATR)
double __st22[]; // accumulator for one stateful call site
double __stb22[]; // secondary buffer (e.g. true range for ATR)
double __st23[]; // accumulator for one stateful call site
double __stb23[]; // secondary buffer (e.g. true range for ATR)
double __st24[]; // accumulator for one stateful call site
double __stb24[]; // secondary buffer (e.g. true range for ATR)
double __st25[]; // accumulator for one stateful call site
double __stb25[]; // secondary buffer (e.g. true range for ATR)
double __st26[]; // accumulator for one stateful call site
double __stb26[]; // secondary buffer (e.g. true range for ATR)
double __st27[]; // accumulator for one stateful call site
double __stb27[]; // secondary buffer (e.g. true range for ATR)
double __st28[]; // accumulator for one stateful call site
double __stb28[]; // secondary buffer (e.g. true range for ATR)
double __st29[]; // accumulator for one stateful call site
double __stb29[]; // secondary buffer (e.g. true range for ATR)
double __st30[]; // accumulator for one stateful call site
double __stb30[]; // secondary buffer (e.g. true range for ATR)
double __st31[]; // accumulator for one stateful call site
double __stb31[]; // secondary buffer (e.g. true range for ATR)
double __st32[]; // accumulator for one stateful call site
double __stb32[]; // secondary buffer (e.g. true range for ATR)
double __st33[]; // accumulator for one stateful call site
double __stb33[]; // secondary buffer (e.g. true range for ATR)
double __st34[]; // accumulator for one stateful call site
double __stb34[]; // secondary buffer (e.g. true range for ATR)
double __st35[]; // accumulator for one stateful call site
double __stb35[]; // secondary buffer (e.g. true range for ATR)
double __st36[]; // accumulator for one stateful call site
double __stb36[]; // secondary buffer (e.g. true range for ATR)
double __st37[]; // accumulator for one stateful call site
double __stb37[]; // secondary buffer (e.g. true range for ATR)
double __st38[]; // accumulator for one stateful call site
double __stb38[]; // secondary buffer (e.g. true range for ATR)
double __st39[]; // accumulator for one stateful call site
double __stb39[]; // secondary buffer (e.g. true range for ATR)
double __st40[]; // accumulator for one stateful call site
double __stb40[]; // secondary buffer (e.g. true range for ATR)
double __st41[]; // accumulator for one stateful call site
double __stb41[]; // secondary buffer (e.g. true range for ATR)
double __sw0[]; // materialised ta.* source: gettrend :: masrc
double __sw1[]; // materialised ta.* source: gettrend :: ma
//--- inputs
enum ENUM_MATYPE_OPTS
{
matype_opt0, // EMA
matype_opt1, // SMA
matype_opt2, // RMA
matype_opt3, // WMA
matype_opt4 // VWMA
};
string matype_Labels[] = {"EMA", "SMA", "RMA", "WMA", "VWMA"};
input ENUM_MATYPE_OPTS matype_Input = matype_opt0; // MA Type
input int prd = 20; // Period to Check Trend
input int rateinp = 1; // Trend Channel Rate %
input bool ulinreg = true; // Use Linear Regression
input int linprd = 10; // Linear Regression Period
//--- user functions
double gettrend(const double len, const int __pineBar,
const double &open[], const double &high[], const double &low[],
const double &close[], const long &volume[], const datetime &time[])
{
double masrc = ((matype == "EMA") ? pine_ema(close, __st0, __pineBar, (int)(len)) : ((matype == "RMA") ? pine_rma(close, __st1, __pineBar, (int)(len)) : ((matype == "VWMA") ? pine_vwma(close, volume, __pineBar, (int)(len)) : ((matype == "WMA") ? pine_wma(close, __pineBar, (int)(len)) : pine_sma(close, __pineBar, (int)(len))))));
double ma = (ulinreg ? pine_linreg_step(masrc, __sw0, __pineBar, (int)(linprd), (int)(0)) : masrc);
double hh = pine_highest_step(ma, __sw1, __pineBar, (int)(prd));
double ll = pine_lowest_step(ma, __sw1, __pineBar, (int)(prd));
double diff = MathAbs((hh - ll));
double trend = (((diff > chan)) ? ((((ma > (ll + chan))) ? (1) : ((((ma < (hh - chan))) ? ((-1)) : (0))))) : (0));
double _ret = pine_div((trend * diff), chan);
return _ret;
return PINE_NA;
}
color getcol(const double trend, const int __pineBar)
{
color _ret = ((trend >= 10.0) ? C'0,255,0' : ((trend >= 9.0) ? C'0,255,0' : ((trend >= 8.0) ? C'0,255,0' : ((trend >= 7.0) ? C'0,255,0' : ((trend >= 6.0) ? C'0,255,0' : ((trend >= 5.0) ? C'0,255,0' : ((trend >= 4.0) ? C'0,255,0' : ((trend >= 3.0) ? C'0,255,0' : ((trend >= 2.0) ? C'0,255,0' : ((trend >= 1.0) ? C'0,255,0' : ((trend <= (-10.0)) ? C'255,0,0' : ((trend <= (-9.0)) ? C'255,0,0' : ((trend <= (-8.0)) ? C'255,0,0' : ((trend <= (-7.0)) ? C'255,0,0' : ((trend <= (-6.0)) ? C'255,0,0' : ((trend <= (-5.0)) ? C'255,0,0' : ((trend <= (-4.0)) ? C'255,0,0' : ((trend <= (-3.0)) ? C'255,0,0' : ((trend <= (-2.0)) ? C'255,0,0' : ((trend <= (-1.0)) ? C'255,0,0' : PINE_NA))))))))))))))))))));
return _ret;
return clrNONE;
}
double getma(const int lngth, const int __pineBar,
const double &open[], const double &high[], const double &low[],
const double &close[], const long &volume[], const datetime &time[])
{
double _ret = ((matype == "EMA") ? pine_ema(close, __st2, __pineBar, (int)(lngth)) : ((matype == "RMA") ? pine_rma(close, __st3, __pineBar, (int)(lngth)) : ((matype == "VWMA") ? pine_vwma(close, volume, __pineBar, (int)(lngth)) : ((matype == "WMA") ? pine_wma(close, __pineBar, (int)(lngth)) : pine_sma(close, __pineBar, (int)(lngth))))));
return _ret;
return PINE_NA;
}
double getma__site1(const int lngth, const int __pineBar,
const double &open[], const double &high[], const double &low[],
const double &close[], const long &volume[], const datetime &time[])
{
double _ret = ((matype == "EMA") ? pine_ema(close, __st4, __pineBar, (int)(lngth)) : ((matype == "RMA") ? pine_rma(close, __st5, __pineBar, (int)(lngth)) : ((matype == "VWMA") ? pine_vwma(close, volume, __pineBar, (int)(lngth)) : ((matype == "WMA") ? pine_wma(close, __pineBar, (int)(lngth)) : pine_sma(close, __pineBar, (int)(lngth))))));
return _ret;
return PINE_NA;
}
double getma__site2(const int lngth, const int __pineBar,
const double &open[], const double &high[], const double &low[],
const double &close[], const long &volume[], const datetime &time[])
{
double _ret = ((matype == "EMA") ? pine_ema(close, __st6, __pineBar, (int)(lngth)) : ((matype == "RMA") ? pine_rma(close, __st7, __pineBar, (int)(lngth)) : ((matype == "VWMA") ? pine_vwma(close, volume, __pineBar, (int)(lngth)) : ((matype == "WMA") ? pine_wma(close, __pineBar, (int)(lngth)) : pine_sma(close, __pineBar, (int)(lngth))))));
return _ret;
return PINE_NA;
}
double getma__site3(const int lngth, const int __pineBar,
const double &open[], const double &high[], const double &low[],
const double &close[], const long &volume[], const datetime &time[])
{
double _ret = ((matype == "EMA") ? pine_ema(close, __st8, __pineBar, (int)(lngth)) : ((matype == "RMA") ? pine_rma(close, __st9, __pineBar, (int)(lngth)) : ((matype == "VWMA") ? pine_vwma(close, volume, __pineBar, (int)(lngth)) : ((matype == "WMA") ? pine_wma(close, __pineBar, (int)(lngth)) : pine_sma(close, __pineBar, (int)(lngth))))));
return _ret;
return PINE_NA;
}
double getma__site4(const int lngth, const int __pineBar,
const double &open[], const double &high[], const double &low[],
const double &close[], const long &volume[], const datetime &time[])
{
double _ret = ((matype == "EMA") ? pine_ema(close, __st10, __pineBar, (int)(lngth)) : ((matype == "RMA") ? pine_rma(close, __st11, __pineBar, (int)(lngth)) : ((matype == "VWMA") ? pine_vwma(close, volume, __pineBar, (int)(lngth)) : ((matype == "WMA") ? pine_wma(close, __pineBar, (int)(lngth)) : pine_sma(close, __pineBar, (int)(lngth))))));
return _ret;
return PINE_NA;
}
double getma__site5(const int lngth, const int __pineBar,
const double &open[], const double &high[], const double &low[],
const double &close[], const long &volume[], const datetime &time[])
{
double _ret = ((matype == "EMA") ? pine_ema(close, __st12, __pineBar, (int)(lngth)) : ((matype == "RMA") ? pine_rma(close, __st13, __pineBar, (int)(lngth)) : ((matype == "VWMA") ? pine_vwma(close, volume, __pineBar, (int)(lngth)) : ((matype == "WMA") ? pine_wma(close, __pineBar, (int)(lngth)) : pine_sma(close, __pineBar, (int)(lngth))))));
return _ret;
return PINE_NA;
}
double getma__site6(const int lngth, const int __pineBar,
const double &open[], const double &high[], const double &low[],
const double &close[], const long &volume[], const datetime &time[])
{
double _ret = ((matype == "EMA") ? pine_ema(close, __st14, __pineBar, (int)(lngth)) : ((matype == "RMA") ? pine_rma(close, __st15, __pineBar, (int)(lngth)) : ((matype == "VWMA") ? pine_vwma(close, volume, __pineBar, (int)(lngth)) : ((matype == "WMA") ? pine_wma(close, __pineBar, (int)(lngth)) : pine_sma(close, __pineBar, (int)(lngth))))));
return _ret;
return PINE_NA;
}
double getma__site7(const int lngth, const int __pineBar,
const double &open[], const double &high[], const double &low[],
const double &close[], const long &volume[], const datetime &time[])
{
double _ret = ((matype == "EMA") ? pine_ema(close, __st16, __pineBar, (int)(lngth)) : ((matype == "RMA") ? pine_rma(close, __st17, __pineBar, (int)(lngth)) : ((matype == "VWMA") ? pine_vwma(close, volume, __pineBar, (int)(lngth)) : ((matype == "WMA") ? pine_wma(close, __pineBar, (int)(lngth)) : pine_sma(close, __pineBar, (int)(lngth))))));
return _ret;
return PINE_NA;
}
double getma__site8(const int lngth, const int __pineBar,
const double &open[], const double &high[], const double &low[],
const double &close[], const long &volume[], const datetime &time[])
{
double _ret = ((matype == "EMA") ? pine_ema(close, __st18, __pineBar, (int)(lngth)) : ((matype == "RMA") ? pine_rma(close, __st19, __pineBar, (int)(lngth)) : ((matype == "VWMA") ? pine_vwma(close, volume, __pineBar, (int)(lngth)) : ((matype == "WMA") ? pine_wma(close, __pineBar, (int)(lngth)) : pine_sma(close, __pineBar, (int)(lngth))))));
return _ret;
return PINE_NA;
}
double getma__site9(const int lngth, const int __pineBar,
const double &open[], const double &high[], const double &low[],
const double &close[], const long &volume[], const datetime &time[])
{
double _ret = ((matype == "EMA") ? pine_ema(close, __st20, __pineBar, (int)(lngth)) : ((matype == "RMA") ? pine_rma(close, __st21, __pineBar, (int)(lngth)) : ((matype == "VWMA") ? pine_vwma(close, volume, __pineBar, (int)(lngth)) : ((matype == "WMA") ? pine_wma(close, __pineBar, (int)(lngth)) : pine_sma(close, __pineBar, (int)(lngth))))));
return _ret;
return PINE_NA;
}
double getma__site10(const int lngth, const int __pineBar,
const double &open[], const double &high[], const double &low[],
const double &close[], const long &volume[], const datetime &time[])
{
double _ret = ((matype == "EMA") ? pine_ema(close, __st22, __pineBar, (int)(lngth)) : ((matype == "RMA") ? pine_rma(close, __st23, __pineBar, (int)(lngth)) : ((matype == "VWMA") ? pine_vwma(close, volume, __pineBar, (int)(lngth)) : ((matype == "WMA") ? pine_wma(close, __pineBar, (int)(lngth)) : pine_sma(close, __pineBar, (int)(lngth))))));
return _ret;
return PINE_NA;
}
double getma__site11(const int lngth, const int __pineBar,
const double &open[], const double &high[], const double &low[],
const double &close[], const long &volume[], const datetime &time[])
{
double _ret = ((matype == "EMA") ? pine_ema(close, __st24, __pineBar, (int)(lngth)) : ((matype == "RMA") ? pine_rma(close, __st25, __pineBar, (int)(lngth)) : ((matype == "VWMA") ? pine_vwma(close, volume, __pineBar, (int)(lngth)) : ((matype == "WMA") ? pine_wma(close, __pineBar, (int)(lngth)) : pine_sma(close, __pineBar, (int)(lngth))))));
return _ret;
return PINE_NA;
}
double getma__site12(const int lngth, const int __pineBar,
const double &open[], const double &high[], const double &low[],
const double &close[], const long &volume[], const datetime &time[])
{
double _ret = ((matype == "EMA") ? pine_ema(close, __st26, __pineBar, (int)(lngth)) : ((matype == "RMA") ? pine_rma(close, __st27, __pineBar, (int)(lngth)) : ((matype == "VWMA") ? pine_vwma(close, volume, __pineBar, (int)(lngth)) : ((matype == "WMA") ? pine_wma(close, __pineBar, (int)(lngth)) : pine_sma(close, __pineBar, (int)(lngth))))));
return _ret;
return PINE_NA;
}
double getma__site13(const int lngth, const int __pineBar,
const double &open[], const double &high[], const double &low[],
const double &close[], const long &volume[], const datetime &time[])
{
double _ret = ((matype == "EMA") ? pine_ema(close, __st28, __pineBar, (int)(lngth)) : ((matype == "RMA") ? pine_rma(close, __st29, __pineBar, (int)(lngth)) : ((matype == "VWMA") ? pine_vwma(close, volume, __pineBar, (int)(lngth)) : ((matype == "WMA") ? pine_wma(close, __pineBar, (int)(lngth)) : pine_sma(close, __pineBar, (int)(lngth))))));
return _ret;
return PINE_NA;
}
double getma__site14(const int lngth, const int __pineBar,
const double &open[], const double &high[], const double &low[],
const double &close[], const long &volume[], const datetime &time[])
{
double _ret = ((matype == "EMA") ? pine_ema(close, __st30, __pineBar, (int)(lngth)) : ((matype == "RMA") ? pine_rma(close, __st31, __pineBar, (int)(lngth)) : ((matype == "VWMA") ? pine_vwma(close, volume, __pineBar, (int)(lngth)) : ((matype == "WMA") ? pine_wma(close, __pineBar, (int)(lngth)) : pine_sma(close, __pineBar, (int)(lngth))))));
return _ret;
return PINE_NA;
}
double getma__site15(const int lngth, const int __pineBar,
const double &open[], const double &high[], const double &low[],
const double &close[], const long &volume[], const datetime &time[])
{
double _ret = ((matype == "EMA") ? pine_ema(close, __st32, __pineBar, (int)(lngth)) : ((matype == "RMA") ? pine_rma(close, __st33, __pineBar, (int)(lngth)) : ((matype == "VWMA") ? pine_vwma(close, volume, __pineBar, (int)(lngth)) : ((matype == "WMA") ? pine_wma(close, __pineBar, (int)(lngth)) : pine_sma(close, __pineBar, (int)(lngth))))));
return _ret;
return PINE_NA;
}
double getma__site16(const int lngth, const int __pineBar,
const double &open[], const double &high[], const double &low[],
const double &close[], const long &volume[], const datetime &time[])
{
double _ret = ((matype == "EMA") ? pine_ema(close, __st34, __pineBar, (int)(lngth)) : ((matype == "RMA") ? pine_rma(close, __st35, __pineBar, (int)(lngth)) : ((matype == "VWMA") ? pine_vwma(close, volume, __pineBar, (int)(lngth)) : ((matype == "WMA") ? pine_wma(close, __pineBar, (int)(lngth)) : pine_sma(close, __pineBar, (int)(lngth))))));
return _ret;
return PINE_NA;
}
double getma__site17(const int lngth, const int __pineBar,
const double &open[], const double &high[], const double &low[],
const double &close[], const long &volume[], const datetime &time[])
{
double _ret = ((matype == "EMA") ? pine_ema(close, __st36, __pineBar, (int)(lngth)) : ((matype == "RMA") ? pine_rma(close, __st37, __pineBar, (int)(lngth)) : ((matype == "VWMA") ? pine_vwma(close, volume, __pineBar, (int)(lngth)) : ((matype == "WMA") ? pine_wma(close, __pineBar, (int)(lngth)) : pine_sma(close, __pineBar, (int)(lngth))))));
return _ret;
return PINE_NA;
}
double getma__site18(const int lngth, const int __pineBar,
const double &open[], const double &high[], const double &low[],
const double &close[], const long &volume[], const datetime &time[])
{
double _ret = ((matype == "EMA") ? pine_ema(close, __st38, __pineBar, (int)(lngth)) : ((matype == "RMA") ? pine_rma(close, __st39, __pineBar, (int)(lngth)) : ((matype == "VWMA") ? pine_vwma(close, volume, __pineBar, (int)(lngth)) : ((matype == "WMA") ? pine_wma(close, __pineBar, (int)(lngth)) : pine_sma(close, __pineBar, (int)(lngth))))));
return _ret;
return PINE_NA;
}
double getma__site19(const int lngth, const int __pineBar,
const double &open[], const double &high[], const double &low[],
const double &close[], const long &volume[], const datetime &time[])
{
double _ret = ((matype == "EMA") ? pine_ema(close, __st40, __pineBar, (int)(lngth)) : ((matype == "RMA") ? pine_rma(close, __st41, __pineBar, (int)(lngth)) : ((matype == "VWMA") ? pine_vwma(close, volume, __pineBar, (int)(lngth)) : ((matype == "WMA") ? pine_wma(close, __pineBar, (int)(lngth)) : pine_sma(close, __pineBar, (int)(lngth))))));
return _ret;
return PINE_NA;
}
int OnInit()
{
matype = matype_Labels[(int)matype_Input];
if(prd < 5) { Print("Period to Check Trend", " must be at least 5"); return INIT_PARAMETERS_INCORRECT; }
if(rateinp < 0.1) { Print("Trend Channel Rate %", " must be at least 0.1"); return INIT_PARAMETERS_INCORRECT; }
if(linprd < 2) { Print("Linear Regression Period", " must be at least 2"); return INIT_PARAMETERS_INCORRECT; }
rate = pine_div(rateinp, 100);
SetIndexBuffer(0, Buf0, INDICATOR_DATA);
SetIndexBuffer(1, Buf1, INDICATOR_COLOR_INDEX);
SetIndexBuffer(2, Buf2, INDICATOR_DATA);
SetIndexBuffer(3, Buf3, INDICATOR_COLOR_INDEX);
SetIndexBuffer(4, Buf4, INDICATOR_DATA);
SetIndexBuffer(5, Buf5, INDICATOR_COLOR_INDEX);
SetIndexBuffer(6, Buf6, INDICATOR_DATA);
SetIndexBuffer(7, Buf7, INDICATOR_COLOR_INDEX);
SetIndexBuffer(8, Buf8, INDICATOR_DATA);
SetIndexBuffer(9, Buf9, INDICATOR_COLOR_INDEX);
SetIndexBuffer(10, Buf10, INDICATOR_DATA);
SetIndexBuffer(11, Buf11, INDICATOR_COLOR_INDEX);
SetIndexBuffer(12, Buf12, INDICATOR_DATA);
SetIndexBuffer(13, Buf13, INDICATOR_COLOR_INDEX);
SetIndexBuffer(14, Buf14, INDICATOR_DATA);
SetIndexBuffer(15, Buf15, INDICATOR_COLOR_INDEX);
SetIndexBuffer(16, Buf16, INDICATOR_DATA);
SetIndexBuffer(17, Buf17, INDICATOR_COLOR_INDEX);
SetIndexBuffer(18, Buf18, INDICATOR_DATA);
SetIndexBuffer(19, Buf19, INDICATOR_COLOR_INDEX);
SetIndexBuffer(20, Buf20, INDICATOR_DATA);
SetIndexBuffer(21, Buf21, INDICATOR_COLOR_INDEX);
SetIndexBuffer(22, Buf22, INDICATOR_DATA);
SetIndexBuffer(23, Buf23, INDICATOR_COLOR_INDEX);
SetIndexBuffer(24, Buf24, INDICATOR_DATA);
SetIndexBuffer(25, Buf25, INDICATOR_COLOR_INDEX);
SetIndexBuffer(26, Buf26, INDICATOR_DATA);
SetIndexBuffer(27, Buf27, INDICATOR_COLOR_INDEX);
SetIndexBuffer(28, Buf28, INDICATOR_DATA);
SetIndexBuffer(29, Buf29, INDICATOR_COLOR_INDEX);
SetIndexBuffer(30, Buf30, INDICATOR_DATA);
SetIndexBuffer(31, Buf31, INDICATOR_COLOR_INDEX);
SetIndexBuffer(32, Buf32, INDICATOR_DATA);
SetIndexBuffer(33, Buf33, INDICATOR_COLOR_INDEX);
SetIndexBuffer(34, Buf34, INDICATOR_DATA);
SetIndexBuffer(35, Buf35, INDICATOR_COLOR_INDEX);
SetIndexBuffer(36, Buf36, INDICATOR_DATA);
SetIndexBuffer(37, Buf37, INDICATOR_COLOR_INDEX);
SetIndexBuffer(38, Buf38, INDICATOR_DATA);
SetIndexBuffer(39, Buf39, INDICATOR_COLOR_INDEX);
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);
PlotIndexSetDouble(16, PLOT_EMPTY_VALUE, EMPTY_VALUE);
PlotIndexSetDouble(17, PLOT_EMPTY_VALUE, EMPTY_VALUE);
PlotIndexSetDouble(18, PLOT_EMPTY_VALUE, EMPTY_VALUE);
PlotIndexSetDouble(19, PLOT_EMPTY_VALUE, EMPTY_VALUE);
IndicatorSetString(INDICATOR_SHORTNAME, "Trend Following Moving Averages");
__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[])
{
matype = matype_Labels[(int)matype_Input];
rate = pine_div(rateinp, 100);
pricerange = (pine_highest(high, __pineBar, (int)(280)) - pine_lowest(low, __pineBar, (int)(280)));
chan = (pricerange * rate);
double __pinePlotValue0 = getma(5, __pineBar, open, high, low, close, volume, time);
double __pinePlotColour0 = (double)(getcol(gettrend(5, __pineBar, open, high, low, close, volume, time), __pineBar));
Buf0[__pineBar] = __pinePlotValue0;
Buf1[__pineBar] = pine_color_slot(0, pine_na(__pinePlotColour0) ? clrNONE : (color)__pinePlotColour0, __pal0, __paln0);
double __pinePlotValue1 = getma__site1(10, __pineBar, open, high, low, close, volume, time);
double __pinePlotColour1 = (double)(getcol(gettrend(10, __pineBar, open, high, low, close, volume, time), __pineBar));
Buf2[__pineBar] = __pinePlotValue1;
Buf3[__pineBar] = pine_color_slot(1, pine_na(__pinePlotColour1) ? clrNONE : (color)__pinePlotColour1, __pal1, __paln1);
double __pinePlotValue2 = getma__site2(15, __pineBar, open, high, low, close, volume, time);
double __pinePlotColour2 = (double)(getcol(gettrend(15, __pineBar, open, high, low, close, volume, time), __pineBar));
Buf4[__pineBar] = __pinePlotValue2;
Buf5[__pineBar] = pine_color_slot(2, pine_na(__pinePlotColour2) ? clrNONE : (color)__pinePlotColour2, __pal2, __paln2);
double __pinePlotValue3 = getma__site3(20, __pineBar, open, high, low, close, volume, time);
double __pinePlotColour3 = (double)(getcol(gettrend(20, __pineBar, open, high, low, close, volume, time), __pineBar));
Buf6[__pineBar] = __pinePlotValue3;
Buf7[__pineBar] = pine_color_slot(3, pine_na(__pinePlotColour3) ? clrNONE : (color)__pinePlotColour3, __pal3, __paln3);
double __pinePlotValue4 = getma__site4(25, __pineBar, open, high, low, close, volume, time);
double __pinePlotColour4 = (double)(getcol(gettrend(25, __pineBar, open, high, low, close, volume, time), __pineBar));
Buf8[__pineBar] = __pinePlotValue4;
Buf9[__pineBar] = pine_color_slot(4, pine_na(__pinePlotColour4) ? clrNONE : (color)__pinePlotColour4, __pal4, __paln4);
double __pinePlotValue5 = getma__site5(30, __pineBar, open, high, low, close, volume, time);
double __pinePlotColour5 = (double)(getcol(gettrend(30, __pineBar, open, high, low, close, volume, time), __pineBar));
Buf10[__pineBar] = __pinePlotValue5;
Buf11[__pineBar] = pine_color_slot(5, pine_na(__pinePlotColour5) ? clrNONE : (color)__pinePlotColour5, __pal5, __paln5);
double __pinePlotValue6 = getma__site6(35, __pineBar, open, high, low, close, volume, time);
double __pinePlotColour6 = (double)(getcol(gettrend(35, __pineBar, open, high, low, close, volume, time), __pineBar));
Buf12[__pineBar] = __pinePlotValue6;
Buf13[__pineBar] = pine_color_slot(6, pine_na(__pinePlotColour6) ? clrNONE : (color)__pinePlotColour6, __pal6, __paln6);
double __pinePlotValue7 = getma__site7(40, __pineBar, open, high, low, close, volume, time);
double __pinePlotColour7 = (double)(getcol(gettrend(40, __pineBar, open, high, low, close, volume, time), __pineBar));
Buf14[__pineBar] = __pinePlotValue7;
Buf15[__pineBar] = pine_color_slot(7, pine_na(__pinePlotColour7) ? clrNONE : (color)__pinePlotColour7, __pal7, __paln7);
double __pinePlotValue8 = getma__site8(45, __pineBar, open, high, low, close, volume, time);
double __pinePlotColour8 = (double)(getcol(gettrend(45, __pineBar, open, high, low, close, volume, time), __pineBar));
Buf16[__pineBar] = __pinePlotValue8;
Buf17[__pineBar] = pine_color_slot(8, pine_na(__pinePlotColour8) ? clrNONE : (color)__pinePlotColour8, __pal8, __paln8);
double __pinePlotValue9 = getma__site9(50, __pineBar, open, high, low, close, volume, time);
double __pinePlotColour9 = (double)(getcol(gettrend(50, __pineBar, open, high, low, close, volume, time), __pineBar));
Buf18[__pineBar] = __pinePlotValue9;
Buf19[__pineBar] = pine_color_slot(9, pine_na(__pinePlotColour9) ? clrNONE : (color)__pinePlotColour9, __pal9, __paln9);
double __pinePlotValue10 = getma__site10(55, __pineBar, open, high, low, close, volume, time);
double __pinePlotColour10 = (double)(getcol(gettrend(55, __pineBar, open, high, low, close, volume, time), __pineBar));
Buf20[__pineBar] = __pinePlotValue10;
Buf21[__pineBar] = pine_color_slot(10, pine_na(__pinePlotColour10) ? clrNONE : (color)__pinePlotColour10, __pal10, __paln10);
double __pinePlotValue11 = getma__site11(60, __pineBar, open, high, low, close, volume, time);
double __pinePlotColour11 = (double)(getcol(gettrend(60, __pineBar, open, high, low, close, volume, time), __pineBar));
Buf22[__pineBar] = __pinePlotValue11;
Buf23[__pineBar] = pine_color_slot(11, pine_na(__pinePlotColour11) ? clrNONE : (color)__pinePlotColour11, __pal11, __paln11);
double __pinePlotValue12 = getma__site12(65, __pineBar, open, high, low, close, volume, time);
double __pinePlotColour12 = (double)(getcol(gettrend(65, __pineBar, open, high, low, close, volume, time), __pineBar));
Buf24[__pineBar] = __pinePlotValue12;
Buf25[__pineBar] = pine_color_slot(12, pine_na(__pinePlotColour12) ? clrNONE : (color)__pinePlotColour12, __pal12, __paln12);
double __pinePlotValue13 = getma__site13(70, __pineBar, open, high, low, close, volume, time);
double __pinePlotColour13 = (double)(getcol(gettrend(70, __pineBar, open, high, low, close, volume, time), __pineBar));
Buf26[__pineBar] = __pinePlotValue13;
Buf27[__pineBar] = pine_color_slot(13, pine_na(__pinePlotColour13) ? clrNONE : (color)__pinePlotColour13, __pal13, __paln13);
double __pinePlotValue14 = getma__site14(75, __pineBar, open, high, low, close, volume, time);
double __pinePlotColour14 = (double)(getcol(gettrend(75, __pineBar, open, high, low, close, volume, time), __pineBar));
Buf28[__pineBar] = __pinePlotValue14;
Buf29[__pineBar] = pine_color_slot(14, pine_na(__pinePlotColour14) ? clrNONE : (color)__pinePlotColour14, __pal14, __paln14);
double __pinePlotValue15 = getma__site15(80, __pineBar, open, high, low, close, volume, time);
double __pinePlotColour15 = (double)(getcol(gettrend(80, __pineBar, open, high, low, close, volume, time), __pineBar));
Buf30[__pineBar] = __pinePlotValue15;
Buf31[__pineBar] = pine_color_slot(15, pine_na(__pinePlotColour15) ? clrNONE : (color)__pinePlotColour15, __pal15, __paln15);
double __pinePlotValue16 = getma__site16(85, __pineBar, open, high, low, close, volume, time);
double __pinePlotColour16 = (double)(getcol(gettrend(85, __pineBar, open, high, low, close, volume, time), __pineBar));
Buf32[__pineBar] = __pinePlotValue16;
Buf33[__pineBar] = pine_color_slot(16, pine_na(__pinePlotColour16) ? clrNONE : (color)__pinePlotColour16, __pal16, __paln16);
double __pinePlotValue17 = getma__site17(90, __pineBar, open, high, low, close, volume, time);
double __pinePlotColour17 = (double)(getcol(gettrend(90, __pineBar, open, high, low, close, volume, time), __pineBar));
Buf34[__pineBar] = __pinePlotValue17;
Buf35[__pineBar] = pine_color_slot(17, pine_na(__pinePlotColour17) ? clrNONE : (color)__pinePlotColour17, __pal17, __paln17);
double __pinePlotValue18 = getma__site18(95, __pineBar, open, high, low, close, volume, time);
double __pinePlotColour18 = (double)(getcol(gettrend(95, __pineBar, open, high, low, close, volume, time), __pineBar));
Buf36[__pineBar] = __pinePlotValue18;
Buf37[__pineBar] = pine_color_slot(18, pine_na(__pinePlotColour18) ? clrNONE : (color)__pinePlotColour18, __pal18, __paln18);
double __pinePlotValue19 = getma__site19(100, __pineBar, open, high, low, close, volume, time);
double __pinePlotColour19 = (double)(getcol(gettrend(100, __pineBar, open, high, low, close, volume, time), __pineBar));
Buf38[__pineBar] = __pinePlotValue19;
Buf39[__pineBar] = pine_color_slot(19, pine_na(__pinePlotColour19) ? clrNONE : (color)__pinePlotColour19, __pal19, __paln19);
}
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(__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(__st13, rates_total);
ArrayResize(__stb13, rates_total);
ArrayResize(__st14, rates_total);
ArrayResize(__stb14, rates_total);
ArrayResize(__st15, rates_total);
ArrayResize(__stb15, rates_total);
ArrayResize(__st16, rates_total);
ArrayResize(__stb16, rates_total);
ArrayResize(__st17, rates_total);
ArrayResize(__stb17, rates_total);
ArrayResize(__st18, rates_total);
ArrayResize(__stb18, rates_total);
ArrayResize(__st19, rates_total);
ArrayResize(__stb19, rates_total);
ArrayResize(__st20, rates_total);
ArrayResize(__stb20, rates_total);
ArrayResize(__st21, rates_total);
ArrayResize(__stb21, rates_total);
ArrayResize(__st22, rates_total);
ArrayResize(__stb22, rates_total);
ArrayResize(__st23, rates_total);
ArrayResize(__stb23, rates_total);
ArrayResize(__st24, rates_total);
ArrayResize(__stb24, rates_total);
ArrayResize(__st25, rates_total);
ArrayResize(__stb25, rates_total);
ArrayResize(__st26, rates_total);
ArrayResize(__stb26, rates_total);
ArrayResize(__st27, rates_total);
ArrayResize(__stb27, rates_total);
ArrayResize(__st28, rates_total);
ArrayResize(__stb28, rates_total);
ArrayResize(__st29, rates_total);
ArrayResize(__stb29, rates_total);
ArrayResize(__st30, rates_total);
ArrayResize(__stb30, rates_total);
ArrayResize(__st31, rates_total);
ArrayResize(__stb31, rates_total);
ArrayResize(__st32, rates_total);
ArrayResize(__stb32, rates_total);
ArrayResize(__st33, rates_total);
ArrayResize(__stb33, rates_total);
ArrayResize(__st34, rates_total);
ArrayResize(__stb34, rates_total);
ArrayResize(__st35, rates_total);
ArrayResize(__stb35, rates_total);
ArrayResize(__st36, rates_total);
ArrayResize(__stb36, rates_total);
ArrayResize(__st37, rates_total);
ArrayResize(__stb37, rates_total);
ArrayResize(__st38, rates_total);
ArrayResize(__stb38, rates_total);
ArrayResize(__st39, rates_total);
ArrayResize(__stb39, rates_total);
ArrayResize(__st40, rates_total);
ArrayResize(__stb40, rates_total);
ArrayResize(__st41, rates_total);
ArrayResize(__stb41, rates_total);
ArrayResize(__sw0, rates_total);
ArrayResize(__sw1, rates_total);
if(prev_calculated == 0)
{
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(__st13, PINE_NA);
ArrayInitialize(__stb13, PINE_NA);
ArrayInitialize(__st14, PINE_NA);
ArrayInitialize(__stb14, PINE_NA);
ArrayInitialize(__st15, PINE_NA);
ArrayInitialize(__stb15, PINE_NA);
ArrayInitialize(__st16, PINE_NA);
ArrayInitialize(__stb16, PINE_NA);
ArrayInitialize(__st17, PINE_NA);
ArrayInitialize(__stb17, PINE_NA);
ArrayInitialize(__st18, PINE_NA);
ArrayInitialize(__stb18, PINE_NA);
ArrayInitialize(__st19, PINE_NA);
ArrayInitialize(__stb19, PINE_NA);
ArrayInitialize(__st20, PINE_NA);
ArrayInitialize(__stb20, PINE_NA);
ArrayInitialize(__st21, PINE_NA);
ArrayInitialize(__stb21, PINE_NA);
ArrayInitialize(__st22, PINE_NA);
ArrayInitialize(__stb22, PINE_NA);
ArrayInitialize(__st23, PINE_NA);
ArrayInitialize(__stb23, PINE_NA);
ArrayInitialize(__st24, PINE_NA);
ArrayInitialize(__stb24, PINE_NA);
ArrayInitialize(__st25, PINE_NA);
ArrayInitialize(__stb25, PINE_NA);
ArrayInitialize(__st26, PINE_NA);
ArrayInitialize(__stb26, PINE_NA);
ArrayInitialize(__st27, PINE_NA);
ArrayInitialize(__stb27, PINE_NA);
ArrayInitialize(__st28, PINE_NA);
ArrayInitialize(__stb28, PINE_NA);
ArrayInitialize(__st29, PINE_NA);
ArrayInitialize(__stb29, PINE_NA);
ArrayInitialize(__st30, PINE_NA);
ArrayInitialize(__stb30, PINE_NA);
ArrayInitialize(__st31, PINE_NA);
ArrayInitialize(__stb31, PINE_NA);
ArrayInitialize(__st32, PINE_NA);
ArrayInitialize(__stb32, PINE_NA);
ArrayInitialize(__st33, PINE_NA);
ArrayInitialize(__stb33, PINE_NA);
ArrayInitialize(__st34, PINE_NA);
ArrayInitialize(__stb34, PINE_NA);
ArrayInitialize(__st35, PINE_NA);
ArrayInitialize(__stb35, PINE_NA);
ArrayInitialize(__st36, PINE_NA);
ArrayInitialize(__stb36, PINE_NA);
ArrayInitialize(__st37, PINE_NA);
ArrayInitialize(__stb37, PINE_NA);
ArrayInitialize(__st38, PINE_NA);
ArrayInitialize(__stb38, PINE_NA);
ArrayInitialize(__st39, PINE_NA);
ArrayInitialize(__stb39, PINE_NA);
ArrayInitialize(__st40, PINE_NA);
ArrayInitialize(__stb40, PINE_NA);
ArrayInitialize(__st41, PINE_NA);
ArrayInitialize(__stb41, PINE_NA);
ArrayInitialize(__sw0, PINE_NA);
ArrayInitialize(__sw1, 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("Trend Following Moving Averages", overlay = true)
matype= input('EMA', title = "MA Type", options =['EMA', 'SMA', 'RMA', 'WMA', 'VWMA'])
prd = input(20, title = "Period to Check Trend", minval = 5)
rateinp = input(1, title = "Trend Channel Rate %", minval = 0.1, step = 0.1)
ulinreg = input(true, title = "Use Linear Regression")
linprd = input(10, title = "Linear Regression Period", minval = 2)
rate = rateinp / 100
pricerange = highest(280) - lowest(280)
chan = pricerange * rate
gettrend(len)=>
masrc = matype == 'EMA' ? ema(close, len) : matype == 'RMA' ? rma(close, len) : matype == 'VWMA' ? vwma(close, len) : matype == 'WMA' ? wma(close, len) : sma(close, len)
ma = ulinreg ? linreg(masrc, linprd, 0) : masrc
hh = highest(ma, prd)
ll = lowest(ma, prd)
diff = abs(hh - ll)
trend = iff(diff > chan, iff(ma > ll + chan, 1, iff(ma < hh - chan, -1, 0)), 0)
_ret = trend * diff / chan
_ret
getcol(trend)=>
_ret = trend >= 10.0 ? #00FF00ff : trend >= 9.0 ? #00FF00ef : trend >= 8.0 ? #00FF00df : trend >= 7.0 ? #00FF00cf : trend >= 6.0 ? #00FF00bf :
trend >= 5.0 ? #00FF00af : trend >= 4.0 ? #00FF009f : trend >= 3.0 ? #00FF008f : trend >= 2.0 ? #00FF007f : trend >= 1.0 ? #00FF006f :
trend <= -10.0 ? #FF0000ff : trend <= -9.0 ? #FF0000ef : trend <= -8.0 ? #FF0000df : trend <= -7.0 ? #FF0000cf : trend <= -6.0 ? #FF0000bf :
trend <= -5.0 ? #FF0000af : trend <= -4.0 ? #FF00009f : trend <= -3.0 ? #FF00008f : trend <= -2.0 ? #FF00007f : trend <= -1.0 ? #FF00006f :
na
getma(lngth)=>
_ret = matype == 'EMA' ? ema(close, lngth) : matype == 'RMA' ? rma(close, lngth) : matype == 'VWMA' ? vwma(close, lngth) : matype == 'WMA' ? wma(close, lngth) : sma(close, lngth)
plot(getma(5), color = getcol(gettrend(5)))
plot(getma(10), color = getcol(gettrend(10)))
plot(getma(15), color = getcol(gettrend(15)))
plot(getma(20), color = getcol(gettrend(20)))
plot(getma(25), color = getcol(gettrend(25)))
plot(getma(30), color = getcol(gettrend(30)))
plot(getma(35), color = getcol(gettrend(35)))
plot(getma(40), color = getcol(gettrend(40)))
plot(getma(45), color = getcol(gettrend(45)))
plot(getma(50), color = getcol(gettrend(50)))
plot(getma(55), color = getcol(gettrend(55)))
plot(getma(60), color = getcol(gettrend(60)))
plot(getma(65), color = getcol(gettrend(65)))
plot(getma(70), color = getcol(gettrend(70)))
plot(getma(75), color = getcol(gettrend(75)))
plot(getma(80), color = getcol(gettrend(80)))
plot(getma(85), color = getcol(gettrend(85)))
plot(getma(90), color = getcol(gettrend(90)))
plot(getma(95), color = getcol(gettrend(95)))
plot(getma(100), color = getcol(gettrend(100)))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