DigitalFilterTF

Download script
Author: Dmitry Yakovlev, Russia,Omsk, WM R865705290089
DigitalFilterTF

This script is a specialized technical analysis tool designed for the MetaTrader platform. Its primary purpose is to filter out "market noise" to reveal underlying price trends, making it easier for traders to identify significant movements while ignoring minor fluctuations.

Core Functionality

The logic operates as a Digital Filter. In non-technical terms, think of this like a sophisticated sieve: it takes in raw, jagged market data (like price changes) and smooths it out into a cleaner path. By removing the "jitter" of the market, the tool helps visualize whether the current momentum is genuinely moving upward or downward.

How the Logic Works

The script processes data through several distinct layers:

1. Signal Smoothing (The Filter) The core engine uses mathematical coefficients to calculate a weighted average of price history. Depending on the settings chosen, it can be configured in different modes:

  • Low-Pass / Band-Pass: Designed to isolate specific frequencies of price movement.
  • High-Pass / Band-Stop: Designed to block out certain types of market noise while letting others through. The results are plotted as a smooth line on the chart, representing the "filtered" version of the price.

2. Trend Identification Once the price is smoothed, the script compares the current filtered value against the previous one. It then assigns a logical state to each point in time:

  • If the filtered price is rising, it marks the trend as positive.
  • If the filtered price is falling, it marks the trend as negative.

3. Visual Output (Trend Coloring) To make these signals easy for a human to read at a glance, the script provides two distinct visual lines:

  • Up Trend: A solid line that appears when the filtered price is moving upward.
  • Down Trend: A solid line that appears when the filtered price is moving downward. The logic ensures that when a trend reverses, the previous trend "finishes," and the new one begins, providing a clear visual transition of market sentiment.

Key User Features

  • Preset Configurations: The script includes pre-defined settings (labeled as FATL, SATL, RFTL, and RSTL). These are professionally balanced combinations of filter parameters designed for different trading styles or timeframes.
  • Price Source Selection: Users can choose which data point the filter should analyze—such as the opening price, closing price, the high of the bar, or the low of the bar.
  • Timeframe Flexibility: The script is capable of handling multiple timeframes, allowing it to interpret price data from various intervals simultaneously.
  • Visual Offsets: It includes a "Bar Shift" feature, which allows the user to move the visual representation forward or backward on the chart for better alignment with their specific analysis needs.

Technical Dependencies

Because this script performs complex mathematical calculations that exceed standard platform capabilities, it relies on external libraries (DLLs). These act as "helper engines" that perform the heavy lifting of the digital signal processing logic behind the scenes.

Price Data Components
Series array that contains close prices for each barSeries array that contains open prices of each barSeries array that contains the highest prices of each barSeries array that contains the lowest prices of each bar
Miscellaneous
Implements a curve of type %1Uses files from the file systemIt reads information from a fileIt writes information to file
13 Views
0 Downloads
0 Favorites
DigitalFilterTF
//------------------------------------------------------------------
//  Digital Filter
//  Dmitry Yakovlev 
//  dmitry_yakovlev@rambler.ru
//  WebMoney R865705290089
//------------------------------------------------------------------
//  Original DF.dll 
//  copyright "Copyright (c) 2005, Sergey Iljukhin, Novosibirsk"
//  "mailto:sergey@tibet.ru"
//------------------------------------------------------------------
#property copyright "Dmitry Yakovlev, Russia,Omsk, WM R865705290089"
#property link      "dmitry_yakovlev@rambler.ru"

#property indicator_chart_window
#property indicator_buffers 4
#property indicator_color1 CLR_NONE
#property indicator_color2 Lime
#property indicator_color3 Red
#property indicator_width1 0
#property indicator_width2 2
#property indicator_width3 2

#import  "shell32.dll"           //Connect a dll (provided with Windows)             
  int ShellExecuteA(int hwnd,string Operation,string File,string Parameters,string Directory,int ShowCmd); 
#import "user32.dll"
  int MessageBoxA(int hWnd ,string lpText,string lpCaption,int uType);
#import "DF.dll"
 int DigitalFilter(int FType, int P1, int D1, int A1, int P2, int D2, int A2, double Ripple, int Delay, double& arr[]); 
#import

