Navigation´╝ÜHome > Content >

alf_non_lag_bands_mtf_alerts_+_arrows_nmc.mq4

Time: 2016-01-10 | Download file:alf_non_lag_bands_mtf_alerts_+_arrows_nmc.mq4

//+------------------------------------------------------------------+
//|   AdaptiveLaguerreFilter.mq4
//+------------------------------------------------------------------+

#property indicator_chart_window
#property indicator_buffers 5
#property indicator_color1  DarkGray
#property indicator_color2  Red
#property indicator_color3  ForestGreen
#property indicator_color4  Aqua
#property indicator_color5  Magenta
#property indicator_style1  STYLE_DASHDOT
#property indicator_width2  1
#property indicator_width3  1

//
//
//
//
//

extern string TimeFrame        = "current time frame";
extern int    LookBack         = 20;
extern int    Median           = 5;
extern int    Length           = 7;
extern int    PriceType        = PRICE_MEDIAN;
extern double Deviations       = 2;
extern int    arrowSize        = 2;
extern bool   alertsOn         = false;
extern bool   alertsOnCurrent  = true;
extern bool   alertsMessage    = true;
extern bool   alertsSound      = false;
extern bool   alertsNotify     = false;
extern bool   alertsEmail      = false;
extern bool   ArrowsOnFirstBar = true;
extern bool   Interpolate      = true;

//
//
//
//
//

double Filter[];
double UpperFilter[];
double LowerFilter[];
double arrowUp[];
double arrowDn[];
double Diff[];
double L0[];
double L1[];
double L2[];
double L3[];
double sortDiff[];
double alphas[];

//
//
//
//
//

int    timeFrame;
string IndicatorFileName;
bool   ReturningBars;
bool   CalculatingAlf;

//+------------------------------------------------------------------+
//|
//+------------------------------------------------------------------+
//
//
//
//

int init() 
{

   IndicatorBuffers(10);
   SetIndexBuffer(0,Filter);      SetIndexLabel(0,"ALF");
   SetIndexBuffer(1,UpperFilter); SetIndexLabel(1,"UpperALF");
   SetIndexBuffer(2,LowerFilter); SetIndexLabel(2,"LowerALF");
   SetIndexBuffer(3,arrowUp);     SetIndexStyle(3,DRAW_ARROW,0,arrowSize); SetIndexArrow(3,241);
   SetIndexBuffer(4,arrowDn);     SetIndexStyle(4,DRAW_ARROW,0,arrowSize); SetIndexArrow(4,242);
   SetIndexBuffer(5,Diff);
   SetIndexBuffer(6,L0);
   SetIndexBuffer(7,L1);
   SetIndexBuffer(8,L2);
   SetIndexBuffer(9,L3);
   ArrayResize(sortDiff,Median);

   //
   //
   //
   //
   //
         
      IndicatorFileName   = WindowExpertName();
      CalculatingAlf      = (TimeFrame=="CalculateAlf"); if (CalculatingAlf) return(0);
      ReturningBars       = (TimeFrame=="returnBars");   if (ReturningBars)  return(0);
      timeFrame           = stringToTimeFrame(TimeFrame);
   
   //
   //
   //
   //
   //
   
   IndicatorShortName("Adaptive Laguerre filter "+timeFrameToString(timeFrame)+" ("+LookBack+","+Median+","+PriceType+")");
   return (0);
}
  
//+------------------------------------------------------------------+
//|
//+------------------------------------------------------------------+
//
//
//
//
//
//

