#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Yellow
#property indicator_color2 Red
#property strict
extern ENUM_TIMEFRAMES TimeFrame = PERIOD_CURRENT; // Time frame
extern double gamma = 0.7; // Gamma
extern ENUM_APPLIED_PRICE Price_Type = 4; // Price
extern bool alertsOn = false; // Turn alerts on?
extern bool alertsOnCurrent = false; // Alerts on currnt (still opened) bar?
extern bool alertsMessage = true; // Alerts should show pop-up message?
extern bool alertsSound = false; // Alerts should play alert sound?
extern bool alertsPushNotif = false; // Alerts should send push notification?
extern bool alertsEmail = false; // Alerts should send email?
extern bool Interpolate = true; // Interpolate in multi time frame mode?
double Filter[];
double Fir[];
double price[];
double L0[];
double L1[];
double L2[];
double L3[],trend[];
string IndicatorFileName;
bool returnBars;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
IndicatorBuffers(8);
SetIndexLabel(0, "LaguerreFilter");
SetIndexLabel(1, "FIR");
SetIndexBuffer(0, Filter);
SetIndexBuffer(1, Fir);
SetIndexBuffer(2, price);
SetIndexBuffer(3, L0);
SetIndexBuffer(4, L1);
SetIndexBuffer(5, L2);
SetIndexBuffer(6, L3);
SetIndexBuffer(7, trend);
IndicatorFileName = WindowExpertName();
returnBars = TimeFrame==-99;
TimeFrame = MathMax(TimeFrame,_Period);
IndicatorShortName("LaguerreFilter(" + DoubleToStr(gamma, 2) + ") "+(string)TimeFrame);
return(0);
}
int deinit() { return(0); }
int start()
{
int counted_bars=IndicatorCounted();
if(counted_bars<0) return(-1);
if(counted_bars>0) counted_bars--;
int limit = MathMin(Bars-counted_bars,Bars-2);
if (returnBars) { Filter[0] = limit+1; return(0); }
if (TimeFrame != _Period)
{
limit = (int)MathMax(limit,MathMin(Bars-1,iCustom(NULL,TimeFrame,IndicatorFileName,-99,0,0)*TimeFrame/Period()));
for (int i=limit; i>= 0; i--)
{
int y = iBarShift(NULL,TimeFrame,Time[i]);
Filter[i] = iCustom(NULL,TimeFrame,IndicatorFileName,PERIOD_CURRENT,gamma,Price_Type,alertsOn,alertsOnCurrent,alertsMessage,alertsSound,alertsPushNotif,alertsEmail,0,y);
Fir[i] = iCustom(NULL,TimeFrame,IndicatorFileName,PERIOD_CURRENT,gamma,Price_Type,alertsOn,alertsOnCurrent,alertsMessage,alertsSound,alertsPushNotif,alertsEmail,1,y);
if (!Interpolate || (i>0 && y==iBarShift(NULL,TimeFrame,Time[i-1]))) continue;
//
//
//
//
//
int n,j; datetime time = iTime(NULL,TimeFrame,y);
for(n = 1; (i+n)<Bars && Time[i+n] >= time; n++) continue;
for(j = 1; j<n && (i+n)<Bars && (i+j)<Bars; j++)
{
Filter[i+j] = Filter[i] + (Filter[i+n]-Filter[i])*j/n;
Fir[i+j] = Fir[i] + (Fir[i+n] -Fir[i] )*j/n;
}
}
return(0);
}
//
//
//
//
//
for (int i=limit; i>=0; i--)
{
price[i]=iMA(NULL,0,1,0,0,Price_Type,i);
if (i>=Bars-3)
{
L0[i] = price[i];
L1[i] = price[i];
L2[i] = price[i];
L3[i] = price[i];
L0[i] = price[i];
Filter[i] = price[i];
Fir[i] = price[i];
trend[i] = 0;
continue;
}
L0[i] = (1.0 - gamma)*price[i] + gamma*L0[i+1];
L1[i] = -gamma*L0[i] + L0[i+1] + gamma*L1[i+1];
L2[i] = -gamma*L1[i] + L1[i+1] + gamma*L2[i+1];
L3[i] = -gamma*L2[i] + L2[i+1] + gamma*L3[i+1];
Filter[i] = (L0[i] + 2 * L1[i] + 2 * L2[i] + L3[i]) / 6.0;
Fir[i] = (price[i]+2.0*price[i+1]+2.0*price[i+2]+price[i+3]) / 6.0;
trend[i] = trend[i+1];
if (Filter[i]<Fir[i]) trend[i]= 1;
if (Filter[i]>Fir[i]) trend[i]=-1;
}
//
//
//
//
//
if (alertsOn && Bars>2)
{
int whichBar = 1; if (alertsOnCurrent) whichBar = 0;
static string mess1 = "";
static datetime time1 = 0;
if (trend[whichBar] != trend[whichBar+1])
{
if (trend[whichBar] == 1) doAlert(mess1,time1,whichBar,"laguerre filter fir trend changed to up");
if (trend[whichBar] == -1) doAlert(mess1,time1,whichBar,"laguerre filter fir trend changed to down");
}
}
return(0);
}
//-------------------------------------------------------------------
//
//-------------------------------------------------------------------
//
//
//
//
//
string sTfTable[] = {"M1","M5","M15","M30","H1","H4","D1","W1","MN"};
int iTfTable[] = {1,5,15,30,60,240,1440,10080,43200};
string timeFrameToString(int tf)
{
for (int i=ArraySize(iTfTable)-1; i>=0; i--)
if (tf==iTfTable[i]) return(sTfTable[i]);
return("");
}
//
//
//
//
//
void doAlert(string& previousAlert, datetime& previousTime, int forBar, string doWhat)
{
string message;
if (previousAlert != doWhat || previousTime != Time[forBar]) {
previousAlert = doWhat;
previousTime = Time[forBar];
//
//
//
//
//
message = timeFrameToString(_Period)+" - "+Symbol()+" at "+TimeToStr(TimeLocal(),TIME_SECONDS)+" "+doWhat;
if (alertsMessage) Alert(message);
if (alertsEmail) SendMail(Symbol()+" LaguerrFilter fir",message);
if (alertsPushNotif) SendNotification(message);
if (alertsSound) PlaySound("alert2.wav");
}
}