/*

  Öèôðîâûå ôèëüòðû äëÿ MetaTrader 4.
 
  Âíèìàíèå! Äëÿ ðàáîòû òðåáóåòñÿ òðè äîïîëíèòåëüíûõ DLL 
  ñîäåðæàùèõ áëîê ìàòåìàòè÷åñêîé îáðàáîòêè - bdsp.dll, lapack.dll, mkl_support.dll,
  êîòîðûå äîëæíû áûòü óñòàíîâëåíû â C:\Windows\System32\ èëè ðÿäîì ñ DF.dll â \experts\libraries\
 
  Ïåðåä èñïîëüçîâàíèåì óáåäèòåñü:
  
  1. ÷òî óñòàíîâëåíû ïóíêòû "Allow DLL import" è "Confirm DLL function's call" â íàñòðîéêàõ Options->Expert Advisors
  2. ×òî â äèðåêòîðèè C:\Windows\System32\ èìåþòñÿ Bdsp.dll, lapack.dll, mkl_support.dll - âñïîìîãàòåëüíûå ìàòåìàòè÷åñêèå áèáëèîòåêè. 
 
  Îïèñàíèå âõîäíûõ ïàðàìåòðîâ:
  
  Ftype - Òèï ôèëüòðà: 0 - ÔÍ× (FATL/SATL/KGLP), 1 - ÔÂ× (KGHP), 
          2 - ïîëîñîâîé (RBCI/KGBP), 3 - ðåæåêòîðíûé (KGBS)
  P1 -    Ïåðèîä îòñå÷êè P1, áàð
  D1 -    Ïåðèîä îòñå÷êè ïåðåõîäíîãî ïðîöåññà D1, áàð
  A1 -    Çàòóõàíèå â ïîëîñå çàäåðæêè À1, äÁ
  P2 -    Ïåðèîä îòñå÷êè P2, áàð
  D2 -    Ïåðèîä îòñå÷êè ïåðåõîäíîãî ïðîöåññà D2, áàð
  A2 -    Çàòóõàíèå â ïîëîñå çàäåðæêè À2, äÁ
  Ripple - Áèåíèÿ â ïîëîñå ïðîïóñêàíèÿ, äÁ
  Delay - Çàäåðæêà, áàð

  Äëÿ ÔÍ× è ÔÂ× çíà÷åíèÿ ïàðàìåòðîé P2,D2,A2 èãíîðèðóþòñÿ
  Óñëîâèÿ ðàáîòû:
  ÔÍ×: P1>D1
  ÔÂ×: P1<D1
  Ïîëîñîâîé è ðåæåêòîðíûé: D2>P2>P1>D1

*/

extern string  _tf="Òàéìôðåéì (0-òåêóùèé)";
extern int     tf=0;
extern string  _pr="Öåíà 0-cl,1-op,2-hi,3-lo";
extern int     pr=0;
extern string  _par="Preset 0-User,1-FATL,2-SATL,3-RFTL,4-RSTL";
extern int     preset=0;
extern string  _filt="Óêàçûâàåì ïàðàìåòðû ôèëüòðà";
extern int     FType=0;
extern int     P1=14;
extern int     D1=10;
extern int     A1=40;
extern int     P2=0;
extern int     D2=0;
extern int     A2=0;
extern int     Delay=0;
extern double  Ripple=0.08; 
extern int     BarShift=0; // Ñäâèã ãðàôèêà, áàð. Ìèíóñ - íàçàä, ïëþñ - âïåðåä
extern string  _donate1="Äëÿ $ áëàãîäàðíîñòè:";
extern string  _donate2="R865705290089";

//---- indicator buffers
double iBuffer[];
double up[];
double dn[];
double trend[];
//------

int FilterSize=0;
double F[1500];
//---

int init()
{
   CheckDonate();
   if(tf<Period()) tf=Period();

   if(preset==1) // FATL
   {
      FType=0;
      P1=14;
      D1=10;
      A1=40;
      P2=0;
      D2=0;
      A2=0;
      Delay=0;
      Ripple=0.08; 
      BarShift=0; // Ñäâèã ãðàôèêà, áàð. Ìèíóñ - íàçàä, ïëþñ - âïåðåä
   }
   if(preset==2) // SATL
   {
      FType=0;
      P1=52;
      D1=32;
      A1=40;
      P2=0;
      D2=0;
      A2=0;
      Delay=0;
      Ripple=0.08; 
      BarShift=0; // Ñäâèã ãðàôèêà, áàð. Ìèíóñ - íàçàä, ïëþñ - âïåðåä
   }
   // --- Parameters: P1=14, D1=10, A1=40
   // --- P2=52, D2=32, A2=40, Ripple=0.0864, Delay=5
   // --- Order [Auto]=40, Calculate method=2
   if(preset==3) // RFTL
   {
      FType=0;
      P1=14;
      D1=10;
      A1=40;
      P2=0;
      D2=0;
      A2=0;
      Delay=5;
      Ripple=0.08; 
      BarShift=0; // Ñäâèã ãðàôèêà, áàð. Ìèíóñ - íàçàä, ïëþñ - âïåðåä
   }
   // --- Parameters: P1=52, D1=32, A1=40
   // --- P2=52, D2=32, A2=40, Ripple=0.0864, Delay=15
   // --- Order [Auto]=97, Calculate method=2
   if(preset==4) // RSTL
   {
      FType=0;
      P1=52;
      D1=32;
      A1=40;
      P2=0;
      D2=0;
      A2=0;
      Delay=15;
      Ripple=0.08; 
      BarShift=0; // Ñäâèã ãðàôèêà, áàð. Ìèíóñ - íàçàä, ïëþñ - âïåðåä
   }

   IndicatorBuffers(4);
   //---- indicator line
   SetIndexStyle(0,DRAW_NONE);
   SetIndexBuffer(0,iBuffer);

   SetIndexStyle(1,DRAW_LINE,STYLE_SOLID,2);
   SetIndexBuffer(1,up);
   SetIndexLabel(1,"Up");

   SetIndexStyle(2,DRAW_LINE,STYLE_SOLID,2);
   SetIndexBuffer(2,dn);
   SetIndexLabel(2,"Dn");
   
   SetIndexStyle(3,DRAW_NONE);
   SetIndexBuffer(3,trend);
   SetIndexLabel(3,"trend");

   ArraySetAsSeries(iBuffer, true); 
   ArraySetAsSeries(trend, true);

   // Âû÷èñëÿåì öèôðîâîé ôèëüòð
   FilterSize=DigitalFilter(FType, P1, D1, A1, P2, D2, A2, Ripple, Delay, F); 

   return(0);
}