int start()
{
   int counted_bars = IndicatorCounted();
   int i,limit;
    
   if (counted_bars<0) return(-1);
   if (counted_bars>0) counted_bars--;
      limit = MathMin(Bars-counted_bars,Bars-1);
           if (ReturningBars)  { Filter[0] = limit; return(0); }

   //
   //
   //
   //
   //
   
   if (CalculatingAlf || timeFrame==Period())
   {
      for (i=limit;i>=0;i--)
      {
         double price = iMA(NULL,0,1,0,MODE_SMA,PriceType,i);
         Diff[i] = MathAbs(price - Filter[i+1]);

         //
         //
         //
         //
         //
      
         double HH = Diff[i];
         double LL = Diff[i];
            for (int j=0; j HH) {HH = Diff[i+j];}
               if (Diff[i+j] < LL) {LL = Diff[i+j];}
            }

            double alpha = 0.5;
            if (HH!=LL)
            {
               for (j=0;j=UpperFilter[i] && High[i+1]<=UpperFilter[i+1]) arrowDn[i] = UpperFilter[i] + iATR(NULL,0,20,i)/2.0;
         if ( Low[i]<=LowerFilter[i] &&  Low[i+1]>=LowerFilter[i+1]) arrowUp[i] = LowerFilter[i] - iATR(NULL,0,20,i)/2.0;   
      }
      
      //
      //
      //
      //
      //
    
      if (alertsOn)
      {
        if (alertsOnCurrent)
             int whichBar = 0;
        else     whichBar = 1; 
      
        //
        //
        //
        //
        //
      
        if (arrowUp[whichBar+1] == EMPTY_VALUE && arrowUp[whichBar] != EMPTY_VALUE) doAlert(whichBar,"low price crossing lower band");
        if (arrowDn[whichBar+1] == EMPTY_VALUE && arrowDn[whichBar] != EMPTY_VALUE) doAlert(whichBar,"high price crossing upper band");
      }         
      return(0);
   }  
   
   //
   //
   //
   //
   //
   
   int shift = -1; if (ArrowsOnFirstBar) shift=1;
   if (timeFrame > Period()) limit = MathMax(limit,MathMin(Bars-1,iCustom(NULL,timeFrame,IndicatorFileName,"returnBars",0,0)*timeFrame/Period()));
   for (i=limit;i>=0;i--)
   {
      int y = iBarShift(NULL,timeFrame,Time[i]);
      int x = iBarShift(NULL,timeFrame,Time[i+shift]);
         Filter[i]      = iCustom(NULL,timeFrame,IndicatorFileName,"CalculateAlf",LookBack,Median,Length,PriceType,Deviations,arrowSize,alertsOn,alertsOnCurrent,alertsMessage,alertsSound,alertsNotify,alertsEmail,0,y);
         UpperFilter[i] = iCustom(NULL,timeFrame,IndicatorFileName,"CalculateAlf",LookBack,Median,Length,PriceType,Deviations,arrowSize,alertsOn,alertsOnCurrent,alertsMessage,alertsSound,alertsNotify,alertsEmail,1,y);
         LowerFilter[i] = iCustom(NULL,timeFrame,IndicatorFileName,"CalculateAlf",LookBack,Median,Length,PriceType,Deviations,arrowSize,alertsOn,alertsOnCurrent,alertsMessage,alertsSound,alertsNotify,alertsEmail,2,y);
         
      if (x!=y)
      {
         arrowUp[i]     = iCustom(NULL,timeFrame,IndicatorFileName,"CalculateAlf",LookBack,Median,Length,PriceType,Deviations,arrowSize,alertsOn,alertsOnCurrent,alertsMessage,alertsSound,alertsNotify,alertsEmail,3,y);
         arrowDn[i]     = iCustom(NULL,timeFrame,IndicatorFileName,"CalculateAlf",LookBack,Median,Length,PriceType,Deviations,arrowSize,alertsOn,alertsOnCurrent,alertsMessage,alertsSound,alertsNotify,alertsEmail,4,y);
      }
      else
      {
         arrowUp[i]     = EMPTY_VALUE;
         arrowDn[i]     = EMPTY_VALUE;
      }
       
         //
         //
         //
         //
      
            if (timeFrame <= Period() || y==iBarShift(NULL,timeFrame,Time[i-1])) continue;
            if (!Interpolate) continue;

         //
         //
         //
         //
         //

         datetime time = iTime(NULL,timeFrame,y);
            for(int n = 1; i+n < Bars && Time[i+n] >= time; n++) continue;	
            for(int k = 1; k < n; k++)
            {
               Filter[i+k]      = Filter[i]      + (Filter[i+n]      - Filter[i])      * k/n;
               UpperFilter[i+k] = UpperFilter[i] + (UpperFilter[i+n] - UpperFilter[i]) * k/n;
               LowerFilter[i+k] = LowerFilter[i] + (LowerFilter[i+n] - LowerFilter[i]) * k/n;
            }
   }
   return (0);   
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//


double values[][3];
double prices[][1];

#define Pi       3.14159265358979323846264338327950288
#define _length  0
#define _len     1
#define _weight  2

//
//
//
//
//

double iNoLagMa(double price, int length, double &alphasArray[], int forValue, int i)
{
   if (ArrayRange(prices,0) != Bars) ArrayResize(prices,Bars);
            int r = Bars-i-1;  prices[r][forValue]=price;
   if (length<3 || r<3) return(prices[r][forValue]);
   
   //
   //
   //
   //
   //
   
   if (ArrayRange(values,0)<(forValue+1) || values[forValue][_length] != length)
   {
      double Cycle = 4.0;
      double Coeff = 3.0*Pi;
      int    Phase = length-1;
      
      if (ArrayRange(values,0)0)
   {
      int    len = values[forValue][_len];
      double sum = 0;
           for (k=0; k < len-1; k++) sum += alphasArray[k]*prices[r-k][forValue];      
           return( sum / values[forValue][_weight]);
   }
   else return(0);           
}

//
//
//
//
//

string sTfTable[] = {"M1","M5","M10","M15","M30","H1","H4","D1","W1","MN"};
int    iTfTable[] = {1,5,10,15,30,60,240,1440,10080,43200};

//
//
//
//
//

int stringToTimeFrame(string tfs)
{
   tfs = StringUpperCase(tfs);
   for (int i=ArraySize(iTfTable)-1; i>=0; i--)
         if (tfs==sTfTable[i] || tfs==""+iTfTable[i]) return(MathMax(iTfTable[i],Period()));
                                                      return(Period());
}
string timeFrameToString(int tf)
{
   for (int i=ArraySize(iTfTable)-1; i>=0; i--) 
         if (tf==iTfTable[i]) return(sTfTable[i]);
                              return("");
}

//
//
//
//
//

string StringUpperCase(string str)
{
   string   s = str;

   for (int length=StringLen(str)-1; length>=0; length--)
   {
      int tchar = StringGetChar(s, length);
         if((tchar > 96 && tchar < 123) || (tchar > 223 && tchar < 256))
                     s = StringSetChar(s, length, tchar - 32);
         else if(tchar > -33 && tchar < 0)
                     s = StringSetChar(s, length, tchar + 224);
   }
   return(s);
}

//
//
//
//
//

void doAlert(int forBar, string doWhat)
{
   static string   previousAlert="nothing";
   static datetime previousTime;
   string message;
   
      if (previousAlert != doWhat || previousTime != Time[forBar]) {
          previousAlert  = doWhat;
          previousTime   = Time[forBar];

          //
          //
          //
          //
          //

          message =  StringConcatenate(Symbol()," ",timeFrameToString(timeFrame)," at ",TimeToStr(TimeLocal(),TIME_SECONDS)," alf non lag bands ",doWhat);
             if (alertsMessage) Alert(message);
             if (alertsNotify)  SendNotification(message);
             if (alertsEmail)   SendMail(StringConcatenate(Symbol(), Period(), " alf non lag bands "),message);
             if (alertsSound)   PlaySound("alert2.wav");
      }
}


  
   
   

Recommend