MQ4 is code for meta trader 4, and yes, I use it for Bitcoin trading.
Was interested in backtesting with zipline using Bitcoin data from BTC-E.com (I think I saw an example here) or just porting this indicator to MT4.
I've found it to be really useful for short term trading.
Seems like the tradingview code is invoking some pre-made script so no go then.
Stumbled upon this, maybe you can work with it (this has the mql4 part covered and contains code for a lot of different MA types, no.25 is Laguerre):
//+------------------------------------------------------------------+
//| AllAverages_v3.1.mq4 |
//| Copyright © 2007-13, TrendLaboratory |
//| http://finance.groups.yahoo.com/group/TrendLaboratory |
//| E-mail: [email protected] |
//+------------------------------------------------------------------+
// List of MAs:
// MA_Method= 0: SMA - Simple Moving Average
// MA_Method= 1: EMA - Exponential Moving Average
// MA_Method= 2: Wilder - Wilder Exponential Moving Average
// MA_Method= 3: LWMA - Linear Weighted Moving Average
// MA_Method= 4: SineWMA - Sine Weighted Moving Average
// MA_Method= 5: TriMA - Triangular Moving Average
// MA_Method= 6: LSMA - Least Square Moving Average (or EPMA, Linear Regression Line)
// MA_Method= 7: SMMA - Smoothed Moving Average
// MA_Method= 8: HMA - Hull Moving Average by Alan Hull
// MA_Method= 9: ZeroLagEMA - Zero-Lag Exponential Moving Average
// MA_Method=10: DEMA - Double Exponential Moving Average by Patrick Mulloy
// MA_Method=11: T3_basic - T3 by T.Tillson (original version)
// MA_Method=12: ITrend - Instantaneous Trendline by J.Ehlers
// MA_Method=13: Median - Moving Median
// MA_Method=14: GeoMean - Geometric Mean
// MA_Method=15: REMA - Regularized EMA by Chris Satchwell
// MA_Method=16: ILRS - Integral of Linear Regression Slope
// MA_Method=17: IE/2 - Combination of LSMA and ILRS
// MA_Method=18: TriMAgen - Triangular Moving Average generalized by J.Ehlers
// MA_Method=19: VWMA - Volume Weighted Moving Average
// MA_Method=20: JSmooth - Smoothing by Mark Jurik
// MA_Method=21: SMA_eq - Simplified SMA
// MA_Method=22: ALMA - Arnaud Legoux Moving Average
// MA_Method=23: TEMA - Triple Exponential Moving Average by Patrick Mulloy
// MA_Method=24: T3 - T3 by T.Tillson (correct version)
// MA_Method=25: Laguerre - Laguerre filter by J.Ehlers
// MA_Method=26: MD - McGinley Dynamic
// List of Prices:
// Price = 0 - Close
// Price = 1 - Open
// Price = 2 - High
// Price = 3 - Low
// Price = 4 - Median Price = (High+Low)/2
// Price = 5 - Typical Price = (High+Low+Close)/3
// Price = 6 - Weighted Close = (High+Low+Close*2)/4
// Price = 7 - Heiken Ashi Close
// Price = 8 - Heiken Ashi Open
// Price = 9 - Heiken Ashi High
// Price =10 - Heiken Ashi Low
#property copyright "Copyright © 2007-13, TrendLaboratory"
#property link "http://finance.groups.yahoo.com/group/TrendLaboratory"
#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1 Silver
#property indicator_width1 2
#property indicator_color2 DeepSkyBlue
#property indicator_width2 2
#property indicator_color3 Tomato
#property indicator_width3 2
//----
extern int TimeFrame = 0;
extern int Price = 0;
extern int MA_Period = 14;
extern int MA_Shift = 0;
extern int MA_Method = 0;
extern int Color_Mode = 0;
extern int Sound_Mode = 0; //0-off,1-on(works only with Color_Mode=1)
extern int Sound_Shift = 0; //0-open bar(multiple),1-closed bar(once)
extern string Buy_Sound = "alert.wav";
extern string Sell_Sound = "alert2.wav";
extern string PriceMode = "";
extern string _0 = "Close";
extern string _1 = "Open";
extern string _2 = "High";
extern string _3 = "Low";
extern string _4 = "Median";
extern string _5 = "Typical";
extern string _6 = "Weighted Close";
extern string _7 = "Heiken Ashi Close";
extern string _8 = "Heiken Ashi Open";
extern string _9 = "Heiken Ashi High";
extern string _10 = "Heiken Ashi Low";
extern string MAMode = "";
extern string __0 = "SMA";
extern string __1 = "EMA";
extern string __2 = "Wilder";
extern string __3 = "LWMA";
extern string __4 = "SineWMA";
extern string __5 = "TriMA";
extern string __6 = "LSMA";
extern string __7 = "SMMA";
extern string __8 = "HMA";
extern string __9 = "ZeroLagEMA";
extern string __10 = "DEMA";
extern string __11 = "T3 basic";
extern string __12 = "ITrend";
extern string __13 = "Median";
extern string __14 = "GeoMean";
extern string __15 = "REMA";
extern string __16 = "ILRS";
extern string __17 = "IE/2";
extern string __18 = "TriMAgen";
extern string __19 = "VWMA";
extern string __20 = "JSmooth";
extern string __21 = "SMA_eq";
extern string __22 = "ALMA";
extern string __23 = "TEMA";
extern string __24 = "T3";
extern string __25 = "Laguerre";
extern string __26 = "MD";
double MA[];
double Up[];
double Dn[];
double aPrice[];
//----
double tmp[][2];
double haClose[2], haOpen[2], haHigh[2], haLow[2];
int draw_begin, arraysize;
string IndicatorName, TF, short_name;
int sUp = 0, sDn =0;
datetime prevtime, prevhatime;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//----
IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS));
if(TimeFrame == 0 || TimeFrame < Period()) TimeFrame = Period();
//----
IndicatorBuffers(4);
SetIndexBuffer(0, MA); SetIndexStyle(0,DRAW_LINE); SetIndexShift(0,MA_Shift*TimeFrame/Period());
SetIndexBuffer(1, Up); SetIndexStyle(1,DRAW_LINE); SetIndexShift(1,MA_Shift*TimeFrame/Period());
SetIndexBuffer(2, Dn); SetIndexStyle(2,DRAW_LINE); SetIndexShift(2,MA_Shift*TimeFrame/Period());
SetIndexBuffer(3,aPrice);
draw_begin=2*MathCeil(0.5*(MA_Period+1))*TimeFrame/Period();
SetIndexDrawBegin(0,draw_begin);
SetIndexDrawBegin(1,draw_begin);
SetIndexDrawBegin(2,draw_begin);
//----
switch(MA_Method)
{
case 1 : short_name="EMA("; break;
case 2 : short_name="Wilder("; break;
case 3 : short_name="LWMA("; break;
case 4 : short_name="SineWMA("; break;
case 5 : short_name="TriMA("; break;
case 6 : short_name="LSMA("; break;
case 7 : short_name="SMMA("; break;
case 8 : short_name="HMA("; break;
case 9 : short_name="ZeroLagEMA("; break;
case 10: short_name="DEMA("; arraysize = 2; break;
case 11: short_name="T3 basic("; arraysize = 6; break;
case 12: short_name="InstTrend("; break;
case 13: short_name="Median("; break;
case 14: short_name="GeometricMean("; break;
case 15: short_name="REMA("; break;
case 16: short_name="ILRS("; break;
case 17: short_name="IE/2("; break;
case 18: short_name="TriMA_gen("; break;
case 19: short_name="VWMA("; break;
case 20: short_name="JSmooth("; arraysize = 5; break;
case 21: short_name="SMA_eq("; break;
case 22: short_name="ALMA("; break;
case 23: short_name="TEMA("; arraysize = 4; break;
case 24: short_name="T3("; arraysize = 6; break;
case 25: short_name="Laguerre("; arraysize = 4; break;
case 26: short_name="McGinleyDynamic("; break;
default: MA_Method=0; short_name="SMA(";
}
switch(TimeFrame)
{
case 1 : TF = "M1" ; break;
case 5 : TF = "M5" ; break;
case 15 : TF = "M15"; break;
case 30 : TF = "M30"; break;
case 60 : TF = "H1" ; break;
case 240 : TF = "H4" ; break;
case 1440 : TF = "D1" ; break;
case 10080 : TF = "W1" ; break;
case 43200 : TF = "MN1"; break;
default : TF = "Current";
}
IndicatorName = WindowExpertName();
IndicatorShortName(short_name+MA_Period+")"+" "+TF);
SetIndexLabel(1,short_name+MA_Period+")"+" "+TF+" UpTrend");
SetIndexLabel(2,short_name+MA_Period+")"+" "+TF+" DnTrend");
SetIndexLabel(0,short_name+MA_Period+")"+" "+TF);
//----
ArrayResize(tmp,arraysize);
return(0);
}
//+------------------------------------------------------------------+
//| AllAverages_v3.1 |
//+------------------------------------------------------------------+
int start()
{
int limit, y, i, shift, cnt_bars = IndicatorCounted();
if(cnt_bars > 0) limit = Bars - cnt_bars - 1;
if(cnt_bars < 0) return(0);
if(cnt_bars < 1)
{
limit = Bars - 1;
for(i=Bars-1;i>0;i--)
{
MA[i] = EMPTY_VALUE;
Up[i] = EMPTY_VALUE;
Dn[i] = EMPTY_VALUE;
}
}
//----
if(TimeFrame != Period())
{
limit = MathMax(limit,TimeFrame/Period()+1);
for(shift = 0;shift < limit;shift++)
{
y = iBarShift(NULL,TimeFrame,Time[shift]);
MA[shift] = iCustom(NULL,TimeFrame,IndicatorName,0,Price,MA_Period,MA_Shift,MA_Method,Color_Mode,Sound_Mode,Sound_Shift,Buy_Sound,Sell_Sound,0,y);
if(Color_Mode > 0)
{
Up[shift] = iCustom(NULL,TimeFrame,IndicatorName,0,Price,MA_Period,MA_Shift,MA_Method,Color_Mode,Sound_Mode,Sound_Shift,Buy_Sound,Sell_Sound,1,y);
Dn[shift] = iCustom(NULL,TimeFrame,IndicatorName,0,Price,MA_Period,MA_Shift,MA_Method,Color_Mode,Sound_Mode,Sound_Shift,Buy_Sound,Sell_Sound,2,y);
}
}
return(0);
}
else
{
for(shift=limit;shift>=0;shift--)
{
if(arraysize > 1 && prevtime != Time[shift])
{
for(i=0;i0) double lwma = Sum/Weight;
else lwma = 0;
return(lwma);
}
// MA_Method=4: SineWMA - Sine Weighted Moving Average
double SineWMA(double array[],int per,int bar)
{
double pi = 3.1415926535;
double Sum = 0;
double Weight = 0;
for(int i = 0;i < per;i++)
{
Weight+= MathSin(pi*(i+1)/(per+1));
Sum += array[bar+i]*MathSin(pi*(i+1)/(per+1));
}
if(Weight>0) double swma = Sum/Weight;
else swma = 0;
return(swma);
}
// MA_Method=5: TriMA - Triangular Moving Average
double TriMA(double array[],int per,int bar)
{
double sma;
int len = MathCeil((per+1)*0.5);
double sum=0;
for(int i = 0;i < len;i++)
{
sma = SMA(array,len,bar+i);
sum += sma;
}
double trima = sum/len;
return(trima);
}
// MA_Method=6: LSMA - Least Square Moving Average (or EPMA, Linear Regression Line)
double LSMA(double array[],int per,int bar)
{
double Sum=0;
for(int i=per; i>=1; i--) Sum += (i-(per+1)/3.0)*array[bar+per-i];
double lsma = Sum*6/(per*(per+1));
return(lsma);
}
// MA_Method=7: SMMA - Smoothed Moving Average
double SMMA(double array[],double prev,int per,int bar)
{
if(bar == Bars - per) double smma = SMA(array,per,bar);
else
if(bar < Bars - per)
{
double Sum = 0;
for(int i = 0;i < per;i++) Sum += array[bar+i+1];
smma = (Sum - prev + array[bar])/per;
}
return(smma);
}
// MA_Method=8: HMA - Hull Moving Average by Alan Hull
double HMA(double array[],int per,int bar)
{
double tmp[];
int len = MathSqrt(per);
ArrayResize(tmp,len);
if(bar == Bars - per) double hma = array[bar];
else
if(bar < Bars - per)
{
for(int i=0;i0) double vwma = Sum/Weight;
else vwma = 0;
return(vwma);
}
// MA_Method=20: JSmooth - Smoothing by Mark Jurik
double JSmooth(int num,double price,int per,double pow,int bar)
{
double beta = 0.45*(per-1)/(0.45*(per-1)+2);
double alpha = MathPow(beta,pow);
if(bar == Bars - 2) {tmp[num+4][0] = price; tmp[num+0][0] = price; tmp[num+2][0] = price;}
else
if(bar < Bars - 2)
{
tmp[num+0][0] = (1-alpha)*price + alpha*tmp[num+0][1];
tmp[num+1][0] = (price - tmp[num+0][0])*(1-beta) + beta*tmp[num+1][1];
tmp[num+2][0] = tmp[num+0][0] + tmp[num+1][0];
tmp[num+3][0] = (tmp[num+2][0] - tmp[num+4][1])*MathPow((1-alpha),2) + MathPow(alpha,2)*tmp[num+3][1];
tmp[num+4][0] = tmp[num+4][1] + tmp[num+3][0];
}
return(tmp[num+4][0]);
}
// MA_Method=21: SMA_eq - Simplified SMA
double SMA_eq(double price[],double array[],int per,int bar)
{
if(bar == Bars - per) double sma = SMA(price,per,bar);
else
if(bar < Bars - per) sma = (price[bar] - price[bar+per])/per + array[bar+1];
return(sma);
}
// MA_Method=22: ALMA by Arnaud Legoux / Dimitris Kouzis-Loukas / Anthony Cascino
double ALMA(double price[],int per,double offset,double sigma,int bar)
{
double m = MathFloor(offset * (per - 1));
double s = per/sigma;
double w, sum =0, wsum = 0;
for (int i=0;i < per;i++)
{
w = MathExp(-((i - m)*(i - m))/(2*s*s));
wsum += w;
sum += price[bar+(per-1-i)] * w;
}
if(wsum != 0) double alma = sum/wsum;
return(alma);
}
// MA_Method=23: TEMA - Triple Exponential Moving Average by Patrick Mulloy
double TEMA(double price,int per,double v,int bar)
{
double alpha = 2.0/(per+1);
if(bar == Bars - 2) {tmp[0][0] = price; tmp[1][0] = price; tmp[2][0] = price;}
else
if(bar < Bars - 2)
{
tmp[0][0] = tmp[0][1] + alpha *(price - tmp[0][1]);
tmp[1][0] = tmp[1][1] + alpha *(tmp[0][0] - tmp[1][1]);
tmp[2][0] = tmp[2][1] + alpha *(tmp[1][0] - tmp[2][1]);
tmp[3][0] = tmp[0][0] + v*(tmp[0][0] + v*(tmp[0][0]-tmp[1][0]) - tmp[1][0] - v*(tmp[1][0] - tmp[2][0]));
}
return(tmp[3][0]);
}
// MA_Method=24: T3 by T.Tillson (correct version)
double T3(int num,double price,int per,double v,int bar)
{
double len = MathMax((per + 5.0)/3.0-1,1), dema1, dema2;
if(bar == Bars - 2)
{
double T3 = price;
for(int k=0;k