double price(int i=0)
{
   if(pr==0) return(iClose(Symbol(),tf,i));
   else if(pr==1) return(iOpen(Symbol(),tf,i));
   else if(pr==2) return(iHigh(Symbol(),tf,i));
   else if(pr==3) return(iLow(Symbol(),tf,i));
}

//+------------------------------------------------------------------+
//| Digital filter main function                                     |
//+------------------------------------------------------------------+
int start()
{
   int k, nn, nn1, i, j, counted_bars=IndicatorCounted();
   int koef=(tf/Period());
   double res=0, step=0;
   // <--- ðàñ÷èòûâàåì çíà÷åíèÿ èíäèêàòîðà
   if (FilterSize>0)
   {
      if(Bars<=FilterSize) return(0);
      //-------------------------------
      i=(Bars-counted_bars+FilterSize-1)/koef+1;
      //-------------------------------
      while(i>=0)
      {
         res=0;
         for (j=0; j<FilterSize; j++)
            res+=F[j]*price(i+j);
         
         nn=i*koef;
         if(nn!=-1) iBuffer[nn]=res;
         
         nn1=(i+1)*koef;
         if(nn!=-1 && nn1!=-1 && iBuffer[nn1]!=EMPTY_VALUE && iBuffer[nn1]!=0)
         {
            step=(iBuffer[nn1]-iBuffer[nn])/koef;
            for(k=1;k<koef;k++) iBuffer[k+nn]=iBuffer[nn]+step*k;
         }
         i--;
      }
   } else return;
   // ðàñ÷èòûâàåì -->
   //-------------------------------------------------------------------------
   // <-- ðàñêðàøèâàåì 
   for(int x=Bars;x>=0;x--)
   {
      trend[x] = trend[x+1];
      if (iBuffer[x]> iBuffer[x+1]) trend[x] =1;
      if (iBuffer[x]< iBuffer[x+1]) trend[x] =-1;
 
      if (trend[x]>0)
      { up[x] = iBuffer[x]; 
        if (trend[x+1]<0) up[x+1]=iBuffer[x+1];
        dn[x] = EMPTY_VALUE;
      }
      else              
      if (trend[x]<0)
      { 
        dn[x] = iBuffer[x]; 
        if (trend[x+1]>0) dn[x+1]=iBuffer[x+1];
        up[x] = EMPTY_VALUE;
      }              
   }
   // ðàñêðàøèâàåì -->
   //-------------------------------------------------------------------------
   return(0);
}

void CheckDonate()
{
   int fd=0; string pay="0"; datetime dt=0;
   string fn="DigitalFilterTF.txt";
   fd=FileOpen(fn,FILE_READ|FILE_CSV,";");
   if(fd>=1)
   {
      pay=FileReadString(fd); if(pay!="0" && pay!="1") pay="0";
      dt=StrToTime(FileReadString(fd));
   }
   else
   {
      dt=TimeCurrent();
      fd=FileOpen(fn,FILE_WRITE|FILE_CSV,";");
      FileWrite(fd,"0",TimeToStr(dt,TIME_DATE));
   }
   FileClose(fd);
   
   if(pay=="0" && (TimeCurrent()-dt)>10*24*60*60) // 5 äíåé
   {
      if(MessageBoxA(0,"Åñëè Âàì ïîíðàâèëñÿ èíäèêàòîð DigitalFilterTF,\n õîòèòå ïîìî÷ü àâòîðó ìàòåðèàëüíî?","Âîïðîñ",4)==6)
      {
         ShellExecuteA(0,"Open","iexplore.exe","wmk:payto?Purse=R865705290089&Amount=100&Desc=Indicator&BringToFront=Y&ExecEvenKeeperIsOffline=Y","",7);
         pay="1";
      }
      dt=TimeCurrent();
      
      fd=FileOpen(fn,FILE_WRITE|FILE_CSV,";");
      FileWrite(fd,pay,TimeToStr(dt,TIME_DATE));
      FileClose(fd);
   }
   FileClose(fd);
}


Comments